|
| 1 | +/** |
| 2 | + * The wire `Message` shape — the legacy REST/streaming message format served |
| 3 | + * on the `messages`, `snapshot`, and `sessions` (`:undo`) edge surfaces. |
| 4 | + * Defined next to `messageProjection.ts`, which projects `ContextMessage` |
| 5 | + * into this shape; consumed by the `messageLegacy` edge adapter and the |
| 6 | + * transports. |
| 7 | + */ |
| 8 | + |
| 9 | +import { z } from 'zod'; |
| 10 | + |
| 11 | +import { isoDateTimeSchema } from '#/_base/utils/isoDateTime'; |
| 12 | + |
| 13 | +export const messageRoleSchema = z.enum(['user', 'assistant', 'tool', 'system']); |
| 14 | +export type MessageRole = z.infer<typeof messageRoleSchema>; |
| 15 | + |
| 16 | +export const textContentSchema = z.object({ |
| 17 | + type: z.literal('text'), |
| 18 | + text: z.string(), |
| 19 | +}); |
| 20 | +export type TextContent = z.infer<typeof textContentSchema>; |
| 21 | + |
| 22 | +export const toolUseContentSchema = z.object({ |
| 23 | + type: z.literal('tool_use'), |
| 24 | + tool_call_id: z.string().min(1), |
| 25 | + tool_name: z.string().min(1), |
| 26 | + input: z.unknown(), |
| 27 | +}); |
| 28 | +export type ToolUseContent = z.infer<typeof toolUseContentSchema>; |
| 29 | + |
| 30 | +export const toolResultContentSchema = z.object({ |
| 31 | + type: z.literal('tool_result'), |
| 32 | + tool_call_id: z.string().min(1), |
| 33 | + output: z.unknown(), |
| 34 | + is_error: z.boolean().optional(), |
| 35 | +}); |
| 36 | +export type ToolResultContent = z.infer<typeof toolResultContentSchema>; |
| 37 | + |
| 38 | +export const imageSourceSchema = z.discriminatedUnion('kind', [ |
| 39 | + z.object({ kind: z.literal('url'), url: z.string().min(1) }), |
| 40 | + z.object({ |
| 41 | + kind: z.literal('base64'), |
| 42 | + media_type: z.string().min(1), |
| 43 | + data: z.string().min(1), |
| 44 | + }), |
| 45 | + z.object({ kind: z.literal('file'), file_id: z.string().min(1) }), |
| 46 | +]); |
| 47 | +export type ImageSource = z.infer<typeof imageSourceSchema>; |
| 48 | + |
| 49 | +export const imageContentSchema = z.object({ |
| 50 | + type: z.literal('image'), |
| 51 | + source: imageSourceSchema, |
| 52 | +}); |
| 53 | +export type ImageContent = z.infer<typeof imageContentSchema>; |
| 54 | + |
| 55 | +// Video uses the same source shape as image (url / base64 / uploaded file id). |
| 56 | +export const videoContentSchema = z.object({ |
| 57 | + type: z.literal('video'), |
| 58 | + source: imageSourceSchema, |
| 59 | +}); |
| 60 | +export type VideoContent = z.infer<typeof videoContentSchema>; |
| 61 | + |
| 62 | +export const fileContentSchema = z.object({ |
| 63 | + type: z.literal('file'), |
| 64 | + file_id: z.string().min(1), |
| 65 | + name: z.string(), |
| 66 | + media_type: z.string().min(1), |
| 67 | + size: z.number().int().nonnegative(), |
| 68 | +}); |
| 69 | +export type FileContent = z.infer<typeof fileContentSchema>; |
| 70 | + |
| 71 | +export const thinkingContentSchema = z.object({ |
| 72 | + type: z.literal('thinking'), |
| 73 | + thinking: z.string(), |
| 74 | + signature: z.string().optional(), |
| 75 | +}); |
| 76 | +export type ThinkingContent = z.infer<typeof thinkingContentSchema>; |
| 77 | + |
| 78 | +export const messageContentSchema = z.discriminatedUnion('type', [ |
| 79 | + textContentSchema, |
| 80 | + toolUseContentSchema, |
| 81 | + toolResultContentSchema, |
| 82 | + imageContentSchema, |
| 83 | + videoContentSchema, |
| 84 | + fileContentSchema, |
| 85 | + thinkingContentSchema, |
| 86 | +]); |
| 87 | +export type MessageContent = z.infer<typeof messageContentSchema>; |
| 88 | + |
| 89 | +export const messageSchema = z.object({ |
| 90 | + id: z.string().min(1), |
| 91 | + session_id: z.string().min(1), |
| 92 | + role: messageRoleSchema, |
| 93 | + content: z.array(messageContentSchema), |
| 94 | + created_at: isoDateTimeSchema, |
| 95 | + prompt_id: z.string().min(1).optional(), |
| 96 | + parent_message_id: z.string().min(1).optional(), |
| 97 | + metadata: z.record(z.string(), z.unknown()).optional(), |
| 98 | +}); |
| 99 | + |
| 100 | +export type Message = z.infer<typeof messageSchema>; |
0 commit comments