Add user warnings for file truncation and size limits
- Alert user when file exceeds 1MB (rejected) - Warn in filename when file is truncated (>50KB) - Show clear '[TRUNCATED]' marker in content - Better error handling with user-facing messages
This commit is contained in:
@@ -547,21 +547,38 @@ function autoResizeTextarea(e) {
|
|||||||
|
|
||||||
async function handleFileSelect(e) {
|
async function handleFileSelect(e) {
|
||||||
const files = Array.from(e.target.files);
|
const files = Array.from(e.target.files);
|
||||||
|
const MAX_FILE_SIZE = 50000; // 50KB text limit
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
try {
|
try {
|
||||||
|
// Check file size first
|
||||||
|
if (file.size > 1024 * 1024) { // 1MB
|
||||||
|
alert(`File "${file.name}" is too large (${(file.size / 1024 / 1024).toFixed(1)}MB). Maximum size is 1MB.`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Read file content
|
// Read file content
|
||||||
const content = await readFileContent(file);
|
const content = await readFileContent(file);
|
||||||
|
|
||||||
|
// Warn if content will be truncated
|
||||||
|
let finalContent = content;
|
||||||
|
let warning = '';
|
||||||
|
if (content.length > MAX_FILE_SIZE) {
|
||||||
|
warning = ` (truncated - file is ${Math.round(content.length / 1000)}KB, max ${MAX_FILE_SIZE / 1000}KB per file)`;
|
||||||
|
finalContent = content.substring(0, MAX_FILE_SIZE) + '\n\n... [TRUNCATED - file too large for context window]';
|
||||||
|
}
|
||||||
|
|
||||||
state.files.push({
|
state.files.push({
|
||||||
id: file.name + '-' + Date.now(),
|
id: file.name + '-' + Date.now(),
|
||||||
name: file.name,
|
name: file.name + warning,
|
||||||
type: file.type,
|
type: file.type,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
content: content
|
content: finalContent,
|
||||||
|
truncated: content.length > MAX_FILE_SIZE
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to read file:', file.name, err);
|
console.error('Failed to read file:', file.name, err);
|
||||||
|
alert(`Failed to read file "${file.name}": ${err.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,7 +590,7 @@ function readFileContent(file) {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = () => resolve(reader.result);
|
reader.onload = () => resolve(reader.result);
|
||||||
reader.onerror = reject;
|
reader.onerror = () => reject(new Error('Failed to read file'));
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -676,15 +693,10 @@ function buildMessageContent(text, files) {
|
|||||||
if (!files.length) return text;
|
if (!files.length) return text;
|
||||||
|
|
||||||
const fileContents = files.map(f => {
|
const fileContents = files.map(f => {
|
||||||
const maxLen = 50000; // Limit file content length
|
return `--- ${f.name.replace(/ \(truncated.*\)$/, '')} ---\n${f.content}\n--- end of file ---`;
|
||||||
const content = f.content || '[File content not available]';
|
|
||||||
const truncated = content.length > maxLen
|
|
||||||
? content.substring(0, maxLen) + '\n... [truncated]'
|
|
||||||
: content;
|
|
||||||
return `--- ${f.name} ---\n${truncated}\n--- end of ${f.name} ---`;
|
|
||||||
}).join('\n\n');
|
}).join('\n\n');
|
||||||
|
|
||||||
return `${text}\n\n${fileContents}`;
|
return `${text}\n\nAttached files:\n\n${fileContents}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateLastMessage(content) {
|
function updateLastMessage(content) {
|
||||||
|
|||||||
Reference in New Issue
Block a user