You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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