Replies: 1 comment
-
|
This discussion was automatically locked because it has not been updated in over 30 days. If you still have questions about this topic, please ask us at community.vercel.com/ai-sdk |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello all,
We have files that are stored in private buckets and want to send them as attachments using the useChat hook, and render them as preview attachments (for images) in the front end.
We were wondering what patterns people are using?
Currently, we are doing the following:
FRONT END
(1) User selects a file from a private bucket, or uploads a file from local device to the private bucket
(2) Upon selection or upload, we invoke a server action to generate a pre-signed URL for the file with an expiry of say 4 hrs (see below why).
(3) We append the local state attachments array with the new file information { name, url, contentType }[]
(4) We also append a separate local state attachments_metadata array { file_id: string|undefined, private: boolean }[].
(5) Repeat 1-4 for any additional files
(6) User clicks submit to invoke handleSubmit to send the message as handleSubmit({ experimental_attachments: attachments, data: { attachments_metadata } })
(7) For any private files that are images, we can immediately render the them in our front-end as a preview attachment because we have a pre-signed URL available
BACKEND
(8) Receive the messages object
(9) For the new user message (the last message in the array ), we save it to our database messages table. We parse the 'data' attribute to also extract the attachments_metadata... this allows us to verify authorization for any attached private files. Importantly, we do not save the url's for private files... as it is pre-signed and does permanently identify the file...
(10) We can invoke the model (streamText) and stream the result back to the client.
(11) We invoke onFinish to save the model result to the database messages table.
FRONT END
(12) Our model can display the streamed message.
Within 4 hours of the chat session commencing / last updated, or if the user were to access the chat session at a later time...
(13) We reload / load the initialMessages from our database... in doing so, since we have saved the attachments_metadata, we will see that some attachments are private, have a file_id, and do not have a URL... for these, we can generate new pre-signed URLs and add them to each file's missing url property.
Much appreciated,
A.
Beta Was this translation helpful? Give feedback.
All reactions