54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
/**
|
|
* 聊天历史响应
|
|
*
|
|
*/
|
|
import { z } from "zod";
|
|
|
|
import {
|
|
arrayOrEmpty,
|
|
booleanOrFalse,
|
|
numberOrZero,
|
|
} from "../../nullable-defaults";
|
|
import { ChatMessage, ChatMessageSchema } from "../chat_message";
|
|
|
|
export const ChatHistoryResponseSchema = z.object({
|
|
messages: arrayOrEmpty(ChatMessageSchema),
|
|
total: numberOrZero,
|
|
limit: numberOrZero,
|
|
offset: numberOrZero,
|
|
isVip: booleanOrFalse,
|
|
privateFreeLimit: numberOrZero,
|
|
privateUsedToday: numberOrZero,
|
|
privateCanViewFree: booleanOrFalse,
|
|
});
|
|
|
|
export type ChatHistoryResponseInput = z.input<typeof ChatHistoryResponseSchema>;
|
|
export type ChatHistoryResponseData = z.output<typeof ChatHistoryResponseSchema>;
|
|
|
|
export class ChatHistoryResponse {
|
|
declare readonly messages: ChatMessage[];
|
|
declare readonly total: number;
|
|
declare readonly limit: number;
|
|
|
|
private constructor(input: ChatHistoryResponseInput) {
|
|
const data = ChatHistoryResponseSchema.parse(input);
|
|
Object.assign(this, {
|
|
...data,
|
|
messages: data.messages.map((m) => ChatMessage.from(m)),
|
|
});
|
|
Object.freeze(this);
|
|
}
|
|
|
|
static from(input: ChatHistoryResponseInput): ChatHistoryResponse {
|
|
return new ChatHistoryResponse(input);
|
|
}
|
|
|
|
static fromJson(json: unknown): ChatHistoryResponse {
|
|
return ChatHistoryResponse.from(json as ChatHistoryResponseInput);
|
|
}
|
|
|
|
toJson(): ChatHistoryResponseData {
|
|
return ChatHistoryResponseSchema.parse(this);
|
|
}
|
|
}
|