♻️ refactor: optimize files schema definition (#10403)

This commit is contained in:
YuTengjing
2025-11-24 23:03:09 +08:00
committed by GitHub
parent bd2e8387dc
commit cf28c87d3e
3 changed files with 19 additions and 15 deletions

View File

@@ -26,7 +26,12 @@ export class AgentModel {
const knowledge = await this.getAgentAssignedKnowledge(id);
// Fetch document content for enabled files
const enabledFileIds = knowledge.files.filter((f) => f.enabled).map((f) => f.id);
const enabledFileIds = knowledge.files
.filter((f) => f.enabled)
.map((f) => f.id)
.filter((id) => id !== undefined);
let files: Array<(typeof knowledge.files)[number] & { content?: string | null }> =
knowledge.files;
if (enabledFileIds.length > 0) {
const documentsData = await this.db.query.documents.findMany({
@@ -34,14 +39,13 @@ export class AgentModel {
});
const documentMap = new Map(documentsData.map((doc) => [doc.fileId, doc.content]));
knowledge.files = knowledge.files.map((file) => ({
files = knowledge.files.map((file) => ({
...file,
content: file.enabled && file.id ? documentMap.get(file.id) : undefined,
}));
}
return { ...agent, ...knowledge };
return { ...agent, ...knowledge, files };
};
getAgentAssignedKnowledge = async (id: string) => {

View File

@@ -1,6 +1,7 @@
/* eslint-disable sort-keys-fix/sort-keys-fix */
import { isNotNull } from 'drizzle-orm';
import {
AnyPgColumn,
boolean,
index,
integer,
@@ -41,7 +42,6 @@ export type GlobalFileItem = typeof globalFiles.$inferSelect;
/**
* Documents table - Stores file content or web search results
*/
// @ts-ignore
export const documents = pgTable(
'documents',
{
@@ -72,15 +72,12 @@ export const documents = pgTable(
source: text('source').notNull(), // File path or web URL
// Associated file (optional)
// Forward reference to files table defined below
// forward reference needs AnyPgColumn to avoid circular type inference
// eslint-disable-next-line @typescript-eslint/no-use-before-define
// @ts-expect-error - files is defined later in this file, forward reference is valid at runtime
// eslint-disable-next-line @typescript-eslint/no-use-before-define
fileId: text('file_id').references(() => files.id, { onDelete: 'set null' }),
fileId: text('file_id').references((): AnyPgColumn => files.id, { onDelete: 'set null' }),
// Parent document (for folder hierarchy structure)
// @ts-ignore
parentId: varchar('parent_id', { length: 255 }).references(() => documents.id, {
parentId: varchar('parent_id', { length: 255 }).references((): AnyPgColumn => documents.id, {
onDelete: 'set null',
}),
@@ -113,7 +110,6 @@ export type NewDocument = typeof documents.$inferInsert;
export type DocumentItem = typeof documents.$inferSelect;
export const insertDocumentSchema = createInsertSchema(documents);
// @ts-ignore
export const files = pgTable(
'files',
{
@@ -140,8 +136,7 @@ export const files = pgTable(
source: text('source').$type<FileSource>(),
// Parent Folder or Document
// @ts-ignore
parentId: varchar('parent_id', { length: 255 }).references(() => documents.id, {
parentId: varchar('parent_id', { length: 255 }).references((): AnyPgColumn => documents.id, {
onDelete: 'set null',
}),

View File

@@ -162,7 +162,12 @@ export const chunkRouter = router({
}
// 2. Find existing parsed document
let document = await ctx.documentModel.findByFileId(fileId);
let document:
| {
content: string | null;
metadata: Record<string, any> | null;
}
| undefined = await ctx.documentModel.findByFileId(fileId);
// 3. If not exists, parse the file
if (!document) {