@lobehub/chat 0.120.2 → 0.120.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/README.md +47 -22
- package/README.zh-CN.md +53 -26
- package/docs/Deployment/Upstream-Sync.md +8 -2
- package/docs/Deployment/Upstream-Sync.zh-CN.md +9 -3
- package/docs/Development/Add-New-Locale.md +9 -1
- package/docs/Development/Add-New-Locale.zh-CN.md +9 -1
- package/docs/Development/Contributing-Guidelines.md +83 -0
- package/docs/Development/Contributing-Guidelines.zh-CN.md +13 -0
- package/docs/Development/Internationalization-Implementation.md +125 -0
- package/docs/Development/Internationalization-Implementation.zh-CN.md +9 -1
- package/docs/Development/State-Management/State-Management-Intro.md +224 -0
- package/docs/Development/State-Management/{index.zh-CN.md → State-Management-Intro.zh-CN.md} +25 -28
- package/docs/Home.md +9 -6
- package/docs/_Sidebar.md +10 -7
- package/locales/ar/common.json +1 -0
- package/locales/ar/setting.json +1 -3
- package/locales/de-DE/common.json +1 -0
- package/locales/de-DE/setting.json +1 -3
- package/locales/en-US/common.json +1 -0
- package/locales/en-US/setting.json +1 -3
- package/locales/es-ES/common.json +1 -0
- package/locales/es-ES/setting.json +1 -3
- package/locales/fr-FR/common.json +1 -0
- package/locales/fr-FR/setting.json +1 -3
- package/locales/it-IT/common.json +1 -0
- package/locales/it-IT/setting.json +1 -3
- package/locales/ja-JP/common.json +1 -0
- package/locales/ja-JP/setting.json +1 -3
- package/locales/ko-KR/common.json +1 -0
- package/locales/ko-KR/setting.json +1 -3
- package/locales/nl-NL/common.json +1 -0
- package/locales/nl-NL/setting.json +1 -3
- package/locales/pl-PL/common.json +1 -0
- package/locales/pl-PL/setting.json +1 -3
- package/locales/pt-BR/common.json +1 -0
- package/locales/pt-BR/setting.json +1 -3
- package/locales/ru-RU/common.json +1 -0
- package/locales/ru-RU/setting.json +1 -3
- package/locales/tr-TR/common.json +1 -0
- package/locales/tr-TR/setting.json +1 -3
- package/locales/vi-VN/common.json +1 -0
- package/locales/vi-VN/setting.json +1 -3
- package/locales/zh-CN/common.json +1 -0
- package/locales/zh-CN/setting.json +1 -3
- package/locales/zh-TW/common.json +1 -0
- package/locales/zh-TW/setting.json +1 -3
- package/next.config.mjs +2 -2
- package/package.json +8 -7
- package/scripts/docsWorkflow/const.ts +1 -1
- package/scripts/docsWorkflow/index.ts +9 -3
- package/scripts/readmeWorkflow/syncAgentIndex.ts +2 -2
- package/scripts/readmeWorkflow/syncPluginIndex.ts +2 -2
- package/src/app/chat/(desktop)/features/ChatHeader.tsx +6 -6
- package/src/app/chat/{features → (desktop)/features/SideBar}/SystemRole/index.tsx +8 -5
- package/src/app/chat/(desktop)/features/{SideBar.tsx → SideBar/index.tsx} +2 -3
- package/src/app/chat/(mobile)/index.tsx +17 -4
- package/src/app/chat/(mobile)/mobile/page.tsx +30 -2
- package/src/app/chat/components/SessionHydration/index.tsx +44 -0
- package/src/app/chat/features/SessionListContent/DefaultMode.tsx +6 -1
- package/src/app/chat/features/SessionListContent/List/Item/Actions.tsx +0 -2
- package/src/app/chat/features/SessionListContent/List/index.tsx +7 -3
- package/src/app/chat/features/TopicListContent/Header.tsx +1 -1
- package/src/app/chat/page.tsx +7 -3
- package/src/app/home/Redirect.tsx +2 -2
- package/src/app/metadata.ts +6 -0
- package/src/app/settings/common/Common.tsx +5 -6
- package/src/app/welcome/features/Banner/index.tsx +6 -7
- package/src/components/StoreHydration/ChatHydration/index.tsx +33 -0
- package/src/config/server.ts +4 -0
- package/src/const/locale.ts +2 -2
- package/src/const/url.ts +1 -1
- package/src/database/models/__tests__/message.test.ts +3 -2
- package/src/database/models/message.ts +13 -9
- package/src/database/schemas/message.ts +1 -1
- package/src/features/ChatInput/ActionBar/Clear.tsx +0 -2
- package/src/features/ChatInput/useChatInput.ts +2 -6
- package/src/features/Conversation/hooks/useInitConversation.ts +0 -2
- package/src/features/Conversation/index.tsx +7 -2
- package/src/features/PluginDetailModal/index.tsx +0 -2
- package/src/features/PluginDevModal/index.tsx +0 -1
- package/src/features/SideBar/TopActions.tsx +5 -5
- package/src/layout/GlobalLayout/Locale.tsx +37 -10
- package/src/layout/GlobalLayout/StoreHydration.tsx +12 -11
- package/src/layout/GlobalLayout/index.tsx +1 -1
- package/src/locales/default/setting.ts +0 -2
- package/src/locales/resources.ts +9 -7
- package/src/store/chat/slices/message/action.ts +1 -1
- package/src/store/chat/store.ts +2 -34
- package/src/store/global/slices/common/action.ts +6 -0
- package/src/store/global/slices/common/initialState.ts +5 -0
- package/src/store/session/index.ts +0 -1
- package/src/store/session/initialState.ts +6 -2
- package/src/store/session/slices/agent/selectors.ts +3 -1
- package/src/store/session/slices/session/action.ts +13 -17
- package/src/store/session/slices/session/selectors/list.ts +7 -0
- package/src/store/session/store.ts +5 -46
- package/src/app/chat/(mobile)/layout.mobile.tsx +0 -17
- package/src/app/chat/(mobile)/mobile/index.tsx +0 -30
- package/src/app/chat/(mobile)/mobile/layout.mobile.tsx +0 -11
- package/src/store/session/hooks/index.ts +0 -4
- package/src/store/session/hooks/useEffectAfterHydrated.ts +0 -27
- package/src/store/session/hooks/useOnFinishHydrationSession.ts +0 -19
- package/src/store/session/hooks/useSessionChatInit.ts +0 -12
- package/src/store/session/hooks/useSessionHydrated.ts +0 -13
- /package/docs/Development/{index.md → Intro.md} +0 -0
- /package/docs/Development/{index.zh-CN.md → Intro.zh-CN.md} +0 -0
- /package/src/app/chat/{features → (desktop)/features/SideBar}/SystemRole/style.ts +0 -0
- /package/src/app/chat/{features → components}/SidebarHeader/index.tsx +0 -0
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
欢迎来到 LobeChat 的代码风格与贡献指南。本指南将帮助您理解我们的代码规范和贡献流程,确保代码的一致性和项目的顺利进行。
|
|
4
4
|
|
|
5
|
+
## TOC
|
|
6
|
+
|
|
7
|
+
- [代码风格](#代码风格)
|
|
8
|
+
- [ESLint](#eslint)
|
|
9
|
+
- [Prettier](#prettier)
|
|
10
|
+
- [remarklint](#remarklint)
|
|
11
|
+
- [stylelint](#stylelint)
|
|
12
|
+
- [贡献流程](#贡献流程)
|
|
13
|
+
- [Gitmoji](#gitmoji)
|
|
14
|
+
- [Semantic Release](#semantic-release)
|
|
15
|
+
- [Commitlint](#commitlint)
|
|
16
|
+
- [如何贡献](#如何贡献)
|
|
17
|
+
|
|
5
18
|
## 代码风格
|
|
6
19
|
|
|
7
20
|
在 LobeChat 中,我们使用 `@lobehub/lint` 程序包来统一代码风格。该程序包内置了 `ESLint`、`Prettier`、`remarklint` 和 `stylelint` 的配置,以确保我们的 JavaScript、Markdown 和 CSS 文件遵循相同的编码标准。
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# Internationalization Implementation Guide
|
|
2
|
+
|
|
3
|
+
Welcome to the LobeChat Internationalization Implementation Guide. This document will guide you through understanding the internationalization mechanism of LobeChat, including file structure and how to add new languages. LobeChat uses `i18next` and `lobe-i18n` as the internationalization solution, aiming to provide users with seamless multilingual support.
|
|
4
|
+
|
|
5
|
+
## TOC
|
|
6
|
+
|
|
7
|
+
- [Internationalization Overview](#internationalization-overview)
|
|
8
|
+
- [File Structure](#file-structure)
|
|
9
|
+
- [Core Implementation Logic](#core-implementation-logic)
|
|
10
|
+
- [Adding Support for New Languages](#adding-support-for-new-languages)
|
|
11
|
+
- [Resources and Further Reading](#resources-and-further-reading)
|
|
12
|
+
|
|
13
|
+
## Internationalization Overview
|
|
14
|
+
|
|
15
|
+
Internationalization (i18n for short) is the process of enabling an application to adapt to different languages and regions. In LobeChat, we support multiple languages and achieve dynamic language switching and content localization through the `i18next` library. Our goal is to provide a localized experience for global users.
|
|
16
|
+
|
|
17
|
+
## File Structure
|
|
18
|
+
|
|
19
|
+
In the LobeChat project, internationalization-related files are organized as follows:
|
|
20
|
+
|
|
21
|
+
- `src/locales/default`: Contains translation files for the default development language (Chinese), which we use as Chinese.
|
|
22
|
+
- `locales`: Contains folders for all supported languages, with each language folder containing the respective translation files generated by lobe-i18n.
|
|
23
|
+
|
|
24
|
+
In the directory structure of `src/locales`, the `default` folder contains the original translation files (Chinese), while each other language folder contains JSON translation files for the respective language. The files in each language folder correspond to the TypeScript files in the `default` folder, ensuring consistency in the structure of translation files across languages.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
src/locales
|
|
28
|
+
├── create.ts
|
|
29
|
+
├── default
|
|
30
|
+
│ ├── chat.ts
|
|
31
|
+
│ ├── common.ts
|
|
32
|
+
│ ├── error.ts
|
|
33
|
+
│ ├── index.ts
|
|
34
|
+
│ ├── market.ts
|
|
35
|
+
│ ├── migration.ts
|
|
36
|
+
│ ├── plugin.ts
|
|
37
|
+
│ ├── setting.ts
|
|
38
|
+
│ ├── tool.ts
|
|
39
|
+
│ └── welcome.ts
|
|
40
|
+
└── resources.ts
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The file structure generated by lobe-i18n is as follows:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
locales
|
|
47
|
+
├── ar
|
|
48
|
+
│ ├── chat.json
|
|
49
|
+
│ ├── common.json
|
|
50
|
+
│ ├── error.json
|
|
51
|
+
│ └── ... (other translation files)
|
|
52
|
+
├── de-DE
|
|
53
|
+
│ ├── chat.json
|
|
54
|
+
│ ├── common.json
|
|
55
|
+
│ ├── error.json
|
|
56
|
+
│ └── ... (other translation files)
|
|
57
|
+
├── en-US
|
|
58
|
+
├── ... (other language directories)
|
|
59
|
+
├── zh-CN
|
|
60
|
+
└── zh-TW
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Core Implementation Logic
|
|
64
|
+
|
|
65
|
+
The internationalization core implementation logic of LobeChat is as follows:
|
|
66
|
+
|
|
67
|
+
- Initialize and configure using the `i18next` library.
|
|
68
|
+
- Automatically detect the user's language preference using `i18next-browser-languagedetector`.
|
|
69
|
+
- Dynamically load translation resources using `i18next-resources-to-backend`.
|
|
70
|
+
- Set the direction of the HTML document (LTR or RTL) based on the user's language preference.
|
|
71
|
+
|
|
72
|
+
Here is a simplified pseudo code example to illustrate the core implementation logic of internationalization in LobeChat:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import i18n from 'i18next';
|
|
76
|
+
import LanguageDetector from 'i18next-browser-languagedetector';
|
|
77
|
+
import resourcesToBackend from 'i18next-resources-to-backend';
|
|
78
|
+
import { isRtlLang } from 'rtl-detect';
|
|
79
|
+
|
|
80
|
+
// Create i18n instance and configure
|
|
81
|
+
const createI18nInstance = (lang) => {
|
|
82
|
+
const i18nInstance = i18n
|
|
83
|
+
.use(LanguageDetector) // Use language detection
|
|
84
|
+
.use(
|
|
85
|
+
resourcesToBackend((language, namespace) => {
|
|
86
|
+
// Dynamically load translation resources for the corresponding language
|
|
87
|
+
return import(`path/to/locales/${language}/${namespace}.json`);
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Listen for language change events and dynamically set document direction
|
|
92
|
+
i18nInstance.on('languageChanged', (language) => {
|
|
93
|
+
const direction = isRtlLang(language) ? 'rtl' : 'ltr';
|
|
94
|
+
document.documentElement.dir = direction; // Set HTML document direction
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Initialize i18n instance
|
|
98
|
+
i18nInstance.init({
|
|
99
|
+
// Relevant configurations
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return i18nInstance;
|
|
103
|
+
};
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
In this example, we demonstrate how to use `i18next` and related plugins to initialize internationalization settings. We dynamically import translation resources and respond to language change events to adjust the text direction of the page. This process provides LobeChat with flexible multilingual support capabilities.
|
|
107
|
+
|
|
108
|
+
## Adding Support for New Languages
|
|
109
|
+
|
|
110
|
+
We have already supported a variety of languages globally through the following efforts:
|
|
111
|
+
|
|
112
|
+
- [✨ feat: adding Arabic Language Support #1049](https://github.com/lobehub/lobe-chat/pull/1049)
|
|
113
|
+
- [🌐 style: Add Vietnamese files and add the vi-VN option in the General Settings #860](https://github.com/lobehub/lobe-chat/pull/860)
|
|
114
|
+
- [🌐 style: support it-IT nl-NL and pl-PL locales #759](https://github.com/lobehub/lobe-chat/pull/759)
|
|
115
|
+
- [🌐 feat(locale): Add fr-FR (#637) #645](https://github.com/lobehub/lobe-chat/pull/645)
|
|
116
|
+
- [🌐 Add russian localy #137](https://github.com/lobehub/lobe-chat/pull/137)
|
|
117
|
+
|
|
118
|
+
To add support for new languages, please refer to the detailed steps in the [New Locale Addition Guide](Add-New-Locale.en-US).
|
|
119
|
+
|
|
120
|
+
## Resources and Further Reading
|
|
121
|
+
|
|
122
|
+
- [i18next Official Documentation](https://www.i18next.com/)
|
|
123
|
+
- [lobe-i18n Tool Description](https://github.com/lobehub/lobe-cli-toolbox/tree/master/packages/lobe-i18n)
|
|
124
|
+
|
|
125
|
+
By following this guide, you can better understand and participate in the internationalization work of LobeChat, providing a seamless multilingual experience for global users.
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 国际化实现指南
|
|
2
2
|
|
|
3
3
|
欢迎阅读 LobeChat 国际化实现指南。本文档将指导你了解 LobeChat 的国际化机制,包括文件结构、如何添加新语种。LobeChat 采用 `i18next` 和 `lobe-i18n` 作为国际化解决方案,旨在为用户提供流畅的多语言支持。
|
|
4
4
|
|
|
5
|
+
## TOC
|
|
6
|
+
|
|
7
|
+
- [国际化概述](#国际化概述)
|
|
8
|
+
- [文件结构](#文件结构)
|
|
9
|
+
- [核心实现逻辑](#核心实现逻辑)
|
|
10
|
+
- [添加新的语言支持](#添加新的语言支持)
|
|
11
|
+
- [资源和进一步阅读](#资源和进一步阅读)
|
|
12
|
+
|
|
5
13
|
## 国际化概述
|
|
6
14
|
|
|
7
15
|
国际化(Internationalization,简称为 i18n)是一个让应用能够适应不同语言和地区的过程。在 LobeChat 中,我们支持多种语言,并通过 `i18next` 库来实现语言的动态切换和内容的本地化。我们的目标是让 LobeChat 能够为全球用户提供本地化的体验。
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# Best Practices for State Management
|
|
2
|
+
|
|
3
|
+
LobeChat differs from traditional CRUD web applications in that it involves a large amount of rich interactive capabilities. Therefore, it is crucial to design a data flow architecture that is easy to develop and maintain. This document will introduce the best practices for data flow management in LobeChat.
|
|
4
|
+
|
|
5
|
+
## TOC
|
|
6
|
+
|
|
7
|
+
- [Key Concepts](#key-concepts)
|
|
8
|
+
- [Hierarchical Structure](#hierarchical-structure)
|
|
9
|
+
- [Best Practices for LobeChat SessionStore Directory Structure](#best-practices-for-lobechat-sessionstore-directory-structure)
|
|
10
|
+
- [Implementation of SessionStore](#implementation-of-sessionstore)
|
|
11
|
+
|
|
12
|
+
## Key Concepts
|
|
13
|
+
|
|
14
|
+
| Concept | Explanation |
|
|
15
|
+
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
16
|
+
| store | The store contains the application's state and actions. It allows access to and modification of the state during application rendering. |
|
|
17
|
+
| state | State refers to the data of the application, storing the current state of the application. Any change in the state will **trigger a re-rendering** to reflect the new state. |
|
|
18
|
+
| action | An action is an operation function that describes the interactive events occurring in the application. Actions are typically triggered by user interactions, network requests, or timers. Actions can be **synchronous** or **asynchronous**. |
|
|
19
|
+
| reducer | A reducer is a pure function that takes the current state and action as parameters and returns a new state. It is used to update the application's state based on the action type. A reducer is a pure function with no side effects, therefore it is always a **synchronous** function. |
|
|
20
|
+
| selector | A selector is a function used to retrieve specific data from the application's state. It takes the application's state as a parameter and returns computed or transformed data. Selectors can combine parts of the state or multiple states to generate derived data. Selectors are commonly used to map the application's state to a component's props for the component's use. |
|
|
21
|
+
| slice | A slice is a concept used to express a part of the data model state. It specifies a state slice and its related state, action, reducer, and selector. Using slices, a large store can be divided into smaller, maintainable subtypes. |
|
|
22
|
+
|
|
23
|
+
## Hierarchical Structure
|
|
24
|
+
|
|
25
|
+
The structure of the Store can vary greatly depending on the complexity:
|
|
26
|
+
|
|
27
|
+
- **Low Complexity**: Generally includes 2 to 5 states and 3 to 4 actions. In this case, the structure usually consists of a `store.ts` and an `initialState.ts`.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
DataFill/store
|
|
31
|
+
├── index.ts
|
|
32
|
+
└── initialState.ts
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **Moderate Complexity**: Typically involves 5 to 15 states and 5 to 10 actions, with the possibility of selectors for derived states and reducers to simplify data changes. The structure usually includes a `store.ts`, an `initialState.ts`, and a `selectors.ts`/`reducer.ts`.
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
IconPicker/store
|
|
39
|
+
├── index.ts
|
|
40
|
+
├── initialState.ts
|
|
41
|
+
├── selectors.ts
|
|
42
|
+
└── store.ts
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
SortableList/store
|
|
47
|
+
├── index.ts
|
|
48
|
+
├── initialState.ts
|
|
49
|
+
├── listDataReducer.ts
|
|
50
|
+
└── store.ts
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
- **Medium Complexity**: Involves 15 to 30 states and 10 to 20 actions, often requiring the use of multiple slices to manage different actions. The following code represents the internal data flow of the `SortableTree` component:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
SortableTree/store
|
|
57
|
+
├── index.ts
|
|
58
|
+
├── initialState.ts
|
|
59
|
+
├── selectors.ts
|
|
60
|
+
├── slices
|
|
61
|
+
│ ├── crudSlice.ts
|
|
62
|
+
│ ├── dndSlice.ts
|
|
63
|
+
│ └── selectionSlice.ts
|
|
64
|
+
├── store.ts
|
|
65
|
+
└── treeDataReducer.ts
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
- **High Complexity**: Involves over 30 states and 20 actions, requiring modular cohesion using slices. Each slice declares its own initState, actions, reducers, and selectors.
|
|
69
|
+
|
|
70
|
+
The directory structure of the previous version of SessionStore for LobeChat, with high complexity, implements a large amount of business logic. However, with the modularization of slices and the fractal architecture, it is easy to find the corresponding modules, making it easy to maintain and iterate on new features.
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
LobeChat SessionStore
|
|
74
|
+
├── index.ts
|
|
75
|
+
├── initialState.ts
|
|
76
|
+
├── selectors.ts
|
|
77
|
+
├── slices
|
|
78
|
+
│ ├── agentConfig
|
|
79
|
+
│ │ ├── action.ts
|
|
80
|
+
│ │ ├── index.ts
|
|
81
|
+
│ │ ├── initialState.ts
|
|
82
|
+
│ │ └── selectors.ts
|
|
83
|
+
│ ├── chat
|
|
84
|
+
│ │ ├── actions
|
|
85
|
+
│ │ │ ├── index.ts
|
|
86
|
+
│ │ │ ├── message.ts
|
|
87
|
+
│ │ │ └── topic.ts
|
|
88
|
+
│ │ ├── index.ts
|
|
89
|
+
│ │ ├── initialState.ts
|
|
90
|
+
│ │ ├── reducers
|
|
91
|
+
│ │ │ ├── message.ts
|
|
92
|
+
│ │ │ └── topic.ts
|
|
93
|
+
│ │ ├── selectors
|
|
94
|
+
│ │ │ ├── chat.ts
|
|
95
|
+
│ │ │ ├── index.ts
|
|
96
|
+
│ │ │ ├── token.ts
|
|
97
|
+
│ │ │ ├── topic.ts
|
|
98
|
+
│ │ │ └── utils.ts
|
|
99
|
+
│ │ └── utils.ts
|
|
100
|
+
│ └── session
|
|
101
|
+
│ ├── action.ts
|
|
102
|
+
│ ├── index.ts
|
|
103
|
+
│ ├── initialState.ts
|
|
104
|
+
│ ├── reducers
|
|
105
|
+
│ │ └── session.ts
|
|
106
|
+
│ └── selectors
|
|
107
|
+
│ ├── export.ts
|
|
108
|
+
│ ├── index.ts
|
|
109
|
+
│ └── list.ts
|
|
110
|
+
└── store.ts
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Based on the provided directory structure of LobeChat SessionStore, we can update the previous document and convert the examples to the implementation of LobeChat's SessionStore. The following is a portion of the updated document:
|
|
114
|
+
|
|
115
|
+
### Best Practices for LobeChat SessionStore Directory Structure
|
|
116
|
+
|
|
117
|
+
In the LobeChat application, session management is a complex functional module, so we use the Slice pattern to organize the data flow. Below is the directory structure of LobeChat SessionStore, where each directory and file has its specific purpose:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
src/store/session
|
|
121
|
+
├── helpers.ts # Helper functions
|
|
122
|
+
├── hooks # Custom React hooks
|
|
123
|
+
│ ├── index.ts # Export file for hooks
|
|
124
|
+
│ ├── useEffectAfterHydrated.ts # Hook for effects after session hydration
|
|
125
|
+
│ ├── useOnFinishHydrationSession.ts # Hook for session hydration completion
|
|
126
|
+
│ ├── useSessionChatInit.ts # Hook for session chat initialization
|
|
127
|
+
│ └── useSessionHydrated.ts # Hook for session hydration status
|
|
128
|
+
├── index.ts # Aggregated export file for SessionStore
|
|
129
|
+
├── initialState.ts # Aggregated initialState for all slices
|
|
130
|
+
├── selectors.ts # Selectors exported from various slices
|
|
131
|
+
├── slices # Separated functional modules
|
|
132
|
+
│ ├── agent # State and operations related to agents
|
|
133
|
+
│ │ ├── action.ts # Action definitions related to agents
|
|
134
|
+
│ │ ├── index.ts # Entry file for agent slice
|
|
135
|
+
│ │ ├── selectors.test.ts # Tests for agent-related selectors
|
|
136
|
+
│ │ └── selectors.ts # Selector definitions related to agents
|
|
137
|
+
│ └── session # State and operations related to sessions
|
|
138
|
+
│ ├── action.test.ts # Tests for session-related actions
|
|
139
|
+
│ ├── action.ts # Action definitions related to sessions
|
|
140
|
+
│ ├── helpers.ts # Helper functions related to sessions
|
|
141
|
+
│ ├── initialState.ts # Initial state for session slice
|
|
142
|
+
│ └── selectors # Session-related selectors and their tests
|
|
143
|
+
│ ├── export.ts # Aggregated export for session selectors
|
|
144
|
+
│ ├── index.ts # Entry file for session selectors
|
|
145
|
+
│ ├── list.test.ts # Tests for list selectors
|
|
146
|
+
│ └── list.ts # Definitions for list-related selectors
|
|
147
|
+
└── store.ts # Creation and usage of SessionStore
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Implementation of SessionStore
|
|
151
|
+
|
|
152
|
+
In LobeChat, the SessionStore is designed as the core module for managing session state and logic. It consists of multiple Slices, with each Slice managing a relevant portion of state and logic. Below is a simplified example of the SessionStore implementation:
|
|
153
|
+
|
|
154
|
+
#### store.ts
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { PersistOptions, devtools, persist, subscribeWithSelector } from 'zustand/middleware';
|
|
158
|
+
import { shallow } from 'zustand/shallow';
|
|
159
|
+
import { devtools } from 'zustand/middleware';
|
|
160
|
+
import { createWithEqualityFn } from 'zustand/traditional';
|
|
161
|
+
|
|
162
|
+
import { SessionStoreState, initialState } from './initialState';
|
|
163
|
+
import { AgentAction, createAgentSlice } from './slices/agent/action';
|
|
164
|
+
import { SessionAction, createSessionSlice } from './slices/session/action';
|
|
165
|
+
|
|
166
|
+
// =============== Aggregate createStoreFn ============ //
|
|
167
|
+
|
|
168
|
+
export type SessionStore = SessionAction & AgentAction & SessionStoreState;
|
|
169
|
+
const createStore: StateCreator<SessionStore, [['zustand/devtools', never]]> = (...parameters) => ({
|
|
170
|
+
...initialState,
|
|
171
|
+
...createAgentSlice(...parameters),
|
|
172
|
+
...createSessionSlice(...parameters),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
// =============== Implement useStore ============ //
|
|
178
|
+
|
|
179
|
+
export const useSessionStore = createWithEqualityFn<SessionStore>()(
|
|
180
|
+
persist(
|
|
181
|
+
subscribeWithSelector(
|
|
182
|
+
devtools(createStore, {
|
|
183
|
+
name: 'LobeChat_Session' + (isDev ? '_DEV' : ''),
|
|
184
|
+
}),
|
|
185
|
+
),
|
|
186
|
+
persistOptions,
|
|
187
|
+
),
|
|
188
|
+
shallow,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
In this `store.ts` file, we create a `useSessionStore` hook that uses the `zustand` library to create a global state manager. We merge the initialState and the state and actions of each Slice to create a complete SessionStore.
|
|
194
|
+
|
|
195
|
+
#### slices/session/action.ts
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { StateCreator } from 'zustand';
|
|
199
|
+
|
|
200
|
+
import { SessionStore } from '@/store/session';
|
|
201
|
+
|
|
202
|
+
export interface SessionActions {
|
|
203
|
+
/**
|
|
204
|
+
* A custom hook that uses SWR to fetch sessions data.
|
|
205
|
+
*/
|
|
206
|
+
useFetchSessions: () => SWRResponse<any>;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export const createSessionSlice: StateCreator<
|
|
210
|
+
SessionStore,
|
|
211
|
+
[['zustand/devtools', never]],
|
|
212
|
+
[],
|
|
213
|
+
SessionAction
|
|
214
|
+
> = (set, get) => ({
|
|
215
|
+
useFetchSessions: () => {
|
|
216
|
+
// ...logic for initializing sessions
|
|
217
|
+
},
|
|
218
|
+
// ...implementation of other actions
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
In the `action.ts` file, we define a `SessionActions` interface to describe session-related actions and implement a `useFetchSessions` function to create these actions. Then, we merge these actions with the initial state to form the session-related Slice.
|
|
223
|
+
|
|
224
|
+
Through this layered and modular approach, we can ensure that LobeChat's SessionStore is clear, maintainable, and easy to extend and test.
|
package/docs/Development/State-Management/{index.zh-CN.md → State-Management-Intro.zh-CN.md}
RENAMED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
LobeChat 不同于传统 CRUD 的网页,存在大量的富交互能力,如何设计一个易于开发与易于维护的数据流架构非常重要。本篇文档将介绍 LobeChat 中的数据流管理最佳实践。
|
|
4
4
|
|
|
5
|
+
## TOC
|
|
6
|
+
|
|
7
|
+
- [概念要素](#概念要素)
|
|
8
|
+
- [结构分层](#结构分层)
|
|
9
|
+
- [LobeChat SessionStore 目录结构最佳实践](#lobechat-sessionstore-目录结构最佳实践)
|
|
10
|
+
- [SessionStore 的实现](#sessionstore-的实现)
|
|
11
|
+
|
|
5
12
|
## 概念要素
|
|
6
13
|
|
|
7
14
|
| 概念名词 | 解释 |
|
|
@@ -105,41 +112,31 @@ LobeChat SessionStore
|
|
|
105
112
|
└── store.ts
|
|
106
113
|
```
|
|
107
114
|
|
|
108
|
-
根据您提供的 LobeChat SessionStore 的目录结构,我们可以更新前面的文档,并将示例转换为 LobeChat 的 SessionStore 的实现案例。以下是转换后的文档部分:
|
|
109
|
-
|
|
110
115
|
### LobeChat SessionStore 目录结构最佳实践
|
|
111
116
|
|
|
112
|
-
在 LobeChat 应用中,由于会话管理是一个复杂的功能模块,因此我们采用了
|
|
117
|
+
在 LobeChat 应用中,由于会话管理是一个复杂的功能模块,因此我们采用了 [slice 模式](https://github.com/pmndrs/zustand/blob/main/docs/guides/slices-pattern.md) 来组织数据流。下面是 LobeChat SessionStore 的目录结构,其中每个目录和文件都有其特定的用途:
|
|
113
118
|
|
|
114
|
-
```
|
|
119
|
+
```fish
|
|
115
120
|
src/store/session
|
|
116
|
-
├── helpers.ts # 辅助函数
|
|
117
|
-
├── hooks # 自定义 React 钩子
|
|
118
|
-
│ ├── index.ts # 钩子的出口文件
|
|
119
|
-
│ ├── useEffectAfterHydrated.ts # 会话加载后的效果钩子
|
|
120
|
-
│ ├── useOnFinishHydrationSession.ts # 会话水合完成钩子
|
|
121
|
-
│ ├── useSessionChatInit.ts # 会话聊天初始化钩子
|
|
122
|
-
│ └── useSessionHydrated.ts # 会话水合状态钩子
|
|
123
121
|
├── index.ts # SessionStore 的聚合导出文件
|
|
124
122
|
├── initialState.ts # 聚合了所有 slice 的 initialState
|
|
125
123
|
├── selectors.ts # 从各个 slices 导出的 selector
|
|
126
|
-
├──
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
│
|
|
131
|
-
│
|
|
132
|
-
│ └──
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
└── store.ts # SessionStore 的创建和使用
|
|
124
|
+
├── store.ts # SessionStore 的创建和使用
|
|
125
|
+
├── helpers.ts # 辅助函数
|
|
126
|
+
└── slices # 各个独立的功能切片
|
|
127
|
+
├── agent # 助理 Slice
|
|
128
|
+
│ ├── action.ts
|
|
129
|
+
│ ├── index.ts
|
|
130
|
+
│ └── selectors.ts
|
|
131
|
+
└── session # 会话 Slice
|
|
132
|
+
├── action.ts
|
|
133
|
+
├── helpers.ts
|
|
134
|
+
├── initialState.ts
|
|
135
|
+
└── selectors
|
|
136
|
+
├── export.ts
|
|
137
|
+
├── list.ts
|
|
138
|
+
└── index.ts
|
|
139
|
+
|
|
143
140
|
```
|
|
144
141
|
|
|
145
142
|
## SessionStore 的实现
|
package/docs/Home.md
CHANGED
|
@@ -17,6 +17,7 @@ LobeChat is a open-source, extensible ([Function Calling][fc-url]), high-perform
|
|
|
17
17
|
### 🤯 Usage
|
|
18
18
|
|
|
19
19
|
- [Custom Agents Guide](https://github.com/lobehub/lobe-chat/wiki/Usage-Agents) | [自定义助手指南](https://github.com/lobehub/lobe-chat/wiki/Usage-Agents.zh-CN)
|
|
20
|
+
- [Plugin Usage](https://github.com/lobehub/lobe-chat/wiki/Plugins) | [插件使用](https://github.com/lobehub/lobe-chat/wiki/Plugins.zh-CN)
|
|
20
21
|
- [Topic Guide](https://github.com/lobehub/lobe-chat/wiki/Usage-Topics) | [话题指南](https://github.com/lobehub/lobe-chat/wiki/Usage-Topics.zh-CN)
|
|
21
22
|
|
|
22
23
|
<br/>
|
|
@@ -27,7 +28,7 @@ LobeChat is a open-source, extensible ([Function Calling][fc-url]), high-perform
|
|
|
27
28
|
- [Deploying with Azure OpenAI](https://github.com/lobehub/lobe-chat/wiki/Deploy-with-Azure-OpenAI) | [使用 Azure OpenAI 部署](https://github.com/lobehub/lobe-chat/wiki/Deploy-with-Azure-OpenAI.zh-CN)
|
|
28
29
|
- [Environment Variables](https://github.com/lobehub/lobe-chat/wiki/Environment-Variable) | [环境变量](https://github.com/lobehub/lobe-chat/wiki/Environment-Variable.zh-CN)
|
|
29
30
|
- [Upstream Sync](https://github.com/lobehub/lobe-chat/wiki/Upstream-Sync) | [自部署保持更新](https://github.com/lobehub/lobe-chat/wiki/Upstream-Sync.zh-CN)
|
|
30
|
-
- [
|
|
31
|
+
- [Frequently Asked Questions](https://github.com/lobehub/lobe-chat/wiki/Common-Error) | [常见问题](https://github.com/lobehub/lobe-chat/wiki/Common-Error.zh-CN)
|
|
31
32
|
- [Data Statistics](https://github.com/lobehub/lobe-chat/wiki/Analytics) | [数据统计](https://github.com/lobehub/lobe-chat/wiki/Analytics.zh-CN)
|
|
32
33
|
|
|
33
34
|
<br/>
|
|
@@ -35,22 +36,24 @@ LobeChat is a open-source, extensible ([Function Calling][fc-url]), high-perform
|
|
|
35
36
|
### ⌨️ Development
|
|
36
37
|
|
|
37
38
|
- [Technical Development Getting Started Guide](https://github.com/lobehub/lobe-chat/wiki/index) | [技术开发上手指南](https://github.com/lobehub/lobe-chat/wiki/index.zh-CN)
|
|
38
|
-
- [
|
|
39
|
+
- [Code Style and Contribution Guidelines](https://github.com/lobehub/lobe-chat/wiki/Contributing-Guidelines) | [代码风格与贡献指南](https://github.com/lobehub/lobe-chat/wiki/Contributing-Guidelines.zh-CN)
|
|
40
|
+
- [Environment Setup Guide](https://github.com/lobehub/lobe-chat/wiki/Setup-Development) | [环境设置指南](https://github.com/lobehub/lobe-chat/wiki/Setup-Development.zh-CN)
|
|
39
41
|
- [Architecture Design](https://github.com/lobehub/lobe-chat/wiki/Architecture) | [架构设计](https://github.com/lobehub/lobe-chat/wiki/Architecture.zh-CN)
|
|
40
42
|
- [Directory Structure](https://github.com/lobehub/lobe-chat/wiki/Folder-Structure) | [目录架构](https://github.com/lobehub/lobe-chat/wiki/Folder-Structure.zh-CN)
|
|
41
|
-
- [State
|
|
42
|
-
- [
|
|
43
|
+
- [Best Practices for State Management](https://github.com/lobehub/lobe-chat/wiki/State-Management-Intro) | [状态管理最佳实践](https://github.com/lobehub/lobe-chat/wiki/State-Management-Intro.zh-CN)
|
|
44
|
+
- [Data Store Selector](https://github.com/lobehub/lobe-chat/wiki/State-Management-Selector) | [数据存储取数模块](https://github.com/lobehub/lobe-chat/wiki/State-Management-Selector.zh-CN)
|
|
43
45
|
- [Conversation API Implementation Logic](https://github.com/lobehub/lobe-chat/wiki/Chat-API) | [会话 API 实现逻辑](https://github.com/lobehub/lobe-chat/wiki/Chat-API.zh-CN)
|
|
44
46
|
- [How to Develop a New Feature](https://github.com/lobehub/lobe-chat/wiki/Feature-Development) | [如何开发一个新功能](https://github.com/lobehub/lobe-chat/wiki/Feature-Development.zh-CN)
|
|
45
|
-
- [
|
|
47
|
+
- [Internationalization Implementation Guide](https://github.com/lobehub/lobe-chat/wiki/Internationalization-Implementation) | [国际化实现指南](https://github.com/lobehub/lobe-chat/wiki/Internationalization-Implementation.zh-CN)
|
|
46
48
|
- [New Locale Guide](https://github.com/lobehub/lobe-chat/wiki/Add-New-Locale) | [新语种添加指南](https://github.com/lobehub/lobe-chat/wiki/Add-New-Locale.zh-CN)
|
|
49
|
+
- [Testing Guide](https://github.com/lobehub/lobe-chat/wiki/Test) | [测试指南](https://github.com/lobehub/lobe-chat/wiki/Test.zh-CN)
|
|
47
50
|
- [Resources and References](https://github.com/lobehub/lobe-chat/wiki/Resources) | [资源与参考](https://github.com/lobehub/lobe-chat/wiki/Resources.zh-CN)
|
|
48
51
|
|
|
49
52
|
<br/>
|
|
50
53
|
|
|
51
54
|
### 🤖 Agents
|
|
52
55
|
|
|
53
|
-
- [Agent Index and Submit](https://github.com/lobehub/lobe-chat-agents) | [
|
|
56
|
+
- [Agent Index and Submit](https://github.com/lobehub/lobe-chat-agents) | [助手索引与提交](https://github.com/lobehub/lobe-chat-agents/blob/main/README.zh-CN.md)
|
|
54
57
|
|
|
55
58
|
<br/>
|
|
56
59
|
|
package/docs/_Sidebar.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
#### 🤯 Usage
|
|
10
10
|
|
|
11
11
|
- [Custom Agents Guide](https://github.com/lobehub/lobe-chat/wiki/Usage-Agents) | [自定义助手指南](https://github.com/lobehub/lobe-chat/wiki/Usage-Agents.zh-CN)
|
|
12
|
+
- [Plugin Usage](https://github.com/lobehub/lobe-chat/wiki/Plugins) | [插件使用](https://github.com/lobehub/lobe-chat/wiki/Plugins.zh-CN)
|
|
12
13
|
- [Topic Guide](https://github.com/lobehub/lobe-chat/wiki/Usage-Topics) | [话题指南](https://github.com/lobehub/lobe-chat/wiki/Usage-Topics.zh-CN)
|
|
13
14
|
|
|
14
15
|
#### 🛳 Self-Hosting
|
|
@@ -17,26 +18,28 @@
|
|
|
17
18
|
- [Deploying with Azure OpenAI](https://github.com/lobehub/lobe-chat/wiki/Deploy-with-Azure-OpenAI) | [使用 Azure OpenAI 部署](https://github.com/lobehub/lobe-chat/wiki/Deploy-with-Azure-OpenAI.zh-CN)
|
|
18
19
|
- [Environment Variables](https://github.com/lobehub/lobe-chat/wiki/Environment-Variable) | [环境变量](https://github.com/lobehub/lobe-chat/wiki/Environment-Variable.zh-CN)
|
|
19
20
|
- [Upstream Sync](https://github.com/lobehub/lobe-chat/wiki/Upstream-Sync) | [自部署保持更新](https://github.com/lobehub/lobe-chat/wiki/Upstream-Sync.zh-CN)
|
|
20
|
-
- [
|
|
21
|
+
- [Frequently Asked Questions](https://github.com/lobehub/lobe-chat/wiki/Common-Error) | [常见问题](https://github.com/lobehub/lobe-chat/wiki/Common-Error.zh-CN)
|
|
21
22
|
- [Data Statistics](https://github.com/lobehub/lobe-chat/wiki/Analytics) | [数据统计](https://github.com/lobehub/lobe-chat/wiki/Analytics.zh-CN)
|
|
22
23
|
|
|
23
24
|
#### ⌨️ Development
|
|
24
25
|
|
|
25
|
-
- [Technical Development Getting Started Guide](https://github.com/lobehub/lobe-chat/wiki/
|
|
26
|
-
- [
|
|
26
|
+
- [Technical Development Getting Started Guide](https://github.com/lobehub/lobe-chat/wiki/Intro) | [技术开发上手指南](https://github.com/lobehub/lobe-chat/wiki/Intro.zh-CN)
|
|
27
|
+
- [Code Style and Contribution Guidelines](https://github.com/lobehub/lobe-chat/wiki/Contributing-Guidelines) | [代码风格与贡献指南](https://github.com/lobehub/lobe-chat/wiki/Contributing-Guidelines.zh-CN)
|
|
28
|
+
- [Environment Setup Guide](https://github.com/lobehub/lobe-chat/wiki/Setup-Development) | [环境设置指南](https://github.com/lobehub/lobe-chat/wiki/Setup-Development.zh-CN)
|
|
27
29
|
- [Architecture Design](https://github.com/lobehub/lobe-chat/wiki/Architecture) | [架构设计](https://github.com/lobehub/lobe-chat/wiki/Architecture.zh-CN)
|
|
28
30
|
- [Directory Structure](https://github.com/lobehub/lobe-chat/wiki/Folder-Structure) | [目录架构](https://github.com/lobehub/lobe-chat/wiki/Folder-Structure.zh-CN)
|
|
29
|
-
- [State
|
|
30
|
-
- [Data Store Selector](https://github.com/lobehub/lobe-chat/wiki/State-Management
|
|
31
|
+
- [Best Practices for State Management](https://github.com/lobehub/lobe-chat/wiki/State-Management-Intro) | [状态管理最佳实践](https://github.com/lobehub/lobe-chat/wiki/State-Management-Intro.zh-CN)
|
|
32
|
+
- [Data Store Selector](https://github.com/lobehub/lobe-chat/wiki/State-Management-Selector) | [数据存储取数模块](https://github.com/lobehub/lobe-chat/wiki/State-Management-Selector.zh-CN)
|
|
31
33
|
- [Conversation API Implementation Logic](https://github.com/lobehub/lobe-chat/wiki/Chat-API) | [会话 API 实现逻辑](https://github.com/lobehub/lobe-chat/wiki/Chat-API.zh-CN)
|
|
32
34
|
- [How to Develop a New Feature](https://github.com/lobehub/lobe-chat/wiki/Feature-Development) | [如何开发一个新功能](https://github.com/lobehub/lobe-chat/wiki/Feature-Development.zh-CN)
|
|
33
|
-
- [
|
|
35
|
+
- [Internationalization Implementation Guide](https://github.com/lobehub/lobe-chat/wiki/Internationalization-Implementation) | [国际化实现指南](https://github.com/lobehub/lobe-chat/wiki/Internationalization-Implementation.zh-CN)
|
|
34
36
|
- [New Locale Guide](https://github.com/lobehub/lobe-chat/wiki/Add-New-Locale) | [新语种添加指南](https://github.com/lobehub/lobe-chat/wiki/Add-New-Locale.zh-CN)
|
|
37
|
+
- [Testing Guide](https://github.com/lobehub/lobe-chat/wiki/Test) | [测试指南](https://github.com/lobehub/lobe-chat/wiki/Test.zh-CN)
|
|
35
38
|
- [Resources and References](https://github.com/lobehub/lobe-chat/wiki/Resources) | [资源与参考](https://github.com/lobehub/lobe-chat/wiki/Resources.zh-CN)
|
|
36
39
|
|
|
37
40
|
#### 🤖 Agents
|
|
38
41
|
|
|
39
|
-
- [Agent Index and Submit](https://github.com/lobehub/lobe-chat-agents) | [
|
|
42
|
+
- [Agent Index and Submit](https://github.com/lobehub/lobe-chat-agents) | [助手索引与提交](https://github.com/lobehub/lobe-chat-agents/blob/main/README.zh-CN.md)
|
|
40
43
|
|
|
41
44
|
#### 🧩 Plugins
|
|
42
45
|
|
package/locales/ar/common.json
CHANGED
package/locales/ar/setting.json
CHANGED