Skip to content

Commit de7368b

Browse files
authored
✨ feat: add new setting for default image num (lobehub#9618)
1 parent 941b6ec commit de7368b

File tree

52 files changed

+8269
-75
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+8269
-75
lines changed

.cursor/rules/add-setting-env.mdc

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
description: Guide for adding environment variables to configure user settings
3+
alwaysApply: false
4+
---
5+
6+
# Adding Environment Variable for User Settings
7+
8+
Add server-side environment variables to configure default values for user settings.
9+
10+
**Priority**: User Custom > Server Env Var > Hardcoded Default
11+
12+
## Steps
13+
14+
### 1. Define Environment Variable
15+
16+
Create `src/envs/<domain>.ts`:
17+
18+
```typescript
19+
import { createEnv } from '@t3-oss/env-nextjs';
20+
import { z } from 'zod';
21+
22+
export const get<Domain>Config = () => {
23+
return createEnv({
24+
server: {
25+
YOUR_ENV_VAR: z.coerce.number().min(MIN).max(MAX).optional(),
26+
},
27+
runtimeEnv: {
28+
YOUR_ENV_VAR: process.env.YOUR_ENV_VAR,
29+
},
30+
});
31+
};
32+
33+
export const <domain>Env = get<Domain>Config();
34+
```
35+
36+
### 2. Update Type (Optional)
37+
38+
**Skip this step if the domain field already exists in `GlobalServerConfig`.**
39+
40+
Add to `packages/types/src/serverConfig.ts`:
41+
42+
```typescript
43+
export interface GlobalServerConfig {
44+
<domain>?: {
45+
<settingName>?: <type>;
46+
};
47+
}
48+
```
49+
50+
**Prefer reusing existing types** from `packages/types/src/user/settings` with `PartialDeep`:
51+
52+
```typescript
53+
import { User<Domain>Config } from './user/settings';
54+
55+
export interface GlobalServerConfig {
56+
<domain>?: PartialDeep<User<Domain>Config>;
57+
}
58+
```
59+
60+
### 3. Assemble Server Config (Optional)
61+
62+
**Skip this step if the domain field already exists in server config.**
63+
64+
In `src/server/globalConfig/index.ts`:
65+
66+
```typescript
67+
import { <domain>Env } from '@/envs/<domain>';
68+
import { cleanObject } from '@/utils/object';
69+
70+
export const getServerGlobalConfig = async () => {
71+
const config: GlobalServerConfig = {
72+
// ...
73+
<domain>: cleanObject({
74+
<settingName>: <domain>Env.YOUR_ENV_VAR,
75+
}),
76+
};
77+
return config;
78+
};
79+
```
80+
81+
If the domain already exists, just add the new field to the existing `cleanObject()`:
82+
83+
```typescript
84+
<domain>: cleanObject({
85+
existingField: <domain>Env.EXISTING_VAR,
86+
<settingName>: <domain>Env.YOUR_ENV_VAR, // Add this line
87+
}),
88+
```
89+
90+
### 4. Merge to User Store (Optional)
91+
92+
**Skip this step if the domain field already exists in `serverSettings`.**
93+
94+
In `src/store/user/slices/common/action.ts`, add to `serverSettings`:
95+
96+
```typescript
97+
const serverSettings: PartialDeep<UserSettings> = {
98+
defaultAgent: serverConfig.defaultAgent,
99+
<domain>: serverConfig.<domain>, // Add this line
100+
// ...
101+
};
102+
```
103+
104+
### 5. Update .env.example
105+
106+
```bash
107+
# <Description> (range/options, default: X)
108+
# YOUR_ENV_VAR=<example>
109+
```
110+
111+
### 6. Update Documentation
112+
113+
Update both English and Chinese documentation:
114+
- `docs/self-hosting/environment-variables/basic.mdx`
115+
- `docs/self-hosting/environment-variables/basic.zh-CN.mdx`
116+
117+
Add new section or subsection with environment variable details (type, description, default, example, range/constraints).
118+
119+
## Type Reuse
120+
121+
**Prefer reusing existing types** from `packages/types/src/user/settings` instead of defining inline types in `serverConfig.ts`.
122+
123+
```typescript
124+
// ✅ Good - reuse existing type
125+
import { UserImageConfig } from './user/settings';
126+
127+
export interface GlobalServerConfig {
128+
image?: PartialDeep<UserImageConfig>;
129+
}
130+
131+
// ❌ Bad - inline type definition
132+
export interface GlobalServerConfig {
133+
image?: {
134+
defaultImageNum?: number;
135+
};
136+
}
137+
```
138+
139+
## Example: AI_IMAGE_DEFAULT_IMAGE_NUM
140+
141+
```typescript
142+
// src/envs/image.ts
143+
export const getImageConfig = () => {
144+
return createEnv({
145+
server: {
146+
AI_IMAGE_DEFAULT_IMAGE_NUM: z.coerce.number().min(1).max(20).optional(),
147+
},
148+
runtimeEnv: {
149+
AI_IMAGE_DEFAULT_IMAGE_NUM: process.env.AI_IMAGE_DEFAULT_IMAGE_NUM,
150+
},
151+
});
152+
};
153+
154+
// packages/types/src/serverConfig.ts
155+
import { UserImageConfig } from './user/settings';
156+
157+
export interface GlobalServerConfig {
158+
image?: PartialDeep<UserImageConfig>;
159+
}
160+
161+
// src/server/globalConfig/index.ts
162+
image: cleanObject({
163+
defaultImageNum: imageEnv.AI_IMAGE_DEFAULT_IMAGE_NUM,
164+
}),
165+
166+
// src/store/user/slices/common/action.ts
167+
const serverSettings: PartialDeep<UserSettings> = {
168+
image: serverConfig.image,
169+
// ...
170+
};
171+
172+
// .env.example
173+
# AI_IMAGE_DEFAULT_IMAGE_NUM=4
174+
```
175+

.cursor/rules/db-migrations.mdc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
globs: packages/database/migrations/**/*
3+
alwaysApply: false
4+
---
5+
6+
# Database Migrations Guide
7+
8+
## Defensive Programming - Use Idempotent Clauses
9+
10+
Always use defensive clauses to make migrations idempotent:
11+
12+
```sql
13+
-- ✅ Good: Idempotent operations
14+
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "avatar" text;
15+
DROP TABLE IF EXISTS "old_table";
16+
CREATE INDEX IF NOT EXISTS "users_email_idx" ON "users" ("email");
17+
ALTER TABLE "posts" DROP COLUMN IF EXISTS "deprecated_field";
18+
19+
-- ❌ Bad: Non-idempotent operations
20+
ALTER TABLE "users" ADD COLUMN "avatar" text;
21+
DROP TABLE "old_table";
22+
CREATE INDEX "users_email_idx" ON "users" ("email");
23+
```
24+
25+
**Important**: After modifying migration SQL (e.g., adding `IF NOT EXISTS` clauses), run `bun run db:generate-client` to update the hash in `packages/database/src/core/migrations.json`.

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ OPENAI_API_KEY=sk-xxxxxxxxx
169169

170170
# FAL_API_KEY=fal-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
171171

172+
########################################
173+
######### AI Image Settings ############
174+
########################################
175+
176+
# Default image generation count (range: 1-20, default: 4)
177+
# AI_IMAGE_DEFAULT_IMAGE_NUM=4
178+
172179
### Nebius ###
173180

174181
# NEBIUS_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

docs/development/database-schema.dbml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ table user_settings {
945945
system_agent jsonb
946946
default_agent jsonb
947947
tool jsonb
948+
image jsonb
948949
}
949950

950951
table users {

docs/self-hosting/advanced/feature-flags.mdx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,30 @@ You can achieve various feature combinations using the above configuration synta
3131
their default values).
3232
</Callout>
3333

34-
| Configuration Item | Description | Default Value |
35-
| ------------------------- | ----------------------------------------------- | ------------- |
36-
| `webrtc_sync` | Enables WebRTC sync functionality. | Disabled |
37-
| `language_model_settings` | Enables language model settings. | Enabled |
38-
| `openai_api_key` | Allows users to customize the OpenAI API Key. | Enabled |
39-
| `openai_proxy_url` | Allows users to customize the OpenAI proxy URL. | Enabled |
40-
| `create_session` | Allows users to create sessions. | Enabled |
41-
| `edit_agent` | Allows users to edit assistants. | Enabled |
42-
| `dalle` | Enables the DALL-E functionality. | Enabled |
43-
| `check_updates` | Allows checking for updates. | Enabled |
44-
| `welcome_suggest` | Displays welcome suggestions. | Enabled |
45-
| `market` | Enables the assistant market functionality. | Enabled |
46-
| `speech_to_text` | Enables speech-to-text functionality. | Enabled |
47-
| `knowledge_base` | Enables the knowledge base functionality. | Enabled |
48-
| `clerk_sign_up` | Enables the Clerk SignUp functionality. | Enabled |
34+
| Configuration Item | Description | Default Value |
35+
| ------------------------- | -------------------------------------------------------------------------------------------------------- | ------------- |
36+
| `check_updates` | Allows checking for updates. | Enabled |
37+
| `pin_list` | Controls pinned agent list display in sidebar. | Disabled |
38+
| `language_model_settings` | Enables language model settings. | Enabled |
39+
| `provider_settings` | Controls model provider settings display. | Enabled |
40+
| `openai_api_key` | Allows users to customize the OpenAI API Key. | Enabled |
41+
| `openai_proxy_url` | Allows users to customize the OpenAI proxy URL. | Enabled |
42+
| `api_key_manage` | Controls access to API key management page (/profile/apikey). | Disabled |
43+
| `create_session` | Allows users to create sessions. | Enabled |
44+
| `edit_agent` | Allows users to edit assistants. | Enabled |
45+
| `plugins` | Controls plugin functionality in chat and agent settings. | Enabled |
46+
| `dalle` | Enables the DALL-E functionality. | Enabled |
47+
| `ai_image` | Controls AI image generation feature and page (/image). | Enabled |
48+
| `speech_to_text` | Enables speech-to-text functionality. | Enabled |
49+
| `token_counter` | Reserved for token counter display. | Enabled |
50+
| `welcome_suggest` | Displays welcome suggestions. | Enabled |
51+
| `changelog` | Controls changelog modal/page display. | Enabled |
52+
| `clerk_sign_up` | Enables the Clerk SignUp functionality. | Enabled |
53+
| `market` | Enables the assistant market functionality. | Enabled |
54+
| `knowledge_base` | Enables the knowledge base functionality. | Enabled |
55+
| `rag_eval` | Controls RAG evaluation feature (/repos/\[id]/evals). | Disabled |
56+
| `cloud_promotion` | Controls cloud service promotion link display in user menu. | Disabled |
57+
| `commercial_hide_github` | Hides GitHub-related links in settings footer (requires commercial license). | Disabled |
58+
| `commercial_hide_docs` | Hides documentation and help menu including changelog, docs, and feedback (requires commercial license). | Disabled |
4959

5060
You can always check the [featureFlags](https://github.com/lobehub/lobe-chat/blob/main/src/config/featureFlags/schema.ts) to get the latest list of feature flags.

docs/self-hosting/advanced/feature-flags.zh-CN.mdx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,30 @@ tags:
2828
关键字,你需要手动控制所有的功能标志(否则它们会采用对应的默认值)。
2929
</Callout>
3030

31-
| 配置项 | 解释 | 默认值 |
32-
| ------------------------- | ----------------------- | --- |
33-
| `webrtc_sync` | 启用 WebRTC 同步功能。 | 关闭 |
34-
| `language_model_settings` | 启用语言模型设置。 | 开启 |
35-
| `openai_api_key` | 允许用户自定义 OpenAI API Key。 | 开启 |
36-
| `openai_proxy_url` | 允许用户自定义 OpenAI 代理 URL。 | 开启 |
37-
| `create_session` | 允许用户创建会话。 | 开启 |
38-
| `edit_agent` | 允许用户编辑助手。 | 开启 |
39-
| `dalle` | 启用 DALL-E 功能。 | 开启 |
40-
| `check_updates` | 允许检查更新。 | 开启 |
41-
| `welcome_suggest` | 显示欢迎建议。 | 开启 |
42-
| `market` | 启用助手市场功能。 | 开启 |
43-
| `speech_to_text` | 启用语音转文本功能。 | 开启 |
44-
| `knowledge_base` | 启用知识库功能。 | 开启 |
45-
| `clerk_sign_up` | 启用 Clerk 注册功能。 | 开启 |
31+
| 配置项 | 解释 | 默认值 |
32+
| ------------------------- | ------------------------------------ | --- |
33+
| `check_updates` | 允许检查更新。 | 开启 |
34+
| `pin_list` | 控制侧边栏中置顶助手列表的显示。 | 关闭 |
35+
| `language_model_settings` | 启用语言模型设置。 | 开启 |
36+
| `provider_settings` | 控制模型供应商设置的显示。 | 开启 |
37+
| `openai_api_key` | 允许用户自定义 OpenAI API Key。 | 开启 |
38+
| `openai_proxy_url` | 允许用户自定义 OpenAI 代理 URL。 | 开启 |
39+
| `api_key_manage` | 控制 API 密钥管理页面 (/profile/apikey) 的访问。 | 关闭 |
40+
| `create_session` | 允许用户创建会话。 | 开启 |
41+
| `edit_agent` | 允许用户编辑助手。 | 开启 |
42+
| `plugins` | 控制聊天和助手设置中的插件功能。 | 开启 |
43+
| `dalle` | 启用 DALL-E 功能。 | 开启 |
44+
| `ai_image` | 控制 AI 图像生成功能和页面 (/image)。 | 开启 |
45+
| `speech_to_text` | 启用语音转文本功能。 | 开启 |
46+
| `token_counter` | 保留用于令牌计数器显示。 | 开启 |
47+
| `welcome_suggest` | 显示欢迎建议。 | 开启 |
48+
| `changelog` | 控制更新日志弹窗 / 页面的显示。 | 开启 |
49+
| `clerk_sign_up` | 启用 Clerk 注册功能。 | 开启 |
50+
| `market` | 启用助手市场功能。 | 开启 |
51+
| `knowledge_base` | 启用知识库功能。 | 开启 |
52+
| `rag_eval` | 控制 RAG 评估功能 (/repos/\[id]/evals)。 | 关闭 |
53+
| `cloud_promotion` | 控制用户菜单中云服务推广链接的显示。 | 关闭 |
54+
| `commercial_hide_github` | 隐藏设置页面底部的 GitHub 相关链接(需要商业授权)。 | 关闭 |
55+
| `commercial_hide_docs` | 隐藏文档和帮助菜单,包括更新日志、文档和反馈(需要商业授权)。 | 关闭 |
4656

4757
你可以随时检查 [featureFlags](https://github.com/lobehub/lobe-chat/blob/main/src/config/featureFlags/schema.ts) 以获取最新的特性标志列表。

docs/self-hosting/environment-variables/basic.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ For specific content, please refer to the [Feature Flags](/docs/self-hosting/adv
152152
- Default: -
153153
- Example: `https://cdn.example.com`
154154

155+
## AI Image
156+
157+
### `AI_IMAGE_DEFAULT_IMAGE_NUM`
158+
159+
- Type: Optional
160+
- Description: Sets the default number of images to generate for AI image generation. Users can still override this value in their settings.
161+
- Default: `4`
162+
- Example: `6`
163+
- Range: `1-20`
164+
165+
This environment variable allows administrators to customize the default image generation count for their deployment. The value must be between 1 and 20. If not set, it defaults to 4. Users can still adjust this value in their personal settings.
166+
155167
## Plugin Service
156168

157169
### `PLUGINS_INDEX_URL`

docs/self-hosting/environment-variables/basic.zh-CN.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ LobeChat 在部署时提供了一些额外的配置项,你可以使用环境
148148
- 默认值:-
149149
- 示例:`https://cdn.example.com`
150150

151+
## AI 图像
152+
153+
### `AI_IMAGE_DEFAULT_IMAGE_NUM`
154+
155+
- 类型:可选
156+
- 描述:设置 AI 图像生成的默认图片数量。用户仍可在个人设置中覆盖此值。
157+
- 默认值:`4`
158+
- 示例:`6`
159+
- 范围:`1-20`
160+
161+
此环境变量允许管理员为其部署自定义默认图片生成数量。值必须在 1 到 20 之间。如果未设置,默认为 4。用户仍可在个人设置中调整此值。
162+
151163
## 插件服务
152164

153165
### `PLUGINS_INDEX_URL`

locales/ar/setting.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@
294294
},
295295
"title": "الإعدادات العامة"
296296
},
297+
"settingImage": {
298+
"defaultCount": {
299+
"desc": "اضبط عدد الصور الافتراضي عند إنشاء مهمة جديدة في لوحة توليد الصور.",
300+
"label": "عدد الصور الافتراضي",
301+
"title": "إعدادات الرسم بالذكاء الاصطناعي"
302+
}
303+
},
297304
"settingModel": {
298305
"enableMaxTokens": {
299306
"title": "تمكين الحد الأقصى للردود"
@@ -549,6 +556,7 @@
549556
"common": "إعدادات عامة",
550557
"experiment": "تجربة",
551558
"hotkey": "اختصارات لوحة المفاتيح",
559+
"image": "الرسم بالذكاء الاصطناعي",
552560
"llm": "نموذج اللغة",
553561
"provider": "مزود خدمة الذكاء الاصطناعي",
554562
"proxy": "وكيل الشبكة",

locales/bg-BG/setting.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@
294294
},
295295
"title": "Общи настройки"
296296
},
297+
"settingImage": {
298+
"defaultCount": {
299+
"desc": "Задайте броя на изображенията по подразбиране, които да се генерират при създаване на нова задача в панела за генериране на изображения.",
300+
"label": "Брой изображения по подразбиране",
301+
"title": "Настройки за AI рисуване"
302+
}
303+
},
297304
"settingModel": {
298305
"enableMaxTokens": {
299306
"title": "Активиране на ограничението за максимален брой токени"
@@ -549,6 +556,7 @@
549556
"common": "Общи настройки",
550557
"experiment": "Експеримент",
551558
"hotkey": "Бързи клавиши",
559+
"image": "AI рисуване",
552560
"llm": "Езиков модел",
553561
"provider": "AI доставчик",
554562
"proxy": "Мрежов прокси",

0 commit comments

Comments
 (0)