-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Simple Image / Text AttachmentAdapters (#782)
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: SimpleImageAttachmentAdapter |
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": patch | ||
--- | ||
|
||
feat: SimpleTextAttachmentAdapter |
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": patch | ||
--- | ||
|
||
feat: ComposedAttachmentAdapter |
46 changes: 46 additions & 0 deletions
46
packages/react/src/runtimes/attachment/SimpleImageAttachmentAdapter.ts
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,46 @@ | ||
import { | ||
ComposerAttachment, | ||
MessageAttachment, | ||
} from "../../context/stores/Attachment"; | ||
import { AttachmentAdapter } from "./AttachmentAdapter"; | ||
|
||
export class SimpleImageAttachmentAdapter implements AttachmentAdapter { | ||
public accept = "image/*"; | ||
|
||
public async add(state: { file: File }): Promise<ComposerAttachment> { | ||
return { | ||
id: state.file.name, | ||
type: "image", | ||
name: state.file.name, | ||
file: state.file, | ||
}; | ||
} | ||
|
||
public async send( | ||
attachment: ComposerAttachment, | ||
): Promise<MessageAttachment> { | ||
return { | ||
...attachment, | ||
content: [ | ||
{ | ||
type: "image", | ||
image: await getFileDataURL(attachment.file), | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
public async remove() { | ||
// noop | ||
} | ||
} | ||
|
||
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); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/react/src/runtimes/attachment/SimpleTextAttachmentAdapter.ts
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,47 @@ | ||
import { | ||
ComposerAttachment, | ||
MessageAttachment, | ||
} from "../../context/stores/Attachment"; | ||
import { AttachmentAdapter } from "./AttachmentAdapter"; | ||
|
||
export class SimpleTextAttachmentAdapter implements AttachmentAdapter { | ||
public accept = | ||
"text/plain,text/html,text/markdown,text/csv,text/xml,text/json,text/css"; | ||
|
||
public async add(state: { file: File }): Promise<ComposerAttachment> { | ||
return { | ||
id: state.file.name, | ||
type: "document", | ||
name: state.file.name, | ||
file: state.file, | ||
}; | ||
} | ||
|
||
public async send( | ||
attachment: ComposerAttachment, | ||
): Promise<MessageAttachment> { | ||
return { | ||
...attachment, | ||
content: [ | ||
{ | ||
type: "text", | ||
text: `<attachment name=${attachment.name}>\n${await getFileText(attachment.file)}\n</attachment>`, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
public async remove() { | ||
// noop | ||
} | ||
} | ||
|
||
const getFileText = (file: File) => | ||
new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = () => resolve(reader.result as string); | ||
reader.onerror = (error) => reject(error); | ||
|
||
reader.readAsText(file); | ||
}); |
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 +1,4 @@ | ||
export type { AttachmentAdapter } from "./AttachmentAdapter"; | ||
export { SimpleImageAttachmentAdapter } from "./SimpleImageAttachmentAdapter"; | ||
export { SimpleTextAttachmentAdapter } from "./SimpleTextAttachmentAdapter"; | ||
export { ComposedAttachmentAdapter } from "./ComposedAttachmentAdapter"; |