Files
cozsweet-frontend-nextjs/src/data/dto/chat/ui_message.ts
T

37 lines
994 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 聊天 UI 消息模型
*
* 原始 Dart: lib/ui/chat/models/message.dartfreezed + json_serializable 生成)
*
* 字段:content、isFromAI、date、imageUrl(可选)、voiceUrl(可选)
*/
import { z } from "zod";
export const UiMessageSchema = z.object({
content: z.string(),
isFromAI: z.boolean(),
/** 显示用时间戳(HH:mm */
date: z.string(),
/** 图片 URLbase64 data URL 或 http URL */
imageUrl: z.string().optional(),
/** 语音 URL */
voiceUrl: z.string().optional(),
});
export type UiMessage = z.infer<typeof UiMessageSchema>;
/** 工厂函数(替代 Dart `UiMessage(content:..., isFromAI:...)` 构造)。 */
export const UiMessage = {
create(input: Omit<UiMessage, "date"> & { date?: string }): UiMessage {
return UiMessageSchema.parse({
...input,
date:
input.date ??
new Date().toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
}),
});
},
};