40 lines
979 B
TypeScript
40 lines
979 B
TypeScript
/**
|
|
* 聊天历史中的单条消息 DTO
|
|
*/
|
|
import {
|
|
ChatMessageSchema,
|
|
type ChatMessageInput,
|
|
type ChatMessageData,
|
|
type ChatImageData,
|
|
type ChatLockDetailData,
|
|
} from "@/data/schemas/chat";
|
|
|
|
export class ChatMessage {
|
|
declare readonly role: string;
|
|
declare readonly type: string;
|
|
declare readonly content: string;
|
|
declare readonly id: string;
|
|
declare readonly createdAt: string;
|
|
declare readonly audioUrl: string | null;
|
|
declare readonly image: ChatImageData;
|
|
declare readonly lockDetail: ChatLockDetailData;
|
|
|
|
private constructor(input: ChatMessageInput) {
|
|
const data = ChatMessageSchema.parse(input);
|
|
Object.assign(this, data);
|
|
Object.freeze(this);
|
|
}
|
|
|
|
static from(input: ChatMessageInput): ChatMessage {
|
|
return new ChatMessage(input);
|
|
}
|
|
|
|
static fromJson(json: unknown): ChatMessage {
|
|
return ChatMessage.from(json as ChatMessageInput);
|
|
}
|
|
|
|
toJson(): ChatMessageData {
|
|
return ChatMessageSchema.parse(this);
|
|
}
|
|
}
|