-
Notifications
You must be signed in to change notification settings - Fork 26
feat(bot): add PR signature to Cloud Agent prompts in bot SDK #1914
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
Merged
+142
−8
Merged
Changes from all commits
Commits
Show all changes
3 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,104 @@ | ||
| import { getAccessTokenFromInstallation } from '@/lib/integrations/slack-service'; | ||
| import { getSlackMessagePermalink } from '@/lib/slack-bot/slack-utils'; | ||
| import { WebClient } from '@slack/web-api'; | ||
| import type { SlackEvent } from '@chat-adapter/slack'; | ||
| import type { PlatformIntegration } from '@kilocode/db'; | ||
| import type { Thread, Message } from 'chat'; | ||
|
|
||
| type RequesterInfo = { | ||
| displayName: string; | ||
| messageLink?: string; | ||
| platform: string; | ||
| }; | ||
|
|
||
| const PLATFORM_LINKS: Record<string, { label: string; url: string }> = { | ||
| slack: { label: 'Kilo for Slack', url: 'https://kilo.ai/features/slack-integration' }, | ||
| discord: { label: 'Kilo for Discord', url: 'https://kilo.ai' }, | ||
| }; | ||
|
|
||
| const DEFAULT_PLATFORM_LINK = { label: 'Kilo', url: 'https://kilo.ai' }; | ||
|
|
||
| /** | ||
| * Build the PR signature instruction to append to the Cloud Agent prompt. | ||
| * Instructs the agent to include a "Built for …" line at the end of any | ||
| * PR/MR description it creates. | ||
| */ | ||
| export function buildPrSignature(requesterInfo: RequesterInfo): string { | ||
| const requesterPart = requesterInfo.messageLink | ||
| ? `[${requesterInfo.displayName}](${requesterInfo.messageLink})` | ||
| : requesterInfo.displayName; | ||
|
|
||
| const { label, url } = PLATFORM_LINKS[requesterInfo.platform] ?? DEFAULT_PLATFORM_LINK; | ||
|
|
||
| return ` | ||
|
|
||
| --- | ||
| **PR Signature to include in the PR description:** | ||
| When you create a pull request or merge request, include the following signature at the end of the PR/MR description: | ||
|
|
||
| Built for ${requesterPart} by [${label}](${url})`; | ||
| } | ||
|
|
||
| /** | ||
| * Gather requester info (display name + message link) for the PR signature. | ||
| * Platform-specific: uses the Slack API for permalinks, constructs Discord | ||
| * links from IDs, and degrades gracefully for unknown platforms. | ||
| */ | ||
| export async function getRequesterInfo( | ||
| thread: Thread, | ||
| message: Message, | ||
| platformIntegration: PlatformIntegration | ||
| ): Promise<RequesterInfo | undefined> { | ||
| const platform = thread.id.split(':')[0]; | ||
| const displayName = message.author.fullName || message.author.userName || message.author.userId; | ||
|
|
||
| switch (platform) { | ||
| case 'slack': | ||
| return getSlackRequesterInfo(message, platformIntegration, displayName); | ||
| case 'discord': | ||
| return getDiscordRequesterInfo(message, displayName); | ||
| default: | ||
| return { displayName, platform }; | ||
| } | ||
| } | ||
|
|
||
| async function getSlackRequesterInfo( | ||
| message: Message, | ||
| platformIntegration: PlatformIntegration, | ||
| displayName: string | ||
| ): Promise<RequesterInfo> { | ||
| const accessToken = getAccessTokenFromInstallation(platformIntegration); | ||
| if (!accessToken) { | ||
| return { displayName, platform: 'slack' }; | ||
| } | ||
|
|
||
| const raw = (message as Message<SlackEvent>).raw; | ||
| const channelId = | ||
| typeof raw === 'object' && raw !== null && 'channel' in raw | ||
| ? (raw as { channel?: string }).channel | ||
| : undefined; | ||
| const messageTs = message.id; // chat SDK uses Slack ts as the message ID | ||
|
|
||
| if (!channelId || !messageTs) { | ||
| return { displayName, platform: 'slack' }; | ||
| } | ||
|
|
||
| const slackClient = new WebClient(accessToken); | ||
| const permalink = await getSlackMessagePermalink(slackClient, channelId, messageTs); | ||
|
|
||
| return { displayName, messageLink: permalink, platform: 'slack' }; | ||
| } | ||
|
|
||
| function getDiscordRequesterInfo(message: Message, displayName: string): RequesterInfo { | ||
| const raw = message.raw as { guild_id?: string; channel_id?: string } | null; | ||
| const guildId = raw?.guild_id; | ||
| const channelId = raw?.channel_id; | ||
| const messageId = message.id; | ||
|
|
||
| const messageLink = | ||
| guildId && channelId && messageId | ||
| ? `https://discord.com/channels/${guildId}/${channelId}/${messageId}` | ||
| : undefined; | ||
|
|
||
| return { displayName, messageLink, platform: 'discord' }; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Escape requester names before interpolating them into markdown
displayNamecomes from Slack/Discord user profiles and is user-controlled. Names containing markdown metacharacters like],(, or)will break the generatedBuilt for [...]()link, and can also inject unintended markdown/instructions into the prompt sent to Cloud Agent. Escape the name before building the link, or render the linked case without reusing raw markdown syntax.