MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / validateImagesForAPI

Function validateImagesForAPI

source code/utils/imageValidation.ts:67–106  ·  view source on GitHub ↗
(messages: unknown[])

Source from the content-addressed store, hash-verified

65 * @throws ImageSizeError if any image exceeds the API limit
66 */
67export function validateImagesForAPI(messages: unknown[]): void {
68 const oversizedImages: OversizedImage[] = []
69 let imageIndex = 0
70
71 for (const msg of messages) {
72 if (typeof msg !== 'object' || msg === null) continue
73
74 const m = msg as Record<string, unknown>
75
76 // Handle wrapped message format { type: 'user', message: { role, content } }
77 // Only check user messages
78 if (m.type !== 'user') continue
79
80 const innerMessage = m.message as Record<string, unknown> | undefined
81 if (!innerMessage) continue
82
83 const content = innerMessage.content
84 if (typeof content === 'string' || !Array.isArray(content)) continue
85
86 for (const block of content) {
87 if (isBase64ImageBlock(block)) {
88 imageIndex++
89 // Check the base64-encoded string length directly (not decoded bytes)
90 // The API limit applies to the base64 payload size
91 const base64Size = block.source.data.length
92 if (base64Size > API_IMAGE_MAX_BASE64_SIZE) {
93 logEvent('tengu_image_api_validation_failed', {
94 base64_size_bytes: base64Size,
95 max_bytes: API_IMAGE_MAX_BASE64_SIZE,
96 })
97 oversizedImages.push({ index: imageIndex, size: base64Size })
98 }
99 }
100 }
101 }
102
103 if (oversizedImages.length > 0) {
104 throw new ImageSizeError(oversizedImages, API_IMAGE_MAX_BASE64_SIZE)
105 }
106}

Callers 1

normalizeMessagesForAPIFunction · 0.85

Calls 2

isBase64ImageBlockFunction · 0.85
logEventFunction · 0.85

Tested by

no test coverage detected