refactor: model list and health check (#7997)

* refactor(ProviderSetting): add a backtop to provider setting

* refactor: decouple ModelList from ProviderSetting

* refactor: move modellist to a single dir

* refactor: allow more props for CollapsibleSearchBar

* refactor: split ModelList into ModelList, ModelListGroup and ModelListItem

* refactor: simplify health check types, improve file structure

* refactor: split HealthStatusIndicator from list items

* refactor: better indicator tooltip

* refactor: improve model search, simplify some expressions

* refactor: further simplify ModelList by extracting onHealthCheck

* refactor: remove double scroller from EditModelsPopup

* revert: remove backtop

* fix: i18n order

* refactor: sort buttons
This commit is contained in:
one
2025-07-21 15:57:08 +08:00
committed by GitHub
parent f13ae2d3c1
commit 2b0c46bfdb
34 changed files with 948 additions and 874 deletions
+6 -2
View File
@@ -42,11 +42,13 @@ In your code, you can call `logger` at any time to record logs. The supported me
For the meaning of each level, please refer to the section below.
The following examples show how to use `logger.info` and `logger.error`. Other levels are used in the same way:
```typescript
logger.info('message', CONTEXT)
logger.info('message %s %d', 'hello', 123, CONTEXT)
logger.error('message', new Error('error message'), CONTEXT)
```
- `message` is a required string. All other options are optional.
- `CONTEXT` as `{ key: value, ... }` is optional and will be recorded in the log file.
- If an `Error` type is passed, the error stack will be automatically recorded.
@@ -57,6 +59,7 @@ logger.error('message', new Error('error message'), CONTEXT)
- In the production environment, the default log level is `info`. Logs are only recorded to the file and are not printed to the terminal.
Changing the log level:
- You can change the log level with `logger.setLevel('newLevel')`.
- `logger.resetLevel()` resets it to the default level.
- `logger.getLevel()` gets the current log level.
@@ -65,7 +68,7 @@ Changing the log level:
## Usage in the `renderer` process
Usage in the `renderer` process for *importing*, *setting module information*, and *setting context information* is **exactly the same** as in the `main` process.
Usage in the `renderer` process for _importing_, _setting module information_, and _setting context information_ is **exactly the same** as in the `main` process.
The following section focuses on the differences.
### `initWindowSource`
@@ -77,6 +80,7 @@ loggerService.initWindowSource('windowName')
```
As a rule, we will set this in the `window`'s `entryPoint.tsx`. This ensures that `windowName` is set before it's used.
- An error will be thrown if `windowName` is not set, and the `logger` will not work.
- `windowName` can only be set once; subsequent attempts to set it will have no effect.
- `windowName` will not be printed in the `devTool`'s `console`, but it will be recorded in the `main` process terminal and the file log.
@@ -109,8 +113,8 @@ logger.setLogToMainLevel('newLevel')
logger.resetLogToMainLevel()
logger.getLogToMainLevel()
```
**Note:** This method has a global effect. Please do not change it arbitrarily in your code unless you are very clear about what you are doing.
**Note:** This method has a global effect. Please do not change it arbitrarily in your code unless you are very clear about what you are doing.
##### Per-log Change
+17 -14
View File
@@ -6,12 +6,11 @@ CherryStudio使用统一的日志服务来打印和记录日志,**若无特殊
以下是详细说明
## 在`main`进程中使用
### 引入
``` typescript
```typescript
import { loggerService } from '@logger'
```
@@ -19,7 +18,7 @@ import { loggerService } from '@logger'
在import头之后,设置:
``` typescript
```typescript
const logger = loggerService.withContext('moduleName')
```
@@ -30,7 +29,7 @@ const logger = loggerService.withContext('moduleName')
`withContext`中,也可以设置其他`CONTEXT`信息:
``` typescript
```typescript
const logger = loggerService.withContext('moduleName', CONTEXT)
```
@@ -43,11 +42,13 @@ const logger = loggerService.withContext('moduleName', CONTEXT)
各级别的含义,请参考下面的章节。
以下以 `logger.info``logger.error` 举例如何使用,其他级别是一样的:
``` typescript
```typescript
logger.info('message', CONTEXT)
logger.info('message %s %d', 'hello', 123, CONTEXT)
logger.error('message', new Error('error message'), CONTEXT)
```
- `message` 是必填的,`string`类型,其他选项都是可选的
- `CONTEXT``{ key: value, ...}` 是可选的,会在日志文件中记录
- 如果传递了`Error`类型,会自动记录错误堆栈
@@ -58,6 +59,7 @@ logger.error('message', new Error('error message'), CONTEXT)
- 生产环境下,默认记录级别为`info`,日志只会记录到文件,不会打印到终端
更改日志记录级别:
- 可以通过 `logger.setLevel('newLevel')` 来更改日志记录级别
- `logger.resetLevel()` 可以重置为默认级别
- `logger.getLevel()` 可以获取当前记录记录级别
@@ -66,7 +68,7 @@ logger.error('message', new Error('error message'), CONTEXT)
## 在`renderer`进程中使用
在`renderer`进程中使用,*引入方法*、*设置`module`信息*、*设置`context`信息的方法*和`main`进程中是**完全一样**的。
`renderer`进程中使用,_引入方法_、_设置`module`信息_、*设置`context`信息的方法*和`main`进程中是**完全一样**的。
下面着重讲一下不同之处。
### `initWindowSource`
@@ -78,6 +80,7 @@ loggerService.initWindowSource('windowName')
```
原则上,我们将在`window``entryPoint.tsx`中进行设置,这可以保证`windowName`在开始使用前已经设置好了。
- 未设置`windowName`会报错,`logger`将不起作用
- `windowName`只能设置一次,重复设置将不生效
- `windowName`不会在`devTool``console`中打印出来,但是会在`main`进程的终端和文件日志中记录
@@ -110,8 +113,8 @@ logger.setLogToMainLevel('newLevel')
logger.resetLogToMainLevel()
logger.getLogToMainLevel()
```
**注意** 该方法是全局生效的,请不要在代码中随意更改,除非你非常清楚自己在做什么
**注意** 该方法是全局生效的,请不要在代码中随意更改,除非你非常清楚自己在做什么
##### 单条更改
@@ -165,11 +168,11 @@ CSLOGGER_MAIN_SHOW_MODULES=MCPService,SelectionService
日志有很多级别,什么时候应该用哪个级别,下面是在CherryStudio中应该遵循的规范:
(按日志级别从高到低排列)
| 日志级别 | 核心定义与使用场景 | 示例 |
| :------------ | :------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`error`** | **严重错误,导致程序崩溃或核心功能无法使用。** <br> 这是最高优的日志,通常需要立即上报或提示用户。 | - 主进程或渲染进程崩溃。 <br> - 无法读写用户关键数据文件(如数据库、配置文件),导致应用无法运行。<br> - 所有未捕获的异常。` |
| **`warn`** | **潜在问题或非预期情况,但不影响程序核心功能。** <br> 程序可以从中恢复或使用备用方案。 | - 配置文件 `settings.json` 缺失,已使用默认配置启动。 <br> - 自动更新检查失败,但不影响当前版本使用。<br> - 某个非核心插件加载失败。` |
| **`info`** | **记录应用生命周期和关键用户行为。** <br> 这是发布版中默认应记录的级别,用于追踪用户的主要操作路径。 | - 应用启动、退出。<br> - 用户成功打开/保存文件。 <br> - 主窗口创建/关闭。<br> - 开始执行一项重要任务(如“开始导出视频”)。` |
| **`verbose`** | **比 `info` 更详细的流程信息,用于追踪特定功能。** <br> 在诊断特定功能问题时开启,帮助理解内部执行流程。 | - 正在加载 `Toolbar` 模块。 <br> - IPC 消息 `open-file-dialog` 已从渲染进程发送。<br> - 正在应用滤镜 'Sepia' 到图像。` |
| 日志级别 | 核心定义与使用场景 | 示例 |
| :------------ | :------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`error`** | **严重错误,导致程序崩溃或核心功能无法使用。** <br> 这是最高优的日志,通常需要立即上报或提示用户。 | - 主进程或渲染进程崩溃。 <br> - 无法读写用户关键数据文件(如数据库、配置文件),导致应用无法运行。<br> - 所有未捕获的异常。` |
| **`warn`** | **潜在问题或非预期情况,但不影响程序核心功能。** <br> 程序可以从中恢复或使用备用方案。 | - 配置文件 `settings.json` 缺失,已使用默认配置启动。 <br> - 自动更新检查失败,但不影响当前版本使用。<br> - 某个非核心插件加载失败。` |
| **`info`** | **记录应用生命周期和关键用户行为。** <br> 这是发布版中默认应记录的级别,用于追踪用户的主要操作路径。 | - 应用启动、退出。<br> - 用户成功打开/保存文件。 <br> - 主窗口创建/关闭。<br> - 开始执行一项重要任务(如“开始导出视频”)。` |
| **`verbose`** | **比 `info` 更详细的流程信息,用于追踪特定功能。** <br> 在诊断特定功能问题时开启,帮助理解内部执行流程。 | - 正在加载 `Toolbar` 模块。 <br> - IPC 消息 `open-file-dialog` 已从渲染进程发送。<br> - 正在应用滤镜 'Sepia' 到图像。` |
| **`debug`** | **开发和调试时使用的详细诊断信息。** <br> **严禁在发布版中默认开启**,因为它可能包含敏感数据并影响性能。 | - 函数 `renderImage` 的入参: `{ width: 800, ... }`。<br> - IPC 消息 `save-file` 收到的具体数据内容。<br> - 渲染进程中 Redux/Vuex 的 state 变更详情。` |
| **`silly`** | **最详尽的底层信息,仅用于极限调试。** <br> 几乎不在常规开发中使用,仅为解决棘手问题。 | - 鼠标移动的实时坐标 `(x: 150, y: 320)`。<br> - 读取文件时每个数据块(chunk)的大小。<br> - 每一次渲染帧的耗时。 |
| **`silly`** | **最详尽的底层信息,仅用于极限调试。** <br> 几乎不在常规开发中使用,仅为解决棘手问题。 | - 鼠标移动的实时坐标 `(x: 150, y: 320)`。<br> - 读取文件时每个数据块(chunk)的大小。<br> - 每一次渲染帧的耗时。 |
@@ -80,7 +80,6 @@ import { ChunkType } from '@renderer/types' // 调整路径
export const createSimpleLoggingMiddleware = (): CompletionsMiddleware => {
return (api: MiddlewareAPI<AiProviderMiddlewareCompletionsContext, [CompletionsParams]>) => {
return (next: (context: AiProviderMiddlewareCompletionsContext, params: CompletionsParams) => Promise<any>) => {
return async (context: AiProviderMiddlewareCompletionsContext, params: CompletionsParams): Promise<void> => {
const startTime = Date.now()