Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

fix: enhance message sending and attachment management in chat context

Salman-Ahamed58ab3c9efe9a
diff --git a/packages/app/src/react/features/ai-chat/components/footer/input.tsx b/packages/app/src/react/features/ai-chat/components/footer/input.tsx
index ea32e6bd..0fbea76d 100644
--- a/packages/app/src/react/features/ai-chat/components/footer/input.tsx
+++ b/packages/app/src/react/features/ai-chat/components/footer/input.tsx
@@ -52,7 +52,7 @@ export const Input = () => {
);
const handleSubmit = useCallback((): void => {
- if (isChatbotDisabled) return;
+ if (isChatbotDisabled || uploading) return;
if (isStreaming) return stopStreaming();
const trimmedMessage = message.trim();
diff --git a/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx b/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx
index 0c34b519..0e94eca1 100644
--- a/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx
+++ b/packages/app/src/react/features/ai-chat/contexts/chat.provider.tsx
@@ -82,22 +82,26 @@ export const ChatContextProvider: FC<TChildren> = ({ children }) => {
const sendMessage = useCallback(
async (message: string) => {
if (!message.trim() && fileUpload.attachments.length === 0) return;
+ if (fileUpload.uploading) return;
- try {
- const attachments = fileUpload.attachments.map((f) => ({
- id: f.id,
- name: f.name,
- type: f.type,
- size: f.size,
- url: f.url,
- blobUrl: f.blobUrl,
- file: f.file,
- }));
+ // Capture the IDs of attachments being sent
+ const sentIds = new Set(fileUpload.attachments.map((f) => f.id));
+
+ const attachments = fileUpload.attachments.map((f) => ({
+ id: f.id,
+ name: f.name,
+ type: f.type,
+ size: f.size,
+ url: f.url,
+ blobUrl: f.blobUrl,
+ file: f.file,
+ }));
+ try {
await addMessage(message, attachments);
- fileUpload.clear();
- } catch (error) {
- console.error('Failed to send message:', error); // eslint-disable-line no-console
+ } finally {
+ // Only clear attachments that were actually sent, keep any new ones added during streaming
+ fileUpload.removeByIds(sentIds);
}
},
[fileUpload, addMessage],
diff --git a/packages/app/src/react/features/ai-chat/hooks/use-file-upload.ts b/packages/app/src/react/features/ai-chat/hooks/use-file-upload.ts
index 82f44a9c..d96b2f33 100644
--- a/packages/app/src/react/features/ai-chat/hooks/use-file-upload.ts
+++ b/packages/app/src/react/features/ai-chat/hooks/use-file-upload.ts
@@ -155,6 +155,39 @@ export const useFileUpload = (options: TFileUploadConfig): IFileUpload => {
[attachments],
);
+ /**
+ * Removes attachments by their IDs.
+ * Only removes attachments that match the provided IDs, keeping any new ones.
+ */
+ const removeByIds = useCallback((ids: Set<string>) => {
+ setAttachments((prev) => {
+ const toRemove = prev.filter((a) => ids.has(a.id));
+ const toKeep = prev.filter((a) => !ids.has(a.id));
+
+ // Revoke blob URLs for removed attachments
+ toRemove.forEach((a) => {
+ if (a.blobUrl) {
+ URL.revokeObjectURL(a.blobUrl);
+ blobsRef.current.delete(a.blobUrl);
+ }
+ });
+
+ return toKeep;
+ });
+
+ setStatus((prev) => {
+ const next = { ...prev };
+ ids.forEach((id) => {
+ // Find attachment name by id to clean up status
+ const attachment = attachments.find((a) => a.id === id);
+ if (attachment) {
+ delete next[attachment.name];
+ }
+ });
+ return next;
+ });
+ }, [attachments]);
+
const clear = useCallback(
(preserveBlobUrls = false) => {
if (preserveBlobUrls) {
@@ -187,6 +220,7 @@ export const useFileUpload = (options: TFileUploadConfig): IFileUpload => {
errorMessage,
addFiles,
remove,
+ removeByIds,
clear,
clearError,
};
diff --git a/packages/app/src/react/features/ai-chat/types/hooks.ts b/packages/app/src/react/features/ai-chat/types/hooks.ts
index 32d59e62..fe6b6354 100644
--- a/packages/app/src/react/features/ai-chat/types/hooks.ts
+++ b/packages/app/src/react/features/ai-chat/types/hooks.ts
@@ -32,6 +32,7 @@ export interface IFileUpload {
errorMessage: string;
addFiles: (files: File[]) => Promise<void>;
remove: (index: number) => void;
+ removeByIds: (ids: Set<string>) => void;
clear: (preserveBlobUrls?: boolean) => void;
clearError: () => void;
}