fix: 支持点击聊天中的绝对 Markdown 路径

This commit is contained in:
kelvinwww authored and Menghuan1918 committed 2026-11-16 00:32:56 +08:00
1 parent 251f257c37
commit 47bcd230d0
5 files changed
+177

No files matched your search

+2
View File
@@ -28,6 +28,8 @@
**Also**: 📂 the `sidebar_open` tool lets the model open the sidebar itself · ⚡ ~325KB core at startup, heavy chunks load on demand · 🌏 i18n follows DSH · 📱 narrow screens switch to a full-width drawer
- **🔗 Markdown paths in chat**: complete absolute `.md` / `.markdown` paths in inline code are clickable in each turn's final assistant message and open through the current session's file address in the native sidebar; missing files show a read error.
### 🌿 Git Graph (read-only commit graph)
Switch to **Git Graph** at the top of the Git panel for commit nodes, forks and merge edges across local branches, remote-tracking branches and HEAD. The branch selector only filters history — it **never checks out or fetches**; each commit is one line (lane edges + ref pills + subject), hover shows the author and short hash, and selecting one opens its full message and diff in the bottom pane. History loads in pages; reference changes during pagination prompt a refresh; reads are bounded (200 commits per page, offset 10,000, 1,000 branches) and report an explicit error past a limit. Lightweight and annotated tags appear on commits in the graph, rows and details use neutral tag pills, and tag changes invalidate pagination. Tag decorations keep the first 5,000 tags by ref name, further bounded by an 8 MiB complete-line output prefix; excess tags are omitted without failing the graph.
+2
View File
@@ -28,6 +28,8 @@
**还有**:📂 `sidebar_open` 工具让模型主动打开侧边栏 · ⚡ 启动只拉 ~325KB 核心、重依赖按需加载 · 🌏 多语言跟随 DSH · 📱 窄屏自动切换全宽抽屉
- **🔗 聊天中的 Markdown 文件路径**:每轮最后一条助手消息里,完整的绝对 `.md` / `.markdown` 内联代码路径可点击,并按当前会话的文件地址交给原生右侧栏打开;路径不存在时显示文件读取错误。
### 🌿 Git Graph(只读提交图)
在 Git 面板顶部切到 **Git Graph**:本地分支、远程跟踪分支与 HEAD 的提交节点、分叉与合并连线。分支下拉框只筛选历史,**不会 checkout 或 fetch**;提交行是单行(泳道连线 + 引用徽章 + 标题),hover 看作者与短 hash,选中后在底部面板看完整提交信息与 diff。历史按页加载,分页期间 refs 变化会提示刷新;读取有上限(每页 200 条、偏移 10,000、分支 1,000),超限明确报错。图中提交支持 lightweight 与 annotated tag,提交行与详情通过中性 tag pill 标识,tag 变化会使旧分页失效。Tag 装饰按引用名保留前 5,000 条,并受 8 MiB 输出的完整行前缀限制;多余 tag 省略,不影响整张图加载。
+44
View File
@@ -0,0 +1,44 @@
import type { MarkdownFileMentions } from '@deepseek-ai/dsh-client-ui-primitives'
import type { Context } from '../context-types.ts'
import { t } from './locales.ts'
interface ChatFileMentions {
forClosing(owner: { openFile: (path: string) => void }, sessionId: string): MarkdownFileMentions | undefined
}
/** 判定内联代码是否为完整的绝对 Markdown 文件路径。 */
export function isAbsoluteMarkdownPath(value: string): boolean {
if (!/\.(?:md|markdown)$/i.test(value) || /[\s\p{Cc}]/u.test(value)) return false
return value.startsWith('/') || /^[A-Za-z]:[\\/]/.test(value) || /^\\\\[^\\/]+[\\/][^\\/]+[\\/]/.test(value)
}
/** 在宿主文件认领结果之后追加绝对 Markdown 路径入口。 */
export function markdownMentionsFor(owner: { openFile: (path: string) => void }, stock: MarkdownFileMentions | undefined): MarkdownFileMentions {
return {
resolve(value) {
const existing = stock?.resolve(value)
if (existing !== undefined) return existing
if (!isAbsoluteMarkdownPath(value)) return undefined
return { open: () => { owner.openFile(value) }, label: t('openFileSide') + ': ' + value, title: value }
},
}
}
/** 随宿主服务生命周期注册聊天 Markdown 路径,并在释放后停止扩展认领结果。 */
export function registerChatMarkdownMentions(ctx: Context): () => Promise<void> {
const seat = ctx.inject(['chatFileMentions'], injected => {
const service = injected.get('chatFileMentions') as ChatFileMentions
const original = service.forClosing
let active = true
const wrapped: ChatFileMentions['forClosing'] = (owner, sessionId) => {
const stock = original.call(service, owner, sessionId)
return active ? markdownMentionsFor(owner, stock) : stock
}
service.forClosing = wrapped
return () => {
active = false
if (service.forClosing === wrapped) service.forClosing = original
}
})
return () => seat.dispose()
}
+2
View File
@@ -28,6 +28,7 @@ import { isTargetAvailable, openInterceptedLink, registerLinkInterception, shoul
import { registerImeGuard } from './ime-guard.ts'
import { registerQuoteSource } from './selection-quote.ts'
import { IconSideCardNav } from './icons.tsx'
import { registerChatMarkdownMentions } from './chat-mentions.ts'
import type { SettingsSectionIconOwnerProps } from '@deepseek-ai/dsh-client-ui-settings/client'
import { loadBootDecision } from './prefs.ts'
import { SideCardSection } from './SideCardSection.tsx'
@@ -175,6 +176,7 @@ export function apply(ctx: Context): void {
const service = createBetterSidebarService(sidebarStore)
ctx.provide('betterSidebar', service)
ctx.effect(() => () => { disposeSidechatSurfaces(service) }, 'dsh-better-sidebar: sidechat surface adapters')
ctx.effect(() => registerChatMarkdownMentions(ctx), 'dsh-better-sidebar: chat Markdown mentions')
// The native right-Sidebar surface: the plugin's content is registered as
// DSH tab types (one per descriptor) and every open routes there, so the
// right column belongs to the host and only the bottom workbench stays
+127
View File
@@ -0,0 +1,127 @@
// @vitest-environment jsdom
import { afterEach, describe, expect, it } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { Context as CordisContext } from '@deepseek-ai/cordis'
import { MarkdownText, type MarkdownFileMentions } from '@deepseek-ai/dsh-client-ui-primitives'
import type { Context } from '../src/context-types.ts'
import { isAbsoluteMarkdownPath, markdownMentionsFor, registerChatMarkdownMentions } from '../src/client/chat-mentions.ts'
import { markdownTextProps } from '../src/client/markdown-labels.tsx'
import { attachLocale } from '../src/client/locales.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
const labels = { copyLabel: 'Copy', copiedLabel: 'Copied', codeLabel: 'Code', wrapLabel: 'Wrap', unwrapLabel: 'Unwrap' }
const roots: Root[] = []
const disposers: Array<() => void | Promise<void>> = []
afterEach(async () => {
for (const root of roots.splice(0)) await act(async () => { root.unmount() })
for (const dispose of disposers.splice(0).reverse()) await dispose()
document.body.innerHTML = ''
attachLocale(undefined)
})
describe('absolute Markdown chat mentions', () => {
it('recognizes complete absolute Markdown paths on supported platforms', () => {
for (const path of ['/Users/x/notes/a.md', '/notes/README.MARKDOWN', 'C:\\notes\\a.md', 'D:/notes/a.MarkDown', '\\\\server\\share\\notes\\a.md', '//server/share/notes/a.md']) {
expect(isAbsoluteMarkdownPath(path), path).toBe(true)
}
for (const value of ['notes/a.md', 'a.md', '@notes/a.md', '/notes/a.txt', '/notes/a.md?raw=1', 'https://example.com/a.md', '/notes/a b.md', '/notes/a.md\n', 'C:notes\\a.md', '\\\\server\\share.md']) {
expect(isAbsoluteMarkdownPath(value), value).toBe(false)
}
})
it('keeps a stock match and routes a new absolute path through the chat opener', () => {
const opened: string[] = []
const stockMatch = { open: () => { opened.push('stock') }, label: 'Stock file', title: '/stock/a.md' }
const stock: MarkdownFileMentions = { resolve: value => value === '/stock/a.md' ? stockMatch : undefined }
const mentions = markdownMentionsFor({ openFile: path => { opened.push(path) } }, stock)
expect(mentions.resolve('/stock/a.md')).toBe(stockMatch)
mentions.resolve('/stock/a.md')?.open()
const path = '/Users/x/notes/a.md'
expect(mentions.resolve(path)?.title).toBe(path)
mentions.resolve(path)?.open()
expect(mentions.resolve('notes/a.md')).toBeUndefined()
expect(opened).toEqual(['stock', path])
})
it('registers after the real Cordis service arrives and restores its method on release', async () => {
const context = new CordisContext()
const disposeMentions = registerChatMarkdownMentions(context as unknown as Context)
disposers.push(disposeMentions)
expect(context.get('chatFileMentions')).toBeUndefined()
const opened: string[] = []
const stockMatch = { open: () => { opened.push('stock') }, label: 'Stock file', title: '/stock/a.md' }
const stock: MarkdownFileMentions = { resolve: value => value === '/stock/a.md' ? stockMatch : undefined }
const originalForClosing = (_owner: { openFile: (path: string) => void }, _sessionId: string): MarkdownFileMentions => stock
const service = { forClosing: originalForClosing }
disposers.push(context.provide('chatFileMentions', service))
await new Promise<void>(resolve => setTimeout(resolve, 0))
expect(service.forClosing).not.toBe(originalForClosing)
const owner = { openFile: (path: string) => { opened.push(path) } }
const mentions = service.forClosing(owner, 's1')
expect(mentions?.resolve('/stock/a.md')).toBe(stockMatch)
const path = '/Users/x/notes/a.md'
mentions?.resolve(path)?.open()
expect(opened).toEqual([path])
await disposeMentions()
expect(service.forClosing).toBe(originalForClosing)
expect(service.forClosing(owner, 's1')?.resolve(path)).toBeUndefined()
})
it('stops extending results when a later host wrapper remains installed', async () => {
const context = new CordisContext()
const disposeMentions = registerChatMarkdownMentions(context as unknown as Context)
disposers.push(disposeMentions)
const stock: MarkdownFileMentions = { resolve: () => undefined }
const originalForClosing = (_owner: { openFile: (path: string) => void }, _sessionId: string): MarkdownFileMentions => stock
const service = { forClosing: originalForClosing }
disposers.push(context.provide('chatFileMentions', service))
await new Promise<void>(resolve => setTimeout(resolve, 0))
const installedWrapper = service.forClosing
let laterWrapperCalls = 0
service.forClosing = function (owner, sessionId) {
laterWrapperCalls++
return installedWrapper.call(service, owner, sessionId)
}
await disposeMentions()
const owner = { openFile: (_path: string) => {} }
const mentions = service.forClosing(owner, 's1')
expect(laterWrapperCalls).toBe(1)
expect(mentions).toBe(stock)
expect(mentions?.resolve('/Users/x/notes/a.md')).toBeUndefined()
})
it('renders only a claimed inline-code path as a keyboard-accessible button', async () => {
const opened: string[] = []
const container = document.createElement('div')
document.body.appendChild(container)
const root = createRoot(container)
roots.push(root)
const path = '/Users/x/notes/a.md'
const tick = String.fromCharCode(96)
await act(async () => {
root.render(createElement(MarkdownText, {
...markdownTextProps(tick + path + tick + ' and ' + tick + 'notes/a.md' + tick, labels),
fileMentions: markdownMentionsFor({ openFile: value => { opened.push(value) } }, undefined),
}))
})
const button = container.querySelector('code button')
expect(button?.getAttribute('type')).toBe('button')
expect(button?.getAttribute('title')).toBe(path)
expect(button?.getAttribute('aria-label')).toContain(path)
expect(container.querySelectorAll('code button')).toHaveLength(1)
expect(container.querySelectorAll('code')).toHaveLength(2)
await act(async () => { (button as HTMLButtonElement).click() })
expect(opened).toEqual([path])
})
})