37 lines
994 B
TypeScript
37 lines
994 B
TypeScript
/**
|
||
* 聊天 UI 消息模型
|
||
*
|
||
* 原始 Dart: lib/ui/chat/models/message.dart(freezed + 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(),
|
||
/** 图片 URL(base64 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",
|
||
}),
|
||
});
|
||
},
|
||
};
|