Compare commits
140 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c980dd1c41 | |||
| 06b2b76963 | |||
| cd9f7848c1 | |||
| c5dbde3912 | |||
| 786331d3f4 | |||
| 378dceefa4 | |||
| 809a60d90c | |||
| 24f47f83f9 | |||
| c3d386691a | |||
| 139323ffc1 | |||
| c9019f23bf | |||
| 8cf8c418bc | |||
| 51ba55dfb4 | |||
| 0db1f753a0 | |||
| 624ab716e6 | |||
| e148fd9c97 | |||
| b652a8fb0a | |||
| 62dd97b688 | |||
| 6481f0bb7a | |||
| 0e39773557 | |||
| e58affe613 | |||
| 0797fac217 | |||
| adbd822851 | |||
| a31f6167bb | |||
| 6ce07b21be | |||
| 908b4be918 | |||
| 50c7570440 | |||
| a021d0ee0e | |||
| 72925ecd0c | |||
| 0633286211 | |||
| e93d27b14d | |||
| e7c0372191 | |||
| 45eae70926 | |||
| 7cec77ae2c | |||
| f7ef381bbb | |||
| 19edff11d7 | |||
| 7cbea6da8a | |||
| 65259e566c | |||
| 09592304f8 | |||
| 19ab3fef16 | |||
| 4884f26ec5 | |||
| b07803d6a8 | |||
| 3925d15fa2 | |||
| 010280afdd | |||
| f2e79fe809 | |||
| cde421edc7 | |||
| f1ac9bf38c | |||
| 8ae66242b5 | |||
| b9c489a115 | |||
| 62f83a6230 | |||
| abcc820239 | |||
| 17f56df3cf | |||
| 66c6e506dc | |||
| 9ad33bdc6e | |||
| 86bac0654c | |||
| a4281e53ef | |||
| a9ed1a634d | |||
| 16dcf8edcd | |||
| 8aeb49fd2d | |||
| 946cd085ac | |||
| 6ba03bf8c3 | |||
| 9e532232d7 | |||
| e76ade32e3 | |||
| a7b89493e4 | |||
| bcc9c54356 | |||
| e53a8db7c9 | |||
| e2ad515379 | |||
| f542b49955 | |||
| 77259e2245 | |||
| d0385e25f1 | |||
| bd9c6e37fc | |||
| 0df9aff7db | |||
| 8a000ac0d8 | |||
| 77f82a37a0 | |||
| 4ceb2ec3ab | |||
| 554fa612b5 | |||
| f9f994f9ff | |||
| 3fa2cc1ec2 | |||
| 6e83440d68 | |||
| 9f1ae50f71 | |||
| e1c4a934dc | |||
| d8d1cc6ddd | |||
| 05c70c6388 | |||
| 0758d68d74 | |||
| 26d3c3eabd | |||
| ef2e2dd1c0 | |||
| d4b7668823 | |||
| 0adcf550d9 | |||
| 5a5484b145 | |||
| 558eac8c21 | |||
| 1c67cf3e05 | |||
| 8b6fac26d4 | |||
| e1d318f56d | |||
| 632d687b63 | |||
| 7e721ba86d | |||
| a8ffdefcdd | |||
| 58f4e0ed8d | |||
| 3b48bf4551 | |||
| 48d0e01434 | |||
| 7d82bb16b9 | |||
| 9923a38d84 | |||
| 97588e6cf4 | |||
| acfe5ea8b0 | |||
| ef6651e305 | |||
| 059e78ba2c | |||
| 765198e00f | |||
| 2cda3e77bc | |||
| 6d9278a018 | |||
| e118e0fa7f | |||
| 7687b21ff0 | |||
| 387ac1e778 | |||
| 6e444c6e5e | |||
| 3d29f8324a | |||
| 9d04179123 | |||
| d598f68313 | |||
| 1cec875a8d | |||
| 336957ec63 | |||
| e678340e57 | |||
| 8905367222 | |||
| 76f3dddf85 | |||
| 5eac228e01 | |||
| 76d546305b | |||
| da928c78dc | |||
| 52c2fdb6db | |||
| 2dd5a72ccd | |||
| f3ab6b8bd7 | |||
| 5ecf59e7e7 | |||
| d0ea7aa45c | |||
| f8ab18d8da | |||
| 965d2829eb | |||
| 33e9767c16 | |||
| 70e54c98bf | |||
| 49f1b97b67 | |||
| 0ed5a6b5ec | |||
| bec44875f7 | |||
| f849d0e102 | |||
| 15102da85d | |||
| 7eee6d1cb2 | |||
| b0e8c4fbb8 | |||
| f1468b7d5a |
@@ -1,9 +1,8 @@
|
||||
---
|
||||
description:
|
||||
description:
|
||||
globs: src/services/**/*,src/database/**/*,src/server/**/*
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
# LobeChat 后端技术架构指南
|
||||
|
||||
本指南旨在阐述 LobeChat 项目的后端分层架构,重点介绍各核心目录的职责以及它们之间的协作方式。
|
||||
@@ -30,21 +29,24 @@ LobeChat 的后端设计注重模块化、可测试性和灵活性,以适应
|
||||
其主要分层如下:
|
||||
|
||||
1. 客户端服务层 (`src/services`):
|
||||
|
||||
- 位于 src/services/。
|
||||
- 这是客户端业务逻辑的核心层,负责封装各种业务操作和数据处理逻辑。
|
||||
- 环境适配: 根据不同的运行环境,服务层会选择合适的数据访问方式:
|
||||
- 本地数据库模式: 直接调用 `Model` 层进行数据操作,适用于浏览器 PGLite 和本地 Electron 应用。
|
||||
- 远程数据库模式: 通过 `tRPC` 客户端调用服务端 API,适用于需要云同步的场景。
|
||||
- 本地数据库模式: 直接调用 `Model` 层进行数据操作,适用于浏览器 PGLite 和本地 Electron 应用。
|
||||
- 远程数据库模式: 通过 `tRPC` 客户端调用服务端 API,适用于需要云同步的场景。
|
||||
- 类型转换: 对于简单的数据类型转换,直接在此层进行类型断言,如 `this.pluginModel.query() as Promise<LobeTool[]>`
|
||||
- 每个服务模块通常包含 `client.ts`(本地模式)、`server.ts`(远程模式)和 `type.ts`(接口定义)文件,在实现时应该确保本地模式和远程模式业务逻辑实现一致,只是数据库不同。
|
||||
|
||||
2. API 接口层 (`TRPC`):
|
||||
|
||||
- 位于 src/server/routers/
|
||||
- 使用 `tRPC` 构建类型安全的 API。Router 根据运行时环境(如 Edge Functions, Node.js Lambda)进行组织。
|
||||
- 负责接收客户端请求,并将其路由到相应的 `Service` 层进行处理。
|
||||
- 新建 lambda 端点时可以参考 src/server/routers/lambda/\_template.ts
|
||||
|
||||
3. 仓库层 (`Repositories`):
|
||||
|
||||
- 位于 src/database/repositories/。
|
||||
- 主要处理复杂的跨表查询和数据聚合逻辑,特别是当需要从多个 `Model` 获取数据并进行组合时。
|
||||
- 与 `Model` 层不同,`Repository` 层专注于复杂的业务查询场景,而不涉及简单的领域模型转换。
|
||||
@@ -52,6 +54,7 @@ LobeChat 的后端设计注重模块化、可测试性和灵活性,以适应
|
||||
- 如果数据操作简单(仅涉及单个 `Model`),则通常直接在 `src/services` 层调用 `Model` 并进行简单的类型断言。
|
||||
|
||||
4. 模型层 (`Models`):
|
||||
|
||||
- 位于 src/database/models/ (例如 src/database/models/plugin.ts 和 src/database/models/document.ts)。
|
||||
- 提供对数据库中各个表(由 src/database/schemas/ 中的 Drizzle ORM schema 定义)的基本 CRUD (创建、读取、更新、删除) 操作和简单的查询能力。
|
||||
- `Model` 类专注于单个数据表的直接操作,不涉及复杂的领域模型转换,这些转换通常在上层的 `src/services` 中通过类型断言完成。
|
||||
@@ -62,11 +65,11 @@ LobeChat 的后端设计注重模块化、可测试性和灵活性,以适应
|
||||
- 客户端模式 (浏览器/PWA): 使用 PGLite (基于 WASM 的 PostgreSQL),数据存储在用户浏览器本地。
|
||||
- 服务端模式 (云部署): 使用远程 PostgreSQL 数据库。
|
||||
- Electron 桌面应用:
|
||||
- Electron 客户端会启动一个本地 Node.js 服务。
|
||||
- 本地服务通过 `tRPC` 与 Electron 的渲染进程通信。
|
||||
- 数据库选择依赖于是否开启云同步功能:
|
||||
- 云同步开启: 连接到远程 PostgreSQL 数据库。
|
||||
- 云同步关闭: 使用 PGLite (通过 Node.js 的 WASM 实现) 在本地存储数据。
|
||||
- Electron 客户端会启动一个本地 Node.js 服务。
|
||||
- 本地服务通过 `tRPC` 与 Electron 的渲染进程通信。
|
||||
- 数据库选择依赖于是否开启云同步功能:
|
||||
- 云同步开启: 连接到远程 PostgreSQL 数据库。
|
||||
- 云同步关闭: 使用 PGLite (通过 Node.js 的 WASM 实现) 在本地存储数据。
|
||||
|
||||
## 数据流向说明
|
||||
|
||||
@@ -90,87 +93,8 @@ UI (Electron Renderer) → Zustand action → Client Service -> TRPC Client →
|
||||
|
||||
## 服务层 (Server Services)
|
||||
|
||||
- 位于 src/server/services/。
|
||||
- 核心职责是封装独立的、可复用的业务逻辑单元。这些服务应易于测试。
|
||||
- 平台差异抽象: 一个关键特性是通过其内部的 `impls` 子目录(例如 src/server/services/file/impls 包含 s3.ts 和 local.ts)来抹平不同运行环境带来的差异(例如云端使用 S3 存储,桌面版使用本地文件系统)。这使得上层(如 `tRPC` routers)无需关心底层具体实现。
|
||||
- 目标是使 `tRPC` router 层的逻辑尽可能纯粹,专注于请求处理和业务流程编排。
|
||||
- 服务可能会调用 `Repository` 层或直接调用 `Model` 层进行数据持久化和检索,也可能调用其他服务。
|
||||
|
||||
## 最佳实践 (Best Practices)
|
||||
|
||||
### 数据库操作封装原则
|
||||
|
||||
**连续的数据库操作应该封装到 Model 层**
|
||||
|
||||
当业务逻辑涉及多个相关的数据库操作时,建议将这些操作封装到 Model 层中,而不是在上层(Service 或 Router 层)中进行多次数据库调用。
|
||||
|
||||
**优势:**
|
||||
|
||||
- **代码复用**: Client DB 环境的 service 实现和 Server DB 的 lambda 层实现可以复用相同的 Model 方法
|
||||
- **事务一致性**: 相关的数据库操作可以在同一个方法中管理,便于维护数据一致性
|
||||
- **性能优化**: 减少数据库连接次数,提高查询效率
|
||||
- **职责清晰**: Model 层专注数据访问,上层专注业务协调
|
||||
|
||||
**示例:**
|
||||
|
||||
```typescript
|
||||
// ✅ 推荐:在 Model 层封装连续的数据库操作
|
||||
class GenerationBatchModel {
|
||||
async delete(id: string): Promise<{ deletedBatch: BatchItem; thumbnailUrls: string[] }> {
|
||||
// 1. 查询相关数据
|
||||
const batchWithGenerations = await this.db.query.generationBatches.findFirst({...});
|
||||
|
||||
// 2. 收集需要处理的数据
|
||||
const thumbnailUrls = [...];
|
||||
|
||||
// 3. 执行删除操作
|
||||
const [deletedBatch] = await this.db.delete(generationBatches)...;
|
||||
|
||||
return { deletedBatch, thumbnailUrls };
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ 上层使用简洁
|
||||
const { thumbnailUrls } = await model.delete(id);
|
||||
await fileService.deleteFiles(thumbnailUrls);
|
||||
```
|
||||
|
||||
### 文件操作与数据库操作的执行顺序
|
||||
|
||||
**删除操作原则:数据库删除在前,文件删除在后**
|
||||
|
||||
当业务逻辑同时涉及数据库记录和文件系统操作时,应该遵循"数据库优先"的原则。
|
||||
|
||||
**原因:**
|
||||
|
||||
- **用户体验优先**: 如果先删除文件再删除数据库记录,可能出现文件已删除但数据库记录仍存在的情况,用户访问时会遇到文件不存在的错误
|
||||
- **影响程度较小**: 如果先删除数据库记录再删除文件,即使文件删除失败,用户也看不到这个记录,只是造成一些存储空间浪费,对用户体验影响更小
|
||||
- **数据一致性**: 数据库记录是业务逻辑的核心,应该优先保证其一致性
|
||||
|
||||
**示例:**
|
||||
|
||||
```typescript
|
||||
// ✅ 推荐:先删除数据库记录,再删除文件
|
||||
async deleteGeneration(id: string) {
|
||||
// 1. 先删除数据库记录
|
||||
const deletedGeneration = await generationModel.delete(id);
|
||||
|
||||
// 2. 再删除相关文件
|
||||
if (deletedGeneration.asset?.thumbnailUrl) {
|
||||
await fileService.deleteFile(deletedGeneration.asset.thumbnailUrl);
|
||||
}
|
||||
}
|
||||
|
||||
// ❌ 不推荐:先删除文件
|
||||
async deleteGeneration(id: string) {
|
||||
const generation = await generationModel.findById(id);
|
||||
|
||||
// 如果这里删除成功,但后面数据库删除失败,用户会遇到访问错误
|
||||
await fileService.deleteFile(generation.asset.thumbnailUrl);
|
||||
await generationModel.delete(id); // 可能失败
|
||||
}
|
||||
```
|
||||
|
||||
**创建操作原则:数据库创建在前,文件操作在后**
|
||||
|
||||
创建操作同样应该优先处理数据库记录,确保数据的一致性和完整性。
|
||||
- 位于 src/server/services/。
|
||||
- 核心职责是封装独立的、可复用的业务逻辑单元。这些服务应易于测试。
|
||||
- 平台差异抽象: 一个关键特性是通过其内部的 `impls` 子目录(例如 src/server/services/file/impls 包含 s3.ts 和 local.ts)来抹平不同运行环境带来的差异(例如云端使用 S3 存储,桌面版使用本地文件系统)。这使得上层(如 `tRPC` routers)无需关心底层具体实现。
|
||||
- 目标是使 `tRPC` router 层的逻辑尽可能纯粹,专注于请求处理和业务流程编排。
|
||||
- 服务可能会调用 `Repository` 层或直接调用 `Model` 层进行数据持久化和检索,也可能调用其他服务。
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
---
|
||||
description: How to code review
|
||||
globs:
|
||||
globs:
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
# Role Description
|
||||
|
||||
- You are a senior full-stack engineer skilled in performance optimization, security, and design systems.
|
||||
- You excel at reviewing code and providing constructive feedback.
|
||||
- Your task is to review submitted Git diffs **in Chinese** and return a structured review report.
|
||||
- You are a senior full-stack engineer skilled in performance optimization, security, and design systems.
|
||||
- You excel at reviewing code and providing constructive feedback.
|
||||
- Your task is to review submitted Git diffs **in Chinese** and return a structured review report.
|
||||
- Review style: concise, direct, focused on what matters most, with actionable suggestions.
|
||||
|
||||
## Before the Review
|
||||
@@ -17,59 +16,54 @@ Gather the modified code and context. Please strictly follow the process below:
|
||||
|
||||
1. Use `read_file` to read [package.json](mdc:package.json)
|
||||
2. Use terminal to run command `git diff HEAD | cat` to obtain the diff and list the changed files. If you recieived empty result, run the same command once more.
|
||||
3. Use `read_file` to open each changed file.
|
||||
4. Use `read_file` to read [rules-attach.mdc](mdc:.cursor/rules/rules-attach.mdc). Even if you think it's unnecessary, you must read it.
|
||||
5. combine changed files, step3 and `agent_requestable_workspace_rules`, list the rules which need to read
|
||||
3. Use `read_file` to open each changed file.
|
||||
4. Use `read_file` to read [rules-attach.mdc](mdc:.cursor/rules/rules-attach.mdc). Even if you think it's unnecessary, you must read it.
|
||||
5. combine changed files, step3 and `agent_requestable_workspace_rules`, list the rules which need to read
|
||||
6. Use `read_file` to read the rules list in step 5
|
||||
|
||||
## Review
|
||||
|
||||
### Code Style
|
||||
|
||||
read [typescript.mdc](mdc:.cursor/rules/typescript.mdc) to learn the project's code style.
|
||||
|
||||
- Ensure JSDoc comments accurately reflect the implementation; update them when needed.
|
||||
- Look for opportunities to simplify or modernize code with the latest JavaScript/TypeScript features.
|
||||
- Prefer `async`/`await` over callbacks or chained `.then` promises.
|
||||
- Use consistent, descriptive naming—avoid obscure abbreviations.
|
||||
- Replace magic numbers or strings with well-named constants.
|
||||
- Ensure JSDoc comments accurately reflect the implementation; update them when needed.
|
||||
- Look for opportunities to simplify or modernize code with the latest JavaScript/TypeScript features.
|
||||
- Prefer `async`/`await` over callbacks or chained `.then` promises.
|
||||
- Use consistent, descriptive naming—avoid obscure abbreviations.
|
||||
- Replace magic numbers or strings with well-named constants.
|
||||
- Use semantically meaningful variable, function, and class names.
|
||||
- Ignore purely formatting issues and other autofixable lint problems.
|
||||
|
||||
### Code Optimization
|
||||
|
||||
- Prefer `for…of` loops to index-based `for` loops when feasible.
|
||||
- Decide whether callbacks should be **debounced** or **throttled**.
|
||||
- Use components from `@lobehub/ui`, Ant Design, or the existing design system instead of raw HTML tags (e.g., `Button` vs. `button`).
|
||||
- reuse npm packages already installed (e.g., `lodash/omit`) rather than reinventing the wheel.
|
||||
- Design for dark mode and mobile responsiveness:
|
||||
- Use the `antd-style` token system instead of hard-coded colors.
|
||||
- Select the proper component variants.
|
||||
- Performance considerations:
|
||||
- Where safe, convert sequential async flows to concurrent ones with `Promise.all`, `Promise.race`, etc.
|
||||
- Prefer `for…of` loops to index-based `for` loops when feasible.
|
||||
- Decide whether callbacks should be **debounced** or **throttled**.
|
||||
- Use components from `@lobehub/ui`, Ant Design, or the existing design system instead of raw HTML tags (e.g., `Button` vs. `button`).
|
||||
- reuse npm packages already installed (e.g., `lodash/omit`) rather than reinventing the wheel.
|
||||
- Design for dark mode and mobile responsiveness:
|
||||
- Use the `antd-style` token system instead of hard-coded colors.
|
||||
- Select the proper component variants.
|
||||
- Performance considerations:
|
||||
- Where safe, convert sequential async flows to concurrent ones with `Promise.all`, `Promise.race`, etc.
|
||||
- Query only the required columns from a database rather than selecting entire rows.
|
||||
|
||||
### Obvious Bugs
|
||||
|
||||
- Do not silently swallow errors in `catch` blocks; at minimum, log them.
|
||||
- Revert temporary code used only for testing (e.g., debug logs, temporary configs).
|
||||
- Remove empty handlers (e.g., an empty `onClick`).
|
||||
- Do not silently swallow errors in `catch` blocks; at minimum, log them.
|
||||
- Revert temporary code used only for testing (e.g., debug logs, temporary configs).
|
||||
- Remove empty handlers (e.g., an empty `onClick`).
|
||||
- Confirm the UI degrades gracefully for unauthenticated users.
|
||||
- Don't leave any debug logs in the code (except when using the `debug` module properly).
|
||||
- When using the `debug` module, avoid `import { log } from 'debug'` as it logs directly to console. Use proper debug namespaces instead.
|
||||
- Check logs for sensitive information like api key, etc
|
||||
|
||||
## After the Review: output
|
||||
|
||||
1. Summary
|
||||
- Start with a brief explanation of what the change set does.
|
||||
- Summarize the changes for each modified file (or logical group).
|
||||
- Start with a brief explanation of what the change set does.
|
||||
- Summarize the changes for each modified file (or logical group).
|
||||
2. Comments Issues
|
||||
- List the most critical issues first.
|
||||
- Use an ordered list, which will be convenient for me to reference later.
|
||||
- For each issue:
|
||||
- Mark severity tag (`❌ Must fix`, `⚠️ Should fix`, `💅 Nitpick`)
|
||||
- Provode file path to the relevant file.
|
||||
- Provide recommended fix
|
||||
- End with a **git commit** command, instruct the author to run it.
|
||||
- We use gitmoji to label commit messages, format: [emoji] <type>(<scope>): <subject>
|
||||
- List the most critical issues first.
|
||||
- Use an ordered list, which will be convenient for me to reference later.
|
||||
- For each issue:
|
||||
- Mark severity tag (`❌ Must fix`, `⚠️ Should fix`, `💅 Nitpick`)
|
||||
- Provode file path to the relevant file.
|
||||
- Provide recommended fix
|
||||
- End with a **git commit** command, instruct the author to run it.
|
||||
- We use gitmoji to label commit messages, format: [emoji] <type>(<scope>): <subject>
|
||||
@@ -1,25 +1,21 @@
|
||||
---
|
||||
description:
|
||||
globs:
|
||||
description:
|
||||
globs:
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
# Guide to Optimize Output(Response) Rendering
|
||||
|
||||
## File Path and Code Symbol Rendering
|
||||
|
||||
- When rendering file paths, use backtick wrapping instead of markdown links so they can be parsed as clickable links in Cursor IDE.
|
||||
- Good: `src/components/Button.tsx`
|
||||
- Bad: [src/components/Button.tsx](src/components/Button.tsx)
|
||||
|
||||
- Don't use line and column number in file path, this will make file path not clickable in Cursor IDE.
|
||||
- Good: `src/components/Button.tsx` `10:20` (add a space between the file path and the line and column number)
|
||||
- Bad: `src/components/Button.tsx:10:20`
|
||||
- Good: `src/components/Button.tsx`
|
||||
- Bad: [src/components/Button.tsx](mdc:src/components/Button.tsx)
|
||||
|
||||
- When rendering functions, variables, or other code symbols, use backtick wrapping so they can be parsed as navigable links in Cursor IDE
|
||||
- Good: The `useState` hook in `MyComponent`
|
||||
- Bad: The useState hook in MyComponent
|
||||
|
||||
- Good: The `useState` hook in `MyComponent`
|
||||
- Bad: The useState hook in MyComponent
|
||||
|
||||
## Markdown Render
|
||||
|
||||
- don't use br tag to wrap in table cell
|
||||
@@ -27,9 +23,9 @@ alwaysApply: true
|
||||
## Terminal Command Output
|
||||
|
||||
- If terminal commands don't produce output, it's likely due to paging issues. Try piping the command to `cat` to ensure full output is displayed.
|
||||
- Good: `git show commit_hash -- file.txt | cat`
|
||||
- Good: `git log --oneline | cat`
|
||||
- Reason: Some git commands use pagers by default, which may prevent output from being captured properly
|
||||
- Good: `git show commit_hash -- file.txt | cat`
|
||||
- Good: `git log --oneline | cat`
|
||||
- Reason: Some git commands use pagers by default, which may prevent output from being captured properly
|
||||
|
||||
## Mermaid Diagram Generation: Strict Syntax Validation Checklist
|
||||
|
||||
@@ -48,56 +44,50 @@ Before producing any Mermaid diagram, you **must** compare your final code line-
|
||||
|
||||
### Checklist Details
|
||||
|
||||
#### Rule 1: Edge Labels – Must Be Plain Text Only
|
||||
|
||||
#### Rule 1: Edge Labels – Must Be Plain Text Only
|
||||
> **Essence:** Anything inside `|...|` must contain pure, unformatted text. Absolutely NO Markdown, list markers, or parentheses/brackets allowed—these often cause rendering failures.
|
||||
|
||||
- **✅ Do:** `A -->|Process plain text data| B`
|
||||
- **❌ Don't:** `A -->|1. Ordered list item| B` (No numbered lists)
|
||||
- **❌ Don't:** `CC --"1. fetch('/api/...')"--> API` (No square brackets)
|
||||
- **❌ Don't:** `A -->|- Unordered list item| B` (No hyphen lists)
|
||||
- **❌ Don't:** `A -->|Transform (important)| B` (No parentheses)
|
||||
- **❌ Don't:** `A -->|Transform [important]| B` (No square brackets)
|
||||
|
||||
#### Rule 2: Node Definition – Handle Special Characters with Care
|
||||
- **✅ Do:** `A -->|Process plain text data| B`
|
||||
- **❌ Don't:** `A -->|1. Ordered list item| B` (No numbered lists)
|
||||
- **❌ Don't:** `CC --"1. fetch('/api/...')"--> API` (No square brackets)
|
||||
- **❌ Don't:** `A -->|- Unordered list item| B` (No hyphen lists)
|
||||
- **❌ Don't:** `A -->|Transform (important)| B` (No parentheses)
|
||||
- **❌ Don't:** `A -->|Transform [important]| B` (No square brackets)
|
||||
|
||||
#### Rule 2: Node Definition – Handle Special Characters with Care
|
||||
> **Essence:** When node text or subgraph titles contain special characters like `()` or `[]`, wrap the text in quotes to avoid conflicts with Mermaid shape syntax.
|
||||
|
||||
- **When your node text includes parentheses (e.g., 'React (JSX)'):**
|
||||
- **✅ Do:** `I_REACT["<b>React component (JSX)</b>"]` (Quotes wrap all text)
|
||||
- **❌ Don't:** `I_REACT(<b>React component (JSX)</b>)` (Wrong, Mermaid parses this as a shape)
|
||||
- **❌ Don't:** `subgraph Plugin Features (Plugins)` (Wrong, subgraph titles with parentheses must also be wrapped in quotes)
|
||||
|
||||
#### Rule 3: Double Quotes in Text – Must Be Escaped
|
||||
- **When your node text includes parentheses (e.g., 'React (JSX)'):**
|
||||
- **✅ Do:** `I_REACT["<b>React component (JSX)</b>"]` (Quotes wrap all text)
|
||||
- **❌ Don't:** `I_REACT(<b>React component (JSX)</b>)` (Wrong, Mermaid parses this as a shape)
|
||||
- **❌ Don't:** `subgraph Plugin Features (Plugins)` (Wrong, subgraph titles with parentheses must also be wrapped in quotes)
|
||||
|
||||
#### Rule 3: Double Quotes in Text – Must Be Escaped
|
||||
> **Essence:** Use `"` for double quotes **inside node text**.
|
||||
|
||||
- **✅ Do:** `A[This node contains "quotes"]`
|
||||
- **❌ Don't:** `A[This node contains "quotes"]`
|
||||
|
||||
#### Rule 4: All Formatting Must Use HTML Tags (NOT Markdown!)
|
||||
- **✅ Do:** `A[This node contains "quotes"]`
|
||||
- **❌ Don't:** `A[This node contains "quotes"]`
|
||||
|
||||
#### Rule 4: All Formatting Must Use HTML Tags (NOT Markdown!)
|
||||
> **Essence:** For newlines, bold, and other text formatting in nodes, use HTML tags only. Markdown is not supported.
|
||||
|
||||
- **✅ Do (robust):** `A["<b>Bold</b> and <code>code</code><br>This is a new line"]`
|
||||
- **❌ Don't (not rendered):** `C["# This is a heading"]`
|
||||
- **❌ Don't (not rendered):** ``C["`const` means constant"]``
|
||||
- **⚠️ Warning (unreliable):** `B["Markdown **bold** might sometimes work but DON'T rely on it"]`
|
||||
|
||||
#### Rule 5: No HTML Tags for Participants and Message Labels (Sequence Diagrams)
|
||||
- **✅ Do (robust):** `A["<b>Bold</b> and <code>code</code><br>This is a new line"]`
|
||||
- **❌ Don't (not rendered):** `C["# This is a heading"]`
|
||||
- **❌ Don't (not rendered):** ``C["`const` means constant"]``
|
||||
- **⚠️ Warning (unreliable):** `B["Markdown **bold** might sometimes work but DON'T rely on it"]`
|
||||
|
||||
#### Rule 5: No HTML Tags for Participants and Message Labels (Sequence Diagrams)
|
||||
> **Important Addition:**
|
||||
> In Mermaid sequence diagrams, you MUST NOT use any HTML tags (such as `<b>`, `<code>`, etc.) in:
|
||||
>
|
||||
> - `participant` display names (`as` part)
|
||||
> - Message labels (the text after `:` in diagram flows)
|
||||
>
|
||||
> These tags are generally not rendered—they may appear as-is or cause compatibility issues.
|
||||
|
||||
- **✅ Do:** `participant A as Client`
|
||||
- **❌ Don't:** `participant A as <b>Client</b>`
|
||||
- **✅ Do:** `A->>B: 1. Establish connection`
|
||||
- **❌ Don't:** `A->>B: 1. <code>Establish connection</code>`
|
||||
- **✅ Do:** `participant A as Client`
|
||||
- **❌ Don't:** `participant A as <b>Client</b>`
|
||||
- **✅ Do:** `A->>B: 1. Establish connection`
|
||||
- **❌ Don't:** `A->>B: 1. <code>Establish connection</code>`
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
description:
|
||||
globs: src/locales/**/*
|
||||
alwaysApply: false
|
||||
---
|
||||
read [i18n.mdc](mdc:.cursor/rules/i18n/i18n.mdc)
|
||||
@@ -1,16 +1,19 @@
|
||||
---
|
||||
description:
|
||||
globs:
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
## Project Description
|
||||
|
||||
You are developing an open-source, modern-design AI chat framework: lobe chat.
|
||||
You are developing an open-source, modern-design AI chat framework: lobe chat.
|
||||
|
||||
Emoji logo: 🤯
|
||||
|
||||
Emoji logo: 🤯
|
||||
|
||||
## Project Technologies Stack
|
||||
|
||||
read [package.json](mdc:package.json) to know all npm packages you can use. read [folder-structure.mdx](mdc:docs/development/basic/folder-structure.mdx) to learn project structure.
|
||||
read [package.json](mdc:package.json) to know all npm packages you can use.
|
||||
read [folder-structure.mdx](mdc:docs/development/basic/folder-structure.mdx) to learn project structure.
|
||||
|
||||
The project uses the following technologies:
|
||||
|
||||
@@ -42,17 +45,3 @@ The project uses the following technologies:
|
||||
- Cursor AI for code editing and AI coding assistance
|
||||
|
||||
Note: All tools and libraries used are the latest versions. The application only needs to be compatible with the latest browsers;
|
||||
|
||||
## Often used npm scripts
|
||||
|
||||
```bash
|
||||
# type check
|
||||
bun type-check
|
||||
|
||||
# install dependencies
|
||||
pnpm install
|
||||
|
||||
# !: don't any build script to check weather code can work after modify
|
||||
```
|
||||
|
||||
check [testing guide](./testing-guide/testing-guide.mdc) to learn test scripts.
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
---
|
||||
description:
|
||||
globs:
|
||||
description:
|
||||
globs:
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
# LobeChat Cursor Rules System Guide
|
||||
|
||||
This document explains how the LobeChat project's Cursor rules system works and serves as an index for manually accessible rules.
|
||||
@@ -15,26 +14,22 @@ This document explains how the LobeChat project's Cursor rules system works and
|
||||
## 📚 Four Ways to Access Rules
|
||||
|
||||
### 1. **Always Applied Rules** - `always_applied_workspace_rules`
|
||||
|
||||
- **What**: Core project guidelines that are always active
|
||||
- **Content**: Project tech stack, basic coding standards, output formatting rules
|
||||
- **Access**: No tools needed - automatically provided in every conversation
|
||||
|
||||
### 2. **Dynamic Context Rules** - `cursor_rules_context`
|
||||
|
||||
- **What**: Rules automatically matched based on files referenced in the conversation
|
||||
- **Trigger**: Only when user **explicitly @ mentions files** or **opens files in Cursor**
|
||||
- **Content**: May include brief descriptions or full rule content, depending on relevance
|
||||
- **Access**: No tools needed - automatically updated when files are referenced
|
||||
|
||||
### 3. **Agent Requestable Rules** - `agent_requestable_workspace_rules`
|
||||
|
||||
- **What**: Detailed operational guides that can be requested on-demand
|
||||
- **Access**: Use `fetch_rules` tool with rule names
|
||||
- **Examples**: `debug`, `i18n/i18n`, `code-review`
|
||||
|
||||
### 4. **Manual Rules Index** - This file + `read_file`
|
||||
|
||||
- **What**: Additional rules not covered by the above mechanisms
|
||||
- **Why needed**: Cursor's rule system only supports "agent request" or "auto attach" modes
|
||||
- **Access**: Use `read_file` tool to read specific `.mdc` files
|
||||
@@ -52,13 +47,10 @@ Use `read_file` to access rules from the index below when:
|
||||
The following rules are available via `read_file` from the `.cursor/rules/` directory:
|
||||
|
||||
- `backend-architecture.mdc` – Backend layer architecture and design guidelines
|
||||
- `define-database-model.mdc` – Database model definition guidelines
|
||||
- `drizzle-schema-style-guide.mdc` – Style guide for defining Drizzle ORM schemas
|
||||
- `react-component.mdc` – React component style guide and conventions
|
||||
- `testing-guide.mdc` – Comprehensive testing guide for Vitest environment
|
||||
- `typescript.mdc` – TypeScript code style guide
|
||||
- `zustand-action-patterns.mdc` – Recommended patterns for organizing Zustand actions
|
||||
- `zustand-slice-organization.mdc` – Best practices for structuring Zustand slices
|
||||
- `drizzle-schema-style-guide.mdc` – Style guide for defining Drizzle ORM schemas
|
||||
- `react-component.mdc` – React component style guide and conventions
|
||||
|
||||
## ❌ Common Misunderstandings to Avoid
|
||||
|
||||
@@ -70,7 +62,7 @@ The following rules are available via `read_file` from the `.cursor/rules/` dire
|
||||
|
||||
```
|
||||
1. Start with always_applied_workspace_rules (automatic)
|
||||
2. Check cursor_rules_context for auto-matched rules (automatic)
|
||||
2. Check cursor_rules_context for auto-matched rules (automatic)
|
||||
3. If you need specific guides: fetch_rules (manual)
|
||||
4. If you identify gaps: consult this index → read_file (manual)
|
||||
```
|
||||
@@ -78,8 +70,7 @@ The following rules are available via `read_file` from the `.cursor/rules/` dire
|
||||
## Example Decision Flow
|
||||
|
||||
**Scenario**: Working on a new Zustand store slice
|
||||
|
||||
1. Follow always_applied_workspace_rules ✅
|
||||
2. If store files were @ mentioned → use cursor_rules_context rules ✅
|
||||
2. If store files were @ mentioned → use cursor_rules_context rules ✅
|
||||
3. Need detailed Zustand guidance → `read_file('.cursor/rules/zustand-slice-organization.mdc')` ✅
|
||||
4. All rules apply simultaneously - no conflicts ✅
|
||||
4. All rules apply simultaneously - no conflicts ✅
|
||||
@@ -1,9 +1,8 @@
|
||||
---
|
||||
description:
|
||||
globs:
|
||||
description:
|
||||
globs:
|
||||
alwaysApply: true
|
||||
---
|
||||
|
||||
## System Role
|
||||
|
||||
You are an expert in full-stack Web development, proficient in JavaScript, TypeScript, CSS, React, Node.js, Next.js, Postgresql, all kinds of network protocols.
|
||||
@@ -12,6 +11,7 @@ You are an expert in LLM and Ai art. In Ai image generation, you are proficient
|
||||
|
||||
You are an expert in UI/UX design, proficient in web interaction patterns, responsive design, accessibility, and user behavior optimization. You excel at improving user retention and paid conversion rates through various interaction details.
|
||||
|
||||
|
||||
## Problem Solving
|
||||
|
||||
- Before formulating any response, you must first gather context by using tools like codebase_search, grep_search, file_search, web_search, fetch_rules, context7, and read_file to avoid making assumptions.
|
||||
@@ -36,8 +36,3 @@ You are an expert in UI/UX design, proficient in web interaction patterns, respo
|
||||
- If you're unable to access or retrieve content from websites, please inform me immediately and request the specific information needed rather than making assumptions
|
||||
- You can use emojis, npm packages like `chalk`/`chalk-animation`/`terminal-link`/`gradient-string`/`log-symbols`/`boxen`/`consola`/`@clack/prompts` to create beautiful terminal output
|
||||
- Don't run `tsc --noEmit` to check ts syntax error, because our project is very large and the validate very slow
|
||||
|
||||
## Some logging rules
|
||||
|
||||
- Never log user private information like api key, etc
|
||||
- Don't use `import { log } from 'debug'` to log messages, because it will directly log the message to the console.
|
||||
|
||||
@@ -0,0 +1,881 @@
|
||||
---
|
||||
description:
|
||||
globs: *.test.ts,*.test.tsx
|
||||
alwaysApply: false
|
||||
---
|
||||
---
|
||||
type: agent-requested
|
||||
title: 测试指南 - LobeChat Testing Guide
|
||||
description: LobeChat 项目的 Vitest 测试环境配置、运行方式、修复原则指南
|
||||
---
|
||||
|
||||
# 测试指南 - LobeChat Testing Guide
|
||||
|
||||
## 🧪 测试环境概览
|
||||
|
||||
LobeChat 项目使用 Vitest 测试库,配置了两种不同的测试环境:
|
||||
|
||||
### 客户端测试环境 (DOM Environment)
|
||||
|
||||
- **配置文件**: [vitest.config.ts](mdc:vitest.config.ts)
|
||||
- **环境**: Happy DOM (浏览器环境模拟)
|
||||
- **数据库**: PGLite (浏览器环境的 PostgreSQL)
|
||||
- **用途**: 测试前端组件、客户端逻辑、React 组件等
|
||||
- **设置文件**: [tests/setup.ts](mdc:tests/setup.ts)
|
||||
|
||||
### 服务端测试环境 (Node Environment)
|
||||
|
||||
- **配置文件**: [vitest.config.server.ts](mdc:vitest.config.server.ts)
|
||||
- **环境**: Node.js
|
||||
- **数据库**: 真实的 PostgreSQL 数据库
|
||||
- **并发限制**: 单线程运行 (`singleFork: true`)
|
||||
- **用途**: 测试数据库模型、服务端逻辑、API 端点等
|
||||
- **设置文件**: [tests/setup-db.ts](mdc:tests/setup-db.ts)
|
||||
|
||||
## 🚀 测试运行命令
|
||||
|
||||
### package.json 脚本说明
|
||||
|
||||
查看 [package.json](mdc:package.json) 中的测试相关脚本:
|
||||
|
||||
```json
|
||||
{
|
||||
"test": "npm run test-app && npm run test-server",
|
||||
"test-app": "vitest run --config vitest.config.ts",
|
||||
"test-app:coverage": "vitest run --config vitest.config.ts --coverage",
|
||||
"test-server": "vitest run --config vitest.config.server.ts",
|
||||
"test-server:coverage": "vitest run --config vitest.config.server.ts --coverage"
|
||||
}
|
||||
```
|
||||
|
||||
### 推荐的测试运行方式
|
||||
|
||||
#### ✅ 正确的命令格式
|
||||
|
||||
```bash
|
||||
# 运行所有客户端测试
|
||||
npx vitest run --config vitest.config.ts
|
||||
|
||||
# 运行所有服务端测试
|
||||
npx vitest run --config vitest.config.server.ts
|
||||
|
||||
# 运行特定测试文件 (支持模糊匹配)
|
||||
npx vitest run --config vitest.config.ts basic
|
||||
npx vitest run --config vitest.config.ts user.test.ts
|
||||
|
||||
# 运行特定文件的特定行号
|
||||
npx vitest run --config vitest.config.ts src/utils/helper.test.ts:25
|
||||
npx vitest run --config vitest.config.ts basic/foo.test.ts:10,basic/foo.test.ts:25
|
||||
|
||||
# 过滤特定测试用例名称
|
||||
npx vitest -t "test case name" --config vitest.config.ts
|
||||
|
||||
# 组合使用文件和测试名称过滤
|
||||
npx vitest run --config vitest.config.ts filename.test.ts -t "specific test"
|
||||
```
|
||||
|
||||
#### ❌ 避免的命令格式
|
||||
|
||||
```bash
|
||||
# ❌ 不要使用 pnpm test xxx (这不是有效的 vitest 命令)
|
||||
pnpm test some-file
|
||||
|
||||
# ❌ 不要使用裸 vitest (会进入 watch 模式)
|
||||
vitest test-file.test.ts
|
||||
|
||||
# ❌ 不要混淆测试环境
|
||||
npx vitest run --config vitest.config.server.ts client-component.test.ts
|
||||
```
|
||||
|
||||
### 关键运行参数说明
|
||||
|
||||
- **`vitest run`**: 运行一次测试然后退出 (避免 watch 模式)
|
||||
- **`vitest`**: 默认进入 watch 模式,持续监听文件变化
|
||||
- **`--config`**: 指定配置文件,选择正确的测试环境
|
||||
- **`-t`**: 过滤测试用例名称,支持正则表达式
|
||||
- **`--coverage`**: 生成测试覆盖率报告
|
||||
|
||||
## 🔧 测试修复原则
|
||||
|
||||
### 核心原则 ⚠️
|
||||
|
||||
1. **充分阅读测试代码**: 在修复测试之前,必须完整理解测试的意图和实现
|
||||
2. **测试优先修复**: 如果是测试本身写错了,修改测试而不是实现代码
|
||||
3. **专注单一问题**: 只修复指定的测试,不要添加额外测试或功能
|
||||
4. **不自作主张**: 不要因为发现其他问题就直接修改,先提出再讨论
|
||||
|
||||
### 测试修复流程
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
subgraph "阶段一:分析与复现"
|
||||
A[开始:收到测试失败报告] --> B[定位并运行失败的测试];
|
||||
B --> C{是否能在本地复现?};
|
||||
C -->|否| D[检查测试环境/配置/依赖];
|
||||
C -->|是| E[分析:阅读测试代码、错误日志、Git 历史];
|
||||
end
|
||||
|
||||
subgraph "阶段二:诊断与调试"
|
||||
E --> F[建立假设:问题出在测试、代码还是环境?];
|
||||
F --> G["调试:使用 console.log 或 debugger 深入检查"];
|
||||
G --> H{假设是否被证实?};
|
||||
H -->|否, 重新假设| F;
|
||||
end
|
||||
|
||||
subgraph "阶段三:修复与验证"
|
||||
H -->|是| I{确定根本原因};
|
||||
I -->|测试逻辑错误| J[修复测试代码];
|
||||
I -->|实现代码 Bug| K[修复实现代码];
|
||||
I -->|环境/配置问题| L[修复配置或依赖];
|
||||
J --> M[验证修复:重新运行失败的测试];
|
||||
K --> M;
|
||||
L --> M;
|
||||
M --> N{测试是否通过?};
|
||||
N -->|否, 修复无效| F;
|
||||
N -->|是| O[扩大验证:运行当前文件内所有测试];
|
||||
O --> P{是否全部通过?};
|
||||
P -->|否, 引入新问题| F;
|
||||
end
|
||||
|
||||
subgraph "阶段四:总结"
|
||||
P -->|是| Q[完成:撰写修复总结];
|
||||
end
|
||||
|
||||
D --> F;
|
||||
```
|
||||
|
||||
### 修复完成后的总结
|
||||
|
||||
测试修复完成后,应该提供简要说明,包括:
|
||||
|
||||
1. **错误原因分析**: 说明测试失败的根本原因
|
||||
- 测试逻辑错误
|
||||
- 实现代码bug
|
||||
- 环境配置问题
|
||||
- 依赖变更导致的问题
|
||||
|
||||
2. **修复方法说明**: 简述采用的修复方式
|
||||
- 修改了哪些文件
|
||||
- 采用了什么解决方案
|
||||
- 为什么选择这种修复方式
|
||||
|
||||
**示例格式**:
|
||||
|
||||
```markdown
|
||||
## 测试修复总结
|
||||
|
||||
**错误原因**: 测试中的 mock 数据格式与实际 API 返回格式不匹配,导致断言失败。
|
||||
|
||||
**修复方法**: 更新了测试文件中的 mock 数据结构,使其与最新的 API 响应格式保持一致。具体修改了 `user.test.ts` 中的 `mockUserData` 对象结构。
|
||||
```
|
||||
|
||||
## 📂 测试文件组织
|
||||
|
||||
### 文件命名约定
|
||||
|
||||
- **客户端测试**: `*.test.ts`, `*.test.tsx` (任意位置)
|
||||
- **服务端测试**: `src/database/models/**/*.test.ts`, `src/database/server/**/*.test.ts` (限定路径)
|
||||
|
||||
### 测试文件组织风格
|
||||
|
||||
项目采用 **测试文件与源文件同目录** 的组织风格:
|
||||
|
||||
- 测试文件放在对应源文件的同一目录下
|
||||
- 命名格式:`原文件名.test.ts` 或 `原文件名.test.tsx`
|
||||
|
||||
例如:
|
||||
|
||||
```
|
||||
src/components/Button/
|
||||
├── index.tsx # 源文件
|
||||
└── index.test.tsx # 测试文件
|
||||
```
|
||||
|
||||
## 🛠️ 测试调试技巧
|
||||
|
||||
### 运行失败测试的步骤
|
||||
|
||||
1. **确定测试类型**: 查看文件路径确定使用哪个配置
|
||||
2. **运行单个测试**: 使用 `-t` 参数隔离问题
|
||||
3. **检查错误日志**: 仔细阅读错误信息和堆栈跟踪
|
||||
4. **查看最近修改记录**: 检查相关文件的最近变更情况
|
||||
5. **添加调试日志**: 在测试中添加 `console.log` 了解执行流程
|
||||
|
||||
### Electron IPC 接口测试策略 🖥️
|
||||
|
||||
对于涉及 Electron IPC 接口的测试,由于提供真实的 Electron 环境比较复杂,采用 **Mock 返回值** 的方式进行测试。
|
||||
|
||||
#### 基本 Mock 设置
|
||||
|
||||
```typescript
|
||||
import { vi } from "vitest";
|
||||
import { electronIpcClient } from "@/server/modules/ElectronIPCClient";
|
||||
|
||||
// Mock Electron IPC 客户端
|
||||
vi.mock("@/server/modules/ElectronIPCClient", () => ({
|
||||
electronIpcClient: {
|
||||
getFilePathById: vi.fn(),
|
||||
deleteFiles: vi.fn(),
|
||||
// 根据需要添加其他 IPC 方法
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
#### 在测试中设置 Mock 行为
|
||||
|
||||
```typescript
|
||||
beforeEach(() => {
|
||||
// 重置所有 Mock
|
||||
vi.resetAllMocks();
|
||||
|
||||
// 设置默认的 Mock 返回值
|
||||
vi.mocked(electronIpcClient.getFilePathById).mockResolvedValue(
|
||||
"/path/to/file.txt"
|
||||
);
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockResolvedValue({
|
||||
success: true,
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
#### 测试不同场景的示例
|
||||
|
||||
```typescript
|
||||
it("应该处理文件删除成功的情况", async () => {
|
||||
// 设置成功场景的 Mock
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockResolvedValue({
|
||||
success: true,
|
||||
});
|
||||
|
||||
const result = await service.deleteFiles(["desktop://file1.txt"]);
|
||||
|
||||
expect(electronIpcClient.deleteFiles).toHaveBeenCalledWith([
|
||||
"desktop://file1.txt",
|
||||
]);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("应该处理文件删除失败的情况", async () => {
|
||||
// 设置失败场景的 Mock
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockRejectedValue(
|
||||
new Error("删除失败")
|
||||
);
|
||||
|
||||
const result = await service.deleteFiles(["desktop://file1.txt"]);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errors).toBeDefined();
|
||||
});
|
||||
```
|
||||
|
||||
#### Mock 策略的优势
|
||||
|
||||
1. **环境简化**: 避免了复杂的 Electron 环境搭建
|
||||
2. **测试可控**: 可以精确控制 IPC 调用的返回值和行为
|
||||
3. **场景覆盖**: 容易测试各种成功/失败场景
|
||||
4. **执行速度**: Mock 调用比真实 IPC 调用更快
|
||||
|
||||
#### 注意事项
|
||||
|
||||
- **Mock 准确性**: 确保 Mock 的行为与真实 IPC 接口行为一致
|
||||
- **类型安全**: 使用 `vi.mocked()` 确保类型安全
|
||||
- **Mock 重置**: 在 `beforeEach` 中重置 Mock 状态,避免测试间干扰
|
||||
- **调用验证**: 不仅要验证返回值,还要验证 IPC 方法是否被正确调用
|
||||
|
||||
### 检查最近修改记录 🔍
|
||||
|
||||
为了更好地判断测试失败的根本原因,需要**系统性地检查相关文件的修改历史**。这是问题定位的关键步骤。
|
||||
|
||||
#### 第一步:确定需要检查的文件范围
|
||||
|
||||
1. **测试文件本身**: `path/to/component.test.ts`
|
||||
2. **对应的实现文件**: `path/to/component.ts` 或 `path/to/component/index.ts`
|
||||
3. **相关依赖文件**: 测试或实现中导入的其他模块
|
||||
|
||||
#### 第二步:检查当前工作目录状态
|
||||
|
||||
```bash
|
||||
# 查看所有未提交的修改状态
|
||||
git status
|
||||
|
||||
# 重点关注测试文件和实现文件是否有未提交的修改
|
||||
git status | grep -E "(test|spec)"
|
||||
```
|
||||
|
||||
#### 第三步:检查未提交的修改内容
|
||||
|
||||
```bash
|
||||
# 查看测试文件的未提交修改 (工作区 vs 暂存区)
|
||||
git diff path/to/component.test.ts | cat
|
||||
|
||||
# 查看对应实现文件的未提交修改
|
||||
git diff path/to/component.ts | cat
|
||||
|
||||
# 查看已暂存但未提交的修改
|
||||
git diff --cached path/to/component.test.ts | cat
|
||||
git diff --cached path/to/component.ts | cat
|
||||
```
|
||||
|
||||
#### 第四步:检查提交历史和时间相关性
|
||||
|
||||
**首先查看提交时间,判断修改的时效性**:
|
||||
|
||||
```bash
|
||||
# 查看测试文件的最近提交历史,包含提交时间
|
||||
git log --pretty=format:"%h %ad %s" --date=relative -5 path/to/component.test.ts | cat
|
||||
|
||||
# 查看实现文件的最近提交历史,包含提交时间
|
||||
git log --pretty=format:"%h %ad %s" --date=relative -5 path/to/component.ts | cat
|
||||
|
||||
# 查看详细的提交时间(ISO格式,便于精确判断)
|
||||
git log --pretty=format:"%h %ad %an %s" --date=iso -3 path/to/component.ts | cat
|
||||
git log --pretty=format:"%h %ad %an %s" --date=iso -3 path/to/component.test.ts | cat
|
||||
```
|
||||
|
||||
**判断提交的参考价值**:
|
||||
|
||||
1. **最近提交(24小时内)**: 🔴 **高度相关** - 很可能是导致测试失败的直接原因
|
||||
2. **近期提交(1-7天内)**: 🟡 **中等相关** - 可能相关,需要仔细分析修改内容
|
||||
3. **较早提交(超过1周)**: ⚪ **低相关性** - 除非是重大重构,否则不太可能是直接原因
|
||||
|
||||
#### 第五步:基于时间相关性查看具体修改内容
|
||||
|
||||
**根据提交时间的远近,优先查看最近的修改**:
|
||||
|
||||
```bash
|
||||
# 如果有24小时内的提交,重点查看这些修改
|
||||
git show HEAD -- path/to/component.test.ts | cat
|
||||
git show HEAD -- path/to/component.ts | cat
|
||||
|
||||
# 查看次新的提交(如果最新提交时间较远)
|
||||
git show HEAD~1 -- path/to/component.ts | cat
|
||||
git show <recent-commit-hash> -- path/to/component.ts | cat
|
||||
|
||||
# 对比最近两次提交的差异
|
||||
git diff HEAD~1 HEAD -- path/to/component.ts | cat
|
||||
```
|
||||
|
||||
#### 第六步:分析修改与测试失败的关系
|
||||
|
||||
基于修改记录和时间相关性判断:
|
||||
|
||||
1. **最近修改了实现代码**:
|
||||
|
||||
```bash
|
||||
# 重点检查实现逻辑的变化
|
||||
git diff HEAD~1 path/to/component.ts | cat
|
||||
```
|
||||
|
||||
- 很可能是实现代码的变更导致测试失败
|
||||
- 检查实现逻辑是否正确
|
||||
- 确认测试是否需要相应更新
|
||||
|
||||
2. **最近修改了测试代码**:
|
||||
|
||||
```bash
|
||||
# 重点检查测试逻辑的变化
|
||||
git diff HEAD~1 path/to/component.test.ts | cat
|
||||
```
|
||||
|
||||
- 可能是测试本身写错了
|
||||
- 检查测试逻辑和断言是否正确
|
||||
- 确认测试是否符合实现的预期行为
|
||||
|
||||
3. **两者都有最近修改**:
|
||||
|
||||
```bash
|
||||
# 对比两个文件的修改时间
|
||||
git log --pretty=format:"%ad %f" --date=iso -1 path/to/component.ts | cat
|
||||
git log --pretty=format:"%ad %f" --date=iso -1 path/to/component.test.ts | cat
|
||||
```
|
||||
|
||||
- 需要综合分析两者的修改
|
||||
- 确定哪个修改更可能导致问题
|
||||
- 优先检查时间更近的修改
|
||||
|
||||
4. **都没有最近修改**:
|
||||
- 可能是依赖变更或环境问题
|
||||
- 检查 `package.json`、配置文件等的修改
|
||||
- 查看是否有全局性的代码重构
|
||||
|
||||
#### 修改记录检查示例
|
||||
|
||||
```bash
|
||||
# 完整的检查流程示例
|
||||
echo "=== 检查文件修改状态 ==="
|
||||
git status | grep component
|
||||
|
||||
echo "=== 检查未提交修改 ==="
|
||||
git diff src/components/Button/index.test.tsx | cat
|
||||
git diff src/components/Button/index.tsx | cat
|
||||
|
||||
echo "=== 检查提交历史和时间 ==="
|
||||
git log --pretty=format:"%h %ad %s" --date=relative -3 src/components/Button/index.test.tsx | cat
|
||||
git log --pretty=format:"%h %ad %s" --date=relative -3 src/components/Button/index.tsx | cat
|
||||
|
||||
echo "=== 根据时间优先级查看修改内容 ==="
|
||||
# 如果有24小时内的提交,重点查看
|
||||
git show HEAD -- src/components/Button/index.tsx | cat
|
||||
```
|
||||
|
||||
## 🗃️ 数据库 Model 测试指南
|
||||
|
||||
### 测试环境选择 💡
|
||||
|
||||
数据库 Model 层通过环境变量控制数据库类型,在两种测试环境下有不同的数据库后端:客户端环境 (PGLite) 和 服务端环境 (PostgreSQL)
|
||||
|
||||
### ⚠️ 双环境验证要求
|
||||
|
||||
**对于所有 Model 测试,必须在两个环境下都验证通过**:
|
||||
|
||||
#### 完整验证流程
|
||||
|
||||
```bash
|
||||
# 1. 先在客户端环境测试(快速验证)
|
||||
npx vitest run --config vitest.config.ts src/database/models/__tests__/myModel.test.ts
|
||||
|
||||
# 2. 再在服务端环境测试(兼容性验证)
|
||||
npx vitest run --config vitest.config.server.ts src/database/models/__tests__/myModel.test.ts
|
||||
```
|
||||
|
||||
### 创建新 Model 测试的最佳实践 📋
|
||||
|
||||
#### 1. 参考现有实现和测试模板
|
||||
|
||||
创建新 Model 测试前,**必须先参考现有的实现模式**:
|
||||
|
||||
- **Model 实现参考**:
|
||||
- **测试模板参考**:
|
||||
- **复杂示例参考**:
|
||||
|
||||
#### 2. 用户权限检查 - 安全第一 🔒
|
||||
|
||||
这是**最关键的安全要求**。所有涉及用户数据的操作都必须包含用户权限检查:
|
||||
|
||||
**❌ 错误示例 - 存在安全漏洞**:
|
||||
|
||||
```typescript
|
||||
// 危险:缺少用户权限检查,任何用户都能操作任何数据
|
||||
update = async (id: string, data: Partial<MyModel>) => {
|
||||
return this.db
|
||||
.update(myTable)
|
||||
.set(data)
|
||||
.where(eq(myTable.id, id)) // ❌ 只检查 ID,没有检查 userId
|
||||
.returning();
|
||||
};
|
||||
```
|
||||
|
||||
**✅ 正确示例 - 安全的实现**:
|
||||
|
||||
```typescript
|
||||
// 安全:必须同时匹配 ID 和 userId
|
||||
update = async (id: string, data: Partial<MyModel>) => {
|
||||
return this.db
|
||||
.update(myTable)
|
||||
.set(data)
|
||||
.where(
|
||||
and(
|
||||
eq(myTable.id, id),
|
||||
eq(myTable.userId, this.userId) // ✅ 用户权限检查
|
||||
)
|
||||
)
|
||||
.returning();
|
||||
};
|
||||
```
|
||||
|
||||
**必须进行用户权限检查的方法**:
|
||||
|
||||
- `update()` - 更新操作
|
||||
- `delete()` - 删除操作
|
||||
- `findById()` - 查找特定记录
|
||||
- 任何涉及特定记录的查询或修改操作
|
||||
|
||||
#### 3. 测试文件结构和必测场景
|
||||
|
||||
**基本测试结构**:
|
||||
|
||||
```typescript
|
||||
// @vitest-environment node
|
||||
describe("MyModel", () => {
|
||||
describe("create", () => {
|
||||
it("should create a new record");
|
||||
it("should handle edge cases");
|
||||
});
|
||||
|
||||
describe("queryAll", () => {
|
||||
it("should return records for current user only");
|
||||
it("should handle empty results");
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
it("should update own records");
|
||||
it("should NOT update other users records"); // 🔒 安全测试
|
||||
});
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete own records");
|
||||
it("should NOT delete other users records"); // 🔒 安全测试
|
||||
});
|
||||
|
||||
describe("user isolation", () => {
|
||||
it("should enforce user data isolation"); // 🔒 核心安全测试
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**必须测试的安全场景** 🔒:
|
||||
|
||||
```typescript
|
||||
it("should not update records of other users", async () => {
|
||||
// 创建其他用户的记录
|
||||
const [otherUserRecord] = await serverDB
|
||||
.insert(myTable)
|
||||
.values({ userId: "other-user", data: "original" })
|
||||
.returning();
|
||||
|
||||
// 尝试更新其他用户的记录
|
||||
const result = await myModel.update(otherUserRecord.id, { data: "hacked" });
|
||||
|
||||
// 应该返回 undefined 或空数组(因为权限检查失败)
|
||||
expect(result).toBeUndefined();
|
||||
|
||||
// 验证原始数据未被修改
|
||||
const unchanged = await serverDB.query.myTable.findFirst({
|
||||
where: eq(myTable.id, otherUserRecord.id),
|
||||
});
|
||||
expect(unchanged?.data).toBe("original"); // 数据应该保持不变
|
||||
});
|
||||
```
|
||||
|
||||
#### 4. Mock 外部依赖服务
|
||||
|
||||
如果 Model 依赖外部服务(如 FileService),需要正确 Mock:
|
||||
|
||||
**设置 Mock**:
|
||||
|
||||
```typescript
|
||||
// 在文件顶部设置 Mock
|
||||
const mockGetFullFileUrl = vi.fn();
|
||||
vi.mock("@/server/services/file", () => ({
|
||||
FileService: vi.fn().mockImplementation(() => ({
|
||||
getFullFileUrl: mockGetFullFileUrl,
|
||||
})),
|
||||
}));
|
||||
|
||||
// 在 beforeEach 中重置和配置 Mock
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
mockGetFullFileUrl.mockImplementation(
|
||||
(url: string) => `https://example.com/${url}`
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
**验证 Mock 调用**:
|
||||
|
||||
```typescript
|
||||
it("should process URLs through FileService", async () => {
|
||||
// ... 测试逻辑
|
||||
|
||||
// 验证 Mock 被正确调用
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledWith("expected-url");
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
```
|
||||
|
||||
#### 5. 数据库状态管理
|
||||
|
||||
**正确的数据清理模式**:
|
||||
|
||||
```typescript
|
||||
const userId = "test-user";
|
||||
const otherUserId = "other-user";
|
||||
|
||||
beforeEach(async () => {
|
||||
// 清理用户表(级联删除相关数据)
|
||||
await serverDB.delete(users);
|
||||
|
||||
// 创建测试用户
|
||||
await serverDB.insert(users).values([{ id: userId }, { id: otherUserId }]);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// 清理测试数据
|
||||
await serverDB.delete(users);
|
||||
});
|
||||
```
|
||||
|
||||
#### 6. 测试数据类型和外键约束处理 ⚠️
|
||||
|
||||
**必须使用 Schema 导出的类型**:
|
||||
|
||||
```typescript
|
||||
// ✅ 正确:使用 schema 导出的类型
|
||||
import { NewGenerationBatch, NewGeneration } from '../../schemas';
|
||||
|
||||
const testBatch: NewGenerationBatch = {
|
||||
userId,
|
||||
generationTopicId: 'test-topic-id',
|
||||
provider: 'test-provider',
|
||||
model: 'test-model',
|
||||
prompt: 'Test prompt for image generation',
|
||||
width: 1024,
|
||||
height: 1024,
|
||||
config: { /* ... */ },
|
||||
};
|
||||
|
||||
const testGeneration: NewGeneration = {
|
||||
id: 'test-gen-id',
|
||||
generationBatchId: 'test-batch-id',
|
||||
asyncTaskId: null, // 处理外键约束
|
||||
fileId: null, // 处理外键约束
|
||||
seed: 12345,
|
||||
userId,
|
||||
};
|
||||
```
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:没有类型声明或使用错误类型
|
||||
const testBatch = { // 缺少类型声明
|
||||
generationTopicId: 'test-topic-id',
|
||||
// ...
|
||||
};
|
||||
|
||||
const testGeneration = { // 缺少类型声明
|
||||
asyncTaskId: 'invalid-uuid', // 外键约束错误
|
||||
fileId: 'non-existent-file', // 外键约束错误
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
**外键约束处理策略**:
|
||||
|
||||
1. **使用 null 值**: 对于可选的外键字段,使用 null 避免约束错误
|
||||
2. **创建关联记录**: 如果需要测试关联关系,先创建被引用的记录
|
||||
3. **理解约束关系**: 了解哪些字段有外键约束,避免引用不存在的记录
|
||||
|
||||
```typescript
|
||||
// 外键约束处理示例
|
||||
beforeEach(async () => {
|
||||
// 清理数据库
|
||||
await serverDB.delete(users);
|
||||
|
||||
// 创建测试用户
|
||||
await serverDB.insert(users).values([{ id: userId }]);
|
||||
|
||||
// 如果需要测试文件关联,创建文件记录
|
||||
if (needsFileAssociation) {
|
||||
await serverDB.insert(files).values({
|
||||
id: 'test-file-id',
|
||||
userId,
|
||||
name: 'test.jpg',
|
||||
url: 'test-url',
|
||||
size: 1024,
|
||||
fileType: 'image/jpeg',
|
||||
});
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**排序测试的可预测性**:
|
||||
|
||||
```typescript
|
||||
// ✅ 正确:使用明确的时间戳确保排序结果可预测
|
||||
it('should find batches by topic id in correct order', async () => {
|
||||
const oldDate = new Date('2024-01-01T10:00:00Z');
|
||||
const newDate = new Date('2024-01-02T10:00:00Z');
|
||||
|
||||
const batch1 = { ...testBatch, prompt: 'First batch', userId, createdAt: oldDate };
|
||||
const batch2 = { ...testBatch, prompt: 'Second batch', userId, createdAt: newDate };
|
||||
|
||||
await serverDB.insert(generationBatches).values([batch1, batch2]);
|
||||
|
||||
const results = await generationBatchModel.findByTopicId(testTopic.id);
|
||||
|
||||
expect(results[0].prompt).toBe('Second batch'); // 最新优先 (desc order)
|
||||
expect(results[1].prompt).toBe('First batch');
|
||||
});
|
||||
```
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:依赖数据库的默认时间戳,结果不可预测
|
||||
it('should find batches by topic id', async () => {
|
||||
const batch1 = { ...testBatch, prompt: 'First batch', userId };
|
||||
const batch2 = { ...testBatch, prompt: 'Second batch', userId };
|
||||
|
||||
await serverDB.insert(generationBatches).values([batch1, batch2]);
|
||||
|
||||
// 插入顺序和数据库时间戳可能不一致,导致测试不稳定
|
||||
const results = await generationBatchModel.findByTopicId(testTopic.id);
|
||||
expect(results[0].prompt).toBe('Second batch'); // 可能失败
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 常见问题和解决方案 💡
|
||||
|
||||
#### 问题 1:权限检查缺失导致安全漏洞
|
||||
|
||||
**现象**: 测试失败,用户能修改其他用户的数据
|
||||
**解决**: 在 Model 的 `update` 和 `delete` 方法中添加 `and(eq(table.id, id), eq(table.userId, this.userId))`
|
||||
|
||||
#### 问题 2:Mock 未生效或验证失败
|
||||
|
||||
**现象**: `undefined is not a spy` 错误
|
||||
**解决**: 检查 Mock 设置位置和方式,确保在测试文件顶部设置,在 `beforeEach` 中重置
|
||||
|
||||
#### 问题 3:测试数据污染
|
||||
|
||||
**现象**: 测试间相互影响,结果不稳定
|
||||
**解决**: 在 `beforeEach` 和 `afterEach` 中正确清理数据库状态
|
||||
|
||||
#### 问题 4:外部依赖导致测试失败
|
||||
|
||||
**现象**: 因为真实的外部服务调用导致测试不稳定
|
||||
**解决**: Mock 所有外部依赖,使测试更可控和快速
|
||||
|
||||
#### 问题 5:外键约束违反导致测试失败
|
||||
|
||||
**现象**: `insert or update on table "xxx" violates foreign key constraint`
|
||||
**解决**:
|
||||
- 将可选外键字段设为 `null` 而不是无效的字符串值
|
||||
- 或者先创建被引用的记录,再创建当前记录
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:无效的外键值
|
||||
const testData = {
|
||||
asyncTaskId: 'invalid-uuid', // 表中不存在此记录
|
||||
fileId: 'non-existent-file', // 表中不存在此记录
|
||||
};
|
||||
|
||||
// ✅ 正确:使用 null 值
|
||||
const testData = {
|
||||
asyncTaskId: null, // 避免外键约束
|
||||
fileId: null, // 避免外键约束
|
||||
};
|
||||
|
||||
// ✅ 或者:先创建被引用的记录
|
||||
beforeEach(async () => {
|
||||
const [asyncTask] = await serverDB.insert(asyncTasks).values({
|
||||
id: 'valid-task-id',
|
||||
status: 'pending',
|
||||
type: 'generation',
|
||||
}).returning();
|
||||
|
||||
const testData = {
|
||||
asyncTaskId: asyncTask.id, // 使用有效的外键值
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
#### 问题 6:排序测试结果不一致
|
||||
|
||||
**现象**: 相同的测试有时通过,有时失败,特别是涉及排序的测试
|
||||
**解决**: 使用明确的时间戳,不要依赖数据库的默认时间戳
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:依赖插入顺序和默认时间戳
|
||||
await serverDB.insert(table).values([data1, data2]); // 时间戳不可预测
|
||||
|
||||
// ✅ 正确:明确指定时间戳
|
||||
const oldDate = new Date('2024-01-01T10:00:00Z');
|
||||
const newDate = new Date('2024-01-02T10:00:00Z');
|
||||
await serverDB.insert(table).values([
|
||||
{ ...data1, createdAt: oldDate },
|
||||
{ ...data2, createdAt: newDate },
|
||||
]);
|
||||
```
|
||||
|
||||
#### 问题 7:Mock 验证失败或调用次数不匹配
|
||||
|
||||
**现象**: `expect(mockFunction).toHaveBeenCalledWith(...)` 失败
|
||||
**解决**:
|
||||
- 检查 Mock 函数的实际调用参数和期望参数是否完全匹配
|
||||
- 确认 Mock 在正确的时机被重置和配置
|
||||
- 使用 `toHaveBeenCalledTimes()` 验证调用次数
|
||||
|
||||
```typescript
|
||||
// 在 beforeEach 中正确配置 Mock
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks(); // 重置所有 Mock
|
||||
|
||||
mockGetFullFileUrl.mockImplementation((url: string) => `https://example.com/${url}`);
|
||||
mockTransformGeneration.mockResolvedValue({
|
||||
id: 'test-id',
|
||||
// ... 其他字段
|
||||
});
|
||||
});
|
||||
|
||||
// 测试中验证 Mock 调用
|
||||
it('should call FileService with correct parameters', async () => {
|
||||
await model.someMethod();
|
||||
|
||||
// 验证调用参数
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledWith('expected-url');
|
||||
// 验证调用次数
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
```
|
||||
|
||||
### Model 测试检查清单 ✅
|
||||
|
||||
创建 Model 测试时,请确保以下各项都已完成:
|
||||
|
||||
#### 🔧 基础配置
|
||||
- [ ] **双环境验证** - 在客户端环境 (vitest.config.ts) 和服务端环境 (vitest.config.server.ts) 下都测试通过
|
||||
- [ ] 参考了 `_template.ts` 和现有 Model 的实现模式
|
||||
- [ ] **使用正确的 Schema 类型** - 测试数据使用 `NewXxx` 类型声明,如 `NewGenerationBatch`、`NewGeneration`
|
||||
|
||||
#### 🔒 安全测试
|
||||
- [ ] **所有涉及用户数据的操作都包含用户权限检查**
|
||||
- [ ] 包含了用户权限隔离的安全测试
|
||||
- [ ] 测试了用户无法访问其他用户数据的场景
|
||||
|
||||
#### 🗃️ 数据处理
|
||||
- [ ] **正确处理外键约束** - 使用 `null` 值或先创建被引用记录
|
||||
- [ ] **排序测试使用明确时间戳** - 不依赖数据库默认时间,确保结果可预测
|
||||
- [ ] 在 `beforeEach` 和 `afterEach` 中正确管理数据库状态
|
||||
- [ ] 所有测试都能独立运行且互不干扰
|
||||
|
||||
#### 🎭 Mock 和外部依赖
|
||||
- [ ] 正确 Mock 了外部依赖服务 (如 FileService、GenerationModel)
|
||||
- [ ] 在 `beforeEach` 中重置和配置 Mock
|
||||
- [ ] 验证了 Mock 服务的调用参数和次数
|
||||
- [ ] 测试了外部服务错误场景的处理
|
||||
|
||||
#### 📋 测试覆盖
|
||||
- [ ] 测试覆盖了所有主要方法 (create, query, update, delete)
|
||||
- [ ] 测试了边界条件和错误场景
|
||||
- [ ] 包含了空结果处理的测试
|
||||
- [ ] **确认两个环境下的测试结果一致**
|
||||
|
||||
#### 🚨 常见问题检查
|
||||
- [ ] 没有外键约束违反错误
|
||||
- [ ] 排序测试结果稳定可预测
|
||||
- [ ] Mock 验证无失败
|
||||
- [ ] 无测试数据污染问题
|
||||
|
||||
### 安全警告 ⚠️
|
||||
|
||||
**数据库 Model 层是安全的第一道防线**。如果 Model 层缺少用户权限检查:
|
||||
|
||||
1. **任何用户都能访问和修改其他用户的数据**
|
||||
2. **即使上层有权限检查,也可能被绕过**
|
||||
3. **可能导致严重的数据泄露和安全事故**
|
||||
|
||||
因此,**每个涉及用户数据的 Model 方法都必须包含用户权限检查,且必须有对应的安全测试来验证这些检查的有效性**。
|
||||
|
||||
## 🎯 总结
|
||||
|
||||
修复测试时,记住以下关键点:
|
||||
|
||||
- **使用正确的命令**: `npx vitest run --config [config-file]`
|
||||
- **理解测试意图**: 先读懂测试再修复
|
||||
- **查看最近修改**: 检查相关文件的 git 修改记录,判断问题根源
|
||||
- **选择正确环境**: 客户端测试用 `vitest.config.ts`,服务端用 `vitest.config.server.ts`
|
||||
- **专注单一问题**: 只修复当前的测试失败
|
||||
- **验证修复结果**: 确保修复后测试通过且无副作用
|
||||
- **提供修复总结**: 说明错误原因和修复方法
|
||||
- **Model 测试安全第一**: 必须包含用户权限检查和对应的安全测试
|
||||
- **Model 双环境验证**: 必须在 PGLite 和 PostgreSQL 两个环境下都验证通过
|
||||
@@ -1,453 +0,0 @@
|
||||
---
|
||||
globs: src/database/**/*.test.ts
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
## 🗃️ 数据库 Model 测试指南
|
||||
|
||||
### 测试环境选择 💡
|
||||
|
||||
数据库 Model 层通过环境变量控制数据库类型,在两种测试环境下有不同的数据库后端:客户端环境 (PGLite) 和 服务端环境 (PostgreSQL)
|
||||
|
||||
### ⚠️ 双环境验证要求
|
||||
|
||||
**对于所有 Model 测试,必须在两个环境下都验证通过**:
|
||||
|
||||
#### 完整验证流程
|
||||
|
||||
```bash
|
||||
# 1. 先在客户端环境测试(快速验证)
|
||||
npx vitest run --config vitest.config.ts src/database/models/__tests__/myModel.test.ts
|
||||
|
||||
# 2. 再在服务端环境测试(兼容性验证)
|
||||
npx vitest run --config vitest.config.server.ts src/database/models/__tests__/myModel.test.ts
|
||||
```
|
||||
|
||||
### 创建新 Model 测试的最佳实践 📋
|
||||
|
||||
#### 1. 参考现有实现和测试模板
|
||||
|
||||
创建新 Model 测试前,**必须先参考现有的实现模式**:
|
||||
|
||||
- **Model 实现参考**:
|
||||
- **测试模板参考**:
|
||||
- **复杂示例参考**:
|
||||
|
||||
#### 2. 用户权限检查 - 安全第一 🔒
|
||||
|
||||
这是**最关键的安全要求**。所有涉及用户数据的操作都必须包含用户权限检查:
|
||||
|
||||
**❌ 错误示例 - 存在安全漏洞**:
|
||||
|
||||
```typescript
|
||||
// 危险:缺少用户权限检查,任何用户都能操作任何数据
|
||||
update = async (id: string, data: Partial<MyModel>) => {
|
||||
return this.db
|
||||
.update(myTable)
|
||||
.set(data)
|
||||
.where(eq(myTable.id, id)) // ❌ 只检查 ID,没有检查 userId
|
||||
.returning();
|
||||
};
|
||||
```
|
||||
|
||||
**✅ 正确示例 - 安全的实现**:
|
||||
|
||||
```typescript
|
||||
// 安全:必须同时匹配 ID 和 userId
|
||||
update = async (id: string, data: Partial<MyModel>) => {
|
||||
return this.db
|
||||
.update(myTable)
|
||||
.set(data)
|
||||
.where(
|
||||
and(
|
||||
eq(myTable.id, id),
|
||||
eq(myTable.userId, this.userId), // ✅ 用户权限检查
|
||||
),
|
||||
)
|
||||
.returning();
|
||||
};
|
||||
```
|
||||
|
||||
**必须进行用户权限检查的方法**:
|
||||
|
||||
- `update()` - 更新操作
|
||||
- `delete()` - 删除操作
|
||||
- `findById()` - 查找特定记录
|
||||
- 任何涉及特定记录的查询或修改操作
|
||||
|
||||
#### 3. 测试文件结构和必测场景
|
||||
|
||||
**基本测试结构**:
|
||||
|
||||
```typescript
|
||||
// @vitest-environment node
|
||||
describe('MyModel', () => {
|
||||
describe('create', () => {
|
||||
it('should create a new record');
|
||||
it('should handle edge cases');
|
||||
});
|
||||
|
||||
describe('queryAll', () => {
|
||||
it('should return records for current user only');
|
||||
it('should handle empty results');
|
||||
});
|
||||
|
||||
describe('update', () => {
|
||||
it('should update own records');
|
||||
it('should NOT update other users records'); // 🔒 安全测试
|
||||
});
|
||||
|
||||
describe('delete', () => {
|
||||
it('should delete own records');
|
||||
it('should NOT delete other users records'); // 🔒 安全测试
|
||||
});
|
||||
|
||||
describe('user isolation', () => {
|
||||
it('should enforce user data isolation'); // 🔒 核心安全测试
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**必须测试的安全场景** 🔒:
|
||||
|
||||
```typescript
|
||||
it('should not update records of other users', async () => {
|
||||
// 创建其他用户的记录
|
||||
const [otherUserRecord] = await serverDB
|
||||
.insert(myTable)
|
||||
.values({ userId: 'other-user', data: 'original' })
|
||||
.returning();
|
||||
|
||||
// 尝试更新其他用户的记录
|
||||
const result = await myModel.update(otherUserRecord.id, { data: 'hacked' });
|
||||
|
||||
// 应该返回 undefined 或空数组(因为权限检查失败)
|
||||
expect(result).toBeUndefined();
|
||||
|
||||
// 验证原始数据未被修改
|
||||
const unchanged = await serverDB.query.myTable.findFirst({
|
||||
where: eq(myTable.id, otherUserRecord.id),
|
||||
});
|
||||
expect(unchanged?.data).toBe('original'); // 数据应该保持不变
|
||||
});
|
||||
```
|
||||
|
||||
#### 4. Mock 外部依赖服务
|
||||
|
||||
如果 Model 依赖外部服务(如 FileService),需要正确 Mock:
|
||||
|
||||
**设置 Mock**:
|
||||
|
||||
```typescript
|
||||
// 在文件顶部设置 Mock
|
||||
const mockGetFullFileUrl = vi.fn();
|
||||
vi.mock('@/server/services/file', () => ({
|
||||
FileService: vi.fn().mockImplementation(() => ({
|
||||
getFullFileUrl: mockGetFullFileUrl,
|
||||
})),
|
||||
}));
|
||||
|
||||
// 在 beforeEach 中重置和配置 Mock
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
mockGetFullFileUrl.mockImplementation((url: string) => `https://example.com/${url}`);
|
||||
});
|
||||
```
|
||||
|
||||
**验证 Mock 调用**:
|
||||
|
||||
```typescript
|
||||
it('should process URLs through FileService', async () => {
|
||||
// ... 测试逻辑
|
||||
|
||||
// 验证 Mock 被正确调用
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledWith('expected-url');
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
```
|
||||
|
||||
#### 5. 数据库状态管理
|
||||
|
||||
**正确的数据清理模式**:
|
||||
|
||||
```typescript
|
||||
const userId = 'test-user';
|
||||
const otherUserId = 'other-user';
|
||||
|
||||
beforeEach(async () => {
|
||||
// 清理用户表(级联删除相关数据)
|
||||
await serverDB.delete(users);
|
||||
|
||||
// 创建测试用户
|
||||
await serverDB.insert(users).values([{ id: userId }, { id: otherUserId }]);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// 清理测试数据
|
||||
await serverDB.delete(users);
|
||||
});
|
||||
```
|
||||
|
||||
#### 6. 测试数据类型和外键约束处理 ⚠️
|
||||
|
||||
**必须使用 Schema 导出的类型**:
|
||||
|
||||
```typescript
|
||||
// ✅ 正确:使用 schema 导出的类型
|
||||
import { NewGeneration, NewGenerationBatch } from '../../schemas';
|
||||
|
||||
const testBatch: NewGenerationBatch = {
|
||||
userId,
|
||||
generationTopicId: 'test-topic-id',
|
||||
provider: 'test-provider',
|
||||
model: 'test-model',
|
||||
prompt: 'Test prompt for image generation',
|
||||
width: 1024,
|
||||
height: 1024,
|
||||
config: {
|
||||
/* ... */
|
||||
},
|
||||
};
|
||||
|
||||
const testGeneration: NewGeneration = {
|
||||
id: 'test-gen-id',
|
||||
generationBatchId: 'test-batch-id',
|
||||
asyncTaskId: null, // 处理外键约束
|
||||
fileId: null, // 处理外键约束
|
||||
seed: 12345,
|
||||
userId,
|
||||
};
|
||||
```
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:没有类型声明或使用错误类型
|
||||
const testBatch = {
|
||||
// 缺少类型声明
|
||||
generationTopicId: 'test-topic-id',
|
||||
// ...
|
||||
};
|
||||
|
||||
const testGeneration = {
|
||||
// 缺少类型声明
|
||||
asyncTaskId: 'invalid-uuid', // 外键约束错误
|
||||
fileId: 'non-existent-file', // 外键约束错误
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
**外键约束处理策略**:
|
||||
|
||||
1. **使用 null 值**: 对于可选的外键字段,使用 null 避免约束错误
|
||||
2. **创建关联记录**: 如果需要测试关联关系,先创建被引用的记录
|
||||
3. **理解约束关系**: 了解哪些字段有外键约束,避免引用不存在的记录
|
||||
|
||||
```typescript
|
||||
// 外键约束处理示例
|
||||
beforeEach(async () => {
|
||||
// 清理数据库
|
||||
await serverDB.delete(users);
|
||||
|
||||
// 创建测试用户
|
||||
await serverDB.insert(users).values([{ id: userId }]);
|
||||
|
||||
// 如果需要测试文件关联,创建文件记录
|
||||
if (needsFileAssociation) {
|
||||
await serverDB.insert(files).values({
|
||||
id: 'test-file-id',
|
||||
userId,
|
||||
name: 'test.jpg',
|
||||
url: 'test-url',
|
||||
size: 1024,
|
||||
fileType: 'image/jpeg',
|
||||
});
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**排序测试的可预测性**:
|
||||
|
||||
```typescript
|
||||
// ✅ 正确:使用明确的时间戳确保排序结果可预测
|
||||
it('should find batches by topic id in correct order', async () => {
|
||||
const oldDate = new Date('2024-01-01T10:00:00Z');
|
||||
const newDate = new Date('2024-01-02T10:00:00Z');
|
||||
|
||||
const batch1 = { ...testBatch, prompt: 'First batch', userId, createdAt: oldDate };
|
||||
const batch2 = { ...testBatch, prompt: 'Second batch', userId, createdAt: newDate };
|
||||
|
||||
await serverDB.insert(generationBatches).values([batch1, batch2]);
|
||||
|
||||
const results = await generationBatchModel.findByTopicId(testTopic.id);
|
||||
|
||||
expect(results[0].prompt).toBe('Second batch'); // 最新优先 (desc order)
|
||||
expect(results[1].prompt).toBe('First batch');
|
||||
});
|
||||
```
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:依赖数据库的默认时间戳,结果不可预测
|
||||
it('should find batches by topic id', async () => {
|
||||
const batch1 = { ...testBatch, prompt: 'First batch', userId };
|
||||
const batch2 = { ...testBatch, prompt: 'Second batch', userId };
|
||||
|
||||
await serverDB.insert(generationBatches).values([batch1, batch2]);
|
||||
|
||||
// 插入顺序和数据库时间戳可能不一致,导致测试不稳定
|
||||
const results = await generationBatchModel.findByTopicId(testTopic.id);
|
||||
expect(results[0].prompt).toBe('Second batch'); // 可能失败
|
||||
});
|
||||
```
|
||||
|
||||
### 常见问题和解决方案 💡
|
||||
|
||||
#### 问题 1:权限检查缺失导致安全漏洞
|
||||
|
||||
**现象**: 测试失败,用户能修改其他用户的数据 **解决**: 在 Model 的 `update` 和 `delete` 方法中添加 `and(eq(table.id, id), eq(table.userId, this.userId))`
|
||||
|
||||
#### 问题 2:Mock 未生效或验证失败
|
||||
|
||||
**现象**: `undefined is not a spy` 错误 **解决**: 检查 Mock 设置位置和方式,确保在测试文件顶部设置,在 `beforeEach` 中重置
|
||||
|
||||
#### 问题 3:测试数据污染
|
||||
|
||||
**现象**: 测试间相互影响,结果不稳定 **解决**: 在 `beforeEach` 和 `afterEach` 中正确清理数据库状态
|
||||
|
||||
#### 问题 4:外部依赖导致测试失败
|
||||
|
||||
**现象**: 因为真实的外部服务调用导致测试不稳定 **解决**: Mock 所有外部依赖,使测试更可控和快速
|
||||
|
||||
#### 问题 5:外键约束违反导致测试失败
|
||||
|
||||
**现象**: `insert or update on table "xxx" violates foreign key constraint` **解决**:
|
||||
|
||||
- 将可选外键字段设为 `null` 而不是无效的字符串值
|
||||
- 或者先创建被引用的记录,再创建当前记录
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:无效的外键值
|
||||
const testData = {
|
||||
asyncTaskId: 'invalid-uuid', // 表中不存在此记录
|
||||
fileId: 'non-existent-file', // 表中不存在此记录
|
||||
};
|
||||
|
||||
// ✅ 正确:使用 null 值
|
||||
const testData = {
|
||||
asyncTaskId: null, // 避免外键约束
|
||||
fileId: null, // 避免外键约束
|
||||
};
|
||||
|
||||
// ✅ 或者:先创建被引用的记录
|
||||
beforeEach(async () => {
|
||||
const [asyncTask] = await serverDB.insert(asyncTasks).values({
|
||||
id: 'valid-task-id',
|
||||
status: 'pending',
|
||||
type: 'generation',
|
||||
}).returning();
|
||||
|
||||
const testData = {
|
||||
asyncTaskId: asyncTask.id, // 使用有效的外键值
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
#### 问题 6:排序测试结果不一致
|
||||
|
||||
**现象**: 相同的测试有时通过,有时失败,特别是涉及排序的测试 **解决**: 使用明确的时间戳,不要依赖数据库的默认时间戳
|
||||
|
||||
```typescript
|
||||
// ❌ 错误:依赖插入顺序和默认时间戳
|
||||
await serverDB.insert(table).values([data1, data2]); // 时间戳不可预测
|
||||
|
||||
// ✅ 正确:明确指定时间戳
|
||||
const oldDate = new Date('2024-01-01T10:00:00Z');
|
||||
const newDate = new Date('2024-01-02T10:00:00Z');
|
||||
await serverDB.insert(table).values([
|
||||
{ ...data1, createdAt: oldDate },
|
||||
{ ...data2, createdAt: newDate },
|
||||
]);
|
||||
```
|
||||
|
||||
#### 问题 7:Mock 验证失败或调用次数不匹配
|
||||
|
||||
**现象**: `expect(mockFunction).toHaveBeenCalledWith(...)` 失败 **解决**:
|
||||
|
||||
- 检查 Mock 函数的实际调用参数和期望参数是否完全匹配
|
||||
- 确认 Mock 在正确的时机被重置和配置
|
||||
- 使用 `toHaveBeenCalledTimes()` 验证调用次数
|
||||
|
||||
```typescript
|
||||
// 在 beforeEach 中正确配置 Mock
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks(); // 重置所有 Mock
|
||||
|
||||
mockGetFullFileUrl.mockImplementation((url: string) => `https://example.com/${url}`);
|
||||
mockTransformGeneration.mockResolvedValue({
|
||||
id: 'test-id',
|
||||
// ... 其他字段
|
||||
});
|
||||
});
|
||||
|
||||
// 测试中验证 Mock 调用
|
||||
it('should call FileService with correct parameters', async () => {
|
||||
await model.someMethod();
|
||||
|
||||
// 验证调用参数
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledWith('expected-url');
|
||||
// 验证调用次数
|
||||
expect(mockGetFullFileUrl).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
```
|
||||
|
||||
### Model 测试检查清单 ✅
|
||||
|
||||
创建 Model 测试时,请确保以下各项都已完成:
|
||||
|
||||
#### 🔧 基础配置
|
||||
|
||||
- [ ] **双环境验证** - 在客户端环境 (vitest.config.ts) 和服务端环境 (vitest.config.server.ts) 下都测试通过
|
||||
- [ ] 参考了 `_template.ts` 和现有 Model 的实现模式
|
||||
- [ ] **使用正确的 Schema 类型** - 测试数据使用 `NewXxx` 类型声明,如 `NewGenerationBatch`、`NewGeneration`
|
||||
|
||||
#### 🔒 安全测试
|
||||
|
||||
- [ ] **所有涉及用户数据的操作都包含用户权限检查**
|
||||
- [ ] 包含了用户权限隔离的安全测试
|
||||
- [ ] 测试了用户无法访问其他用户数据的场景
|
||||
|
||||
#### 🗃️ 数据处理
|
||||
|
||||
- [ ] **正确处理外键约束** - 使用 `null` 值或先创建被引用记录
|
||||
- [ ] **排序测试使用明确时间戳** - 不依赖数据库默认时间,确保结果可预测
|
||||
- [ ] 在 `beforeEach` 和 `afterEach` 中正确管理数据库状态
|
||||
- [ ] 所有测试都能独立运行且互不干扰
|
||||
|
||||
#### 🎭 Mock 和外部依赖
|
||||
|
||||
- [ ] 正确 Mock 了外部依赖服务 (如 FileService、GenerationModel)
|
||||
- [ ] 在 `beforeEach` 中重置和配置 Mock
|
||||
- [ ] 验证了 Mock 服务的调用参数和次数
|
||||
- [ ] 测试了外部服务错误场景的处理
|
||||
|
||||
#### 📋 测试覆盖
|
||||
|
||||
- [ ] 测试覆盖了所有主要方法 (create, query, update, delete)
|
||||
- [ ] 测试了边界条件和错误场景
|
||||
- [ ] 包含了空结果处理的测试
|
||||
- [ ] **确认两个环境下的测试结果一致**
|
||||
|
||||
#### 🚨 常见问题检查
|
||||
|
||||
- [ ] 没有外键约束违反错误
|
||||
- [ ] 排序测试结果稳定可预测
|
||||
- [ ] Mock 验证无失败
|
||||
- [ ] 无测试数据污染问题
|
||||
|
||||
### 安全警告 ⚠️
|
||||
|
||||
**数据库 Model 层是安全的第一道防线**。如果 Model 层缺少用户权限检查:
|
||||
|
||||
1. **任何用户都能访问和修改其他用户的数据**
|
||||
2. **即使上层有权限检查,也可能被绕过**
|
||||
3. **可能导致严重的数据泄露和安全事故**
|
||||
|
||||
因此,**每个涉及用户数据的 Model 方法都必须包含用户权限检查,且必须有对应的安全测试来验证这些检查的有效性**。
|
||||
@@ -1,80 +0,0 @@
|
||||
---
|
||||
description: Electron IPC 接口测试策略
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
### Electron IPC 接口测试策略 🖥️
|
||||
|
||||
对于涉及 Electron IPC 接口的测试,由于提供真实的 Electron 环境比较复杂,采用 **Mock 返回值** 的方式进行测试。
|
||||
|
||||
#### 基本 Mock 设置
|
||||
|
||||
```typescript
|
||||
import { vi } from 'vitest';
|
||||
|
||||
import { electronIpcClient } from '@/server/modules/ElectronIPCClient';
|
||||
|
||||
// Mock Electron IPC 客户端
|
||||
vi.mock('@/server/modules/ElectronIPCClient', () => ({
|
||||
electronIpcClient: {
|
||||
getFilePathById: vi.fn(),
|
||||
deleteFiles: vi.fn(),
|
||||
// 根据需要添加其他 IPC 方法
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
#### 在测试中设置 Mock 行为
|
||||
|
||||
```typescript
|
||||
beforeEach(() => {
|
||||
// 重置所有 Mock
|
||||
vi.resetAllMocks();
|
||||
|
||||
// 设置默认的 Mock 返回值
|
||||
vi.mocked(electronIpcClient.getFilePathById).mockResolvedValue('/path/to/file.txt');
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockResolvedValue({
|
||||
success: true,
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
#### 测试不同场景的示例
|
||||
|
||||
```typescript
|
||||
it('应该处理文件删除成功的情况', async () => {
|
||||
// 设置成功场景的 Mock
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockResolvedValue({
|
||||
success: true,
|
||||
});
|
||||
|
||||
const result = await service.deleteFiles(['desktop://file1.txt']);
|
||||
|
||||
expect(electronIpcClient.deleteFiles).toHaveBeenCalledWith(['desktop://file1.txt']);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('应该处理文件删除失败的情况', async () => {
|
||||
// 设置失败场景的 Mock
|
||||
vi.mocked(electronIpcClient.deleteFiles).mockRejectedValue(new Error('删除失败'));
|
||||
|
||||
const result = await service.deleteFiles(['desktop://file1.txt']);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errors).toBeDefined();
|
||||
});
|
||||
```
|
||||
|
||||
#### Mock 策略的优势
|
||||
|
||||
1. **环境简化**: 避免了复杂的 Electron 环境搭建
|
||||
2. **测试可控**: 可以精确控制 IPC 调用的返回值和行为
|
||||
3. **场景覆盖**: 容易测试各种成功/失败场景
|
||||
4. **执行速度**: Mock 调用比真实 IPC 调用更快
|
||||
|
||||
#### 注意事项
|
||||
|
||||
- **Mock 准确性**: 确保 Mock 的行为与真实 IPC 接口行为一致
|
||||
- **类型安全**: 使用 `vi.mocked()` 确保类型安全
|
||||
- **Mock 重置**: 在 `beforeEach` 中重置 Mock 状态,避免测试间干扰
|
||||
- **调用验证**: 不仅要验证返回值,还要验证 IPC 方法是否被正确调用
|
||||
@@ -1,496 +0,0 @@
|
||||
---
|
||||
globs: *.test.ts,*.test.tsx
|
||||
alwaysApply: false
|
||||
---
|
||||
|
||||
# 测试指南 - LobeChat Testing Guide
|
||||
|
||||
## 🧪 测试环境概览
|
||||
|
||||
LobeChat 项目使用 Vitest 测试库,配置了两种不同的测试环境:
|
||||
|
||||
### 客户端测试环境 (DOM Environment)
|
||||
|
||||
- **配置文件**: [vitest.config.ts](mdc:vitest.config.ts)
|
||||
- **环境**: Happy DOM (浏览器环境模拟)
|
||||
- **数据库**: PGLite (浏览器环境的 PostgreSQL)
|
||||
- **用途**: 测试前端组件、客户端逻辑、React 组件等
|
||||
- **设置文件**: [tests/setup.ts](mdc:tests/setup.ts)
|
||||
|
||||
### 服务端测试环境 (Node Environment)
|
||||
|
||||
- **配置文件**: [vitest.config.server.ts](mdc:vitest.config.server.ts)
|
||||
- **环境**: Node.js
|
||||
- **数据库**: 真实的 PostgreSQL 数据库
|
||||
- **并发限制**: 单线程运行 (`singleFork: true`)
|
||||
- **用途**: 测试数据库模型、服务端逻辑、API 端点等
|
||||
- **设置文件**: [tests/setup-db.ts](mdc:tests/setup-db.ts)
|
||||
|
||||
## 🚀 测试运行命令
|
||||
|
||||
**🚨 性能警告**: 项目包含 3000+ 测试用例,完整运行需要约 10 分钟。务必使用文件过滤或测试名称过滤。
|
||||
|
||||
### ✅ 正确的命令格式
|
||||
|
||||
```bash
|
||||
# 运行所有客户端/服务端测试
|
||||
npx vitest run --config vitest.config.ts # 客户端测试
|
||||
npx vitest run --config vitest.config.server.ts # 服务端测试
|
||||
|
||||
# 运行特定测试文件 (支持模糊匹配)
|
||||
npx vitest run --config vitest.config.ts user.test.ts
|
||||
|
||||
# 运行特定测试用例名称 (使用 -t 参数)
|
||||
npx vitest run --config vitest.config.ts -t "test case name"
|
||||
|
||||
# 组合使用文件和测试名称过滤
|
||||
npx vitest run --config vitest.config.ts filename.test.ts -t "specific test"
|
||||
|
||||
# 生成覆盖率报告 (使用 --coverage 参数)
|
||||
npx vitest run --config vitest.config.ts --coverage
|
||||
```
|
||||
|
||||
### ❌ 避免的命令格式
|
||||
|
||||
```bash
|
||||
# ❌ 这些命令会运行所有 3000+ 测试用例,耗时约 10 分钟!
|
||||
npm test
|
||||
npm test some-file.test.ts
|
||||
|
||||
# ❌ 不要使用裸 vitest (会进入 watch 模式)
|
||||
vitest test-file.test.ts
|
||||
```
|
||||
|
||||
## 🔧 测试修复原则
|
||||
|
||||
### 核心原则 ⚠️
|
||||
|
||||
1. **充分阅读测试代码**: 在修复测试之前,必须完整理解测试的意图和实现
|
||||
2. **测试优先修复**: 如果是测试本身写错了,修改测试而不是实现代码
|
||||
3. **专注单一问题**: 只修复指定的测试,不要添加额外测试或功能
|
||||
4. **不自作主张**: 不要因为发现其他问题就直接修改,先提出再讨论
|
||||
|
||||
### 测试协作最佳实践 🤝
|
||||
|
||||
基于实际开发经验总结的重要协作原则:
|
||||
|
||||
#### 1. 失败处理策略
|
||||
|
||||
**核心原则**: 避免盲目重试,快速识别问题并寻求帮助。
|
||||
|
||||
- **失败阈值**: 当连续尝试修复测试 1-2 次都失败后,应立即停止继续尝试
|
||||
- **问题总结**: 分析失败原因,整理已尝试的解决方案及其失败原因
|
||||
- **寻求帮助**: 带着清晰的问题摘要和尝试记录向团队寻求帮助
|
||||
- **避免陷阱**: 不要陷入"不断尝试相同或类似方法"的循环
|
||||
|
||||
```typescript
|
||||
// ❌ 错误做法:连续失败后继续盲目尝试
|
||||
// 第3次、第4次仍在用相似的方法修复同一个问题
|
||||
|
||||
// ✅ 正确做法:失败1-2次后总结问题
|
||||
/*
|
||||
问题总结:
|
||||
1. 尝试过的方法:修改 mock 数据结构
|
||||
2. 失败原因:仍然提示类型不匹配
|
||||
3. 具体错误:Expected 'UserData' but received 'UserProfile'
|
||||
4. 需要帮助:不确定最新的 UserData 接口定义
|
||||
*/
|
||||
```
|
||||
|
||||
#### 2. 测试用例命名规范
|
||||
|
||||
**核心原则**: 测试应该关注"行为",而不是"实现细节"。
|
||||
|
||||
- **描述业务场景**: `describe` 和 `it` 的标题应该描述具体的业务场景和预期行为
|
||||
- **避免实现绑定**: 不要在测试名称中提及具体的代码行号、覆盖率目标或实现细节
|
||||
- **保持稳定性**: 测试名称应该在代码重构后仍然有意义
|
||||
|
||||
```typescript
|
||||
// ❌ 错误的测试命名
|
||||
describe('User component coverage', () => {
|
||||
it('covers line 45-50 in getUserData', () => {
|
||||
// 为了覆盖第45-50行而写的测试
|
||||
});
|
||||
|
||||
it('tests the else branch', () => {
|
||||
// 仅为了测试某个分支而存在
|
||||
});
|
||||
});
|
||||
|
||||
// ✅ 正确的测试命名
|
||||
describe('<UserAvatar />', () => {
|
||||
it('should render fallback icon when image url is not provided', () => {
|
||||
// 测试具体的业务场景,自然会覆盖相关代码分支
|
||||
});
|
||||
|
||||
it('should display user initials when avatar image fails to load', () => {
|
||||
// 描述用户行为和预期结果
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**覆盖率提升的正确思路**:
|
||||
|
||||
- ✅ 通过设计各种业务场景(正常流程、边缘情况、错误处理)来自然提升覆盖率
|
||||
- ❌ 不要为了达到覆盖率数字而写测试,更不要在测试中注释"为了覆盖 xxx 行"
|
||||
|
||||
#### 3. 测试组织结构
|
||||
|
||||
**核心原则**: 维护清晰的测试层次结构,避免冗余的顶级测试块。
|
||||
|
||||
- **复用现有结构**: 添加新测试时,优先在现有的 `describe` 块中寻找合适的位置
|
||||
- **逻辑分组**: 相关的测试用例应该组织在同一个 `describe` 块内
|
||||
- **避免碎片化**: 不要为了单个测试用例就创建新的顶级 `describe` 块
|
||||
|
||||
```typescript
|
||||
// ❌ 错误的组织方式:创建过多顶级块
|
||||
describe('<UserProfile />', () => {
|
||||
it('should render user name', () => {});
|
||||
});
|
||||
|
||||
describe('UserProfile new prop test', () => {
|
||||
// 不必要的新块
|
||||
it('should handle email display', () => {});
|
||||
});
|
||||
|
||||
describe('UserProfile edge cases', () => {
|
||||
// 不必要的新块
|
||||
it('should handle missing avatar', () => {});
|
||||
});
|
||||
|
||||
// ✅ 正确的组织方式:合并相关测试
|
||||
describe('<UserProfile />', () => {
|
||||
it('should render user name', () => {});
|
||||
|
||||
it('should handle email display', () => {});
|
||||
|
||||
it('should handle missing avatar', () => {});
|
||||
|
||||
describe('when user data is incomplete', () => {
|
||||
// 只有在有多个相关子场景时才创建子组
|
||||
it('should show placeholder for missing name', () => {});
|
||||
it('should hide email section when email is undefined', () => {});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**组织决策流程**:
|
||||
|
||||
1. 是否存在逻辑相关的现有 `describe` 块? → 如果有,添加到其中
|
||||
2. 是否有多个(3个以上)相关的测试用例? → 如果有,可以考虑创建新的子 `describe`
|
||||
3. 是否是独立的、无关联的功能模块? → 如果是,才考虑创建新的顶级 `describe`
|
||||
|
||||
### 测试修复流程
|
||||
|
||||
1. **复现问题**: 定位并运行失败的测试,确认能在本地复现
|
||||
2. **分析原因**: 阅读测试代码、错误日志和相关文件的 Git 修改历史
|
||||
3. **建立假设**: 判断问题出在测试逻辑、实现代码还是环境配置
|
||||
4. **修复验证**: 根据假设进行修复,重新运行测试确认通过
|
||||
5. **扩大验证**: 运行当前文件内所有测试,确保没有引入新问题
|
||||
6. **撰写总结**: 说明错误原因和修复方法
|
||||
|
||||
### 修复完成后的总结
|
||||
|
||||
测试修复完成后,应该提供简要说明,包括:
|
||||
|
||||
1. **错误原因分析**: 说明测试失败的根本原因
|
||||
- 测试逻辑错误
|
||||
- 实现代码bug
|
||||
- 环境配置问题
|
||||
- 依赖变更导致的问题
|
||||
|
||||
2. **修复方法说明**: 简述采用的修复方式
|
||||
- 修改了哪些文件
|
||||
- 采用了什么解决方案
|
||||
- 为什么选择这种修复方式
|
||||
|
||||
**示例格式**:
|
||||
|
||||
```markdown
|
||||
## 测试修复总结
|
||||
|
||||
**错误原因**: 测试中的 mock 数据格式与实际 API 返回格式不匹配,导致断言失败。
|
||||
|
||||
**修复方法**: 更新了测试文件中的 mock 数据结构,使其与最新的 API 响应格式保持一致。具体修改了 `user.test.ts` 中的 `mockUserData` 对象结构。
|
||||
```
|
||||
|
||||
## 🎯 测试编写最佳实践
|
||||
|
||||
### Mock 数据策略:追求"低成本的真实性" 📋
|
||||
|
||||
**核心原则**: 测试数据应默认追求真实性,只有在引入"高昂的测试成本"时才进行简化。
|
||||
|
||||
#### 什么是"高昂的测试成本"?
|
||||
|
||||
"高成本"指的是测试中引入了外部依赖,使测试变慢、不稳定或复杂:
|
||||
|
||||
- **文件 I/O 操作**:读写硬盘文件
|
||||
- **网络请求**:HTTP 调用、数据库连接
|
||||
- **系统调用**:获取系统时间、环境变量等
|
||||
|
||||
#### ✅ 推荐做法:Mock 依赖,保留真实数据
|
||||
|
||||
```typescript
|
||||
// ✅ 好的做法:Mock I/O 操作,但使用真实的文件内容格式
|
||||
describe('parseContentType', () => {
|
||||
beforeEach(() => {
|
||||
// Mock 文件读取操作(避免真实 I/O)
|
||||
vi.spyOn(fs, 'readFileSync').mockImplementation((path) => {
|
||||
// 但返回真实的文件内容格式
|
||||
if (path.includes('.pdf')) return '%PDF-1.4\n%âãÏÓ'; // 真实 PDF 文件头
|
||||
if (path.includes('.png')) return '\x89PNG\r\n\x1a\n'; // 真实 PNG 文件头
|
||||
return '';
|
||||
});
|
||||
});
|
||||
|
||||
it('should detect PDF content type correctly', () => {
|
||||
const result = parseContentType('/path/to/file.pdf');
|
||||
expect(result).toBe('application/pdf');
|
||||
});
|
||||
});
|
||||
|
||||
// ❌ 过度简化:使用不真实的数据
|
||||
describe('parseContentType', () => {
|
||||
it('should detect PDF content type correctly', () => {
|
||||
// 这种简化数据没有测试价值
|
||||
const result = parseContentType('fake-pdf-content');
|
||||
expect(result).toBe('application/pdf');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
#### 🎯 真实标识符的价值
|
||||
|
||||
```typescript
|
||||
// ✅ 使用真实的提供商标识符
|
||||
it('should parse OpenAI model list correctly', () => {
|
||||
const result = parseModelString('openai', '+gpt-4,+gpt-3.5-turbo');
|
||||
expect(result.add).toHaveLength(2);
|
||||
expect(result.add[0].id).toBe('gpt-4');
|
||||
});
|
||||
|
||||
// ❌ 使用占位符标识符(价值较低)
|
||||
it('should parse model list correctly', () => {
|
||||
const result = parseModelString('test-provider', '+model1,+model2');
|
||||
expect(result.add).toHaveLength(2);
|
||||
// 这种测试对理解真实场景帮助不大
|
||||
});
|
||||
```
|
||||
|
||||
### 错误处理测试:测试"行为"而非"文本" ⚠️
|
||||
|
||||
**核心原则**: 测试应该验证程序在错误发生时的行为是可预测的,而不是验证易变的错误信息文本。
|
||||
|
||||
#### ✅ 推荐的错误测试方式
|
||||
|
||||
```typescript
|
||||
// ✅ 测试是否抛出错误
|
||||
it('should throw error when invalid input provided', () => {
|
||||
expect(() => processInput(null)).toThrow();
|
||||
});
|
||||
|
||||
// ✅ 测试错误类型(最推荐)
|
||||
it('should throw ValidationError for invalid data', () => {
|
||||
expect(() => validateUser({})).toThrow(ValidationError);
|
||||
});
|
||||
|
||||
// ✅ 测试错误属性而非消息文本
|
||||
it('should throw error with correct error code', () => {
|
||||
expect(() => processPayment({})).toThrow(
|
||||
expect.objectContaining({
|
||||
code: 'INVALID_PAYMENT_DATA',
|
||||
statusCode: 400,
|
||||
}),
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
#### ❌ 应避免的做法
|
||||
|
||||
```typescript
|
||||
// ❌ 过度依赖具体错误信息文本
|
||||
it('should throw specific error message', () => {
|
||||
expect(() => processUser({})).toThrow('用户数据不能为空,请检查输入参数');
|
||||
// 这种测试很脆弱,错误文案稍有修改就会失败
|
||||
});
|
||||
```
|
||||
|
||||
#### 🎯 例外情况:何时可以测试错误信息
|
||||
|
||||
```typescript
|
||||
// ✅ 测试标准 API 错误(这是契约的一部分)
|
||||
it('should return proper HTTP error for API', () => {
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.error).toBe('Bad Request');
|
||||
});
|
||||
|
||||
// ✅ 测试错误信息的关键部分(使用正则)
|
||||
it('should include field name in validation error', () => {
|
||||
expect(() => validateField('email', '')).toThrow(/email/i);
|
||||
});
|
||||
```
|
||||
|
||||
### 疑难解答:警惕模块污染 🚨
|
||||
|
||||
**识别信号**: 当你的测试出现以下"灵异"现象时,优先怀疑模块污染:
|
||||
|
||||
- 单独运行某个测试通过,但和其他测试一起运行就失败
|
||||
- 测试的执行顺序影响结果
|
||||
- Mock 设置看起来正确,但实际使用的是旧的 Mock 版本
|
||||
|
||||
#### 典型场景:动态 Mock 同一模块
|
||||
|
||||
```typescript
|
||||
// ❌ 容易出现模块污染的写法
|
||||
describe('ConfigService', () => {
|
||||
it('should work in development mode', async () => {
|
||||
vi.doMock('./config', () => ({ isDev: true }));
|
||||
const { getSettings } = await import('./configService'); // 第一次加载
|
||||
expect(getSettings().debugMode).toBe(true);
|
||||
});
|
||||
|
||||
it('should work in production mode', async () => {
|
||||
vi.doMock('./config', () => ({ isDev: false }));
|
||||
const { getSettings } = await import('./configService'); // 可能使用缓存的旧版本!
|
||||
expect(getSettings().debugMode).toBe(false); // ❌ 可能失败
|
||||
});
|
||||
});
|
||||
|
||||
// ✅ 使用 resetModules 解决模块污染
|
||||
describe('ConfigService', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules(); // 清除模块缓存,确保每个测试都是干净的环境
|
||||
});
|
||||
|
||||
it('should work in development mode', async () => {
|
||||
vi.doMock('./config', () => ({ isDev: true }));
|
||||
const { getSettings } = await import('./configService');
|
||||
expect(getSettings().debugMode).toBe(true);
|
||||
});
|
||||
|
||||
it('should work in production mode', async () => {
|
||||
vi.doMock('./config', () => ({ isDev: false }));
|
||||
const { getSettings } = await import('./configService');
|
||||
expect(getSettings().debugMode).toBe(false); // ✅ 测试通过
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
#### 🔧 排查和解决步骤
|
||||
|
||||
1. **识别问题**: 测试失败时,首先问自己:"是否有多个测试在 Mock 同一个模块?"
|
||||
2. **添加隔离**: 在 `beforeEach` 中添加 `vi.resetModules()`
|
||||
3. **验证修复**: 重新运行测试,确认问题解决
|
||||
|
||||
**记住**: `vi.resetModules()` 是解决测试"灵异"失败的终极武器,当常规调试方法都无效时,它往往能一针见血地解决问题。
|
||||
|
||||
## 📂 测试文件组织
|
||||
|
||||
### 文件命名约定
|
||||
|
||||
- **客户端测试**: `*.test.ts`, `*.test.tsx` (任意位置)
|
||||
- **服务端测试**: `src/database/models/**/*.test.ts`, `src/database/server/**/*.test.ts` (限定路径)
|
||||
|
||||
### 测试文件组织风格
|
||||
|
||||
项目采用 **测试文件与源文件同目录** 的组织风格:
|
||||
|
||||
- 测试文件放在对应源文件的同一目录下
|
||||
- 命名格式:`原文件名.test.ts` 或 `原文件名.test.tsx`
|
||||
|
||||
例如:
|
||||
|
||||
```plaintext
|
||||
src/components/Button/
|
||||
├── index.tsx # 源文件
|
||||
└── index.test.tsx # 测试文件
|
||||
```
|
||||
|
||||
## 🛠️ 测试调试技巧
|
||||
|
||||
### 测试调试步骤
|
||||
|
||||
1. **确定测试环境**: 根据文件路径选择正确的配置文件
|
||||
2. **隔离问题**: 使用 `-t` 参数只运行失败的测试用例
|
||||
3. **分析错误**: 仔细阅读错误信息、堆栈跟踪和最近的文件修改记录
|
||||
4. **添加调试**: 在测试中添加 `console.log` 了解执行流程
|
||||
|
||||
### TypeScript 类型处理 📝
|
||||
|
||||
在测试中,为了提高编写效率和可读性,可以适当放宽 TypeScript 类型检测:
|
||||
|
||||
#### ✅ 推荐的类型放宽策略
|
||||
|
||||
```typescript
|
||||
// ✅ 使用非空断言访问测试中确定存在的属性
|
||||
const result = await someFunction();
|
||||
expect(result!.data).toBeDefined();
|
||||
expect(result!.status).toBe('success');
|
||||
|
||||
// ✅ 使用 any 类型简化复杂的 Mock 设置
|
||||
const mockStream = new ReadableStream() as any;
|
||||
mockStream.toReadableStream = () => mockStream;
|
||||
```
|
||||
|
||||
#### 🎯 适用场景
|
||||
|
||||
- **Mock 对象**: 对于测试用的 Mock 数据,使用 `as any` 避免复杂的类型定义
|
||||
- **第三方库**: 处理复杂的第三方库类型时,适当使用 `any` 提高效率
|
||||
- **测试断言**: 在确定对象存在的测试场景中,使用 `!` 非空断言
|
||||
- **临时调试**: 快速编写测试时,先用 `any` 保证功能,后续可选择性地优化类型
|
||||
|
||||
#### ⚠️ 注意事项
|
||||
|
||||
- **适度使用**: 不要过度依赖 `any`,核心业务逻辑的类型仍应保持严格
|
||||
- **文档说明**: 对于使用 `any` 的复杂场景,添加注释说明原因
|
||||
- **测试覆盖**: 确保即使使用了 `any`,测试仍能有效验证功能正确性
|
||||
|
||||
### 检查最近修改记录 🔍
|
||||
|
||||
系统性地检查相关文件的修改历史是问题定位的关键步骤。
|
||||
|
||||
#### 三步检查法
|
||||
|
||||
**Step 1: 查看当前状态**
|
||||
|
||||
```bash
|
||||
git status # 查看未提交的修改
|
||||
git diff path/to/component.test.ts | cat # 查看测试文件修改
|
||||
git diff path/to/component.ts | cat # 查看实现文件修改
|
||||
```
|
||||
|
||||
**Step 2: 查看提交历史**
|
||||
|
||||
```bash
|
||||
git log --pretty=format:"%h %ad %s" --date=relative -3 path/to/component.ts | cat
|
||||
```
|
||||
|
||||
**Step 3: 查看具体修改内容**
|
||||
|
||||
```bash
|
||||
git show HEAD -- path/to/component.ts | cat # 查看最新提交的修改
|
||||
```
|
||||
|
||||
#### 时间相关性判断
|
||||
|
||||
- **24小时内的提交**: 🔴 **高度相关** - 很可能是直接原因
|
||||
- **1-7天内的提交**: 🟡 **中等相关** - 需要仔细分析
|
||||
- **超过1周的提交**: ⚪ **低相关性** - 除非重大重构
|
||||
|
||||
## 特殊场景的测试
|
||||
|
||||
针对一些特殊场景的测试,需要阅读相关 rules:
|
||||
|
||||
- [Electron IPC 接口测试策略](mdc:./electron-ipc-test.mdc)
|
||||
- [数据库 Model 测试指南](mdc:./db-model-test.mdc)
|
||||
|
||||
## 🎯 核心要点
|
||||
|
||||
- **命令格式**: 使用 `npx vitest run --config [config-file]` 并指定文件过滤
|
||||
- **修复原则**: 失败1-2次后寻求帮助,测试命名关注行为而非实现细节
|
||||
- **调试流程**: 复现 → 分析 → 假设 → 修复 → 验证 → 总结
|
||||
- **文件组织**: 优先在现有 `describe` 块中添加测试,避免创建冗余顶级块
|
||||
- **数据策略**: 默认追求真实性,只有高成本(I/O、网络等)时才简化
|
||||
- **错误测试**: 测试错误类型和行为,避免依赖具体的错误信息文本
|
||||
- **模块污染**: 测试"灵异"失败时,优先怀疑模块污染,使用 `vi.resetModules()` 解决
|
||||
- **安全要求**: Model 测试必须包含权限检查,并在双环境下验证通过
|
||||
@@ -16,6 +16,4 @@ TypeScript Code Style Guide:
|
||||
- Always refactor repeated logic into a reusable function
|
||||
- Don't remove meaningful code comments, be sure to keep original comments when providing applied code
|
||||
- Update the code comments when needed after you modify the related code
|
||||
- Please respect my prettier preferences when you provide code
|
||||
- Prefer object destructuring when accessing and using properties
|
||||
- Prefer async version api than sync version, eg: use readFile from 'fs/promises' instead of 'fs'
|
||||
- Please respect my prettier preferences when you provide code
|
||||
@@ -232,10 +232,3 @@ OPENAI_API_KEY=sk-xxxxxxxxx
|
||||
|
||||
# Specify the Embedding model and Reranker model(unImplemented)
|
||||
# DEFAULT_FILES_CONFIG="embedding_model=openai/embedding-text-3-small,reranker_model=cohere/rerank-english-v3.0,query_mode=full_text"
|
||||
|
||||
########################################
|
||||
########## MCP Service Config ##########
|
||||
########################################
|
||||
|
||||
# MCP tool call timeout (milliseconds)
|
||||
# MCP_TOOL_TIMEOUT=60000
|
||||
|
||||
@@ -36,16 +36,6 @@ config.overrides = [
|
||||
'mdx/code-blocks': false,
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
files: ['src/store/image/**/*', 'src/types/generation/**/*'],
|
||||
rules: {
|
||||
'@typescript-eslint/no-empty-interface': 0,
|
||||
'sort-keys-fix/sort-keys-fix': 0,
|
||||
'typescript-sort-keys/interface': 0,
|
||||
'typescript-sort-keys/string-enum': 0,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
module.exports = config;
|
||||
|
||||
@@ -155,9 +155,9 @@ jobs:
|
||||
KEY_VAULTS_SECRET: 'oLXWIiR/AKF+rWaqy9lHkrYgzpATbW3CtJp3UfkVgpE='
|
||||
NEXT_PUBLIC_DESKTOP_PROJECT_ID: ${{ secrets.UMAMI_NIGHTLY_DESKTOP_PROJECT_ID }}
|
||||
NEXT_PUBLIC_DESKTOP_UMAMI_BASE_URL: ${{ secrets.UMAMI_NIGHTLY_DESKTOP_BASE_URL }}
|
||||
# 将 TEMP 和 TMP 目录设置到 C 盘
|
||||
TEMP: C:\temp
|
||||
TMP: C:\temp
|
||||
# 将 TEMP 和 TMP 目录设置到 D 盘
|
||||
TEMP: D:\temp
|
||||
TMP: D:\temp
|
||||
|
||||
# Linux 平台构建处理
|
||||
- name: Build artifact on Linux
|
||||
|
||||
@@ -12,7 +12,8 @@ concurrency:
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
REGISTRY_IMAGE: lobehub/lobe-chat-database
|
||||
REGISTRY_URL: jaworldwide.azurecr.io
|
||||
REGISTRY_IMAGE: jaworldwide.azurecr.io/oneja/ai/bot-database
|
||||
PR_TAG_PREFIX: pr-
|
||||
|
||||
jobs:
|
||||
@@ -70,8 +71,9 @@ jobs:
|
||||
- name: Docker login
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_REGISTRY_USER }}
|
||||
password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
|
||||
registry: ${{ env.REGISTRY_URL }}
|
||||
username: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||
password: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }}
|
||||
|
||||
- name: Get commit SHA
|
||||
if: github.ref == 'refs/heads/main'
|
||||
@@ -147,8 +149,9 @@ jobs:
|
||||
- name: Docker login
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_REGISTRY_USER }}
|
||||
password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
|
||||
registry: ${{ env.REGISTRY_URL }}
|
||||
username: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||
password: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }}
|
||||
|
||||
- name: Create manifest list and push
|
||||
working-directory: /tmp/digests
|
||||
|
||||
@@ -2,7 +2,7 @@ name: Lighthouse Badger
|
||||
|
||||
env:
|
||||
TOKEN_NAME: 'GH_TOKEN'
|
||||
REPO_BRANCH: 'lobehub/lobe-chat lighthouse'
|
||||
REPO_BRANCH: 'jaworldwideorg/OneJA-Bot lighthouse'
|
||||
USER_NAME: 'lobehubbot'
|
||||
USER_EMAIL: 'i@lobehub.com'
|
||||
AUDIT_TYPE: 'both'
|
||||
|
||||
@@ -139,9 +139,9 @@ jobs:
|
||||
NEXT_PUBLIC_DESKTOP_PROJECT_ID: ${{ secrets.UMAMI_BETA_DESKTOP_PROJECT_ID }}
|
||||
NEXT_PUBLIC_DESKTOP_UMAMI_BASE_URL: ${{ secrets.UMAMI_BETA_DESKTOP_BASE_URL }}
|
||||
|
||||
# 将 TEMP 和 TMP 目录设置到 C 盘
|
||||
TEMP: C:\temp
|
||||
TMP: C:\temp
|
||||
# 将 TEMP 和 TMP 目录设置到 D 盘
|
||||
TEMP: D:\temp
|
||||
TMP: D:\temp
|
||||
|
||||
# Linux 平台构建处理
|
||||
- name: Build artifact on Linux
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
name: Release CI
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
issues: write
|
||||
actions: write
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
release:
|
||||
|
||||
@@ -9,6 +9,12 @@ on:
|
||||
schedule:
|
||||
- cron: '0 */6 * * *' # every 6 hours
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
ignore_files:
|
||||
description: 'Files to ignore during sync (comma-separated)'
|
||||
required: false
|
||||
default: 'changelog/*,CHANGELOG.md'
|
||||
type: string
|
||||
|
||||
jobs:
|
||||
sync_latest_from_upstream:
|
||||
@@ -17,13 +23,50 @@ jobs:
|
||||
if: ${{ github.event.repository.fork }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Clean issue notice
|
||||
uses: actions-cool/issues-helper@v3
|
||||
with:
|
||||
actions: 'close-issues'
|
||||
labels: '🚨 Sync Fail'
|
||||
labels: '🚨 Sync Failed'
|
||||
|
||||
- name: Extract ignore files
|
||||
id: extract_ignore
|
||||
run: |
|
||||
# Default files to ignore
|
||||
DEFAULT_IGNORE="Changelog.md"
|
||||
|
||||
# Get input files to ignore or use default
|
||||
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
|
||||
IGNORE_FILES="${{ github.event.inputs.ignore_files }}"
|
||||
else
|
||||
IGNORE_FILES="$DEFAULT_IGNORE"
|
||||
fi
|
||||
|
||||
# Convert to array and prepare for output
|
||||
echo "IGNORE_FILES=$IGNORE_FILES" >> $GITHUB_ENV
|
||||
echo "ignore_files=$IGNORE_FILES" >> $GITHUB_OUTPUT
|
||||
|
||||
# Create a backup of files to be preserved
|
||||
mkdir -p .file_backup
|
||||
|
||||
# Process each pattern (comma-separated)
|
||||
for pattern in $(echo $IGNORE_FILES | tr ',' ' '); do
|
||||
# Use find to handle wildcards
|
||||
for file in $(find . -type f -path "./$pattern" 2>/dev/null || echo "$pattern"); do
|
||||
# Skip if the file doesn't exist or if it's the find command returning the pattern
|
||||
if [ -f "$file" ]; then
|
||||
echo "Backing up $file"
|
||||
# Create directory structure in backup
|
||||
mkdir -p ".file_backup/$(dirname "$file")"
|
||||
cp "$file" ".file_backup/$file" || true
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
- name: Sync upstream changes
|
||||
id: sync
|
||||
@@ -32,23 +75,58 @@ jobs:
|
||||
upstream_sync_repo: lobehub/lobe-chat
|
||||
upstream_sync_branch: main
|
||||
target_sync_branch: main
|
||||
target_repo_token: ${{ secrets.GITHUB_TOKEN }} # automatically generated, no need to set
|
||||
target_repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
test_mode: false
|
||||
|
||||
- name: Restore ignored files
|
||||
if: steps.sync.outputs.has_new_commits == 'true'
|
||||
run: |
|
||||
# Process each pattern (comma-separated)
|
||||
for pattern in $(echo $IGNORE_FILES | tr ',' ' '); do
|
||||
# Find all matching files in the backup
|
||||
for file in $(find .file_backup -type f -path ".file_backup/$pattern" 2>/dev/null || find .file_backup -type f -name "$(basename "$pattern")" 2>/dev/null); do
|
||||
# Get the original file path
|
||||
original_file="${file#.file_backup/}"
|
||||
if [ -f "$file" ]; then
|
||||
echo "Restoring $original_file"
|
||||
# Ensure directory exists
|
||||
mkdir -p "$(dirname "$original_file")"
|
||||
cp "$file" "$original_file"
|
||||
git add "$original_file"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# Commit the restored files if there are changes
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
git config --local user.email "action@github.com"
|
||||
git config --local user.name "GitHub Action"
|
||||
git commit -m "Restore ignored files after sync: ${{ steps.extract_ignore.outputs.ignore_files }}"
|
||||
git push
|
||||
fi
|
||||
|
||||
- name: Sync check
|
||||
if: failure()
|
||||
uses: actions-cool/issues-helper@v3
|
||||
with:
|
||||
actions: 'create-issue'
|
||||
title: '🚨 同步失败 | Sync Fail'
|
||||
labels: '🚨 Sync Fail'
|
||||
title: '🚨 Sync Failed'
|
||||
labels: '🚨 Sync Failed'
|
||||
body: |
|
||||
Due to a change in the workflow file of the [LobeChat][lobechat] upstream repository, GitHub has automatically suspended the scheduled automatic update. You need to manually sync your fork. Please refer to the detailed [Tutorial][tutorial-en-US] for instructions.
|
||||
Due to a change in the workflow file of the upstream repository, GitHub has automatically suspended the scheduled automatic update. You need to manually sync your fork.
|
||||
|
||||
由于 [LobeChat][lobechat] 上游仓库的 workflow 文件变更,导致 GitHub 自动暂停了本次自动更新,你需要手动 Sync Fork 一次,请查看 [详细教程][tutorial-zh-CN]
|
||||
To manually sync your fork:
|
||||
1. Go to your fork's GitHub page
|
||||
2. Click on "Sync fork" button
|
||||
3. Click on "Update branch"
|
||||
|
||||

|
||||
If you encounter any issues, please contact the repository maintainers.
|
||||
|
||||
[lobechat]: https://github.com/lobehub/lobe-chat
|
||||
[tutorial-zh-CN]: https://github.com/lobehub/lobe-chat/wiki/Upstream-Sync.zh-CN
|
||||
[tutorial-en-US]: https://github.com/lobehub/lobe-chat/wiki/Upstream-Sync
|
||||
- name: Trigger Release Workflow
|
||||
if: success() && steps.sync.outputs.has_new_commits == 'true'
|
||||
run: |
|
||||
curl -X POST \
|
||||
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
https://api.github.com/repos/${{ github.repository }}/actions/workflows/release.yml/dispatches \
|
||||
-d '{"ref":"main"}'
|
||||
|
||||
@@ -41,6 +41,9 @@ test-output
|
||||
# husky
|
||||
.husky/prepare-commit-msg
|
||||
|
||||
# misc
|
||||
# add other ignore file below
|
||||
|
||||
# local env files
|
||||
.env*.local
|
||||
|
||||
@@ -68,10 +71,3 @@ public/swe-worker*
|
||||
vertex-ai-key.json
|
||||
.pnpm-store
|
||||
./packages/lobe-ui
|
||||
|
||||
|
||||
# local use ai coding files
|
||||
docs/.prd
|
||||
.claude
|
||||
.mcp.json
|
||||
CLAUDE.md
|
||||
@@ -1,2 +1,5 @@
|
||||
npm run type-check
|
||||
npx --no-install lint-staged
|
||||
# .husky/post-merge
|
||||
#!/bin/sh
|
||||
. "$(dirname "$0")/_/husky.sh"
|
||||
|
||||
npm run type-check || echo "Type check failed, please fix issues!"
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
lockfile=false
|
||||
resolution-mode=highest
|
||||
|
||||
ignore-workspace-root-check=true
|
||||
enable-pre-post-scripts=true
|
||||
|
||||
public-hoist-pattern[]=*@umijs/lint*
|
||||
public-hoist-pattern[]=*changelog*
|
||||
public-hoist-pattern[]=*commitlint*
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
const config = require('@lobehub/lint').semanticRelease;
|
||||
|
||||
// Remove NPM publishing by excluding "@semantic-release/npm" plugin
|
||||
// Keep or add other plugins like GitHub Releases
|
||||
config.plugins = config.plugins.filter((plugin) => plugin !== '@semantic-release/npm');
|
||||
|
||||
// Add GitHub only if required
|
||||
config.plugins.push([
|
||||
'@semantic-release/exec',
|
||||
{
|
||||
@@ -7,4 +12,32 @@ config.plugins.push([
|
||||
},
|
||||
]);
|
||||
|
||||
// Override GitHub repository URL without modifying package.json
|
||||
// Make sure @semantic-release/github is present in the plugins
|
||||
if (!config.plugins.some(plugin => Array.isArray(plugin) ? plugin[0] === '@semantic-release/github' : plugin === '@semantic-release/github')) {
|
||||
config.plugins.push([
|
||||
'@semantic-release/github',
|
||||
{
|
||||
repositoryUrl: 'https://github.com/jaworldwideorg/OneJA-Bot.git'
|
||||
}
|
||||
]);
|
||||
} else {
|
||||
// Find and update the existing GitHub plugin configuration
|
||||
config.plugins = config.plugins.map(plugin => {
|
||||
if (Array.isArray(plugin) && plugin[0] === '@semantic-release/github') {
|
||||
return [
|
||||
'@semantic-release/github',
|
||||
{
|
||||
...(plugin[1] || {}),
|
||||
repositoryUrl: 'https://github.com/jaworldwideorg/OneJA-Bot.git'
|
||||
}
|
||||
];
|
||||
}
|
||||
return plugin;
|
||||
});
|
||||
}
|
||||
|
||||
// Set repository URL in global config
|
||||
config.repositoryUrl = 'https://github.com/jaworldwideorg/OneJA-Bot.git';
|
||||
|
||||
module.exports = config;
|
||||
|
||||
@@ -53,8 +53,6 @@ ENV NEXT_PUBLIC_SENTRY_DSN="${NEXT_PUBLIC_SENTRY_DSN}" \
|
||||
SENTRY_ORG="" \
|
||||
SENTRY_PROJECT=""
|
||||
|
||||
ENV APP_URL="http://app.com"
|
||||
|
||||
# Posthog
|
||||
ENV NEXT_PUBLIC_ANALYTICS_POSTHOG="${NEXT_PUBLIC_ANALYTICS_POSTHOG}" \
|
||||
NEXT_PUBLIC_POSTHOG_HOST="${NEXT_PUBLIC_POSTHOG_HOST}" \
|
||||
|
||||
@@ -55,8 +55,6 @@ ENV NEXT_PUBLIC_SENTRY_DSN="${NEXT_PUBLIC_SENTRY_DSN}" \
|
||||
SENTRY_ORG="" \
|
||||
SENTRY_PROJECT=""
|
||||
|
||||
ENV APP_URL="http://app.com"
|
||||
|
||||
# Posthog
|
||||
ENV NEXT_PUBLIC_ANALYTICS_POSTHOG="${NEXT_PUBLIC_ANALYTICS_POSTHOG}" \
|
||||
NEXT_PUBLIC_POSTHOG_HOST="${NEXT_PUBLIC_POSTHOG_HOST}" \
|
||||
|
||||
@@ -52,26 +52,22 @@ One-click **FREE** deployment of your private OpenAI ChatGPT/Claude/Gemini/Groq/
|
||||
|
||||
- [👋🏻 Getting Started & Join Our Community](#-getting-started--join-our-community)
|
||||
- [✨ Features](#-features)
|
||||
- [✨ MCP Plugin One-Click Installation](#-mcp-plugin-one-click-installation)
|
||||
- [🏪 MCP Marketplace](#-mcp-marketplace)
|
||||
- [🖥️ Desktop App](#️-desktop-app)
|
||||
- [🌐 Smart Internet Search](#-smart-internet-search)
|
||||
- [Chain of Thought](#chain-of-thought)
|
||||
- [Branching Conversations](#branching-conversations)
|
||||
- [Artifacts Support](#artifacts-support)
|
||||
- [File Upload /Knowledge Base](#file-upload-knowledge-base)
|
||||
- [Multi-Model Service Provider Support](#multi-model-service-provider-support)
|
||||
- [Local Large Language Model (LLM) Support](#local-large-language-model-llm-support)
|
||||
- [Model Visual Recognition](#model-visual-recognition)
|
||||
- [TTS & STT Voice Conversation](#tts--stt-voice-conversation)
|
||||
- [Text to Image Generation](#text-to-image-generation)
|
||||
- [Plugin System (Function Calling)](#plugin-system-function-calling)
|
||||
- [Agent Market (GPTs)](#agent-market-gpts)
|
||||
- [Support Local / Remote Database](#support-local--remote-database)
|
||||
- [Support Multi-User Management](#support-multi-user-management)
|
||||
- [Progressive Web App (PWA)](#progressive-web-app-pwa)
|
||||
- [Mobile Device Adaptation](#mobile-device-adaptation)
|
||||
- [Custom Themes](#custom-themes)
|
||||
- [`1` Chain of Thought](#1-chain-of-thought)
|
||||
- [`2` Branching Conversations](#2-branching-conversations)
|
||||
- [`3` Artifacts Support](#3-artifacts-support)
|
||||
- [`4` File Upload /Knowledge Base](#4-file-upload-knowledge-base)
|
||||
- [`5` Multi-Model Service Provider Support](#5-multi-model-service-provider-support)
|
||||
- [`6` Local Large Language Model (LLM) Support](#6-local-large-language-model-llm-support)
|
||||
- [`7` Model Visual Recognition](#7-model-visual-recognition)
|
||||
- [`8` TTS & STT Voice Conversation](#8-tts--stt-voice-conversation)
|
||||
- [`9` Text to Image Generation](#9-text-to-image-generation)
|
||||
- [`10` Plugin System (Function Calling)](#10-plugin-system-function-calling)
|
||||
- [`11` Agent Market (GPTs)](#11-agent-market-gpts)
|
||||
- [`12` Support Local / Remote Database](#12-support-local--remote-database)
|
||||
- [`13` Support Multi-User Management](#13-support-multi-user-management)
|
||||
- [`14` Progressive Web App (PWA)](#14-progressive-web-app-pwa)
|
||||
- [`15` Mobile Device Adaptation](#15-mobile-device-adaptation)
|
||||
- [`16` Custom Themes](#16-custom-themes)
|
||||
- [`*` What's more](#-whats-more)
|
||||
- [⚡️ Performance](#️-performance)
|
||||
- [🛳 Self Hosting](#-self-hosting)
|
||||
@@ -118,59 +114,9 @@ Whether for users or professional developers, LobeHub will be your AI Agent play
|
||||
|
||||
## ✨ Features
|
||||
|
||||
Transform your AI experience with LobeChat's powerful features designed for seamless connectivity, enhanced productivity, and unlimited creativity.
|
||||
|
||||
![][image-feat-mcp]
|
||||
|
||||
### ✨ MCP Plugin One-Click Installation
|
||||
|
||||
**Seamlessly Connect Your AI to the World**
|
||||
|
||||
Unlock the full potential of your AI by enabling smooth, secure, and dynamic interactions with external tools, data sources, and services. LobeChat's MCP (Model Context Protocol) plugin system breaks down the barriers between your AI and the digital ecosystem, allowing for unprecedented connectivity and functionality.
|
||||
|
||||
Transform your conversations into powerful workflows by connecting to databases, APIs, file systems, and more. Experience the freedom of AI that truly understands and interacts with your world.
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
![][image-feat-mcp-market]
|
||||
|
||||
### 🏪 MCP Marketplace
|
||||
|
||||
**Discover, Connect, Extend**
|
||||
|
||||
Browse a growing library of MCP plugins to expand your AI's capabilities and streamline your workflows effortlessly. Visit [lobehub.com/mcp](https://lobehub.com/mcp) to explore the MCP Marketplace, which offers a curated collection of integrations that enhance your AI's ability to work with various tools and services.
|
||||
|
||||
From productivity tools to development environments, discover new ways to extend your AI's reach and effectiveness. Connect with the community and find the perfect plugins for your specific needs.
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
![][image-feat-desktop]
|
||||
|
||||
### 🖥️ Desktop App
|
||||
|
||||
**Peak Performance, Zero Distractions**
|
||||
|
||||
Get the full LobeChat experience without browser limitations—lightweight, focused, and always ready to go. Our desktop application provides a dedicated environment for your AI interactions, ensuring optimal performance and minimal distractions.
|
||||
|
||||
Experience faster response times, better resource management, and a more stable connection to your AI assistant. The desktop app is designed for users who demand the best performance from their AI tools.
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
![][image-feat-web-search]
|
||||
|
||||
### 🌐 Smart Internet Search
|
||||
|
||||
**Online Knowledge On Demand**
|
||||
|
||||
With real-time internet access, your AI keeps up with the world—news, data, trends, and more. Stay informed and get the most current information available, enabling your AI to provide accurate and up-to-date responses.
|
||||
|
||||
Access live information, verify facts, and explore current events without leaving your conversation. Your AI becomes a gateway to the world's knowledge, always current and comprehensive.
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
[![][image-feat-cot]][docs-feat-cot]
|
||||
|
||||
### [Chain of Thought][docs-feat-cot]
|
||||
### `1` [Chain of Thought][docs-feat-cot]
|
||||
|
||||
Experience AI reasoning like never before. Watch as complex problems unfold step by step through our innovative Chain of Thought (CoT) visualization. This breakthrough feature provides unprecedented transparency into AI's decision-making process, allowing you to observe how conclusions are reached in real-time.
|
||||
|
||||
@@ -180,7 +126,7 @@ By breaking down complex reasoning into clear, logical steps, you can better und
|
||||
|
||||
[![][image-feat-branch]][docs-feat-branch]
|
||||
|
||||
### [Branching Conversations][docs-feat-branch]
|
||||
### `2` [Branching Conversations][docs-feat-branch]
|
||||
|
||||
Introducing a more natural and flexible way to chat with AI. With Branch Conversations, your discussions can flow in multiple directions, just like human conversations do. Create new conversation branches from any message, giving you the freedom to explore different paths while preserving the original context.
|
||||
|
||||
@@ -195,7 +141,7 @@ This groundbreaking feature transforms linear conversations into dynamic, tree-l
|
||||
|
||||
[![][image-feat-artifacts]][docs-feat-artifacts]
|
||||
|
||||
### [Artifacts Support][docs-feat-artifacts]
|
||||
### `3` [Artifacts Support][docs-feat-artifacts]
|
||||
|
||||
Experience the power of Claude Artifacts, now integrated into LobeChat. This revolutionary feature expands the boundaries of AI-human interaction, enabling real-time creation and visualization of diverse content formats.
|
||||
|
||||
@@ -209,7 +155,7 @@ Create and visualize with unprecedented flexibility:
|
||||
|
||||
[![][image-feat-knowledgebase]][docs-feat-knowledgebase]
|
||||
|
||||
### [File Upload /Knowledge Base][docs-feat-knowledgebase]
|
||||
### `4` [File Upload /Knowledge Base][docs-feat-knowledgebase]
|
||||
|
||||
LobeChat supports file upload and knowledge base functionality. You can upload various types of files including documents, images, audio, and video, as well as create knowledge bases, making it convenient for users to manage and search for files. Additionally, you can utilize files and knowledge base features during conversations, enabling a richer dialogue experience.
|
||||
|
||||
@@ -227,7 +173,7 @@ LobeChat supports file upload and knowledge base functionality. You can upload v
|
||||
|
||||
[![][image-feat-privoder]][docs-feat-provider]
|
||||
|
||||
### [Multi-Model Service Provider Support][docs-feat-provider]
|
||||
### `5` [Multi-Model Service Provider Support][docs-feat-provider]
|
||||
|
||||
In the continuous development of LobeChat, we deeply understand the importance of diversity in model service providers for meeting the needs of the community when providing AI conversation services. Therefore, we have expanded our support to multiple model service providers, rather than being limited to a single one, in order to offer users a more diverse and rich selection of conversations.
|
||||
|
||||
@@ -237,7 +183,7 @@ In this way, LobeChat can more flexibly adapt to the needs of different users, w
|
||||
|
||||
We have implemented support for the following model service providers:
|
||||
|
||||
<!-- PROVIDER LIST -->
|
||||
<!-- PROVIDER LIST -->
|
||||
|
||||
- **[OpenAI](https://lobechat.com/discover/provider/openai)**: OpenAI is a global leader in artificial intelligence research, with models like the GPT series pushing the frontiers of natural language processing. OpenAI is committed to transforming multiple industries through innovative and efficient AI solutions. Their products demonstrate significant performance and cost-effectiveness, widely used in research, business, and innovative applications.
|
||||
- **[Ollama](https://lobechat.com/discover/provider/ollama)**: Ollama provides models that cover a wide range of fields, including code generation, mathematical operations, multilingual processing, and conversational interaction, catering to diverse enterprise-level and localized deployment needs.
|
||||
@@ -278,7 +224,7 @@ We have implemented support for the following model service providers:
|
||||
- **[InternLM](https://lobechat.com/discover/provider/internlm)**: An open-source organization dedicated to the research and development of large model toolchains. It provides an efficient and user-friendly open-source platform for all AI developers, making cutting-edge large models and algorithm technologies easily accessible.
|
||||
- **[Higress](https://lobechat.com/discover/provider/higress)**: Higress is a cloud-native API gateway that was developed internally at Alibaba to address the issues of Tengine reload affecting long-lived connections and the insufficient load balancing capabilities for gRPC/Dubbo.
|
||||
- **[Gitee AI](https://lobechat.com/discover/provider/giteeai)**: Gitee AI's Serverless API provides AI developers with an out of the box large model inference API service.
|
||||
- **[Taichu](https://lobechat.com/discover/provider/taichu)**: The Institute of Automation, Chinese Academy of Sciences, and Wuhan Artificial Intelligence Research Institute have launched a new generation of multimodal large models, supporting comprehensive question-answering tasks such as multi-turn Q\&A, text creation, image generation, 3D understanding, and signal analysis, with stronger cognitive, understanding, and creative abilities, providing a new interactive experience.
|
||||
- **[Taichu](https://lobechat.com/discover/provider/taichu)**: The Institute of Automation, Chinese Academy of Sciences, and Wuhan Artificial Intelligence Research Institute have launched a new generation of multimodal large models, supporting comprehensive question-answering tasks such as multi-turn Q&A, text creation, image generation, 3D understanding, and signal analysis, with stronger cognitive, understanding, and creative abilities, providing a new interactive experience.
|
||||
- **[360 AI](https://lobechat.com/discover/provider/ai360)**: 360 AI is an AI model and service platform launched by 360 Company, offering various advanced natural language processing models, including 360GPT2 Pro, 360GPT Pro, 360GPT Turbo, and 360GPT Turbo Responsibility 8K. These models combine large-scale parameters and multimodal capabilities, widely applied in text generation, semantic understanding, dialogue systems, and code generation. With flexible pricing strategies, 360 AI meets diverse user needs, supports developer integration, and promotes the innovation and development of intelligent applications.
|
||||
- **[Search1API](https://lobechat.com/discover/provider/search1api)**: Search1API provides access to the DeepSeek series of models that can connect to the internet as needed, including standard and fast versions, supporting a variety of model sizes.
|
||||
- **[InfiniAI](https://lobechat.com/discover/provider/infiniai)**: Provides high-performance, easy-to-use, and secure large model services for application developers, covering the entire process from large model development to service deployment.
|
||||
@@ -300,7 +246,7 @@ At the same time, we are also planning to support more model service providers.
|
||||
|
||||
[![][image-feat-local]][docs-feat-local]
|
||||
|
||||
### [Local Large Language Model (LLM) Support][docs-feat-local]
|
||||
### `6` [Local Large Language Model (LLM) Support][docs-feat-local]
|
||||
|
||||
To meet the specific needs of users, LobeChat also supports the use of local models based on [Ollama](https://ollama.ai), allowing users to flexibly use their own or third-party models.
|
||||
|
||||
@@ -316,7 +262,7 @@ To meet the specific needs of users, LobeChat also supports the use of local mod
|
||||
|
||||
[![][image-feat-vision]][docs-feat-vision]
|
||||
|
||||
### [Model Visual Recognition][docs-feat-vision]
|
||||
### `7` [Model Visual Recognition][docs-feat-vision]
|
||||
|
||||
LobeChat now supports OpenAI's latest [`gpt-4-vision`](https://platform.openai.com/docs/guides/vision) model with visual recognition capabilities,
|
||||
a multimodal intelligence that can perceive visuals. Users can easily upload or drag and drop images into the dialogue box,
|
||||
@@ -334,7 +280,7 @@ Whether it's sharing images in daily use or interpreting images within specific
|
||||
|
||||
[![][image-feat-tts]][docs-feat-tts]
|
||||
|
||||
### [TTS & STT Voice Conversation][docs-feat-tts]
|
||||
### `8` [TTS & STT Voice Conversation][docs-feat-tts]
|
||||
|
||||
LobeChat supports Text-to-Speech (TTS) and Speech-to-Text (STT) technologies, enabling our application to convert text messages into clear voice outputs,
|
||||
allowing users to interact with our conversational agent as if they were talking to a real person. Users can choose from a variety of voices to pair with the agent.
|
||||
@@ -351,7 +297,7 @@ Users can choose the voice that suits their personal preferences or specific sce
|
||||
|
||||
[![][image-feat-t2i]][docs-feat-t2i]
|
||||
|
||||
### [Text to Image Generation][docs-feat-t2i]
|
||||
### `9` [Text to Image Generation][docs-feat-t2i]
|
||||
|
||||
With support for the latest text-to-image generation technology, LobeChat now allows users to invoke image creation tools directly within conversations with the agent. By leveraging the capabilities of AI tools such as [`DALL-E 3`](https://openai.com/dall-e-3), [`MidJourney`](https://www.midjourney.com/), and [`Pollinations`](https://pollinations.ai/), the agents are now equipped to transform your ideas into images.
|
||||
|
||||
@@ -365,7 +311,7 @@ This enables a more private and immersive creative process, allowing for the sea
|
||||
|
||||
[![][image-feat-plugin]][docs-feat-plugin]
|
||||
|
||||
### [Plugin System (Function Calling)][docs-feat-plugin]
|
||||
### `10` [Plugin System (Function Calling)][docs-feat-plugin]
|
||||
|
||||
The plugin ecosystem of LobeChat is an important extension of its core functionality, greatly enhancing the practicality and flexibility of the LobeChat assistant.
|
||||
|
||||
@@ -379,16 +325,16 @@ In addition, these plugins are not limited to news aggregation, but can also ext
|
||||
>
|
||||
> Learn more about [📘 Plugin Usage][docs-usage-plugin] by checking it out.
|
||||
|
||||
<!-- PLUGIN LIST -->
|
||||
<!-- PLUGIN LIST -->
|
||||
|
||||
| Recent Submits | Description |
|
||||
| ---------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
||||
| [PortfolioMeta](https://lobechat.com/discover/plugin/StockData)<br/><sup>By **portfoliometa** on **2025-07-21**</sup> | Analyze stocks and get comprehensive real-time investment data and analytics.<br/>`stock` |
|
||||
| [Speak](https://lobechat.com/discover/plugin/speak)<br/><sup>By **speak** on **2025-07-18**</sup> | Learn how to say anything in another language with Speak, your AI-powered language tutor.<br/>`education` `language` |
|
||||
| [PortfolioMeta](https://lobechat.com/discover/plugin/StockData)<br/><sup>By **portfoliometa** on **2025-05-27**</sup> | Analyze stocks and get comprehensive real-time investment data and analytics.<br/>`stock` |
|
||||
| [Web](https://lobechat.com/discover/plugin/web)<br/><sup>By **Proghit** on **2025-01-24**</sup> | Smart web search that reads and analyzes pages to deliver comprehensive answers from Google results.<br/>`web` `search` |
|
||||
| [Bing_websearch](https://lobechat.com/discover/plugin/Bingsearch-identifier)<br/><sup>By **FineHow** on **2024-12-22**</sup> | Search for information from the internet base BingApi<br/>`bingsearch` |
|
||||
| [Google CSE](https://lobechat.com/discover/plugin/google-cse)<br/><sup>By **vsnthdev** on **2024-12-02**</sup> | Searches Google through their official CSE API.<br/>`web` `search` |
|
||||
|
||||
> 📊 Total plugins: [<kbd>**43**</kbd>](https://lobechat.com/discover/plugins)
|
||||
> 📊 Total plugins: [<kbd>**42**</kbd>](https://lobechat.com/discover/plugins)
|
||||
|
||||
<!-- PLUGIN LIST -->
|
||||
|
||||
@@ -400,7 +346,7 @@ In addition, these plugins are not limited to news aggregation, but can also ext
|
||||
|
||||
[![][image-feat-agent]][docs-feat-agent]
|
||||
|
||||
### [Agent Market (GPTs)][docs-feat-agent]
|
||||
### `11` [Agent Market (GPTs)][docs-feat-agent]
|
||||
|
||||
In LobeChat Agent Marketplace, creators can discover a vibrant and innovative community that brings together a multitude of well-designed agents,
|
||||
which not only play an important role in work scenarios but also offer great convenience in learning processes.
|
||||
@@ -418,7 +364,7 @@ Our marketplace is not just a showcase platform but also a collaborative space.
|
||||
> We welcome all users to join this growing ecosystem and participate in the iteration and optimization of agents.
|
||||
> Together, we can create more interesting, practical, and innovative agents, further enriching the diversity and practicality of the agent offerings.
|
||||
|
||||
<!-- AGENT LIST -->
|
||||
<!-- AGENT LIST -->
|
||||
|
||||
| Recent Submits | Description |
|
||||
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
@@ -439,7 +385,7 @@ Our marketplace is not just a showcase platform but also a collaborative space.
|
||||
|
||||
[![][image-feat-database]][docs-feat-database]
|
||||
|
||||
### [Support Local / Remote Database][docs-feat-database]
|
||||
### `12` [Support Local / Remote Database][docs-feat-database]
|
||||
|
||||
LobeChat supports the use of both server-side and local databases. Depending on your needs, you can choose the appropriate deployment solution:
|
||||
|
||||
@@ -456,7 +402,7 @@ Regardless of which database you choose, LobeChat can provide you with an excell
|
||||
|
||||
[![][image-feat-auth]][docs-feat-auth]
|
||||
|
||||
### [Support Multi-User Management][docs-feat-auth]
|
||||
### `13` [Support Multi-User Management][docs-feat-auth]
|
||||
|
||||
LobeChat supports multi-user management and provides two main user authentication and management solutions to meet different needs:
|
||||
|
||||
@@ -474,7 +420,7 @@ Regardless of which user management solution you choose, LobeChat can provide yo
|
||||
|
||||
[![][image-feat-pwa]][docs-feat-pwa]
|
||||
|
||||
### [Progressive Web App (PWA)][docs-feat-pwa]
|
||||
### `14` [Progressive Web App (PWA)][docs-feat-pwa]
|
||||
|
||||
We deeply understand the importance of providing a seamless experience for users in today's multi-device environment.
|
||||
Therefore, we have adopted Progressive Web Application ([PWA](https://support.google.com/chrome/answer/9658361)) technology,
|
||||
@@ -501,7 +447,7 @@ providing smooth animations, responsive layouts, and adapting to different devic
|
||||
|
||||
[![][image-feat-mobile]][docs-feat-mobile]
|
||||
|
||||
### [Mobile Device Adaptation][docs-feat-mobile]
|
||||
### `15` [Mobile Device Adaptation][docs-feat-mobile]
|
||||
|
||||
We have carried out a series of optimization designs for mobile devices to enhance the user's mobile experience. Currently, we are iterating on the mobile user experience to achieve smoother and more intuitive interactions. If you have any suggestions or ideas, we welcome you to provide feedback through GitHub Issues or Pull Requests.
|
||||
|
||||
@@ -513,7 +459,7 @@ We have carried out a series of optimization designs for mobile devices to enhan
|
||||
|
||||
[![][image-feat-theme]][docs-feat-theme]
|
||||
|
||||
### [Custom Themes][docs-feat-theme]
|
||||
### `16` [Custom Themes][docs-feat-theme]
|
||||
|
||||
As a design-engineering-oriented application, LobeChat places great emphasis on users' personalized experiences,
|
||||
hence introducing flexible and diverse theme modes, including a light mode for daytime and a dark mode for nighttime.
|
||||
@@ -912,11 +858,8 @@ This project is [Apache 2.0](./LICENSE) licensed.
|
||||
[image-feat-branch]: https://github.com/user-attachments/assets/92f72082-02bd-4835-9c54-b089aad7fd41
|
||||
[image-feat-cot]: https://github.com/user-attachments/assets/f74f1139-d115-4e9c-8c43-040a53797a5e
|
||||
[image-feat-database]: https://github.com/user-attachments/assets/f1697c8b-d1fb-4dac-ba05-153c6295d91d
|
||||
[image-feat-desktop]: https://github.com/user-attachments/assets/a7bac8d3-ea96-4000-bb39-fadc9b610f96
|
||||
[image-feat-knowledgebase]: https://github.com/user-attachments/assets/7da7a3b2-92fd-4630-9f4e-8560c74955ae
|
||||
[image-feat-local]: https://github.com/user-attachments/assets/1239da50-d832-4632-a7ef-bd754c0f3850
|
||||
[image-feat-mcp]: https://github.com/user-attachments/assets/1be85d36-3975-4413-931f-27e05e440995
|
||||
[image-feat-mcp-market]: https://github.com/user-attachments/assets/bb114f9f-24c5-4000-a984-c10d187da5a0
|
||||
[image-feat-mobile]: https://github.com/user-attachments/assets/32cf43c4-96bd-4a4c-bfb6-59acde6fe380
|
||||
[image-feat-plugin]: https://github.com/user-attachments/assets/66a891ac-01b6-4e3f-b978-2eb07b489b1b
|
||||
[image-feat-privoder]: https://github.com/user-attachments/assets/e553e407-42de-4919-977d-7dbfcf44a821
|
||||
@@ -925,7 +868,6 @@ This project is [Apache 2.0](./LICENSE) licensed.
|
||||
[image-feat-theme]: https://github.com/user-attachments/assets/b47c39f1-806f-492b-8fcb-b0fa973937c1
|
||||
[image-feat-tts]: https://github.com/user-attachments/assets/50189597-2cc3-4002-b4c8-756a52ad5c0a
|
||||
[image-feat-vision]: https://github.com/user-attachments/assets/18574a1f-46c2-4cbc-af2c-35a86e128a07
|
||||
[image-feat-web-search]: https://github.com/user-attachments/assets/cfdc48ac-b5f8-4a00-acee-db8f2eba09ad
|
||||
[image-overview]: https://github.com/user-attachments/assets/dbfaa84a-2c82-4dd9-815c-5be616f264a4
|
||||
[image-star]: https://github.com/user-attachments/assets/c3b482e7-cef5-4e94-bef9-226900ecfaab
|
||||
[issues-link]: https://img.shields.io/github/issues/lobehub/lobe-chat.svg?style=flat
|
||||
|
||||
@@ -52,26 +52,22 @@
|
||||
|
||||
- [👋🏻 开始使用 & 交流](#-开始使用--交流)
|
||||
- [✨ 特性一览](#-特性一览)
|
||||
- [✨ MCP 插件一键安装](#-mcp-插件一键安装)
|
||||
- [🏪 MCP 市场](#-mcp-市场)
|
||||
- [🖥️ 桌面应用](#️-桌面应用)
|
||||
- [🌐 智能联网搜索](#-智能联网搜索)
|
||||
- [思维链 (CoT)](#思维链-cot)
|
||||
- [分支对话](#分支对话)
|
||||
- [支持白板 (Artifacts)](#支持白板-artifacts)
|
||||
- [文件上传 / 知识库](#文件上传--知识库)
|
||||
- [多模型服务商支持](#多模型服务商支持)
|
||||
- [支持本地大语言模型 (LLM)](#支持本地大语言模型-llm)
|
||||
- [模型视觉识别 (Model Visual)](#模型视觉识别-model-visual)
|
||||
- [TTS & STT 语音会话](#tts--stt-语音会话)
|
||||
- [Text to Image 文生图](#text-to-image-文生图)
|
||||
- [插件系统 (Tools Calling)](#插件系统-tools-calling)
|
||||
- [助手市场 (GPTs)](#助手市场-gpts)
|
||||
- [支持本地 / 远程数据库](#支持本地--远程数据库)
|
||||
- [支持多用户管理](#支持多用户管理)
|
||||
- [渐进式 Web 应用 (PWA)](#渐进式-web-应用-pwa)
|
||||
- [移动设备适配](#移动设备适配)
|
||||
- [自定义主题](#自定义主题)
|
||||
- [`1` 思维链 (CoT)](#1-思维链-cot)
|
||||
- [`2` 分支对话](#2-分支对话)
|
||||
- [`3` 支持白板 (Artifacts)](#3-支持白板-artifacts)
|
||||
- [`4` 文件上传 / 知识库](#4-文件上传--知识库)
|
||||
- [`5` 多模型服务商支持](#5-多模型服务商支持)
|
||||
- [`6` 支持本地大语言模型 (LLM)](#6-支持本地大语言模型-llm)
|
||||
- [`7` 模型视觉识别 (Model Visual)](#7-模型视觉识别-model-visual)
|
||||
- [`8` TTS & STT 语音会话](#8-tts--stt-语音会话)
|
||||
- [`9` Text to Image 文生图](#9-text-to-image-文生图)
|
||||
- [`10` 插件系统 (Tools Calling)](#10-插件系统-tools-calling)
|
||||
- [`11` 助手市场 (GPTs)](#11-助手市场-gpts)
|
||||
- [`12` 支持本地 / 远程数据库](#12-支持本地--远程数据库)
|
||||
- [`13` 支持多用户管理](#13-支持多用户管理)
|
||||
- [`14` 渐进式 Web 应用 (PWA)](#14-渐进式-web-应用-pwa)
|
||||
- [`15` 移动设备适配](#15-移动设备适配)
|
||||
- [`16` 自定义主题](#16-自定义主题)
|
||||
- [`*` 更多特性](#-更多特性)
|
||||
- [⚡️ 性能测试](#️-性能测试)
|
||||
- [🛳 开箱即用](#-开箱即用)
|
||||
@@ -118,59 +114,9 @@
|
||||
|
||||
## ✨ 特性一览
|
||||
|
||||
通过 LobeChat 的强大功能,体验为无缝连接、提升效率和无限创意而设计的全新 AI 体验。
|
||||
|
||||
### ✨ MCP 插件一键安装
|
||||
|
||||
[](https://lobehub.com/mcp)
|
||||
|
||||
**无缝连接你的 AI 与世界**
|
||||
|
||||
通过启用与外部工具、数据源和服务的平滑、安全和动态交互,释放你的 AI 的全部潜力。基于 MCP(模型上下文协议)的插件系统打破了 AI 与数字生态系统之间的壁垒,实现了前所未有的连接性和功能性。
|
||||
|
||||
将对话转化为强大的工作流程,连接数据库、API、文件系统等。体验真正理解并与你的世界互动的 AI Agent。
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
### 🏪 MCP 市场
|
||||
|
||||
![][image-feat-mcp-market]
|
||||
|
||||
**发现、连接、扩展**
|
||||
|
||||
浏览不断增长的 MCP 插件库,轻松扩展你的 AI 能力并简化工作流程。访问 [lobehub.com/mcp](https://lobehub.com/mcp) 探索 MCP 市场,提供精选的集成集合,增强你的 AI 与各种工具和服务协作的能力。
|
||||
|
||||
从生产力工具到开发环境,发现扩展 AI 覆盖范围和效率的新方式。与社区连接,找到满足特定需求的完美插件。
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
### 🖥️ 桌面应用
|
||||
|
||||
![][image-feat-desktop]
|
||||
|
||||
**巅峰性能,零干扰**
|
||||
|
||||
获得完整的 LobeChat 体验,摆脱浏览器限制 —— 轻量级、专注且随时就绪。我们的桌面应用程序为你的 AI 交互提供专用环境,确保最佳性能和最小干扰。
|
||||
|
||||
体验更快的响应时间、更好的资源管理和与 AI 助手的更稳定连接。桌面应用专为要求 AI 工具最佳性能的用户设计。
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
### 🌐 智能联网搜索
|
||||
|
||||
![][image-feat-web-search]
|
||||
|
||||
**在线知识,按需获取**
|
||||
|
||||
通过实时联网访问,你的 AI 与世界保持同步 —— 新闻、数据、趋势等。保持信息更新,获取最新可用信息,使你的 AI 能够提供准确和最新的回复。
|
||||
|
||||
访问实时信息,验证事实,探索当前事件,无需离开对话。你的 AI 成为通向世界知识的门户,始终保持最新和全面。
|
||||
|
||||
[![][back-to-top]](#readme-top)
|
||||
|
||||
[![][image-feat-cot]][docs-feat-cot]
|
||||
|
||||
### [思维链 (CoT)][docs-feat-cot]
|
||||
### `1` [思维链 (CoT)][docs-feat-cot]
|
||||
|
||||
体验前所未有的 AI 推理过程。通过创新的思维链(CoT)可视化功能,您可以实时观察复杂问题是如何一步步被解析的。这项突破性的功能为 AI 的决策过程提供了前所未有的透明度,让您能够清晰地了解结论是如何得出的。
|
||||
|
||||
@@ -180,7 +126,7 @@
|
||||
|
||||
[![][image-feat-branch]][docs-feat-branch]
|
||||
|
||||
### [分支对话][docs-feat-branch]
|
||||
### `2` [分支对话][docs-feat-branch]
|
||||
|
||||
为您带来更自然、更灵活的 AI 对话方式。通过分支对话功能,您的讨论可以像人类对话一样自然延伸。在任意消息处创建新的对话分支,让您在保留原有上下文的同时,自由探索不同的对话方向。
|
||||
|
||||
@@ -195,7 +141,7 @@
|
||||
|
||||
[![][image-feat-artifacts]][docs-feat-artifacts]
|
||||
|
||||
### [支持白板 (Artifacts)][docs-feat-artifacts]
|
||||
### `3` [支持白板 (Artifacts)][docs-feat-artifacts]
|
||||
|
||||
体验集成于 LobeChat 的 Claude Artifacts 能力。这项革命性功能突破了 AI 人机交互的边界,让您能够实时创建和可视化各种格式的内容。
|
||||
|
||||
@@ -209,7 +155,7 @@
|
||||
|
||||
[![][image-feat-knowledgebase]][docs-feat-knowledgebase]
|
||||
|
||||
### [文件上传 / 知识库][docs-feat-knowledgebase]
|
||||
### `4` [文件上传 / 知识库][docs-feat-knowledgebase]
|
||||
|
||||
LobeChat 支持文件上传与知识库功能,你可以上传文件、图片、音频、视频等多种类型的文件,以及创建知识库,方便用户管理和查找文件。同时在对话中使用文件和知识库功能,实现更加丰富的对话体验。
|
||||
|
||||
@@ -227,7 +173,7 @@ LobeChat 支持文件上传与知识库功能,你可以上传文件、图片
|
||||
|
||||
[![][image-feat-privoder]][docs-feat-provider]
|
||||
|
||||
### [多模型服务商支持][docs-feat-provider]
|
||||
### `5` [多模型服务商支持][docs-feat-provider]
|
||||
|
||||
在 LobeChat 的不断发展过程中,我们深刻理解到在提供 AI 会话服务时模型服务商的多样性对于满足社区需求的重要性。因此,我们不再局限于单一的模型服务商,而是拓展了对多种模型服务商的支持,以便为用户提供更为丰富和多样化的会话选择。
|
||||
|
||||
@@ -237,18 +183,18 @@ LobeChat 支持文件上传与知识库功能,你可以上传文件、图片
|
||||
|
||||
我们已经实现了对以下模型服务商的支持:
|
||||
|
||||
<!-- PROVIDER LIST -->
|
||||
<!-- PROVIDER LIST -->
|
||||
|
||||
- **[OpenAI](https://lobechat.com/discover/provider/openai)**: OpenAI 是全球领先的人工智能研究机构,其开发的模型如 GPT 系列推动了自然语言处理的前沿。OpenAI 致力于通过创新和高效的 AI 解决方案改变多个行业。他们的产品具有显著的性能和经济性,广泛用于研究、商业和创新应用。
|
||||
- **[OpenAI](https://lobechat.com/discover/provider/openai)**: OpenAI 是全球领先的人工智能研究机构,其开发的模型如GPT系列推动了自然语言处理的前沿。OpenAI 致力于通过创新和高效的AI解决方案改变多个行业。他们的产品具有显著的性能和经济性,广泛用于研究、商业和创新应用。
|
||||
- **[Ollama](https://lobechat.com/discover/provider/ollama)**: Ollama 提供的模型广泛涵盖代码生成、数学运算、多语种处理和对话互动等领域,支持企业级和本地化部署的多样化需求。
|
||||
- **[Anthropic](https://lobechat.com/discover/provider/anthropic)**: Anthropic 是一家专注于人工智能研究和开发的公司,提供了一系列先进的语言模型,如 Claude 3.5 Sonnet、Claude 3 Sonnet、Claude 3 Opus 和 Claude 3 Haiku。这些模型在智能、速度和成本之间取得了理想的平衡,适用于从企业级工作负载到快速响应的各种应用场景。Claude 3.5 Sonnet 作为其最新模型,在多项评估中表现优异,同时保持了较高的性价比。
|
||||
- **[Bedrock](https://lobechat.com/discover/provider/bedrock)**: Bedrock 是亚马逊 AWS 提供的一项服务,专注于为企业提供先进的 AI 语言模型和视觉模型。其模型家族包括 Anthropic 的 Claude 系列、Meta 的 Llama 3.1 系列等,涵盖从轻量级到高性能的多种选择,支持文本生成、对话、图像处理等多种任务,适用于不同规模和需求的企业应用。
|
||||
- **[Google](https://lobechat.com/discover/provider/google)**: Google 的 Gemini 系列是其最先进、通用的 AI 模型,由 Google DeepMind 打造,专为多模态设计,支持文本、代码、图像、音频和视频的无缝理解与处理。适用于从数据中心到移动设备的多种环境,极大提升了 AI 模型的效率与应用广泛性。
|
||||
- **[Google](https://lobechat.com/discover/provider/google)**: Google 的 Gemini 系列是其最先进、通用的 AI模型,由 Google DeepMind 打造,专为多模态设计,支持文本、代码、图像、音频和视频的无缝理解与处理。适用于从数据中心到移动设备的多种环境,极大提升了AI模型的效率与应用广泛性。
|
||||
- **[DeepSeek](https://lobechat.com/discover/provider/deepseek)**: DeepSeek 是一家专注于人工智能技术研究和应用的公司,其最新模型 DeepSeek-V3 多项评测成绩超越 Qwen2.5-72B 和 Llama-3.1-405B 等开源模型,性能对齐领军闭源模型 GPT-4o 与 Claude-3.5-Sonnet。
|
||||
- **[HuggingFace](https://lobechat.com/discover/provider/huggingface)**: HuggingFace Inference API 提供了一种快速且免费的方式,让您可以探索成千上万种模型,适用于各种任务。无论您是在为新应用程序进行原型设计,还是在尝试机器学习的功能,这个 API 都能让您即时访问多个领域的高性能模型。
|
||||
- **[OpenRouter](https://lobechat.com/discover/provider/openrouter)**: OpenRouter 是一个提供多种前沿大模型接口的服务平台,支持 OpenAI、Anthropic、LLaMA 及更多,适合多样化的开发和应用需求。用户可根据自身需求灵活选择最优的模型和价格,助力 AI 体验的提升。
|
||||
- **[OpenRouter](https://lobechat.com/discover/provider/openrouter)**: OpenRouter 是一个提供多种前沿大模型接口的服务平台,支持 OpenAI、Anthropic、LLaMA 及更多,适合多样化的开发和应用需求。用户可根据自身需求灵活选择最优的模型和价格,助力AI体验的提升。
|
||||
- **[Cloudflare Workers AI](https://lobechat.com/discover/provider/cloudflare)**: 在 Cloudflare 的全球网络上运行由无服务器 GPU 驱动的机器学习模型。
|
||||
- **[GitHub](https://lobechat.com/discover/provider/github)**: 通过 GitHub 模型,开发人员可以成为 AI 工程师,并使用行业领先的 AI 模型进行构建。
|
||||
- **[GitHub](https://lobechat.com/discover/provider/github)**: 通过GitHub模型,开发人员可以成为AI工程师,并使用行业领先的AI模型进行构建。
|
||||
|
||||
<details><summary><kbd>See more providers (+31)</kbd></summary>
|
||||
|
||||
@@ -257,18 +203,18 @@ LobeChat 支持文件上传与知识库功能,你可以上传文件、图片
|
||||
- **[Together AI](https://lobechat.com/discover/provider/togetherai)**: Together AI 致力于通过创新的 AI 模型实现领先的性能,提供广泛的自定义能力,包括快速扩展支持和直观的部署流程,满足企业的各种需求。
|
||||
- **[Fireworks AI](https://lobechat.com/discover/provider/fireworksai)**: Fireworks AI 是一家领先的高级语言模型服务商,专注于功能调用和多模态处理。其最新模型 Firefunction V2 基于 Llama-3,优化用于函数调用、对话及指令跟随。视觉语言模型 FireLLaVA-13B 支持图像和文本混合输入。其他 notable 模型包括 Llama 系列和 Mixtral 系列,提供高效的多语言指令跟随与生成支持。
|
||||
- **[Groq](https://lobechat.com/discover/provider/groq)**: Groq 的 LPU 推理引擎在最新的独立大语言模型(LLM)基准测试中表现卓越,以其惊人的速度和效率重新定义了 AI 解决方案的标准。Groq 是一种即时推理速度的代表,在基于云的部署中展现了良好的性能。
|
||||
- **[Perplexity](https://lobechat.com/discover/provider/perplexity)**: Perplexity 是一家领先的对话生成模型提供商,提供多种先进的 Llama 3.1 模型,支持在线和离线应用,特别适用于复杂的自然语言处理任务。
|
||||
- **[Perplexity](https://lobechat.com/discover/provider/perplexity)**: Perplexity 是一家领先的对话生成模型提供商,提供多种先进的Llama 3.1模型,支持在线和离线应用,特别适用于复杂的自然语言处理任务。
|
||||
- **[Mistral](https://lobechat.com/discover/provider/mistral)**: Mistral 提供先进的通用、专业和研究型模型,广泛应用于复杂推理、多语言任务、代码生成等领域,通过功能调用接口,用户可以集成自定义功能,实现特定应用。
|
||||
- **[ModelScope](https://lobechat.com/discover/provider/modelscope)**: ModelScope 是阿里云推出的模型即服务平台,提供丰富的 AI 模型和推理服务。
|
||||
- **[ModelScope](https://lobechat.com/discover/provider/modelscope)**: ModelScope是阿里云推出的模型即服务平台,提供丰富的AI模型和推理服务。
|
||||
- **[Ai21Labs](https://lobechat.com/discover/provider/ai21)**: AI21 Labs 为企业构建基础模型和人工智能系统,加速生成性人工智能在生产中的应用。
|
||||
- **[Upstage](https://lobechat.com/discover/provider/upstage)**: Upstage 专注于为各种商业需求开发 AI 模型,包括 Solar LLM 和文档 AI,旨在实现工作的人造通用智能(AGI)。通过 Chat API 创建简单的对话代理,并支持功能调用、翻译、嵌入以及特定领域应用。
|
||||
- **[Upstage](https://lobechat.com/discover/provider/upstage)**: Upstage 专注于为各种商业需求开发AI模型,包括 Solar LLM 和文档 AI,旨在实现工作的人造通用智能(AGI)。通过 Chat API 创建简单的对话代理,并支持功能调用、翻译、嵌入以及特定领域应用。
|
||||
- **[xAI (Grok)](https://lobechat.com/discover/provider/xai)**: xAI 是一家致力于构建人工智能以加速人类科学发现的公司。我们的使命是推动我们对宇宙的共同理解。
|
||||
- **[Aliyun Bailian](https://lobechat.com/discover/provider/qwen)**: 通义千问是阿里云自主研发的超大规模语言模型,具有强大的自然语言理解和生成能力。它可以回答各种问题、创作文字内容、表达观点看法、撰写代码等,在多个领域发挥作用。
|
||||
- **[Wenxin](https://lobechat.com/discover/provider/wenxin)**: 企业级一站式大模型与 AI 原生应用开发及服务平台,提供最全面易用的生成式人工智能模型开发、应用开发全流程工具链
|
||||
- **[Wenxin](https://lobechat.com/discover/provider/wenxin)**: 企业级一站式大模型与AI原生应用开发及服务平台,提供最全面易用的生成式人工智能模型开发、应用开发全流程工具链
|
||||
- **[Hunyuan](https://lobechat.com/discover/provider/hunyuan)**: 由腾讯研发的大语言模型,具备强大的中文创作能力,复杂语境下的逻辑推理能力,以及可靠的任务执行能力
|
||||
- **[ZhiPu](https://lobechat.com/discover/provider/zhipu)**: 智谱 AI 提供多模态与语言模型的开放平台,支持广泛的 AI 应用场景,包括文本处理、图像理解与编程辅助等。
|
||||
- **[ZhiPu](https://lobechat.com/discover/provider/zhipu)**: 智谱 AI 提供多模态与语言模型的开放平台,支持广泛的AI应用场景,包括文本处理、图像理解与编程辅助等。
|
||||
- **[SiliconCloud](https://lobechat.com/discover/provider/siliconcloud)**: SiliconCloud,基于优秀开源基础模型的高性价比 GenAI 云服务
|
||||
- **[01.AI](https://lobechat.com/discover/provider/zeroone)**: 零一万物致力于推动以人为本的 AI 2.0 技术革命,旨在通过大语言模型创造巨大的经济和社会价值,并开创新的 AI 生态与商业模式。
|
||||
- **[01.AI](https://lobechat.com/discover/provider/zeroone)**: 零一万物致力于推动以人为本的AI 2.0技术革命,旨在通过大语言模型创造巨大的经济和社会价值,并开创新的AI生态与商业模式。
|
||||
- **[Spark](https://lobechat.com/discover/provider/spark)**: 科大讯飞星火大模型提供多领域、多语言的强大 AI 能力,利用先进的自然语言处理技术,构建适用于智能硬件、智慧医疗、智慧金融等多种垂直场景的创新应用。
|
||||
- **[SenseNova](https://lobechat.com/discover/provider/sensenova)**: 商汤日日新,依托商汤大装置的强大的基础支撑,提供高效易用的全栈大模型服务。
|
||||
- **[Stepfun](https://lobechat.com/discover/provider/stepfun)**: 阶级星辰大模型具备行业领先的多模态及复杂推理能力,支持超长文本理解和强大的自主调度搜索引擎功能。
|
||||
@@ -278,7 +224,7 @@ LobeChat 支持文件上传与知识库功能,你可以上传文件、图片
|
||||
- **[InternLM](https://lobechat.com/discover/provider/internlm)**: 致力于大模型研究与开发工具链的开源组织。为所有 AI 开发者提供高效、易用的开源平台,让最前沿的大模型与算法技术触手可及
|
||||
- **[Higress](https://lobechat.com/discover/provider/higress)**: Higress 是一款云原生 API 网关,在阿里内部为解决 Tengine reload 对长连接业务有损,以及 gRPC/Dubbo 负载均衡能力不足而诞生。
|
||||
- **[Gitee AI](https://lobechat.com/discover/provider/giteeai)**: Gitee AI 的 Serverless API 为 AI 开发者提供开箱即用的大模型推理 API 服务。
|
||||
- **[Taichu](https://lobechat.com/discover/provider/taichu)**: 中科院自动化研究所和武汉人工智能研究院推出新一代多模态大模型,支持多轮问答、文本创作、图像生成、3D 理解、信号分析等全面问答任务,拥有更强的认知、理解、创作能力,带来全新互动体验。
|
||||
- **[Taichu](https://lobechat.com/discover/provider/taichu)**: 中科院自动化研究所和武汉人工智能研究院推出新一代多模态大模型,支持多轮问答、文本创作、图像生成、3D理解、信号分析等全面问答任务,拥有更强的认知、理解、创作能力,带来全新互动体验。
|
||||
- **[360 AI](https://lobechat.com/discover/provider/ai360)**: 360 AI 是 360 公司推出的 AI 模型和服务平台,提供多种先进的自然语言处理模型,包括 360GPT2 Pro、360GPT Pro、360GPT Turbo 和 360GPT Turbo Responsibility 8K。这些模型结合了大规模参数和多模态能力,广泛应用于文本生成、语义理解、对话系统与代码生成等领域。通过灵活的定价策略,360 AI 满足多样化用户需求,支持开发者集成,推动智能化应用的革新和发展。
|
||||
- **[Search1API](https://lobechat.com/discover/provider/search1api)**: Search1API 提供可根据需要自行联网的 DeepSeek 系列模型的访问,包括标准版和快速版本,支持多种参数规模的模型选择。
|
||||
- **[InfiniAI](https://lobechat.com/discover/provider/infiniai)**: 为应用开发者提供高性能、易上手、安全可靠的大模型服务,覆盖从大模型开发到大模型服务化部署的全流程。
|
||||
@@ -300,7 +246,7 @@ LobeChat 支持文件上传与知识库功能,你可以上传文件、图片
|
||||
|
||||
[![][image-feat-local]][docs-feat-local]
|
||||
|
||||
### [支持本地大语言模型 (LLM)][docs-feat-local]
|
||||
### `6` [支持本地大语言模型 (LLM)][docs-feat-local]
|
||||
|
||||
为了满足特定用户的需求,LobeChat 还基于 [Ollama](https://ollama.ai) 支持了本地模型的使用,让用户能够更灵活地使用自己的或第三方的模型。
|
||||
|
||||
@@ -316,7 +262,7 @@ LobeChat 支持文件上传与知识库功能,你可以上传文件、图片
|
||||
|
||||
[![][image-feat-vision]][docs-feat-vision]
|
||||
|
||||
### [模型视觉识别 (Model Visual)][docs-feat-vision]
|
||||
### `7` [模型视觉识别 (Model Visual)][docs-feat-vision]
|
||||
|
||||
LobeChat 已经支持 OpenAI 最新的 [`gpt-4-vision`](https://platform.openai.com/docs/guides/vision) 支持视觉识别的模型,这是一个具备视觉识别能力的多模态应用。
|
||||
用户可以轻松上传图片或者拖拽图片到对话框中,助手将能够识别图片内容,并在此基础上进行智能对话,构建更智能、更多元化的聊天场景。
|
||||
@@ -331,7 +277,7 @@ LobeChat 已经支持 OpenAI 最新的 [`gpt-4-vision`](https://platform.openai.
|
||||
|
||||
[![][image-feat-tts]][docs-feat-tts]
|
||||
|
||||
### [TTS & STT 语音会话][docs-feat-tts]
|
||||
### `8` [TTS & STT 语音会话][docs-feat-tts]
|
||||
|
||||
LobeChat 支持文字转语音(Text-to-Speech,TTS)和语音转文字(Speech-to-Text,STT)技术,这使得我们的应用能够将文本信息转化为清晰的语音输出,用户可以像与真人交谈一样与我们的对话助手进行交流。
|
||||
用户可以从多种声音中选择,给助手搭配合适的音源。 同时,对于那些倾向于听觉学习或者想要在忙碌中获取信息的用户来说,TTS 提供了一个极佳的解决方案。
|
||||
@@ -346,7 +292,7 @@ LobeChat 支持文字转语音(Text-to-Speech,TTS)和语音转文字(Spe
|
||||
|
||||
[![][image-feat-t2i]][docs-feat-t2i]
|
||||
|
||||
### [Text to Image 文生图][docs-feat-t2i]
|
||||
### `9` [Text to Image 文生图][docs-feat-t2i]
|
||||
|
||||
支持最新的文本到图片生成技术,LobeChat 现在能够让用户在与助手对话中直接调用文生图工具进行创作。
|
||||
通过利用 [`DALL-E 3`](https://openai.com/dall-e-3)、[`MidJourney`](https://www.midjourney.com/) 和 [`Pollinations`](https://pollinations.ai/) 等 AI 工具的能力, 助手们现在可以将你的想法转化为图像。
|
||||
@@ -360,7 +306,7 @@ LobeChat 支持文字转语音(Text-to-Speech,TTS)和语音转文字(Spe
|
||||
|
||||
[![][image-feat-plugin]][docs-feat-plugin]
|
||||
|
||||
### [插件系统 (Tools Calling)][docs-feat-plugin]
|
||||
### `10` [插件系统 (Tools Calling)][docs-feat-plugin]
|
||||
|
||||
LobeChat 的插件生态系统是其核心功能的重要扩展,它极大地增强了 ChatGPT 的实用性和灵活性。
|
||||
|
||||
@@ -372,16 +318,16 @@ LobeChat 的插件生态系统是其核心功能的重要扩展,它极大地
|
||||
|
||||
> 通过文档了解更多 [📘 插件使用][docs-usage-plugin]
|
||||
|
||||
<!-- PLUGIN LIST -->
|
||||
<!-- PLUGIN LIST -->
|
||||
|
||||
| 最近新增 | 描述 |
|
||||
| -------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
|
||||
| [PortfolioMeta](https://lobechat.com/discover/plugin/StockData)<br/><sup>By **portfoliometa** on **2025-07-21**</sup> | 分析股票并获取全面的实时投资数据和分析。<br/>`股票` |
|
||||
| [Speak](https://lobechat.com/discover/plugin/speak)<br/><sup>By **speak** on **2025-07-18**</sup> | 使用 Speak,您的 AI 语言导师,学习如何用另一种语言说任何事情。<br/>`教育` `语言` |
|
||||
| [网页](https://lobechat.com/discover/plugin/web)<br/><sup>By **Proghit** on **2025-01-24**</sup> | 智能网页搜索,读取和分析页面,以提供来自 Google 结果的全面答案。<br/>`网页` `搜索` |
|
||||
| [必应网页搜索](https://lobechat.com/discover/plugin/Bingsearch-identifier)<br/><sup>By **FineHow** on **2024-12-22**</sup> | 通过 BingApi 搜索互联网上的信息<br/>`bingsearch` |
|
||||
| 最近新增 | 描述 |
|
||||
| --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
|
||||
| [PortfolioMeta](https://lobechat.com/discover/plugin/StockData)<br/><sup>By **portfoliometa** on **2025-05-27**</sup> | 分析股票并获取全面的实时投资数据和分析。<br/>`股票` |
|
||||
| [网页](https://lobechat.com/discover/plugin/web)<br/><sup>By **Proghit** on **2025-01-24**</sup> | 智能网页搜索,读取和分析页面,以提供来自Google结果的全面答案。<br/>`网页` `搜索` |
|
||||
| [必应网页搜索](https://lobechat.com/discover/plugin/Bingsearch-identifier)<br/><sup>By **FineHow** on **2024-12-22**</sup> | 通过BingApi搜索互联网上的信息<br/>`bingsearch` |
|
||||
| [谷歌自定义搜索引擎](https://lobechat.com/discover/plugin/google-cse)<br/><sup>By **vsnthdev** on **2024-12-02**</sup> | 通过他们的官方自定义搜索引擎API搜索谷歌。<br/>`网络` `搜索` |
|
||||
|
||||
> 📊 Total plugins: [<kbd>**43**</kbd>](https://lobechat.com/discover/plugins)
|
||||
> 📊 Total plugins: [<kbd>**42**</kbd>](https://lobechat.com/discover/plugins)
|
||||
|
||||
<!-- PLUGIN LIST -->
|
||||
|
||||
@@ -393,7 +339,7 @@ LobeChat 的插件生态系统是其核心功能的重要扩展,它极大地
|
||||
|
||||
[![][image-feat-agent]][docs-feat-agent]
|
||||
|
||||
### [助手市场 (GPTs)][docs-feat-agent]
|
||||
### `11` [助手市场 (GPTs)][docs-feat-agent]
|
||||
|
||||
在 LobeChat 的助手市场中,创作者们可以发现一个充满活力和创新的社区,它汇聚了众多精心设计的助手,这些助手不仅在工作场景中发挥着重要作用,也在学习过程中提供了极大的便利。
|
||||
我们的市场不仅是一个展示平台,更是一个协作的空间。在这里,每个人都可以贡献自己的智慧,分享个人开发的助手。
|
||||
@@ -407,14 +353,14 @@ LobeChat 的插件生态系统是其核心功能的重要扩展,它极大地
|
||||
>
|
||||
> 我欢迎所有用户加入这个不断成长的生态系统,共同参与到助手的迭代与优化中来。共同创造出更多有趣、实用且具有创新性的助手,进一步丰富助手的多样性和实用性。
|
||||
|
||||
<!-- AGENT LIST -->
|
||||
<!-- AGENT LIST -->
|
||||
|
||||
| 最近新增 | 描述 |
|
||||
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
||||
| 最近新增 | 描述 |
|
||||
| ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
|
||||
| [海龟汤主持人](https://lobechat.com/discover/assistant/lateral-thinking-puzzle)<br/><sup>By **[CSY2022](https://github.com/CSY2022)** on **2025-06-19**</sup> | 一个海龟汤主持人,需要自己提供汤面,汤底与关键点(猜中的判定条件)。<br/>`海龟汤` `推理` `互动` `谜题` `角色扮演` |
|
||||
| [美食评论员🍟](https://lobechat.com/discover/assistant/food-reviewer)<br/><sup>By **[renhai-lab](https://github.com/renhai-lab)** on **2025-06-17**</sup> | 美食评价专家<br/>`美食` `评价` `写作` |
|
||||
| [学术写作助手](https://lobechat.com/discover/assistant/academic-writing-assistant)<br/><sup>By **[swarfte](https://github.com/swarfte)** on **2025-06-17**</sup> | 专业的学术研究论文写作和正式文档编写专家<br/>`学术写作` `研究` `正式风格` |
|
||||
| [Minecraft 资深开发者](https://lobechat.com/discover/assistant/java-development)<br/><sup>By **[iamyuuk](https://github.com/iamyuuk)** on **2025-06-17**</sup> | 擅长高级 Java 开发及 Minecraft 开发<br/>`开发` `编程` `minecraft` `java` |
|
||||
| [美食评论员🍟](https://lobechat.com/discover/assistant/food-reviewer)<br/><sup>By **[renhai-lab](https://github.com/renhai-lab)** on **2025-06-17**</sup> | 美食评价专家<br/>`美食` `评价` `写作` |
|
||||
| [学术写作助手](https://lobechat.com/discover/assistant/academic-writing-assistant)<br/><sup>By **[swarfte](https://github.com/swarfte)** on **2025-06-17**</sup> | 专业的学术研究论文写作和正式文档编写专家<br/>`学术写作` `研究` `正式风格` |
|
||||
| [Minecraft资深开发者](https://lobechat.com/discover/assistant/java-development)<br/><sup>By **[iamyuuk](https://github.com/iamyuuk)** on **2025-06-17**</sup> | 擅长高级 Java 开发及 Minecraft 开发<br/>`开发` `编程` `minecraft` `java` |
|
||||
|
||||
> 📊 Total agents: [<kbd>**505**</kbd> ](https://lobechat.com/discover/assistants)
|
||||
|
||||
@@ -428,7 +374,7 @@ LobeChat 的插件生态系统是其核心功能的重要扩展,它极大地
|
||||
|
||||
[![][image-feat-database]][docs-feat-database]
|
||||
|
||||
### [支持本地 / 远程数据库][docs-feat-database]
|
||||
### `12` [支持本地 / 远程数据库][docs-feat-database]
|
||||
|
||||
LobeChat 支持同时使用服务端数据库和本地数据库。根据您的需求,您可以选择合适的部署方案:
|
||||
|
||||
@@ -445,7 +391,7 @@ LobeChat 支持同时使用服务端数据库和本地数据库。根据您的
|
||||
|
||||
[![][image-feat-auth]][docs-feat-auth]
|
||||
|
||||
### [支持多用户管理][docs-feat-auth]
|
||||
### `13` [支持多用户管理][docs-feat-auth]
|
||||
|
||||
LobeChat 支持多用户管理,提供了两种主要的用户认证和管理方案,以满足不同需求:
|
||||
|
||||
@@ -463,7 +409,7 @@ LobeChat 支持多用户管理,提供了两种主要的用户认证和管理
|
||||
|
||||
[![][image-feat-pwa]][docs-feat-pwa]
|
||||
|
||||
### [渐进式 Web 应用 (PWA)][docs-feat-pwa]
|
||||
### `14` [渐进式 Web 应用 (PWA)][docs-feat-pwa]
|
||||
|
||||
我们深知在当今多设备环境下为用户提供无缝体验的重要性。为此,我们采用了渐进式 Web 应用 [PWA](https://support.google.com/chrome/answer/9658361) 技术,
|
||||
这是一种能够将网页应用提升至接近原生应用体验的现代 Web 技术。通过 PWA,LobeChat 能够在桌面和移动设备上提供高度优化的用户体验,同时保持轻量级和高性能的特点。
|
||||
@@ -476,6 +422,7 @@ LobeChat 支持多用户管理,提供了两种主要的用户认证和管理
|
||||
> - 在电脑上运行 Chrome 或 Edge 浏览器 .
|
||||
> - 访问 LobeChat 网页 .
|
||||
> - 在地址栏的右上角,单击 <kbd>安装</kbd> 图标 .
|
||||
> - 根据屏幕上的指示完成 PWA 的安装 .
|
||||
|
||||
<div align="right">
|
||||
|
||||
@@ -485,7 +432,7 @@ LobeChat 支持多用户管理,提供了两种主要的用户认证和管理
|
||||
|
||||
[![][image-feat-mobile]][docs-feat-mobile]
|
||||
|
||||
### [移动设备适配][docs-feat-mobile]
|
||||
### `15` [移动设备适配][docs-feat-mobile]
|
||||
|
||||
针对移动设备进行了一系列的优化设计,以提升用户的移动体验。目前,我们正在对移动端的用户体验进行版本迭代,以实现更加流畅和直观的交互。如果您有任何建议或想法,我们非常欢迎您通过 GitHub Issues 或者 Pull Requests 提供反馈。
|
||||
|
||||
@@ -497,7 +444,7 @@ LobeChat 支持多用户管理,提供了两种主要的用户认证和管理
|
||||
|
||||
[![][image-feat-theme]][docs-feat-theme]
|
||||
|
||||
### [自定义主题][docs-feat-theme]
|
||||
### `16` [自定义主题][docs-feat-theme]
|
||||
|
||||
作为设计工程师出身,LobeChat 在界面设计上充分考虑用户的个性化体验,因此引入了灵活多变的主题模式,其中包括日间的亮色模式和夜间的深色模式。
|
||||
除了主题模式的切换,还提供了一系列的颜色定制选项,允许用户根据自己的喜好来调整应用的主题色彩。无论是想要沉稳的深蓝,还是希望活泼的桃粉,或者是专业的灰白,用户都能够在 LobeChat 中找到匹配自己风格的颜色选择。
|
||||
@@ -584,7 +531,7 @@ LobeChat 提供了 Vercel 的 自托管版本 和 [Docker 镜像][docker-release
|
||||
|
||||
#### 保持更新
|
||||
|
||||
如果你根据 README 中的一键部署步骤部署了自己的项目,你可能会发现总是被提示 "有可用更新"。这是因为 Vercel 默认为你创建新项目而非 fork 本项目,这将导致无法准确检测更新。
|
||||
如果你根据 README 中的一键部署步骤部署了自己的项目,你可能会发现总是被提示 “有可用更新”。这是因为 Vercel 默认为你创建新项目而非 fork 本项目,这将导致无法准确检测更新。
|
||||
|
||||
> \[!TIP]
|
||||
>
|
||||
@@ -598,9 +545,9 @@ LobeChat 提供了 Vercel 的 自托管版本 和 [Docker 镜像][docker-release
|
||||
[![][docker-size-shield]][docker-size-link]
|
||||
[![][docker-pulls-shield]][docker-pulls-link]
|
||||
|
||||
我们提供了一个用于在您自己的私有设备上部署 LobeChat 服务的 Docker 镜像。请使用以下命令启动 LobeChat 服务:
|
||||
We provide a Docker image for deploying the LobeChat service on your own private device. Use the following command to start the LobeChat service:
|
||||
|
||||
1. 创建一个用于存储文件的文件夹
|
||||
1. create a folder to for storage files
|
||||
|
||||
```fish
|
||||
$ mkdir lobe-chat-db && cd lobe-chat-db
|
||||
@@ -933,10 +880,8 @@ This project is [Apache 2.0](./LICENSE) licensed.
|
||||
[image-feat-branch]: https://github.com/user-attachments/assets/92f72082-02bd-4835-9c54-b089aad7fd41
|
||||
[image-feat-cot]: https://github.com/user-attachments/assets/f74f1139-d115-4e9c-8c43-040a53797a5e
|
||||
[image-feat-database]: https://github.com/user-attachments/assets/f1697c8b-d1fb-4dac-ba05-153c6295d91d
|
||||
[image-feat-desktop]: https://github.com/user-attachments/assets/a7bac8d3-ea96-4000-bb39-fadc9b610f96
|
||||
[image-feat-knowledgebase]: https://github.com/user-attachments/assets/7da7a3b2-92fd-4630-9f4e-8560c74955ae
|
||||
[image-feat-local]: https://github.com/user-attachments/assets/1239da50-d832-4632-a7ef-bd754c0f3850
|
||||
[image-feat-mcp-market]: https://github.com/user-attachments/assets/bb114f9f-24c5-4000-a984-c10d187da5a0
|
||||
[image-feat-mobile]: https://github.com/user-attachments/assets/32cf43c4-96bd-4a4c-bfb6-59acde6fe380
|
||||
[image-feat-plugin]: https://github.com/user-attachments/assets/66a891ac-01b6-4e3f-b978-2eb07b489b1b
|
||||
[image-feat-privoder]: https://github.com/user-attachments/assets/e553e407-42de-4919-977d-7dbfcf44a821
|
||||
@@ -945,7 +890,6 @@ This project is [Apache 2.0](./LICENSE) licensed.
|
||||
[image-feat-theme]: https://github.com/user-attachments/assets/b47c39f1-806f-492b-8fcb-b0fa973937c1
|
||||
[image-feat-tts]: https://github.com/user-attachments/assets/50189597-2cc3-4002-b4c8-756a52ad5c0a
|
||||
[image-feat-vision]: https://github.com/user-attachments/assets/18574a1f-46c2-4cbc-af2c-35a86e128a07
|
||||
[image-feat-web-search]: https://github.com/user-attachments/assets/cfdc48ac-b5f8-4a00-acee-db8f2eba09ad
|
||||
[image-overview]: https://github.com/user-attachments/assets/dbfaa84a-2c82-4dd9-815c-5be616f264a4
|
||||
[image-star]: https://github.com/user-attachments/assets/c3b482e7-cef5-4e94-bef9-226900ecfaab
|
||||
[issues-link]: https://img.shields.io/github/issues/lobehub/lobe-chat.svg?style=flat
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
lockfile=false
|
||||
shamefully-hoist=true
|
||||
ignore-workspace-root-check=true
|
||||
|
||||
electron_mirror=https://npmmirror.com/mirrors/electron/
|
||||
electron_builder_binaries_mirror=https://npmmirror.com/mirrors/electron-builder-binaries/
|
||||
|
||||
@@ -1,353 +1,60 @@
|
||||
# 🤯 LobeHub Desktop Application
|
||||
# LobeHub Desktop Application
|
||||
|
||||
LobeHub Desktop is a cross-platform desktop application for [LobeChat](https://github.com/lobehub/lobe-chat), built with Electron, providing a more native desktop experience and functionality.
|
||||
LobeHub Desktop 是 [LobeChat](https://github.com/lobehub/lobe-chat) 的跨平台桌面应用程序,使用 Electron 构建,提供了更加原生的桌面体验和功能。
|
||||
|
||||
## ✨ Features
|
||||
## 功能特点
|
||||
|
||||
- **🌍 Cross-platform Support**: Supports macOS (Intel/Apple Silicon), Windows, and Linux systems
|
||||
- **🔄 Auto Updates**: Built-in update mechanism ensures you always have the latest version
|
||||
- **🌐 Multi-language Support**: Complete i18n support for 18+ languages with lazy loading
|
||||
- **🎨 Native Integration**: Deep OS integration with native menus, shortcuts, and notifications
|
||||
- **🔒 Secure & Reliable**: macOS notarized, encrypted token storage, secure OAuth flow
|
||||
- **📦 Multiple Release Channels**: Stable, beta, and nightly build versions
|
||||
- **⚡ Advanced Window Management**: Multi-window architecture with theme synchronization
|
||||
- **🔗 Remote Server Sync**: Secure data synchronization with remote LobeChat instances
|
||||
- **🎯 Developer Tools**: Built-in development panel and comprehensive debugging tools
|
||||
- **跨平台支持**:支持 macOS (Intel/Apple Silicon)、Windows 和 Linux 系统
|
||||
- **自动更新**:内置更新机制,确保您始终使用最新版本
|
||||
- **多语言支持**:完整的国际化支持,包括中文、英文等多种语言
|
||||
- **原生集成**:与操作系统深度集成,提供原生菜单、快捷键和通知
|
||||
- **安全可靠**:macOS 版本经过公证,确保安全性
|
||||
- **多渠道发布**:提供稳定版、测试版和每日构建版本
|
||||
|
||||
## 🚀 Development Setup
|
||||
## 开发环境设置
|
||||
|
||||
### Prerequisites
|
||||
### 前提条件
|
||||
|
||||
- **Node.js** 22+
|
||||
- **pnpm** 10+
|
||||
- **Electron** compatible development environment
|
||||
- Node.js 22+
|
||||
- pnpm 10+
|
||||
|
||||
### Quick Start
|
||||
### 安装依赖
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
pnpm install-isolated
|
||||
```
|
||||
|
||||
# Start development server
|
||||
### 开发模式运行
|
||||
|
||||
```bash
|
||||
pnpm electron:dev
|
||||
|
||||
# Type checking
|
||||
pnpm typecheck
|
||||
|
||||
# Run tests
|
||||
pnpm test
|
||||
```
|
||||
|
||||
### Environment Configuration
|
||||
### 构建应用
|
||||
|
||||
Copy `.env.desktop` to `.env` and configure as needed:
|
||||
构建所有平台:
|
||||
|
||||
```bash
|
||||
cp .env.desktop .env
|
||||
pnpm build
|
||||
```
|
||||
|
||||
> \[!WARNING]
|
||||
> Backup your `.env` file before making changes to avoid losing configurations.
|
||||
|
||||
### Build Commands
|
||||
|
||||
| Command | Description |
|
||||
| ------------------ | --------------------------------------- |
|
||||
| `pnpm build` | Build for all platforms |
|
||||
| `pnpm build:mac` | Build for macOS (Intel + Apple Silicon) |
|
||||
| `pnpm build:win` | Build for Windows |
|
||||
| `pnpm build:linux` | Build for Linux |
|
||||
| `pnpm build-local` | Local development build |
|
||||
|
||||
### Development Workflow
|
||||
构建特定平台:
|
||||
|
||||
```bash
|
||||
# 1. Development
|
||||
pnpm electron:dev # Start with hot reload
|
||||
# macOS
|
||||
pnpm build:mac
|
||||
|
||||
# 2. Code Quality
|
||||
pnpm lint # ESLint checking
|
||||
pnpm format # Prettier formatting
|
||||
pnpm typecheck # TypeScript validation
|
||||
# Windows
|
||||
pnpm build:win
|
||||
|
||||
# 3. Testing
|
||||
pnpm test # Run Vitest tests
|
||||
|
||||
# 4. Build & Package
|
||||
pnpm build # Production build
|
||||
pnpm build-local # Local testing build
|
||||
# Linux
|
||||
pnpm build:linux
|
||||
```
|
||||
|
||||
## 🎯 Release Channels
|
||||
## 发布渠道
|
||||
|
||||
| Channel | Description | Stability | Auto-Updates |
|
||||
| ----------- | -------------------------------- | --------- | ------------ |
|
||||
| **Stable** | Thoroughly tested releases | 🟢 High | ✅ Yes |
|
||||
| **Beta** | Pre-release with new features | 🟡 Medium | ✅ Yes |
|
||||
| **Nightly** | Daily builds with latest changes | 🟠 Low | ✅ Yes |
|
||||
应用提供三个发布渠道:
|
||||
|
||||
## 🛠 Technology Stack
|
||||
|
||||
### Core Framework
|
||||
|
||||
- **Electron** `37.1.0` - Cross-platform desktop framework
|
||||
- **Node.js** `22+` - Backend runtime
|
||||
- **TypeScript** `5.7+` - Type-safe development
|
||||
- **Vite** `6.2+` - Build tooling
|
||||
|
||||
### Architecture & Patterns
|
||||
|
||||
- **Dependency Injection** - IoC container with decorator-based registration
|
||||
- **Event-Driven Architecture** - IPC communication between processes
|
||||
- **Module Federation** - Dynamic controller and service loading
|
||||
- **Observer Pattern** - State management and UI synchronization
|
||||
|
||||
### Development Tools
|
||||
|
||||
- **Vitest** - Unit testing framework
|
||||
- **ESLint** - Code linting
|
||||
- **Prettier** - Code formatting
|
||||
- **electron-builder** - Application packaging
|
||||
- **electron-updater** - Auto-update mechanism
|
||||
|
||||
### Security & Storage
|
||||
|
||||
- **Electron Safe Storage** - Encrypted token storage
|
||||
- **OAuth 2.0 + PKCE** - Secure authentication flow
|
||||
- **electron-store** - Persistent configuration
|
||||
- **Custom Protocol Handler** - Secure callback handling
|
||||
|
||||
## 🏗 Architecture
|
||||
|
||||
The desktop application uses a sophisticated dependency injection and event-driven architecture:
|
||||
|
||||
### 📁 Core Structure
|
||||
|
||||
```
|
||||
src/main/core/
|
||||
├── App.ts # 🎯 Main application orchestrator
|
||||
├── IoCContainer.ts # 🔌 Dependency injection container
|
||||
├── window/ # 🪟 Window management modules
|
||||
│ ├── WindowThemeManager.ts # 🎨 Theme synchronization
|
||||
│ ├── WindowPositionManager.ts # 📐 Position persistence
|
||||
│ ├── WindowErrorHandler.ts # ⚠️ Error boundaries
|
||||
│ └── WindowConfigBuilder.ts # ⚙️ Configuration builder
|
||||
├── browser/ # 🌐 Browser management modules
|
||||
│ ├── Browser.ts # 🪟 Individual window instances
|
||||
│ └── BrowserManager.ts # 👥 Multi-window coordinator
|
||||
├── ui/ # 🎨 UI system modules
|
||||
│ ├── Tray.ts # 📍 System tray integration
|
||||
│ ├── TrayManager.ts # 🔧 Tray management
|
||||
│ ├── MenuManager.ts # 📋 Native menu system
|
||||
│ └── ShortcutManager.ts # ⌨️ Global shortcuts
|
||||
└── infrastructure/ # 🔧 Infrastructure services
|
||||
├── StoreManager.ts # 💾 Configuration storage
|
||||
├── I18nManager.ts # 🌍 Internationalization
|
||||
├── UpdaterManager.ts # 📦 Auto-update system
|
||||
└── StaticFileServerManager.ts # 🗂️ Local file serving
|
||||
```
|
||||
|
||||
### 🔄 Application Lifecycle
|
||||
|
||||
The `App.ts` class orchestrates the entire application lifecycle through key phases:
|
||||
|
||||
#### 1. 🚀 Initialization Phase
|
||||
|
||||
- **System Information Logging** - Captures OS, CPU, RAM, and locale details
|
||||
- **Store Manager Setup** - Initializes persistent configuration storage
|
||||
- **Dynamic Module Loading** - Auto-discovers controllers and services via glob imports
|
||||
- **IPC Event Registration** - Sets up inter-process communication channels
|
||||
|
||||
#### 2. 🏃 Bootstrap Phase
|
||||
|
||||
- **Single Instance Check** - Ensures only one application instance runs
|
||||
- **IPC Server Launch** - Starts the communication server
|
||||
- **Core Manager Initialization** - Sequential initialization of all managers:
|
||||
- 🌍 I18n for internationalization
|
||||
- 📋 Menu system for native menus
|
||||
- 🗂️ Static file server for local assets
|
||||
- ⌨️ Global shortcuts registration
|
||||
- 🪟 Browser window management
|
||||
- 📍 System tray (Windows only)
|
||||
- 📦 Auto-updater system
|
||||
|
||||
### 🔧 Core Components Deep Dive
|
||||
|
||||
#### 🌐 Browser Management System
|
||||
|
||||
- **Multi-Window Architecture** - Supports chat, settings, and devtools windows
|
||||
- **Window State Management** - Handles positioning, theming, and lifecycle
|
||||
- **WebContents Mapping** - Bidirectional mapping between WebContents and identifiers
|
||||
- **Event Broadcasting** - Centralized event distribution to all or specific windows
|
||||
|
||||
#### 🔌 Dependency Injection & Event System
|
||||
|
||||
- **IoC Container** - WeakMap-based container for decorated controller methods
|
||||
- **Decorator Registration** - `@ipcClientEvent` and `@ipcServerEvent` decorators
|
||||
- **Automatic Event Mapping** - Events registered during controller loading
|
||||
- **Service Locator** - Type-safe service and controller retrieval
|
||||
|
||||
#### 🪟 Window Management
|
||||
|
||||
- **Theme-Aware Windows** - Automatic adaptation to system dark/light mode
|
||||
- **Platform-Specific Styling** - Windows title bar and overlay customization
|
||||
- **Position Persistence** - Save and restore window positions across sessions
|
||||
- **Error Boundaries** - Centralized error handling for window operations
|
||||
|
||||
#### 🔧 Infrastructure Services
|
||||
|
||||
##### 🌍 I18n Manager
|
||||
|
||||
- **18+ Language Support** with lazy loading and namespace organization
|
||||
- **System Integration** with Electron's locale detection
|
||||
- **Dynamic UI Refresh** on language changes
|
||||
- **Resource Management** with efficient loading strategies
|
||||
|
||||
##### 📦 Update Manager
|
||||
|
||||
- **Multi-Channel Support** (stable, beta, nightly) with configurable intervals
|
||||
- **Background Downloads** with progress tracking and user notifications
|
||||
- **Rollback Protection** with error handling and recovery mechanisms
|
||||
- **Channel Management** with automatic channel switching
|
||||
|
||||
##### 💾 Store Manager
|
||||
|
||||
- **Type-Safe Storage** using electron-store with TypeScript interfaces
|
||||
- **Encrypted Secrets** via Electron's Safe Storage API
|
||||
- **Configuration Validation** with default value management
|
||||
- **File System Integration** with automatic directory creation
|
||||
|
||||
##### 🗂️ Static File Server
|
||||
|
||||
- **Local HTTP Server** for serving application assets and user files
|
||||
- **Security Controls** with request filtering and access validation
|
||||
- **File Management** with upload, download, and deletion capabilities
|
||||
- **Path Resolution** with intelligent routing between storage locations
|
||||
|
||||
#### 🎨 UI System Integration
|
||||
|
||||
- **Global Shortcuts** - Platform-aware keyboard shortcut registration with conflict detection
|
||||
- **System Tray** - Native integration with context menus and notifications
|
||||
- **Native Menus** - Platform-specific application and context menus with i18n
|
||||
- **Theme Synchronization** - Automatic theme updates across all UI components
|
||||
|
||||
### 🏛 Controller & Service Architecture
|
||||
|
||||
#### 🎮 Controller Pattern
|
||||
|
||||
- **IPC Event Handling** - Processes events from renderer with decorator-based registration
|
||||
- **Lifecycle Hooks** - `beforeAppReady` and `afterAppReady` for initialization phases
|
||||
- **Type-Safe Communication** - Strong typing for all IPC events and responses
|
||||
- **Error Boundaries** - Comprehensive error handling with proper propagation
|
||||
|
||||
#### 🔧 Service Pattern
|
||||
|
||||
- **Business Logic Encapsulation** - Clean separation of concerns
|
||||
- **Dependency Management** - Managed through IoC container
|
||||
- **Cross-Controller Sharing** - Services accessible via service locator pattern
|
||||
- **Resource Management** - Proper initialization and cleanup
|
||||
|
||||
### 🔗 Inter-Process Communication
|
||||
|
||||
#### 📡 IPC System Features
|
||||
|
||||
- **Bidirectional Communication** - Main↔Renderer and Main↔Next.js server
|
||||
- **Type-Safe Events** - TypeScript interfaces for all event parameters
|
||||
- **Context Awareness** - Events include sender context for window-specific operations
|
||||
- **Error Propagation** - Centralized error handling with proper status codes
|
||||
|
||||
#### 🛡️ Security Features
|
||||
|
||||
- **OAuth 2.0 + PKCE** - Secure authentication with state parameter validation
|
||||
- **Encrypted Token Storage** - Using Electron's Safe Storage API when available
|
||||
- **Custom Protocol Handler** - Secure callback handling for OAuth flows
|
||||
- **Request Filtering** - Security controls for web requests and external links
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
### Test Structure
|
||||
|
||||
```bash
|
||||
apps/desktop/src/main/controllers/__tests__/ # Controller unit tests
|
||||
tests/ # Integration tests
|
||||
```
|
||||
|
||||
### Running Tests
|
||||
|
||||
```bash
|
||||
pnpm test # Run all tests
|
||||
pnpm test:watch # Watch mode
|
||||
pnpm typecheck # Type validation
|
||||
```
|
||||
|
||||
### Test Coverage
|
||||
|
||||
- **Controller Tests** - IPC event handling validation
|
||||
- **Service Tests** - Business logic verification
|
||||
- **Integration Tests** - End-to-end workflow testing
|
||||
- **Type Tests** - TypeScript interface validation
|
||||
|
||||
## 🔒 Security Features
|
||||
|
||||
### Authentication & Authorization
|
||||
|
||||
- **OAuth 2.0 Flow** with PKCE for secure token exchange
|
||||
- **State Parameter Validation** to prevent CSRF attacks
|
||||
- **Encrypted Token Storage** using platform-native secure storage
|
||||
- **Automatic Token Refresh** with fallback to re-authentication
|
||||
|
||||
### Application Security
|
||||
|
||||
- **Code Signing** - macOS notarization for enhanced security
|
||||
- **Sandboxing** - Controlled access to system resources
|
||||
- **CSP Controls** - Content Security Policy management
|
||||
- **Request Filtering** - Security controls for external requests
|
||||
|
||||
### Data Protection
|
||||
|
||||
- **Encrypted Configuration** - Sensitive data encrypted at rest
|
||||
- **Secure IPC** - Type-safe communication channels
|
||||
- **Path Validation** - Secure file system access controls
|
||||
- **Network Security** - HTTPS enforcement and proxy support
|
||||
|
||||
## 🤝 Contribution
|
||||
|
||||
Desktop application development involves complex cross-platform considerations and native integrations. We welcome community contributions to improve functionality, performance, and user experience. You can participate in improvements through:
|
||||
|
||||
### How to Contribute
|
||||
|
||||
1. **Platform Support**: Enhance cross-platform compatibility and native integrations
|
||||
2. **Performance Optimization**: Improve application startup time, memory usage, and responsiveness
|
||||
3. **Feature Development**: Add new desktop-specific features and capabilities
|
||||
4. **Bug Fixes**: Fix platform-specific issues and edge cases
|
||||
5. **Security Improvements**: Enhance security measures and authentication flows
|
||||
6. **UI/UX Enhancements**: Improve desktop user interface and experience
|
||||
|
||||
### Contribution Process
|
||||
|
||||
1. Fork the [LobeChat repository](https://github.com/lobehub/lobe-chat)
|
||||
2. Set up the desktop development environment following our setup guide
|
||||
3. Make your changes to the desktop application
|
||||
4. Submit a Pull Request describing:
|
||||
|
||||
- Platform compatibility testing results
|
||||
- Performance impact analysis
|
||||
- Security considerations
|
||||
- User experience improvements
|
||||
- Breaking changes (if any)
|
||||
|
||||
### Development Areas
|
||||
|
||||
- **Core Architecture**: Dependency injection, event system, and lifecycle management
|
||||
- **Window Management**: Multi-window support, theme synchronization, and state persistence
|
||||
- **IPC Communication**: Type-safe inter-process communication between main and renderer
|
||||
- **Platform Integration**: Native menus, shortcuts, notifications, and system tray
|
||||
- **Security Features**: OAuth flows, token encryption, and secure storage
|
||||
- **Auto-Update System**: Multi-channel updates and rollback mechanisms
|
||||
|
||||
## 📚 Additional Resources
|
||||
|
||||
- **Development Guide**: [`Development.md`](./Development.md) - Comprehensive development documentation
|
||||
- **Architecture Docs**: [`/docs`](../../docs/) - Detailed technical specifications
|
||||
- **Contributing**: [`CONTRIBUTING.md`](../../CONTRIBUTING.md) - Contribution guidelines
|
||||
- **Issues & Support**: [GitHub Issues](https://github.com/lobehub/lobe-chat/issues)
|
||||
- **稳定版**:经过充分测试的正式版本
|
||||
- **测试版 (Beta)**:预发布版本,包含即将发布的新功能
|
||||
- **每日构建版 (Nightly)**:包含最新开发进展的构建版本
|
||||
|
||||
@@ -1,353 +0,0 @@
|
||||
# 🤯 LobeHub 桌面应用程序
|
||||
|
||||
LobeHub Desktop 是 [LobeChat](https://github.com/lobehub/lobe-chat) 的跨平台桌面应用程序,使用 Electron 构建,提供了更加原生的桌面体验和功能。
|
||||
|
||||
## ✨ 功能特点
|
||||
|
||||
- **🌍 跨平台支持**:支持 macOS (Intel/Apple Silicon)、Windows 和 Linux 系统
|
||||
- **🔄 自动更新**:内置更新机制,确保您始终使用最新版本
|
||||
- **🌐 多语言支持**:完整的 i18n 支持,包含 18+ 种语言的懒加载
|
||||
- **🎨 原生集成**:与操作系统深度集成,提供原生菜单、快捷键和通知
|
||||
- **🔒 安全可靠**:macOS 公证认证,加密令牌存储,安全的 OAuth 流程
|
||||
- **📦 多渠道发布**:提供稳定版、测试版和每日构建版本
|
||||
- **⚡ 高级窗口管理**:多窗口架构,支持主题同步
|
||||
- **🔗 远程服务器同步**:与远程 LobeChat 实例的安全数据同步
|
||||
- **🎯 开发者工具**:内置开发面板和全面的调试工具
|
||||
|
||||
## 🚀 开发环境设置
|
||||
|
||||
### 前提条件
|
||||
|
||||
- **Node.js** 22+
|
||||
- **pnpm** 10+
|
||||
- **Electron** 兼容的开发环境
|
||||
|
||||
### 快速开始
|
||||
|
||||
```bash
|
||||
# 安装依赖
|
||||
pnpm install-isolated
|
||||
|
||||
# 启动开发服务器
|
||||
pnpm electron:dev
|
||||
|
||||
# 类型检查
|
||||
pnpm typecheck
|
||||
|
||||
# 运行测试
|
||||
pnpm test
|
||||
```
|
||||
|
||||
### 环境变量配置
|
||||
|
||||
复制 `.env.desktop` 到 `.env` 并根据需要配置:
|
||||
|
||||
```bash
|
||||
cp .env.desktop .env
|
||||
```
|
||||
|
||||
> \[!WARNING]
|
||||
> 在进行更改之前请备份您的 `.env` 文件,避免丢失配置。
|
||||
|
||||
### 构建命令
|
||||
|
||||
| 命令 | 描述 |
|
||||
| ------------------ | ---------------------------------- |
|
||||
| `pnpm build` | 构建所有平台 |
|
||||
| `pnpm build:mac` | 构建 macOS (Intel + Apple Silicon) |
|
||||
| `pnpm build:win` | 构建 Windows |
|
||||
| `pnpm build:linux` | 构建 Linux |
|
||||
| `pnpm build-local` | 本地开发构建 |
|
||||
|
||||
### 开发工作流
|
||||
|
||||
```bash
|
||||
# 1. 开发
|
||||
pnpm electron:dev # 启动热重载开发服务器
|
||||
|
||||
# 2. 代码质量
|
||||
pnpm lint # ESLint 检查
|
||||
pnpm format # Prettier 格式化
|
||||
pnpm typecheck # TypeScript 验证
|
||||
|
||||
# 3. 测试
|
||||
pnpm test # 运行 Vitest 测试
|
||||
|
||||
# 4. 构建和打包
|
||||
pnpm build # 生产构建
|
||||
pnpm build-local # 本地测试构建
|
||||
```
|
||||
|
||||
## 🎯 发布渠道
|
||||
|
||||
| 渠道 | 描述 | 稳定性 | 自动更新 |
|
||||
| ------------------------ | ---------------------- | ------ | -------- |
|
||||
| **稳定版** | 经过充分测试的正式版本 | 🟢 高 | ✅ 是 |
|
||||
| **测试版 (Beta)** | 包含新功能的预发布版本 | 🟡 中 | ✅ 是 |
|
||||
| **每日构建版 (Nightly)** | 包含最新更改的每日构建 | 🟠 低 | ✅ 是 |
|
||||
|
||||
## 🛠 技术栈
|
||||
|
||||
### 核心框架
|
||||
|
||||
- **Electron** `37.1.0` - 跨平台桌面框架
|
||||
- **Node.js** `22+` - 后端运行时
|
||||
- **TypeScript** `5.7+` - 类型安全开发
|
||||
- **Vite** `6.2+` - 构建工具
|
||||
|
||||
### 架构和模式
|
||||
|
||||
- **依赖注入** - 基于装饰器注册的 IoC 容器
|
||||
- **事件驱动架构** - 进程间 IPC 通信
|
||||
- **模块联邦** - 动态控制器和服务加载
|
||||
- **观察者模式** - 状态管理和 UI 同步
|
||||
|
||||
### 开发工具
|
||||
|
||||
- **Vitest** - 单元测试框架
|
||||
- **ESLint** - 代码检查
|
||||
- **Prettier** - 代码格式化
|
||||
- **electron-builder** - 应用程序打包
|
||||
- **electron-updater** - 自动更新机制
|
||||
|
||||
### 安全和存储
|
||||
|
||||
- **Electron Safe Storage** - 加密令牌存储
|
||||
- **OAuth 2.0 + PKCE** - 安全认证流程
|
||||
- **electron-store** - 持久化配置
|
||||
- **自定义协议处理器** - 安全回调处理
|
||||
|
||||
## 🏗 架构设计
|
||||
|
||||
桌面应用程序采用了复杂的依赖注入和事件驱动架构:
|
||||
|
||||
### 📁 核心结构
|
||||
|
||||
```
|
||||
src/main/core/
|
||||
├── App.ts # 🎯 主应用程序协调器
|
||||
├── IoCContainer.ts # 🔌 依赖注入容器
|
||||
├── window/ # 🪟 窗口管理模块
|
||||
│ ├── WindowThemeManager.ts # 🎨 主题同步
|
||||
│ ├── WindowPositionManager.ts # 📐 位置持久化
|
||||
│ ├── WindowErrorHandler.ts # ⚠️ 错误边界
|
||||
│ └── WindowConfigBuilder.ts # ⚙️ 配置构建器
|
||||
├── browser/ # 🌐 浏览器管理模块
|
||||
│ ├── Browser.ts # 🪟 单个窗口实例
|
||||
│ └── BrowserManager.ts # 👥 多窗口协调器
|
||||
├── ui/ # 🎨 UI 系统模块
|
||||
│ ├── Tray.ts # 📍 系统托盘集成
|
||||
│ ├── TrayManager.ts # 🔧 托盘管理
|
||||
│ ├── MenuManager.ts # 📋 原生菜单系统
|
||||
│ └── ShortcutManager.ts # ⌨️ 全局快捷键
|
||||
└── infrastructure/ # 🔧 基础设施服务
|
||||
├── StoreManager.ts # 💾 配置存储
|
||||
├── I18nManager.ts # 🌍 国际化
|
||||
├── UpdaterManager.ts # 📦 自动更新系统
|
||||
└── StaticFileServerManager.ts # 🗂️ 本地文件服务
|
||||
```
|
||||
|
||||
### 🔄 应用程序生命周期
|
||||
|
||||
`App.ts` 类通过几个关键阶段协调整个应用程序的生命周期:
|
||||
|
||||
#### 1. 🚀 初始化阶段
|
||||
|
||||
- **系统信息记录** - 捕获操作系统、CPU、内存和区域设置详细信息
|
||||
- **存储管理器设置** - 初始化持久配置存储
|
||||
- **动态模块加载** - 通过 glob 导入自动发现控制器和服务
|
||||
- **IPC 事件注册** - 设置进程间通信通道
|
||||
|
||||
#### 2. 🏃 引导阶段
|
||||
|
||||
- **单实例检查** - 确保只运行一个应用程序实例
|
||||
- **IPC 服务器启动** - 启动通信服务器
|
||||
- **核心管理器初始化** - 按顺序初始化所有管理器:
|
||||
- 🌍 国际化管理器
|
||||
- 📋 原生菜单系统
|
||||
- 🗂️ 本地资源服务器
|
||||
- ⌨️ 全局快捷键注册
|
||||
- 🪟 浏览器窗口管理
|
||||
- 📍 系统托盘(仅 Windows)
|
||||
- 📦 自动更新系统
|
||||
|
||||
### 🔧 核心组件深度解析
|
||||
|
||||
#### 🌐 浏览器管理系统
|
||||
|
||||
- **多窗口架构** - 支持聊天、设置和开发工具窗口
|
||||
- **窗口状态管理** - 处理定位、主题和生命周期
|
||||
- **WebContents 映射** - WebContents 和标识符之间的双向映射
|
||||
- **事件广播** - 向所有或特定窗口的集中事件分发
|
||||
|
||||
#### 🔌 依赖注入和事件系统
|
||||
|
||||
- **IoC 容器** - 基于 WeakMap 的装饰控制器方法容器
|
||||
- **装饰器注册** - `@ipcClientEvent` 和 `@ipcServerEvent` 装饰器
|
||||
- **自动事件映射** - 控制器加载期间注册的事件
|
||||
- **服务定位器** - 类型安全的服务和控制器检索
|
||||
|
||||
#### 🪟 窗口管理
|
||||
|
||||
- **主题感知窗口** - 自动适应系统深色 / 浅色模式
|
||||
- **平台特定样式** - Windows 标题栏和覆盖自定义
|
||||
- **位置持久化** - 跨会话保存和恢复窗口位置
|
||||
- **错误边界** - 窗口操作的集中错误处理
|
||||
|
||||
#### 🔧 基础设施服务
|
||||
|
||||
##### 🌍 国际化管理器
|
||||
|
||||
- **18+ 语言支持** 懒加载和命名空间组织
|
||||
- **系统集成** 与 Electron 的区域检测集成
|
||||
- **动态 UI 刷新** 语言更改时的 UI 更新
|
||||
- **资源管理** 高效的加载策略
|
||||
|
||||
##### 📦 更新管理器
|
||||
|
||||
- **多渠道支持** (稳定版、测试版、每日构建)可配置间隔
|
||||
- **后台下载** 进度跟踪和用户通知
|
||||
- **回滚保护** 错误处理和恢复机制
|
||||
- **渠道管理** 自动渠道切换
|
||||
|
||||
##### 💾 存储管理器
|
||||
|
||||
- **类型安全存储** 使用带有 TypeScript 接口的 electron-store
|
||||
- **加密机密** 通过 Electron 的安全存储 API
|
||||
- **配置验证** 默认值管理
|
||||
- **文件系统集成** 自动目录创建
|
||||
|
||||
##### 🗂️ 静态文件服务器
|
||||
|
||||
- **本地 HTTP 服务器** 用于提供应用程序资源和用户文件
|
||||
- **安全控制** 请求过滤和访问验证
|
||||
- **文件管理** 上传、下载和删除功能
|
||||
- **路径解析** 存储位置之间的智能路由
|
||||
|
||||
#### 🎨 UI 系统集成
|
||||
|
||||
- **全局快捷键** - 平台感知的键盘快捷键注册与冲突检测
|
||||
- **系统托盘** - 带有上下文菜单和通知的原生集成
|
||||
- **原生菜单** - 带有 i18n 的平台特定应用程序和上下文菜单
|
||||
- **主题同步** - 所有 UI 组件的自动主题更新
|
||||
|
||||
### 🏛 控制器和服务架构
|
||||
|
||||
#### 🎮 控制器模式
|
||||
|
||||
- **IPC 事件处理** - 通过基于装饰器的注册处理来自渲染器的事件
|
||||
- **生命周期钩子** - 初始化阶段的 `beforeAppReady` 和 `afterAppReady`
|
||||
- **类型安全通信** - 所有 IPC 事件和响应的强类型
|
||||
- **错误边界** - 具有适当传播的全面错误处理
|
||||
|
||||
#### 🔧 服务模式
|
||||
|
||||
- **业务逻辑封装** - 关注点的清晰分离
|
||||
- **依赖管理** - 通过 IoC 容器管理
|
||||
- **跨控制器共享** - 通过服务定位器模式访问的服务
|
||||
- **资源管理** - 适当的初始化和清理
|
||||
|
||||
### 🔗 进程间通信
|
||||
|
||||
#### 📡 IPC 系统功能
|
||||
|
||||
- **双向通信** - Main↔Renderer 和 Main↔Next.js 服务器
|
||||
- **类型安全事件** - 所有事件参数的 TypeScript 接口
|
||||
- **上下文感知** - 事件包含用于窗口特定操作的发送者上下文
|
||||
- **错误传播** - 具有适当状态码的集中错误处理
|
||||
|
||||
#### 🛡️ 安全功能
|
||||
|
||||
- **OAuth 2.0 + PKCE** - 具有状态参数验证的安全认证
|
||||
- **加密令牌存储** - 在可用时使用 Electron 的安全存储 API
|
||||
- **自定义协议处理器** - OAuth 流程的安全回调处理
|
||||
- **请求过滤** - 网络请求和外部链接的安全控制
|
||||
|
||||
## 🧪 测试
|
||||
|
||||
### 测试结构
|
||||
|
||||
```bash
|
||||
apps/desktop/src/main/controllers/__tests__/ # 控制器单元测试
|
||||
tests/ # 集成测试
|
||||
```
|
||||
|
||||
### 运行测试
|
||||
|
||||
```bash
|
||||
pnpm test # 运行所有测试
|
||||
pnpm test:watch # 监视模式
|
||||
pnpm typecheck # 类型验证
|
||||
```
|
||||
|
||||
### 测试覆盖
|
||||
|
||||
- **控制器测试** - IPC 事件处理验证
|
||||
- **服务测试** - 业务逻辑验证
|
||||
- **集成测试** - 端到端工作流测试
|
||||
- **类型测试** - TypeScript 接口验证
|
||||
|
||||
## 🔒 安全功能
|
||||
|
||||
### 认证和授权
|
||||
|
||||
- **OAuth 2.0 流程** 使用 PKCE 进行安全令牌交换
|
||||
- **状态参数验证** 防止 CSRF 攻击
|
||||
- **加密令牌存储** 使用平台原生安全存储
|
||||
- **自动令牌刷新** 在失败时回退到重新认证
|
||||
|
||||
### 应用程序安全
|
||||
|
||||
- **代码签名** - macOS 公证认证以增强安全性
|
||||
- **沙盒** - 对系统资源的受控访问
|
||||
- **CSP 控制** - 内容安全策略管理
|
||||
- **请求过滤** - 外部请求的安全控制
|
||||
|
||||
### 数据保护
|
||||
|
||||
- **加密配置** - 敏感数据静态加密
|
||||
- **安全 IPC** - 类型安全的通信通道
|
||||
- **路径验证** - 安全的文件系统访问控制
|
||||
- **网络安全** - HTTPS 强制和代理支持
|
||||
|
||||
## 🤝 参与贡献
|
||||
|
||||
桌面应用程序开发涉及复杂的跨平台考虑和原生集成。我们欢迎社区贡献来改进功能、性能和用户体验。您可以通过以下方式参与改进:
|
||||
|
||||
### 如何贡献
|
||||
|
||||
1. **平台支持**:增强跨平台兼容性和原生集成
|
||||
2. **性能优化**:改进应用程序启动时间、内存使用和响应性
|
||||
3. **功能开发**:添加新的桌面特定功能和能力
|
||||
4. **错误修复**:修复平台特定问题和边缘情况
|
||||
5. **安全改进**:增强安全措施和认证流程
|
||||
6. **UI/UX 增强**:改进桌面用户界面和体验
|
||||
|
||||
### 贡献流程
|
||||
|
||||
1. Fork [LobeChat 仓库](https://github.com/lobehub/lobe-chat)
|
||||
2. 按照我们的设置指南建立桌面开发环境
|
||||
3. 对桌面应用程序进行修改
|
||||
4. 提交 Pull Request 并描述:
|
||||
|
||||
- 平台兼容性测试结果
|
||||
- 性能影响分析
|
||||
- 安全考虑
|
||||
- 用户体验改进
|
||||
- 破坏性更改(如有)
|
||||
|
||||
### 开发领域
|
||||
|
||||
- **核心架构**:依赖注入、事件系统和生命周期管理
|
||||
- **窗口管理**:多窗口支持、主题同步和状态持久化
|
||||
- **IPC 通信**:主进程和渲染进程之间的类型安全进程间通信
|
||||
- **平台集成**:原生菜单、快捷键、通知和系统托盘
|
||||
- **安全功能**:OAuth 流程、令牌加密和安全存储
|
||||
- **自动更新系统**:多渠道更新和回滚机制
|
||||
|
||||
## 📚 其他资源
|
||||
|
||||
- **开发指南**:[`Development.md`](./Development.md) - 全面的开发文档
|
||||
- **架构文档**:[`/docs`](../../docs/) - 详细的技术规范
|
||||
- **贡献指南**:[`CONTRIBUTING.md`](../../CONTRIBUTING.md) - 贡献指导
|
||||
- **问题和支持**:[GitHub Issues](https://github.com/lobehub/lobe-chat/issues)
|
||||
|
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 78 KiB |
|
Before Width: | Height: | Size: 156 KiB |
|
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 74 KiB |
|
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 72 KiB |
@@ -25,11 +25,6 @@ const config = {
|
||||
artifactName: '${productName}-${version}.${ext}',
|
||||
},
|
||||
asar: true,
|
||||
asarUnpack: [
|
||||
// https://github.com/electron-userland/electron-builder/issues/9001#issuecomment-2778802044
|
||||
'**/node_modules/sharp/**/*',
|
||||
'**/node_modules/@img/**/*',
|
||||
],
|
||||
detectUpdateChannel: true,
|
||||
directories: {
|
||||
buildResources: 'build',
|
||||
|
||||
@@ -11,9 +11,8 @@ console.log(`[electron-vite.config.ts] Detected UPDATE_CHANNEL: ${updateChannel}
|
||||
export default defineConfig({
|
||||
main: {
|
||||
build: {
|
||||
minify: !isDev,
|
||||
outDir: 'dist/main',
|
||||
sourcemap: isDev ? 'inline' : false,
|
||||
sourcemap: isDev,
|
||||
},
|
||||
// 这里是关键:在构建时进行文本替换
|
||||
define: {
|
||||
@@ -31,9 +30,8 @@ export default defineConfig({
|
||||
},
|
||||
preload: {
|
||||
build: {
|
||||
minify: !isDev,
|
||||
outDir: 'dist/preload',
|
||||
sourcemap: isDev ? 'inline' : false,
|
||||
sourcemap: isDev,
|
||||
},
|
||||
plugins: [externalizeDepsPlugin({})],
|
||||
resolve: {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"name": "lobehub-desktop-dev",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"version": "0.0.10",
|
||||
"description": "LobeHub Desktop Application",
|
||||
"homepage": "https://lobehub.com",
|
||||
"repository": {
|
||||
@@ -15,7 +14,6 @@
|
||||
"build-local": "npm run build && electron-builder --dir --config electron-builder.js --c.mac.notarize=false -c.mac.identity=null --c.asar=false",
|
||||
"build:linux": "npm run build && electron-builder --linux --config electron-builder.js --publish never",
|
||||
"build:mac": "npm run build && electron-builder --mac --config electron-builder.js --publish never",
|
||||
"build:mac:local": "npm run build && UPDATE_CHANNEL=nightly electron-builder --mac --config electron-builder.js --publish never",
|
||||
"build:win": "npm run build && electron-builder --win --config electron-builder.js --publish never",
|
||||
"electron:dev": "electron-vite dev",
|
||||
"electron:run-unpack": "electron .",
|
||||
@@ -26,12 +24,10 @@
|
||||
"lint": "eslint --cache ",
|
||||
"pg-server": "bun run scripts/pglite-server.ts",
|
||||
"start": "electron-vite preview",
|
||||
"test": "vitest --run",
|
||||
"typecheck": "tsgo --noEmit -p tsconfig.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"electron-updater": "^6.6.2",
|
||||
"electron-window-state": "^5.0.3",
|
||||
"get-port-please": "^3.1.2",
|
||||
"pdfjs-dist": "4.10.38"
|
||||
},
|
||||
@@ -49,10 +45,10 @@
|
||||
"@types/resolve": "^1.20.6",
|
||||
"@types/semver": "^7.7.0",
|
||||
"@types/set-cookie-parser": "^2.4.10",
|
||||
"@typescript/native-preview": "7.0.0-dev.20250711.1",
|
||||
"@typescript/native-preview": "latest",
|
||||
"consola": "^3.1.0",
|
||||
"cookie": "^1.0.2",
|
||||
"electron": "~37.1.0",
|
||||
"electron": "^36.2.0",
|
||||
"electron-builder": "^26.0.12",
|
||||
"electron-is": "^3.0.0",
|
||||
"electron-log": "^5.3.3",
|
||||
@@ -60,24 +56,19 @@
|
||||
"electron-vite": "^3.0.0",
|
||||
"execa": "^9.5.2",
|
||||
"fix-path": "^4.0.0",
|
||||
"http-proxy-agent": "^7.0.2",
|
||||
"https-proxy-agent": "^7.0.6",
|
||||
"just-diff": "^6.0.2",
|
||||
"lodash": "^4.17.21",
|
||||
"lodash-es": "^4.17.21",
|
||||
"pglite-server": "^0.1.4",
|
||||
"resolve": "^1.22.8",
|
||||
"semver": "^7.5.4",
|
||||
"set-cookie-parser": "^2.7.1",
|
||||
"tsx": "^4.19.3",
|
||||
"typescript": "^5.7.3",
|
||||
"undici": "^7.9.0",
|
||||
"vite": "^6.3.5",
|
||||
"vitest": "^3.2.4"
|
||||
"vite": "^6.2.5"
|
||||
},
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"electron",
|
||||
"electron-builder"
|
||||
"electron"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
Before Width: | Height: | Size: 807 B |
|
After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 738 B |
|
Before Width: | Height: | Size: 2.5 KiB |
@@ -0,0 +1,14 @@
|
||||
import { PGlite } from "@electric-sql/pglite";
|
||||
import { createServer } from "pglite-server";
|
||||
|
||||
// 创建或连接到您现有的 PGlite 数据库
|
||||
const db = new PGlite("/Users/arvinxx/Library/Application Support/lobehub-desktop/lobehub-local-db");
|
||||
await db.waitReady;
|
||||
|
||||
// 创建服务器并监听端口
|
||||
const PORT = 6543;
|
||||
const pgServer = createServer(db);
|
||||
|
||||
pgServer.listen(PORT, () => {
|
||||
console.log(`PGlite 服务器已启动,监听端口 ${PORT}`);
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { BrowserWindowOpts } from './core/browser/Browser';
|
||||
import type { BrowserWindowOpts } from './core/Browser';
|
||||
|
||||
export const BrowsersIdentifiers = {
|
||||
chat: 'chat',
|
||||
|
||||
@@ -27,6 +27,3 @@ export const LOCAL_DATABASE_DIR = 'lobehub-local-db';
|
||||
export const FILE_STORAGE_DIR = 'file-storage';
|
||||
// Plugin 安装目录
|
||||
export const INSTALL_PLUGINS_DIR = 'plugins';
|
||||
|
||||
// Desktop file service
|
||||
export const LOCAL_STORAGE_URL_PREFIX = '/lobe-desktop-file';
|
||||
|
||||
@@ -1,29 +1,3 @@
|
||||
import { dev, linux, macOS, windows } from 'electron-is';
|
||||
import os from 'node:os';
|
||||
|
||||
export const isDev = dev();
|
||||
export const isDev = process.env.NODE_ENV === 'development';
|
||||
|
||||
export const OFFICIAL_CLOUD_SERVER = process.env.OFFICIAL_CLOUD_SERVER || 'https://lobechat.com';
|
||||
|
||||
export const isMac = macOS();
|
||||
export const isWindows = windows();
|
||||
export const isLinux = linux();
|
||||
|
||||
function getIsWindows11() {
|
||||
if (!isWindows) return false;
|
||||
// 获取操作系统版本(如 "10.0.22621")
|
||||
const release = os.release();
|
||||
const parts = release.split('.');
|
||||
|
||||
// 主版本和次版本
|
||||
const majorVersion = parseInt(parts[0], 10);
|
||||
const minorVersion = parseInt(parts[1], 10);
|
||||
|
||||
// 构建号是第三部分
|
||||
const buildNumber = parseInt(parts[2], 10);
|
||||
|
||||
// Windows 11 的构建号从 22000 开始
|
||||
return majorVersion === 10 && minorVersion === 0 && buildNumber >= 22_000;
|
||||
}
|
||||
|
||||
export const isWindows11 = getIsWindows11();
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
/**
|
||||
* 应用设置存储相关常量
|
||||
*/
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
|
||||
import { appStorageDir } from '@/const/dir';
|
||||
import { DEFAULT_SHORTCUTS_CONFIG } from '@/shortcuts';
|
||||
import { ElectronMainStore } from '@/types/store';
|
||||
@@ -12,15 +10,6 @@ import { ElectronMainStore } from '@/types/store';
|
||||
*/
|
||||
export const STORE_NAME = 'lobehub-settings';
|
||||
|
||||
export const defaultProxySettings: NetworkProxySettings = {
|
||||
enableProxy: false,
|
||||
proxyBypass: 'localhost, 127.0.0.1, ::1',
|
||||
proxyPort: '',
|
||||
proxyRequireAuth: false,
|
||||
proxyServer: '',
|
||||
proxyType: 'http',
|
||||
};
|
||||
|
||||
/**
|
||||
* 存储默认值
|
||||
*/
|
||||
@@ -28,8 +17,6 @@ export const STORE_DEFAULTS: ElectronMainStore = {
|
||||
dataSyncConfig: { storageMode: 'local' },
|
||||
encryptedTokens: {},
|
||||
locale: 'auto',
|
||||
networkProxy: defaultProxySettings,
|
||||
shortcuts: DEFAULT_SHORTCUTS_CONFIG,
|
||||
storagePath: appStorageDir,
|
||||
themeMode: 'auto',
|
||||
};
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
// Theme colors
|
||||
export const BACKGROUND_DARK = '#000';
|
||||
export const BACKGROUND_LIGHT = '#f8f8f8';
|
||||
export const SYMBOL_COLOR_DARK = '#ffffff80';
|
||||
export const SYMBOL_COLOR_LIGHT = '#00000080';
|
||||
|
||||
// Window dimensions and constraints
|
||||
export const TITLE_BAR_HEIGHT = 29;
|
||||
|
||||
// Default window configuration
|
||||
export const THEME_CHANGE_DELAY = 100;
|
||||
@@ -1,9 +1,10 @@
|
||||
import { DataSyncConfig } from '@lobechat/electron-client-ipc';
|
||||
import { BrowserWindow, shell } from 'electron';
|
||||
import { BrowserWindow, app, shell } from 'electron';
|
||||
import crypto from 'node:crypto';
|
||||
import querystring from 'node:querystring';
|
||||
import { URL } from 'node:url';
|
||||
|
||||
import { name } from '@/../../package.json';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import RemoteServerConfigCtr from './RemoteServerConfigCtr';
|
||||
@@ -12,9 +13,10 @@ import { ControllerModule, ipcClientEvent } from './index';
|
||||
// Create logger
|
||||
const logger = createLogger('controllers:AuthCtr');
|
||||
|
||||
const protocolPrefix = `com.lobehub.${name}`;
|
||||
/**
|
||||
* Authentication Controller
|
||||
* 使用中间页 + 轮询的方式实现 OAuth 授权流程
|
||||
* Used to implement the OAuth authorization flow
|
||||
*/
|
||||
export default class AuthCtr extends ControllerModule {
|
||||
/**
|
||||
@@ -30,29 +32,9 @@ export default class AuthCtr extends ControllerModule {
|
||||
private codeVerifier: string | null = null;
|
||||
private authRequestState: string | null = null;
|
||||
|
||||
/**
|
||||
* 轮询相关参数
|
||||
*/
|
||||
// eslint-disable-next-line no-undef
|
||||
private pollingInterval: NodeJS.Timeout | null = null;
|
||||
private cachedRemoteUrl: string | null = null;
|
||||
|
||||
/**
|
||||
* 自动刷新定时器
|
||||
*/
|
||||
// eslint-disable-next-line no-undef
|
||||
private autoRefreshTimer: NodeJS.Timeout | null = null;
|
||||
|
||||
/**
|
||||
* 构造 redirect_uri,确保授权和令牌交换时使用相同的 URI
|
||||
* @param remoteUrl 远程服务器 URL
|
||||
* @param includeHandoffId 是否包含 handoff ID(仅在授权时需要)
|
||||
*/
|
||||
private constructRedirectUri(remoteUrl: string): string {
|
||||
const callbackUrl = new URL('/oidc/callback/desktop', remoteUrl);
|
||||
|
||||
return callbackUrl.toString();
|
||||
}
|
||||
beforeAppReady = () => {
|
||||
this.registerProtocolHandler();
|
||||
};
|
||||
|
||||
/**
|
||||
* Request OAuth authorization
|
||||
@@ -61,9 +43,6 @@ export default class AuthCtr extends ControllerModule {
|
||||
async requestAuthorization(config: DataSyncConfig) {
|
||||
const remoteUrl = await this.remoteServerConfigCtr.getRemoteServerUrl(config);
|
||||
|
||||
// 缓存远程服务器 URL 用于后续轮询
|
||||
this.cachedRemoteUrl = remoteUrl;
|
||||
|
||||
logger.info(
|
||||
`Requesting OAuth authorization, storageMode:${config.storageMode} server URL: ${remoteUrl}`,
|
||||
);
|
||||
@@ -78,11 +57,8 @@ export default class AuthCtr extends ControllerModule {
|
||||
this.authRequestState = crypto.randomBytes(16).toString('hex');
|
||||
logger.debug(`Generated state parameter: ${this.authRequestState}`);
|
||||
|
||||
// Construct authorization URL with new redirect_uri
|
||||
// Construct authorization URL
|
||||
const authUrl = new URL('/oidc/auth', remoteUrl);
|
||||
const redirectUri = this.constructRedirectUri(remoteUrl);
|
||||
|
||||
logger.info('redirectUri', redirectUri);
|
||||
|
||||
// Add query parameters
|
||||
authUrl.search = querystring.stringify({
|
||||
@@ -90,9 +66,7 @@ export default class AuthCtr extends ControllerModule {
|
||||
code_challenge: codeChallenge,
|
||||
code_challenge_method: 'S256',
|
||||
prompt: 'consent',
|
||||
redirect_uri: redirectUri,
|
||||
// https://github.com/lobehub/lobe-chat/pull/8450
|
||||
resource: 'urn:lobehub:chat',
|
||||
redirect_uri: `${protocolPrefix}://auth/callback`,
|
||||
response_type: 'code',
|
||||
scope: 'profile email offline_access',
|
||||
state: this.authRequestState,
|
||||
@@ -104,9 +78,6 @@ export default class AuthCtr extends ControllerModule {
|
||||
await shell.openExternal(authUrl.toString());
|
||||
logger.debug('Opening authorization URL in default browser');
|
||||
|
||||
// Start polling for credentials
|
||||
this.startPolling();
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
logger.error('Authorization request failed:', error);
|
||||
@@ -115,188 +86,85 @@ export default class AuthCtr extends ControllerModule {
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动轮询机制获取凭证
|
||||
* Handle authorization callback
|
||||
* This method is called when the browser redirects to our custom protocol
|
||||
*/
|
||||
private startPolling() {
|
||||
if (!this.authRequestState) {
|
||||
logger.error('No handoff ID available for polling');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info('Starting credential polling');
|
||||
const pollInterval = 3000; // 3 seconds
|
||||
const maxPollTime = 5 * 60 * 1000; // 5 minutes
|
||||
const startTime = Date.now();
|
||||
|
||||
this.pollingInterval = setInterval(async () => {
|
||||
try {
|
||||
// Check if polling has timed out
|
||||
if (Date.now() - startTime > maxPollTime) {
|
||||
logger.warn('Credential polling timed out');
|
||||
this.stopPolling();
|
||||
this.broadcastAuthorizationFailed('Authorization timed out');
|
||||
return;
|
||||
}
|
||||
|
||||
// Poll for credentials
|
||||
const result = await this.pollForCredentials();
|
||||
|
||||
if (result) {
|
||||
logger.info('Successfully received credentials from polling');
|
||||
this.stopPolling();
|
||||
|
||||
// Validate state parameter
|
||||
if (result.state !== this.authRequestState) {
|
||||
logger.error(
|
||||
`Invalid state parameter: expected ${this.authRequestState}, received ${result.state}`,
|
||||
);
|
||||
this.broadcastAuthorizationFailed('Invalid state parameter');
|
||||
return;
|
||||
}
|
||||
|
||||
// Exchange code for tokens
|
||||
const exchangeResult = await this.exchangeCodeForToken(result.code, this.codeVerifier!);
|
||||
|
||||
if (exchangeResult.success) {
|
||||
logger.info('Authorization successful');
|
||||
this.broadcastAuthorizationSuccessful();
|
||||
} else {
|
||||
logger.warn(`Authorization failed: ${exchangeResult.error || 'Unknown error'}`);
|
||||
this.broadcastAuthorizationFailed(exchangeResult.error || 'Unknown error');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error during credential polling:', error);
|
||||
this.stopPolling();
|
||||
this.broadcastAuthorizationFailed('Polling error: ' + error.message);
|
||||
}
|
||||
}, pollInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止轮询
|
||||
*/
|
||||
private stopPolling() {
|
||||
if (this.pollingInterval) {
|
||||
clearInterval(this.pollingInterval);
|
||||
this.pollingInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动自动刷新定时器
|
||||
*/
|
||||
private startAutoRefresh() {
|
||||
// 先停止现有的定时器
|
||||
this.stopAutoRefresh();
|
||||
|
||||
const checkInterval = 2 * 60 * 1000; // 每 2 分钟检查一次
|
||||
logger.debug('Starting auto-refresh timer');
|
||||
|
||||
this.autoRefreshTimer = setInterval(async () => {
|
||||
try {
|
||||
// 检查 token 是否即将过期 (提前 5 分钟刷新)
|
||||
if (this.remoteServerConfigCtr.isTokenExpiringSoon()) {
|
||||
const expiresAt = this.remoteServerConfigCtr.getTokenExpiresAt();
|
||||
logger.info(
|
||||
`Token is expiring soon, triggering auto-refresh. Expires at: ${expiresAt ? new Date(expiresAt).toISOString() : 'unknown'}`,
|
||||
);
|
||||
|
||||
const result = await this.remoteServerConfigCtr.refreshAccessToken();
|
||||
if (result.success) {
|
||||
logger.info('Auto-refresh successful');
|
||||
this.broadcastTokenRefreshed();
|
||||
} else {
|
||||
logger.error(`Auto-refresh failed: ${result.error}`);
|
||||
// 如果自动刷新失败,停止定时器并清除 token
|
||||
this.stopAutoRefresh();
|
||||
await this.remoteServerConfigCtr.clearTokens();
|
||||
await this.remoteServerConfigCtr.setRemoteServerConfig({ active: false });
|
||||
this.broadcastAuthorizationRequired();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error during auto-refresh check:', error);
|
||||
}
|
||||
}, checkInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止自动刷新定时器
|
||||
*/
|
||||
private stopAutoRefresh() {
|
||||
if (this.autoRefreshTimer) {
|
||||
clearInterval(this.autoRefreshTimer);
|
||||
this.autoRefreshTimer = null;
|
||||
logger.debug('Stopped auto-refresh timer');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询获取凭证
|
||||
* 直接发送 HTTP 请求到远程服务器
|
||||
*/
|
||||
private async pollForCredentials(): Promise<{ code: string; state: string } | null> {
|
||||
if (!this.authRequestState || !this.cachedRemoteUrl) {
|
||||
return null;
|
||||
}
|
||||
|
||||
async handleAuthCallback(callbackUrl: string) {
|
||||
logger.info(`Handling authorization callback: ${callbackUrl}`);
|
||||
try {
|
||||
// 使用缓存的远程服务器 URL
|
||||
const remoteUrl = this.cachedRemoteUrl;
|
||||
const url = new URL(callbackUrl);
|
||||
const params = new URLSearchParams(url.search);
|
||||
|
||||
// 构造请求 URL
|
||||
const url = new URL('/oidc/handoff', remoteUrl);
|
||||
url.searchParams.set('id', this.authRequestState);
|
||||
url.searchParams.set('client', 'desktop');
|
||||
// Get authorization code
|
||||
const code = params.get('code');
|
||||
const state = params.get('state');
|
||||
logger.debug(`Got parameters from callback URL: code=${code}, state=${state}`);
|
||||
|
||||
logger.debug(`Polling for credentials: ${url.toString()}`);
|
||||
// Validate state parameter to prevent CSRF attacks
|
||||
if (state !== this.authRequestState) {
|
||||
logger.error(
|
||||
`Invalid state parameter: expected ${this.authRequestState}, received ${state}`,
|
||||
);
|
||||
throw new Error('Invalid state parameter');
|
||||
}
|
||||
logger.debug('State parameter validation passed');
|
||||
|
||||
// 直接发送 HTTP 请求
|
||||
const response = await fetch(url.toString(), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
// 检查响应状态
|
||||
if (response.status === 404) {
|
||||
// 凭证还未准备好,这是正常情况
|
||||
return null;
|
||||
if (!code) {
|
||||
logger.error('No authorization code received');
|
||||
throw new Error('No authorization code received');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
// Get configuration information
|
||||
const config = await this.remoteServerConfigCtr.getRemoteServerConfig();
|
||||
logger.debug(`Getting remote server configuration: url=${config.remoteServerUrl}`);
|
||||
|
||||
if (config.storageMode === 'selfHost' && !config.remoteServerUrl) {
|
||||
logger.error('Server URL not configured');
|
||||
throw new Error('No server URL configured');
|
||||
}
|
||||
|
||||
// 解析响应数据
|
||||
const data = (await response.json()) as {
|
||||
data: {
|
||||
id: string;
|
||||
payload: { code: string; state: string };
|
||||
};
|
||||
success: boolean;
|
||||
};
|
||||
// Get the previously saved code_verifier
|
||||
const codeVerifier = this.codeVerifier;
|
||||
if (!codeVerifier) {
|
||||
logger.error('Code verifier not found');
|
||||
throw new Error('No code verifier found');
|
||||
}
|
||||
logger.debug('Found code verifier');
|
||||
|
||||
if (data.success && data.data?.payload) {
|
||||
logger.debug('Successfully retrieved credentials from handoff');
|
||||
return {
|
||||
code: data.data.payload.code,
|
||||
state: data.data.payload.state,
|
||||
};
|
||||
// Exchange authorization code for token
|
||||
logger.debug('Starting to exchange authorization code for token');
|
||||
const result = await this.exchangeCodeForToken(code, codeVerifier);
|
||||
|
||||
if (result.success) {
|
||||
logger.info('Authorization successful');
|
||||
// Notify render process of successful authorization
|
||||
this.broadcastAuthorizationSuccessful();
|
||||
} else {
|
||||
logger.warn(`Authorization failed: ${result.error || 'Unknown error'}`);
|
||||
// Notify render process of failed authorization
|
||||
this.broadcastAuthorizationFailed(result.error || 'Unknown error');
|
||||
}
|
||||
|
||||
return null;
|
||||
return result;
|
||||
} catch (error) {
|
||||
logger.debug('Polling attempt failed (this is normal):', error.message);
|
||||
return null;
|
||||
logger.error('Handling authorization callback failed:', error);
|
||||
|
||||
// Notify render process of failed authorization
|
||||
this.broadcastAuthorizationFailed(error.message);
|
||||
|
||||
return { error: error.message, success: false };
|
||||
} finally {
|
||||
// Clear authorization request state
|
||||
logger.debug('Clearing authorization request state');
|
||||
this.authRequestState = null;
|
||||
this.codeVerifier = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh access token
|
||||
*/
|
||||
@ipcClientEvent('refreshAccessToken')
|
||||
async refreshAccessToken() {
|
||||
logger.info('Starting to refresh access token');
|
||||
try {
|
||||
@@ -307,8 +175,6 @@ export default class AuthCtr extends ControllerModule {
|
||||
logger.info('Token refresh successful via AuthCtr call.');
|
||||
// Notify render process that token has been refreshed
|
||||
this.broadcastTokenRefreshed();
|
||||
// Restart auto-refresh timer with new expiration time
|
||||
this.startAutoRefresh();
|
||||
return { success: true };
|
||||
} else {
|
||||
// Throw an error to be caught by the catch block below
|
||||
@@ -322,7 +188,6 @@ export default class AuthCtr extends ControllerModule {
|
||||
|
||||
// Refresh failed, clear tokens and disable remote server
|
||||
logger.warn('Refresh failed, clearing tokens and disabling remote server');
|
||||
this.stopAutoRefresh();
|
||||
await this.remoteServerConfigCtr.clearTokens();
|
||||
await this.remoteServerConfigCtr.setRemoteServerConfig({ active: false });
|
||||
|
||||
@@ -333,15 +198,48 @@ export default class AuthCtr extends ControllerModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register custom protocol handler
|
||||
*/
|
||||
private registerProtocolHandler() {
|
||||
logger.info(`Registering custom protocol handler ${protocolPrefix}://`);
|
||||
app.setAsDefaultProtocolClient(protocolPrefix);
|
||||
|
||||
// Register custom protocol handler
|
||||
if (process.platform === 'darwin') {
|
||||
// Handle open-url event on macOS
|
||||
logger.debug('Registering open-url event handler for macOS');
|
||||
app.on('open-url', (event, url) => {
|
||||
event.preventDefault();
|
||||
logger.info(`Received open-url event: ${url}`);
|
||||
this.handleAuthCallback(url);
|
||||
});
|
||||
} else {
|
||||
// Handle protocol callback via second-instance event on Windows and Linux
|
||||
logger.debug('Registering second-instance event handler for Windows/Linux');
|
||||
app.on('second-instance', async (event, commandLine) => {
|
||||
// Find the URL from command line arguments
|
||||
const url = commandLine.find((arg) => arg.startsWith(`${protocolPrefix}://`));
|
||||
if (url) {
|
||||
logger.info(`Found URL from second-instance command line arguments: ${url}`);
|
||||
const { success } = await this.handleAuthCallback(url);
|
||||
if (success) {
|
||||
this.app.browserManager.getMainWindow().show();
|
||||
}
|
||||
} else {
|
||||
logger.warn('Protocol URL not found in second-instance command line arguments');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`Registered ${protocolPrefix}:// custom protocol handler`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchange authorization code for token
|
||||
*/
|
||||
private async exchangeCodeForToken(code: string, codeVerifier: string) {
|
||||
if (!this.cachedRemoteUrl) {
|
||||
throw new Error('No cached remote URL available for token exchange');
|
||||
}
|
||||
|
||||
const remoteUrl = this.cachedRemoteUrl;
|
||||
const remoteUrl = await this.remoteServerConfigCtr.getRemoteServerUrl();
|
||||
logger.info('Starting to exchange authorization code for token');
|
||||
try {
|
||||
const tokenUrl = new URL('/oidc/token', remoteUrl);
|
||||
@@ -353,7 +251,7 @@ export default class AuthCtr extends ControllerModule {
|
||||
code,
|
||||
code_verifier: codeVerifier,
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: this.constructRedirectUri(remoteUrl),
|
||||
redirect_uri: `${protocolPrefix}://auth/callback`,
|
||||
});
|
||||
|
||||
logger.debug('Sending token exchange request');
|
||||
@@ -374,20 +272,10 @@ export default class AuthCtr extends ControllerModule {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
let data;
|
||||
|
||||
// Parse response
|
||||
try {
|
||||
data = await response.clone().json();
|
||||
} catch {
|
||||
const status = response.status;
|
||||
|
||||
throw new Error(
|
||||
`Parse JSON failed, please check your server, response status: ${status}, detail:\n\n ${await response.text()} `,
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
logger.debug('Successfully received token exchange response');
|
||||
// console.log(data); // Keep original log for debugging, or remove/change to logger.debug as needed
|
||||
|
||||
// Ensure response contains necessary fields
|
||||
if (!data.access_token || !data.refresh_token) {
|
||||
@@ -397,20 +285,13 @@ export default class AuthCtr extends ControllerModule {
|
||||
|
||||
// Save tokens
|
||||
logger.debug('Starting to save exchanged tokens');
|
||||
await this.remoteServerConfigCtr.saveTokens(
|
||||
data.access_token,
|
||||
data.refresh_token,
|
||||
data.expires_in,
|
||||
);
|
||||
await this.remoteServerConfigCtr.saveTokens(data.access_token, data.refresh_token);
|
||||
logger.info('Successfully saved exchanged tokens');
|
||||
|
||||
// Set server to active state
|
||||
logger.debug(`Setting remote server to active state: ${remoteUrl}`);
|
||||
await this.remoteServerConfigCtr.setRemoteServerConfig({ active: true });
|
||||
|
||||
// Start auto-refresh timer
|
||||
this.startAutoRefresh();
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
logger.error('Exchanging authorization code failed:', error);
|
||||
@@ -509,84 +390,4 @@ export default class AuthCtr extends ControllerModule {
|
||||
logger.debug('Generated code challenge (partial): ' + challenge.slice(0, 10) + '...'); // Avoid logging full sensitive info
|
||||
return challenge;
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用启动后初始化
|
||||
*/
|
||||
afterAppReady() {
|
||||
logger.debug('AuthCtr initialized, checking for existing tokens');
|
||||
this.initializeAutoRefresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有定时器
|
||||
*/
|
||||
cleanup() {
|
||||
logger.debug('Cleaning up AuthCtr timers');
|
||||
this.stopPolling();
|
||||
this.stopAutoRefresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化自动刷新功能
|
||||
* 在应用启动时检查是否有有效的 token,如果有就启动自动刷新定时器
|
||||
*/
|
||||
private async initializeAutoRefresh() {
|
||||
try {
|
||||
const config = await this.remoteServerConfigCtr.getRemoteServerConfig();
|
||||
|
||||
// 检查是否配置了远程服务器且处于活动状态
|
||||
if (!config.active || !config.remoteServerUrl) {
|
||||
logger.debug(
|
||||
'Remote server not active or configured, skipping auto-refresh initialization',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否有有效的访问令牌
|
||||
const accessToken = await this.remoteServerConfigCtr.getAccessToken();
|
||||
if (!accessToken) {
|
||||
logger.debug('No access token found, skipping auto-refresh initialization');
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否有过期时间信息
|
||||
const expiresAt = this.remoteServerConfigCtr.getTokenExpiresAt();
|
||||
if (!expiresAt) {
|
||||
logger.debug('No token expiration time found, skipping auto-refresh initialization');
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查 token 是否已经过期
|
||||
const currentTime = Date.now();
|
||||
if (currentTime >= expiresAt) {
|
||||
logger.info('Token has expired, attempting to refresh it');
|
||||
|
||||
// 尝试刷新 token
|
||||
const refreshResult = await this.remoteServerConfigCtr.refreshAccessToken();
|
||||
if (refreshResult.success) {
|
||||
logger.info('Token refresh successful during initialization');
|
||||
this.broadcastTokenRefreshed();
|
||||
// 重新启动自动刷新定时器
|
||||
this.startAutoRefresh();
|
||||
return;
|
||||
} else {
|
||||
logger.error(`Token refresh failed during initialization: ${refreshResult.error}`);
|
||||
// 只有在刷新失败时才清除 token 并要求重新授权
|
||||
await this.remoteServerConfigCtr.clearTokens();
|
||||
await this.remoteServerConfigCtr.setRemoteServerConfig({ active: false });
|
||||
this.broadcastAuthorizationRequired();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 启动自动刷新定时器
|
||||
logger.info(
|
||||
`Token is valid, starting auto-refresh timer. Token expires at: ${new Date(expiresAt).toISOString()}`,
|
||||
);
|
||||
this.startAutoRefresh();
|
||||
} catch (error) {
|
||||
logger.error('Error during auto-refresh initialization:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { IpcClientEventSender } from '@/types/ipcClientEvent';
|
||||
import { ControllerModule, ipcClientEvent, shortcut } from './index';
|
||||
|
||||
export default class BrowserWindowsCtr extends ControllerModule {
|
||||
@shortcut('showApp')
|
||||
@shortcut('toggleMainWindow')
|
||||
async toggleMainWindow() {
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
mainWindow.toggleVisible();
|
||||
|
||||
@@ -1,172 +0,0 @@
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
import { merge } from 'lodash';
|
||||
import { isEqual } from 'lodash-es';
|
||||
|
||||
import { defaultProxySettings } from '@/const/store';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import {
|
||||
ProxyConfigValidator,
|
||||
ProxyConnectionTester,
|
||||
ProxyDispatcherManager,
|
||||
ProxyTestResult,
|
||||
} from '../modules/networkProxy';
|
||||
import { ControllerModule, ipcClientEvent } from './index';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('controllers:NetworkProxyCtr');
|
||||
|
||||
/**
|
||||
* 网络代理控制器
|
||||
* 处理桌面应用的网络代理相关功能
|
||||
*/
|
||||
export default class NetworkProxyCtr extends ControllerModule {
|
||||
/**
|
||||
* 获取代理设置
|
||||
*/
|
||||
@ipcClientEvent('getProxySettings')
|
||||
async getDesktopSettings(): Promise<NetworkProxySettings> {
|
||||
try {
|
||||
const settings = this.app.storeManager.get(
|
||||
'networkProxy',
|
||||
defaultProxySettings,
|
||||
) as NetworkProxySettings;
|
||||
logger.debug('Retrieved proxy settings:', {
|
||||
enableProxy: settings.enableProxy,
|
||||
proxyType: settings.proxyType,
|
||||
});
|
||||
return settings;
|
||||
} catch (error) {
|
||||
logger.error('Failed to get proxy settings:', error);
|
||||
return defaultProxySettings;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置代理配置
|
||||
*/
|
||||
@ipcClientEvent('setProxySettings')
|
||||
async setProxySettings(config: NetworkProxySettings): Promise<void> {
|
||||
try {
|
||||
// 验证配置
|
||||
const validation = ProxyConfigValidator.validate(config);
|
||||
if (!validation.isValid) {
|
||||
const errorMessage = `Invalid proxy configuration: ${validation.errors.join(', ')}`;
|
||||
logger.error(errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
// 获取当前配置
|
||||
const currentConfig = this.app.storeManager.get(
|
||||
'networkProxy',
|
||||
defaultProxySettings,
|
||||
) as NetworkProxySettings;
|
||||
|
||||
// 检查是否有变化
|
||||
if (isEqual(currentConfig, config)) {
|
||||
logger.debug('Proxy settings unchanged, skipping update');
|
||||
return;
|
||||
}
|
||||
|
||||
// 合并配置
|
||||
const newConfig = merge({}, currentConfig, config);
|
||||
|
||||
// 应用代理设置
|
||||
await ProxyDispatcherManager.applyProxySettings(newConfig);
|
||||
|
||||
// 保存到存储
|
||||
this.app.storeManager.set('networkProxy', newConfig);
|
||||
|
||||
logger.info('Proxy settings updated successfully', {
|
||||
enableProxy: newConfig.enableProxy,
|
||||
proxyPort: newConfig.proxyPort,
|
||||
proxyServer: newConfig.proxyServer,
|
||||
proxyType: newConfig.proxyType,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to update proxy settings:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试代理连接
|
||||
*/
|
||||
@ipcClientEvent('testProxyConnection')
|
||||
async testProxyConnection(url: string): Promise<{ message?: string; success: boolean }> {
|
||||
try {
|
||||
const result = await ProxyConnectionTester.testConnection(url);
|
||||
|
||||
if (result.success) {
|
||||
return { success: true };
|
||||
} else {
|
||||
throw new Error(result.message || 'Connection test failed');
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
logger.error('Proxy connection test failed:', errorMessage);
|
||||
throw new Error(`Connection failed: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试指定代理配置
|
||||
*/
|
||||
@ipcClientEvent('testProxyConfig')
|
||||
async testProxyConfig({
|
||||
config,
|
||||
testUrl,
|
||||
}: {
|
||||
config: NetworkProxySettings;
|
||||
testUrl?: string;
|
||||
}): Promise<ProxyTestResult> {
|
||||
try {
|
||||
return await ProxyConnectionTester.testProxyConfig(config, testUrl);
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
logger.error('Proxy config test failed:', errorMessage);
|
||||
return {
|
||||
message: `Proxy config test failed: ${errorMessage}`,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用初始代理设置
|
||||
*/
|
||||
async beforeAppReady(): Promise<void> {
|
||||
try {
|
||||
// 获取存储的代理设置
|
||||
const networkProxy = this.app.storeManager.get(
|
||||
'networkProxy',
|
||||
defaultProxySettings,
|
||||
) as NetworkProxySettings;
|
||||
|
||||
// 验证配置
|
||||
const validation = ProxyConfigValidator.validate(networkProxy);
|
||||
if (!validation.isValid) {
|
||||
logger.warn('Invalid stored proxy configuration, using defaults:', validation.errors);
|
||||
await ProxyDispatcherManager.applyProxySettings(defaultProxySettings);
|
||||
return;
|
||||
}
|
||||
|
||||
// 应用代理设置
|
||||
await ProxyDispatcherManager.applyProxySettings(networkProxy);
|
||||
|
||||
logger.info('Initial proxy settings applied successfully', {
|
||||
enableProxy: networkProxy.enableProxy,
|
||||
proxyType: networkProxy.proxyType,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to apply initial proxy settings:', error);
|
||||
// 出错时使用默认设置
|
||||
try {
|
||||
await ProxyDispatcherManager.applyProxySettings(defaultProxySettings);
|
||||
logger.info('Fallback to default proxy settings');
|
||||
} catch (fallbackError) {
|
||||
logger.error('Failed to apply fallback proxy settings:', fallbackError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
import {
|
||||
DesktopNotificationResult,
|
||||
ShowDesktopNotificationParams,
|
||||
} from '@lobechat/electron-client-ipc';
|
||||
import { Notification, app } from 'electron';
|
||||
import { macOS, windows } from 'electron-is';
|
||||
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { ControllerModule, ipcClientEvent } from './index';
|
||||
|
||||
const logger = createLogger('controllers:NotificationCtr');
|
||||
|
||||
export default class NotificationCtr extends ControllerModule {
|
||||
/**
|
||||
* 在应用准备就绪后设置桌面通知
|
||||
*/
|
||||
afterAppReady() {
|
||||
this.setupNotifications();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置桌面通知权限和配置
|
||||
*/
|
||||
private setupNotifications() {
|
||||
logger.debug('Setting up desktop notifications');
|
||||
|
||||
try {
|
||||
// 检查通知支持
|
||||
if (!Notification.isSupported()) {
|
||||
logger.warn('Desktop notifications are not supported on this platform');
|
||||
return;
|
||||
}
|
||||
|
||||
// 在 macOS 上,我们可能需要显式请求通知权限
|
||||
if (macOS()) {
|
||||
logger.debug('macOS detected, notification permissions should be handled by system');
|
||||
}
|
||||
|
||||
// 在 Windows 上设置应用用户模型 ID
|
||||
if (windows()) {
|
||||
app.setAppUserModelId('com.lobehub.chat');
|
||||
logger.debug('Set Windows App User Model ID for notifications');
|
||||
}
|
||||
|
||||
logger.info('Desktop notifications setup completed');
|
||||
} catch (error) {
|
||||
logger.error('Failed to setup desktop notifications:', error);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 显示系统桌面通知(仅当窗口隐藏时)
|
||||
*/
|
||||
@ipcClientEvent('showDesktopNotification')
|
||||
async showDesktopNotification(
|
||||
params: ShowDesktopNotificationParams,
|
||||
): Promise<DesktopNotificationResult> {
|
||||
logger.debug('收到桌面通知请求:', params);
|
||||
|
||||
try {
|
||||
// 检查通知支持
|
||||
if (!Notification.isSupported()) {
|
||||
logger.warn('系统不支持桌面通知');
|
||||
return { error: 'Desktop notifications not supported', success: false };
|
||||
}
|
||||
|
||||
// 检查窗口是否隐藏
|
||||
const isWindowHidden = this.isMainWindowHidden();
|
||||
|
||||
if (!isWindowHidden) {
|
||||
logger.debug('主窗口可见,跳过桌面通知');
|
||||
return { reason: 'Window is visible', skipped: true, success: true };
|
||||
}
|
||||
|
||||
logger.info('窗口已隐藏,显示桌面通知:', params.title);
|
||||
|
||||
const notification = new Notification({
|
||||
body: params.body,
|
||||
// 添加更多配置以确保通知能正常显示
|
||||
hasReply: false,
|
||||
silent: params.silent || false,
|
||||
timeoutType: 'default',
|
||||
title: params.title,
|
||||
urgency: 'normal',
|
||||
});
|
||||
|
||||
// 添加更多事件监听来调试
|
||||
notification.on('show', () => {
|
||||
logger.info('通知已显示');
|
||||
});
|
||||
|
||||
notification.on('click', () => {
|
||||
logger.debug('用户点击通知,显示主窗口');
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
mainWindow.show();
|
||||
mainWindow.browserWindow.focus();
|
||||
});
|
||||
|
||||
notification.on('close', () => {
|
||||
logger.debug('通知已关闭');
|
||||
});
|
||||
|
||||
notification.on('failed', (error) => {
|
||||
logger.error('通知显示失败:', error);
|
||||
});
|
||||
|
||||
// 使用 Promise 来确保通知显示
|
||||
return new Promise((resolve) => {
|
||||
notification.show();
|
||||
|
||||
// 给通知一些时间来显示,然后检查结果
|
||||
setTimeout(() => {
|
||||
logger.info('通知显示调用完成');
|
||||
resolve({ success: true });
|
||||
}, 100);
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('显示桌面通知失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查主窗口是否隐藏
|
||||
*/
|
||||
@ipcClientEvent('isMainWindowHidden')
|
||||
isMainWindowHidden(): boolean {
|
||||
try {
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
const browserWindow = mainWindow.browserWindow;
|
||||
|
||||
// 如果窗口被销毁,认为是隐藏的
|
||||
if (browserWindow.isDestroyed()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 检查窗口是否可见和聚焦
|
||||
const isVisible = browserWindow.isVisible();
|
||||
const isFocused = browserWindow.isFocused();
|
||||
const isMinimized = browserWindow.isMinimized();
|
||||
|
||||
logger.debug('窗口状态检查:', { isFocused, isMinimized, isVisible });
|
||||
|
||||
// 窗口隐藏的条件:不可见或最小化或失去焦点
|
||||
return !isVisible || isMinimized || !isFocused;
|
||||
} catch (error) {
|
||||
logger.error('检查窗口状态失败:', error);
|
||||
return true; // 发生错误时认为窗口隐藏,确保通知能显示
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,12 +79,6 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
private encryptedAccessToken?: string;
|
||||
private encryptedRefreshToken?: string;
|
||||
|
||||
/**
|
||||
* Token expiration time (timestamp in milliseconds)
|
||||
* Used for automatic token refresh
|
||||
*/
|
||||
private tokenExpiresAt?: number;
|
||||
|
||||
/**
|
||||
* Promise representing the ongoing token refresh operation.
|
||||
* Used to prevent concurrent refreshes and allow callers to wait.
|
||||
@@ -95,19 +89,10 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
* Encrypt and store tokens
|
||||
* @param accessToken Access token
|
||||
* @param refreshToken Refresh token
|
||||
* @param expiresIn Token expiration time in seconds (optional)
|
||||
*/
|
||||
async saveTokens(accessToken: string, refreshToken: string, expiresIn?: number) {
|
||||
async saveTokens(accessToken: string, refreshToken: string) {
|
||||
logger.info('Saving encrypted tokens');
|
||||
|
||||
// Calculate expiration time if provided
|
||||
if (expiresIn) {
|
||||
this.tokenExpiresAt = Date.now() + expiresIn * 1000;
|
||||
logger.debug(`Token expires at: ${new Date(this.tokenExpiresAt).toISOString()}`);
|
||||
} else {
|
||||
this.tokenExpiresAt = undefined;
|
||||
}
|
||||
|
||||
// If platform doesn't support secure storage, store raw tokens
|
||||
if (!safeStorage.isEncryptionAvailable()) {
|
||||
logger.warn('Safe storage not available, storing tokens unencrypted');
|
||||
@@ -116,7 +101,6 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
// Persist unencrypted tokens (consider security implications)
|
||||
this.app.storeManager.set(this.encryptedTokensKey, {
|
||||
accessToken: this.encryptedAccessToken,
|
||||
expiresAt: this.tokenExpiresAt,
|
||||
refreshToken: this.encryptedRefreshToken,
|
||||
});
|
||||
return;
|
||||
@@ -136,7 +120,6 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
logger.debug(`Persisting encrypted tokens to store key: ${this.encryptedTokensKey}`);
|
||||
this.app.storeManager.set(this.encryptedTokensKey, {
|
||||
accessToken: this.encryptedAccessToken,
|
||||
expiresAt: this.tokenExpiresAt,
|
||||
refreshToken: this.encryptedRefreshToken,
|
||||
});
|
||||
}
|
||||
@@ -216,40 +199,17 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
logger.info('Clearing access and refresh tokens');
|
||||
this.encryptedAccessToken = undefined;
|
||||
this.encryptedRefreshToken = undefined;
|
||||
this.tokenExpiresAt = undefined;
|
||||
// Also clear from persistent storage
|
||||
logger.debug(`Deleting tokens from store key: ${this.encryptedTokensKey}`);
|
||||
this.app.storeManager.delete(this.encryptedTokensKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get token expiration time
|
||||
*/
|
||||
getTokenExpiresAt(): number | undefined {
|
||||
return this.tokenExpiresAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if token is expired or will expire soon
|
||||
* @param bufferTimeMs Buffer time in milliseconds (default 5 minutes)
|
||||
* @returns true if token is expired or will expire soon
|
||||
*/
|
||||
isTokenExpiringSoon(bufferTimeMs: number = 5 * 60 * 1000): boolean {
|
||||
if (!this.tokenExpiresAt) {
|
||||
return false; // No expiration time available
|
||||
}
|
||||
|
||||
const currentTime = Date.now();
|
||||
const bufferTime = this.tokenExpiresAt - bufferTimeMs;
|
||||
|
||||
return currentTime >= bufferTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新访问令牌
|
||||
* 使用存储的刷新令牌获取新的访问令牌
|
||||
* Handles concurrent requests by returning the existing refresh promise if one is in progress.
|
||||
*/
|
||||
@ipcClientEvent('refreshAccessToken')
|
||||
async refreshAccessToken(): Promise<{ error?: string; success: boolean }> {
|
||||
// If a refresh is already in progress, return the existing promise
|
||||
if (this.refreshPromise) {
|
||||
@@ -330,7 +290,7 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
|
||||
// 保存新令牌
|
||||
logger.info('Token refresh successful, saving new tokens.');
|
||||
await this.saveTokens(data.access_token, data.refresh_token, data.expires_in);
|
||||
await this.saveTokens(data.access_token, data.refresh_token);
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
@@ -356,13 +316,6 @@ export default class RemoteServerConfigCtr extends ControllerModule {
|
||||
logger.info('Successfully loaded tokens from store into memory.');
|
||||
this.encryptedAccessToken = storedTokens.accessToken;
|
||||
this.encryptedRefreshToken = storedTokens.refreshToken;
|
||||
this.tokenExpiresAt = storedTokens.expiresAt;
|
||||
|
||||
if (this.tokenExpiresAt) {
|
||||
logger.debug(
|
||||
`Loaded token expiration time: ${new Date(this.tokenExpiresAt).toISOString()}`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
logger.debug('No valid tokens found in store.');
|
||||
}
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
import {
|
||||
ProxyTRPCRequestParams,
|
||||
ProxyTRPCRequestResult,
|
||||
ProxyTRPCStreamRequestParams,
|
||||
} from '@lobechat/electron-client-ipc';
|
||||
import { IpcMainEvent, WebContents, ipcMain } from 'electron';
|
||||
import { HttpProxyAgent } from 'http-proxy-agent';
|
||||
import { HttpsProxyAgent } from 'https-proxy-agent';
|
||||
} from '@lobechat/electron-client-ipc/src/types/proxyTRPCRequest';
|
||||
import { Buffer } from 'node:buffer';
|
||||
import http, { IncomingMessage, OutgoingHttpHeaders } from 'node:http';
|
||||
import https from 'node:https';
|
||||
import { URL } from 'node:url';
|
||||
|
||||
import { defaultProxySettings } from '@/const/store';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import RemoteServerConfigCtr from './RemoteServerConfigCtr';
|
||||
@@ -46,137 +41,6 @@ export default class RemoteServerSyncCtr extends ControllerModule {
|
||||
afterAppReady() {
|
||||
logger.info('RemoteServerSyncCtr initialized (IPC based)');
|
||||
// No need to register protocol handler anymore
|
||||
ipcMain.on('stream:start', this.handleStreamRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理流式请求的 IPC 调用
|
||||
*/
|
||||
private handleStreamRequest = async (event: IpcMainEvent, args: ProxyTRPCStreamRequestParams) => {
|
||||
const { requestId } = args;
|
||||
const logPrefix = `[StreamProxy ${args.method} ${args.urlPath}][${requestId}]`;
|
||||
logger.debug(`${logPrefix} Received stream:start IPC call`);
|
||||
|
||||
try {
|
||||
const config = await this.remoteServerConfigCtr.getRemoteServerConfig();
|
||||
if (!config.active || (config.storageMode === 'selfHost' && !config.remoteServerUrl)) {
|
||||
logger.warn(`${logPrefix} Remote server sync not active or configured.`);
|
||||
event.sender.send(
|
||||
`stream:error:${requestId}`,
|
||||
new Error('Remote server sync not active or configured'),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const remoteServerUrl = await this.remoteServerConfigCtr.getRemoteServerUrl();
|
||||
const token = await this.remoteServerConfigCtr.getAccessToken();
|
||||
|
||||
if (!token) {
|
||||
// 401 Unauthorized
|
||||
event.sender.send(`stream:response:${requestId}`, {
|
||||
headers: {},
|
||||
status: 401,
|
||||
statusText: 'Authentication required, missing token',
|
||||
});
|
||||
event.sender.send(`stream:end:${requestId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用新的流式转发方法
|
||||
await this.forwardStreamRequest(event.sender, {
|
||||
...args,
|
||||
accessToken: token,
|
||||
remoteServerUrl,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(`${logPrefix} Unhandled error processing stream request:`, error);
|
||||
event.sender.send(
|
||||
`stream:error:${requestId}`,
|
||||
error instanceof Error ? error : new Error('Unknown error'),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 执行实际的流式请求转发
|
||||
*/
|
||||
private async forwardStreamRequest(
|
||||
sender: WebContents,
|
||||
args: ProxyTRPCStreamRequestParams & { accessToken: string; remoteServerUrl: string },
|
||||
) {
|
||||
const {
|
||||
urlPath,
|
||||
method,
|
||||
headers: originalHeaders,
|
||||
body: requestBody,
|
||||
accessToken,
|
||||
remoteServerUrl,
|
||||
requestId,
|
||||
} = args;
|
||||
const targetUrl = new URL(urlPath, remoteServerUrl);
|
||||
const logPrefix = `[ForwardStream ${method} ${targetUrl.pathname}][${requestId}]`;
|
||||
|
||||
const { requestOptions, requester } = this.createRequester({
|
||||
accessToken,
|
||||
headers: originalHeaders,
|
||||
method,
|
||||
url: targetUrl,
|
||||
});
|
||||
|
||||
const clientReq = requester.request(requestOptions, (clientRes: IncomingMessage) => {
|
||||
logger.debug(`${logPrefix} Received response with status ${clientRes.statusCode}`);
|
||||
|
||||
// 添加调试信息
|
||||
logger.debug(`${logPrefix} Response details:`, {
|
||||
headers: clientRes.headers,
|
||||
statusCode: clientRes.statusCode,
|
||||
statusMessage: clientRes.statusMessage,
|
||||
});
|
||||
|
||||
// 1. 立刻发送响应头和状态码
|
||||
const responseData = {
|
||||
headers: clientRes.headers || {},
|
||||
status: clientRes.statusCode || 500,
|
||||
statusText: clientRes.statusMessage || 'Unknown Status',
|
||||
};
|
||||
|
||||
logger.debug(`${logPrefix} Sending response data:`, responseData);
|
||||
sender.send(`stream:response:${requestId}`, responseData);
|
||||
|
||||
// 2. 监听数据块并转发
|
||||
clientRes.on('data', (chunk: Buffer) => {
|
||||
if (sender.isDestroyed()) return;
|
||||
logger.debug(`${logPrefix} Received data chunk, size: ${chunk.length}. Forwarding...`);
|
||||
sender.send(`stream:data:${requestId}`, chunk);
|
||||
});
|
||||
|
||||
// 3. 监听结束信号并转发
|
||||
clientRes.on('end', () => {
|
||||
logger.debug(`${logPrefix} Stream ended. Forwarding end signal...`);
|
||||
if (sender.isDestroyed()) return;
|
||||
sender.send(`stream:end:${requestId}`);
|
||||
});
|
||||
|
||||
// 4. 监听响应流错误并转发
|
||||
clientRes.on('error', (error) => {
|
||||
logger.error(`${logPrefix} Error reading response stream:`, error);
|
||||
if (sender.isDestroyed()) return;
|
||||
sender.send(`stream:error:${requestId}`, error);
|
||||
});
|
||||
});
|
||||
|
||||
// 5. 监听请求本身的错误(如 DNS 解析失败)
|
||||
clientReq.on('error', (error) => {
|
||||
logger.error(`${logPrefix} Error forwarding request:`, error);
|
||||
if (sender.isDestroyed()) return;
|
||||
sender.send(`stream:error:${requestId}`, error);
|
||||
});
|
||||
|
||||
if (requestBody) {
|
||||
clientReq.write(Buffer.from(requestBody));
|
||||
}
|
||||
|
||||
clientReq.end();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -221,12 +85,28 @@ export default class RemoteServerSyncCtr extends ControllerModule {
|
||||
|
||||
// 1. Determine target URL and prepare request options
|
||||
const targetUrl = new URL(urlPath, remoteServerUrl); // Combine base URL and path
|
||||
const { requestOptions, requester } = this.createRequester({
|
||||
accessToken,
|
||||
headers: originalHeaders,
|
||||
method,
|
||||
url: targetUrl,
|
||||
});
|
||||
|
||||
// Prepare headers, cloning and adding Authorization
|
||||
const requestHeaders: OutgoingHttpHeaders = { ...originalHeaders }; // Use OutgoingHttpHeaders
|
||||
requestHeaders['Authorization'] = `Bearer ${accessToken}`;
|
||||
|
||||
// Let node handle Host, Content-Length etc. Remove potentially problematic headers
|
||||
delete requestHeaders['host'];
|
||||
delete requestHeaders['connection']; // Often causes issues
|
||||
// delete requestHeaders['content-length']; // Let node handle it based on body
|
||||
|
||||
const requestOptions: https.RequestOptions | http.RequestOptions = {
|
||||
// Use union type
|
||||
headers: requestHeaders,
|
||||
hostname: targetUrl.hostname,
|
||||
method: method,
|
||||
path: targetUrl.pathname + targetUrl.search,
|
||||
port: targetUrl.port || (targetUrl.protocol === 'https:' ? 443 : 80),
|
||||
protocol: targetUrl.protocol,
|
||||
// agent: false, // Consider for keep-alive issues if they arise
|
||||
};
|
||||
|
||||
const requester = targetUrl.protocol === 'https:' ? https : http;
|
||||
|
||||
// 2. Make the request and capture response
|
||||
return new Promise((resolve) => {
|
||||
@@ -296,51 +176,6 @@ export default class RemoteServerSyncCtr extends ControllerModule {
|
||||
});
|
||||
}
|
||||
|
||||
private createRequester({
|
||||
headers,
|
||||
accessToken,
|
||||
method,
|
||||
url,
|
||||
}: {
|
||||
accessToken: string;
|
||||
headers: Record<string, string>;
|
||||
method: string;
|
||||
url: URL;
|
||||
}) {
|
||||
// Prepare headers, cloning and adding Oidc-Auth
|
||||
const requestHeaders: OutgoingHttpHeaders = { ...headers }; // Use OutgoingHttpHeaders
|
||||
requestHeaders['Oidc-Auth'] = accessToken;
|
||||
|
||||
// Let node handle Host, Content-Length etc. Remove potentially problematic headers
|
||||
delete requestHeaders['host'];
|
||||
delete requestHeaders['connection']; // Often causes issues
|
||||
// delete requestHeaders['content-length']; // Let node handle it based on body
|
||||
|
||||
// 读取代理配置
|
||||
const proxyConfig = this.app.storeManager.get('networkProxy', defaultProxySettings);
|
||||
|
||||
let agent;
|
||||
if (proxyConfig?.enableProxy && proxyConfig.proxyServer) {
|
||||
const proxyUrl = `${proxyConfig.proxyType}://${proxyConfig.proxyServer}${proxyConfig.proxyPort ? `:${proxyConfig.proxyPort}` : ''}`;
|
||||
agent =
|
||||
url.protocol === 'https:' ? new HttpsProxyAgent(proxyUrl) : new HttpProxyAgent(proxyUrl);
|
||||
}
|
||||
|
||||
const requestOptions: https.RequestOptions | http.RequestOptions = {
|
||||
agent,
|
||||
// Use union type
|
||||
headers: requestHeaders,
|
||||
hostname: url.hostname,
|
||||
method: method,
|
||||
path: url.pathname + url.search,
|
||||
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
||||
protocol: url.protocol,
|
||||
};
|
||||
|
||||
const requester = url.protocol === 'https:' ? https : http;
|
||||
return { requestOptions, requester };
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the 'proxy-trpc-request' IPC call from the renderer process.
|
||||
* This method should be invoked by the ipcMain.handle setup in your main process entry point.
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { ShortcutUpdateResult } from '@/core/ui/ShortcutManager';
|
||||
|
||||
import { ControllerModule, ipcClientEvent } from '.';
|
||||
|
||||
export default class ShortcutController extends ControllerModule {
|
||||
@@ -15,13 +13,7 @@ export default class ShortcutController extends ControllerModule {
|
||||
* 更新单个快捷键配置
|
||||
*/
|
||||
@ipcClientEvent('updateShortcutConfig')
|
||||
updateShortcutConfig({
|
||||
id,
|
||||
accelerator,
|
||||
}: {
|
||||
accelerator: string;
|
||||
id: string;
|
||||
}): ShortcutUpdateResult {
|
||||
updateShortcutConfig(id: string, accelerator: string): boolean {
|
||||
return this.app.shortcutManager.updateShortcutConfig(id, accelerator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,15 @@
|
||||
import { ElectronAppState, ThemeMode } from '@lobechat/electron-client-ipc';
|
||||
import { app, nativeTheme, shell, systemPreferences } from 'electron';
|
||||
import { app, shell, systemPreferences } from 'electron';
|
||||
import { macOS } from 'electron-is';
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
import { DB_SCHEMA_HASH_FILENAME, LOCAL_DATABASE_DIR, userDataDir } from '@/const/dir';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { ControllerModule, ipcClientEvent, ipcServerEvent } from './index';
|
||||
|
||||
const logger = createLogger('controllers:SystemCtr');
|
||||
|
||||
export default class SystemController extends ControllerModule {
|
||||
private systemThemeListenerInitialized = false;
|
||||
|
||||
/**
|
||||
* Initialize system theme listener when app is ready
|
||||
*/
|
||||
afterAppReady() {
|
||||
this.initializeSystemThemeListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the 'getDesktopAppState' IPC request.
|
||||
* Gathers essential application and system information.
|
||||
@@ -38,7 +26,6 @@ export default class SystemController extends ControllerModule {
|
||||
isMac: platform === 'darwin',
|
||||
isWindows: platform === 'win32',
|
||||
platform: platform as 'darwin' | 'win32' | 'linux',
|
||||
systemAppearance: nativeTheme.shouldUseDarkColors ? 'dark' : 'light',
|
||||
userPath: {
|
||||
// User Paths (ensure keys match UserPathData / DesktopAppState interface)
|
||||
desktop: app.getPath('desktop'),
|
||||
@@ -83,11 +70,7 @@ export default class SystemController extends ControllerModule {
|
||||
|
||||
@ipcClientEvent('updateThemeMode')
|
||||
async updateThemeModeHandler(themeMode: ThemeMode) {
|
||||
this.app.storeManager.set('themeMode', themeMode);
|
||||
this.app.browserManager.broadcastToAllWindows('themeChanged', { themeMode });
|
||||
|
||||
// Apply visual effects to all browser windows when theme mode changes
|
||||
this.app.browserManager.handleAppThemeChange();
|
||||
}
|
||||
|
||||
@ipcServerEvent('getDatabasePath')
|
||||
@@ -117,37 +100,4 @@ export default class SystemController extends ControllerModule {
|
||||
private get DB_SCHEMA_HASH_PATH() {
|
||||
return join(this.app.appStoragePath, DB_SCHEMA_HASH_FILENAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize system theme listener to monitor OS theme changes
|
||||
*/
|
||||
private initializeSystemThemeListener() {
|
||||
if (this.systemThemeListenerInitialized) {
|
||||
logger.debug('System theme listener already initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info('Initializing system theme listener');
|
||||
|
||||
// Get initial system theme
|
||||
const initialDarkMode = nativeTheme.shouldUseDarkColors;
|
||||
const initialSystemTheme: ThemeMode = initialDarkMode ? 'dark' : 'light';
|
||||
logger.info(`Initial system theme: ${initialSystemTheme}`);
|
||||
|
||||
// Listen for system theme changes
|
||||
nativeTheme.on('updated', () => {
|
||||
const isDarkMode = nativeTheme.shouldUseDarkColors;
|
||||
const systemTheme: ThemeMode = isDarkMode ? 'dark' : 'light';
|
||||
|
||||
logger.info(`System theme changed to: ${systemTheme}`);
|
||||
|
||||
// Broadcast system theme change to all renderer processes
|
||||
this.app.browserManager.broadcastToAllWindows('systemThemeChanged', {
|
||||
themeMode: systemTheme,
|
||||
});
|
||||
});
|
||||
|
||||
this.systemThemeListenerInitialized = true;
|
||||
logger.info('System theme listener initialized successfully');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,16 @@ import {
|
||||
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { ControllerModule, ipcClientEvent } from './index';
|
||||
import { ControllerModule, ipcClientEvent, shortcut } from './index';
|
||||
|
||||
// 创建日志记录器
|
||||
const logger = createLogger('controllers:TrayMenuCtr');
|
||||
|
||||
export default class TrayMenuCtr extends ControllerModule {
|
||||
/**
|
||||
* 使用快捷键切换窗口可见性
|
||||
*/
|
||||
@shortcut('toggleMainWindow')
|
||||
async toggleMainWindow() {
|
||||
logger.debug('通过快捷键切换主窗口可见性');
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
@@ -43,7 +47,7 @@ export default class TrayMenuCtr extends ControllerModule {
|
||||
|
||||
return {
|
||||
error: '托盘通知仅在 Windows 平台支持',
|
||||
success: false,
|
||||
success: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -67,7 +71,7 @@ export default class TrayMenuCtr extends ControllerModule {
|
||||
logger.error('更新托盘图标失败:', error);
|
||||
return {
|
||||
error: String(error),
|
||||
success: false,
|
||||
success: false
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -75,7 +79,7 @@ export default class TrayMenuCtr extends ControllerModule {
|
||||
|
||||
return {
|
||||
error: '托盘功能仅在 Windows 平台支持',
|
||||
success: false,
|
||||
success: false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -99,7 +103,7 @@ export default class TrayMenuCtr extends ControllerModule {
|
||||
|
||||
return {
|
||||
error: '托盘功能仅在 Windows 平台支持',
|
||||
success: false,
|
||||
success: false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import { UploadFileParams } from '@lobechat/electron-client-ipc';
|
||||
import { CreateFileParams } from '@lobechat/electron-server-ipc';
|
||||
|
||||
import FileService from '@/services/fileSrv';
|
||||
|
||||
import { ControllerModule, ipcClientEvent, ipcServerEvent } from './index';
|
||||
|
||||
interface UploadFileParams {
|
||||
content: ArrayBuffer;
|
||||
filename: string;
|
||||
hash: string;
|
||||
path: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export default class UploadFileCtr extends ControllerModule {
|
||||
private get fileService() {
|
||||
return this.app.getService(FileService);
|
||||
@@ -22,18 +27,8 @@ export default class UploadFileCtr extends ControllerModule {
|
||||
return this.fileService.getFilePath(id);
|
||||
}
|
||||
|
||||
@ipcServerEvent('getFileHTTPURL')
|
||||
async getFileHTTPURL(path: string) {
|
||||
return this.fileService.getFileHTTPURL(path);
|
||||
}
|
||||
|
||||
@ipcServerEvent('deleteFiles')
|
||||
async deleteFiles(paths: string[]) {
|
||||
return this.fileService.deleteFiles(paths);
|
||||
}
|
||||
|
||||
@ipcServerEvent('createFile')
|
||||
async createFile(params: CreateFileParams) {
|
||||
return this.fileService.uploadFile(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,420 +0,0 @@
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { App } from '@/core/App';
|
||||
|
||||
import NetworkProxyCtr from '../NetworkProxyCtr';
|
||||
|
||||
// 模拟 logger
|
||||
vi.mock('@/utils/logger', () => ({
|
||||
createLogger: () => ({
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
// 模拟 undici - 使用 vi.fn() 直接在 Mock 中创建
|
||||
vi.mock('undici', () => ({
|
||||
fetch: vi.fn(),
|
||||
getGlobalDispatcher: vi.fn(),
|
||||
setGlobalDispatcher: vi.fn(),
|
||||
Agent: vi.fn(),
|
||||
ProxyAgent: vi.fn(),
|
||||
}));
|
||||
|
||||
// 模拟 defaultProxySettings
|
||||
vi.mock('@/const/store', () => ({
|
||||
defaultProxySettings: {
|
||||
enableProxy: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
proxyPort: '',
|
||||
proxyRequireAuth: false,
|
||||
proxyServer: '',
|
||||
proxyType: 'http',
|
||||
},
|
||||
}));
|
||||
|
||||
// 模拟 App 及其依赖项
|
||||
const mockStoreManager = {
|
||||
get: vi.fn(),
|
||||
set: vi.fn(),
|
||||
};
|
||||
|
||||
const mockApp = {
|
||||
storeManager: mockStoreManager,
|
||||
} as unknown as App;
|
||||
|
||||
describe('NetworkProxyCtr', () => {
|
||||
let networkProxyCtr: NetworkProxyCtr;
|
||||
|
||||
// 动态导入 undici 的 Mock
|
||||
let mockUndici: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// 动态导入 undici Mock
|
||||
mockUndici = await import('undici');
|
||||
|
||||
networkProxyCtr = new NetworkProxyCtr(mockApp);
|
||||
|
||||
// 设置 undici mocks 的默认返回值
|
||||
vi.mocked(mockUndici.Agent).mockReturnValue({});
|
||||
vi.mocked(mockUndici.ProxyAgent).mockReturnValue({});
|
||||
vi.mocked(mockUndici.getGlobalDispatcher).mockReturnValue({
|
||||
destroy: vi.fn().mockResolvedValue(undefined),
|
||||
});
|
||||
vi.mocked(mockUndici.setGlobalDispatcher).mockReturnValue(undefined);
|
||||
|
||||
// 设置 fetch mock 的默认返回值
|
||||
vi.mocked(mockUndici.fetch).mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
});
|
||||
});
|
||||
|
||||
describe('ProxyConfigValidator', () => {
|
||||
const validConfig: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
it('should validate enabled proxy config with all required fields', () => {
|
||||
// 通过测试公共方法来间接测试验证逻辑
|
||||
expect(() => networkProxyCtr.setProxySettings(validConfig)).not.toThrow();
|
||||
});
|
||||
|
||||
it('should validate disabled proxy config', () => {
|
||||
const disabledConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
enableProxy: false,
|
||||
};
|
||||
|
||||
expect(() => networkProxyCtr.setProxySettings(disabledConfig)).not.toThrow();
|
||||
});
|
||||
|
||||
it('should reject invalid proxy type', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
proxyType: 'invalid' as any,
|
||||
};
|
||||
|
||||
await expect(networkProxyCtr.setProxySettings(invalidConfig)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should reject missing proxy server', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
proxyServer: '',
|
||||
};
|
||||
|
||||
await expect(networkProxyCtr.setProxySettings(invalidConfig)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should reject invalid proxy port', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
proxyPort: 'invalid',
|
||||
};
|
||||
|
||||
await expect(networkProxyCtr.setProxySettings(invalidConfig)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should reject missing auth credentials when auth is required', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
proxyRequireAuth: true,
|
||||
proxyUsername: '',
|
||||
proxyPassword: '',
|
||||
};
|
||||
|
||||
await expect(networkProxyCtr.setProxySettings(invalidConfig)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDesktopSettings', () => {
|
||||
it('should return stored proxy settings', async () => {
|
||||
const expectedSettings: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
mockStoreManager.get.mockReturnValue(expectedSettings);
|
||||
|
||||
const result = await networkProxyCtr.getDesktopSettings();
|
||||
|
||||
expect(result).toEqual(expectedSettings);
|
||||
expect(mockStoreManager.get).toHaveBeenCalledWith('networkProxy', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should return default settings when store fails', async () => {
|
||||
mockStoreManager.get.mockImplementation(() => {
|
||||
throw new Error('Store error');
|
||||
});
|
||||
|
||||
const result = await networkProxyCtr.getDesktopSettings();
|
||||
|
||||
expect(result).toEqual({
|
||||
enableProxy: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
proxyPort: '',
|
||||
proxyRequireAuth: false,
|
||||
proxyServer: '',
|
||||
proxyType: 'http',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setProxySettings', () => {
|
||||
const validConfig: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
it('should save valid proxy settings', async () => {
|
||||
mockStoreManager.get.mockReturnValue({
|
||||
enableProxy: false,
|
||||
proxyType: 'http',
|
||||
proxyServer: '',
|
||||
proxyPort: '',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
});
|
||||
|
||||
await networkProxyCtr.setProxySettings(validConfig);
|
||||
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith(
|
||||
'networkProxy',
|
||||
expect.objectContaining(validConfig),
|
||||
);
|
||||
});
|
||||
|
||||
it('should skip update if settings are unchanged', async () => {
|
||||
mockStoreManager.get.mockReturnValue(validConfig);
|
||||
|
||||
await networkProxyCtr.setProxySettings(validConfig);
|
||||
|
||||
expect(mockStoreManager.set).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error for invalid configuration', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
proxyServer: '',
|
||||
};
|
||||
|
||||
await expect(networkProxyCtr.setProxySettings(invalidConfig)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('testProxyConnection', () => {
|
||||
it('should return success for successful connection', async () => {
|
||||
const mockResponse = {
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
};
|
||||
|
||||
vi.mocked(mockUndici.fetch).mockResolvedValueOnce(mockResponse);
|
||||
|
||||
const result = await networkProxyCtr.testProxyConnection('https://www.google.com');
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(mockUndici.fetch).toHaveBeenCalledWith('https://www.google.com', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should throw error for failed connection', async () => {
|
||||
const mockResponse = {
|
||||
ok: false,
|
||||
status: 404,
|
||||
statusText: 'Not Found',
|
||||
};
|
||||
|
||||
vi.mocked(mockUndici.fetch).mockResolvedValueOnce(mockResponse);
|
||||
|
||||
await expect(networkProxyCtr.testProxyConnection('https://www.google.com')).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should throw error for network error', async () => {
|
||||
vi.mocked(mockUndici.fetch).mockRejectedValueOnce(new Error('Network error'));
|
||||
|
||||
await expect(networkProxyCtr.testProxyConnection('https://www.google.com')).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('testProxyConfig', () => {
|
||||
const validConfig: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
it('should return success for valid config and successful connection', async () => {
|
||||
const mockResponse = {
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
};
|
||||
|
||||
vi.mocked(mockUndici.fetch).mockResolvedValueOnce(mockResponse);
|
||||
|
||||
const result = await networkProxyCtr.testProxyConfig({ config: validConfig });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.responseTime).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it('should return failure for invalid config', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
proxyServer: '',
|
||||
};
|
||||
|
||||
const result = await networkProxyCtr.testProxyConfig({ config: invalidConfig });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toContain('Invalid proxy configuration');
|
||||
});
|
||||
|
||||
it('should test direct connection for disabled proxy', async () => {
|
||||
const disabledConfig: NetworkProxySettings = {
|
||||
...validConfig,
|
||||
enableProxy: false,
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
};
|
||||
|
||||
vi.mocked(mockUndici.fetch).mockResolvedValueOnce(mockResponse);
|
||||
|
||||
const result = await networkProxyCtr.testProxyConfig({ config: disabledConfig });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should return failure for connection error', async () => {
|
||||
vi.mocked(mockUndici.fetch).mockRejectedValueOnce(new Error('Connection failed'));
|
||||
|
||||
const result = await networkProxyCtr.testProxyConfig({ config: validConfig });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toContain('Connection failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('beforeAppReady', () => {
|
||||
it('should apply stored proxy settings on app ready', async () => {
|
||||
const storedConfig: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
mockStoreManager.get.mockReturnValue(storedConfig);
|
||||
|
||||
await networkProxyCtr.beforeAppReady();
|
||||
|
||||
expect(mockStoreManager.get).toHaveBeenCalledWith('networkProxy', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should use default settings if stored config is invalid', async () => {
|
||||
const invalidConfig: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: '', // 无效的服务器
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
mockStoreManager.get.mockReturnValue(invalidConfig);
|
||||
|
||||
await networkProxyCtr.beforeAppReady();
|
||||
|
||||
expect(mockStoreManager.get).toHaveBeenCalledWith('networkProxy', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', async () => {
|
||||
mockStoreManager.get.mockImplementation(() => {
|
||||
throw new Error('Store error');
|
||||
});
|
||||
|
||||
// 不应该抛出错误
|
||||
await expect(networkProxyCtr.beforeAppReady()).resolves.not.toThrow();
|
||||
|
||||
mockStoreManager.get.mockReset();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ProxyUrlBuilder', () => {
|
||||
it('should build URL without authentication', () => {
|
||||
const config: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: false,
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
// 通过测试代理设置来间接测试 URL 构建
|
||||
expect(() => networkProxyCtr.setProxySettings(config)).not.toThrow();
|
||||
});
|
||||
|
||||
it('should build URL with authentication', () => {
|
||||
const config: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: true,
|
||||
proxyUsername: 'user',
|
||||
proxyPassword: 'pass',
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
// 通过测试代理设置来间接测试 URL 构建
|
||||
expect(() => networkProxyCtr.setProxySettings(config)).not.toThrow();
|
||||
});
|
||||
|
||||
it('should handle special characters in credentials', () => {
|
||||
const config: NetworkProxySettings = {
|
||||
enableProxy: true,
|
||||
proxyType: 'http',
|
||||
proxyServer: 'proxy.example.com',
|
||||
proxyPort: '8080',
|
||||
proxyRequireAuth: true,
|
||||
proxyUsername: 'user@domain',
|
||||
proxyPassword: 'pass:word',
|
||||
proxyBypass: 'localhost,127.0.0.1,::1',
|
||||
};
|
||||
|
||||
// 通过测试代理设置来间接测试 URL 构建
|
||||
expect(() => networkProxyCtr.setProxySettings(config)).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,9 +5,9 @@ import type { App } from '@/core/App';
|
||||
import ShortcutController from '../ShortcutCtr';
|
||||
|
||||
// 模拟 App 及其依赖项
|
||||
const mockGetShortcutsConfig = vi.fn().mockReturnValue({
|
||||
const mockGetShortcutsConfig = vi.fn().mockReturnValue({
|
||||
toggleMainWindow: 'CommandOrControl+Shift+L',
|
||||
openSettings: 'CommandOrControl+,',
|
||||
openSettings: 'CommandOrControl+,'
|
||||
});
|
||||
const mockUpdateShortcutConfig = vi.fn().mockImplementation((id, accelerator) => {
|
||||
// 简单模拟更新成功
|
||||
@@ -32,11 +32,11 @@ describe('ShortcutController', () => {
|
||||
describe('getShortcutsConfig', () => {
|
||||
it('should return shortcuts config from shortcutManager', () => {
|
||||
const result = shortcutController.getShortcutsConfig();
|
||||
|
||||
|
||||
expect(mockGetShortcutsConfig).toHaveBeenCalled();
|
||||
expect(result).toEqual({
|
||||
toggleMainWindow: 'CommandOrControl+Shift+L',
|
||||
openSettings: 'CommandOrControl+,',
|
||||
openSettings: 'CommandOrControl+,'
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -45,9 +45,9 @@ describe('ShortcutController', () => {
|
||||
it('should call shortcutManager.updateShortcutConfig with correct parameters', () => {
|
||||
const id = 'toggleMainWindow';
|
||||
const accelerator = 'CommandOrControl+Alt+L';
|
||||
|
||||
const result = shortcutController.updateShortcutConfig({ id, accelerator });
|
||||
|
||||
|
||||
const result = shortcutController.updateShortcutConfig(id, accelerator);
|
||||
|
||||
expect(mockUpdateShortcutConfig).toHaveBeenCalledWith(id, accelerator);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
@@ -55,13 +55,10 @@ describe('ShortcutController', () => {
|
||||
it('should return the result from shortcutManager.updateShortcutConfig', () => {
|
||||
// 模拟更新失败的情况
|
||||
mockUpdateShortcutConfig.mockReturnValueOnce(false);
|
||||
|
||||
const result = shortcutController.updateShortcutConfig({
|
||||
id: 'invalidKey',
|
||||
accelerator: 'invalid+combo',
|
||||
});
|
||||
|
||||
|
||||
const result = shortcutController.updateShortcutConfig('invalidKey', 'invalid+combo');
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import type { ClientDispatchEvents } from '@lobechat/electron-client-ipc';
|
||||
import type { ServerDispatchEvents } from '@lobechat/electron-server-ipc';
|
||||
|
||||
import type { App } from '@/core/App';
|
||||
import { IoCContainer } from '@/core/infrastructure/IoCContainer';
|
||||
import { IoCContainer } from '@/core/IoCContainer';
|
||||
import { ShortcutActionType } from '@/shortcuts';
|
||||
|
||||
const ipcDecorator =
|
||||
|
||||
@@ -13,15 +13,14 @@ import { IpcClientEventSender } from '@/types/ipcClientEvent';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
import { CustomRequestHandler, createHandler } from '@/utils/next-electron-rsc';
|
||||
|
||||
import { BrowserManager } from './browser/BrowserManager';
|
||||
import { I18nManager } from './infrastructure/I18nManager';
|
||||
import { IoCContainer } from './infrastructure/IoCContainer';
|
||||
import { StaticFileServerManager } from './infrastructure/StaticFileServerManager';
|
||||
import { StoreManager } from './infrastructure/StoreManager';
|
||||
import { UpdaterManager } from './infrastructure/UpdaterManager';
|
||||
import { MenuManager } from './ui/MenuManager';
|
||||
import { ShortcutManager } from './ui/ShortcutManager';
|
||||
import { TrayManager } from './ui/TrayManager';
|
||||
import BrowserManager from './BrowserManager';
|
||||
import { I18nManager } from './I18nManager';
|
||||
import { IoCContainer } from './IoCContainer';
|
||||
import MenuManager from './MenuManager';
|
||||
import { ShortcutManager } from './ShortcutManager';
|
||||
import { StoreManager } from './StoreManager';
|
||||
import TrayManager from './TrayManager';
|
||||
import { UpdaterManager } from './UpdaterManager';
|
||||
|
||||
const logger = createLogger('core:App');
|
||||
|
||||
@@ -42,7 +41,6 @@ export class App {
|
||||
updaterManager: UpdaterManager;
|
||||
shortcutManager: ShortcutManager;
|
||||
trayManager: TrayManager;
|
||||
staticFileServerManager: StaticFileServerManager;
|
||||
chromeFlags: string[] = ['OverlayScrollbar', 'FluentOverlayScrollbar', 'FluentScrollbar'];
|
||||
|
||||
/**
|
||||
@@ -99,7 +97,6 @@ export class App {
|
||||
this.updaterManager = new UpdaterManager(this);
|
||||
this.shortcutManager = new ShortcutManager(this);
|
||||
this.trayManager = new TrayManager(this);
|
||||
this.staticFileServerManager = new StaticFileServerManager(this);
|
||||
|
||||
// register the schema to interceptor url
|
||||
// it should register before app ready
|
||||
@@ -133,9 +130,6 @@ export class App {
|
||||
await this.i18n.init();
|
||||
this.menuManager.initialize();
|
||||
|
||||
// Initialize static file manager
|
||||
await this.staticFileServerManager.initialize();
|
||||
|
||||
// Initialize global shortcuts: globalShortcut must be called after app.whenReady()
|
||||
this.shortcutManager.initialize();
|
||||
|
||||
@@ -405,7 +399,6 @@ export class App {
|
||||
}
|
||||
|
||||
// 执行清理操作
|
||||
this.staticFileServerManager.destroy();
|
||||
this.unregisterAllRequestHandlers();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,21 +6,13 @@ import {
|
||||
nativeTheme,
|
||||
screen,
|
||||
} from 'electron';
|
||||
import os from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
import { buildDir, preloadDir, resourcesDir } from '@/const/dir';
|
||||
import { isDev, isWindows } from '@/const/env';
|
||||
import {
|
||||
BACKGROUND_DARK,
|
||||
BACKGROUND_LIGHT,
|
||||
SYMBOL_COLOR_DARK,
|
||||
SYMBOL_COLOR_LIGHT,
|
||||
THEME_CHANGE_DELAY,
|
||||
TITLE_BAR_HEIGHT,
|
||||
} from '@/const/theme';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from '../App';
|
||||
import { preloadDir, resourcesDir } from '../const/dir';
|
||||
import type { App } from './App';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:Browser');
|
||||
@@ -28,6 +20,9 @@ const logger = createLogger('core:Browser');
|
||||
export interface BrowserWindowOpts extends BrowserWindowConstructorOptions {
|
||||
devTools?: boolean;
|
||||
height?: number;
|
||||
/**
|
||||
* URL
|
||||
*/
|
||||
identifier: string;
|
||||
keepAlive?: boolean;
|
||||
parentIdentifier?: string;
|
||||
@@ -39,18 +34,38 @@ export interface BrowserWindowOpts extends BrowserWindowConstructorOptions {
|
||||
|
||||
export default class Browser {
|
||||
private app: App;
|
||||
|
||||
/**
|
||||
* Internal electron window
|
||||
*/
|
||||
private _browserWindow?: BrowserWindow;
|
||||
private themeListenerSetup = false;
|
||||
|
||||
private stopInterceptHandler;
|
||||
/**
|
||||
* Identifier
|
||||
*/
|
||||
identifier: string;
|
||||
|
||||
/**
|
||||
* Options at creation
|
||||
*/
|
||||
options: BrowserWindowOpts;
|
||||
|
||||
/**
|
||||
* Key for storing window state in storeManager
|
||||
*/
|
||||
private readonly windowStateKey: string;
|
||||
|
||||
/**
|
||||
* Method to expose window externally
|
||||
*/
|
||||
get browserWindow() {
|
||||
return this.retrieveOrInitialize();
|
||||
}
|
||||
|
||||
get webContents() {
|
||||
if (this._browserWindow.isDestroyed()) return null;
|
||||
|
||||
return this._browserWindow.webContents;
|
||||
}
|
||||
|
||||
@@ -71,101 +86,6 @@ export default class Browser {
|
||||
this.retrieveOrInitialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get platform-specific theme configuration for window creation
|
||||
*/
|
||||
private getPlatformThemeConfig(isDarkMode?: boolean): Record<string, any> {
|
||||
const darkMode = isDarkMode ?? nativeTheme.shouldUseDarkColors;
|
||||
|
||||
if (isWindows) {
|
||||
return this.getWindowsThemeConfig(darkMode);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Windows-specific theme configuration
|
||||
*/
|
||||
private getWindowsThemeConfig(isDarkMode: boolean) {
|
||||
return {
|
||||
backgroundColor: isDarkMode ? BACKGROUND_DARK : BACKGROUND_LIGHT,
|
||||
icon: isDev ? join(buildDir, 'icon-dev.ico') : undefined,
|
||||
titleBarOverlay: {
|
||||
color: isDarkMode ? BACKGROUND_DARK : BACKGROUND_LIGHT,
|
||||
height: TITLE_BAR_HEIGHT,
|
||||
symbolColor: isDarkMode ? SYMBOL_COLOR_DARK : SYMBOL_COLOR_LIGHT,
|
||||
},
|
||||
titleBarStyle: 'hidden' as const,
|
||||
};
|
||||
}
|
||||
|
||||
private setupThemeListener(): void {
|
||||
if (this.themeListenerSetup) return;
|
||||
|
||||
nativeTheme.on('updated', this.handleThemeChange);
|
||||
this.themeListenerSetup = true;
|
||||
}
|
||||
|
||||
private handleThemeChange = (): void => {
|
||||
logger.debug(`[${this.identifier}] System theme changed, reapplying visual effects.`);
|
||||
setTimeout(() => {
|
||||
this.applyVisualEffects();
|
||||
}, THEME_CHANGE_DELAY);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle application theme mode change (called from BrowserManager)
|
||||
*/
|
||||
handleAppThemeChange = (): void => {
|
||||
logger.debug(`[${this.identifier}] App theme mode changed, reapplying visual effects.`);
|
||||
setTimeout(() => {
|
||||
this.applyVisualEffects();
|
||||
}, THEME_CHANGE_DELAY);
|
||||
};
|
||||
|
||||
private applyVisualEffects(): void {
|
||||
if (!this._browserWindow || this._browserWindow.isDestroyed()) return;
|
||||
|
||||
logger.debug(`[${this.identifier}] Applying visual effects for platform`);
|
||||
const isDarkMode = this.isDarkMode;
|
||||
|
||||
try {
|
||||
if (isWindows) {
|
||||
this.applyWindowsVisualEffects(isDarkMode);
|
||||
}
|
||||
|
||||
logger.debug(
|
||||
`[${this.identifier}] Visual effects applied successfully (dark mode: ${isDarkMode})`,
|
||||
);
|
||||
} catch (error) {
|
||||
logger.error(`[${this.identifier}] Failed to apply visual effects:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
private applyWindowsVisualEffects(isDarkMode: boolean): void {
|
||||
const config = this.getWindowsThemeConfig(isDarkMode);
|
||||
|
||||
this._browserWindow.setBackgroundColor(config.backgroundColor);
|
||||
this._browserWindow.setTitleBarOverlay(config.titleBarOverlay);
|
||||
}
|
||||
|
||||
private cleanupThemeListener(): void {
|
||||
if (this.themeListenerSetup) {
|
||||
// Note: nativeTheme listeners are global, consider using a centralized theme manager
|
||||
nativeTheme.off('updated', this.handleThemeChange);
|
||||
// for multiple windows to avoid duplicate listeners
|
||||
this.themeListenerSetup = false;
|
||||
}
|
||||
}
|
||||
|
||||
private get isDarkMode() {
|
||||
const themeMode = this.app.storeManager.get('themeMode');
|
||||
if (themeMode === 'auto') return nativeTheme.shouldUseDarkColors;
|
||||
|
||||
return themeMode === 'dark';
|
||||
}
|
||||
|
||||
loadUrl = async (path: string) => {
|
||||
const initUrl = this.app.nextServerUrl + path;
|
||||
|
||||
@@ -232,8 +152,7 @@ export default class Browser {
|
||||
|
||||
show() {
|
||||
logger.debug(`Showing window: ${this.identifier}`);
|
||||
if (!this._browserWindow.isDestroyed()) this.determineWindowPosition();
|
||||
|
||||
this.determineWindowPosition();
|
||||
this.browserWindow.show();
|
||||
}
|
||||
|
||||
@@ -283,7 +202,6 @@ export default class Browser {
|
||||
destroy() {
|
||||
logger.debug(`Destroying window instance: ${this.identifier}`);
|
||||
this.stopInterceptHandler?.();
|
||||
this.cleanupThemeListener();
|
||||
this._browserWindow = undefined;
|
||||
}
|
||||
|
||||
@@ -309,37 +227,45 @@ export default class Browser {
|
||||
`[${this.identifier}] Saved window state (only size used): ${JSON.stringify(savedState)}`,
|
||||
);
|
||||
|
||||
const { isWindows11, isWindows } = this.getWindowsVersion();
|
||||
const isDarkMode = nativeTheme.shouldUseDarkColors;
|
||||
|
||||
const browserWindow = new BrowserWindow({
|
||||
...res,
|
||||
...(isWindows
|
||||
? {
|
||||
titleBarStyle: 'hidden',
|
||||
}
|
||||
: {}),
|
||||
...(isWindows11
|
||||
? {
|
||||
backgroundMaterial: isDarkMode ? 'mica' : 'acrylic',
|
||||
vibrancy: 'under-window',
|
||||
visualEffectState: 'active',
|
||||
}
|
||||
: {}),
|
||||
autoHideMenuBar: true,
|
||||
backgroundColor: '#00000000',
|
||||
darkTheme: isDarkMode,
|
||||
frame: false,
|
||||
|
||||
height: savedState?.height || height,
|
||||
// Always create hidden first
|
||||
show: false,
|
||||
title,
|
||||
vibrancy: 'sidebar',
|
||||
visualEffectState: 'active',
|
||||
|
||||
webPreferences: {
|
||||
backgroundThrottling: false,
|
||||
// Context isolation environment
|
||||
// https://www.electronjs.org/docs/tutorial/context-isolation
|
||||
contextIsolation: true,
|
||||
preload: join(preloadDir, 'index.js'),
|
||||
},
|
||||
width: savedState?.width || width,
|
||||
...this.getPlatformThemeConfig(isDarkMode),
|
||||
});
|
||||
|
||||
this._browserWindow = browserWindow;
|
||||
logger.debug(`[${this.identifier}] BrowserWindow instance created.`);
|
||||
|
||||
// Initialize theme listener for this window to handle theme changes
|
||||
this.setupThemeListener();
|
||||
logger.debug(`[${this.identifier}] Theme listener setup and applying initial visual effects.`);
|
||||
|
||||
// Apply initial visual effects
|
||||
this.applyVisualEffects();
|
||||
if (isWindows11) this.applyVisualEffects();
|
||||
|
||||
logger.debug(`[${this.identifier}] Setting up nextInterceptor.`);
|
||||
this.stopInterceptHandler = this.app.nextInterceptor({
|
||||
@@ -393,9 +319,8 @@ export default class Browser {
|
||||
} catch (error) {
|
||||
logger.error(`[${this.identifier}] Failed to save window state on quit:`, error);
|
||||
}
|
||||
// Need to clean up intercept handler and theme manager
|
||||
// Need to clean up intercept handler
|
||||
this.stopInterceptHandler?.();
|
||||
this.cleanupThemeListener();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -429,9 +354,8 @@ export default class Browser {
|
||||
} catch (error) {
|
||||
logger.error(`[${this.identifier}] Failed to save window state on close:`, error);
|
||||
}
|
||||
// Need to clean up intercept handler and theme manager
|
||||
// Need to clean up intercept handler
|
||||
this.stopInterceptHandler?.();
|
||||
this.cleanupThemeListener();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -456,12 +380,20 @@ export default class Browser {
|
||||
}
|
||||
|
||||
broadcast = <T extends MainBroadcastEventKey>(channel: T, data?: MainBroadcastParams<T>) => {
|
||||
if (this._browserWindow.isDestroyed()) return;
|
||||
|
||||
logger.debug(`Broadcasting to window ${this.identifier}, channel: ${channel}`);
|
||||
this._browserWindow.webContents.send(channel, data);
|
||||
};
|
||||
|
||||
applyVisualEffects() {
|
||||
// Windows 11 can use this new API
|
||||
if (this._browserWindow) {
|
||||
logger.debug(`[${this.identifier}] Setting window background material for Windows 11`);
|
||||
const isDarkMode = nativeTheme.shouldUseDarkColors;
|
||||
this._browserWindow?.setBackgroundMaterial(isDarkMode ? 'mica' : 'acrylic');
|
||||
this._browserWindow?.setVibrancy('under-window');
|
||||
}
|
||||
}
|
||||
|
||||
toggleVisible() {
|
||||
logger.debug(`Toggling visibility for window: ${this.identifier}`);
|
||||
if (this._browserWindow.isVisible() && this._browserWindow.isFocused()) {
|
||||
@@ -472,11 +404,35 @@ export default class Browser {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually reapply visual effects (useful for fixing lost effects after window state changes)
|
||||
*/
|
||||
reapplyVisualEffects(): void {
|
||||
logger.debug(`[${this.identifier}] Manually reapplying visual effects via Browser.`);
|
||||
this.applyVisualEffects();
|
||||
getWindowsVersion() {
|
||||
if (process.platform !== 'win32') {
|
||||
return {
|
||||
isWindows: false,
|
||||
isWindows10: false,
|
||||
isWindows11: false,
|
||||
version: null,
|
||||
};
|
||||
}
|
||||
|
||||
// 获取操作系统版本(如 "10.0.22621")
|
||||
const release = os.release();
|
||||
const parts = release.split('.');
|
||||
|
||||
// 主版本和次版本
|
||||
const majorVersion = parseInt(parts[0], 10);
|
||||
const minorVersion = parseInt(parts[1], 10);
|
||||
|
||||
// 构建号是第三部分
|
||||
const buildNumber = parseInt(parts[2], 10);
|
||||
|
||||
// Windows 11 的构建号从 22000 开始
|
||||
const isWindows11 = majorVersion === 10 && minorVersion === 0 && buildNumber >= 22_000;
|
||||
|
||||
return {
|
||||
buildNumber,
|
||||
isWindows: true,
|
||||
isWindows11,
|
||||
version: release,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,15 @@ import { WebContents } from 'electron';
|
||||
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { AppBrowsersIdentifiers, appBrowsers } from '../../appBrowsers';
|
||||
import type { App } from '../App';
|
||||
import { AppBrowsersIdentifiers, appBrowsers } from '../appBrowsers';
|
||||
import type { App } from './App';
|
||||
import type { BrowserWindowOpts } from './Browser';
|
||||
import Browser from './Browser';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:BrowserManager');
|
||||
|
||||
export class BrowserManager {
|
||||
export default class BrowserManager {
|
||||
app: App;
|
||||
|
||||
browsers: Map<AppBrowsersIdentifiers, Browser> = new Map();
|
||||
@@ -128,12 +128,9 @@ export class BrowserManager {
|
||||
*/
|
||||
initializeBrowsers() {
|
||||
logger.info('Initializing all browsers');
|
||||
Object.values(appBrowsers).forEach((browser: BrowserWindowOpts) => {
|
||||
Object.values(appBrowsers).forEach((browser) => {
|
||||
logger.debug(`Initializing browser: ${browser.identifier}`);
|
||||
|
||||
if (browser.keepAlive) {
|
||||
this.retrieveOrInitialize(browser);
|
||||
}
|
||||
this.retrieveOrInitialize(browser);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -194,14 +191,4 @@ export class BrowserManager {
|
||||
getIdentifierByWebContents(webContents: WebContents): AppBrowsersIdentifiers | null {
|
||||
return this.webContentsMap.get(webContents) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle application theme mode changes and reapply visual effects to all windows
|
||||
*/
|
||||
handleAppThemeChange(): void {
|
||||
logger.debug('Handling app theme change for all browser windows');
|
||||
this.browsers.forEach((browser) => {
|
||||
browser.handleAppThemeChange();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,12 @@ import { Menu } from 'electron';
|
||||
import { IMenuPlatform, MenuOptions, createMenuImpl } from '@/menus';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from '../App';
|
||||
import type { App } from './App';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:MenuManager');
|
||||
|
||||
export class MenuManager {
|
||||
export default class MenuManager {
|
||||
app: App;
|
||||
private platformImpl: IMenuPlatform;
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
import { globalShortcut } from 'electron';
|
||||
|
||||
import { DEFAULT_SHORTCUTS_CONFIG } from '@/shortcuts';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from './App';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:ShortcutManager');
|
||||
|
||||
export class ShortcutManager {
|
||||
private app: App;
|
||||
private shortcuts: Map<string, () => void> = new Map();
|
||||
private shortcutsConfig: Record<string, string> = {};
|
||||
|
||||
constructor(app: App) {
|
||||
logger.debug('Initializing ShortcutManager');
|
||||
this.app = app;
|
||||
|
||||
app.shortcutMethodMap.forEach((method, key) => {
|
||||
this.shortcuts.set(key, method);
|
||||
});
|
||||
}
|
||||
|
||||
initialize() {
|
||||
logger.info('Initializing global shortcuts');
|
||||
// Load shortcuts configuration from storage
|
||||
this.loadShortcutsConfig();
|
||||
// Register configured shortcuts
|
||||
this.registerConfiguredShortcuts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shortcuts configuration
|
||||
*/
|
||||
getShortcutsConfig(): Record<string, string> {
|
||||
return this.shortcutsConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single shortcut configuration
|
||||
*/
|
||||
updateShortcutConfig(id: string, accelerator: string): boolean {
|
||||
try {
|
||||
logger.debug(`Updating shortcut ${id} to ${accelerator}`);
|
||||
// Update configuration
|
||||
this.shortcutsConfig[id] = accelerator;
|
||||
|
||||
this.saveShortcutsConfig();
|
||||
this.registerConfiguredShortcuts();
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error(`Error updating shortcut ${id}:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register global shortcut
|
||||
* @param accelerator Shortcut key combination
|
||||
* @param callback Callback function
|
||||
* @returns Whether registration was successful
|
||||
*/
|
||||
registerShortcut(accelerator: string, callback: () => void): boolean {
|
||||
try {
|
||||
// If already registered, unregister first
|
||||
if (this.shortcuts.has(accelerator)) {
|
||||
this.unregisterShortcut(accelerator);
|
||||
}
|
||||
|
||||
// Register new shortcut
|
||||
const success = globalShortcut.register(accelerator, callback);
|
||||
|
||||
if (success) {
|
||||
this.shortcuts.set(accelerator, callback);
|
||||
logger.debug(`Registered shortcut: ${accelerator}`);
|
||||
} else {
|
||||
logger.error(`Failed to register shortcut: ${accelerator}`);
|
||||
}
|
||||
|
||||
return success;
|
||||
} catch (error) {
|
||||
logger.error(`Error registering shortcut: ${accelerator}`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister global shortcut
|
||||
* @param accelerator Shortcut key combination
|
||||
*/
|
||||
unregisterShortcut(accelerator: string): void {
|
||||
try {
|
||||
globalShortcut.unregister(accelerator);
|
||||
this.shortcuts.delete(accelerator);
|
||||
logger.debug(`Unregistered shortcut: ${accelerator}`);
|
||||
} catch (error) {
|
||||
logger.error(`Error unregistering shortcut: ${accelerator}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a shortcut is already registered
|
||||
* @param accelerator Shortcut key combination
|
||||
* @returns Whether it is registered
|
||||
*/
|
||||
isRegistered(accelerator: string): boolean {
|
||||
return globalShortcut.isRegistered(accelerator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister all shortcuts
|
||||
*/
|
||||
unregisterAll(): void {
|
||||
globalShortcut.unregisterAll();
|
||||
logger.info('Unregistered all shortcuts');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load shortcuts configuration from storage
|
||||
*/
|
||||
private loadShortcutsConfig() {
|
||||
try {
|
||||
// Try to get configuration from storage
|
||||
const config = this.app.storeManager.get('shortcuts');
|
||||
|
||||
// If no configuration, use default configuration
|
||||
if (!config || Object.keys(config).length === 0) {
|
||||
logger.debug('No shortcuts config found, using defaults');
|
||||
this.shortcutsConfig = DEFAULT_SHORTCUTS_CONFIG;
|
||||
this.saveShortcutsConfig();
|
||||
} else {
|
||||
this.shortcutsConfig = config;
|
||||
}
|
||||
|
||||
logger.debug('Loaded shortcuts config:', this.shortcutsConfig);
|
||||
} catch (error) {
|
||||
logger.error('Error loading shortcuts config:', error);
|
||||
this.shortcutsConfig = DEFAULT_SHORTCUTS_CONFIG;
|
||||
this.saveShortcutsConfig();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save shortcuts configuration to storage
|
||||
*/
|
||||
private saveShortcutsConfig() {
|
||||
try {
|
||||
this.app.storeManager.set('shortcuts', this.shortcutsConfig);
|
||||
logger.debug('Saved shortcuts config');
|
||||
} catch (error) {
|
||||
logger.error('Error saving shortcuts config:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register configured shortcuts
|
||||
*/
|
||||
private registerConfiguredShortcuts() {
|
||||
// Unregister all shortcuts first
|
||||
this.unregisterAll();
|
||||
|
||||
// Register each enabled shortcut
|
||||
Object.entries(this.shortcutsConfig).forEach(([id, accelerator]) => {
|
||||
logger.debug(`Registering shortcut '${id}' with ${accelerator}`);
|
||||
|
||||
const method = this.shortcuts.get(id);
|
||||
if (accelerator && method) {
|
||||
this.registerShortcut(accelerator, method);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { ElectronMainStore, StoreKey } from '@/types/store';
|
||||
import { makeSureDirExist } from '@/utils/file-system';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { App } from '../App';
|
||||
import { App } from './App';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:StoreManager');
|
||||
@@ -12,157 +12,157 @@ import { join } from 'node:path';
|
||||
import { resourcesDir } from '@/const/dir';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from '../App';
|
||||
import type { App } from './App';
|
||||
|
||||
// Create logger
|
||||
// 创建日志记录器
|
||||
const logger = createLogger('core:Tray');
|
||||
|
||||
export interface TrayOptions {
|
||||
/**
|
||||
* Tray icon path (relative to resource directory)
|
||||
* 托盘图标路径(相对于资源目录)
|
||||
*/
|
||||
iconPath: string;
|
||||
|
||||
/**
|
||||
* Tray identifier
|
||||
* 托盘标识符
|
||||
*/
|
||||
identifier: string;
|
||||
|
||||
/**
|
||||
* Tray tooltip text
|
||||
* 托盘提示文本
|
||||
*/
|
||||
tooltip?: string;
|
||||
}
|
||||
|
||||
export class Tray {
|
||||
export default class Tray {
|
||||
private app: App;
|
||||
|
||||
/**
|
||||
* Internal Electron tray
|
||||
* 内部 Electron 托盘
|
||||
*/
|
||||
private _tray?: ElectronTray;
|
||||
|
||||
/**
|
||||
* Identifier
|
||||
* 标识符
|
||||
*/
|
||||
identifier: string;
|
||||
|
||||
/**
|
||||
* Options when created
|
||||
* 创建时的选项
|
||||
*/
|
||||
options: TrayOptions;
|
||||
|
||||
/**
|
||||
* Get tray instance
|
||||
* 获取托盘实例
|
||||
*/
|
||||
get tray() {
|
||||
return this.retrieveOrInitialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct tray object
|
||||
* @param options Tray options
|
||||
* @param application App instance
|
||||
* 构造托盘对象
|
||||
* @param options 托盘选项
|
||||
* @param application 应用实例
|
||||
*/
|
||||
constructor(options: TrayOptions, application: App) {
|
||||
logger.debug(`Creating tray instance: ${options.identifier}`);
|
||||
logger.debug(`Tray options: ${JSON.stringify(options)}`);
|
||||
logger.debug(`创建托盘实例: ${options.identifier}`);
|
||||
logger.debug(`托盘选项: ${JSON.stringify(options)}`);
|
||||
this.app = application;
|
||||
this.identifier = options.identifier;
|
||||
this.options = options;
|
||||
|
||||
// Initialize
|
||||
// 初始化
|
||||
this.retrieveOrInitialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize tray
|
||||
* 初始化托盘
|
||||
*/
|
||||
retrieveOrInitialize() {
|
||||
// If tray already exists and is not destroyed, return it
|
||||
// 如果托盘已存在且未被销毁,则返回
|
||||
if (this._tray) {
|
||||
logger.debug(`[${this.identifier}] Returning existing tray instance`);
|
||||
logger.debug(`[${this.identifier}] 返回现有托盘实例`);
|
||||
return this._tray;
|
||||
}
|
||||
|
||||
const { iconPath, tooltip } = this.options;
|
||||
|
||||
// Load tray icon
|
||||
logger.info(`Creating new tray instance: ${this.identifier}`);
|
||||
// 加载托盘图标
|
||||
logger.info(`创建新的托盘实例: ${this.identifier}`);
|
||||
const iconFile = join(resourcesDir, iconPath);
|
||||
logger.debug(`[${this.identifier}] Loading icon: ${iconFile}`);
|
||||
logger.debug(`[${this.identifier}] 加载图标: ${iconFile}`);
|
||||
|
||||
try {
|
||||
const icon = nativeImage.createFromPath(iconFile);
|
||||
this._tray = new ElectronTray(icon);
|
||||
|
||||
// Set tooltip
|
||||
// 设置工具提示
|
||||
if (tooltip) {
|
||||
logger.debug(`[${this.identifier}] Setting tooltip: ${tooltip}`);
|
||||
logger.debug(`[${this.identifier}] 设置提示文本: ${tooltip}`);
|
||||
this._tray.setToolTip(tooltip);
|
||||
}
|
||||
|
||||
// Set default context menu
|
||||
// 设置默认上下文菜单
|
||||
this.setContextMenu();
|
||||
|
||||
// Set click event
|
||||
// 设置点击事件
|
||||
this._tray.on('click', () => {
|
||||
logger.debug(`[${this.identifier}] Tray clicked`);
|
||||
logger.debug(`[${this.identifier}] 托盘被点击`);
|
||||
this.onClick();
|
||||
});
|
||||
|
||||
logger.debug(`[${this.identifier}] Tray instance created successfully`);
|
||||
logger.debug(`[${this.identifier}] 托盘实例创建完成`);
|
||||
return this._tray;
|
||||
} catch (error) {
|
||||
logger.error(`[${this.identifier}] Failed to create tray:`, error);
|
||||
logger.error(`[${this.identifier}] 创建托盘失败:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tray context menu
|
||||
* @param template Menu template, if not provided default template will be used
|
||||
* 设置托盘上下文菜单
|
||||
* @param template 菜单模板,如果未提供则使用默认模板
|
||||
*/
|
||||
setContextMenu(template?: MenuItemConstructorOptions[]) {
|
||||
logger.debug(`[${this.identifier}] Setting tray context menu`);
|
||||
logger.debug(`[${this.identifier}] 设置托盘上下文菜单`);
|
||||
|
||||
// If no template provided, use default menu
|
||||
// 如果未提供模板,使用默认菜单
|
||||
const defaultTemplate: MenuItemConstructorOptions[] = template || [
|
||||
{
|
||||
click: () => {
|
||||
logger.debug(`[${this.identifier}] Menu item "Show Main Window" clicked`);
|
||||
logger.debug(`[${this.identifier}] 菜单项 "显示主窗口" 被点击`);
|
||||
this.app.browserManager.showMainWindow();
|
||||
},
|
||||
label: 'Show Main Window',
|
||||
label: '显示主窗口',
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
click: () => {
|
||||
logger.debug(`[${this.identifier}] Menu item "Quit" clicked`);
|
||||
logger.debug(`[${this.identifier}] 菜单项 "退出" 被点击`);
|
||||
app.quit();
|
||||
},
|
||||
label: 'Quit',
|
||||
label: '退出',
|
||||
},
|
||||
];
|
||||
|
||||
const contextMenu = Menu.buildFromTemplate(defaultTemplate);
|
||||
this._tray?.setContextMenu(contextMenu);
|
||||
logger.debug(`[${this.identifier}] Tray context menu has been set`);
|
||||
logger.debug(`[${this.identifier}] 托盘上下文菜单已设置`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle tray click event
|
||||
* 处理托盘点击事件
|
||||
*/
|
||||
onClick() {
|
||||
logger.debug(`[${this.identifier}] Handling tray click event`);
|
||||
logger.debug(`[${this.identifier}] 处理托盘点击事件`);
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
|
||||
if (mainWindow) {
|
||||
if (mainWindow.browserWindow.isVisible() && mainWindow.browserWindow.isFocused()) {
|
||||
logger.debug(`[${this.identifier}] Main window is visible and focused, hiding it now`);
|
||||
logger.debug(`[${this.identifier}] 主窗口已可见且聚焦,现在隐藏它`);
|
||||
mainWindow.hide();
|
||||
} else {
|
||||
logger.debug(`[${this.identifier}] Showing and focusing main window`);
|
||||
logger.debug(`[${this.identifier}] 显示并聚焦主窗口`);
|
||||
mainWindow.show();
|
||||
mainWindow.browserWindow.focus();
|
||||
}
|
||||
@@ -170,61 +170,59 @@ export class Tray {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update tray icon
|
||||
* @param iconPath New icon path (relative to resource directory)
|
||||
* 更新托盘图标
|
||||
* @param iconPath 新图标路径(相对于资源目录)
|
||||
*/
|
||||
updateIcon(iconPath: string) {
|
||||
logger.debug(`[${this.identifier}] Updating icon: ${iconPath}`);
|
||||
logger.debug(`[${this.identifier}] 更新图标: ${iconPath}`);
|
||||
try {
|
||||
const iconFile = join(resourcesDir, iconPath);
|
||||
const icon = nativeImage.createFromPath(iconFile);
|
||||
this._tray?.setImage(icon);
|
||||
this.options.iconPath = iconPath;
|
||||
logger.debug(`[${this.identifier}] Icon updated successfully`);
|
||||
logger.debug(`[${this.identifier}] 图标已更新`);
|
||||
} catch (error) {
|
||||
logger.error(`[${this.identifier}] Failed to update icon:`, error);
|
||||
logger.error(`[${this.identifier}] 更新图标失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update tooltip text
|
||||
* @param tooltip New tooltip text
|
||||
* 更新提示文本
|
||||
* @param tooltip 新提示文本
|
||||
*/
|
||||
updateTooltip(tooltip: string) {
|
||||
logger.debug(`[${this.identifier}] Updating tooltip: ${tooltip}`);
|
||||
logger.debug(`[${this.identifier}] 更新提示文本: ${tooltip}`);
|
||||
this._tray?.setToolTip(tooltip);
|
||||
this.options.tooltip = tooltip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display balloon notification (only supported on Windows)
|
||||
* @param options Balloon options
|
||||
* 显示气泡通知(仅在 Windows 上支持)
|
||||
* @param options 气泡选项
|
||||
*/
|
||||
displayBalloon(options: DisplayBalloonOptions) {
|
||||
if (process.platform === 'win32' && this._tray) {
|
||||
logger.debug(
|
||||
`[${this.identifier}] Displaying balloon notification: ${JSON.stringify(options)}`,
|
||||
);
|
||||
logger.debug(`[${this.identifier}] 显示气泡通知: ${JSON.stringify(options)}`);
|
||||
this._tray.displayBalloon(options);
|
||||
} else {
|
||||
logger.debug(`[${this.identifier}] Balloon notification is only supported on Windows`);
|
||||
logger.debug(`[${this.identifier}] 气泡通知仅在 Windows 上支持`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast event
|
||||
* 广播事件
|
||||
*/
|
||||
broadcast = <T extends MainBroadcastEventKey>(channel: T, data?: MainBroadcastParams<T>) => {
|
||||
logger.debug(`Broadcasting to tray ${this.identifier}, channel: ${channel}`);
|
||||
// Can forward message to main window through App instance's browserManager
|
||||
logger.debug(`向托盘 ${this.identifier} 广播, 频道: ${channel}`);
|
||||
// 可以通过 App 实例的 browserManager 将消息转发到主窗口
|
||||
this.app.browserManager.getMainWindow()?.broadcast(channel, data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Destroy tray instance
|
||||
* 销毁托盘实例
|
||||
*/
|
||||
destroy() {
|
||||
logger.debug(`Destroying tray instance: ${this.identifier}`);
|
||||
logger.debug(`销毁托盘实例: ${this.identifier}`);
|
||||
if (this._tray) {
|
||||
this._tray.destroy();
|
||||
this._tray = undefined;
|
||||
@@ -1,12 +1,10 @@
|
||||
import { MainBroadcastEventKey, MainBroadcastParams } from '@lobechat/electron-client-ipc';
|
||||
import { nativeTheme } from 'electron';
|
||||
|
||||
import { name } from '@/../../package.json';
|
||||
import { isMac } from '@/const/env';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from '../App';
|
||||
import { Tray, TrayOptions } from './Tray';
|
||||
import type { App } from './App';
|
||||
import Tray, { TrayOptions } from './Tray';
|
||||
|
||||
// 创建日志记录器
|
||||
const logger = createLogger('core:TrayManager');
|
||||
@@ -16,7 +14,7 @@ const logger = createLogger('core:TrayManager');
|
||||
*/
|
||||
export type TrayIdentifiers = 'main';
|
||||
|
||||
export class TrayManager {
|
||||
export default class TrayManager {
|
||||
app: App;
|
||||
|
||||
/**
|
||||
@@ -56,13 +54,9 @@ export class TrayManager {
|
||||
initializeMainTray() {
|
||||
logger.debug('初始化主托盘');
|
||||
return this.retrieveOrInitialize({
|
||||
iconPath: isMac
|
||||
? nativeTheme.shouldUseDarkColors
|
||||
? 'tray-dark.png'
|
||||
: 'tray-light.png'
|
||||
: 'tray.png',
|
||||
identifier: 'main', // Use app icon, ensure this file exists in resources directory
|
||||
tooltip: name, // Can use app.getName() or localized string
|
||||
iconPath: 'tray-icon.png',
|
||||
identifier: 'main', // 使用应用图标,需要确保资源目录中有此文件
|
||||
tooltip: name, // 可以使用 app.getName() 或本地化字符串
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { isDev } from '@/const/env';
|
||||
import { UPDATE_CHANNEL as channel, updaterConfig } from '@/modules/updater/configs';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App as AppCore } from '../App';
|
||||
import type { App as AppCore } from './App';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:UpdaterManager');
|
||||
@@ -1,227 +0,0 @@
|
||||
import { getPort } from 'get-port-please';
|
||||
import { createServer } from 'node:http';
|
||||
|
||||
import { LOCAL_STORAGE_URL_PREFIX } from '@/const/dir';
|
||||
import FileService from '@/services/fileSrv';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from '../App';
|
||||
|
||||
const logger = createLogger('core:StaticFileServerManager');
|
||||
|
||||
export class StaticFileServerManager {
|
||||
private app: App;
|
||||
private fileService: FileService;
|
||||
private httpServer: any = null;
|
||||
private serverPort: number = 0;
|
||||
private isInitialized = false;
|
||||
|
||||
constructor(app: App) {
|
||||
this.app = app;
|
||||
this.fileService = app.getService(FileService);
|
||||
logger.debug('StaticFileServerManager initialized');
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化静态文件管理器
|
||||
*/
|
||||
async initialize(): Promise<void> {
|
||||
if (this.isInitialized) {
|
||||
logger.warn('StaticFileServerManager already initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info('Initializing StaticFileServerManager');
|
||||
|
||||
try {
|
||||
// 启动 HTTP 文件服务器
|
||||
await this.startHttpServer();
|
||||
|
||||
this.isInitialized = true;
|
||||
logger.info(
|
||||
`StaticFileServerManager initialization completed, server running on port ${this.serverPort}`,
|
||||
);
|
||||
} catch (error) {
|
||||
logger.error('Failed to initialize StaticFileServerManager:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动 HTTP 文件服务器
|
||||
*/
|
||||
private async startHttpServer(): Promise<void> {
|
||||
try {
|
||||
// 使用 get-port-please 获取可用端口
|
||||
this.serverPort = await getPort({
|
||||
// 备用端口
|
||||
host: '127.0.0.1',
|
||||
|
||||
port: 33_250,
|
||||
// 首选端口
|
||||
ports: [33_251, 33_252, 33_253, 33_254, 33_255],
|
||||
});
|
||||
|
||||
logger.debug(`Found available port: ${this.serverPort}`);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = createServer(async (req, res) => {
|
||||
// 设置请求超时
|
||||
req.setTimeout(30_000, () => {
|
||||
logger.warn('Request timeout, closing connection');
|
||||
if (!res.destroyed && !res.headersSent) {
|
||||
res.writeHead(408, { 'Content-Type': 'text/plain' });
|
||||
res.end('Request Timeout');
|
||||
}
|
||||
});
|
||||
|
||||
// 监听客户端断开连接
|
||||
req.on('close', () => {
|
||||
logger.debug('Client disconnected during request processing');
|
||||
});
|
||||
|
||||
try {
|
||||
await this.handleHttpRequest(req, res);
|
||||
} catch (error) {
|
||||
logger.error('Unhandled error in HTTP request handler:', error);
|
||||
|
||||
// 尝试发送错误响应,但确保不会导致进一步错误
|
||||
try {
|
||||
if (!res.destroyed && !res.headersSent) {
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
||||
res.end('Internal Server Error');
|
||||
}
|
||||
} catch (responseError) {
|
||||
logger.error('Failed to send error response:', responseError);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 监听指定端口
|
||||
server.listen(this.serverPort, '127.0.0.1', () => {
|
||||
this.httpServer = server;
|
||||
logger.info(`HTTP file server started on port ${this.serverPort}`);
|
||||
resolve();
|
||||
});
|
||||
|
||||
server.on('error', (error) => {
|
||||
logger.error('HTTP server error:', error);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to get available port:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 HTTP 请求
|
||||
*/
|
||||
private async handleHttpRequest(req: any, res: any): Promise<void> {
|
||||
try {
|
||||
// 检查响应是否已经结束
|
||||
if (res.destroyed || res.headersSent) {
|
||||
logger.warn('Response already ended, skipping request processing');
|
||||
return;
|
||||
}
|
||||
|
||||
const url = new URL(req.url, `http://127.0.0.1:${this.serverPort}`);
|
||||
logger.debug(`Processing HTTP file request: ${req.url}`);
|
||||
|
||||
// 提取文件路径:从 /desktop-file/path/to/file.png 中提取相对路径
|
||||
let filePath = decodeURIComponent(url.pathname.slice(1)); // 移除开头的 /
|
||||
|
||||
// 如果路径以 desktop-file/ 开头,则移除该前缀
|
||||
const prefixWithoutSlash = LOCAL_STORAGE_URL_PREFIX.slice(1) + '/'; // 移除开头的 / 并添加结尾的 /
|
||||
if (filePath.startsWith(prefixWithoutSlash)) {
|
||||
filePath = filePath.slice(prefixWithoutSlash.length);
|
||||
}
|
||||
|
||||
if (!filePath) {
|
||||
logger.warn(`Empty file path in HTTP request: ${req.url}`);
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
||||
res.end('Bad Request: Empty file path');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 FileService 获取文件
|
||||
const fileResult = await this.fileService.getFile(`desktop://${filePath}`);
|
||||
|
||||
// 再次检查响应状态
|
||||
if (res.destroyed || res.headersSent) {
|
||||
logger.warn('Response ended during file processing');
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
res.writeHead(200, {
|
||||
// 缓存一年
|
||||
'Access-Control-Allow-Origin': 'http://localhost:*',
|
||||
|
||||
'Cache-Control': 'public, max-age=31536000',
|
||||
// 允许 localhost 的任意端口
|
||||
'Content-Length': Buffer.byteLength(fileResult.content),
|
||||
'Content-Type': fileResult.mimeType,
|
||||
});
|
||||
|
||||
// 发送文件内容
|
||||
res.end(Buffer.from(fileResult.content));
|
||||
|
||||
logger.debug(`HTTP file served successfully: desktop://${filePath}`);
|
||||
} catch (error) {
|
||||
logger.error(`Error serving HTTP file: ${error}`);
|
||||
|
||||
// 检查响应是否仍然可写
|
||||
if (!res.destroyed && !res.headersSent) {
|
||||
try {
|
||||
// 判断是否是文件未找到错误
|
||||
if (error.name === 'FileNotFoundError') {
|
||||
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
||||
res.end('File Not Found');
|
||||
} else {
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
||||
res.end('Internal Server Error');
|
||||
}
|
||||
} catch (writeError) {
|
||||
logger.error('Failed to write error response:', writeError);
|
||||
}
|
||||
} else {
|
||||
logger.warn('Cannot write error response: connection already closed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件服务器域名
|
||||
*/
|
||||
getFileServerDomain(): string {
|
||||
if (!this.isInitialized || !this.serverPort) {
|
||||
throw new Error('StaticFileServerManager not initialized or server not started');
|
||||
}
|
||||
|
||||
const serverDomain = `http://127.0.0.1:${this.serverPort}`;
|
||||
return serverDomain;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁静态文件管理器
|
||||
*/
|
||||
destroy() {
|
||||
logger.info('Destroying StaticFileServerManager');
|
||||
|
||||
if (this.httpServer) {
|
||||
logger.debug('Closing HTTP file server');
|
||||
this.httpServer.close(() => {
|
||||
logger.debug('HTTP file server closed');
|
||||
});
|
||||
this.httpServer = null;
|
||||
this.serverPort = 0;
|
||||
}
|
||||
|
||||
this.isInitialized = false;
|
||||
logger.info('StaticFileServerManager destroyed');
|
||||
}
|
||||
}
|
||||
@@ -1,300 +0,0 @@
|
||||
import { globalShortcut } from 'electron';
|
||||
|
||||
import { DEFAULT_SHORTCUTS_CONFIG } from '@/shortcuts';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App } from '../App';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:ShortcutManager');
|
||||
|
||||
export interface ShortcutUpdateResult {
|
||||
errorType?:
|
||||
| 'INVALID_ID'
|
||||
| 'INVALID_FORMAT'
|
||||
| 'NO_MODIFIER'
|
||||
| 'CONFLICT'
|
||||
| 'SYSTEM_OCCUPIED'
|
||||
| 'UNKNOWN';
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export class ShortcutManager {
|
||||
private app: App;
|
||||
private shortcuts: Map<string, () => void> = new Map();
|
||||
private shortcutsConfig: Record<string, string> = {};
|
||||
|
||||
constructor(app: App) {
|
||||
logger.debug('Initializing ShortcutManager');
|
||||
this.app = app;
|
||||
|
||||
app.shortcutMethodMap.forEach((method, key) => {
|
||||
this.shortcuts.set(key, method);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert react-hotkey format to Electron accelerator format
|
||||
* @param accelerator The accelerator string from frontend
|
||||
* @returns Converted accelerator string for Electron
|
||||
*/
|
||||
private convertAcceleratorFormat(accelerator: string): string {
|
||||
return accelerator
|
||||
.split('+')
|
||||
.map((key) => {
|
||||
const trimmedKey = key.trim().toLowerCase();
|
||||
|
||||
// Convert react-hotkey 'mod' to Electron 'CommandOrControl'
|
||||
if (trimmedKey === 'mod') {
|
||||
return 'CommandOrControl';
|
||||
}
|
||||
|
||||
// Keep other keys as is, but preserve proper casing
|
||||
return key.trim().length === 1 ? key.trim().toUpperCase() : key.trim();
|
||||
})
|
||||
.join('+');
|
||||
}
|
||||
|
||||
initialize() {
|
||||
logger.info('Initializing global shortcuts');
|
||||
// Load shortcuts configuration from storage
|
||||
this.loadShortcutsConfig();
|
||||
// Register configured shortcuts
|
||||
this.registerConfiguredShortcuts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shortcuts configuration
|
||||
*/
|
||||
getShortcutsConfig(): Record<string, string> {
|
||||
return this.shortcutsConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single shortcut configuration
|
||||
*/
|
||||
updateShortcutConfig(id: string, accelerator: string): ShortcutUpdateResult {
|
||||
try {
|
||||
logger.debug(`Updating shortcut ${id} to ${accelerator}`);
|
||||
|
||||
// 1. 检查 ID 是否有效
|
||||
if (!DEFAULT_SHORTCUTS_CONFIG[id]) {
|
||||
logger.error(`Invalid shortcut ID: ${id}`);
|
||||
return { errorType: 'INVALID_ID', success: false };
|
||||
}
|
||||
|
||||
// 2. 基本格式校验
|
||||
if (!accelerator || typeof accelerator !== 'string' || accelerator.trim() === '') {
|
||||
logger.error(`Invalid accelerator format: ${accelerator}`);
|
||||
return { errorType: 'INVALID_FORMAT', success: false };
|
||||
}
|
||||
|
||||
// 转换前端格式到 Electron 格式
|
||||
const convertedAccelerator = this.convertAcceleratorFormat(accelerator.trim());
|
||||
const cleanAccelerator = convertedAccelerator.toLowerCase();
|
||||
|
||||
logger.debug(`Converted accelerator from ${accelerator} to ${convertedAccelerator}`);
|
||||
|
||||
// 3. 检查是否包含 + 号(修饰键格式)
|
||||
if (!cleanAccelerator.includes('+')) {
|
||||
logger.error(
|
||||
`Invalid accelerator format: ${cleanAccelerator}. Must contain modifier keys like 'CommandOrControl+E'`,
|
||||
);
|
||||
return { errorType: 'INVALID_FORMAT', success: false };
|
||||
}
|
||||
|
||||
// 4. 检查是否有基本的修饰键
|
||||
const hasModifier = ['CommandOrControl', 'Command', 'Ctrl', 'Alt', 'Shift'].some((modifier) =>
|
||||
cleanAccelerator.includes(modifier.toLowerCase()),
|
||||
);
|
||||
|
||||
if (!hasModifier) {
|
||||
logger.error(`Invalid accelerator format: ${cleanAccelerator}. Must contain modifier keys`);
|
||||
return { errorType: 'NO_MODIFIER', success: false };
|
||||
}
|
||||
|
||||
// 5. 检查冲突
|
||||
for (const [existingId, existingAccelerator] of Object.entries(this.shortcutsConfig)) {
|
||||
if (
|
||||
existingId !== id &&
|
||||
typeof existingAccelerator === 'string' &&
|
||||
existingAccelerator.toLowerCase() === cleanAccelerator
|
||||
) {
|
||||
logger.error(`Shortcut conflict: ${cleanAccelerator} already used by ${existingId}`);
|
||||
return { errorType: 'CONFLICT', success: false };
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 尝试注册测试(检查是否被系统占用)
|
||||
const testSuccess = globalShortcut.register(convertedAccelerator, () => {});
|
||||
if (!testSuccess) {
|
||||
logger.error(
|
||||
`Shortcut ${convertedAccelerator} is already registered by system or other app`,
|
||||
);
|
||||
return { errorType: 'SYSTEM_OCCUPIED', success: false };
|
||||
} else {
|
||||
// 测试成功,立即取消注册
|
||||
globalShortcut.unregister(convertedAccelerator);
|
||||
}
|
||||
|
||||
// 7. 更新配置
|
||||
this.shortcutsConfig[id] = convertedAccelerator;
|
||||
|
||||
this.saveShortcutsConfig();
|
||||
this.registerConfiguredShortcuts();
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
logger.error(`Error updating shortcut ${id}:`, error);
|
||||
return { errorType: 'UNKNOWN', success: false };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register global shortcut
|
||||
* @param accelerator Shortcut key combination
|
||||
* @param callback Callback function
|
||||
* @returns Whether registration was successful
|
||||
*/
|
||||
registerShortcut(accelerator: string, callback: () => void): boolean {
|
||||
try {
|
||||
// If already registered, unregister first
|
||||
if (this.shortcuts.has(accelerator)) {
|
||||
this.unregisterShortcut(accelerator);
|
||||
}
|
||||
|
||||
// Register new shortcut
|
||||
const success = globalShortcut.register(accelerator, callback);
|
||||
|
||||
if (success) {
|
||||
this.shortcuts.set(accelerator, callback);
|
||||
logger.debug(`Registered shortcut: ${accelerator}`);
|
||||
} else {
|
||||
logger.error(`Failed to register shortcut: ${accelerator}`);
|
||||
}
|
||||
|
||||
return success;
|
||||
} catch (error) {
|
||||
logger.error(`Error registering shortcut: ${accelerator}`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister global shortcut
|
||||
* @param accelerator Shortcut key combination
|
||||
*/
|
||||
unregisterShortcut(accelerator: string): void {
|
||||
try {
|
||||
globalShortcut.unregister(accelerator);
|
||||
this.shortcuts.delete(accelerator);
|
||||
logger.debug(`Unregistered shortcut: ${accelerator}`);
|
||||
} catch (error) {
|
||||
logger.error(`Error unregistering shortcut: ${accelerator}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a shortcut is already registered
|
||||
* @param accelerator Shortcut key combination
|
||||
* @returns Whether it is registered
|
||||
*/
|
||||
isRegistered(accelerator: string): boolean {
|
||||
return globalShortcut.isRegistered(accelerator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister all shortcuts
|
||||
*/
|
||||
unregisterAll(): void {
|
||||
globalShortcut.unregisterAll();
|
||||
logger.info('Unregistered all shortcuts');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load shortcuts configuration from storage
|
||||
*/
|
||||
private loadShortcutsConfig() {
|
||||
try {
|
||||
// Try to get configuration from storage
|
||||
const config = this.app.storeManager.get('shortcuts');
|
||||
|
||||
// If no configuration, use default configuration
|
||||
if (!config || Object.keys(config).length === 0) {
|
||||
logger.debug('No shortcuts config found, using defaults');
|
||||
this.shortcutsConfig = DEFAULT_SHORTCUTS_CONFIG;
|
||||
this.saveShortcutsConfig();
|
||||
} else {
|
||||
// Filter out invalid shortcuts that are not in DEFAULT_SHORTCUTS_CONFIG
|
||||
const filteredConfig: Record<string, string> = {};
|
||||
let hasInvalidKeys = false;
|
||||
|
||||
Object.entries(config).forEach(([id, accelerator]) => {
|
||||
if (DEFAULT_SHORTCUTS_CONFIG[id]) {
|
||||
filteredConfig[id] = accelerator;
|
||||
} else {
|
||||
hasInvalidKeys = true;
|
||||
logger.debug(`Filtering out invalid shortcut ID: ${id}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Ensure all default shortcuts are present
|
||||
Object.entries(DEFAULT_SHORTCUTS_CONFIG).forEach(([id, defaultAccelerator]) => {
|
||||
if (!(id in filteredConfig)) {
|
||||
filteredConfig[id] = defaultAccelerator;
|
||||
logger.debug(`Adding missing default shortcut: ${id} = ${defaultAccelerator}`);
|
||||
}
|
||||
});
|
||||
|
||||
this.shortcutsConfig = filteredConfig;
|
||||
|
||||
// Save the filtered configuration back to storage if we removed invalid keys
|
||||
if (hasInvalidKeys) {
|
||||
logger.debug('Saving filtered shortcuts config to remove invalid keys');
|
||||
this.saveShortcutsConfig();
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug('Loaded shortcuts config:', this.shortcutsConfig);
|
||||
} catch (error) {
|
||||
logger.error('Error loading shortcuts config:', error);
|
||||
this.shortcutsConfig = DEFAULT_SHORTCUTS_CONFIG;
|
||||
this.saveShortcutsConfig();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save shortcuts configuration to storage
|
||||
*/
|
||||
private saveShortcutsConfig() {
|
||||
try {
|
||||
this.app.storeManager.set('shortcuts', this.shortcutsConfig);
|
||||
logger.debug('Saved shortcuts config');
|
||||
} catch (error) {
|
||||
logger.error('Error saving shortcuts config:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register configured shortcuts
|
||||
*/
|
||||
private registerConfiguredShortcuts() {
|
||||
// Unregister all shortcuts first
|
||||
this.unregisterAll();
|
||||
|
||||
// Register each enabled shortcut
|
||||
Object.entries(this.shortcutsConfig).forEach(([id, accelerator]) => {
|
||||
logger.debug(`Registering shortcut '${id}' with ${accelerator}`);
|
||||
|
||||
// 只注册在 DEFAULT_SHORTCUTS_CONFIG 中存在的快捷键
|
||||
if (!DEFAULT_SHORTCUTS_CONFIG[id]) {
|
||||
logger.debug(`Skipping shortcut '${id}' - not found in DEFAULT_SHORTCUTS_CONFIG`);
|
||||
return;
|
||||
}
|
||||
|
||||
const method = this.shortcuts.get(id);
|
||||
if (accelerator && method) {
|
||||
this.registerShortcut(accelerator, method);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,539 +0,0 @@
|
||||
import { globalShortcut } from 'electron';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { DEFAULT_SHORTCUTS_CONFIG } from '@/shortcuts';
|
||||
|
||||
import type { App } from '../../App';
|
||||
import { ShortcutManager } from '../ShortcutManager';
|
||||
|
||||
// Mock electron
|
||||
vi.mock('electron', () => ({
|
||||
globalShortcut: {
|
||||
register: vi.fn(),
|
||||
unregister: vi.fn(),
|
||||
unregisterAll: vi.fn(),
|
||||
isRegistered: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock Logger
|
||||
vi.mock('@/utils/logger', () => ({
|
||||
createLogger: () => ({
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock DEFAULT_SHORTCUTS_CONFIG
|
||||
vi.mock('@/shortcuts', () => ({
|
||||
DEFAULT_SHORTCUTS_CONFIG: {
|
||||
showApp: 'Control+E',
|
||||
openSettings: 'CommandOrControl+,',
|
||||
},
|
||||
}));
|
||||
|
||||
describe('ShortcutManager', () => {
|
||||
let shortcutManager: ShortcutManager;
|
||||
let mockApp: App;
|
||||
let mockStoreManager: any;
|
||||
let mockShortcutMethodMap: Map<string, () => void>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Reset all mocks to their default behavior
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(true);
|
||||
vi.mocked(globalShortcut.unregister).mockReturnValue(undefined);
|
||||
vi.mocked(globalShortcut.unregisterAll).mockReturnValue(undefined);
|
||||
vi.mocked(globalShortcut.isRegistered).mockReturnValue(false);
|
||||
|
||||
// Mock store manager
|
||||
mockStoreManager = {
|
||||
get: vi.fn(),
|
||||
set: vi.fn(),
|
||||
};
|
||||
|
||||
// Mock shortcut method map
|
||||
mockShortcutMethodMap = new Map();
|
||||
const showAppMethod = vi.fn();
|
||||
const openSettingsMethod = vi.fn();
|
||||
mockShortcutMethodMap.set('showApp', showAppMethod);
|
||||
mockShortcutMethodMap.set('openSettings', openSettingsMethod);
|
||||
|
||||
// Mock App
|
||||
mockApp = {
|
||||
storeManager: mockStoreManager,
|
||||
shortcutMethodMap: mockShortcutMethodMap,
|
||||
} as unknown as App;
|
||||
|
||||
shortcutManager = new ShortcutManager(mockApp);
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('should initialize shortcut manager with app', () => {
|
||||
expect(shortcutManager).toBeDefined();
|
||||
expect(shortcutManager['app']).toBe(mockApp);
|
||||
});
|
||||
|
||||
it('should populate shortcuts map from app shortcut method map', () => {
|
||||
expect(shortcutManager['shortcuts'].size).toBe(2);
|
||||
expect(shortcutManager['shortcuts'].has('showApp')).toBe(true);
|
||||
expect(shortcutManager['shortcuts'].has('openSettings')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertAcceleratorFormat', () => {
|
||||
it('should convert mod to CommandOrControl', () => {
|
||||
const result = shortcutManager['convertAcceleratorFormat']('mod+e');
|
||||
expect(result).toBe('CommandOrControl+E');
|
||||
});
|
||||
|
||||
it('should preserve other keys as is except single characters', () => {
|
||||
const result = shortcutManager['convertAcceleratorFormat']('ctrl+alt+f12');
|
||||
expect(result).toBe('ctrl+alt+f12');
|
||||
});
|
||||
|
||||
it('should handle single character keys with uppercase', () => {
|
||||
const result = shortcutManager['convertAcceleratorFormat']('ctrl + a');
|
||||
expect(result).toBe('ctrl+A');
|
||||
});
|
||||
|
||||
it('should handle complex combinations', () => {
|
||||
const result = shortcutManager['convertAcceleratorFormat']('mod+shift+delete');
|
||||
expect(result).toBe('CommandOrControl+shift+delete');
|
||||
});
|
||||
});
|
||||
|
||||
describe('initialize', () => {
|
||||
it('should load shortcuts config and register shortcuts', () => {
|
||||
// Mock store to return empty config (will use defaults)
|
||||
mockStoreManager.get.mockReturnValue({});
|
||||
|
||||
shortcutManager.initialize();
|
||||
|
||||
expect(mockStoreManager.get).toHaveBeenCalledWith('shortcuts');
|
||||
expect(globalShortcut.unregisterAll).toHaveBeenCalled();
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Control+E', expect.any(Function));
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith(
|
||||
'CommandOrControl+,',
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle stored config with filtering', () => {
|
||||
const storedConfig = {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+Shift+P',
|
||||
invalidKey: 'Ctrl+I', // Should be filtered out
|
||||
};
|
||||
mockStoreManager.get.mockReturnValue(storedConfig);
|
||||
|
||||
shortcutManager.initialize();
|
||||
|
||||
const config = shortcutManager.getShortcutsConfig();
|
||||
expect(config.showApp).toBe('Alt+E');
|
||||
expect(config.openSettings).toBe('Ctrl+Shift+P');
|
||||
expect(config.invalidKey).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getShortcutsConfig', () => {
|
||||
it('should return current shortcuts configuration', () => {
|
||||
mockStoreManager.get.mockReturnValue({});
|
||||
shortcutManager.initialize();
|
||||
|
||||
const config = shortcutManager.getShortcutsConfig();
|
||||
expect(config).toEqual(DEFAULT_SHORTCUTS_CONFIG);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateShortcutConfig', () => {
|
||||
beforeEach(() => {
|
||||
mockStoreManager.get.mockReturnValue({});
|
||||
shortcutManager.initialize();
|
||||
});
|
||||
|
||||
it('should successfully update valid shortcut', () => {
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'Alt+E');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.errorType).toBeUndefined();
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith(
|
||||
'shortcuts',
|
||||
expect.objectContaining({
|
||||
showApp: 'Alt+E',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should reject invalid shortcut ID', () => {
|
||||
const result = shortcutManager.updateShortcutConfig('invalidId', 'Alt+E');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('INVALID_ID');
|
||||
});
|
||||
|
||||
it('should reject empty accelerator', () => {
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', '');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('INVALID_FORMAT');
|
||||
});
|
||||
|
||||
it('should reject accelerator without modifier keys', () => {
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'E');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('INVALID_FORMAT');
|
||||
});
|
||||
|
||||
it('should reject accelerator without proper modifiers', () => {
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'F1+E');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('NO_MODIFIER');
|
||||
});
|
||||
|
||||
it('should detect conflicts with existing shortcuts', () => {
|
||||
// First set a shortcut
|
||||
shortcutManager.updateShortcutConfig('showApp', 'Alt+E');
|
||||
|
||||
// Try to set the same accelerator for another shortcut
|
||||
const result = shortcutManager.updateShortcutConfig('openSettings', 'Alt+E');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('CONFLICT');
|
||||
});
|
||||
|
||||
it('should detect system occupied shortcuts', () => {
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(false);
|
||||
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'Ctrl+Alt+T');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('SYSTEM_OCCUPIED');
|
||||
});
|
||||
|
||||
it('should handle registration test cleanup', () => {
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(true);
|
||||
|
||||
shortcutManager.updateShortcutConfig('showApp', 'Ctrl+Alt+T');
|
||||
|
||||
// Should unregister the test registration
|
||||
expect(globalShortcut.unregister).toHaveBeenCalledWith('Ctrl+Alt+T');
|
||||
});
|
||||
|
||||
it('should handle conversion from react-hotkey format', () => {
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'mod+shift+e');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
const config = shortcutManager.getShortcutsConfig();
|
||||
expect(config.showApp).toBe('CommandOrControl+shift+E');
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', () => {
|
||||
// Mock globalShortcut.register to throw an error during testing
|
||||
vi.mocked(globalShortcut.register).mockImplementation(() => {
|
||||
throw new Error('Register error');
|
||||
});
|
||||
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'Alt+E');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorType).toBe('UNKNOWN');
|
||||
});
|
||||
});
|
||||
|
||||
describe('registerShortcut', () => {
|
||||
it('should register new shortcut successfully', () => {
|
||||
const callback = vi.fn();
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(true);
|
||||
|
||||
const result = shortcutManager.registerShortcut('Ctrl+T', callback);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Ctrl+T', callback);
|
||||
expect(shortcutManager['shortcuts'].has('Ctrl+T')).toBe(true);
|
||||
});
|
||||
|
||||
it('should unregister existing shortcut before registering new one', () => {
|
||||
const callback1 = vi.fn();
|
||||
const callback2 = vi.fn();
|
||||
|
||||
// First registration
|
||||
shortcutManager['shortcuts'].set('Ctrl+T', callback1);
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(true);
|
||||
|
||||
shortcutManager.registerShortcut('Ctrl+T', callback2);
|
||||
|
||||
expect(globalShortcut.unregister).toHaveBeenCalledWith('Ctrl+T');
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Ctrl+T', callback2);
|
||||
});
|
||||
|
||||
it('should handle registration failure', () => {
|
||||
const callback = vi.fn();
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(false);
|
||||
|
||||
const result = shortcutManager.registerShortcut('Ctrl+T', callback);
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(shortcutManager['shortcuts'].has('Ctrl+T')).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle registration errors', () => {
|
||||
const callback = vi.fn();
|
||||
vi.mocked(globalShortcut.register).mockImplementation(() => {
|
||||
throw new Error('Registration error');
|
||||
});
|
||||
|
||||
const result = shortcutManager.registerShortcut('Ctrl+T', callback);
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unregisterShortcut', () => {
|
||||
it('should unregister shortcut successfully', () => {
|
||||
const callback = vi.fn();
|
||||
shortcutManager['shortcuts'].set('Ctrl+T', callback);
|
||||
|
||||
shortcutManager.unregisterShortcut('Ctrl+T');
|
||||
|
||||
expect(globalShortcut.unregister).toHaveBeenCalledWith('Ctrl+T');
|
||||
expect(shortcutManager['shortcuts'].has('Ctrl+T')).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle unregistration errors', () => {
|
||||
vi.mocked(globalShortcut.unregister).mockImplementation(() => {
|
||||
throw new Error('Unregister error');
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
expect(() => shortcutManager.unregisterShortcut('Ctrl+T')).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isRegistered', () => {
|
||||
it('should check if shortcut is registered', () => {
|
||||
vi.mocked(globalShortcut.isRegistered).mockReturnValue(true);
|
||||
|
||||
const result = shortcutManager.isRegistered('Ctrl+T');
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(globalShortcut.isRegistered).toHaveBeenCalledWith('Ctrl+T');
|
||||
});
|
||||
});
|
||||
|
||||
describe('unregisterAll', () => {
|
||||
it('should unregister all shortcuts', () => {
|
||||
shortcutManager.unregisterAll();
|
||||
|
||||
expect(globalShortcut.unregisterAll).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadShortcutsConfig', () => {
|
||||
it('should use defaults when no config exists', () => {
|
||||
mockStoreManager.get.mockReturnValue(null);
|
||||
|
||||
shortcutManager['loadShortcutsConfig']();
|
||||
|
||||
expect(shortcutManager['shortcutsConfig']).toEqual(DEFAULT_SHORTCUTS_CONFIG);
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith('shortcuts', DEFAULT_SHORTCUTS_CONFIG);
|
||||
});
|
||||
|
||||
it('should use defaults when config is empty', () => {
|
||||
mockStoreManager.get.mockReturnValue({});
|
||||
|
||||
shortcutManager['loadShortcutsConfig']();
|
||||
|
||||
expect(shortcutManager['shortcutsConfig']).toEqual(DEFAULT_SHORTCUTS_CONFIG);
|
||||
});
|
||||
|
||||
it('should filter invalid keys from stored config', () => {
|
||||
const storedConfig = {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+P',
|
||||
invalidKey1: 'Ctrl+I',
|
||||
invalidKey2: 'Ctrl+J',
|
||||
};
|
||||
mockStoreManager.get.mockReturnValue(storedConfig);
|
||||
|
||||
shortcutManager['loadShortcutsConfig']();
|
||||
|
||||
const config = shortcutManager['shortcutsConfig'];
|
||||
expect(config.showApp).toBe('Alt+E');
|
||||
expect(config.openSettings).toBe('Ctrl+P');
|
||||
expect(config.invalidKey1).toBeUndefined();
|
||||
expect(config.invalidKey2).toBeUndefined();
|
||||
|
||||
// Should save filtered config
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith('shortcuts', config);
|
||||
});
|
||||
|
||||
it('should add missing default shortcuts', () => {
|
||||
const incompleteConfig = {
|
||||
showApp: 'Alt+E',
|
||||
// Missing openSettings
|
||||
};
|
||||
mockStoreManager.get.mockReturnValue(incompleteConfig);
|
||||
|
||||
shortcutManager['loadShortcutsConfig']();
|
||||
|
||||
const config = shortcutManager['shortcutsConfig'];
|
||||
expect(config.showApp).toBe('Alt+E');
|
||||
expect(config.openSettings).toBe('CommandOrControl+,'); // Default value
|
||||
});
|
||||
|
||||
it('should not save config if no invalid keys were found', () => {
|
||||
const validConfig = {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+P',
|
||||
};
|
||||
mockStoreManager.get.mockReturnValue(validConfig);
|
||||
|
||||
shortcutManager['loadShortcutsConfig']();
|
||||
|
||||
// Should not call set since no changes were made
|
||||
expect(mockStoreManager.set).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle store errors gracefully', () => {
|
||||
mockStoreManager.get.mockImplementation(() => {
|
||||
throw new Error('Store error');
|
||||
});
|
||||
|
||||
shortcutManager['loadShortcutsConfig']();
|
||||
|
||||
expect(shortcutManager['shortcutsConfig']).toEqual(DEFAULT_SHORTCUTS_CONFIG);
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith('shortcuts', DEFAULT_SHORTCUTS_CONFIG);
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveShortcutsConfig', () => {
|
||||
it('should save shortcuts config to store', () => {
|
||||
shortcutManager['shortcutsConfig'] = { showApp: 'Alt+E', openSettings: 'Ctrl+P' };
|
||||
|
||||
shortcutManager['saveShortcutsConfig']();
|
||||
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith('shortcuts', {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+P',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle save errors gracefully', () => {
|
||||
mockStoreManager.set.mockImplementation(() => {
|
||||
throw new Error('Save error');
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
expect(() => shortcutManager['saveShortcutsConfig']()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('registerConfiguredShortcuts', () => {
|
||||
beforeEach(() => {
|
||||
shortcutManager['shortcutsConfig'] = {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+P',
|
||||
};
|
||||
});
|
||||
|
||||
it('should register all configured shortcuts', () => {
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(true);
|
||||
|
||||
shortcutManager['registerConfiguredShortcuts']();
|
||||
|
||||
expect(globalShortcut.unregisterAll).toHaveBeenCalled();
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Alt+E', expect.any(Function));
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Ctrl+P', expect.any(Function));
|
||||
});
|
||||
|
||||
it('should skip shortcuts not in DEFAULT_SHORTCUTS_CONFIG', () => {
|
||||
shortcutManager['shortcutsConfig'] = {
|
||||
showApp: 'Alt+E',
|
||||
invalidKey: 'Ctrl+I',
|
||||
};
|
||||
|
||||
shortcutManager['registerConfiguredShortcuts']();
|
||||
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Alt+E', expect.any(Function));
|
||||
expect(globalShortcut.register).not.toHaveBeenCalledWith('Ctrl+I', expect.any(Function));
|
||||
});
|
||||
|
||||
it('should skip shortcuts with empty accelerator', () => {
|
||||
shortcutManager['shortcutsConfig'] = {
|
||||
showApp: '',
|
||||
openSettings: 'Ctrl+P',
|
||||
};
|
||||
|
||||
shortcutManager['registerConfiguredShortcuts']();
|
||||
|
||||
expect(globalShortcut.register).not.toHaveBeenCalledWith('', expect.any(Function));
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Ctrl+P', expect.any(Function));
|
||||
});
|
||||
|
||||
it('should skip shortcuts without corresponding methods', () => {
|
||||
// Remove method from map
|
||||
mockShortcutMethodMap.delete('openSettings');
|
||||
shortcutManager = new ShortcutManager(mockApp);
|
||||
shortcutManager['shortcutsConfig'] = {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+P',
|
||||
};
|
||||
|
||||
shortcutManager['registerConfiguredShortcuts']();
|
||||
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith('Alt+E', expect.any(Function));
|
||||
expect(globalShortcut.register).not.toHaveBeenCalledWith('Ctrl+P', expect.any(Function));
|
||||
});
|
||||
});
|
||||
|
||||
describe('integration tests', () => {
|
||||
it('should complete full initialization flow', () => {
|
||||
const storedConfig = {
|
||||
showApp: 'Alt+E',
|
||||
openSettings: 'Ctrl+Shift+P',
|
||||
invalidKey: 'Ctrl+I',
|
||||
};
|
||||
mockStoreManager.get.mockReturnValue(storedConfig);
|
||||
vi.mocked(globalShortcut.register).mockReturnValue(true);
|
||||
|
||||
shortcutManager.initialize();
|
||||
|
||||
// Should filter config and register valid shortcuts
|
||||
const config = shortcutManager.getShortcutsConfig();
|
||||
expect(config.showApp).toBe('Alt+E');
|
||||
expect(config.openSettings).toBe('Ctrl+Shift+P');
|
||||
expect(config.invalidKey).toBeUndefined();
|
||||
|
||||
expect(globalShortcut.register).toHaveBeenCalledTimes(2);
|
||||
expect(mockStoreManager.set).toHaveBeenCalledWith('shortcuts', config);
|
||||
});
|
||||
|
||||
it('should handle complete update workflow', () => {
|
||||
mockStoreManager.get.mockReturnValue({});
|
||||
shortcutManager.initialize();
|
||||
|
||||
// Update a shortcut
|
||||
const result = shortcutManager.updateShortcutConfig('showApp', 'mod+alt+e');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
// Should convert format and register
|
||||
const config = shortcutManager.getShortcutsConfig();
|
||||
expect(config.showApp).toBe('CommandOrControl+alt+E');
|
||||
|
||||
// Should have saved and re-registered shortcuts
|
||||
expect(mockStoreManager.set).toHaveBeenCalled();
|
||||
expect(globalShortcut.unregisterAll).toHaveBeenCalled();
|
||||
expect(globalShortcut.register).toHaveBeenCalledWith(
|
||||
'CommandOrControl+alt+E',
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,116 +0,0 @@
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
import { Agent, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from 'undici';
|
||||
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { ProxyUrlBuilder } from './urlBuilder';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('modules:networkProxy:dispatcher');
|
||||
|
||||
/**
|
||||
* 代理管理器
|
||||
*/
|
||||
export class ProxyDispatcherManager {
|
||||
private static isChanging = false;
|
||||
private static changeQueue: Array<() => Promise<void>> = [];
|
||||
|
||||
/**
|
||||
* 应用代理设置(带并发控制)
|
||||
*/
|
||||
static async applyProxySettings(config: NetworkProxySettings): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const operation = async () => {
|
||||
try {
|
||||
await this.doApplyProxySettings(config);
|
||||
resolve();
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
if (this.isChanging) {
|
||||
// 如果正在切换,加入队列
|
||||
this.changeQueue.push(operation);
|
||||
} else {
|
||||
// 立即执行
|
||||
operation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行代理设置应用
|
||||
*/
|
||||
private static async doApplyProxySettings(config: NetworkProxySettings): Promise<void> {
|
||||
this.isChanging = true;
|
||||
|
||||
try {
|
||||
const currentDispatcher = getGlobalDispatcher();
|
||||
|
||||
// 禁用代理,恢复默认连接
|
||||
if (!config.enableProxy) {
|
||||
await this.safeDestroyDispatcher(currentDispatcher);
|
||||
// 创建一个新的默认 Agent 来替代代理
|
||||
setGlobalDispatcher(new Agent());
|
||||
logger.debug('Proxy disabled, reset to direct connection mode');
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建代理 URL
|
||||
const proxyUrl = ProxyUrlBuilder.build(config);
|
||||
|
||||
// 创建代理 agent
|
||||
const agent = this.createProxyAgent(config.proxyType, proxyUrl);
|
||||
|
||||
// 切换代理前销毁旧 dispatcher
|
||||
await this.safeDestroyDispatcher(currentDispatcher);
|
||||
setGlobalDispatcher(agent);
|
||||
|
||||
logger.info(
|
||||
`Proxy settings applied: ${config.proxyType}://${config.proxyServer}:${config.proxyPort}`,
|
||||
);
|
||||
logger.debug(
|
||||
'Global request proxy set, all Node.js network requests will go through this proxy',
|
||||
);
|
||||
} finally {
|
||||
this.isChanging = false;
|
||||
|
||||
// 处理队列中的下一个操作
|
||||
if (this.changeQueue.length > 0) {
|
||||
const nextOperation = this.changeQueue.shift();
|
||||
if (nextOperation) {
|
||||
setTimeout(() => nextOperation(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理 agent
|
||||
*/
|
||||
static createProxyAgent(proxyType: string, proxyUrl: string) {
|
||||
try {
|
||||
// undici 的 ProxyAgent 支持 http, https 和 socks5
|
||||
return new ProxyAgent({ uri: proxyUrl });
|
||||
} catch (error) {
|
||||
logger.error(`Failed to create proxy agent for ${proxyType}:`, error);
|
||||
throw new Error(
|
||||
`Failed to create proxy agent: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全销毁 dispatcher
|
||||
*/
|
||||
private static async safeDestroyDispatcher(dispatcher: any): Promise<void> {
|
||||
try {
|
||||
if (dispatcher && typeof dispatcher.destroy === 'function') {
|
||||
await dispatcher.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('Failed to destroy dispatcher:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export { ProxyDispatcherManager } from './dispatcher';
|
||||
export type { ProxyTestResult } from './tester';
|
||||
export { ProxyConnectionTester } from './tester';
|
||||
export { ProxyUrlBuilder } from './urlBuilder';
|
||||
export type { ProxyValidationResult } from './validator';
|
||||
export { ProxyConfigValidator } from './validator';
|
||||
@@ -1,163 +0,0 @@
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
import { fetch, getGlobalDispatcher, setGlobalDispatcher } from 'undici';
|
||||
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { ProxyDispatcherManager } from './dispatcher';
|
||||
import { ProxyUrlBuilder } from './urlBuilder';
|
||||
import { ProxyConfigValidator } from './validator';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('modules:networkProxy:tester');
|
||||
|
||||
/**
|
||||
* 代理连接测试结果
|
||||
*/
|
||||
export interface ProxyTestResult {
|
||||
message?: string;
|
||||
responseTime?: number;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理连接测试器
|
||||
*/
|
||||
export class ProxyConnectionTester {
|
||||
private static readonly DEFAULT_TIMEOUT = 10_000; // 10秒超时
|
||||
private static readonly DEFAULT_TEST_URL = 'https://www.google.com';
|
||||
|
||||
/**
|
||||
* 测试代理连接
|
||||
*/
|
||||
static async testConnection(
|
||||
url: string = this.DEFAULT_TEST_URL,
|
||||
timeout: number = this.DEFAULT_TIMEOUT,
|
||||
): Promise<ProxyTestResult> {
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
logger.info(`Testing proxy connection with URL: ${url}`);
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'LobeChat-Desktop/1.0.0',
|
||||
},
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
logger.info(`Proxy connection test successful, response time: ${responseTime}ms`);
|
||||
|
||||
return {
|
||||
responseTime,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
const responseTime = Date.now() - startTime;
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.error(`Proxy connection test failed after ${responseTime}ms:`, errorMessage);
|
||||
|
||||
return {
|
||||
message: errorMessage,
|
||||
responseTime,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试指定代理配置的连接
|
||||
*/
|
||||
static async testProxyConfig(
|
||||
config: NetworkProxySettings,
|
||||
testUrl: string = this.DEFAULT_TEST_URL,
|
||||
): Promise<ProxyTestResult> {
|
||||
// 验证配置
|
||||
const validation = ProxyConfigValidator.validate(config);
|
||||
if (!validation.isValid) {
|
||||
return {
|
||||
message: `Invalid proxy configuration: ${validation.errors.join(', ')}`,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
// 如果未启用代理,直接测试
|
||||
if (!config.enableProxy) {
|
||||
return this.testConnection(testUrl);
|
||||
}
|
||||
|
||||
// 创建临时代理 agent 进行测试
|
||||
try {
|
||||
const proxyUrl = ProxyUrlBuilder.build(config);
|
||||
logger.debug(`Testing proxy with URL: ${proxyUrl}`);
|
||||
|
||||
const agent = ProxyDispatcherManager.createProxyAgent(config.proxyType, proxyUrl);
|
||||
|
||||
const startTime = Date.now();
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), this.DEFAULT_TIMEOUT);
|
||||
|
||||
// 临时设置代理进行测试
|
||||
const originalDispatcher = getGlobalDispatcher();
|
||||
setGlobalDispatcher(agent);
|
||||
|
||||
try {
|
||||
const response = await fetch(testUrl, {
|
||||
dispatcher: agent,
|
||||
headers: {
|
||||
'User-Agent': 'LobeChat-Desktop/1.0.0',
|
||||
},
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseTime = Date.now() - startTime;
|
||||
logger.info(`Proxy test successful, response time: ${responseTime}ms`);
|
||||
|
||||
return {
|
||||
responseTime,
|
||||
success: true,
|
||||
};
|
||||
} catch (fetchError) {
|
||||
clearTimeout(timeoutId);
|
||||
throw fetchError;
|
||||
} finally {
|
||||
// 恢复原来的 dispatcher
|
||||
setGlobalDispatcher(originalDispatcher);
|
||||
// 清理临时创建的代理 agent
|
||||
if (agent && typeof agent.destroy === 'function') {
|
||||
try {
|
||||
await agent.destroy();
|
||||
} catch (error) {
|
||||
logger.warn('Failed to destroy test agent:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
|
||||
logger.error(`Proxy test failed: ${errorMessage}`, error);
|
||||
|
||||
return {
|
||||
message: `Proxy test failed: ${errorMessage}`,
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
|
||||
/**
|
||||
* 代理 URL 构建器
|
||||
*/
|
||||
export const ProxyUrlBuilder = {
|
||||
/**
|
||||
* 构建代理 URL
|
||||
*/
|
||||
build(config: NetworkProxySettings): string {
|
||||
const { proxyType, proxyServer, proxyPort, proxyRequireAuth, proxyUsername, proxyPassword } =
|
||||
config;
|
||||
|
||||
let proxyUrl = `${proxyType}://${proxyServer}:${proxyPort}`;
|
||||
|
||||
// 添加认证信息
|
||||
if (proxyRequireAuth && proxyUsername && proxyPassword) {
|
||||
const encodedUsername = encodeURIComponent(proxyUsername);
|
||||
const encodedPassword = encodeURIComponent(proxyPassword);
|
||||
proxyUrl = `${proxyType}://${encodedUsername}:${encodedPassword}@${proxyServer}:${proxyPort}`;
|
||||
}
|
||||
|
||||
return proxyUrl;
|
||||
},
|
||||
};
|
||||
@@ -1,80 +0,0 @@
|
||||
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
|
||||
/**
|
||||
* 代理配置验证结果
|
||||
*/
|
||||
export interface ProxyValidationResult {
|
||||
errors: string[];
|
||||
isValid: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理配置验证器
|
||||
*/
|
||||
export class ProxyConfigValidator {
|
||||
private static readonly SUPPORTED_TYPES = ['http', 'https', 'socks5'] as const;
|
||||
private static readonly DEFAULT_BYPASS = 'localhost,127.0.0.1,::1';
|
||||
|
||||
/**
|
||||
* 验证代理配置
|
||||
*/
|
||||
static validate(config: NetworkProxySettings): ProxyValidationResult {
|
||||
const errors: string[] = [];
|
||||
|
||||
// 如果未启用代理,跳过验证
|
||||
if (!config.enableProxy) {
|
||||
return { errors: [], isValid: true };
|
||||
}
|
||||
|
||||
// 验证代理类型
|
||||
if (!this.SUPPORTED_TYPES.includes(config.proxyType as any)) {
|
||||
errors.push(
|
||||
`Unsupported proxy type: ${config.proxyType}. Supported types: ${this.SUPPORTED_TYPES.join(', ')}`,
|
||||
);
|
||||
}
|
||||
|
||||
// 验证代理服务器
|
||||
if (!config.proxyServer?.trim()) {
|
||||
errors.push('Proxy server is required when proxy is enabled');
|
||||
} else if (!this.isValidHost(config.proxyServer)) {
|
||||
errors.push('Invalid proxy server format');
|
||||
}
|
||||
|
||||
// 验证代理端口
|
||||
if (!config.proxyPort?.trim()) {
|
||||
errors.push('Proxy port is required when proxy is enabled');
|
||||
} else {
|
||||
const port = parseInt(config.proxyPort, 10);
|
||||
if (isNaN(port) || port < 1 || port > 65_535) {
|
||||
errors.push('Proxy port must be a valid number between 1 and 65535');
|
||||
}
|
||||
}
|
||||
|
||||
// 验证认证信息
|
||||
if (config.proxyRequireAuth) {
|
||||
if (!config.proxyUsername?.trim()) {
|
||||
errors.push('Proxy username is required when authentication is enabled');
|
||||
}
|
||||
if (!config.proxyPassword?.trim()) {
|
||||
errors.push('Proxy password is required when authentication is enabled');
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
errors,
|
||||
isValid: errors.length === 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证主机名格式
|
||||
*/
|
||||
private static isValidHost(host: string): boolean {
|
||||
// 简单的主机名验证(IP 地址或域名)
|
||||
const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
|
||||
const domainRegex =
|
||||
/^[\dA-Za-z]([\dA-Za-z-]*[\dA-Za-z])?(\.[\dA-Za-z]([\dA-Za-z-]*[\dA-Za-z])?)*$/;
|
||||
|
||||
return ipRegex.test(host) || domainRegex.test(host);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,15 @@
|
||||
import { DeleteFilesResponse } from '@lobechat/electron-server-ipc';
|
||||
import * as fs from 'node:fs';
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import path, { join } from 'node:path';
|
||||
import { join } from 'node:path';
|
||||
import { promisify } from 'node:util';
|
||||
|
||||
import { FILE_STORAGE_DIR, LOCAL_STORAGE_URL_PREFIX } from '@/const/dir';
|
||||
import { FILE_STORAGE_DIR } from '@/const/dir';
|
||||
import { makeSureDirExist } from '@/utils/file-system';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
import { ServiceModule } from './index';
|
||||
|
||||
/**
|
||||
* 文件未找到错误类
|
||||
*/
|
||||
export class FileNotFoundError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public path: string,
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'FileNotFoundError';
|
||||
}
|
||||
}
|
||||
|
||||
const readFilePromise = promisify(fs.readFile);
|
||||
const unlinkPromise = promisify(fs.unlink);
|
||||
|
||||
@@ -30,7 +17,7 @@ const unlinkPromise = promisify(fs.unlink);
|
||||
const logger = createLogger('services:FileService');
|
||||
|
||||
interface UploadFileParams {
|
||||
content: ArrayBuffer | string; // ArrayBuffer from browser or Base64 string from server
|
||||
content: ArrayBuffer;
|
||||
filename: string;
|
||||
hash: string;
|
||||
path: string;
|
||||
@@ -45,16 +32,17 @@ interface FileMetadata {
|
||||
}
|
||||
|
||||
export default class FileService extends ServiceModule {
|
||||
/**
|
||||
* 获取旧版上传目录路径
|
||||
* @deprecated 仅用于向后兼容旧版文件访问,新文件应存储在 FILE_STORAGE_DIR 的自定义路径下
|
||||
*/
|
||||
get UPLOADS_DIR() {
|
||||
return join(this.app.appStoragePath, FILE_STORAGE_DIR, 'uploads');
|
||||
}
|
||||
|
||||
constructor(app) {
|
||||
super(app);
|
||||
|
||||
// Initialize file storage directory
|
||||
logger.info('Initializing file storage directory');
|
||||
makeSureDirExist(this.UPLOADS_DIR);
|
||||
logger.debug(`Upload directory created: ${this.UPLOADS_DIR}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,44 +52,31 @@ export default class FileService extends ServiceModule {
|
||||
content,
|
||||
filename,
|
||||
hash,
|
||||
path: filePath,
|
||||
type,
|
||||
}: UploadFileParams): Promise<{ metadata: FileMetadata; success: boolean }> {
|
||||
logger.info(`Starting to upload file: ${filename}, hash: ${hash}, path: ${filePath}`);
|
||||
logger.info(`Starting to upload file: ${filename}, hash: ${hash}`);
|
||||
try {
|
||||
// 获取当前时间戳,避免重复调用 Date.now()
|
||||
const now = Date.now();
|
||||
const date = (now / 1000 / 60 / 60).toFixed(0);
|
||||
// 创建时间戳目录
|
||||
const date = (Date.now() / 1000 / 60 / 60).toFixed(0);
|
||||
const dirname = join(this.UPLOADS_DIR, date);
|
||||
logger.debug(`Creating timestamp directory: ${dirname}`);
|
||||
makeSureDirExist(dirname);
|
||||
|
||||
// 使用传入的 filePath 作为文件的存储路径
|
||||
const fullStoragePath = join(this.app.appStoragePath, FILE_STORAGE_DIR, filePath);
|
||||
logger.debug(`Target file storage path: ${fullStoragePath}`);
|
||||
// 生成文件保存路径
|
||||
const fileExt = filename.split('.').pop() || '';
|
||||
const savedFilename = `${hash}${fileExt ? `.${fileExt}` : ''}`;
|
||||
const savedPath = join(dirname, savedFilename);
|
||||
logger.debug(`Generated file save path: ${savedPath}`);
|
||||
|
||||
// 确保目标目录存在
|
||||
const targetDir = path.dirname(fullStoragePath);
|
||||
logger.debug(`Ensuring target directory exists: ${targetDir}`);
|
||||
makeSureDirExist(targetDir);
|
||||
|
||||
const savedPath = fullStoragePath;
|
||||
logger.debug(`Final file save path: ${savedPath}`);
|
||||
|
||||
// 根据 content 类型创建 Buffer
|
||||
let buffer: Buffer;
|
||||
if (typeof content === 'string') {
|
||||
// 来自服务端的 Base64 字符串
|
||||
buffer = Buffer.from(content, 'base64');
|
||||
logger.debug(`Creating buffer from Base64 string, size: ${buffer.length} bytes`);
|
||||
} else {
|
||||
// 来自浏览器端的 ArrayBuffer
|
||||
buffer = Buffer.from(content);
|
||||
logger.debug(`Creating buffer from ArrayBuffer, size: ${buffer.length} bytes`);
|
||||
}
|
||||
// 写入文件内容
|
||||
const buffer = Buffer.from(content);
|
||||
logger.debug(`Writing file content, size: ${buffer.length} bytes`);
|
||||
await writeFile(savedPath, buffer);
|
||||
|
||||
// 写入元数据文件
|
||||
const metaFilePath = `${savedPath}.meta`;
|
||||
const metadata = {
|
||||
createdAt: now, // 使用统一的时间戳
|
||||
createdAt: Date.now(),
|
||||
filename,
|
||||
hash,
|
||||
size: buffer.length,
|
||||
@@ -111,18 +86,13 @@ export default class FileService extends ServiceModule {
|
||||
await writeFile(metaFilePath, JSON.stringify(metadata, null, 2));
|
||||
|
||||
// 返回与S3兼容的元数据格式
|
||||
const desktopPath = `desktop://${filePath}`;
|
||||
const desktopPath = `desktop://${date}/${savedFilename}`;
|
||||
logger.info(`File upload successful: ${desktopPath}`);
|
||||
|
||||
// 从路径中提取文件名和目录信息
|
||||
const parsedPath = path.parse(filePath);
|
||||
const dirname = parsedPath.dir || '';
|
||||
const savedFilename = parsedPath.base;
|
||||
|
||||
return {
|
||||
metadata: {
|
||||
date, // 保持时间戳格式,用于兼容性和时间追踪
|
||||
dirname,
|
||||
date,
|
||||
dirname: date,
|
||||
filename: savedFilename,
|
||||
path: desktopPath,
|
||||
},
|
||||
@@ -134,24 +104,6 @@ export default class FileService extends ServiceModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断路径是否为旧版格式(时间戳目录)
|
||||
*
|
||||
* 旧版路径格式: {timestamp}/{hash}.{ext} (例如: 1234567890/abc123.png)
|
||||
* 新版路径格式: 任意自定义路径 (例如: user_uploads/images/photo.png, ai_generations/image.jpg)
|
||||
*
|
||||
* @param path - 相对路径,不包含 desktop:// 前缀
|
||||
* @returns true 如果是旧版格式,false 如果是新版格式
|
||||
*/
|
||||
private isLegacyPath(path: string): boolean {
|
||||
const parts = path.split('/');
|
||||
if (parts.length < 2) return false;
|
||||
|
||||
// 如果第一部分是纯数字(时间戳),则认为是旧版格式
|
||||
// 时间戳格式:精确到小时的 Unix 时间戳,通常是 10 位数字
|
||||
return /^\d+$/.test(parts[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件内容
|
||||
*/
|
||||
@@ -171,49 +123,13 @@ export default class FileService extends ServiceModule {
|
||||
|
||||
// 解析路径
|
||||
const relativePath = normalizedPath.replace('desktop://', '');
|
||||
const filePath = join(this.UPLOADS_DIR, relativePath);
|
||||
logger.debug(`Reading file from path: ${filePath}`);
|
||||
|
||||
// 智能路由:根据路径格式决定从哪个目录读取文件
|
||||
let filePath: string;
|
||||
let isLegacyAttempt = false;
|
||||
|
||||
if (this.isLegacyPath(relativePath)) {
|
||||
// 旧版路径:从 uploads 目录读取(向后兼容)
|
||||
filePath = join(this.UPLOADS_DIR, relativePath);
|
||||
isLegacyAttempt = true;
|
||||
logger.debug(`Legacy path detected, reading from uploads directory: ${filePath}`);
|
||||
} else {
|
||||
// 新版路径:从 FILE_STORAGE_DIR 根目录读取
|
||||
filePath = join(this.app.appStoragePath, FILE_STORAGE_DIR, relativePath);
|
||||
logger.debug(`New path format, reading from storage root: ${filePath}`);
|
||||
}
|
||||
|
||||
// 读取文件内容,如果第一次尝试失败且是 legacy 路径,则尝试新路径
|
||||
// 读取文件内容
|
||||
logger.debug(`Starting to read file content`);
|
||||
let content: Buffer;
|
||||
try {
|
||||
content = await readFilePromise(filePath);
|
||||
logger.debug(`File content read complete, size: ${content.length} bytes`);
|
||||
} catch (firstError) {
|
||||
if (isLegacyAttempt) {
|
||||
// 如果是 legacy 路径读取失败,尝试从新路径读取
|
||||
const fallbackPath = join(this.app.appStoragePath, FILE_STORAGE_DIR, relativePath);
|
||||
logger.debug(
|
||||
`Legacy path read failed, attempting fallback to storage root: ${fallbackPath}`,
|
||||
);
|
||||
try {
|
||||
content = await readFilePromise(fallbackPath);
|
||||
filePath = fallbackPath; // 更新 filePath 用于后续的元数据读取
|
||||
logger.debug(`Fallback read successful, size: ${content.length} bytes`);
|
||||
} catch (fallbackError) {
|
||||
logger.error(
|
||||
`Both legacy and fallback paths failed. Legacy error: ${(firstError as Error).message}, Fallback error: ${(fallbackError as Error).message}`,
|
||||
);
|
||||
throw firstError; // 抛出原始错误
|
||||
}
|
||||
} else {
|
||||
throw firstError;
|
||||
}
|
||||
}
|
||||
const content = await readFilePromise(filePath);
|
||||
logger.debug(`File content read complete, size: ${content.length} bytes`);
|
||||
|
||||
// 读取元数据获取MIME类型
|
||||
const metaFilePath = `${filePath}.meta`;
|
||||
@@ -226,9 +142,7 @@ export default class FileService extends ServiceModule {
|
||||
mimeType = metadata.type || mimeType;
|
||||
logger.debug(`Got MIME type from metadata: ${mimeType}`);
|
||||
} catch (metaError) {
|
||||
logger.warn(
|
||||
`Failed to read metadata file: ${(metaError as Error).message}, using default MIME type`,
|
||||
);
|
||||
logger.warn(`Failed to read metadata file: ${(metaError as Error).message}, using default MIME type`);
|
||||
// 如果元数据文件不存在,尝试从文件扩展名猜测MIME类型
|
||||
const ext = path.split('.').pop()?.toLowerCase();
|
||||
if (ext) {
|
||||
@@ -270,12 +184,6 @@ export default class FileService extends ServiceModule {
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error(`File retrieval failed:`, error);
|
||||
|
||||
// 如果是文件不存在错误,抛出自定义的 FileNotFoundError
|
||||
if (error instanceof Error && error.message.includes('ENOENT')) {
|
||||
throw new FileNotFoundError(`File not found: ${path}`, path);
|
||||
}
|
||||
|
||||
throw new Error(`File retrieval failed: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
@@ -292,53 +200,15 @@ export default class FileService extends ServiceModule {
|
||||
throw new Error(`Invalid desktop file path: ${path}`);
|
||||
}
|
||||
|
||||
// 标准化路径格式
|
||||
const normalizedPath = path.replace(/^desktop:\/+/, 'desktop://');
|
||||
|
||||
// 解析路径
|
||||
const relativePath = normalizedPath.replace('desktop://', '');
|
||||
const relativePath = path.replace('desktop://', '');
|
||||
const filePath = join(this.UPLOADS_DIR, relativePath);
|
||||
logger.debug(`File deletion path: ${filePath}`);
|
||||
|
||||
// 智能路由:根据路径格式决定从哪个目录删除文件
|
||||
let filePath: string;
|
||||
let isLegacyAttempt = false;
|
||||
|
||||
if (this.isLegacyPath(relativePath)) {
|
||||
// 旧版路径:从 uploads 目录删除(向后兼容)
|
||||
filePath = join(this.UPLOADS_DIR, relativePath);
|
||||
isLegacyAttempt = true;
|
||||
logger.debug(`Legacy path detected, deleting from uploads directory: ${filePath}`);
|
||||
} else {
|
||||
// 新版路径:从 FILE_STORAGE_DIR 根目录删除
|
||||
filePath = join(this.app.appStoragePath, FILE_STORAGE_DIR, relativePath);
|
||||
logger.debug(`New path format, deleting from storage root: ${filePath}`);
|
||||
}
|
||||
|
||||
// 删除文件及其元数据,如果第一次尝试失败且是 legacy 路径,则尝试新路径
|
||||
// 删除文件及其元数据
|
||||
logger.debug(`Starting file deletion`);
|
||||
try {
|
||||
await unlinkPromise(filePath);
|
||||
logger.debug(`File deletion successful`);
|
||||
} catch (firstError) {
|
||||
if (isLegacyAttempt) {
|
||||
// 如果是 legacy 路径删除失败,尝试从新路径删除
|
||||
const fallbackPath = join(this.app.appStoragePath, FILE_STORAGE_DIR, relativePath);
|
||||
logger.debug(
|
||||
`Legacy path deletion failed, attempting fallback to storage root: ${fallbackPath}`,
|
||||
);
|
||||
try {
|
||||
await unlinkPromise(fallbackPath);
|
||||
filePath = fallbackPath; // 更新 filePath 用于后续的元数据删除
|
||||
logger.debug(`Fallback deletion successful`);
|
||||
} catch (fallbackError) {
|
||||
logger.error(
|
||||
`Both legacy and fallback deletion failed. Legacy error: ${(firstError as Error).message}, Fallback error: ${(fallbackError as Error).message}`,
|
||||
);
|
||||
throw firstError; // 抛出原始错误
|
||||
}
|
||||
} else {
|
||||
throw firstError;
|
||||
}
|
||||
}
|
||||
await unlinkPromise(filePath);
|
||||
logger.debug(`File deletion successful`);
|
||||
|
||||
// 尝试删除元数据文件,但不强制要求存在
|
||||
try {
|
||||
@@ -400,9 +270,7 @@ export default class FileService extends ServiceModule {
|
||||
});
|
||||
|
||||
const success = errors.length === 0;
|
||||
logger.info(
|
||||
`Batch deletion operation complete, success: ${success}, error count: ${errors.length}`,
|
||||
);
|
||||
logger.info(`Batch deletion operation complete, success: ${success}, error count: ${errors.length}`);
|
||||
return {
|
||||
success,
|
||||
...(errors.length > 0 && { errors }),
|
||||
@@ -417,65 +285,10 @@ export default class FileService extends ServiceModule {
|
||||
throw new Error(`Invalid desktop file path: ${path}`);
|
||||
}
|
||||
|
||||
// 标准化路径格式
|
||||
const normalizedPath = path.replace(/^desktop:\/+/, 'desktop://');
|
||||
|
||||
// 解析路径
|
||||
const relativePath = normalizedPath.replace('desktop://', '');
|
||||
|
||||
// 智能路由:根据路径格式决定从哪个目录获取文件路径
|
||||
let fullPath: string;
|
||||
if (this.isLegacyPath(relativePath)) {
|
||||
// 旧版路径:从 uploads 目录获取(向后兼容)
|
||||
fullPath = join(this.UPLOADS_DIR, relativePath);
|
||||
logger.debug(`Legacy path detected, resolved to uploads directory: ${fullPath}`);
|
||||
|
||||
// 检查文件是否存在,如果不存在则尝试新路径
|
||||
try {
|
||||
await fs.promises.access(fullPath, fs.constants.F_OK);
|
||||
logger.debug(`Legacy path file exists: ${fullPath}`);
|
||||
} catch {
|
||||
// 如果 legacy 路径文件不存在,尝试新路径
|
||||
const fallbackPath = join(this.app.appStoragePath, FILE_STORAGE_DIR, relativePath);
|
||||
logger.debug(`Legacy path file not found, trying fallback path: ${fallbackPath}`);
|
||||
try {
|
||||
await fs.promises.access(fallbackPath, fs.constants.F_OK);
|
||||
fullPath = fallbackPath;
|
||||
logger.debug(`Fallback path file exists: ${fullPath}`);
|
||||
} catch {
|
||||
// 两个路径都不存在,返回原始的 legacy 路径(保持原有行为)
|
||||
logger.debug(
|
||||
`Neither legacy nor fallback path exists, returning legacy path: ${fullPath}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 新版路径:从 FILE_STORAGE_DIR 根目录获取
|
||||
fullPath = join(this.app.appStoragePath, FILE_STORAGE_DIR, relativePath);
|
||||
logger.debug(`New path format, resolved to storage root: ${fullPath}`);
|
||||
}
|
||||
|
||||
const relativePath = path.replace('desktop://', '');
|
||||
const fullPath = join(this.UPLOADS_DIR, relativePath);
|
||||
logger.debug(`Resolved filesystem path: ${fullPath}`);
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
async getFileHTTPURL(path: string): Promise<string> {
|
||||
logger.debug(`Getting file HTTP URL: ${path}`);
|
||||
// 处理desktop://路径
|
||||
if (!path.startsWith('desktop://')) {
|
||||
logger.error(`Invalid desktop file path: ${path}`);
|
||||
throw new Error(`Invalid desktop file path: ${path}`);
|
||||
}
|
||||
|
||||
// 标准化路径格式
|
||||
const normalizedPath = path.replace(/^desktop:\/+/, 'desktop://');
|
||||
|
||||
// 解析路径:从 desktop://path/to/file.png 中提取 path/to/file.png
|
||||
const relativePath = normalizedPath.replace('desktop://', '');
|
||||
|
||||
// 使用 StaticFileServerManager 获取文件服务器域名,然后构建完整 URL
|
||||
const serverDomain = this.app.staticFileServerManager.getFileServerDomain();
|
||||
const httpURL = `${serverDomain}${LOCAL_STORAGE_URL_PREFIX}/${relativePath}`;
|
||||
logger.debug(`Generated HTTP URL: ${httpURL}`);
|
||||
return httpURL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,10 @@
|
||||
* 快捷键操作类型枚举
|
||||
*/
|
||||
export const ShortcutActionEnum = {
|
||||
openSettings: 'openSettings',
|
||||
/**
|
||||
* 显示/隐藏主窗口
|
||||
*/
|
||||
showApp: 'showApp',
|
||||
toggleMainWindow: 'toggleMainWindow',
|
||||
} as const;
|
||||
|
||||
export type ShortcutActionType = (typeof ShortcutActionEnum)[keyof typeof ShortcutActionEnum];
|
||||
@@ -15,6 +14,5 @@ export type ShortcutActionType = (typeof ShortcutActionEnum)[keyof typeof Shortc
|
||||
* 默认快捷键配置
|
||||
*/
|
||||
export const DEFAULT_SHORTCUTS_CONFIG: Record<ShortcutActionType, string> = {
|
||||
[ShortcutActionEnum.showApp]: 'Control+E',
|
||||
[ShortcutActionEnum.openSettings]: 'CommandOrControl+,',
|
||||
[ShortcutActionEnum.toggleMainWindow]: 'CommandOrControl+E',
|
||||
};
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
import { DataSyncConfig, NetworkProxySettings } from '@lobechat/electron-client-ipc';
|
||||
import { DataSyncConfig } from '@lobechat/electron-client-ipc';
|
||||
|
||||
export interface ElectronMainStore {
|
||||
dataSyncConfig: DataSyncConfig;
|
||||
encryptedTokens: {
|
||||
accessToken?: string;
|
||||
expiresAt?: number;
|
||||
refreshToken?: string;
|
||||
};
|
||||
locale: string;
|
||||
networkProxy: NetworkProxySettings;
|
||||
shortcuts: Record<string, string>;
|
||||
storagePath: string;
|
||||
themeMode: 'dark' | 'light' | 'auto';
|
||||
}
|
||||
|
||||
export type StoreKey = keyof ElectronMainStore;
|
||||
|
||||
@@ -19,9 +19,8 @@ export const createLogger = (namespace: string) => {
|
||||
error: (message, ...args) => {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
electronLog.error(message, ...args);
|
||||
} else {
|
||||
console.error(message, ...args);
|
||||
}
|
||||
debugLogger(`ERROR: ${message}`, ...args);
|
||||
},
|
||||
info: (message, ...args) => {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// copy from https://github.com/kirill-konshin/next-electron-rsc
|
||||
import { serialize as serializeCookie } from 'cookie';
|
||||
import { type Protocol, type Session, protocol } from 'electron';
|
||||
import type { Protocol, Session } from 'electron';
|
||||
import type { NextConfig } from 'next';
|
||||
import type NextNodeServer from 'next/dist/server/next-server';
|
||||
import assert from 'node:assert';
|
||||
@@ -11,7 +11,6 @@ import { parse } from 'node:url';
|
||||
import resolve from 'resolve';
|
||||
import { parse as parseCookie, splitCookiesString } from 'set-cookie-parser';
|
||||
|
||||
import { LOCAL_STORAGE_URL_PREFIX } from '@/const/dir';
|
||||
import { isDev } from '@/const/env';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
|
||||
@@ -59,7 +58,7 @@ export const createRequest = async ({
|
||||
|
||||
for (const cookie of cookies) {
|
||||
const { name, value } = cookie;
|
||||
cookiesHeader.push(serializeCookie(name, value));
|
||||
cookiesHeader.push(serializeCookie(name, value)); // ...(options as any)?
|
||||
}
|
||||
|
||||
req.headers.cookie = cookiesHeader.join('; ');
|
||||
@@ -179,9 +178,7 @@ export function createHandler({
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let registerProtocolHandle = false;
|
||||
let interceptorCount = 0; // 追踪活跃的拦截器数量
|
||||
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{
|
||||
@@ -227,14 +224,6 @@ export function createHandler({
|
||||
socket: Socket,
|
||||
): Promise<Response> => {
|
||||
try {
|
||||
// 检查是否是本地文件服务请求,如果是则跳过处理
|
||||
const url = new URL(request.url);
|
||||
if (url.pathname.startsWith(LOCAL_STORAGE_URL_PREFIX + '/')) {
|
||||
if (debug) logger.debug(`Skipping local file service request: ${request.url}`);
|
||||
// 直接使用 fetch 转发请求到本地文件服务
|
||||
return fetch(request);
|
||||
}
|
||||
|
||||
// 先尝试使用自定义处理器处理请求
|
||||
for (const customHandler of customHandlers) {
|
||||
try {
|
||||
@@ -316,27 +305,20 @@ export function createHandler({
|
||||
);
|
||||
|
||||
for (const cookie of cookies) {
|
||||
let expirationDate: number | undefined;
|
||||
const expires = cookie.expires
|
||||
? cookie.expires.getTime()
|
||||
: cookie.maxAge
|
||||
? Date.now() + cookie.maxAge * 1000
|
||||
: undefined;
|
||||
|
||||
if (cookie.expires) {
|
||||
// expires 是 Date 对象,转换为秒级时间戳
|
||||
expirationDate = Math.floor(cookie.expires.getTime() / 1000);
|
||||
} else if (cookie.maxAge) {
|
||||
// maxAge 是秒数,计算过期时间戳
|
||||
expirationDate = Math.floor(Date.now() / 1000) + cookie.maxAge;
|
||||
}
|
||||
|
||||
// 如果都没有,则为 session cookie,不设置 expirationDate
|
||||
|
||||
// 检查是否已过期
|
||||
if (expirationDate && expirationDate < Math.floor(Date.now() / 1000)) {
|
||||
if (expires && expires < Date.now()) {
|
||||
await session.cookies.remove(request.url, cookie.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
await session.cookies.set({
|
||||
domain: cookie.domain,
|
||||
expirationDate,
|
||||
expirationDate: expires,
|
||||
httpOnly: cookie.httpOnly,
|
||||
name: cookie.name,
|
||||
path: cookie.path,
|
||||
@@ -365,32 +347,19 @@ export function createHandler({
|
||||
);
|
||||
|
||||
const socket = new Socket();
|
||||
interceptorCount++; // 增加拦截器计数
|
||||
|
||||
const closeSocket = () => socket.end();
|
||||
|
||||
process.on('SIGTERM', () => closeSocket);
|
||||
process.on('SIGINT', () => closeSocket);
|
||||
|
||||
if (!registerProtocolHandle) {
|
||||
if (!isDev && !registerProtocolHandle) {
|
||||
logger.debug(
|
||||
`Registering HTTP protocol handler in ${isDev ? 'development' : 'production'} mode`,
|
||||
);
|
||||
protocol.handle('http', async (request) => {
|
||||
if (!isDev) {
|
||||
// 检查是否是本地文件服务请求,如果是则允许通过
|
||||
const isLocalhost = request.url.startsWith(localhostUrl);
|
||||
|
||||
const url = new URL(request.url);
|
||||
const isLocalIP =
|
||||
request.url.startsWith('http://127.0.0.1:') ||
|
||||
request.url.startsWith('http://localhost:');
|
||||
const isLocalFileService = url.pathname.startsWith(LOCAL_STORAGE_URL_PREFIX + '/');
|
||||
|
||||
const valid = isLocalhost || (isLocalIP && isLocalFileService);
|
||||
if (!valid) {
|
||||
throw new Error('External HTTP not supported, use HTTPS');
|
||||
}
|
||||
assert(request.url.startsWith(localhostUrl), 'External HTTP not supported, use HTTPS');
|
||||
}
|
||||
|
||||
return handleRequest(request, session, socket);
|
||||
@@ -398,19 +367,12 @@ export function createHandler({
|
||||
registerProtocolHandle = true;
|
||||
}
|
||||
|
||||
logger.debug(`Active interceptors count: ${interceptorCount}`);
|
||||
|
||||
return function stopIntercept() {
|
||||
interceptorCount--; // 减少拦截器计数
|
||||
logger.debug(`Stopping interceptor, remaining count: ${interceptorCount}`);
|
||||
|
||||
// 只有当没有活跃的拦截器时才取消注册协议处理器
|
||||
if (registerProtocolHandle && interceptorCount === 0) {
|
||||
logger.debug('Unregistering HTTP protocol handler (no active interceptors)');
|
||||
if (registerProtocolHandle) {
|
||||
logger.debug('Unregistering HTTP protocol handler');
|
||||
protocol.unhandle('http');
|
||||
registerProtocolHandle = false;
|
||||
}
|
||||
|
||||
process.off('SIGTERM', () => closeSocket);
|
||||
process.off('SIGINT', () => closeSocket);
|
||||
closeSocket();
|
||||
|
||||
@@ -2,7 +2,6 @@ import { electronAPI } from '@electron-toolkit/preload';
|
||||
import { contextBridge } from 'electron';
|
||||
|
||||
import { invoke } from './invoke';
|
||||
import { onStreamInvoke } from './streamer';
|
||||
|
||||
export const setupElectronApi = () => {
|
||||
// Use `contextBridge` APIs to expose Electron APIs to
|
||||
@@ -15,5 +14,5 @@ export const setupElectronApi = () => {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', { invoke, onStreamInvoke });
|
||||
contextBridge.exposeInMainWorld('electronAPI', { invoke });
|
||||
};
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import type { ProxyTRPCRequestParams } from '@lobechat/electron-client-ipc';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
interface StreamResponse {
|
||||
headers: Record<string, string>;
|
||||
status: number;
|
||||
statusText: string;
|
||||
}
|
||||
|
||||
export interface StreamerCallbacks {
|
||||
onData: (chunk: Uint8Array) => void;
|
||||
onEnd: () => void;
|
||||
onError: (error: Error) => void;
|
||||
onResponse: (response: StreamResponse) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the main process method and handles the stream response via callbacks.
|
||||
* @param params The request parameters.
|
||||
* @param callbacks The callbacks to handle stream events.
|
||||
*/
|
||||
export const onStreamInvoke = (
|
||||
params: ProxyTRPCRequestParams,
|
||||
callbacks: StreamerCallbacks,
|
||||
): (() => void) => {
|
||||
const requestId = uuid();
|
||||
|
||||
const cleanup = () => {
|
||||
ipcRenderer.removeAllListeners(`stream:data:${requestId}`);
|
||||
ipcRenderer.removeAllListeners(`stream:end:${requestId}`);
|
||||
ipcRenderer.removeAllListeners(`stream:error:${requestId}`);
|
||||
ipcRenderer.removeAllListeners(`stream:response:${requestId}`);
|
||||
};
|
||||
|
||||
ipcRenderer.on(`stream:data:${requestId}`, (_, chunk: Buffer) => {
|
||||
callbacks.onData(new Uint8Array(chunk));
|
||||
});
|
||||
|
||||
ipcRenderer.once(`stream:end:${requestId}`, () => {
|
||||
callbacks.onEnd();
|
||||
cleanup();
|
||||
});
|
||||
|
||||
ipcRenderer.once(`stream:error:${requestId}`, (_, error: Error) => {
|
||||
callbacks.onError(error);
|
||||
cleanup();
|
||||
});
|
||||
|
||||
ipcRenderer.once(`stream:response:${requestId}`, (_, response: StreamResponse) => {
|
||||
callbacks.onResponse(response);
|
||||
});
|
||||
|
||||
ipcRenderer.send('stream:start', { ...params, requestId });
|
||||
|
||||
// Return a cleanup function to be called on cancellation
|
||||
return cleanup;
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
# Proxy, if you need it
|
||||
# HTTP_PROXY=http://localhost:7890
|
||||
# HTTPS_PROXY=http://localhost:7890
|
||||
|
||||
|
||||
# Other environment variables, as needed. You can refer to the environment variables configuration for the client version, making sure not to have ACCESS_CODE.
|
||||
# OPENAI_API_KEY=sk-xxxx
|
||||
# OPENAI_PROXY_URL=https://api.openai.com/v1
|
||||
# OPENAI_MODEL_LIST=...
|
||||
|
||||
|
||||
# ===========================
|
||||
# ====== Preset config ======
|
||||
# ===========================
|
||||
# if no special requirements, no need to change
|
||||
LOBE_PORT=3210
|
||||
CASDOOR_PORT=8000
|
||||
MINIO_PORT=9000
|
||||
APP_URL=http://localhost:3210
|
||||
AUTH_URL=http://localhost:3210/api/auth
|
||||
|
||||
# Postgres related, which are the necessary environment variables for DB
|
||||
LOBE_DB_NAME=lobechat
|
||||
POSTGRES_PASSWORD=uWNZugjBqixf8dxC
|
||||
|
||||
# MinIO S3 configuration
|
||||
MINIO_ROOT_USER=admin
|
||||
MINIO_ROOT_PASSWORD=YOUR_MINIO_PASSWORD
|
||||
|
||||
# Configure the bucket information of MinIO
|
||||
S3_PUBLIC_DOMAIN=http://localhost:9000
|
||||
S3_ENDPOINT=http://localhost:9000
|
||||
MINIO_LOBE_BUCKET=lobe
|
||||
|
||||
#Configure OIDC
|
||||
NEXT_AUTH_SSO_PROVIDERS=okta
|
||||
NEXTAUTH_URL=http://localhost:3210/api/auth
|
||||
AUTH_OKTA_ID=ndfvkndfk3489349
|
||||
AUTH_OKTA_SECRET=-98494-8d093498d_njkefiuJKSAK-KJ9843JNDSiud78932-JKDSU929
|
||||
AUTH_OKTA_ISSUER=https://dev1234.okta.com
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
## Set global build ENV
|
||||
ARG NODEJS_VERSION="22"
|
||||
|
||||
## Base image for all stages
|
||||
FROM node:${NODEJS_VERSION}-slim AS base
|
||||
|
||||
ARG USE_CN_MIRROR
|
||||
ARG NEXT_PUBLIC_BASE_PATH
|
||||
ARG NEXT_PUBLIC_SERVICE_MODE
|
||||
ARG NEXT_PUBLIC_ENABLE_NEXT_AUTH
|
||||
ARG NEXT_PUBLIC_SENTRY_DSN
|
||||
ARG NEXT_PUBLIC_ANALYTICS_POSTHOG
|
||||
ARG NEXT_PUBLIC_POSTHOG_HOST
|
||||
ARG NEXT_PUBLIC_POSTHOG_KEY
|
||||
ARG NEXT_PUBLIC_ANALYTICS_UMAMI
|
||||
ARG NEXT_PUBLIC_UMAMI_SCRIPT_URL
|
||||
ARG NEXT_PUBLIC_UMAMI_WEBSITE_ID
|
||||
|
||||
ENV DEBIAN_FRONTEND="noninteractive"
|
||||
|
||||
ENV NEXT_PUBLIC_BASE_PATH="${NEXT_PUBLIC_BASE_PATH}"
|
||||
|
||||
ENV NEXT_PUBLIC_SERVICE_MODE="${NEXT_PUBLIC_SERVICE_MODE:-server}" \
|
||||
NEXT_PUBLIC_ENABLE_NEXT_AUTH="${NEXT_PUBLIC_ENABLE_NEXT_AUTH:-1}" \
|
||||
APP_URL="http://app.com" \
|
||||
DATABASE_DRIVER="node" \
|
||||
DATABASE_URL="postgres://postgres:password@localhost:5432/postgres" \
|
||||
KEY_VAULTS_SECRET="use-for-build"
|
||||
|
||||
# Sentry
|
||||
ENV NEXT_PUBLIC_SENTRY_DSN="${NEXT_PUBLIC_SENTRY_DSN}" \
|
||||
SENTRY_ORG="" \
|
||||
SENTRY_PROJECT=""
|
||||
|
||||
# Posthog
|
||||
ENV NEXT_PUBLIC_ANALYTICS_POSTHOG="${NEXT_PUBLIC_ANALYTICS_POSTHOG}" \
|
||||
NEXT_PUBLIC_POSTHOG_HOST="${NEXT_PUBLIC_POSTHOG_HOST}" \
|
||||
NEXT_PUBLIC_POSTHOG_KEY="${NEXT_PUBLIC_POSTHOG_KEY}"
|
||||
|
||||
# Umami
|
||||
ENV NEXT_PUBLIC_ANALYTICS_UMAMI="${NEXT_PUBLIC_ANALYTICS_UMAMI}" \
|
||||
NEXT_PUBLIC_UMAMI_SCRIPT_URL="${NEXT_PUBLIC_UMAMI_SCRIPT_URL}" \
|
||||
NEXT_PUBLIC_UMAMI_WEBSITE_ID="${NEXT_PUBLIC_UMAMI_WEBSITE_ID}"
|
||||
|
||||
# Node
|
||||
ENV NODE_OPTIONS="--max-old-space-size=8192"
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json pnpm-workspace.yaml ./
|
||||
COPY .npmrc ./
|
||||
COPY packages ./packages
|
||||
|
||||
RUN \
|
||||
# If you want to build docker in China, build with --build-arg USE_CN_MIRROR=true
|
||||
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
|
||||
sed -i "s/deb.debian.org/mirrors.ustc.edu.cn/g" "/etc/apt/sources.list.d/debian.sources"; \
|
||||
fi \
|
||||
# Add required package
|
||||
&& apt update \
|
||||
&& apt install ca-certificates proxychains-ng -qy
|
||||
|
||||
RUN \
|
||||
# If you want to build docker in China, build with --build-arg USE_CN_MIRROR=true
|
||||
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
|
||||
export SENTRYCLI_CDNURL="https://npmmirror.com/mirrors/sentry-cli"; \
|
||||
npm config set registry "https://registry.npmmirror.com/"; \
|
||||
echo 'canvas_binary_host_mirror=https://npmmirror.com/mirrors/canvas' >> .npmrc; \
|
||||
fi \
|
||||
# Set the registry for corepack
|
||||
&& export COREPACK_NPM_REGISTRY=$(npm config get registry | sed 's/\/$//') \
|
||||
# Update corepack to latest (nodejs/corepack#612)
|
||||
&& npm i -g corepack@latest \
|
||||
# Enable corepack
|
||||
&& corepack enable \
|
||||
# Use pnpm for corepack
|
||||
&& corepack use $(sed -n 's/.*"packageManager": "\(.*\)".*/\1/p' package.json) \
|
||||
# Install the dependencies
|
||||
&& pnpm i \
|
||||
# Add db migration dependencies
|
||||
&& mkdir -p /deps \
|
||||
&& cd /deps \
|
||||
&& pnpm init \
|
||||
&& pnpm add pg drizzle-orm
|
||||
|
||||
COPY . .
|
||||
|
||||
ENV NODE_ENV="development" \
|
||||
NODE_OPTIONS="--dns-result-order=ipv4first --use-openssl-ca" \
|
||||
NODE_EXTRA_CA_CERTS="" \
|
||||
NODE_TLS_REJECT_UNAUTHORIZED="" \
|
||||
SSL_CERT_DIR="/etc/ssl/certs/ca-certificates.crt"
|
||||
|
||||
# Make the middleware rewrite through local as default
|
||||
# refs: https://github.com/lobehub/lobe-chat/issues/5876
|
||||
ENV MIDDLEWARE_REWRITE_THROUGH_LOCAL="1"
|
||||
|
||||
# set hostname to localhost
|
||||
ENV HOSTNAME="0.0.0.0" \
|
||||
PORT="3210"
|
||||
|
||||
# General Variables
|
||||
ENV ACCESS_CODE="" \
|
||||
APP_URL="" \
|
||||
API_KEY_SELECT_MODE="" \
|
||||
DEFAULT_AGENT_CONFIG="" \
|
||||
SYSTEM_AGENT="" \
|
||||
FEATURE_FLAGS="" \
|
||||
PROXY_URL=""
|
||||
|
||||
# Database
|
||||
ENV KEY_VAULTS_SECRET="" \
|
||||
DATABASE_DRIVER="node" \
|
||||
DATABASE_URL=""
|
||||
|
||||
# Next Auth
|
||||
ENV NEXT_AUTH_SECRET="" \
|
||||
NEXT_AUTH_SSO_PROVIDERS="" \
|
||||
NEXTAUTH_URL=""
|
||||
|
||||
# S3
|
||||
ENV NEXT_PUBLIC_S3_DOMAIN="" \
|
||||
S3_PUBLIC_DOMAIN="" \
|
||||
S3_ACCESS_KEY_ID="" \
|
||||
S3_BUCKET="" \
|
||||
S3_ENDPOINT="" \
|
||||
S3_SECRET_ACCESS_KEY=""
|
||||
|
||||
# Model Variables
|
||||
ENV \
|
||||
# AI21
|
||||
AI21_API_KEY="" AI21_MODEL_LIST="" \
|
||||
# Ai360
|
||||
AI360_API_KEY="" AI360_MODEL_LIST="" \
|
||||
# Anthropic
|
||||
ANTHROPIC_API_KEY="" ANTHROPIC_MODEL_LIST="" ANTHROPIC_PROXY_URL="" \
|
||||
# Amazon Bedrock
|
||||
AWS_ACCESS_KEY_ID="" AWS_SECRET_ACCESS_KEY="" AWS_REGION="" AWS_BEDROCK_MODEL_LIST="" \
|
||||
# Azure OpenAI
|
||||
AZURE_API_KEY="" AZURE_API_VERSION="" AZURE_ENDPOINT="" AZURE_MODEL_LIST="" \
|
||||
# Baichuan
|
||||
BAICHUAN_API_KEY="" BAICHUAN_MODEL_LIST="" \
|
||||
# Cloudflare
|
||||
CLOUDFLARE_API_KEY="" CLOUDFLARE_BASE_URL_OR_ACCOUNT_ID="" CLOUDFLARE_MODEL_LIST="" \
|
||||
# Cohere
|
||||
COHERE_API_KEY="" COHERE_MODEL_LIST="" COHERE_PROXY_URL="" \
|
||||
# DeepSeek
|
||||
DEEPSEEK_API_KEY="" DEEPSEEK_MODEL_LIST="" \
|
||||
# Fireworks AI
|
||||
FIREWORKSAI_API_KEY="" FIREWORKSAI_MODEL_LIST="" \
|
||||
# Gitee AI
|
||||
GITEE_AI_API_KEY="" GITEE_AI_MODEL_LIST="" \
|
||||
# GitHub
|
||||
GITHUB_TOKEN="" GITHUB_MODEL_LIST="" \
|
||||
# Google
|
||||
GOOGLE_API_KEY="" GOOGLE_MODEL_LIST="" GOOGLE_PROXY_URL="" \
|
||||
# Groq
|
||||
GROQ_API_KEY="" GROQ_MODEL_LIST="" GROQ_PROXY_URL="" \
|
||||
# Higress
|
||||
HIGRESS_API_KEY="" HIGRESS_MODEL_LIST="" HIGRESS_PROXY_URL="" \
|
||||
# HuggingFace
|
||||
HUGGINGFACE_API_KEY="" HUGGINGFACE_MODEL_LIST="" HUGGINGFACE_PROXY_URL="" \
|
||||
# Hunyuan
|
||||
HUNYUAN_API_KEY="" HUNYUAN_MODEL_LIST="" \
|
||||
# InternLM
|
||||
INTERNLM_API_KEY="" INTERNLM_MODEL_LIST="" \
|
||||
# Jina
|
||||
JINA_API_KEY="" JINA_MODEL_LIST="" JINA_PROXY_URL="" \
|
||||
# Minimax
|
||||
MINIMAX_API_KEY="" MINIMAX_MODEL_LIST="" \
|
||||
# Mistral
|
||||
MISTRAL_API_KEY="" MISTRAL_MODEL_LIST="" \
|
||||
# Moonshot
|
||||
MOONSHOT_API_KEY="" MOONSHOT_MODEL_LIST="" MOONSHOT_PROXY_URL="" \
|
||||
# Novita
|
||||
NOVITA_API_KEY="" NOVITA_MODEL_LIST="" \
|
||||
# Nvidia NIM
|
||||
NVIDIA_API_KEY="" NVIDIA_MODEL_LIST="" NVIDIA_PROXY_URL="" \
|
||||
# Ollama
|
||||
ENABLED_OLLAMA="" OLLAMA_MODEL_LIST="" OLLAMA_PROXY_URL="" \
|
||||
# OpenAI
|
||||
OPENAI_API_KEY="" OPENAI_MODEL_LIST="" OPENAI_PROXY_URL="" \
|
||||
# OpenRouter
|
||||
OPENROUTER_API_KEY="" OPENROUTER_MODEL_LIST="" \
|
||||
# Perplexity
|
||||
PERPLEXITY_API_KEY="" PERPLEXITY_MODEL_LIST="" PERPLEXITY_PROXY_URL="" \
|
||||
# PPIO
|
||||
PPIO_API_KEY="" PPIO_MODEL_LIST="" \
|
||||
# Qwen
|
||||
QWEN_API_KEY="" QWEN_MODEL_LIST="" QWEN_PROXY_URL="" \
|
||||
# SambaNova
|
||||
SAMBANOVA_API_KEY="" SAMBANOVA_MODEL_LIST="" \
|
||||
# SenseNova
|
||||
SENSENOVA_API_KEY="" SENSENOVA_MODEL_LIST="" \
|
||||
# SiliconCloud
|
||||
SILICONCLOUD_API_KEY="" SILICONCLOUD_MODEL_LIST="" SILICONCLOUD_PROXY_URL="" \
|
||||
# Spark
|
||||
SPARK_API_KEY="" SPARK_MODEL_LIST="" \
|
||||
# Stepfun
|
||||
STEPFUN_API_KEY="" STEPFUN_MODEL_LIST="" \
|
||||
# Taichu
|
||||
TAICHU_API_KEY="" TAICHU_MODEL_LIST="" \
|
||||
# TogetherAI
|
||||
TOGETHERAI_API_KEY="" TOGETHERAI_MODEL_LIST="" \
|
||||
# Upstage
|
||||
UPSTAGE_API_KEY="" UPSTAGE_MODEL_LIST="" \
|
||||
# vLLM
|
||||
VLLM_API_KEY="" VLLM_MODEL_LIST="" VLLM_PROXY_URL="" \
|
||||
# Wenxin
|
||||
WENXIN_API_KEY="" WENXIN_MODEL_LIST="" \
|
||||
# xAI
|
||||
XAI_API_KEY="" XAI_MODEL_LIST="" XAI_PROXY_URL="" \
|
||||
# 01.AI
|
||||
ZEROONE_API_KEY="" ZEROONE_MODEL_LIST="" \
|
||||
# Zhipu
|
||||
ZHIPU_API_KEY="" ZHIPU_MODEL_LIST="" \
|
||||
# Tencent Cloud
|
||||
TENCENT_CLOUD_API_KEY="" TENCENT_CLOUD_MODEL_LIST=""
|
||||
|
||||
EXPOSE 3210/tcp
|
||||
|
||||
ENTRYPOINT ["sh", "-c", "npm run db:migrate && npm run dev -- --turbopack -p 3210"]
|
||||
@@ -0,0 +1,117 @@
|
||||
name: LobeChat-Database-Barebones
|
||||
services:
|
||||
network-service:
|
||||
image: alpine
|
||||
container_name: lobe-network
|
||||
restart: always
|
||||
ports:
|
||||
- '${MINIO_PORT}:${MINIO_PORT}' # MinIO API
|
||||
- '9001:9001' # MinIO Console
|
||||
- '${CASDOOR_PORT}:${CASDOOR_PORT}' # Casdoor
|
||||
- '${LOBE_PORT}:3210' # LobeChat
|
||||
command: tail -f /dev/null
|
||||
networks:
|
||||
- lobe-network
|
||||
|
||||
postgresql:
|
||||
image: pgvector/pgvector:pg17
|
||||
container_name: lobe-postgres
|
||||
ports:
|
||||
- '5432:5432'
|
||||
volumes:
|
||||
- './data:/var/lib/postgresql/data'
|
||||
environment:
|
||||
- 'POSTGRES_DB=${LOBE_DB_NAME}'
|
||||
- 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'pg_isready -U postgres']
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
restart: always
|
||||
networks:
|
||||
- lobe-network
|
||||
|
||||
minio:
|
||||
image: minio/minio
|
||||
container_name: lobe-minio
|
||||
network_mode: 'service:network-service'
|
||||
volumes:
|
||||
- './s3_data:/etc/minio/data'
|
||||
environment:
|
||||
- 'MINIO_API_CORS_ALLOW_ORIGIN=*'
|
||||
env_file:
|
||||
- .env
|
||||
restart: always
|
||||
entrypoint: >
|
||||
/bin/sh -c "
|
||||
minio server /etc/minio/data --address ':${MINIO_PORT}' --console-address ':9001' &
|
||||
MINIO_PID=\$!
|
||||
while ! curl -s http://localhost:${MINIO_PORT}/minio/health/live; do
|
||||
echo 'Waiting for MinIO to start...'
|
||||
sleep 1
|
||||
done
|
||||
sleep 5
|
||||
mc alias set myminio http://localhost:${MINIO_PORT} ${MINIO_ROOT_USER} ${MINIO_ROOT_PASSWORD}
|
||||
echo 'Creating bucket ${MINIO_LOBE_BUCKET}'
|
||||
mc mb myminio/${MINIO_LOBE_BUCKET}
|
||||
wait \$MINIO_PID
|
||||
"
|
||||
|
||||
searxng:
|
||||
image: searxng/searxng
|
||||
container_name: lobe-searxng
|
||||
volumes:
|
||||
- './searxng-settings.yml:/etc/searxng/settings.yml'
|
||||
environment:
|
||||
- 'SEARXNG_SETTINGS_FILE=/etc/searxng/settings.yml'
|
||||
restart: always
|
||||
networks:
|
||||
- lobe-network
|
||||
env_file:
|
||||
- .env
|
||||
|
||||
lobe:
|
||||
build:
|
||||
context: ../../
|
||||
dockerfile: docker-compose/development/Dockerfile.dev.database
|
||||
container_name: lobe-chat
|
||||
network_mode: 'service:network-service'
|
||||
depends_on:
|
||||
postgresql:
|
||||
condition: service_healthy
|
||||
network-service:
|
||||
condition: service_started
|
||||
minio:
|
||||
condition: service_started
|
||||
|
||||
volumes:
|
||||
- ../../:/app
|
||||
- /app/node_modules
|
||||
|
||||
environment:
|
||||
- 'NEXT_PUBLIC_ENABLE_NEXT_AUTH=1'
|
||||
- 'KEY_VAULTS_SECRET=Kix2wcUONd4CX51E/ZPAd36BqM4wzJgKjPtz2sGztqQ='
|
||||
- 'NEXT_AUTH_SECRET=NX2kaPE923dt6BL2U8e9oSre5RfoT7hg'
|
||||
- 'DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgresql:5432/${LOBE_DB_NAME}'
|
||||
- 'S3_BUCKET=${MINIO_LOBE_BUCKET}'
|
||||
- 'S3_ENABLE_PATH_STYLE=1'
|
||||
- 'S3_ACCESS_KEY=${MINIO_ROOT_USER}'
|
||||
- 'S3_ACCESS_KEY_ID=${MINIO_ROOT_USER}'
|
||||
- 'S3_SECRET_ACCESS_KEY=${MINIO_ROOT_PASSWORD}'
|
||||
- 'LLM_VISION_IMAGE_USE_BASE64=1'
|
||||
- 'S3_SET_ACL=0'
|
||||
- 'SEARXNG_URL=http://searxng:8080'
|
||||
env_file:
|
||||
- .env
|
||||
restart: always
|
||||
|
||||
volumes:
|
||||
data:
|
||||
driver: local
|
||||
s3_data:
|
||||
driver: local
|
||||
|
||||
networks:
|
||||
lobe-network:
|
||||
driver: bridge
|
||||
@@ -33,7 +33,7 @@ services:
|
||||
- lobe-network
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2025-04-22T22-12-26Z
|
||||
image: minio/minio
|
||||
container_name: lobe-minio
|
||||
network_mode: 'service:network-service'
|
||||
volumes:
|
||||
|
||||
@@ -33,7 +33,7 @@ services:
|
||||
- lobe-network
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2025-04-22T22-12-26Z
|
||||
image: minio/minio
|
||||
container_name: lobe-minio
|
||||
network_mode: 'service:network-service'
|
||||
volumes:
|
||||
|
||||
@@ -32,7 +32,7 @@ services:
|
||||
- lobe-network
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2025-04-22T22-12-26Z
|
||||
image: minio/minio
|
||||
container_name: lobe-minio
|
||||
network_mode: 'service:network-service'
|
||||
volumes:
|
||||
|
||||
@@ -18,7 +18,7 @@ services:
|
||||
restart: always
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2025-04-22T22-12-26Z
|
||||
image: minio/minio
|
||||
container_name: lobe-minio
|
||||
ports:
|
||||
- '9000:9000'
|
||||
|
||||
@@ -18,7 +18,7 @@ services:
|
||||
restart: always
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2025-04-22T22-12-26Z
|
||||
image: minio/minio
|
||||
container_name: lobe-minio
|
||||
ports:
|
||||
- '9000:9000'
|
||||
|
||||