π€― 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(chat): resolve internal entity links into titles and surface the underline (#19055)
Arvin Xu227228dfd05e
diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityLink.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityLink.tsxindex 3859c196cff..2d8565e9a91 100644--- a/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityLink.tsx+++ b/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityLink.tsx@@ -13,6 +13,7 @@ import {} from 'lucide-react';import type { MouseEvent } from 'react';import { memo, useCallback } from 'react';+import { useTranslation } from 'react-i18next';import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';import { shouldHardNavigateToWorkbench } from '@/libs/next/workbenchNavigation';@@ -21,8 +22,12 @@ import { agentDocumentService, agentDocumentSWRKeys } from '@/services/agentDocuimport { useAgentStore } from '@/store/agent';import { useChatStore } from '@/store/chat';-import type { InternalLinkReference } from '../internalLink';-import { InternalEntityPreview } from './InternalEntityPreview';+import { type InternalLinkReference, isBareLinkLabel } from '../internalLink';+import {+ getPreviewData,+ InternalEntityPreview,+ internalEntityPreviewKey,+} from './InternalEntityPreview';const styles = createStaticStyles(({ css, cssVar }) => ({link: css`@@ -31,7 +36,11 @@ const styles = createStaticStyles(({ css, cssVar }) => ({align-items: center;color: ${cssVar.colorText} !important;- text-decoration-color: ${cssVar.colorBorder};++ /* Tertiary, not colorBorder: on a dark background colorBorder sits so+ close to the bubble that the underline vanishes and the link reads as+ plain text β findability is the underline's whole job here. */+ text-decoration-color: ${cssVar.colorTextTertiary};text-decoration-line: underline;text-decoration-thickness: 1px;text-underline-offset: 3px;@@ -73,6 +82,7 @@ interface InternalEntityLinkProps {}export const InternalEntityLink = memo<InternalEntityLinkProps>(({ href, label, reference }) => {+ const { t } = useTranslation('chat');const navigate = useWorkspaceAwareNavigate();const activeAgentId = useAgentStore((s) => s.activeAgentId);const [openAcceptance, openAgentDetail, openDocument, openTaskDetail, openVerifyReport] =@@ -83,6 +93,23 @@ export const InternalEntityLink = memo<InternalEntityLinkProps>(({ href, label,s.openTaskDetail,s.openVerifyReport,]);+ // A pasted URL says nothing about what it points to, so resolve the entity's+ // own title and show that instead β the same read the hover preview makes,+ // under the same key, so the eager fetch also makes the hover card instant.+ // Bare links are sparse in real messages, which is why prefetching the full+ // preview beats a dedicated title read. Cross-workspace links are skipped:+ // the ambient client resolves against the ACTIVE scope, where a+ // workspace-unique id like T-198 can name a different entity entirely.+ // Authored link text is never replaced; a failed read just leaves the URL.+ const shouldResolveTitle =+ reference.type !== 'route' && !reference.workspaceSlug && isBareLinkLabel(label, href);+ const { data: entity } = useClientDataSWR(+ shouldResolveTitle ? internalEntityPreviewKey(reference) : null,+ () => getPreviewData(reference, t),+ { revalidateOnFocus: false },+ );+ const displayLabel = (shouldResolveTitle && entity?.title) || label;+const linkedAgentId = reference.type === 'document' ? reference.agentId : undefined;const shouldResolveAgentDocument = !!linkedAgentId && linkedAgentId === activeAgentId;const { data: agentDocuments, mutate: resolveAgentDocuments } = useClientDataSWR(@@ -197,14 +224,14 @@ export const InternalEntityLink = memo<InternalEntityLinkProps>(({ href, label,onClick={handleClick}>{icon && <Icon icon={icon} size={14} />}- {label}+ {displayLabel}</a>);if (reference.type === 'route' || reference.workspaceSlug) return link;return (- <InternalEntityPreview fallbackTitle={label} reference={reference}>+ <InternalEntityPreview fallbackTitle={displayLabel} reference={reference}>{link}</InternalEntityPreview>);diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityPreview.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityPreview.tsxindex 56cf0cc644e..d7af7a85515 100644--- a/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityPreview.tsx+++ b/src/features/Conversation/Markdown/plugins/Link/Render/InternalEntityPreview.tsx@@ -109,7 +109,17 @@ const getVerifyStatusLabel = (status: string | null | undefined, t: TFunction<'c}};-const getPreviewData = async (+/**+ * One cache entry per entity, shared between the hover preview and the eager+ * title resolution on the link itself β whichever fires first warms the other.+ */+export const internalEntityPreviewKey = (reference: InternalLinkReference) => [+ 'internal-entity-preview',+ reference.type,+ reference.pathname,+];++export const getPreviewData = async (reference: InternalLinkReference,t: TFunction<'chat'>,): Promise<PreviewData | null> => {@@ -205,7 +215,7 @@ export const InternalEntityPreview = memo<InternalEntityPreviewProps>(const { t } = useTranslation('chat');const [open, setOpen] = useState(false);const { data, isLoading } = useClientDataSWR(- open ? ['internal-entity-preview', reference.type, reference.pathname] : null,+ open ? internalEntityPreviewKey(reference) : null,() => getPreviewData(reference, t),{ revalidateOnFocus: false },);diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/index.test.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/index.test.tsxindex f51cb897e46..c11f2324554 100644--- a/src/features/Conversation/Markdown/plugins/Link/Render/index.test.tsx+++ b/src/features/Conversation/Markdown/plugins/Link/Render/index.test.tsx@@ -45,11 +45,18 @@ vi.mock('@/features/Workspace/useWorkspaceAwareNavigate', () => ({useWorkspaceAwareNavigate: () => mockNavigate,}));+// What the shared preview/title read answers for this case (null = unresolved).+let mockEntityPreview: { title?: string } | null = null;+vi.mock('@/libs/swr', () => ({- useClientDataSWR: () => ({- data: [{ documentId: 'docs_doc1', id: 'agent-document-1' }],- mutate: vi.fn(),- }),+ useClientDataSWR: (key: unknown) => {+ if (Array.isArray(key) && key[0] === 'internal-entity-preview')+ return { data: mockEntityPreview, mutate: vi.fn() };+ return {+ data: [{ documentId: 'docs_doc1', id: 'agent-document-1' }],+ mutate: vi.fn(),+ };+ },}));vi.mock('@/services/agentDocument', () => ({@@ -97,6 +104,7 @@ const renderLink = (properties: Record<string, unknown>) =>afterEach(() => {mockShowIcon = true;mockConst.isDesktop = false;+ mockEntityPreview = null;vi.restoreAllMocks();});@@ -176,6 +184,72 @@ describe('Link Render β message link icon toggle', () => {});describe('Link Render β internal entities', () => {+ it('replaces a pasted URL label with the resolved entity title', () => {+ // A raw acceptance URL says nothing about what it points to; once the+ // entity resolves, the link reads as its title.+ mockEntityPreview = { title: 'Gateway ζΆζ―ιειε»Ί' };++ const { container } = renderLink({+ linkHref: '/acceptance/acceptance-1',+ linkKind: 'generic',+ linkLabel: '/acceptance/acceptance-1',+ });++ const anchor = container.querySelector('a')!;+ expect(anchor.textContent).toBe('Gateway ζΆζ―ιειε»Ί');+ expect(anchor.getAttribute('href')).toBe('/acceptance/acceptance-1');+ });++ it('keeps the pasted URL while the title has not resolved', () => {+ mockEntityPreview = null;++ const { container } = renderLink({+ linkHref: '/acceptance/acceptance-1',+ linkKind: 'generic',+ linkLabel: '/acceptance/acceptance-1',+ });++ expect(container.querySelector('a')!.textContent).toBe('/acceptance/acceptance-1');+ });++ it('never replaces authored link text with the entity title', () => {+ mockEntityPreview = { title: 'Resolved title' };++ const { container } = renderLink({+ linkHref: '/acceptance/acceptance-1',+ linkKind: 'generic',+ linkLabel: 'Acceptance',+ });++ expect(container.querySelector('a')!.textContent).toBe('Acceptance');+ });++ it('keeps a URL-shaped authored label β bare means label === href, not "looks like a URL"', () => {+ mockEntityPreview = { title: 'Resolved title' };++ const { container } = renderLink({+ linkHref: '/task/T-198',+ linkKind: 'generic',+ linkLabel: 'https://docs.example',+ });++ expect(container.querySelector('a')!.textContent).toBe('https://docs.example');+ });++ it('does not resolve titles for workspace-qualified links', () => {+ // The ambient client reads the ACTIVE scope; /lobe-team/task/T-198 may+ // name a different entity there, so the URL stays.+ mockEntityPreview = { title: 'Wrong-scope title' };++ const { container } = renderLink({+ linkHref: '/lobe-team/task/T-198',+ linkKind: 'generic',+ linkLabel: '/lobe-team/task/T-198',+ });++ expect(container.querySelector('a')!.textContent).toBe('/lobe-team/task/T-198');+ });+it('opens acceptance links in the conversation portal', () => {const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => undefined);diff --git a/src/features/Conversation/Markdown/plugins/Link/internalLink.test.ts b/src/features/Conversation/Markdown/plugins/Link/internalLink.test.tsindex 039d405b9fa..8aed56c13a4 100644--- a/src/features/Conversation/Markdown/plugins/Link/internalLink.test.ts+++ b/src/features/Conversation/Markdown/plugins/Link/internalLink.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest';-import { parseInternalLink } from './internalLink';+import { isBareLinkLabel, parseInternalLink } from './internalLink';describe('parseInternalLink', () => {it('parses official agent document links', () => {@@ -166,3 +166,19 @@ describe('parseInternalLink', () => {expect(parseInternalLink('/.well-known/assetlinks.json')).toBeNull();});});++describe('isBareLinkLabel', () => {+ it('treats a label that IS the address as bare', () => {+ expect(isBareLinkLabel('/acceptance/acc_1', '/acceptance/acc_1')).toBe(true);+ expect(+ isBareLinkLabel('https://app.lobehub.com/task/tsk_1', 'https://app.lobehub.com/task/tsk_1'),+ ).toBe(true);+ });++ it('treats authored text as not bare, even when it looks like a URL', () => {+ expect(isBareLinkLabel('ιͺζΆζ₯ε', '/acceptance/acc_1')).toBe(false);+ // A URL-shaped authored label must survive: the author chose it.+ expect(isBareLinkLabel('https://docs.example', '/task/T-198')).toBe(false);+ expect(isBareLinkLabel('/project plan', '/task/T-198')).toBe(false);+ });+});diff --git a/src/features/Conversation/Markdown/plugins/Link/internalLink.ts b/src/features/Conversation/Markdown/plugins/Link/internalLink.tsindex 4ed76701f78..2dda10a7890 100644--- a/src/features/Conversation/Markdown/plugins/Link/internalLink.ts+++ b/src/features/Conversation/Markdown/plugins/Link/internalLink.ts@@ -74,6 +74,18 @@ const isInternalHost = (url: URL, currentOrigin?: string) => {}};+/**+ * Whether a link's visible text is just the pasted address rather than+ * something a human wrote. Only such a label may be replaced by the entity's+ * resolved title; authored link text is the author's choice and stays.+ *+ * Exact equality, not a URL-shaped prefix test: for an autolink the rehype+ * pass sets the label to the canonical form, which for an internal link IS+ * the href β while `[https://docs.example](/task/1)` is authored text that+ * merely looks like a URL and must survive.+ */+export const isBareLinkLabel = (label: string, href: string) => label === href;+/** Parse a LobeHub route into a semantic entity reference. */export const parseInternalLink = (href: string | undefined,