use-chat-setting-schema.tsx


Overview

The use-chat-setting-schema.tsx file defines a custom React hook useChatSettingSchema that provides a comprehensive Zod validation schema for chat settings configuration in a web application. The schema encapsulates various chat-related settings including prompt configurations, knowledge base IDs, LLM (Large Language Model) parameters, reranking options, vector similarity weights, and metadata filters.

This schema is primarily used for form validation purposes, ensuring that user input for chat assistant settings meets the expected structure and constraints. It also integrates translation support for validation messages, adapting error messages dynamically based on localization.


Detailed Explanation

useChatSettingSchema()

Purpose

To create and return a Zod schema object representing the entire form structure for configuring chat assistant settings. This hook leverages other imported schemas and adds localization to validation messages.

Returns

Usage Example

import { useChatSettingSchema } from './use-chat-setting-schema';

function ChatSettingsForm() {
  const chatSettingSchema = useChatSettingSchema();

  // Example form data that needs validation
  const formData = {
    name: "My Assistant",
    icon: "robot",
    kb_ids: ["kb1", "kb2"],
    prompt_config: {
      quote: true,
      keyword: false,
      tts: true,
      system: "System prompt message",
      refine_multiturn: false,
      use_kg: true,
      parameters: [{ key: "param1", optional: false }],
      cross_languages: ["en", "fr"]
    },
    // other fields as per the schema...
  };

  try {
    chatSettingSchema.parse(formData);
    // Valid form data, proceed with submission
  } catch (e) {
    // Handle validation errors
  }

  return <div>/* form UI here */</div>;
}

Schema Structure

1. promptConfigSchema

Defines the structure of the prompt_config object within the form, which configures prompt-related settings for the chat assistant.

Property

Type

Description

Validation/Notes

quote

boolean

Whether quoting is enabled in prompts

Required

keyword

boolean

Whether keyword extraction is enabled

Required

tts

boolean

Whether text-to-speech is enabled

Required

empty_response

string (optional)

Optional string to use when response is empty

Optional

prologue

string (optional)

Optional prologue text prepended to prompts

Optional

system

string

System message text

Required, minimum 1 character; error message localized

refine_multiturn

boolean

Whether to refine multi-turn conversations

Required

use_kg

boolean

Whether to use knowledge graph

Required

parameters

Array of objects

Array of parameter objects with key and optional flag

Required; each object must have a string key and a boolean optional

tavily_api_key

string (optional)

Optional API key

Optional

reasoning

boolean (optional)

Optional flag for enabling reasoning

Optional

cross_languages

Array<string> (optional)

Optional array of language codes

Optional


2. formSchema

Top-level schema representing the entire chat assistant form.

Property

Type

Description

Validation/Notes

name

string

Name of the assistant

Required, min 1 character, localized error message

icon

string

Icon identifier for the assistant

Required

description

string (optional)

Optional description of the assistant

Optional

kb_ids

Array<string>

Array of knowledge base IDs associated with the assistant

Required, minimum length 0 (can be empty), localized message

prompt_config

promptConfigSchema

Nested prompt configuration object

Required

rerankFormSchema

Imported schema spread

Reranking related form schema

Spread into form schema

llm_setting

Object with LlmSettingFieldSchema

LLM (Large Language Model) specific settings

Required

LlmSettingEnabledSchema

Imported schema spread

Schema for enabling/disabling LLM settings

Spread into form schema

llm_id

string (optional)

Optional LLM identifier

Optional

vectorSimilarityWeightSchema

Imported schema spread

Schema for vector similarity weighting

Spread into form schema

similarityThresholdSchema

Imported schema spread

Schema for similarity threshold

Spread into form schema

topnSchema

Imported schema spread

Schema for top-N selection parameters

Spread into form schema

MetadataFilterSchema

Imported schema spread

Schema for metadata filtering

Spread into form schema


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class useChatSettingSchema {
        +t: function
        +promptConfigSchema: object
        +formSchema: object
        +useChatSettingSchema(): ZodObject
    }
    class promptConfigSchema {
        +quote: boolean
        +keyword: boolean
        +tts: boolean
        +empty_response?: string
        +prologue?: string
        +system: string
        +refine_multiturn: boolean
        +use_kg: boolean
        +parameters: Array<{key: string, optional: boolean}>
        +tavily_api_key?: string
        +reasoning?: boolean
        +cross_languages?: Array<string>
    }
    class formSchema {
        +name: string
        +icon: string
        +description?: string
        +kb_ids: Array<string>
        +prompt_config: promptConfigSchema
        +rerankFormSchema: object (spread)
        +llm_setting: object (LlmSettingFieldSchema)
        +LlmSettingEnabledSchema: object (spread)
        +llm_id?: string
        +vectorSimilarityWeightSchema: object (spread)
        +similarityThresholdSchema: object (spread)
        +topnSchema: object (spread)
        +MetadataFilterSchema: object (spread)
    }

    useChatSettingSchema --> promptConfigSchema : defines
    useChatSettingSchema --> formSchema : defines

Summary

use-chat-setting-schema.tsx provides a modular, extensible, and localized Zod schema hook to validate chat assistant settings form data. It integrates multiple feature-specific schemas, supports internationalization, and enforces strict validation on critical fields like system prompts, assistant name, and knowledge base associations. This hook is a key utility in ensuring robust and consistent configuration data for chat assistants within the application.