mirror of
https://github.com/lobehub/lobe-chat.git
synced 2026-06-14 03:30:19 +00:00
⚡️ perf: fix search agent slow (#9261)
* try to fix search agent issue * Update session.ts
This commit is contained in:
@@ -113,3 +113,5 @@ CLAUDE.local.md
|
||||
*.ppt*
|
||||
*.doc*
|
||||
*.xls*
|
||||
|
||||
prd
|
||||
|
||||
@@ -24,6 +24,8 @@ table agents {
|
||||
|
||||
indexes {
|
||||
(client_id, user_id) [name: 'client_id_user_id_unique', unique]
|
||||
title [name: 'agents_title_idx']
|
||||
description [name: 'agents_description_idx']
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
CREATE INDEX "agents_title_idx" ON "agents" USING btree ("title");--> statement-breakpoint
|
||||
CREATE INDEX "agents_description_idx" ON "agents" USING btree ("description");
|
||||
File diff suppressed because it is too large
Load Diff
@@ -217,6 +217,13 @@
|
||||
"when": 1756298669289,
|
||||
"tag": "0030_add_group_chat",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 31,
|
||||
"version": "7",
|
||||
"when": 1757902833213,
|
||||
"tag": "0031_add_agent_index",
|
||||
"breakpoints": true
|
||||
}
|
||||
],
|
||||
"version": "6"
|
||||
|
||||
@@ -578,5 +578,14 @@
|
||||
"bps": true,
|
||||
"folderMillis": 1756298669289,
|
||||
"hash": "3af468ca75761ca4ca4e32ba8704728f0499aa935bfe00ae5aabfa8405a18bd4"
|
||||
},
|
||||
{
|
||||
"sql": [
|
||||
"CREATE INDEX \"agents_title_idx\" ON \"agents\" USING btree (\"title\");",
|
||||
"\nCREATE INDEX \"agents_description_idx\" ON \"agents\" USING btree (\"description\");"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1757902833213,
|
||||
"hash": "7f76a42dcaae9ca7de30a6eec432a3b52e64fa44b31828e3318d8b815f396624"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -18,14 +18,6 @@ import type { PartialDeep } from 'type-fest';
|
||||
import { DEFAULT_INBOX_AVATAR } from '@/const/meta';
|
||||
import { INBOX_SESSION_ID } from '@/const/session';
|
||||
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
|
||||
import { LobeChatDatabase } from '../type';
|
||||
import {
|
||||
genEndDateWhere,
|
||||
genRangeWhere,
|
||||
genStartDateWhere,
|
||||
genWhere,
|
||||
} from '../utils/genWhere';
|
||||
import { idGenerator } from '../utils/idGenerator';
|
||||
import { LobeAgentConfig } from '@/types/agent';
|
||||
import { ChatSessionList, LobeAgentSession, SessionRankItem } from '@/types/session';
|
||||
import { merge } from '@/utils/merge';
|
||||
@@ -41,6 +33,9 @@ import {
|
||||
sessions,
|
||||
topics,
|
||||
} from '../schemas';
|
||||
import { LobeChatDatabase } from '../type';
|
||||
import { genEndDateWhere, genRangeWhere, genStartDateWhere, genWhere } from '../utils/genWhere';
|
||||
import { idGenerator } from '../utils/idGenerator';
|
||||
|
||||
export class SessionModel {
|
||||
private userId: string;
|
||||
@@ -461,28 +456,36 @@ export class SessionModel {
|
||||
}) => {
|
||||
const { keyword, pageSize = 9999, current = 0 } = params;
|
||||
const offset = current * pageSize;
|
||||
const results = await this.db.query.agents.findMany({
|
||||
limit: pageSize,
|
||||
offset,
|
||||
orderBy: [desc(agents.updatedAt)],
|
||||
where: and(
|
||||
eq(agents.userId, this.userId),
|
||||
or(
|
||||
like(sql`lower(${agents.title})` as unknown as Column, `%${keyword.toLowerCase()}%`),
|
||||
like(
|
||||
sql`lower(${agents.description})` as unknown as Column,
|
||||
`%${keyword.toLowerCase()}%`,
|
||||
|
||||
try {
|
||||
const results = await this.db.query.agents.findMany({
|
||||
limit: pageSize,
|
||||
offset,
|
||||
orderBy: [desc(agents.updatedAt)],
|
||||
where: and(
|
||||
eq(agents.userId, this.userId),
|
||||
or(
|
||||
like(sql`lower(${agents.title})` as unknown as Column, `%${keyword.toLowerCase()}%`),
|
||||
like(
|
||||
sql`lower(${agents.description})` as unknown as Column,
|
||||
`%${keyword.toLowerCase()}%`,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
with: { agentsToSessions: { columns: {}, with: { session: true } } },
|
||||
});
|
||||
try {
|
||||
// @ts-expect-error
|
||||
return results.map((item) => item.agentsToSessions[0].session);
|
||||
with: { agentsToSessions: { columns: {}, with: { session: true } } },
|
||||
});
|
||||
|
||||
// 过滤和映射结果,确保有有效的 session 关联
|
||||
return (
|
||||
results
|
||||
.filter((item) => item.agentsToSessions && item.agentsToSessions.length > 0)
|
||||
// @ts-expect-error
|
||||
.map((item) => item.agentsToSessions[0].session)
|
||||
.filter((session) => session !== null && session !== undefined)
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('findSessionsByKeywords error:', e);
|
||||
console.error('findSessionsByKeywords error:', e, { keyword });
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* eslint-disable sort-keys-fix/sort-keys-fix */
|
||||
import {
|
||||
boolean,
|
||||
index,
|
||||
jsonb,
|
||||
pgTable,
|
||||
primaryKey,
|
||||
@@ -61,6 +62,8 @@ export const agents = pgTable(
|
||||
},
|
||||
(t) => ({
|
||||
clientIdUnique: uniqueIndex('client_id_user_id_unique').on(t.clientId, t.userId),
|
||||
titleIndex: index('agents_title_idx').on(t.title),
|
||||
descriptionIndex: index('agents_description_idx').on(t.description),
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user