|
| 1 | +import z from 'zod/v4' |
| 2 | + |
| 3 | +import { dataContentSchema } from './data-content' |
| 4 | +import { jsonValueSchema } from './json' |
| 5 | +import { providerMetadataSchema } from './provider-metadata' |
| 6 | + |
| 7 | +export const textPartSchema = z.object({ |
| 8 | + type: z.literal('text'), |
| 9 | + text: z.string(), |
| 10 | + providerOptions: providerMetadataSchema.optional(), |
| 11 | +}) |
| 12 | +export type TextPart = z.infer<typeof textPartSchema> |
| 13 | + |
| 14 | +export const imagePartSchema = z.object({ |
| 15 | + type: z.literal('image'), |
| 16 | + image: z.union([dataContentSchema, z.instanceof(URL)]), |
| 17 | + mediaType: z.string().optional(), |
| 18 | + providerOptions: providerMetadataSchema.optional(), |
| 19 | +}) |
| 20 | +export type ImagePart = z.infer<typeof imagePartSchema> |
| 21 | + |
| 22 | +export const filePartSchema = z.object({ |
| 23 | + type: z.literal('file'), |
| 24 | + data: z.union([dataContentSchema, z.instanceof(URL)]), |
| 25 | + filename: z.string().optional(), |
| 26 | + mediaType: z.string(), |
| 27 | + providerOptions: providerMetadataSchema.optional(), |
| 28 | +}) |
| 29 | +export type FilePart = z.infer<typeof filePartSchema> |
| 30 | + |
| 31 | +export const reasoningPartSchema = z.object({ |
| 32 | + type: z.literal('reasoning'), |
| 33 | + text: z.string(), |
| 34 | + providerOptions: providerMetadataSchema.optional(), |
| 35 | +}) |
| 36 | +export type ReasoningPart = z.infer<typeof reasoningPartSchema> |
| 37 | + |
| 38 | +export const toolCallPartSchema = z.object({ |
| 39 | + type: z.literal('tool-call'), |
| 40 | + toolCallId: z.string(), |
| 41 | + toolName: z.string(), |
| 42 | + input: z.record(z.string(), z.unknown()), |
| 43 | + providerOptions: providerMetadataSchema.optional(), |
| 44 | + providerExecuted: z.boolean().optional(), |
| 45 | +}) |
| 46 | +export type ToolCallPart = z.infer<typeof toolCallPartSchema> |
| 47 | + |
| 48 | +export const toolResultOutputSchema = z.discriminatedUnion('type', [ |
| 49 | + z.object({ |
| 50 | + type: z.literal('json'), |
| 51 | + value: jsonValueSchema, |
| 52 | + }), |
| 53 | + z.object({ |
| 54 | + type: z.literal('media'), |
| 55 | + data: z.string(), |
| 56 | + mediaType: z.string(), |
| 57 | + }), |
| 58 | +]) |
| 59 | +export type ToolResultOutput = z.infer<typeof toolResultOutputSchema> |
| 60 | + |
| 61 | +export const toolResultPartSchema = z.object({ |
| 62 | + type: z.literal('tool-result'), |
| 63 | + toolCallId: z.string(), |
| 64 | + toolName: z.string(), |
| 65 | + output: toolResultOutputSchema.array(), |
| 66 | + providerOptions: providerMetadataSchema.optional(), |
| 67 | +}) |
| 68 | +export type ToolResultPart = z.infer<typeof toolResultPartSchema> |
0 commit comments