Repository navigation
Expand file tree
/
Copy pathagentFileAttach.js
More file actions
102 lines (91 loc) · 2.86 KB
/
Copy pathagentFileAttach.js
File metadata and controls
102 lines (91 loc) · 2.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs')
const path = require('path')
const { nativeImage } = require('electron')
const {
DOCUMENT_EXTENSIONS,
extractDocumentFromPath,
MAX_FILE_BYTES
} = require('./agentDocumentExtract')
const MAX_CHAT_ATTACHMENTS = 8
const MAX_IMAGE_BYTES = 5 * 1024 * 1024
const IMAGE_EXTENSIONS = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'heic', 'heif'])
const ALL_EXTENSIONS = [...new Set([...IMAGE_EXTENSIONS, ...DOCUMENT_EXTENSIONS])]
function isImageExtension(ext) {
return IMAGE_EXTENSIONS.has(String(ext || '').toLowerCase())
}
function readImageAttachmentFromPath(filePath) {
const stat = fs.statSync(filePath)
if (stat.size > MAX_IMAGE_BYTES) {
throw new Error(`Image too large (max ${Math.round(MAX_IMAGE_BYTES / (1024 * 1024))}MB): ${path.basename(filePath)}`)
}
const image = nativeImage.createFromPath(filePath)
if (!image || image.isEmpty()) {
throw new Error(`Could not read image: ${path.basename(filePath)}`)
}
const png = image.toPNG()
const base64 = png.toString('base64')
return {
kind: 'image',
name: path.basename(filePath),
mediaType: 'image/png',
dataUrl: `data:image/png;base64,${base64}`
}
}
async function readAttachmentFromPath(filePath) {
const ext = path.extname(filePath).slice(1).toLowerCase()
if (!ALL_EXTENSIONS.has(ext)) {
throw new Error(`Unsupported file type: ${path.basename(filePath)}`)
}
if (isImageExtension(ext)) {
return readImageAttachmentFromPath(filePath)
}
return extractDocumentFromPath(filePath)
}
async function readAttachmentsFromPaths(filePaths, existingCount = 0) {
const remaining = Math.max(0, MAX_CHAT_ATTACHMENTS - existingCount)
if (!remaining) {
throw new Error(`Maximum ${MAX_CHAT_ATTACHMENTS} attachments per message`)
}
const attachments = []
for (const filePath of filePaths.slice(0, remaining)) {
attachments.push(await readAttachmentFromPath(filePath))
}
return attachments
}
function splitAttachments(attachments) {
const images = []
const files = []
for (const item of attachments || []) {
if (!item) continue
if (item.kind === 'image' || item.dataUrl) {
images.push({
name: item.name,
mediaType: item.mediaType || 'image/png',
dataUrl: item.dataUrl
})
} else if (item.kind === 'document' || item.extractedText) {
files.push({
kind: 'document',
name: item.name,
mediaType: item.mediaType || 'text/plain',
extractedText: item.extractedText,
size: item.size
})
}
}
return { images, files }
}
module.exports = {
MAX_CHAT_ATTACHMENTS,
MAX_CHAT_IMAGES: MAX_CHAT_ATTACHMENTS,
MAX_IMAGE_BYTES,
MAX_FILE_BYTES,
IMAGE_EXTENSIONS,
ALL_EXTENSIONS,
readImageAttachmentFromPath,
readImageAttachmentsFromPaths: readAttachmentsFromPaths,
readAttachmentsFromPaths,
readAttachmentFromPath,
splitAttachments,
isImageExtension
}