π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
β¨ feat: add lobeAgents markdown tag for inline agent card rendering (#14495)
LiJiana372acd50d99
diff --git a/packages/builtin-tool-agent-management/src/systemRole.ts b/packages/builtin-tool-agent-management/src/systemRole.tsindex 081eb4fc791..234662bd49e 100644--- a/packages/builtin-tool-agent-management/src/systemRole.ts+++ b/packages/builtin-tool-agent-management/src/systemRole.ts@@ -275,6 +275,38 @@ The agent will work in the background and return results upon completion.3. Call the agent with instructions that leverage the installed plugins</workflow_patterns>+<agent_card_rendering>+## Rendering Agent Cards++After successfully creating, duplicating, or finding an agent, render a clickable agent card by outputting a \`<lobeAgents>\` tag. This card appears inline in the conversation and lets the user navigate directly to the agent.++**Format:**+\`\`\`+<lobeAgents identifier="{sessionId or agentId}" title="{title}" description="{description}" avatar="{avatar}" backgroundColor="{backgroundColor}" />+\`\`\`++**Attribute rules:**+- **identifier** (required): Use \`sessionId\` from the tool result if available, otherwise use \`agentId\`+- **title** (required): The agent's display name+- **description** (optional): Brief description of the agent+- **avatar** (optional): Emoji or image URL used for the agent+- **backgroundColor** (optional): The agent's background color++**When to render:**+- After **createAgent** succeeds β render a card for the newly created agent+- After **duplicateAgent** succeeds β render a card for the duplicated agent+- After **searchAgent** returns results β render a card for each relevant agent found (up to 5)++**Example β after createAgent:**+\`\`\`+I've created your coding assistant agent.++<lobeAgents identifier="session-abc123" title="Coding Assistant" description="Expert in TypeScript and React" avatar="π»" backgroundColor="#3B82F6" />+\`\`\`++Do NOT render a card when calling \`getAgentDetail\`, \`updateAgent\`, \`updatePrompt\`, \`deleteAgent\`, or \`installPlugin\`.+</agent_card_rendering>+<best_practices>## Best Practicesdiff --git a/packages/const/src/plugin.ts b/packages/const/src/plugin.tsindex 4f045b74a23..dfeff393c1e 100644--- a/packages/const/src/plugin.ts+++ b/packages/const/src/plugin.ts@@ -19,3 +19,6 @@ export const ARTIFACT_THINKING_TAG_REGEX = /<lobeThinking\b[^>]*>([\S\s]*?)(?:<\export const THINKING_TAG_REGEX = /<think\b[^>]*>([\S\s]*?)(?:<\/think>|$)/;export const MENTION_TAG_REGEX = /<mention\b[^>]*>([\S\s]*?)(?:<\/mention>|$)/;++export const AGENTS_TAG = 'lobeAgents';+export const AGENTS_TAG_REGEX = /<lobeAgents\b[^>]*(?:\/>|>([\S\s]*?)(?:<\/lobeAgents>|$))/;diff --git a/src/features/Conversation/Markdown/plugins/LobeAgents/Render/index.tsx b/src/features/Conversation/Markdown/plugins/LobeAgents/Render/index.tsxnew file mode 100644index 00000000000..4eec7bd49d8--- /dev/null+++ b/src/features/Conversation/Markdown/plugins/LobeAgents/Render/index.tsx@@ -0,0 +1,92 @@+'use client';++import { SESSION_CHAT_URL } from '@lobechat/const';+import { Avatar, Flexbox } from '@lobehub/ui';+import { createStaticStyles } from 'antd-style';+import { ArrowRight } from 'lucide-react';+import { memo, useCallback } from 'react';+import { useNavigate } from 'react-router-dom';++import { type MarkdownElementProps } from '../../type';++const styles = createStaticStyles(({ css, cssVar }) => ({+ arrowIcon: css`+ flex-shrink: 0;+ color: ${cssVar.colorTextTertiary};+ `,+ card: css`+ cursor: pointer;++ padding-block: 10px;+ padding-inline: 12px;+ border-radius: 8px;++ background: ${cssVar.colorFillQuaternary};++ transition: background 0.15s;++ &:hover {+ background: ${cssVar.colorFillTertiary};+ }+ `,+ description: css`+ overflow: hidden;++ font-size: 12px;+ color: ${cssVar.colorTextSecondary};+ text-overflow: ellipsis;+ white-space: nowrap;+ `,+ title: css`+ font-size: 13px;+ font-weight: 500;+ `,+}));++interface LobeAgentsProps extends MarkdownElementProps {+ avatar?: string;+ backgroundColor?: string;+ description?: string;+ identifier: string;+ title: string;+}++const Render = memo<LobeAgentsProps>(+ ({ identifier, title, description, avatar, backgroundColor }) => {+ const navigate = useNavigate();++ const handleClick = useCallback(() => {+ if (!identifier) return;+ navigate(SESSION_CHAT_URL(identifier));+ }, [navigate, identifier]);++ if (!identifier) return null;++ return (+ <Flexbox+ align={'center'}+ className={styles.card}+ gap={12}+ horizontal+ onClick={handleClick}+ >+ <Avatar+ avatar={avatar || 'π€'}+ background={backgroundColor}+ shape={'square'}+ size={36}+ title={title || undefined}+ />+ <Flexbox flex={1} gap={2}>+ <span className={styles.title}>{title || identifier}</span>+ {description && <span className={styles.description}>{description}</span>}+ </Flexbox>+ <ArrowRight className={styles.arrowIcon} size={16} />+ </Flexbox>+ );+ },+);++Render.displayName = 'LobeAgentsRender';++export default Render;diff --git a/src/features/Conversation/Markdown/plugins/LobeAgents/index.ts b/src/features/Conversation/Markdown/plugins/LobeAgents/index.tsnew file mode 100644index 00000000000..e2d42d2a7e6--- /dev/null+++ b/src/features/Conversation/Markdown/plugins/LobeAgents/index.ts@@ -0,0 +1,16 @@+import { type FC } from 'react';++import { AGENTS_TAG } from '@/const/plugin';++import { type MarkdownElement, type MarkdownElementProps } from '../type';+import rehypePlugin from './rehypePlugin';+import Component from './Render';++const LobeAgentsElement: MarkdownElement = {+ Component: Component as unknown as FC<MarkdownElementProps>,+ rehypePlugin,+ scope: 'assistant',+ tag: AGENTS_TAG,+};++export default LobeAgentsElement;diff --git a/src/features/Conversation/Markdown/plugins/LobeAgents/rehypePlugin.ts b/src/features/Conversation/Markdown/plugins/LobeAgents/rehypePlugin.tsnew file mode 100644index 00000000000..739f4917a49--- /dev/null+++ b/src/features/Conversation/Markdown/plugins/LobeAgents/rehypePlugin.ts@@ -0,0 +1,61 @@+import { SKIP, visit } from 'unist-util-visit';++import { AGENTS_TAG } from '@/const/plugin';++const attributeRegex = /(\w+)=["']([^"']*)["']/g;++function parseAttributes(raw: string): Record<string, string> {+ const attributes: Record<string, string> = {};+ let match;+ attributeRegex.lastIndex = 0;+ while ((match = attributeRegex.exec(raw)) !== null) {+ attributes[match[1]] = match[2];+ }+ return attributes;+}++function rehypeLobeAgents() {+ return (tree: any) => {+ visit(tree, (node, index, parent) => {+ // Handle <lobeAgents .../> wrapped inside a <p> element+ if (node.type === 'element' && node.tagName === 'p' && node.children.length > 0) {+ const firstChild = node.children[0];+ if (firstChild.type === 'raw' && firstChild.value.startsWith(`<${AGENTS_TAG}`)) {+ const newNode = {+ children: [],+ properties: parseAttributes(firstChild.value),+ tagName: AGENTS_TAG,+ type: 'element',+ };++ // P1 fix: preserve any trailing siblings in the paragraph+ const remainingChildren = node.children.slice(1).filter(+ (c: any) => !(c.type === 'raw' && c.value.startsWith('</')),+ );++ if (remainingChildren.length > 0) {+ const remainingP = { ...node, children: remainingChildren };+ parent.children.splice(index, 1, newNode, remainingP);+ } else {+ parent.children.splice(index, 1, newNode);+ }+ return [SKIP, index];+ }+ }+ // Handle bare <lobeAgents .../> raw node (self-closing or opening tag)+ else if (node.type === 'raw' && node.value.startsWith(`<${AGENTS_TAG}`)) {+ const newNode = {+ children: [],+ properties: parseAttributes(node.value),+ tagName: AGENTS_TAG,+ type: 'element',+ };++ parent.children.splice(index, 1, newNode);+ return [SKIP, index];+ }+ });+ };+}++export default rehypeLobeAgents;diff --git a/src/features/Conversation/Markdown/plugins/index.ts b/src/features/Conversation/Markdown/plugins/index.tsindex 736eb031105..97fff872f88 100644--- a/src/features/Conversation/Markdown/plugins/index.ts+++ b/src/features/Conversation/Markdown/plugins/index.ts@@ -1,4 +1,5 @@import ImageSearchRef from './ImageSearchRef';+import LobeAgents from './LobeAgents';import LobeArtifact from './LobeArtifact';import LobeThinking from './LobeThinking';import LocalFile from './LocalFile';@@ -17,4 +18,5 @@ export const markdownElements: MarkdownElement[] = [Mention,Task,ImageSearchRef,+ LobeAgents,];diff --git a/src/features/Conversation/utils/markdown.ts b/src/features/Conversation/utils/markdown.tsindex aa23a9a631b..0dffd8bc0f5 100644--- a/src/features/Conversation/utils/markdown.ts+++ b/src/features/Conversation/utils/markdown.ts@@ -3,6 +3,8 @@ import { ARTIFACT_THINKING_TAG_REGEX } from '@lobechat/const';const ARTIFACT_TAG_REGEX_GLOBAL =/<lobeArtifact\b[^>]*>(?<content>[\S\s]*?)(?:<\/lobeArtifact>|$)/g;+const AGENTS_TAG_REGEX_GLOBAL = /<lobeAgents\b[^>]*(?:\/>|>([\S\s]*?)(?:<\/lobeAgents>|$))/g;+/*** Replace all line breaks in the matched `lobeArtifact` tag with an empty string*/@@ -63,6 +65,11 @@ export const processWithArtifact = (input: string = '') => {output = output.replace(regex, '<lobeArtifact>');}+ // Strip newlines inside lobeAgents tags (self-closing or wrapping)+ output = output.replaceAll(AGENTS_TAG_REGEX_GLOBAL, (match) =>+ match.replaceAll(/\r?\n|\r/g, ''),+ );+return output;};