forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
10 changed files
with
1,516 additions
and
7,233 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { INBOX_SESSION_ID } from '@/const/session'; | ||
import { clientDB } from '@/database/client/db'; | ||
import { MessageModel } from '@/database/server/models/message'; | ||
import { ThreadModel } from '@/database/server/models/thread'; | ||
import { BaseClientService } from '@/services/baseClientService'; | ||
import { CreateMessageParams } from '@/types/message'; | ||
import { CreateThreadParams, ThreadItem } from '@/types/topic'; | ||
|
||
import { IThreadService } from './type'; | ||
|
||
interface CreateThreadWithMessageParams extends CreateThreadParams { | ||
message: CreateMessageParams; | ||
} | ||
export class ClientService extends BaseClientService implements IThreadService { | ||
private get threadModel(): ThreadModel { | ||
return new ThreadModel(clientDB as any, this.userId); | ||
} | ||
private get messageModel(): MessageModel { | ||
return new MessageModel(clientDB as any, this.userId); | ||
} | ||
|
||
getThreads = async (topicId: string): Promise<ThreadItem[]> => { | ||
return this.threadModel.queryByTopicId(topicId); | ||
}; | ||
|
||
createThreadWithMessage = async ( | ||
input: CreateThreadWithMessageParams, | ||
): Promise<{ messageId: string; threadId: string }> => { | ||
const thread = await this.threadModel.create({ | ||
parentThreadId: input.parentThreadId, | ||
sourceMessageId: input.sourceMessageId, | ||
title: input.message.content.slice(0, 20), | ||
topicId: input.topicId, | ||
type: input.type, | ||
}); | ||
|
||
const message = await this.messageModel.create({ | ||
...input.message, | ||
sessionId: this.toDbSessionId(input.message.sessionId) as string, | ||
threadId: thread?.id, | ||
}); | ||
|
||
return { messageId: message?.id, threadId: thread?.id }; | ||
}; | ||
|
||
updateThread(id: string, data: Partial<ThreadItem>): Promise<any> { | ||
return this.threadModel.update(id, data); | ||
} | ||
|
||
removeThread(id: string): Promise<any> { | ||
return this.threadModel.delete(id); | ||
} | ||
|
||
private toDbSessionId(sessionId: string | undefined) { | ||
return sessionId === INBOX_SESSION_ID ? null : sessionId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ClientService } from './client'; | ||
import { ServerService } from './server'; | ||
|
||
export const threadService = | ||
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable typescript-sort-keys/interface */ | ||
import { CreateMessageParams } from '@/types/message'; | ||
import { CreateThreadParams, ThreadItem } from '@/types/topic'; | ||
|
||
interface CreateThreadWithMessageParams extends CreateThreadParams { | ||
message: CreateMessageParams; | ||
} | ||
|
||
export interface IThreadService { | ||
getThreads(topicId: string): Promise<ThreadItem[]>; | ||
|
||
createThreadWithMessage({ | ||
message, | ||
...params | ||
}: CreateThreadWithMessageParams): Promise<{ messageId: string; threadId: string }>; | ||
|
||
updateThread(id: string, data: Partial<ThreadItem>): Promise<any>; | ||
// | ||
removeThread(id: string): Promise<any>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters