-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathimage-attachment.ts
More file actions
75 lines (68 loc) · 1.86 KB
/
Copy pathimage-attachment.ts
File metadata and controls
75 lines (68 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Supported image MIME types for upload
* Limited to most common web formats for reliability
*/
export const SUPPORTED_IMAGE_MIME_TYPES = [
'image/png',
'image/jpeg',
'image/webp',
] as const;
export type SupportedImageMimeType = typeof SUPPORTED_IMAGE_MIME_TYPES[number];
/**
* Image attachment for user messages
* Represents an image that can be sent with text prompts
*/
export interface ImageAttachment {
/** Unique identifier for this attachment */
id: string;
/** Original filename */
filename: string;
/** MIME type of the image */
mimeType: SupportedImageMimeType;
/** Base64-encoded image data (without data URL prefix) */
base64Data: string;
/** Size of the original file in bytes */
size?: number;
/** Optional dimensions if available */
dimensions?: {
width: number;
height: number;
};
}
export interface ProcessedImageAttachment {
/** MIME type of the image */
mimeType: SupportedImageMimeType;
/** Base64-encoded image data (without data URL prefix) */
base64Data?: string;
/** R2 key of the image */
r2Key: string;
/** URL of the image */
publicUrl: string;
/** image data hash */
hash: string;
}
/**
* Utility to check if a MIME type is supported
*/
export function isSupportedImageType(mimeType: string): mimeType is SupportedImageMimeType {
return SUPPORTED_IMAGE_MIME_TYPES.includes(mimeType as SupportedImageMimeType);
}
/**
* Utility to get file extension from MIME type
*/
export function getFileExtensionFromMimeType(mimeType: SupportedImageMimeType): string {
const map: Record<SupportedImageMimeType, string> = {
'image/png': 'png',
'image/jpeg': 'jpg',
'image/webp': 'webp',
};
return map[mimeType] || 'jpg';
}
/**
* Maximum file size for images (10MB)
*/
export const MAX_IMAGE_SIZE_BYTES = 10 * 1024 * 1024;
/**
* Maximum number of images per message
*/
export const MAX_IMAGES_PER_MESSAGE = 2;