@lobehub/chat 1.77.4 → 1.77.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/changelog/v1.json +14 -0
- package/package.json +1 -1
- package/src/app/[variants]/(main)/repos/[id]/@menu/default.tsx +3 -2
- package/src/components/Loading/BrandTextLoading/index.tsx +2 -2
- package/src/database/core/db-adaptor.ts +20 -2
- package/src/database/models/__tests__/aiProvider.test.ts +2 -0
- package/src/database/models/file.ts +70 -38
- package/src/database/models/session.ts +8 -0
- package/src/database/models/user.ts +7 -1
- package/src/database/server/index.ts +1 -1
- package/src/libs/agent-runtime/openrouter/__snapshots__/index.test.ts.snap +1 -1
- package/src/libs/trpc/async/index.ts +11 -1
- package/src/libs/trpc/lambda/index.ts +1 -0
- package/src/libs/trpc/lambda/serverDatabase.ts +10 -0
- package/src/server/routers/async/file.ts +4 -5
- package/src/server/routers/async/ragEval.ts +3 -4
- package/src/server/routers/lambda/_template.ts +3 -5
- package/src/server/routers/lambda/agent.ts +8 -8
- package/src/server/routers/lambda/aiModel.ts +5 -5
- package/src/server/routers/lambda/aiProvider.test.ts +0 -2
- package/src/server/routers/lambda/aiProvider.ts +5 -5
- package/src/server/routers/lambda/chunk.ts +18 -15
- package/src/server/routers/lambda/exporter.ts +4 -4
- package/src/server/routers/lambda/file.ts +6 -6
- package/src/server/routers/lambda/importer.ts +3 -3
- package/src/server/routers/lambda/knowledgeBase.ts +3 -3
- package/src/server/routers/lambda/message.ts +5 -3
- package/src/server/routers/lambda/plugin.ts +5 -3
- package/src/server/routers/lambda/ragEval.ts +17 -14
- package/src/server/routers/lambda/session.ts +6 -4
- package/src/server/routers/lambda/sessionGroup.ts +3 -3
- package/src/server/routers/lambda/thread.ts +4 -4
- package/src/server/routers/lambda/topic.ts +5 -3
- package/src/server/routers/lambda/user.ts +7 -7
- package/src/server/services/nextAuthUser/index.test.ts +1 -2
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,48 @@
|
|
2
2
|
|
3
3
|
# Changelog
|
4
4
|
|
5
|
+
### [Version 1.77.6](https://github.com/lobehub/lobe-chat/compare/v1.77.5...v1.77.6)
|
6
|
+
|
7
|
+
<sup>Released on **2025-04-01**</sup>
|
8
|
+
|
9
|
+
#### ♻ Code Refactoring
|
10
|
+
|
11
|
+
- **misc**: Refactor the db to context inject mode.
|
12
|
+
|
13
|
+
<br/>
|
14
|
+
|
15
|
+
<details>
|
16
|
+
<summary><kbd>Improvements and Fixes</kbd></summary>
|
17
|
+
|
18
|
+
#### Code refactoring
|
19
|
+
|
20
|
+
- **misc**: Refactor the db to context inject mode, closes [#7255](https://github.com/lobehub/lobe-chat/issues/7255) ([ffd0dbc](https://github.com/lobehub/lobe-chat/commit/ffd0dbc))
|
21
|
+
|
22
|
+
</details>
|
23
|
+
|
24
|
+
<div align="right">
|
25
|
+
|
26
|
+
[](#readme-top)
|
27
|
+
|
28
|
+
</div>
|
29
|
+
|
30
|
+
### [Version 1.77.5](https://github.com/lobehub/lobe-chat/compare/v1.77.4...v1.77.5)
|
31
|
+
|
32
|
+
<sup>Released on **2025-04-01**</sup>
|
33
|
+
|
34
|
+
<br/>
|
35
|
+
|
36
|
+
<details>
|
37
|
+
<summary><kbd>Improvements and Fixes</kbd></summary>
|
38
|
+
|
39
|
+
</details>
|
40
|
+
|
41
|
+
<div align="right">
|
42
|
+
|
43
|
+
[](#readme-top)
|
44
|
+
|
45
|
+
</div>
|
46
|
+
|
5
47
|
### [Version 1.77.4](https://github.com/lobehub/lobe-chat/compare/v1.77.3...v1.77.4)
|
6
48
|
|
7
49
|
<sup>Released on **2025-03-31**</sup>
|
package/changelog/v1.json
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
[
|
2
|
+
{
|
3
|
+
"children": {
|
4
|
+
"improvements": [
|
5
|
+
"Refactor the db to context inject mode."
|
6
|
+
]
|
7
|
+
},
|
8
|
+
"date": "2025-04-01",
|
9
|
+
"version": "1.77.6"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"children": {},
|
13
|
+
"date": "2025-04-01",
|
14
|
+
"version": "1.77.5"
|
15
|
+
},
|
2
16
|
{
|
3
17
|
"children": {
|
4
18
|
"improvements": [
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@lobehub/chat",
|
3
|
-
"version": "1.77.
|
3
|
+
"version": "1.77.6",
|
4
4
|
"description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
|
5
5
|
"keywords": [
|
6
6
|
"framework",
|
@@ -11,9 +11,10 @@ interface Params {
|
|
11
11
|
id: string;
|
12
12
|
}
|
13
13
|
|
14
|
-
type Props = { params: Params };
|
14
|
+
type Props = { params: Promise<Params> };
|
15
15
|
|
16
|
-
const MenuPage = async (
|
16
|
+
const MenuPage = async (props: Props) => {
|
17
|
+
const params = await props.params;
|
17
18
|
const id = params.id;
|
18
19
|
const item = await KnowledgeBaseModel.findById(serverDB, params.id);
|
19
20
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { BrandLoading,
|
1
|
+
import { BrandLoading, LobeHubText } from '@lobehub/ui/brand';
|
2
2
|
import { Center } from 'react-layout-kit';
|
3
3
|
|
4
4
|
import { isCustomBranding } from '@/const/version';
|
@@ -10,7 +10,7 @@ export default () => {
|
|
10
10
|
|
11
11
|
return (
|
12
12
|
<Center height={'100%'} width={'100%'}>
|
13
|
-
<BrandLoading size={40} style={{ opacity: 0.6 }} text={
|
13
|
+
<BrandLoading size={40} style={{ opacity: 0.6 }} text={LobeHubText} />
|
14
14
|
</Center>
|
15
15
|
);
|
16
16
|
};
|
@@ -1,6 +1,24 @@
|
|
1
|
-
// import { isDesktop } from '@/const/version';
|
2
1
|
import { getDBInstance } from '@/database/core/web-server';
|
2
|
+
import { LobeChatDatabase } from '@/database/type';
|
3
3
|
|
4
|
-
|
4
|
+
/**
|
5
|
+
* 懒加载数据库实例
|
6
|
+
* 避免每次模块导入时都初始化数据库
|
7
|
+
*/
|
8
|
+
let cachedDB: LobeChatDatabase | null = null;
|
9
|
+
|
10
|
+
export const getServerDB = async (): Promise<LobeChatDatabase> => {
|
11
|
+
// 如果已经有缓存的实例,直接返回
|
12
|
+
if (cachedDB) return cachedDB;
|
13
|
+
|
14
|
+
try {
|
15
|
+
// 根据环境选择合适的数据库实例
|
16
|
+
cachedDB = getDBInstance();
|
17
|
+
return cachedDB;
|
18
|
+
} catch (error) {
|
19
|
+
console.error('❌ Failed to initialize database:', error);
|
20
|
+
throw error;
|
21
|
+
}
|
22
|
+
};
|
5
23
|
|
6
24
|
export const serverDB = getDBInstance();
|
@@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
|
5
5
|
import { LobeChatDatabase } from '@/database/type';
|
6
6
|
import { ModelProvider } from '@/libs/agent-runtime';
|
7
|
+
import { sleep } from '@/utils/sleep';
|
7
8
|
|
8
9
|
import { aiProviders, users } from '../../schemas';
|
9
10
|
import { AiProviderModel } from '../aiProvider';
|
@@ -96,6 +97,7 @@ describe('AiProviderModel', () => {
|
|
96
97
|
describe('query', () => {
|
97
98
|
it('should query ai providers for the user', async () => {
|
98
99
|
await aiProviderModel.create({ name: 'AiHubMix', source: 'custom', id: 'aihubmix' });
|
100
|
+
await sleep(10);
|
99
101
|
await aiProviderModel.create({ name: 'AiHubMix', source: 'custom', id: 'aihubmix-2' });
|
100
102
|
|
101
103
|
const userGroups = await aiProviderModel.query();
|
@@ -128,50 +128,49 @@ export class FileModel {
|
|
128
128
|
};
|
129
129
|
|
130
130
|
deleteMany = async (ids: string[], removeGlobalFile: boolean = true) => {
|
131
|
-
|
132
|
-
const hashList = fileList.map((file) => file.fileHash!);
|
131
|
+
if (ids.length === 0) return [];
|
133
132
|
|
134
133
|
return await this.db.transaction(async (trx) => {
|
135
|
-
// 1.
|
134
|
+
// 1. 先获取文件列表,以便返回删除的文件
|
135
|
+
const fileList = await trx.query.files.findMany({
|
136
|
+
where: and(inArray(files.id, ids), eq(files.userId, this.userId)),
|
137
|
+
});
|
138
|
+
|
139
|
+
if (fileList.length === 0) return [];
|
140
|
+
|
141
|
+
// 提取需要检查的文件哈希值
|
142
|
+
const hashList = fileList.map((file) => file.fileHash!).filter(Boolean);
|
143
|
+
|
144
|
+
// 2. 删除相关的 chunks
|
136
145
|
await this.deleteFileChunks(trx as any, ids);
|
137
146
|
|
138
|
-
//
|
147
|
+
// 3. 删除文件记录
|
139
148
|
await trx.delete(files).where(and(inArray(files.id, ids), eq(files.userId, this.userId)));
|
140
149
|
|
141
|
-
//
|
142
|
-
|
150
|
+
// 如果不需要删除全局文件,直接返回
|
151
|
+
if (!removeGlobalFile || hashList.length === 0) return fileList;
|
152
|
+
|
153
|
+
// 4. 找出不再被引用的哈希值
|
154
|
+
const remainingFiles = await trx
|
143
155
|
.select({
|
144
|
-
|
145
|
-
hashId: files.fileHash,
|
156
|
+
fileHash: files.fileHash,
|
146
157
|
})
|
147
158
|
.from(files)
|
148
|
-
.where(inArray(files.fileHash, hashList))
|
149
|
-
.groupBy(files.fileHash);
|
150
|
-
|
151
|
-
// Create a Map to store the query result
|
152
|
-
const countMap = new Map(result.map((item) => [item.hashId, item.count]));
|
159
|
+
.where(inArray(files.fileHash, hashList));
|
153
160
|
|
154
|
-
//
|
155
|
-
const
|
156
|
-
count: countMap.get(hashId) || 0,
|
157
|
-
hashId: hashId,
|
158
|
-
}));
|
161
|
+
// 将仍在使用的哈希值放入Set中,便于快速查找
|
162
|
+
const usedHashes = new Set(remainingFiles.map((file) => file.fileHash));
|
159
163
|
|
160
|
-
|
164
|
+
// 找出需要删除的哈希值(不再被任何文件使用的)
|
165
|
+
const hashesToDelete = hashList.filter((hash) => !usedHashes.has(hash));
|
161
166
|
|
162
|
-
if (
|
167
|
+
if (hashesToDelete.length === 0) return fileList;
|
163
168
|
|
164
|
-
//
|
165
|
-
await trx.delete(globalFiles).where(
|
166
|
-
inArray(
|
167
|
-
globalFiles.hashId,
|
168
|
-
needToDeleteList.map((item) => item.hashId!),
|
169
|
-
),
|
170
|
-
);
|
169
|
+
// 5. 删除不再被引用的全局文件
|
170
|
+
await trx.delete(globalFiles).where(inArray(globalFiles.hashId, hashesToDelete));
|
171
171
|
|
172
|
-
|
173
|
-
|
174
|
-
);
|
172
|
+
// 返回删除的文件列表
|
173
|
+
return fileList;
|
175
174
|
});
|
176
175
|
};
|
177
176
|
|
@@ -318,25 +317,58 @@ export class FileModel {
|
|
318
317
|
|
319
318
|
// 抽象出通用的删除 chunks 方法
|
320
319
|
private deleteFileChunks = async (trx: PgTransaction<any>, fileIds: string[]) => {
|
321
|
-
|
320
|
+
if (fileIds.length === 0) return;
|
322
321
|
|
323
|
-
//
|
322
|
+
// 直接使用 JOIN 优化查询,减少数据传输量
|
324
323
|
const relatedChunks = await trx
|
325
324
|
.select({ chunkId: fileChunks.chunkId })
|
326
325
|
.from(fileChunks)
|
327
|
-
.where(
|
326
|
+
.where(
|
327
|
+
and(
|
328
|
+
inArray(fileChunks.fileId, fileIds),
|
329
|
+
// 确保只查询有效的 chunkId
|
330
|
+
notExists(
|
331
|
+
trx
|
332
|
+
.select()
|
333
|
+
.from(knowledgeBaseFiles)
|
334
|
+
.where(eq(knowledgeBaseFiles.fileId, fileChunks.fileId)),
|
335
|
+
),
|
336
|
+
),
|
337
|
+
);
|
328
338
|
|
329
339
|
const chunkIds = relatedChunks.map((c) => c.chunkId).filter(Boolean) as string[];
|
330
340
|
|
331
341
|
if (chunkIds.length === 0) return;
|
332
342
|
|
333
|
-
//
|
334
|
-
|
335
|
-
|
343
|
+
// 批量处理配置
|
344
|
+
const BATCH_SIZE = 1000; // 增加批处理量
|
345
|
+
const MAX_CONCURRENT_BATCHES = 3; // 最大并行批次数
|
346
|
+
|
347
|
+
// 分批并行处理
|
348
|
+
for (let i = 0; i < chunkIds.length; i += BATCH_SIZE * MAX_CONCURRENT_BATCHES) {
|
349
|
+
const batchPromises = [];
|
336
350
|
|
337
|
-
|
351
|
+
// 创建多个并行批次
|
352
|
+
for (let j = 0; j < MAX_CONCURRENT_BATCHES; j++) {
|
353
|
+
const startIdx = i + j * BATCH_SIZE;
|
354
|
+
if (startIdx >= chunkIds.length) break;
|
355
|
+
|
356
|
+
const batchChunkIds = chunkIds.slice(startIdx, startIdx + BATCH_SIZE);
|
357
|
+
if (batchChunkIds.length === 0) continue;
|
358
|
+
|
359
|
+
// 为每个批次创建一个删除任务
|
360
|
+
const batchPromise = (async () => {
|
361
|
+
// 先删除嵌入向量
|
362
|
+
await trx.delete(embeddings).where(inArray(embeddings.chunkId, batchChunkIds));
|
363
|
+
// 再删除块
|
364
|
+
await trx.delete(chunks).where(inArray(chunks.id, batchChunkIds));
|
365
|
+
})();
|
366
|
+
|
367
|
+
batchPromises.push(batchPromise);
|
368
|
+
}
|
338
369
|
|
339
|
-
|
370
|
+
// 等待当前批次的所有任务完成
|
371
|
+
await Promise.all(batchPromises);
|
340
372
|
}
|
341
373
|
|
342
374
|
return chunkIds;
|
@@ -193,6 +193,14 @@ export class SessionModel {
|
|
193
193
|
type: 'agent' | 'group';
|
194
194
|
}): Promise<SessionItem> => {
|
195
195
|
return this.db.transaction(async (trx) => {
|
196
|
+
if (slug) {
|
197
|
+
const existResult = await trx.query.sessions.findFirst({
|
198
|
+
where: and(eq(sessions.slug, slug), eq(sessions.userId, this.userId)),
|
199
|
+
});
|
200
|
+
|
201
|
+
if (existResult) return existResult;
|
202
|
+
}
|
203
|
+
|
196
204
|
const newAgents = await trx
|
197
205
|
.insert(agents)
|
198
206
|
.values({
|
@@ -85,7 +85,13 @@ export class UserModel {
|
|
85
85
|
const state = result[0];
|
86
86
|
|
87
87
|
// Decrypt keyVaults
|
88
|
-
|
88
|
+
let decryptKeyVaults = {};
|
89
|
+
|
90
|
+
try {
|
91
|
+
decryptKeyVaults = await decryptor(state.settingsKeyVaults, this.userId);
|
92
|
+
} catch {
|
93
|
+
/* empty */
|
94
|
+
}
|
89
95
|
|
90
96
|
const settings: DeepPartial<UserSettings> = {
|
91
97
|
defaultAgent: state.settingsDefaultAgent || {},
|
@@ -1 +1 @@
|
|
1
|
-
export { serverDB } from '../core/db-adaptor';
|
1
|
+
export { getServerDB, serverDB } from '../core/db-adaptor';
|
@@ -1724,7 +1724,7 @@ See benchmarks on the launch announcement [here](https://mistral.ai/news/mixtral
|
|
1724
1724
|
#moe",
|
1725
1725
|
"displayName": "Mistral: Mixtral 8x22B Instruct",
|
1726
1726
|
"enabled": false,
|
1727
|
-
"functionCall":
|
1727
|
+
"functionCall": true,
|
1728
1728
|
"id": "mistralai/mixtral-8x22b-instruct",
|
1729
1729
|
"maxTokens": undefined,
|
1730
1730
|
"pricing": {
|
@@ -1,3 +1,5 @@
|
|
1
|
+
import { getServerDB } from '@/database/core/db-adaptor';
|
2
|
+
|
1
3
|
import { asyncAuth } from './asyncAuth';
|
2
4
|
import { asyncTrpc } from './init';
|
3
5
|
|
@@ -5,6 +7,14 @@ export const publicProcedure = asyncTrpc.procedure;
|
|
5
7
|
|
6
8
|
export const asyncRouter = asyncTrpc.router;
|
7
9
|
|
8
|
-
export const asyncAuthedProcedure = asyncTrpc.procedure.use(asyncAuth)
|
10
|
+
export const asyncAuthedProcedure = asyncTrpc.procedure.use(asyncAuth).use(
|
11
|
+
asyncTrpc.middleware(async (opts) => {
|
12
|
+
const serverDB = await getServerDB();
|
13
|
+
|
14
|
+
return opts.next({
|
15
|
+
ctx: { serverDB },
|
16
|
+
});
|
17
|
+
}),
|
18
|
+
);
|
9
19
|
|
10
20
|
export const createAsyncCallerFactory = asyncTrpc.createCallerFactory;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './serverDatabase';
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { getServerDB } from '@/database/core/db-adaptor';
|
2
|
+
import { trpc } from '@/libs/trpc/init';
|
3
|
+
|
4
|
+
export const serverDatabase = trpc.middleware(async (opts) => {
|
5
|
+
const serverDB = await getServerDB();
|
6
|
+
|
7
|
+
return opts.next({
|
8
|
+
ctx: { serverDB },
|
9
|
+
});
|
10
|
+
});
|
@@ -11,7 +11,6 @@ import { ChunkModel } from '@/database/models/chunk';
|
|
11
11
|
import { EmbeddingModel } from '@/database/models/embedding';
|
12
12
|
import { FileModel } from '@/database/models/file';
|
13
13
|
import { NewChunkItem, NewEmbeddingsItem } from '@/database/schemas';
|
14
|
-
import { serverDB } from '@/database/server';
|
15
14
|
import { asyncAuthedProcedure, asyncRouter as router } from '@/libs/trpc/async';
|
16
15
|
import { getServerDefaultFilesConfig } from '@/server/globalConfig';
|
17
16
|
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
|
@@ -31,11 +30,11 @@ const fileProcedure = asyncAuthedProcedure.use(async (opts) => {
|
|
31
30
|
|
32
31
|
return opts.next({
|
33
32
|
ctx: {
|
34
|
-
asyncTaskModel: new AsyncTaskModel(serverDB, ctx.userId),
|
35
|
-
chunkModel: new ChunkModel(serverDB, ctx.userId),
|
33
|
+
asyncTaskModel: new AsyncTaskModel(ctx.serverDB, ctx.userId),
|
34
|
+
chunkModel: new ChunkModel(ctx.serverDB, ctx.userId),
|
36
35
|
chunkService: new ChunkService(ctx.userId),
|
37
|
-
embeddingModel: new EmbeddingModel(serverDB, ctx.userId),
|
38
|
-
fileModel: new FileModel(serverDB, ctx.userId),
|
36
|
+
embeddingModel: new EmbeddingModel(ctx.serverDB, ctx.userId),
|
37
|
+
fileModel: new FileModel(ctx.serverDB, ctx.userId),
|
39
38
|
},
|
40
39
|
});
|
41
40
|
});
|
@@ -7,7 +7,6 @@ import { DEFAULT_EMBEDDING_MODEL, DEFAULT_MODEL } from '@/const/settings';
|
|
7
7
|
import { ChunkModel } from '@/database/models/chunk';
|
8
8
|
import { EmbeddingModel } from '@/database/models/embedding';
|
9
9
|
import { FileModel } from '@/database/models/file';
|
10
|
-
import { serverDB } from '@/database/server';
|
11
10
|
import {
|
12
11
|
EvalDatasetRecordModel,
|
13
12
|
EvalEvaluationModel,
|
@@ -25,13 +24,13 @@ const ragEvalProcedure = asyncAuthedProcedure.use(async (opts) => {
|
|
25
24
|
|
26
25
|
return opts.next({
|
27
26
|
ctx: {
|
28
|
-
chunkModel: new ChunkModel(serverDB, ctx.userId),
|
27
|
+
chunkModel: new ChunkModel(ctx.serverDB, ctx.userId),
|
29
28
|
chunkService: new ChunkService(ctx.userId),
|
30
29
|
datasetRecordModel: new EvalDatasetRecordModel(ctx.userId),
|
31
|
-
embeddingModel: new EmbeddingModel(serverDB, ctx.userId),
|
30
|
+
embeddingModel: new EmbeddingModel(ctx.serverDB, ctx.userId),
|
32
31
|
evalRecordModel: new EvaluationRecordModel(ctx.userId),
|
33
32
|
evaluationModel: new EvalEvaluationModel(ctx.userId),
|
34
|
-
fileModel: new FileModel(serverDB, ctx.userId),
|
33
|
+
fileModel: new FileModel(ctx.serverDB, ctx.userId),
|
35
34
|
},
|
36
35
|
});
|
37
36
|
});
|
@@ -2,16 +2,16 @@ import { z } from 'zod';
|
|
2
2
|
|
3
3
|
import { SessionGroupModel } from '@/database/models/sessionGroup';
|
4
4
|
import { insertSessionGroupSchema } from '@/database/schemas';
|
5
|
-
import { serverDB } from '@/database/server';
|
6
5
|
import { authedProcedure, router } from '@/libs/trpc';
|
6
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
7
7
|
import { SessionGroupItem } from '@/types/session';
|
8
8
|
|
9
|
-
const sessionProcedure = authedProcedure.use(async (opts) => {
|
9
|
+
const sessionProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
10
10
|
const { ctx } = opts;
|
11
11
|
|
12
12
|
return opts.next({
|
13
13
|
ctx: {
|
14
|
-
sessionGroupModel: new SessionGroupModel(serverDB, ctx.userId),
|
14
|
+
sessionGroupModel: new SessionGroupModel(ctx.serverDB, ctx.userId),
|
15
15
|
},
|
16
16
|
});
|
17
17
|
});
|
@@ -69,8 +69,6 @@ export const sessionGroupRouter = router({
|
|
69
69
|
}),
|
70
70
|
)
|
71
71
|
.mutation(async ({ input, ctx }) => {
|
72
|
-
console.log('sortMap:', input.sortMap);
|
73
|
-
|
74
72
|
return ctx.sessionGroupModel.updateOrder(input.sortMap);
|
75
73
|
}),
|
76
74
|
});
|
@@ -7,22 +7,22 @@ import { FileModel } from '@/database/models/file';
|
|
7
7
|
import { KnowledgeBaseModel } from '@/database/models/knowledgeBase';
|
8
8
|
import { SessionModel } from '@/database/models/session';
|
9
9
|
import { UserModel } from '@/database/models/user';
|
10
|
-
import { serverDB } from '@/database/server';
|
11
10
|
import { pino } from '@/libs/logger';
|
12
11
|
import { authedProcedure, router } from '@/libs/trpc';
|
12
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
13
13
|
import { AgentService } from '@/server/services/agent';
|
14
14
|
import { KnowledgeItem, KnowledgeType } from '@/types/knowledgeBase';
|
15
15
|
|
16
|
-
const agentProcedure = authedProcedure.use(async (opts) => {
|
16
|
+
const agentProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
17
17
|
const { ctx } = opts;
|
18
18
|
|
19
19
|
return opts.next({
|
20
20
|
ctx: {
|
21
|
-
agentModel: new AgentModel(serverDB, ctx.userId),
|
22
|
-
agentService: new AgentService(serverDB, ctx.userId),
|
23
|
-
fileModel: new FileModel(serverDB, ctx.userId),
|
24
|
-
knowledgeBaseModel: new KnowledgeBaseModel(serverDB, ctx.userId),
|
25
|
-
sessionModel: new SessionModel(serverDB, ctx.userId),
|
21
|
+
agentModel: new AgentModel(ctx.serverDB, ctx.userId),
|
22
|
+
agentService: new AgentService(ctx.serverDB, ctx.userId),
|
23
|
+
fileModel: new FileModel(ctx.serverDB, ctx.userId),
|
24
|
+
knowledgeBaseModel: new KnowledgeBaseModel(ctx.serverDB, ctx.userId),
|
25
|
+
sessionModel: new SessionModel(ctx.serverDB, ctx.userId),
|
26
26
|
},
|
27
27
|
});
|
28
28
|
});
|
@@ -90,7 +90,7 @@ export const agentRouter = router({
|
|
90
90
|
// if there is no session for user, create one
|
91
91
|
if (!item) {
|
92
92
|
// if there is no user, return default config
|
93
|
-
const user = await UserModel.findById(serverDB, ctx.userId);
|
93
|
+
const user = await UserModel.findById(ctx.serverDB, ctx.userId);
|
94
94
|
if (!user) return DEFAULT_AGENT_CONFIG;
|
95
95
|
|
96
96
|
const res = await ctx.agentService.createInbox();
|
@@ -3,8 +3,8 @@ import { z } from 'zod';
|
|
3
3
|
import { AiModelModel } from '@/database/models/aiModel';
|
4
4
|
import { UserModel } from '@/database/models/user';
|
5
5
|
import { AiInfraRepos } from '@/database/repositories/aiInfra';
|
6
|
-
import { serverDB } from '@/database/server';
|
7
6
|
import { authedProcedure, router } from '@/libs/trpc';
|
7
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
8
8
|
import { getServerGlobalConfig } from '@/server/globalConfig';
|
9
9
|
import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
|
10
10
|
import {
|
@@ -15,7 +15,7 @@ import {
|
|
15
15
|
} from '@/types/aiModel';
|
16
16
|
import { ProviderConfig } from '@/types/user/settings';
|
17
17
|
|
18
|
-
const aiModelProcedure = authedProcedure.use(async (opts) => {
|
18
|
+
const aiModelProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
19
19
|
const { ctx } = opts;
|
20
20
|
|
21
21
|
const gateKeeper = await KeyVaultsGateKeeper.initWithEnvKey();
|
@@ -24,13 +24,13 @@ const aiModelProcedure = authedProcedure.use(async (opts) => {
|
|
24
24
|
return opts.next({
|
25
25
|
ctx: {
|
26
26
|
aiInfraRepos: new AiInfraRepos(
|
27
|
-
serverDB,
|
27
|
+
ctx.serverDB,
|
28
28
|
ctx.userId,
|
29
29
|
aiProvider as Record<string, ProviderConfig>,
|
30
30
|
),
|
31
|
-
aiModelModel: new AiModelModel(serverDB, ctx.userId),
|
31
|
+
aiModelModel: new AiModelModel(ctx.serverDB, ctx.userId),
|
32
32
|
gateKeeper,
|
33
|
-
userModel: new UserModel(serverDB, ctx.userId),
|
33
|
+
userModel: new UserModel(ctx.serverDB, ctx.userId),
|
34
34
|
},
|
35
35
|
});
|
36
36
|
});
|
@@ -1,9 +1,7 @@
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
2
2
|
|
3
3
|
import { AiProviderModel } from '@/database/models/aiProvider';
|
4
|
-
import { UserModel } from '@/database/models/user';
|
5
4
|
import { AiInfraRepos } from '@/database/repositories/aiInfra';
|
6
|
-
import { serverDB } from '@/database/server';
|
7
5
|
import { getServerGlobalConfig } from '@/server/globalConfig';
|
8
6
|
import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
|
9
7
|
import { AiProviderDetailItem, AiProviderRuntimeState } from '@/types/aiProvider';
|
@@ -3,8 +3,8 @@ import { z } from 'zod';
|
|
3
3
|
import { AiProviderModel } from '@/database/models/aiProvider';
|
4
4
|
import { UserModel } from '@/database/models/user';
|
5
5
|
import { AiInfraRepos } from '@/database/repositories/aiInfra';
|
6
|
-
import { serverDB } from '@/database/server';
|
7
6
|
import { authedProcedure, router } from '@/libs/trpc';
|
7
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
8
8
|
import { getServerGlobalConfig } from '@/server/globalConfig';
|
9
9
|
import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
|
10
10
|
import {
|
@@ -16,7 +16,7 @@ import {
|
|
16
16
|
} from '@/types/aiProvider';
|
17
17
|
import { ProviderConfig } from '@/types/user/settings';
|
18
18
|
|
19
|
-
const aiProviderProcedure = authedProcedure.use(async (opts) => {
|
19
|
+
const aiProviderProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
20
20
|
const { ctx } = opts;
|
21
21
|
|
22
22
|
const { aiProvider } = await getServerGlobalConfig();
|
@@ -25,13 +25,13 @@ const aiProviderProcedure = authedProcedure.use(async (opts) => {
|
|
25
25
|
return opts.next({
|
26
26
|
ctx: {
|
27
27
|
aiInfraRepos: new AiInfraRepos(
|
28
|
-
serverDB,
|
28
|
+
ctx.serverDB,
|
29
29
|
ctx.userId,
|
30
30
|
aiProvider as Record<string, ProviderConfig>,
|
31
31
|
),
|
32
|
-
aiProviderModel: new AiProviderModel(serverDB, ctx.userId),
|
32
|
+
aiProviderModel: new AiProviderModel(ctx.serverDB, ctx.userId),
|
33
33
|
gateKeeper,
|
34
|
-
userModel: new UserModel(serverDB, ctx.userId),
|
34
|
+
userModel: new UserModel(ctx.serverDB, ctx.userId),
|
35
35
|
},
|
36
36
|
});
|
37
37
|
});
|
@@ -9,28 +9,31 @@ import { EmbeddingModel } from '@/database/models/embedding';
|
|
9
9
|
import { FileModel } from '@/database/models/file';
|
10
10
|
import { MessageModel } from '@/database/models/message';
|
11
11
|
import { knowledgeBaseFiles } from '@/database/schemas';
|
12
|
-
import { serverDB } from '@/database/server';
|
13
12
|
import { authedProcedure, router } from '@/libs/trpc';
|
13
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
14
14
|
import { keyVaults } from '@/libs/trpc/middleware/keyVaults';
|
15
15
|
import { getServerDefaultFilesConfig } from '@/server/globalConfig';
|
16
16
|
import { initAgentRuntimeWithUserPayload } from '@/server/modules/AgentRuntime';
|
17
17
|
import { ChunkService } from '@/server/services/chunk';
|
18
18
|
import { SemanticSearchSchema } from '@/types/rag';
|
19
19
|
|
20
|
-
const chunkProcedure = authedProcedure
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
ctx
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
20
|
+
const chunkProcedure = authedProcedure
|
21
|
+
.use(serverDatabase)
|
22
|
+
.use(keyVaults)
|
23
|
+
.use(async (opts) => {
|
24
|
+
const { ctx } = opts;
|
25
|
+
|
26
|
+
return opts.next({
|
27
|
+
ctx: {
|
28
|
+
asyncTaskModel: new AsyncTaskModel(ctx.serverDB, ctx.userId),
|
29
|
+
chunkModel: new ChunkModel(ctx.serverDB, ctx.userId),
|
30
|
+
chunkService: new ChunkService(ctx.userId),
|
31
|
+
embeddingModel: new EmbeddingModel(ctx.serverDB, ctx.userId),
|
32
|
+
fileModel: new FileModel(ctx.serverDB, ctx.userId),
|
33
|
+
messageModel: new MessageModel(ctx.serverDB, ctx.userId),
|
34
|
+
},
|
35
|
+
});
|
32
36
|
});
|
33
|
-
});
|
34
37
|
|
35
38
|
export const chunkRouter = router({
|
36
39
|
createEmbeddingChunksTask: chunkProcedure
|
@@ -173,7 +176,7 @@ export const chunkRouter = router({
|
|
173
176
|
let finalFileIds = input.fileIds ?? [];
|
174
177
|
|
175
178
|
if (input.knowledgeIds && input.knowledgeIds.length > 0) {
|
176
|
-
const knowledgeFiles = await serverDB.query.knowledgeBaseFiles.findMany({
|
179
|
+
const knowledgeFiles = await ctx.serverDB.query.knowledgeBaseFiles.findMany({
|
177
180
|
where: inArray(knowledgeBaseFiles.knowledgeBaseId, input.knowledgeIds),
|
178
181
|
});
|
179
182
|
|
@@ -1,13 +1,13 @@
|
|
1
1
|
import { DrizzleMigrationModel } from '@/database/models/drizzleMigration';
|
2
2
|
import { DataExporterRepos } from '@/database/repositories/dataExporter';
|
3
|
-
import { serverDB } from '@/database/server';
|
4
3
|
import { authedProcedure, router } from '@/libs/trpc';
|
4
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
5
5
|
import { ExportDatabaseData } from '@/types/export';
|
6
6
|
|
7
|
-
const exportProcedure = authedProcedure.use(async (opts) => {
|
7
|
+
const exportProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
8
8
|
const { ctx } = opts;
|
9
|
-
const dataExporterRepos = new DataExporterRepos(serverDB, ctx.userId);
|
10
|
-
const drizzleMigration = new DrizzleMigrationModel(serverDB);
|
9
|
+
const dataExporterRepos = new DataExporterRepos(ctx.serverDB, ctx.userId);
|
10
|
+
const drizzleMigration = new DrizzleMigrationModel(ctx.serverDB);
|
11
11
|
|
12
12
|
return opts.next({
|
13
13
|
ctx: { dataExporterRepos, drizzleMigration },
|
@@ -5,21 +5,21 @@ import { serverDBEnv } from '@/config/db';
|
|
5
5
|
import { AsyncTaskModel } from '@/database/models/asyncTask';
|
6
6
|
import { ChunkModel } from '@/database/models/chunk';
|
7
7
|
import { FileModel } from '@/database/models/file';
|
8
|
-
import { serverDB } from '@/database/server';
|
9
8
|
import { authedProcedure, router } from '@/libs/trpc';
|
9
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
10
10
|
import { S3 } from '@/server/modules/S3';
|
11
11
|
import { getFullFileUrl } from '@/server/utils/files';
|
12
12
|
import { AsyncTaskStatus, AsyncTaskType } from '@/types/asyncTask';
|
13
13
|
import { FileListItem, QueryFileListSchema, UploadFileSchema } from '@/types/files';
|
14
14
|
|
15
|
-
const fileProcedure = authedProcedure.use(async (opts) => {
|
15
|
+
const fileProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
16
16
|
const { ctx } = opts;
|
17
17
|
|
18
18
|
return opts.next({
|
19
19
|
ctx: {
|
20
|
-
asyncTaskModel: new AsyncTaskModel(serverDB, ctx.userId),
|
21
|
-
chunkModel: new ChunkModel(serverDB, ctx.userId),
|
22
|
-
fileModel: new FileModel(serverDB, ctx.userId),
|
20
|
+
asyncTaskModel: new AsyncTaskModel(ctx.serverDB, ctx.userId),
|
21
|
+
chunkModel: new ChunkModel(ctx.serverDB, ctx.userId),
|
22
|
+
fileModel: new FileModel(ctx.serverDB, ctx.userId),
|
23
23
|
},
|
24
24
|
});
|
25
25
|
});
|
@@ -74,7 +74,7 @@ export const fileRouter = router({
|
|
74
74
|
.query(async ({ ctx, input }): Promise<FileListItem | undefined> => {
|
75
75
|
const item = await ctx.fileModel.findById(input.id);
|
76
76
|
|
77
|
-
if (!item) throw new TRPCError({ code: '
|
77
|
+
if (!item) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
|
78
78
|
|
79
79
|
let embeddingTask = null;
|
80
80
|
if (item.embeddingTaskId) {
|
@@ -2,15 +2,15 @@ import { TRPCError } from '@trpc/server';
|
|
2
2
|
import { z } from 'zod';
|
3
3
|
|
4
4
|
import { DataImporterRepos } from '@/database/repositories/dataImporter';
|
5
|
-
import { serverDB } from '@/database/server';
|
6
5
|
import { authedProcedure, router } from '@/libs/trpc';
|
6
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
7
7
|
import { S3 } from '@/server/modules/S3';
|
8
8
|
import { ImportPgDataStructure } from '@/types/export';
|
9
9
|
import { ImportResultData, ImporterEntryData } from '@/types/importer';
|
10
10
|
|
11
|
-
const importProcedure = authedProcedure.use(async (opts) => {
|
11
|
+
const importProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
12
12
|
const { ctx } = opts;
|
13
|
-
const dataImporterService = new DataImporterRepos(serverDB, ctx.userId);
|
13
|
+
const dataImporterService = new DataImporterRepos(ctx.serverDB, ctx.userId);
|
14
14
|
|
15
15
|
return opts.next({
|
16
16
|
ctx: { dataImporterService },
|
@@ -2,16 +2,16 @@ import { z } from 'zod';
|
|
2
2
|
|
3
3
|
import { KnowledgeBaseModel } from '@/database/models/knowledgeBase';
|
4
4
|
import { insertKnowledgeBasesSchema } from '@/database/schemas';
|
5
|
-
import { serverDB } from '@/database/server';
|
6
5
|
import { authedProcedure, router } from '@/libs/trpc';
|
6
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
7
7
|
import { KnowledgeBaseItem } from '@/types/knowledgeBase';
|
8
8
|
|
9
|
-
const knowledgeBaseProcedure = authedProcedure.use(async (opts) => {
|
9
|
+
const knowledgeBaseProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
10
10
|
const { ctx } = opts;
|
11
11
|
|
12
12
|
return opts.next({
|
13
13
|
ctx: {
|
14
|
-
knowledgeBaseModel: new KnowledgeBaseModel(serverDB, ctx.userId),
|
14
|
+
knowledgeBaseModel: new KnowledgeBaseModel(ctx.serverDB, ctx.userId),
|
15
15
|
},
|
16
16
|
});
|
17
17
|
});
|
@@ -2,19 +2,20 @@ import { z } from 'zod';
|
|
2
2
|
|
3
3
|
import { MessageModel } from '@/database/models/message';
|
4
4
|
import { updateMessagePluginSchema } from '@/database/schemas';
|
5
|
-
import {
|
5
|
+
import { getServerDB } from '@/database/server';
|
6
6
|
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
|
7
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
7
8
|
import { getFullFileUrl } from '@/server/utils/files';
|
8
9
|
import { ChatMessage } from '@/types/message';
|
9
10
|
import { BatchTaskResult } from '@/types/service';
|
10
11
|
|
11
12
|
type ChatMessageList = ChatMessage[];
|
12
13
|
|
13
|
-
const messageProcedure = authedProcedure.use(async (opts) => {
|
14
|
+
const messageProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
14
15
|
const { ctx } = opts;
|
15
16
|
|
16
17
|
return opts.next({
|
17
|
-
ctx: { messageModel: new MessageModel(serverDB, ctx.userId) },
|
18
|
+
ctx: { messageModel: new MessageModel(ctx.serverDB, ctx.userId) },
|
18
19
|
});
|
19
20
|
});
|
20
21
|
|
@@ -95,6 +96,7 @@ export const messageRouter = router({
|
|
95
96
|
)
|
96
97
|
.query(async ({ input, ctx }) => {
|
97
98
|
if (!ctx.userId) return [];
|
99
|
+
const serverDB = await getServerDB();
|
98
100
|
|
99
101
|
const messageModel = new MessageModel(serverDB, ctx.userId);
|
100
102
|
|
@@ -1,15 +1,16 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
|
3
3
|
import { PluginModel } from '@/database/models/plugin';
|
4
|
-
import {
|
4
|
+
import { getServerDB } from '@/database/server';
|
5
5
|
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
|
6
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
6
7
|
import { LobeTool } from '@/types/tool';
|
7
8
|
|
8
|
-
const pluginProcedure = authedProcedure.use(async (opts) => {
|
9
|
+
const pluginProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
9
10
|
const { ctx } = opts;
|
10
11
|
|
11
12
|
return opts.next({
|
12
|
-
ctx: { pluginModel: new PluginModel(serverDB, ctx.userId) },
|
13
|
+
ctx: { pluginModel: new PluginModel(ctx.serverDB, ctx.userId) },
|
13
14
|
});
|
14
15
|
});
|
15
16
|
|
@@ -66,6 +67,7 @@ export const pluginRouter = router({
|
|
66
67
|
getPlugins: publicProcedure.query(async ({ ctx }): Promise<LobeTool[]> => {
|
67
68
|
if (!ctx.userId) return [];
|
68
69
|
|
70
|
+
const serverDB = await getServerDB();
|
69
71
|
const pluginModel = new PluginModel(serverDB, ctx.userId);
|
70
72
|
|
71
73
|
return pluginModel.query();
|
@@ -7,7 +7,6 @@ import { z } from 'zod';
|
|
7
7
|
|
8
8
|
import { DEFAULT_EMBEDDING_MODEL, DEFAULT_MODEL } from '@/const/settings';
|
9
9
|
import { FileModel } from '@/database/models/file';
|
10
|
-
import { serverDB } from '@/database/server';
|
11
10
|
import {
|
12
11
|
EvalDatasetModel,
|
13
12
|
EvalDatasetRecordModel,
|
@@ -15,6 +14,7 @@ import {
|
|
15
14
|
EvaluationRecordModel,
|
16
15
|
} from '@/database/server/models/ragEval';
|
17
16
|
import { authedProcedure, router } from '@/libs/trpc';
|
17
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
18
18
|
import { keyVaults } from '@/libs/trpc/middleware/keyVaults';
|
19
19
|
import { S3 } from '@/server/modules/S3';
|
20
20
|
import { createAsyncServerClient } from '@/server/routers/async';
|
@@ -29,20 +29,23 @@ import {
|
|
29
29
|
insertEvalEvaluationSchema,
|
30
30
|
} from '@/types/eval';
|
31
31
|
|
32
|
-
const ragEvalProcedure = authedProcedure
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
ctx
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
32
|
+
const ragEvalProcedure = authedProcedure
|
33
|
+
.use(serverDatabase)
|
34
|
+
.use(keyVaults)
|
35
|
+
.use(async (opts) => {
|
36
|
+
const { ctx } = opts;
|
37
|
+
|
38
|
+
return opts.next({
|
39
|
+
ctx: {
|
40
|
+
datasetModel: new EvalDatasetModel(ctx.userId),
|
41
|
+
fileModel: new FileModel(ctx.serverDB, ctx.userId),
|
42
|
+
datasetRecordModel: new EvalDatasetRecordModel(ctx.userId),
|
43
|
+
evaluationModel: new EvalEvaluationModel(ctx.userId),
|
44
|
+
evaluationRecordModel: new EvaluationRecordModel(ctx.userId),
|
45
|
+
s3: new S3(),
|
46
|
+
},
|
47
|
+
});
|
44
48
|
});
|
45
|
-
});
|
46
49
|
|
47
50
|
export const ragEvalRouter = router({
|
48
51
|
createDataset: ragEvalProcedure
|
@@ -3,21 +3,22 @@ import { z } from 'zod';
|
|
3
3
|
import { SessionModel } from '@/database/models/session';
|
4
4
|
import { SessionGroupModel } from '@/database/models/sessionGroup';
|
5
5
|
import { insertAgentSchema, insertSessionSchema } from '@/database/schemas';
|
6
|
-
import {
|
6
|
+
import { getServerDB } from '@/database/server';
|
7
7
|
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
|
8
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
8
9
|
import { AgentChatConfigSchema } from '@/types/agent';
|
9
10
|
import { LobeMetaDataSchema } from '@/types/meta';
|
10
11
|
import { BatchTaskResult } from '@/types/service';
|
11
12
|
import { ChatSessionList } from '@/types/session';
|
12
13
|
import { merge } from '@/utils/merge';
|
13
14
|
|
14
|
-
const sessionProcedure = authedProcedure.use(async (opts) => {
|
15
|
+
const sessionProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
15
16
|
const { ctx } = opts;
|
16
17
|
|
17
18
|
return opts.next({
|
18
19
|
ctx: {
|
19
|
-
sessionGroupModel: new SessionGroupModel(serverDB, ctx.userId),
|
20
|
-
sessionModel: new SessionModel(serverDB, ctx.userId),
|
20
|
+
sessionGroupModel: new SessionGroupModel(ctx.serverDB, ctx.userId),
|
21
|
+
sessionModel: new SessionModel(ctx.serverDB, ctx.userId),
|
21
22
|
},
|
22
23
|
});
|
23
24
|
});
|
@@ -95,6 +96,7 @@ export const sessionRouter = router({
|
|
95
96
|
sessions: [],
|
96
97
|
};
|
97
98
|
|
99
|
+
const serverDB = await getServerDB();
|
98
100
|
const sessionModel = new SessionModel(serverDB, ctx.userId);
|
99
101
|
|
100
102
|
return sessionModel.queryWithGroups();
|
@@ -2,16 +2,16 @@ import { z } from 'zod';
|
|
2
2
|
|
3
3
|
import { SessionGroupModel } from '@/database/models/sessionGroup';
|
4
4
|
import { insertSessionGroupSchema } from '@/database/schemas';
|
5
|
-
import { serverDB } from '@/database/server';
|
6
5
|
import { authedProcedure, router } from '@/libs/trpc';
|
6
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
7
7
|
import { SessionGroupItem } from '@/types/session';
|
8
8
|
|
9
|
-
const sessionProcedure = authedProcedure.use(async (opts) => {
|
9
|
+
const sessionProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
10
10
|
const { ctx } = opts;
|
11
11
|
|
12
12
|
return opts.next({
|
13
13
|
ctx: {
|
14
|
-
sessionGroupModel: new SessionGroupModel(serverDB, ctx.userId),
|
14
|
+
sessionGroupModel: new SessionGroupModel(ctx.serverDB, ctx.userId),
|
15
15
|
},
|
16
16
|
});
|
17
17
|
});
|
@@ -3,17 +3,17 @@ import { z } from 'zod';
|
|
3
3
|
import { MessageModel } from '@/database/models/message';
|
4
4
|
import { ThreadModel } from '@/database/models/thread';
|
5
5
|
import { insertThreadSchema } from '@/database/schemas';
|
6
|
-
import { serverDB } from '@/database/server';
|
7
6
|
import { authedProcedure, router } from '@/libs/trpc';
|
7
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
8
8
|
import { ThreadItem, createThreadSchema } from '@/types/topic/thread';
|
9
9
|
|
10
|
-
const threadProcedure = authedProcedure.use(async (opts) => {
|
10
|
+
const threadProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
11
11
|
const { ctx } = opts;
|
12
12
|
|
13
13
|
return opts.next({
|
14
14
|
ctx: {
|
15
|
-
messageModel: new MessageModel(serverDB, ctx.userId),
|
16
|
-
threadModel: new ThreadModel(serverDB, ctx.userId),
|
15
|
+
messageModel: new MessageModel(ctx.serverDB, ctx.userId),
|
16
|
+
threadModel: new ThreadModel(ctx.serverDB, ctx.userId),
|
17
17
|
},
|
18
18
|
});
|
19
19
|
});
|
@@ -1,15 +1,16 @@
|
|
1
1
|
import { z } from 'zod';
|
2
2
|
|
3
3
|
import { TopicModel } from '@/database/models/topic';
|
4
|
-
import {
|
4
|
+
import { getServerDB } from '@/database/server';
|
5
5
|
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
|
6
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
6
7
|
import { BatchTaskResult } from '@/types/service';
|
7
8
|
|
8
|
-
const topicProcedure = authedProcedure.use(async (opts) => {
|
9
|
+
const topicProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
|
9
10
|
const { ctx } = opts;
|
10
11
|
|
11
12
|
return opts.next({
|
12
|
-
ctx: { topicModel: new TopicModel(serverDB, ctx.userId) },
|
13
|
+
ctx: { topicModel: new TopicModel(ctx.serverDB, ctx.userId) },
|
13
14
|
});
|
14
15
|
});
|
15
16
|
|
@@ -101,6 +102,7 @@ export const topicRouter = router({
|
|
101
102
|
.query(async ({ input, ctx }) => {
|
102
103
|
if (!ctx.userId) return [];
|
103
104
|
|
105
|
+
const serverDB = await getServerDB();
|
104
106
|
const topicModel = new TopicModel(serverDB, ctx.userId);
|
105
107
|
|
106
108
|
return topicModel.query(input);
|
@@ -5,10 +5,10 @@ import { enableClerk } from '@/const/auth';
|
|
5
5
|
import { MessageModel } from '@/database/models/message';
|
6
6
|
import { SessionModel } from '@/database/models/session';
|
7
7
|
import { UserModel, UserNotFoundError } from '@/database/models/user';
|
8
|
-
import { serverDB } from '@/database/server';
|
9
8
|
import { ClerkAuth } from '@/libs/clerk-auth';
|
10
9
|
import { LobeNextAuthDbAdapter } from '@/libs/next-auth/adapter';
|
11
10
|
import { authedProcedure, router } from '@/libs/trpc';
|
11
|
+
import { serverDatabase } from '@/libs/trpc/lambda';
|
12
12
|
import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
|
13
13
|
import { UserService } from '@/server/services/user';
|
14
14
|
import {
|
@@ -19,12 +19,12 @@ import {
|
|
19
19
|
} from '@/types/user';
|
20
20
|
import { UserSettings } from '@/types/user/settings';
|
21
21
|
|
22
|
-
const userProcedure = authedProcedure.use(async (
|
23
|
-
return
|
22
|
+
const userProcedure = authedProcedure.use(serverDatabase).use(async ({ ctx, next }) => {
|
23
|
+
return next({
|
24
24
|
ctx: {
|
25
25
|
clerkAuth: new ClerkAuth(),
|
26
|
-
nextAuthDbAdapter: LobeNextAuthDbAdapter(serverDB),
|
27
|
-
userModel: new UserModel(serverDB,
|
26
|
+
nextAuthDbAdapter: LobeNextAuthDbAdapter(ctx.serverDB),
|
27
|
+
userModel: new UserModel(ctx.serverDB, ctx.userId),
|
28
28
|
},
|
29
29
|
});
|
30
30
|
});
|
@@ -77,10 +77,10 @@ export const userRouter = router({
|
|
77
77
|
}
|
78
78
|
}
|
79
79
|
|
80
|
-
const messageModel = new MessageModel(serverDB, ctx.userId);
|
80
|
+
const messageModel = new MessageModel(ctx.serverDB, ctx.userId);
|
81
81
|
const hasMoreThan4Messages = await messageModel.hasMoreThanN(4);
|
82
82
|
|
83
|
-
const sessionModel = new SessionModel(serverDB, ctx.userId);
|
83
|
+
const sessionModel = new SessionModel(ctx.serverDB, ctx.userId);
|
84
84
|
const hasAnyMessages = await messageModel.hasMoreThanN(0);
|
85
85
|
const hasExtraSession = await sessionModel.hasMoreThanN(1);
|
86
86
|
|
@@ -6,7 +6,6 @@ import { UserModel } from '@/database/models/user';
|
|
6
6
|
import { UserItem } from '@/database/schemas';
|
7
7
|
import { serverDB } from '@/database/server';
|
8
8
|
import { pino } from '@/libs/logger';
|
9
|
-
import { LobeNextAuthDbAdapter } from '@/libs/next-auth/adapter';
|
10
9
|
|
11
10
|
import { NextAuthUserService } from './index';
|
12
11
|
|
@@ -23,7 +22,7 @@ vi.mock('@/database/server');
|
|
23
22
|
describe('NextAuthUserService', () => {
|
24
23
|
let service: NextAuthUserService;
|
25
24
|
|
26
|
-
beforeEach(() => {
|
25
|
+
beforeEach(async () => {
|
27
26
|
vi.clearAllMocks();
|
28
27
|
service = new NextAuthUserService();
|
29
28
|
});
|