Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ’„ style(conversation): show GitHub as the sender of SCM wake-up messages (#20448)

Arvin Xu697cc7e68c42
diff --git a/src/features/Conversation/Markdown/plugins/ScmEvent/Render.tsx b/src/features/Conversation/Markdown/plugins/ScmEvent/Render.tsx
index d3113e8f668..52700659e0e 100644
--- a/src/features/Conversation/Markdown/plugins/ScmEvent/Render.tsx
+++ b/src/features/Conversation/Markdown/plugins/ScmEvent/Render.tsx
@@ -1,6 +1,5 @@
'use client';
-import { Github } from '@lobehub/icons';
import { Flexbox, Icon } from '@lobehub/ui';
import { Text } from '@lobehub/ui/base-ui';
import { createStaticStyles, cx } from 'antd-style';
@@ -18,7 +17,6 @@ import { type MarkdownElementProps } from '../type';
import {
type ParsedScmEvent,
parseScmEvent,
- safeScmUrl,
type ScmEventAttributes,
type ScmEventCheck,
type ScmEventReview,
@@ -42,11 +40,6 @@ const styles = createStaticStyles(({ css, cssVar }) => ({
checkPassed: css`
color: ${cssVar.colorSuccess};
`,
- header: css`
- padding-block: 4px 10px;
- padding-inline: 0;
- border-block-end: 1px solid ${cssVar.colorBorderSecondary};
- `,
instruction: css`
padding-block: 8px 2px;
padding-inline: 0;
@@ -110,20 +103,6 @@ const styles = createStaticStyles(({ css, cssVar }) => ({
transform: rotate(90deg);
}
`,
- mark: css`
- display: flex;
- flex: none;
- align-items: center;
- justify-content: center;
-
- inline-size: 28px;
- block-size: 28px;
- border-radius: 6px;
-
- color: ${cssVar.colorText};
-
- background: ${cssVar.colorFillTertiary};
- `,
mono: css`
font-family: ${cssVar.fontFamilyCode};
font-size: 12px;
@@ -276,7 +255,8 @@ ReviewRow.displayName = 'ScmEventReviewRow';
/**
* A GitHub-styled card for the wake-up message the SCM integration injects
- * into a conversation: the pull request in the header, then one status
+ * into a conversation. The pull request is the message's sender (see the
+ * user message header), so the card opens with one status
* line that carries the event (as a pill) together with what it is about β€”
* the failing check with its details link and log toggle, or the reviewers
* β€” and the instruction the agent was given as a footer.
@@ -287,48 +267,19 @@ const Render = memo<MarkdownElementProps<ScmEventAttributes>>(({ children, node
const text = typeof children === 'string' ? children : String(children ?? '');
const parsed = useMemo<ParsedScmEvent>(() => parseScmEvent(text), [text]);
- const title = attrs.repo
- ? `${attrs.repo}${attrs.number ? ` #${attrs.number}` : ''}`
- : (attrs.url ?? '');
const kindLabel = t(`scmEvent.kind.${attrs.kind}` as any, { defaultValue: attrs.kind });
- const subtitle = [attrs.branch, attrs.sha].filter(Boolean).join(' @ ');
const pill = (
<span className={cx(styles.pill, PILL_STYLE[attrs.kind] ?? styles.pillNeutral)}>
{kindLabel}
</span>
);
- // The header link is a tag attribute, so it skips the parser's guard.
- const headerUrl = safeScmUrl(attrs.url);
const single = parsed.checks.length === 1 ? parsed.checks[0] : null;
const many = parsed.checks.length > 1 ? parsed.checks : null;
const reviewers = [...new Set(parsed.reviews.map((review) => `@${review.author}`))];
return (
<div className={styles.root}>
- <Flexbox horizontal align="center" className={styles.header} gap={10}>
- <span className={styles.mark}>
- <Github size={16} />
- </span>
- <Flexbox flex={1} gap={1} style={{ minWidth: 0 }}>
- <Text ellipsis weight={500}>
- {headerUrl ? (
- <a className={styles.link} href={headerUrl} rel="noreferrer" target="_blank">
- {title}
- <Icon icon={ExternalLinkIcon} size={12} />
- </a>
- ) : (
- title
- )}
- </Text>
- {subtitle ? (
- <Text ellipsis className={styles.mono} type="secondary">
- {subtitle}
- </Text>
- ) : null}
- </Flexbox>
- </Flexbox>
-
<div className={styles.body}>
{single ? <CheckRow check={single} leading={pill} /> : null}
{many ? (
diff --git a/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.test.ts b/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.test.ts
index 419c9796dd1..b29a0ab8924 100644
--- a/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.test.ts
+++ b/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.test.ts
@@ -1,6 +1,12 @@
import { describe, expect, it } from 'vitest';
-import { parseAttributes, parseScmEvent, safeScmUrl } from './parseScmEvent';
+import {
+ getScmEventSource,
+ parseAttributes,
+ parseScmEvent,
+ safeScmUrl,
+ scmEventTitle,
+} from './parseScmEvent';
// Mirrors what apps/server/src/services/scm/wakePrompt.ts emits.
const ciInner = `<check conclusion="failure" name="Test" url="https://github.com/o/r/actions/runs/9/job/2">
@@ -114,3 +120,39 @@ describe('safeScmUrl', () => {
expect(parsed.reviews[0]).toMatchObject({ author: 'a', url: undefined });
});
});
+
+describe('getScmEventSource', () => {
+ it('reads the pull request a wake-up message is from', () => {
+ const content = `<scmEvent branch="fix/x" kind="review_commented" number="7" provider="github" repo="o/r" sha="260d994" url="https://github.com/o/r/pull/7">
+${reviewInner}
+</scmEvent>`;
+ const source = getScmEventSource(content);
+ expect(source).toEqual({
+ branch: 'fix/x',
+ kind: 'review_commented',
+ number: '7',
+ provider: 'github',
+ repo: 'o/r',
+ sha: '260d994',
+ url: 'https://github.com/o/r/pull/7',
+ });
+ expect(scmEventTitle(source!)).toBe('o/r #7');
+ });
+
+ it('is undefined for an ordinary message', () => {
+ expect(getScmEventSource('hello')).toBeUndefined();
+ expect(getScmEventSource(undefined)).toBeUndefined();
+ });
+
+ it('ignores a tag quoted inside an ordinary message', () => {
+ expect(
+ getScmEventSource('Why does this render?\n```\n<scmEvent repo="o/r" number="7">\n```'),
+ ).toBeUndefined();
+ });
+
+ it('titles a repo-less event by its url', () => {
+ expect(scmEventTitle({ url: 'https://github.com/o/r/pull/7' })).toBe(
+ 'https://github.com/o/r/pull/7',
+ );
+ });
+});
diff --git a/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.ts b/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.ts
index cda4557588f..905d2f4a36a 100644
--- a/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.ts
+++ b/src/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent.ts
@@ -57,6 +57,27 @@ export const safeScmUrl = (value: string | undefined): string | undefined => {
/** `key="value"` pairs of an open tag, values unescaped. */
export const parseAttributes = parseXmlAttributes;
+/**
+ * The attributes of a wake-up message's `<scmEvent>` block, or undefined for
+ * any other message. The user message uses them to show the pull request as
+ * the message's sender instead of a header inside the card. The server sends
+ * the block as the whole message, so only a message that opens with it
+ * counts β€” a user quoting the tag mid-message keeps their own identity.
+ */
+export const getScmEventSource = (
+ content: string | null | undefined,
+): ScmEventAttributes | undefined => {
+ if (!content) return undefined;
+ const open = /^\s*<scmEvent\b([^>]*)>/.exec(content);
+ if (!open) return undefined;
+ const attrs = parseAttributes(open[1] ?? '');
+ return { ...attrs, kind: attrs.kind ?? '', provider: attrs.provider ?? '' };
+};
+
+/** `owner/repo #number`, falling back to the url when the repo is unknown. */
+export const scmEventTitle = (attrs: Pick<ScmEventAttributes, 'number' | 'repo' | 'url'>) =>
+ attrs.repo ? `${attrs.repo}${attrs.number ? ` #${attrs.number}` : ''}` : (attrs.url ?? '');
+
/** The text of every CDATA section in `raw`, joined; plain text when there is none. */
const cdataText = (raw: string) => {
const sections = [...raw.matchAll(/<!\[CDATA\[([\S\s]*?)\]\]>/g)].map((m) => m[1]);
diff --git a/src/features/Conversation/Messages/User/components/ScmEventSender.tsx b/src/features/Conversation/Messages/User/components/ScmEventSender.tsx
new file mode 100644
index 00000000000..ddd06ce4dcc
--- /dev/null
+++ b/src/features/Conversation/Messages/User/components/ScmEventSender.tsx
@@ -0,0 +1,84 @@
+import { Github } from '@lobehub/icons';
+import { Flexbox } from '@lobehub/ui';
+import { Text } from '@lobehub/ui/base-ui';
+import { createStaticStyles } from 'antd-style';
+import { memo } from 'react';
+
+import {
+ safeScmUrl,
+ type ScmEventAttributes,
+ scmEventTitle,
+} from '@/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent';
+
+const styles = createStaticStyles(({ css, cssVar }) => ({
+ link: css`
+ display: inline-flex;
+ gap: 4px;
+ align-items: center;
+ color: inherit;
+
+ &:hover {
+ color: ${cssVar.colorTextSecondary};
+ }
+ `,
+ mark: css`
+ display: flex;
+ flex: none;
+ align-items: center;
+ justify-content: center;
+
+ inline-size: 36px;
+ block-size: 36px;
+ border-radius: 8px;
+
+ color: ${cssVar.colorText};
+
+ background: ${cssVar.colorFillTertiary};
+ `,
+ mono: css`
+ font-family: ${cssVar.fontFamilyCode};
+ font-size: 12px;
+ `,
+}));
+
+/** GitHub mark standing in for the avatar of a wake-up message. */
+export const ScmEventAvatar = memo(() => (
+ <span className={styles.mark}>
+ <Github size={20} />
+ </span>
+));
+
+ScmEventAvatar.displayName = 'ScmEventAvatar';
+
+/**
+ * The pull request as the sender name of a wake-up message: `owner/repo #n`
+ * linking to it, with the branch and commit it is about on a second line β€”
+ * sized to sit beside the two-line-tall GitHub mark.
+ */
+export const ScmEventSenderTitle = memo<{ source: ScmEventAttributes }>(({ source }) => {
+ const title = scmEventTitle(source);
+ const subtitle = [source.branch, source.sha].filter(Boolean).join(' @ ');
+ // The url is a tag attribute, so it skips the parser's guard.
+ const url = safeScmUrl(source.url);
+
+ return (
+ <Flexbox align={'flex-end'} gap={2} style={{ minWidth: 0 }}>
+ <Text style={{ whiteSpace: 'nowrap' }} weight={500}>
+ {url ? (
+ <a className={styles.link} href={url} rel="noreferrer" target="_blank">
+ {title}
+ </a>
+ ) : (
+ title
+ )}
+ </Text>
+ {subtitle ? (
+ <Text ellipsis className={styles.mono} type={'secondary'}>
+ {subtitle}
+ </Text>
+ ) : null}
+ </Flexbox>
+ );
+});
+
+ScmEventSenderTitle.displayName = 'ScmEventSenderTitle';
diff --git a/src/features/Conversation/Messages/User/index.tsx b/src/features/Conversation/Messages/User/index.tsx
index 8dda91544ff..5f632706138 100644
--- a/src/features/Conversation/Messages/User/index.tsx
+++ b/src/features/Conversation/Messages/User/index.tsx
@@ -7,6 +7,7 @@ import { useTranslation } from 'react-i18next';
import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import { ChatItem } from '@/features/Conversation/ChatItem';
+import { getScmEventSource } from '@/features/Conversation/Markdown/plugins/ScmEvent/parseScmEvent';
import { useMessageCommentCount } from '@/features/TopicComment/hooks';
import MessageCommentBadge from '@/features/TopicComment/MessageCommentBadge';
import { useUserAvatar } from '@/hooks/useUserAvatar';
@@ -23,6 +24,7 @@ import {
} from '../Contexts/message-action-context';
import Actions from './Actions';
import UserMessageContent from './components/MessageContent';
+import { ScmEventAvatar, ScmEventSenderTitle } from './components/ScmEventSender';
import { UserMessageExtra } from './Extra';
import { getBotSender, resolveSenderIdentity } from './resolveSenderIdentity';
import ScheduledRunFooter from './ScheduledRunFooter';
@@ -37,6 +39,9 @@ const UserMessage = memo<UserMessageProps>(({ id, disableEditing, index }) => {
const item = useConversationStore(dataSelectors.getDisplayMessageById(id), isEqual)!;
const { content, createdAt, error, role, extra, targetId, sender, metadata } = item;
const botSender = getBotSender(item);
+ // A wake-up message from the SCM integration is authored by the pull
+ // request, so GitHub takes the sender slot instead of the card's header.
+ const scmSource = useMemo(() => getScmEventSource(content), [content]);
const { t } = useTranslation('chat');
const selfAvatar = useUserAvatar();
@@ -50,7 +55,7 @@ const UserMessage = memo<UserMessageProps>(({ id, disableEditing, index }) => {
// rows β€” see resolveSenderIdentity.
// A bot-channel row is authored by someone else even in personal mode, so
// its sender is always shown.
- const showSender = Boolean(activeWorkspaceId) || !!botSender;
+ const showSender = Boolean(activeWorkspaceId) || !!botSender || !!scmSource;
const currentUserId = useUserStore(userProfileSelectors.userId);
const { avatar, title } = resolveSenderIdentity({
botSender,
@@ -107,14 +112,14 @@ const UserMessage = memo<UserMessageProps>(({ id, disableEditing, index }) => {
actions={<Actions data={item} disableEditing={disableEditing} id={id} />}
avatar={{ avatar, title }}
belowMessage={<ScheduledRunFooter id={id} />}
+ customAvatarRender={scmSource ? () => <ScmEventAvatar /> : undefined}
editing={editing}
- headerAddon={metadata?.steer ? <Tag>{t('steer.tag')}</Tag> : undefined}
id={id}
message={content}
messageExtra={<UserMessageExtra extra={extra} id={id} />}
placement={'right'}
showAvatar={showSender}
- showTitle={showSender}
+ showTitle={showSender && !scmSource}
time={createdAt}
titleAddon={dmIndicator}
actionAddon={
@@ -122,6 +127,14 @@ const UserMessage = memo<UserMessageProps>(({ id, disableEditing, index }) => {
<MessageCommentBadge count={commentCount} messageId={id} topicId={commentTopicId} />
) : undefined
}
+ headerAddon={
+ scmSource || metadata?.steer ? (
+ <>
+ {scmSource && <ScmEventSenderTitle source={scmSource} />}
+ {metadata?.steer && <Tag>{t('steer.tag')}</Tag>}
+ </>
+ ) : undefined
+ }
onDoubleClick={onDoubleClick}
onMouseEnter={onMouseEnter}
>