-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai-sdk): attachments support (#764)
- Loading branch information
Showing
6 changed files
with
62 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
--- | ||
"@assistant-ui/react": patch | ||
"@assistant-ui/react-playground": patch | ||
"@assistant-ui/react-ai-sdk": patch | ||
--- | ||
|
||
feat: add composer attachments state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@assistant-ui/react-ai-sdk": patch | ||
--- | ||
|
||
feat: allow image content types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@assistant-ui/react-ai-sdk": patch | ||
--- | ||
|
||
feat: AI SDK attachments support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { AppendMessage } from "@assistant-ui/react"; | ||
import { CreateMessage } from "ai"; | ||
|
||
export const toCreateMessage = async ( | ||
message: AppendMessage, | ||
): Promise<CreateMessage> => { | ||
const content = message.content | ||
.filter((part) => part.type === "text") | ||
.map((t) => t.text) | ||
.join("\n\n"); | ||
|
||
const images = message.content | ||
.filter((part) => part.type === "image") | ||
.map((part) => ({ url: part.image })); | ||
|
||
return { | ||
role: message.role, | ||
content, | ||
experimental_attachments: [ | ||
...images, | ||
...(await Promise.all( | ||
message.attachments.map(async (m) => { | ||
if (m.file == null) | ||
throw new Error("Attachment did not contain a file"); | ||
return { | ||
contentType: m.file.type, | ||
name: m.file.name, | ||
url: await getFileDataURL(m.file), | ||
}; | ||
}), | ||
)), | ||
], | ||
}; | ||
}; | ||
|
||
const getFileDataURL = (file: File) => | ||
new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = () => resolve(reader.result as string); | ||
reader.onerror = (error) => reject(error); | ||
|
||
reader.readAsDataURL(file); | ||
}); |