Skip to content

Conversation

tjx666
Copy link
Collaborator

@tjx666 tjx666 commented Aug 17, 2025

💻 变更类型 | Change Type

  • 🐛 fix

🔀 变更说明 | Description of Change

修复桌面版文件向量化失败问题,将所有 Model 和 Service 从硬编码 serverDB 导入改为接受数据库实例作为构造函数参数。

主要变更

  • 修复 4 个 RAG 评估模型:EvalDatasetModel, EvalDatasetRecordModel, EvalEvaluationModel, EvaluationRecordModel
  • 更新 UserServiceNextAuthUserService 构造函数
  • 修复所有实例化位置:webhook routes, tRPC routers, 测试文件
  • 消除硬编码的 serverDB 导入,改善依赖注入模式

技术细节

  • 所有相关构造函数现在接受 (db: LobeChatDatabase, userId: string) 参数
  • 更新了 15 个文件中的实例化调用
  • 通过 TypeScript 类型检查和所有相关测试

📝 补充信息 | Additional Information

解决问题:修复 issue #8826 - 桌面版无法向量化的问题

测试状态

  • ✅ TypeScript 类型检查通过
  • ✅ 现有单元测试通过
  • ✅ 质量检查验证
  • ✅ 无破坏性变更

影响范围

  • 桌面版用户:修复文件向量化功能
  • 架构改进:更好的依赖注入模式
  • 代码质量:提高可测试性

tjx666 added 2 commits August 17, 2025 18:01
Fixed ChunkService constructor to properly receive serverDB parameter,
which is required for desktop file vectorization functionality.

Fixes #8826
- Fixed 4 RAG evaluation models to accept LobeChatDatabase parameter
- Updated UserService and NextAuthUserService constructors
- Fixed all instantiation sites across routes and tests
- Eliminates hardcoded serverDB imports for better testability

Resolves desktop file vectorization issues by ensuring proper
database instance management across all model/service layers.

Fixes #8826
Copy link

vercel bot commented Aug 17, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
lobe-chat-database Ready Ready Preview Comment Aug 17, 2025 10:46am
lobe-chat-preview Ready Ready Preview Comment Aug 17, 2025 10:46am

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 17, 2025
Copy link
Contributor

sourcery-ai bot commented Aug 17, 2025

Reviewer's Guide

This PR refactors core models and services to inject the database instance via constructor parameters instead of relying on hardcoded serverDB imports, and updates all instantiation sites and tests to pass the LobeChatDatabase parameter for improved dependency management and testability.

Sequence diagram for model/service instantiation with injected database

sequenceDiagram
  participant Route
  participant DB as LobeChatDatabase
  participant UserService
  participant EvalDatasetModel
  Route->>DB: Obtain database instance
  Route->>UserService: new UserService(DB)
  Route->>EvalDatasetModel: new EvalDatasetModel(DB, userId)
  UserService->>DB: Use DB for operations
  EvalDatasetModel->>DB: Use DB for operations
Loading

Class diagram for updated RAG evaluation models and services

classDiagram
  class EvalDatasetModel {
    - db: LobeChatDatabase
    - userId: string
    + constructor(db: LobeChatDatabase, userId: string)
    + create(params: NewEvalDatasetsItem)
    + delete(id: number)
    + query(knowledgeBaseId: string): Promise<RAGEvalDataSetItem[]>
    + findById(id: number)
    + update(id: number, value: Partial<NewEvalDatasetsItem>)
  }
  class EvalDatasetRecordModel {
    - db: LobeChatDatabase
    - userId: string
    + constructor(db: LobeChatDatabase, userId: string)
    + create(params: NewEvalDatasetRecordsItem)
    + batchCreate(params: NewEvalDatasetRecordsItem[])
    + delete(id: number)
    + query(datasetId: number)
    + findByDatasetId(datasetId: number)
    + findById(id: number)
    + update(id: number, value: Partial<NewEvalDatasetRecordsItem>)
  }
  class EvalEvaluationModel {
    - db: LobeChatDatabase
    - userId: string
    + constructor(db: LobeChatDatabase, userId: string)
    + create(params: NewEvalEvaluationItem)
    + delete(id: number)
    + queryByKnowledgeBaseId(knowledgeBaseId: string)
    + findById(id: number)
    + update(id: number, value: Partial<NewEvalEvaluationItem>)
  }
  class EvaluationRecordModel {
    - db: LobeChatDatabase
    - userId: string
    + constructor(db: LobeChatDatabase, userId: string)
    + create(params: NewEvaluationRecordsItem)
    + batchCreate(params: NewEvaluationRecordsItem[])
    + delete(id: number)
    + query(reportId: number)
    + findById(id: number)
    + findByEvaluationId(evaluationId: number)
    + update(id: number, value: Partial<NewEvaluationRecordsItem>)
  }
  class UserService {
    - db: LobeChatDatabase
    + constructor(db: LobeChatDatabase)
    + createUser(id: string, params: UserJSON)
    + deleteUser(id: string)
    + updateUser(id: string, params: UserJSON)
    + getUserApiKeys(id: string)
    + getUserAvatar(id: string, image: string)
  }
  class NextAuthUserService {
    - db: LobeChatDatabase
    - adapter
    + constructor(db: LobeChatDatabase)
    + safeUpdateUser(user: UserItem, provider: string)
  }
  class ChunkService {
    - userId: string
    + constructor(db: LobeChatDatabase, userId: string)
    ...
  }
  LobeChatDatabase <|-- EvalDatasetModel
  LobeChatDatabase <|-- EvalDatasetRecordModel
  LobeChatDatabase <|-- EvalEvaluationModel
  LobeChatDatabase <|-- EvaluationRecordModel
  LobeChatDatabase <|-- UserService
  LobeChatDatabase <|-- NextAuthUserService
  LobeChatDatabase <|-- ChunkService
Loading

File-Level Changes

Change Details Files
Injected LobeChatDatabase into RAG evaluation models
  • Updated constructors to accept a db parameter
  • Replaced all serverDB references with this.db
  • Stored db in a private class property
src/database/server/models/ragEval/dataset.ts
src/database/server/models/ragEval/datasetRecord.ts
src/database/server/models/ragEval/evaluation.ts
src/database/server/models/ragEval/evaluationRecord.ts
Refactored UserService, NextAuthUserService, and ChunkService for DI
  • Added private db property and updated constructors to accept db
  • Replaced serverDB usages with this.db
  • Initialized adapter with injected db in NextAuthUserService
src/server/services/user/index.ts
src/server/services/nextAuthUser/index.ts
src/server/services/chunk/index.ts
Updated all instantiation sites to pass the db parameter
  • Passed serverDB into service and model constructors in routes and routers
  • Replaced parameterless instantiations with ones including ctx.serverDB
  • Adjusted webhooks and API routes to inject db
src/server/routers/async/ragEval.ts
src/server/routers/lambda/ragEval.ts
src/server/routers/async/file.ts
src/server/routers/lambda/chunk.ts
src/server/routers/lambda/user.ts
src/app/(backend)/webapi/user/avatar/[id]/[image]/route.ts
src/app/(backend)/api/webhooks/casdoor/route.ts
src/app/(backend)/api/webhooks/clerk/route.ts
src/app/(backend)/api/webhooks/logto/route.ts
Adjusted unit tests to use injectable database mocks
  • Passed a mock LobeChatDatabase into service constructors
  • Updated test setup to clear mocks accordingly
src/server/services/user/index.test.ts
src/server/services/nextAuthUser/index.test.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#8826 Fix the inability to vectorize files in the desktop (客户端模式) version of the application.
#8826 Refactor database model and service instantiation to use the correct database instance (dependency injection) rather than hardcoded imports, ensuring compatibility with desktop multi-window environments.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

gru-agent bot commented Aug 17, 2025

There is too much information in the pull request to test.

@lobehubbot
Copy link
Member

👍 @tjx666

Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link
Contributor

claude bot commented Aug 17, 2025

Claude encountered an error —— View job


I'll analyze this and get back to you.

@dosubot dosubot bot added the 🐛 Bug Something isn't working | 缺陷 label Aug 17, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • ChunkService now takes a db parameter but doesn’t assign it to a member or use it internally—ensure you store it (e.g. this.db = db) and update all internal calls to use the injected instance.
  • Double‐check that you’ve added the import { serverDB } from '@/database/server' in every file where you pass serverDB to constructors to avoid missing import/runtime errors.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- ChunkService now takes a db parameter but doesn’t assign it to a member or use it internally—ensure you store it (e.g. this.db = db) and update all internal calls to use the injected instance.
- Double‐check that you’ve added the `import { serverDB } from '@/database/server'` in every file where you pass `serverDB` to constructors to avoid missing import/runtime errors.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@tjx666 tjx666 changed the title 🐛 fix(db): update all models to use passed db parameter for desktop vectorization 🐛 fix(db): desktop none server mode can't vectorization Aug 17, 2025
@tjx666 tjx666 changed the title 🐛 fix(db): desktop none server mode can't vectorization 🐛 fix(db): desktop local db can't vectorization Aug 17, 2025
@tjx666 tjx666 merged commit a00fd9d into main Aug 17, 2025
32 of 33 checks passed
@tjx666 tjx666 deleted the tj/fix/desktop-file-vectorization branch August 17, 2025 11:01
@lobehubbot
Copy link
Member

❤️ Great PR @tjx666 ❤️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.
项目的成长离不开用户反馈和贡献,感谢您的贡献! 如果您对 LobeHub 开发者社区感兴趣,请加入我们的 discord,然后私信 @arvinxx@canisminor1990。他们会邀请您加入我们的私密开发者频道。我们将会讨论关于 Lobe Chat 的开发,分享和讨论全球范围内的 AI 消息。

github-actions bot pushed a commit that referenced this pull request Aug 17, 2025
### [Version&nbsp;1.113.1](v1.113.0...v1.113.1)
<sup>Released on **2025-08-17**</sup>

#### 🐛 Bug Fixes

- **db**: Desktop local db can't vectorization.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's fixed

* **db**: Desktop local db can't vectorization, closes [#8830](#8830) ([a00fd9d](a00fd9d))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@lobehubbot
Copy link
Member

🎉 This PR is included in version 1.113.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit to jaworldwideorg/OneJA-Bot that referenced this pull request Aug 18, 2025
## [Version&nbsp;1.113.0](v1.112.0...v1.113.0)
<sup>Released on **2025-08-18**</sup>

#### ♻ Code Refactoring

- **misc**: Refactor const folder to a new package, refactor prompts folder to the `@lobechat/prompts` pacakge, 重构ArgsInput组件.

#### ✨ Features

- **provider**: Add BFL provider support for image generation.

#### 🐛 Bug Fixes

- **db**: Desktop local db can't vectorization.
- **misc**: Improve mcp tracing with user config.

#### 💄 Styles

- **misc**: Add Imagen 4 GA models, style improve auth sign in box loading.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

* **misc**: Refactor const folder to a new package, closes [lobehub#8756](https://github.com/jaworldwideorg/OneJA-Bot/issues/8756) ([30a4734](30a4734))
* **misc**: Refactor prompts folder to the `@lobechat/prompts` pacakge, closes [lobehub#8810](https://github.com/jaworldwideorg/OneJA-Bot/issues/8810) ([d82e7bb](d82e7bb))
* **misc**: 重构 ArgsInput 组件, closes [lobehub#8765](https://github.com/jaworldwideorg/OneJA-Bot/issues/8765) ([0905559](0905559))

#### What's improved

* **provider**: Add BFL provider support for image generation, closes [lobehub#8806](https://github.com/jaworldwideorg/OneJA-Bot/issues/8806) ([519e03e](519e03e))

#### What's fixed

* **db**: Desktop local db can't vectorization, closes [lobehub#8830](https://github.com/jaworldwideorg/OneJA-Bot/issues/8830) ([a00fd9d](a00fd9d))
* **misc**: Improve mcp tracing with user config, closes [lobehub#8827](https://github.com/jaworldwideorg/OneJA-Bot/issues/8827) ([5cab2ee](5cab2ee))

#### Styles

* **misc**: Add Imagen 4 GA models, closes [lobehub#8799](https://github.com/jaworldwideorg/OneJA-Bot/issues/8799) ([2e9ad20](2e9ad20))
* **misc**: Style improve auth sign in box loading, closes [lobehub#8805](https://github.com/jaworldwideorg/OneJA-Bot/issues/8805) ([62f5a1b](62f5a1b))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@187370
Copy link

187370 commented Aug 19, 2025

@claude 这个改了哪些代码呢

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@claude What codes did this change?

Copy link
Contributor

claude bot commented Aug 19, 2025

Claude encountered an error —— View job


I'll analyze this and get back to you.

@tjx666
Copy link
Collaborator Author

tjx666 commented Aug 19, 2025

@187370 claude 号被封了,等 @arvinxx 换个号。不过貌似没有仓库写入权限的人可能也没法用 claude code action

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@187370 The claude number was blocked, wait for @arvinxx to change the number. However, people who seem to have no warehouse write permission may not be able to use claude code action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working | 缺陷 released size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants