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
ZodObject: A Zod schema object that validates chat settings form data.
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 |
|---|---|---|---|
|
| Whether quoting is enabled in prompts | Required |
|
| Whether keyword extraction is enabled | Required |
|
| Whether text-to-speech is enabled | Required |
|
| Optional string to use when response is empty | Optional |
|
| Optional prologue text prepended to prompts | Optional |
|
| System message text | Required, minimum 1 character; error message localized |
|
| Whether to refine multi-turn conversations | Required |
|
| Whether to use knowledge graph | Required |
|
| Array of parameter objects with | Required; each object must have a string |
|
| Optional API key | Optional |
|
| Optional flag for enabling reasoning | Optional |
|
| Optional array of language codes | Optional |
2. formSchema
Top-level schema representing the entire chat assistant form.
Property | Type | Description | Validation/Notes |
|---|---|---|---|
|
| Name of the assistant | Required, min 1 character, localized error message |
|
| Icon identifier for the assistant | Required |
|
| Optional description of the assistant | Optional |
|
| Array of knowledge base IDs associated with the assistant | Required, minimum length 0 (can be empty), localized message |
|
| Nested prompt configuration object | Required |
| Imported schema spread | Reranking related form schema | Spread into form schema |
| Object with | LLM (Large Language Model) specific settings | Required |
| Imported schema spread | Schema for enabling/disabling LLM settings | Spread into form schema |
|
| Optional LLM identifier | Optional |
| Imported schema spread | Schema for vector similarity weighting | Spread into form schema |
| Imported schema spread | Schema for similarity threshold | Spread into form schema |
| Imported schema spread | Schema for top-N selection parameters | Spread into form schema |
| Imported schema spread | Schema for metadata filtering | Spread into form schema |
Important Implementation Details
Localization:
The hook usesuseTranslate('chat')to get the translation functiontscoped to the chat namespace. This function is used to localize validation error messages for fields likesystemandname.Schema Composition:
The form schema is composed by merging multiple imported schemas (likererankFormSchema,LlmSettingEnabledSchema, etc.) with locally defined schemas (promptConfigSchema). This modular approach allows for flexible, reusable validation logic.Zod Library:
The schema uses thezodlibrary for defining and enforcing type-safe validation rules. This ensures form data integrity before submission or processing.
Interaction with Other Parts of the System
Imported Schemas:
The file imports multiple schemas from other components:LlmSettingEnabledSchema,LlmSettingFieldSchema— from LLM settings components.MetadataFilterSchema— from metadata filter components.rerankFormSchema— from rerank feature components.similarityThresholdSchema,vectorSimilarityWeightSchema— from similarity slider components.topnSchema— from top N item selection components.
These imports indicate that the chat setting schema aggregates validation rules from various specialized modules, ensuring a unified form validation.
Translation Hook:
The hookuseTranslateis imported from common hooks, enabling multilingual support for validation messages.Usage Context:
This schema is likely used in chat assistant creation or configuration forms within the larger application, ensuring that the settings entered by users are valid and complete before being saved or used.
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.