Skip to content

Commit

Permalink
Flav/input bar uses file api (#5997)
Browse files Browse the repository at this point in the history
* Use file upload API in the input bar

* Stop uploading raw content fragment

* ✨

* 👕

* 📝

* ✨

* ✂️

* ✂️

* ✂️

* ✂️

* :sparkles

* ✨
  • Loading branch information
flvndvd authored Jul 4, 2024
1 parent 3a241be commit 618f150
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 242 deletions.
9 changes: 5 additions & 4 deletions front/components/assistant/conversation/ContentFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { CitationType } from "@dust-tt/sparkle/dist/cjs/components/Citation
import type { ContentFragmentType } from "@dust-tt/types";
import {
isSupportedImageContentFragmentType,
isSupportedTextContentFragmentType,
isSupportedImageContentType,
isSupportedPlainTextContentType,
} from "@dust-tt/types";

export function ContentFragment({ message }: { message: ContentFragmentType }) {
Expand All @@ -14,9 +15,9 @@ export function ContentFragment({ message }: { message: ContentFragmentType }) {
message.contentType === "dust-application/slack"
) {
citationType = "slack";
} else if (isSupportedTextContentFragmentType(message.contentType)) {
} else if (isSupportedPlainTextContentType(message.contentType)) {
citationType = "document";
} else if (isSupportedImageContentFragmentType(message.contentType)) {
} else if (isSupportedImageContentType(message.contentType)) {
citationType = "image";
}

Expand All @@ -38,7 +39,7 @@ function getViewUrlForContentFragment(message: ContentFragmentType) {
}

if (isSupportedImageContentFragmentType(message.contentType)) {
return `${message.sourceUrl}&action=view`;
return `${message.sourceUrl}?action=view`;
}

return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import ConversationViewer from "@app/components/assistant/conversation/Conversat
import { HelpAndQuickGuideWrapper } from "@app/components/assistant/conversation/HelpAndQuickGuideWrapper";
import { FixedAssistantInputBar } from "@app/components/assistant/conversation/input_bar/InputBar";
import { InputBarContext } from "@app/components/assistant/conversation/input_bar/InputBarContext";
import type { ContentFragmentInput } from "@app/components/assistant/conversation/lib";
import {
createConversationWithMessage,
createPlaceholderUserMessage,
Expand Down Expand Up @@ -98,7 +97,7 @@ export function ConversationContainer({
const handleSubmit = async (
input: string,
mentions: MentionType[],
contentFragments: ContentFragmentInput[]
contentFragments: UploadedContentFragment[]
) => {
if (!activeConversationId) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,18 @@ export function AssistantInputBar({
onSubmit(
text,
mentions,
fileUploaderService.fileBlobs.map((cf) => {
fileUploaderService.getFileBlobs().map((cf) => {
return {
title: cf.filename,
content: cf.content,
file: cf.file,
contentType: cf.contentType,
fileId: cf.fileId,
};
})
);
resetEditorText();
fileUploaderService.resetUpload();
};

const fileUploaderService = useFileUploaderService();
const fileUploaderService = useFileUploaderService({ owner });

const [isProcessing, setIsProcessing] = useState<boolean>(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export function InputBarCitations({
size="xs"
type={isImage ? "image" : "document"}
imgSrc={blob.preview}
description={isImage ? undefined : blob.content}
onClose={() => {
fileUploaderService.removeFile(blob.id);
}}
isLoading={blob.isUploading}
/>
);
}
Expand Down
61 changes: 4 additions & 57 deletions front/components/assistant/conversation/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {
ContentFragmentType,
ConversationType,
ConversationVisibility,
InternalPostConversationsRequestBodySchema,
Expand All @@ -14,7 +13,6 @@ import { Err, Ok } from "@dust-tt/types";
import type * as t from "io-ts";

import type { NotificationType } from "@app/components/sparkle/Notification";
import { getMimeTypeFromFile } from "@app/lib/file";
import type { PostConversationsResponseBody } from "@app/pages/api/w/[wId]/assistant/conversations";

/**
Expand Down Expand Up @@ -90,7 +88,7 @@ export async function submitMessage({
messageData: {
input: string;
mentions: MentionType[];
contentFragments: ContentFragmentInput[];
contentFragments: UploadedContentFragment[];
};
}): Promise<
Result<{ message: UserMessageWithRankType }, ConversationErrorType>
Expand All @@ -109,9 +107,7 @@ export async function submitMessage({
},
body: JSON.stringify({
title: contentFragment.title,
content: contentFragment.content,
url: null,
contentType: getMimeTypeFromFile(contentFragment.file),
fileId: contentFragment.fileId,
context: {
timezone:
Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC",
Expand All @@ -123,7 +119,7 @@ export async function submitMessage({
})
);

for (const [i, mcfRes] of contentFragmentsRes.entries()) {
for (const mcfRes of contentFragmentsRes) {
if (!mcfRes.ok) {
const data = await mcfRes.json();
console.error("Error creating content fragment", data);
Expand All @@ -133,14 +129,6 @@ export async function submitMessage({
message: data.error.message || "Please try again or contact us.",
});
}
const cfData = (await mcfRes.json())
.contentFragment as ContentFragmentType;
uploadRawContentFragment({
workspaceId: owner.sId,
conversationId,
contentFragmentId: cfData.sId,
file: contentFragments[i].file,
});
}
}

Expand Down Expand Up @@ -243,13 +231,11 @@ export async function createConversationWithMessage({
mentions,
},
contentFragments: contentFragments.map((cf) => ({
content: cf.content,
title: cf.title,
url: null, // sourceUrl will be set on raw content upload success
contentType: cf.contentType,
context: {
profilePictureUrl: user.image,
},
fileId: cf.fileId,
})),
};

Expand All @@ -276,44 +262,5 @@ export async function createConversationWithMessage({

const conversationData = (await cRes.json()) as PostConversationsResponseBody;

if (conversationData.contentFragments.length > 0) {
for (const [i, cf] of conversationData.contentFragments.entries()) {
uploadRawContentFragment({
workspaceId: owner.sId,
conversationId: conversationData.conversation.sId,
contentFragmentId: cf.sId,
file: contentFragments[i].file,
});
}
}

return new Ok(conversationData.conversation);
}

function uploadRawContentFragment({
workspaceId,
conversationId,
contentFragmentId,
file,
}: {
workspaceId: string;
conversationId: string;
contentFragmentId: string;
file: File;
}) {
const formData = new FormData();
formData.append("file", file);

// do not await, to avoid slowing the UX
// an error from this function does not prevent the conversation from continuing
// API errors are handled server side
fetch(
`/api/w/${workspaceId}/assistant/conversations/${conversationId}/messages/${contentFragmentId}/raw_content_fragment`,
{
method: "POST",
body: formData,
}
).catch((e) => {
console.error(`Error uploading raw content for file`, e);
});
}
Loading

0 comments on commit 618f150

Please sign in to comment.