import { z } from 'zod'; import { isoDateTimeSchema } from './time'; export const messageRoleSchema = z.enum(['user', 'assistant', 'tool', 'system']); export type MessageRole = z.infer; export const textContentSchema = z.object({ type: z.literal('text'), text: z.string(), }); export type TextContent = z.infer; export const toolUseContentSchema = z.object({ type: z.literal('tool_use'), tool_call_id: z.string().min(1), tool_name: z.string().min(1), input: z.unknown(), }); export type ToolUseContent = z.infer; export const toolResultContentSchema = z.object({ type: z.literal('tool_result'), tool_call_id: z.string().min(1), output: z.unknown(), is_error: z.boolean().optional(), }); export type ToolResultContent = z.infer; export const imageSourceSchema = z.discriminatedUnion('kind', [ z.object({ kind: z.literal('url'), url: z.string().min(1) }), z.object({ kind: z.literal('base64'), media_type: z.string().min(1), data: z.string().min(1), }), z.object({ kind: z.literal('file'), file_id: z.string().min(1) }), ]); export type ImageSource = z.infer; export const imageContentSchema = z.object({ type: z.literal('image'), source: imageSourceSchema, }); export type ImageContent = z.infer; // Video uses the same source shape as image (url / base64 / uploaded file id). export const videoContentSchema = z.object({ type: z.literal('video'), source: imageSourceSchema, }); export type VideoContent = z.infer; export const fileContentSchema = z.object({ type: z.literal('file'), file_id: z.string().min(1), name: z.string(), media_type: z.string().min(1), size: z.number().int().nonnegative(), }); export type FileContent = z.infer; export const thinkingContentSchema = z.object({ type: z.literal('thinking'), thinking: z.string(), signature: z.string().optional(), }); export type ThinkingContent = z.infer; export const messageContentSchema = z.discriminatedUnion('type', [ textContentSchema, toolUseContentSchema, toolResultContentSchema, imageContentSchema, videoContentSchema, fileContentSchema, thinkingContentSchema, ]); export type MessageContent = z.infer; export const messageSchema = z.object({ id: z.string().min(1), session_id: z.string().min(1), role: messageRoleSchema, content: z.array(messageContentSchema), created_at: isoDateTimeSchema, prompt_id: z.string().min(1).optional(), parent_message_id: z.string().min(1).optional(), metadata: z.record(z.string(), z.unknown()).optional(), }); export type Message = z.infer;