-
Notifications
You must be signed in to change notification settings - Fork 35
feat(bot): add image attachment support for Slack bot #2749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kilo-code-bot
wants to merge
2
commits into
main
Choose a base branch
from
feat/slack-bot-image-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,101 @@ | ||
| import { PutObjectCommand } from '@aws-sdk/client-s3'; | ||
| import { | ||
| CLOUD_AGENT_IMAGE_ALLOWED_TYPES, | ||
| CLOUD_AGENT_IMAGE_MAX_COUNT, | ||
| CLOUD_AGENT_IMAGE_MAX_SIZE_BYTES, | ||
| CLOUD_AGENT_IMAGE_MIME_TO_EXTENSION, | ||
| type CloudAgentImageAllowedType, | ||
| } from '@/lib/cloud-agent/constants'; | ||
| import type { Images } from '@/lib/images-schema'; | ||
| import { r2Client, r2CloudAgentAttachmentsBucketName } from '@/lib/r2/client'; | ||
| import { captureException } from '@sentry/nextjs'; | ||
| import type { Attachment, Message } from 'chat'; | ||
| import { randomUUID } from 'crypto'; | ||
|
|
||
| const ALLOWED_TYPES_SET = new Set<string>(CLOUD_AGENT_IMAGE_ALLOWED_TYPES); | ||
|
|
||
| function isAllowedImageType(mimeType: string): mimeType is CloudAgentImageAllowedType { | ||
| return ALLOWED_TYPES_SET.has(mimeType); | ||
| } | ||
|
|
||
| /** | ||
| * Extract image attachments from a chat Message, download them via the | ||
| * adapter's authenticated `fetchData()`, upload to R2, and return an | ||
| * `Images` reference that can be passed to the Cloud Agent API. | ||
| * | ||
| * Returns `undefined` when the message has no usable image attachments. | ||
| */ | ||
| export async function extractAndUploadImages( | ||
| message: Message, | ||
| userId: string | ||
| ): Promise<Images | undefined> { | ||
| const imageAttachments = message.attachments.filter( | ||
| (a): a is Attachment & { mimeType: string; fetchData: () => Promise<Buffer> } => | ||
| a.type === 'image' && | ||
| typeof a.mimeType === 'string' && | ||
| isAllowedImageType(a.mimeType) && | ||
| typeof a.fetchData === 'function' | ||
| ); | ||
|
|
||
| if (imageAttachments.length === 0) return undefined; | ||
|
|
||
| // Respect the Cloud Agent's per-message image limit | ||
| const toProcess = imageAttachments.slice(0, CLOUD_AGENT_IMAGE_MAX_COUNT); | ||
|
|
||
| const messageUuid = randomUUID(); | ||
| const uploadResults = await Promise.allSettled( | ||
| toProcess.map(async attachment => { | ||
| const imageId = randomUUID(); | ||
| const ext = | ||
| CLOUD_AGENT_IMAGE_MIME_TO_EXTENSION[attachment.mimeType as CloudAgentImageAllowedType]; | ||
| const filename = `${imageId}.${ext}`; | ||
| const r2Key = `${userId}/cloud-agent/${messageUuid}/${filename}`; | ||
|
|
||
| if ( | ||
| typeof attachment.size === 'number' && | ||
| attachment.size > CLOUD_AGENT_IMAGE_MAX_SIZE_BYTES | ||
| ) { | ||
| throw new Error( | ||
| `Image ${attachment.name ?? filename} exceeds ${CLOUD_AGENT_IMAGE_MAX_SIZE_BYTES / (1024 * 1024)}MB limit (${(attachment.size / (1024 * 1024)).toFixed(1)}MB)` | ||
| ); | ||
| } | ||
|
|
||
| const data = await attachment.fetchData(); | ||
|
|
||
| if (data.byteLength > CLOUD_AGENT_IMAGE_MAX_SIZE_BYTES) { | ||
| throw new Error( | ||
| `Image ${attachment.name ?? filename} exceeds ${CLOUD_AGENT_IMAGE_MAX_SIZE_BYTES / (1024 * 1024)}MB limit (${(data.byteLength / (1024 * 1024)).toFixed(1)}MB)` | ||
| ); | ||
| } | ||
|
|
||
| await r2Client.send( | ||
| new PutObjectCommand({ | ||
| Bucket: r2CloudAgentAttachmentsBucketName, | ||
| Key: r2Key, | ||
| Body: data, | ||
| ContentType: attachment.mimeType, | ||
| ContentLength: data.byteLength, | ||
| Metadata: { userId, messageUuid, imageId }, | ||
| }) | ||
| ); | ||
|
|
||
| return filename; | ||
| }) | ||
| ); | ||
|
|
||
| const filenames: string[] = []; | ||
| for (const result of uploadResults) { | ||
| if (result.status === 'fulfilled') { | ||
| filenames.push(result.value); | ||
| } else { | ||
| console.error('[KiloBot] Failed to upload image attachment:', result.reason); | ||
| captureException(result.reason, { | ||
| tags: { component: 'kilo-bot', op: 'upload-slack-image' }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (filenames.length === 0) return undefined; | ||
|
|
||
| return { path: messageUuid, files: filenames }; | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.