feat(client): open message links in Sidebar Browser

This commit is contained in:
imccyu committed 2026-09-17 15:14:11 +08:00
1 parent 66edeb85af
commit 8968fedec2
17 files changed
+201 -39

No files matched your search

@@ -50,6 +50,7 @@ const DONE = 'LINK_GALLERY_DONE'
const GALLERY_TIME = Date.UTC(2026, 0, 15, 12)
const GUIDE_URL = 'https://docs.example.test/guide'
const HTTP_URL = 'http://docs.example.test/plain'
const API_URL = 'https://docs.example.test/api'
const RELEASES_URL = 'https://docs.example.test/releases'
const MAILTO_URL = 'mailto:[email protected]'
@@ -260,6 +261,8 @@ function galleryFixture(imageUrl: string): string {
`Docs: [style guide](${GUIDE_URL}) and \`${API_URL}\`; see [the release notes][rel], `
+ `contact [the maintainer](${MAILTO_URL}), and check the fine print[^1].`,
'',
`Preview: [plain HTTP](${HTTP_URL}).`,
'',
`Upstream: [the repository](${REPO_URL}).`,
'',
`Inert contrasts: \`curl ${API_URL}\`, \`javascript:alert(1)\`, and \`pnpm run build\`.`,
@@ -315,6 +318,10 @@ describe('web e2e: clickable links gallery', () => {
await seedSession(scaffold, galleryFixture(imageUrl), SEED_ID, undefined, { createdAt: GALLERY_TIME })
browser = await chromium.launch()
page = await newEnglishPage(browser)
await page.route(/https?:\/\/docs\.example\.test\/.*/u, async route => route.fulfill({
contentType: 'text/html',
body: `<h1>${new URL(route.request().url()).pathname}</h1>`,
}))
await page.clock.setFixedTime(GALLERY_TIME + 60_000)
tripwire = watchConsole(page)
await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
@@ -342,6 +349,7 @@ describe('web e2e: clickable links gallery', () => {
const markdown = page.locator('[class*="markdown"]')
await expect.poll(() => markdown.locator(`a[href="${GUIDE_URL}"]`).count(), { timeout: 10_000 }).toBe(1)
expect(await markdown.locator(`a[href="${RELEASES_URL}"]`).count()).toBe(1)
expect(await markdown.locator(`a[href="${HTTP_URL}"]`).count()).toBe(1)
expect(await markdown.locator(`a[href="${MAILTO_URL}"]`).count()).toBe(1)
const inlineCodeLink = markdown.locator(`code a[href="${API_URL}"]`)
expect(await inlineCodeLink.count()).toBe(1)
@@ -439,5 +447,12 @@ describe('web e2e: clickable links gallery', () => {
expect(await styleOf(mentions.first(), 'text-decoration-style')).toBe('dotted')
// The excluded grey affordance: tool-row file links keep their own color.
expect(await styleOf(page.locator('button[class*="fileLink"]').first(), 'color')).not.toBe(LINK_BLUE)
// Ordinary message HTTP(S) links delegate to the right Sidebar Browser.
await guideLink.click()
const browserAddress = page.locator('[data-rightbar-col]').getByRole('textbox', { name: 'Enter an HTTP(S) address' })
await expect.poll(() => browserAddress.inputValue()).toBe(GUIDE_URL)
await markdown.locator(`a[href="${HTTP_URL}"]`).click()
await expect.poll(() => browserAddress.inputValue()).toBe(HTTP_URL)
}, 90_000)
})
@@ -142,6 +142,11 @@
- text: ", and check the fine print"
- superscript: "1"
- text: .
- paragraph:
- text: "Preview:"
- link "plain HTTP":
- /url: http://docs.example.test/plain
- text: .
- paragraph:
- text: "Upstream:"
- link "the repository":
+1
View File
@@ -69,6 +69,7 @@
"@deepseek-ai/dsh-client-ui-renderer": "workspace:^",
"@deepseek-ai/dsh-client-ui-session": "workspace:^",
"@deepseek-ai/dsh-client-ui-settings": "workspace:^",
"@deepseek-ai/dsh-client-ui-sidebar-browser": "workspace:^",
"@deepseek-ai/dsh-client-ui-sidebar-documentpreview": "workspace:^",
"@deepseek-ai/dsh-client-ui-sidebar-right": "workspace:^",
"@deepseek-ai/dsh-client-ui-slots": "workspace:^",
@@ -6,6 +6,7 @@ import type { SessionBinding } from '@deepseek-ai/dsh-api-session-controller/cli
import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-store'
import type { SessionId } from '@deepseek-ai/dsh-session/types'
import type {} from '@deepseek-ai/dsh-client-ui-sidebar-right/client'
import type {} from '@deepseek-ai/dsh-client-ui-sidebar-browser/client'
import type {} from '@deepseek-ai/dsh-client-ui-input-trigger/client'
// The `file` entry of `SidebarRightResourceParamsMap`, which types `{ params: { line } }` below.
import type {} from '@deepseek-ai/dsh-client-ui-sidebar-documentpreview/client'
@@ -142,6 +143,9 @@ export function apply(ctx: Context): void {
if (scope === undefined) return
ctx.get('inputTriggers')?.sessionOf(scope).openReference('skill', { ref: `/${name}` })
},
openExternalLink: (url) => {
ctx.sidebarRight.openTab('browser', { params: { url } })
},
loadOlder: () => { void session.loadOlder() },
loadThrough: seq => session.loadThrough(seq),
loadImage: Object.assign(
@@ -7,7 +7,7 @@ import type {
} from '@deepseek-ai/dsh-client-ui-conversation/client'
import type { SessionSeq } from '@deepseek-ai/dsh-session/types'
import type { InboxState } from '@deepseek-ai/dsh-agent/types'
import { Button, IconChevronDownOutline14, Modal } from '@deepseek-ai/dsh-client-ui-primitives'
import { Button, IconChevronDownOutline14, MarkdownDelegateProvider, Modal } from '@deepseek-ai/dsh-client-ui-primitives'
import type { ChatViewSlotProps, OpenFileOptions } from '../contract/slots.ts'
import type { ChatSnapshot } from '../contract/snapshot.ts'
import { PendingSteeringBubble, PendingSubmissionBubble } from './MessageItem.tsx'
@@ -217,7 +217,7 @@ const ChatNodeList = memo(function ChatNodeList({ order, ...seatProps }: ChatNod
*/
export function ChatView({
useSession, useChat, useChatNode, useChatNodeProcess, useSessions, useStore, actions, renderSlot,
sessionId, openFile, openSkill, loadOlder, loadThrough, loadImage, openView, chatScroll, forkAt, fileMentions,
sessionId, openFile, openSkill, openExternalLink, loadOlder, loadThrough, loadImage, openView, chatScroll, forkAt, fileMentions,
useTranscriptView, useProjection, t,
}: ChatViewSlotProps) {
const order = useChat(s => s.order)
@@ -782,25 +782,27 @@ export function ChatView({
</button>
</div>
)}
<ChatNodeList
order={order}
useChatNode={useChatNode}
useChatNodeProcess={useChatNodeProcess}
historyIncomplete={hasMore}
compactTranscript={compactTranscript}
useStore={useStore}
actions={actions}
cwd={cwd}
openFile={requestOpenFile}
openSkill={openSkill}
inspectCall={inspectCall}
forkAt={forkAt}
loadImage={loadImage}
renderMessageImages={renderMessageImages}
fileMentions={fileMentions}
renderSlot={renderSlot}
t={t}
/>
<MarkdownDelegateProvider openExternalLink={openExternalLink}>
<ChatNodeList
order={order}
useChatNode={useChatNode}
useChatNodeProcess={useChatNodeProcess}
historyIncomplete={hasMore}
compactTranscript={compactTranscript}
useStore={useStore}
actions={actions}
cwd={cwd}
openFile={requestOpenFile}
openSkill={openSkill}
inspectCall={inspectCall}
forkAt={forkAt}
loadImage={loadImage}
renderMessageImages={renderMessageImages}
fileMentions={fileMentions}
renderSlot={renderSlot}
t={t}
/>
</MarkdownDelegateProvider>
{/* No pending placeholders: questions (ui-user-questions) and approvals
(ApprovalPanel) both take over the composer, so a flow card would
double-render the same wait. */}
@@ -142,6 +142,8 @@ export interface ChatViewInjected {
}
/** Open the current source file of a skill referenced by a sent message. */
openSkill: (name: string) => void
/** Open one HTTP(S) message link in a Sidebar Browser tab. */
openExternalLink: (url: string) => void
openFile: (path: string, options?: OpenFileOptions) => Promise<void>
loadOlder: () => void
/** Jump loader: page history back through seq; resolves when the window covers it. */
@@ -51,7 +51,10 @@ async function bench() {
runtime.ctx.provide('settingsScope', { bind: () => stubSettingsScope().scope } as never)
const layout = { closeRightbar: vi.fn(), openRightbar: vi.fn() }
runtime.ctx.provide('layout', layout as never)
const sidebarRight = { openResource: vi.fn<(address: string) => void>() }
const sidebarRight = {
openResource: vi.fn<(address: string) => void>(),
openTab: vi.fn<(kind: string, options?: unknown) => void>(),
}
runtime.ctx.provide('sidebarRight', sidebarRight as never)
const openWorkspacePath = vi.fn<ClientRemote['session']['openWorkspacePath']>(
() => Promise.resolve({ ok: true, value: { opened: true } }),
@@ -145,6 +148,18 @@ describe('Chat inject API', () => {
await b.runtime.dispose()
})
it('opens message HTTP(S) links in Sidebar Browser tabs', async () => {
const b = await bench()
const { injected } = b.chatViewApi(b.rootReference)
injected.openExternalLink('http://example.test/path')
injected.openExternalLink('https://example.test/path')
expect(b.sidebarRight.openTab.mock.calls).toEqual([
['browser', { params: { url: 'http://example.test/path' } }],
['browser', { params: { url: 'https://example.test/path' } }],
])
await b.runtime.dispose()
})
it('routes sent skill previews through the viewed Session source and tolerates an absent provider', async () => {
const b = await bench()
const { injected } = b.chatViewApi(b.rootReference)
@@ -198,6 +213,9 @@ describe('Chat inject API', () => {
const { injected } = b.chatViewApi(b.rootReference)
const owner = {} as never
expect(injected.keyedHooks.chatNode('missing')).toBeDefined()
expect(injected.keyedHooks.chatNodeProcess('missing')).toBeDefined()
expect(injected.fileMentions(owner)).toBeUndefined()
const mentions = { resolve: vi.fn() } as never
const forClosing = vi.fn(() => mentions)
@@ -43,7 +43,7 @@ async function bench() {
: stubSettingsScope().scope,
} as never)
runtime.ctx.provide('layout', { openRightbar: vi.fn(), closeRightbar: vi.fn() } as never)
runtime.ctx.provide('sidebarRight', { openResource: vi.fn() } as never)
runtime.ctx.provide('sidebarRight', { openResource: vi.fn(), openTab: vi.fn() } as never)
const openSession = vi.fn<(id: SessionId) => void>()
runtime.ctx.provide('uiWorkspace', {
openWorkspace: vi.fn(async (_workspaceId: WorkspaceId, beforeOpen: (id: SessionId) => void) => {
@@ -408,6 +408,7 @@ function makeHarness(
completeViewRequest: () => {},
openFile,
openSkill,
openExternalLink: vi.fn(),
loadOlder,
loadThrough,
loadImage: vi.fn(() => Promise.reject(new Error('not used'))),
+3
View File
@@ -86,6 +86,9 @@
{
"path": "../ui-session"
},
{
"path": "../ui-sidebar-browser"
},
{
"path": "../ui-sidebar-right"
},
@@ -68,6 +68,8 @@ export type {
export { CodeBlock } from './markdown/CodeBlock.tsx'
export type { CodeBlockProps } from './markdown/CodeBlock.tsx'
export { JsonBlock } from './markdown/JsonBlock.tsx'
export { MarkdownDelegateProvider } from './markdown/MarkdownDelegate.tsx'
export type { MarkdownDelegateProviderProps, MarkdownExternalLinkHandler } from './markdown/MarkdownDelegate.tsx'
export { MarkdownText } from './markdown/MarkdownText.tsx'
export type { MarkdownCodeLabels, MarkdownFileMentions, MarkdownLabels, MarkdownPathImages } from './markdown/MarkdownText.tsx'
export { extractMarkdownPlainText } from './markdown/plain-text.ts'
@@ -0,0 +1,38 @@
/** Consumer-owned navigation for ordinary Markdown HTTP(S) link activation. */
import { createContext, useContext } from 'react'
import type { ReactNode } from 'react'
/**
* Handle one sanitized absolute HTTP(S) URL selected from Markdown.
* @param href - destination URL.
*/
export type MarkdownExternalLinkHandler = (href: string) => void
const MarkdownDelegateContext = createContext<MarkdownExternalLinkHandler | undefined>(undefined)
/** Props for one Markdown navigation scope. */
export interface MarkdownDelegateProviderProps {
readonly children: ReactNode
readonly openExternalLink: MarkdownExternalLinkHandler
}
/**
* Delegate ordinary Markdown HTTP(S) activation without threading callbacks through renderers.
* @param props - child tree and its link handler.
* @returns the scoped child tree.
*/
export function MarkdownDelegateProvider({
children,
openExternalLink,
}: MarkdownDelegateProviderProps): ReactNode {
return (
<MarkdownDelegateContext.Provider value={openExternalLink}>
{children}
</MarkdownDelegateContext.Provider>
)
}
/** Read the nearest optional Markdown HTTP(S) navigation delegate. */
export function useMarkdownExternalLinkDelegate(): MarkdownExternalLinkHandler | undefined {
return useContext(MarkdownDelegateContext)
}
@@ -157,12 +157,14 @@ class StreamingRenderer {
* identity discards the streaming render cache mid-message. `fileMentions`
* links inline-code tokens its resolver recognizes as real files, and
* `pathImages` rewrites image destinations that are local file paths into
* displayable URLs its resolver vouches for; both vocabularies are the
* displayable URLs its resolver vouches for. Those two vocabularies are the
* single streaming gate — they apply to settled renders only, because a
* streaming message's vocabulary is not final and frozen cached elements
* must not bake in handlers that could go stale. `variant="compact"` uses
* secondary text sizing, uniform bold headings, and tight block spacing;
* the default `body` variant uses the full document typography.
* must not bake in handlers that could go stale. A surrounding
* `MarkdownDelegateProvider` can delegate ordinary HTTP(S) activation while
* modified clicks retain native behavior. `variant="compact"` uses secondary
* text sizing, uniform bold headings, and tight block spacing; the default
* `body` variant uses the full document typography.
* @returns A GFM document with TeX math rendered through KaTeX; raw HTML,
* relative links, and unsafe protocols are disabled, while absolute HTTP(S)
* images render directly.
@@ -25,6 +25,7 @@ import { normalizeUri } from 'micromark-util-sanitize-uri'
import { CodeBlock } from './CodeBlock.tsx'
import { renderTexToReact } from './katex.tsx'
import { LinkIcon, classifyLinkPath } from '../LinkIcon.tsx'
import { useMarkdownExternalLinkDelegate } from './MarkdownDelegate.tsx'
import type { PositionedBlock } from './incremental.ts'
import css from './MarkdownText.module.css'
@@ -525,18 +526,32 @@ function anchorWrapsOnlyImages(children: Md.PhrasingContent[]): boolean {
return children.length > 0 && children.every(child => child.type === 'image' || child.type === 'imageReference')
}
/** Anchor over an already-authored href: allowlisted or unwrapped, external links get the safe attributes. */
/** Anchor over an already-authored href: allowlisted or unwrapped, with optional owner navigation for HTTP(S). */
function renderSafeLink(href: string, children: ReactNode[], key: Key, glyph = true): ReactNode {
const safeHref = sanitizeUrl(href)
if (safeHref === '') return <Fragment key={key}>{children}</Fragment>
const external = ['http:', 'https:'].includes(new URL(safeHref).protocol)
return <MarkdownAnchor key={key} href={safeHref} glyph={glyph}>{children}</MarkdownAnchor>
}
function MarkdownAnchor({ href, glyph, children }: {
readonly href: string
readonly glyph: boolean
readonly children: ReactNode[]
}): ReactNode {
const openExternalLink = useMarkdownExternalLinkDelegate()
const external = ['http:', 'https:'].includes(new URL(href).protocol)
const open = external ? openExternalLink : undefined
return (
<a
key={key}
href={safeHref}
href={href}
{...(external ? { target: '_blank', rel: 'noopener noreferrer' } : {})}
onClick={open === undefined ? undefined : (event) => {
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
event.preventDefault()
open(href)
}}
>
{glyph && <LinkIcon kind="url" href={safeHref} className={css.linkIcon} />}
{glyph && <LinkIcon kind="url" href={href} className={css.linkIcon} />}
{children}
</a>
)
@@ -1,8 +1,8 @@
// @vitest-environment jsdom
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { JsonBlock, MarkdownText } from './markdown-test-components.tsx'
import { LinkIcon } from '../src/index.ts'
import { LinkIcon, MarkdownDelegateProvider } from '../src/index.ts'
import { cjkFriendlyStrong } from '../src/markdown/cjkFriendlyStrong.ts'
import { mathCompatibility } from '../src/markdown/mathCompatibility.ts'
@@ -141,6 +141,57 @@ describe('MarkdownText', () => {
expect(container.querySelector('pre code a')).toBeNull()
})
it('delegates ordinary HTTP(S) clicks while preserving modified-click behavior', () => {
const openExternalLink = vi.fn<(href: string) => void>()
render(
<MarkdownDelegateProvider openExternalLink={openExternalLink}>
<MarkdownText text={'[secure](https://example.com/a) [plain](http://example.com/b) `https://example.com/code` [mail](mailto:[email protected])'} />
</MarkdownDelegateProvider>,
)
const secure = screen.getByRole('link', { name: 'secure' })
const plain = screen.getByRole('link', { name: 'plain' })
const code = screen.getByRole('link', { name: 'https://example.com/code' })
expect(fireEvent.click(secure)).toBe(false)
expect(fireEvent.click(plain)).toBe(false)
expect(fireEvent.click(code)).toBe(false)
expect(openExternalLink.mock.calls).toEqual([
['https://example.com/a'],
['http://example.com/b'],
['https://example.com/code'],
])
for (const modified of [
{ button: 1 },
{ metaKey: true },
{ ctrlKey: true },
{ shiftKey: true },
{ altKey: true },
]) expect(fireEvent.click(secure, modified)).toBe(true)
expect(openExternalLink).toHaveBeenCalledTimes(3)
expect(screen.getByRole('link', { name: 'mail' }).getAttribute('target')).toBeNull()
})
it('updates the delegated link handler while Markdown is streaming', () => {
const first = vi.fn<(href: string) => void>()
const second = vi.fn<(href: string) => void>()
const source = '[web](https://example.com/)'
const view = render(
<MarkdownDelegateProvider openExternalLink={first}>
<MarkdownText text={source} streaming />
</MarkdownDelegateProvider>,
)
fireEvent.click(screen.getByRole('link', { name: 'web' }))
view.rerender(
<MarkdownDelegateProvider openExternalLink={second}>
<MarkdownText text={source} streaming />
</MarkdownDelegateProvider>,
)
fireEvent.click(screen.getByRole('link', { name: 'web' }))
expect(first).toHaveBeenCalledOnce()
expect(second).toHaveBeenCalledOnce()
})
it('links inline code through the file-mention resolver: URL first, settled only, never inside links', () => {
const opened: string[] = []
const fileMentions = {
@@ -176,7 +176,7 @@ export const CLIENT_SLOT_API: readonly ClientSlotEntry[] = [
],
replaceRisk: 'none',
example: 'return {\n inject: [\'slots\'],\n apply(ctx) {\n ctx.slots.inject(\'conversation.chat.assistant-actions\', () => ctx.slots.register(\n { name: \'conversation.chat.assistant-actions\', id: \'my-entry\', order: 100, label: \'My entry\' },\n () => React.createElement(\'div\', null, \'hello\'),\n ))\n },\n}',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:217',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:219',
},
{
key: 'conversation.chat.commandview',
@@ -224,7 +224,7 @@ export const CLIENT_SLOT_API: readonly ClientSlotEntry[] = [
occupants: [],
replaceRisk: 'none',
example: 'return {\n inject: [\'slots\'],\n apply(ctx) {\n ctx.slots.inject(\'conversation.chat.commandview\', () => ctx.slots.register(\n { name: \'conversation.chat.commandview\', key: \'<one key the owner dispatches>\' },\n () => React.createElement(\'div\', null, \'hello\'),\n ))\n },\n}',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:205',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:207',
},
{
key: 'conversation.chat.node',
@@ -293,7 +293,7 @@ export const CLIENT_SLOT_API: readonly ClientSlotEntry[] = [
],
replaceRisk: 'shadows-shipped-ui',
example: 'return {\n inject: [\'slots\'],\n apply(ctx) {\n ctx.slots.inject(\'conversation.chat.node\', () => ctx.slots.register(\n { name: \'conversation.chat.node\', key: \'<one key the owner dispatches>\' },\n () => React.createElement(\'div\', null, \'hello\'),\n ))\n },\n}',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:186',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:188',
},
{
key: 'conversation.chat.turnTail',
@@ -341,7 +341,7 @@ export const CLIENT_SLOT_API: readonly ClientSlotEntry[] = [
],
replaceRisk: 'none',
example: 'return {\n inject: [\'slots\'],\n apply(ctx) {\n ctx.slots.inject(\'conversation.chat.turnTail\', () => ctx.slots.register(\n { name: \'conversation.chat.turnTail\', select: owner => null },\n () => React.createElement(\'div\', null, \'hello\'),\n ))\n },\n}',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:211',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:213',
},
{
key: 'conversation.composer',
@@ -1049,7 +1049,7 @@ export const CLIENT_SLOT_API: readonly ClientSlotEntry[] = [
],
replaceRisk: 'shadows-shipped-ui',
example: 'return {\n inject: [\'slots\'],\n apply(ctx) {\n ctx.slots.inject(\'conversation.message.images\', () => ctx.slots.register(\n { name: \'conversation.message.images\' },\n () => React.createElement(\'div\', null, \'hello\'),\n ))\n },\n}',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:199',
source: 'packages/client/ui-chat/src/client/contract/slots.ts:201',
},
{
key: 'conversation.session',
+3
View File
@@ -2763,6 +2763,9 @@ importers:
'@deepseek-ai/dsh-client-ui-settings':
specifier: workspace:^
version: link:../ui-settings
'@deepseek-ai/dsh-client-ui-sidebar-browser':
specifier: workspace:^
version: link:../ui-sidebar-browser
'@deepseek-ai/dsh-client-ui-sidebar-documentpreview':
specifier: workspace:^
version: link:../ui-sidebar-documentpreview