Skip to content

Commit

Permalink
Merge pull request #475 from IvanLi-CN/fix/rebuild-index-avoid-update…
Browse files Browse the repository at this point in the history
…dat-update

fix(ai): Preserve original updatedAt timestamp during memos import and reindexing
  • Loading branch information
blinko-space authored Feb 5, 2025
2 parents 3ed0f49 + 7e55724 commit 478391f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
21 changes: 12 additions & 9 deletions src/server/plugins/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class AiService {
}
}

static async embeddingUpsert({ id, content, type, createTime }: { id: number, content: string, type: 'update' | 'insert', createTime: Date }) {
static async embeddingUpsert({ id, content, type, createTime, updatedAt }: { id: number, content: string, type: 'update' | 'insert', createTime: Date, updatedAt?: Date }) {
try {
const { VectorStore, MarkdownSplitter } = await AiModelFactory.GetProvider()
const config = await AiModelFactory.globalConfig()
Expand Down Expand Up @@ -111,8 +111,10 @@ export class AiService {
data: {
metadata: {
isIndexed: true
}
}
},
updatedAt,
},

})
} catch (error) {
console.log(error)
Expand All @@ -131,7 +133,7 @@ export class AiService {
}

//api/file/123.pdf
static async embeddingInsertAttachments({ id, filePath }: { id: number, filePath: string }) {
static async embeddingInsertAttachments({ id, updatedAt, filePath }: { id: number, updatedAt?: Date, filePath: string }) {
try {
// const note = await prisma.notes.findUnique({ where: { id } })
// //@ts-ignore
Expand Down Expand Up @@ -159,7 +161,8 @@ export class AiService {
metadata: {
isIndexed: true,
isAttachmentsIndexed: true
}
},
updatedAt
}
})
} catch (error) {
Expand Down Expand Up @@ -244,6 +247,7 @@ export class AiService {
if (note?.content != '') {
const { ok, error } = await AiService.embeddingUpsert({
createTime: note.createdAt,
updatedAt: note.updatedAt,
id: note?.id,
content: note?.content,
type: 'update' as const
Expand All @@ -263,12 +267,11 @@ export class AiService {
};
}
}
//@ts-ignore
if (note?.attachments) {
//@ts-ignore
for (const attachment of note?.attachments) {
for (const attachment of note.attachments) {
const { ok, error } = await AiService.embeddingInsertAttachments({
id: note?.id,
id: note.id,
updatedAt: note.updatedAt,
filePath: attachment?.path
});
if (ok) {
Expand Down
9 changes: 2 additions & 7 deletions src/server/plugins/memos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ export class Memos {

const note = await userCaller(ctx).notes.upsert({
content: row?.content,
createdAt: new Date(row!.created_ts * 1000),
updatedAt: new Date(row!.updated_ts * 1000),
});

if (note) {
await prisma.notes.update({
where: { id: note.id },
data: {
createdAt: new Date(row!.created_ts * 1000),
updatedAt: new Date(row!.updated_ts * 1000),
}
});
yield {
type: 'success',
content: note.content.slice(0, 30),
Expand Down
8 changes: 4 additions & 4 deletions src/server/routers/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,9 @@ export const noteRouter = router({
}

if (config?.isUseAI) {
AiService.embeddingUpsert({ id: note.id, content: note.content, type: 'update', createTime: note.createdAt! })
AiService.embeddingUpsert({ id: note.id, content: note.content, type: 'update', createTime: note.createdAt!, updatedAt: note.updatedAt })
for (const attachment of attachments) {
AiService.embeddingInsertAttachments({ id: note.id, filePath: attachment.path })
AiService.embeddingInsertAttachments({ id: note.id, updatedAt: note.updatedAt, filePath: attachment.path })
}
}
SendWebhook({ ...note, attachments }, isRecycle ? 'delete' : 'update', ctx)
Expand Down Expand Up @@ -732,9 +732,9 @@ export const noteRouter = router({
SendWebhook({ ...note, attachments }, 'create', ctx)

if (config?.isUseAI) {
AiService.embeddingUpsert({ id: note.id, content: note.content, type: 'insert', createTime: note.createdAt! })
AiService.embeddingUpsert({ id: note.id, content: note.content, type: 'insert', createTime: note.createdAt!, updatedAt: note.updatedAt })
for (const attachment of attachments) {
AiService.embeddingInsertAttachments({ id: note.id, filePath: attachment.path })
AiService.embeddingInsertAttachments({ id: note.id, updatedAt: note.updatedAt, filePath: attachment.path })
}
}
return note
Expand Down

0 comments on commit 478391f

Please sign in to comment.