70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
/**
|
|
* 发送消息响应
|
|
*
|
|
*/
|
|
import { z } from "zod";
|
|
import {
|
|
booleanOrFalse,
|
|
booleanOrTrue,
|
|
numberOrLazy,
|
|
numberOrZero,
|
|
stringOrEmpty,
|
|
} from "../../nullable-defaults";
|
|
import {
|
|
ChatImageSchema,
|
|
ChatLockDetailSchema,
|
|
type ChatImageData,
|
|
type ChatLockDetailData,
|
|
} from "../chat_payloads";
|
|
|
|
export const ChatSendResponseSchema = z.object({
|
|
reply: stringOrEmpty,
|
|
audioUrl: stringOrEmpty,
|
|
messageId: stringOrEmpty,
|
|
isGuest: booleanOrFalse,
|
|
// 函数式 default —— 每次 parse 时重新调 Date.now()(后端不返回 timestamp 时用当下时间)
|
|
timestamp: numberOrLazy(() => Date.now()),
|
|
image: ChatImageSchema,
|
|
lockDetail: ChatLockDetailSchema,
|
|
canSendMessage: booleanOrTrue,
|
|
creditBalance: numberOrZero,
|
|
creditsCharged: numberOrZero,
|
|
requiredCredits: numberOrZero,
|
|
shortfallCredits: numberOrZero,
|
|
});
|
|
|
|
export type ChatSendResponseInput = z.input<typeof ChatSendResponseSchema>;
|
|
export type ChatSendResponseData = z.output<typeof ChatSendResponseSchema>;
|
|
|
|
export class ChatSendResponse {
|
|
declare readonly reply: string;
|
|
declare readonly audioUrl: string;
|
|
declare readonly messageId: string;
|
|
declare readonly timestamp: number;
|
|
declare readonly image: ChatImageData;
|
|
declare readonly lockDetail: ChatLockDetailData;
|
|
declare readonly canSendMessage: boolean;
|
|
declare readonly creditBalance: number;
|
|
declare readonly creditsCharged: number;
|
|
declare readonly requiredCredits: number;
|
|
declare readonly shortfallCredits: number;
|
|
|
|
private constructor(input: ChatSendResponseInput) {
|
|
const data = ChatSendResponseSchema.parse(input);
|
|
Object.assign(this, data);
|
|
Object.freeze(this);
|
|
}
|
|
|
|
static from(input: ChatSendResponseInput): ChatSendResponse {
|
|
return new ChatSendResponse(input);
|
|
}
|
|
|
|
static fromJson(json: unknown): ChatSendResponse {
|
|
return ChatSendResponse.from(json as ChatSendResponseInput);
|
|
}
|
|
|
|
toJson(): ChatSendResponseData {
|
|
return ChatSendResponseSchema.parse(this);
|
|
}
|
|
}
|