Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

frontend: no Retry on an upload the server refused (400/413/415)

Lin (kwant bot)08931790d9f7
diff --git a/frontend/src/attachments.ts b/frontend/src/attachments.ts
index 06bdc86e..936d7d90 100644
--- a/frontend/src/attachments.ts
+++ b/frontend/src/attachments.ts
@@ -91,6 +91,16 @@ export function uploadErrorText(status: number, serverMessage?: string): string
}
}
+/** Whether retrying the same file can help: not when the server refused the file itself. */
+export const uploadRetryable = (status: number) => status !== 400 && status !== 413 && status !== 415;
+
+/** An upload the server refused, or that never reached it (`status` 0). */
+export class UploadError extends Error {
+ constructor(readonly status: number, serverMessage?: string) {
+ super(uploadErrorText(status, serverMessage));
+ }
+}
+
/** Why `/chat/send` refused a message's files, or null when the status isn't about them. */
export function sendAttachmentErrorText(status: number): string | null {
if (status === 409) return 'A file here was already sent. Attach it again to resend.';
@@ -111,6 +121,8 @@ export interface PendingUpload {
progress: number;
state: UploadState;
error?: string;
+ /** False when Retry can't help, e.g. an unsupported file type. */
+ retryable?: boolean;
attachment?: Attachment;
}
@@ -149,10 +161,10 @@ export function uploadAttachment(file: Blob & { name?: string }, onProgress: (fr
resolve(body as Attachment);
} else {
const msg = body && typeof (body as { error?: unknown }).error === 'string' ? (body as { error: string }).error : undefined;
- reject(new Error(uploadErrorText(xhr.status, msg)));
+ reject(new UploadError(xhr.status, msg));
}
};
- xhr.onerror = () => reject(new Error(uploadErrorText(0)));
+ xhr.onerror = () => reject(new UploadError(0));
xhr.onabort = () => reject(new DOMException('Upload cancelled', 'AbortError'));
signal?.addEventListener('abort', () => xhr.abort(), { once: true });
xhr.send(file);
diff --git a/frontend/src/components/Attachments.tsx b/frontend/src/components/Attachments.tsx
index 89586da4..f9b94f92 100644
--- a/frontend/src/components/Attachments.tsx
+++ b/frontend/src/components/Attachments.tsx
@@ -97,7 +97,7 @@ export const UploadChip: React.FC<{ item: PendingUpload; onRemove: () => void; o
{failed ? item.error : item.state === 'uploading' ? `Uploading… ${pct}%` : formatBytes(item.size)}
</span>
</span>
- {failed && onRetry && (
+ {failed && onRetry && item.retryable !== false && (
<button
type="button"
onClick={onRetry}
diff --git a/frontend/src/components/ChatView.tsx b/frontend/src/components/ChatView.tsx
index 13b4eb97..72c65ab5 100644
--- a/frontend/src/components/ChatView.tsx
+++ b/frontend/src/components/ChatView.tsx
@@ -15,6 +15,7 @@ import {
AttachmentRef,
MAX_ATTACHMENTS,
PendingUpload,
+ UploadError,
composerReady,
filesFrom,
isSharedFileMessage,
@@ -22,6 +23,7 @@ import {
preflightFile,
readyAttachments,
uploadAttachment,
+ uploadRetryable,
uploadSlotsLeft,
} from '../attachments';
import { FileTiles, UploadChip } from './Attachments';
@@ -468,7 +470,9 @@ export const ChatView: React.FC<ChatViewProps> = ({
uploadAttachment(file, (progress) => patchUpload(key, { progress }), abort.signal).then(
(attachment) => patchUpload(key, { state: 'done', progress: 1, attachment, error: undefined }),
(err: Error) => {
- if (err.name !== 'AbortError') patchUpload(key, { state: 'error', error: err.message });
+ if (err.name === 'AbortError') return;
+ const retryable = !(err instanceof UploadError) || uploadRetryable(err.status);
+ patchUpload(key, { state: 'error', error: err.message, retryable });
},
);
};
diff --git a/frontend/tests/attachments.test.mjs b/frontend/tests/attachments.test.mjs
index 1194a146..abc2676b 100644
--- a/frontend/tests/attachments.test.mjs
+++ b/frontend/tests/attachments.test.mjs
@@ -27,6 +27,12 @@ test('server refusals read as plain language', () => {
assert.equal(a.uploadErrorText(400, 'bad name'), 'bad name');
assert.match(a.uploadErrorText(0), /interrupted/);
assert.match(a.uploadErrorText(500), /HTTP 500/);
+ // Retry can't help when the server refused the file itself
+ for (const st of [400, 413, 415]) assert.equal(a.uploadRetryable(st), false, `HTTP ${st}`);
+ for (const st of [0, 401, 429, 500, 503]) assert.equal(a.uploadRetryable(st), true, `HTTP ${st}`);
+ const e = new a.UploadError(415);
+ assert.equal(e.status, 415);
+ assert.match(e.message, /isn't supported/);
assert.match(a.sendAttachmentErrorText(409), /already sent/);
assert.equal(a.sendAttachmentErrorText(500), null);
});
@@ -94,4 +100,7 @@ test('upload chips show progress, and failures with the reason', () => {
const failed = ui.chip({ key: 'k', name: 'c.exe', size: 10, progress: 0, state: 'error', error: 'Not supported' });
assert.match(failed, /role="alert">Not supported/);
assert.match(failed, /Retry c.exe/);
+ const refused = ui.chip({ key: 'k', name: 'c.exe', size: 10, progress: 0, state: 'error', error: 'Not supported', retryable: false });
+ assert.match(refused, /role="alert">Not supported/);
+ assert.doesNotMatch(refused, /Retry/);
});