diff --git a/src/client/chat-mentions.ts b/src/client/chat-mentions.ts index 59ef92bc..4d5eccd3 100644 --- a/src/client/chat-mentions.ts +++ b/src/client/chat-mentions.ts @@ -6,20 +6,28 @@ interface ChatFileMentions { forClosing(owner: { openFile: (path: string) => void }, sessionId: string): MarkdownFileMentions | undefined } -/** 判定内联代码是否为完整的绝对 Markdown 文件路径。 */ -export function isAbsoluteMarkdownPath(value: string): boolean { +/** 根据会话工作目录判定绝对 Markdown 路径,并拒绝宿主会改写的 POSIX 路径。 */ +export function isAbsoluteMarkdownPath(value: string, cwd?: 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) + const windowsWorkspace = cwd !== undefined && (/^[A-Za-z]:[\\/]/.test(cwd) + || /^\\\\[^\\/]+[\\/][^\\/]+(?:[\\/]|$)/.test(cwd)) + if (value.startsWith('/')) return windowsWorkspace || !value.includes('\\') + return windowsWorkspace && (/^[A-Za-z]:[\\/]/.test(value) || /^\\\\[^\\/]+[\\/][^\\/]+[\\/]/.test(value)) } /** 在宿主文件认领结果之后追加绝对 Markdown 路径入口。 */ -export function markdownMentionsFor(owner: { openFile: (path: string) => void }, stock: MarkdownFileMentions | undefined): MarkdownFileMentions { +export function markdownMentionsFor( + owner: { openFile: (path: string) => void }, + stock: MarkdownFileMentions | undefined, + cwd?: string, + isActive: () => boolean = () => true, +): 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 } + if (!isAbsoluteMarkdownPath(value, cwd)) return undefined + return { open: () => { if (isActive()) owner.openFile(value) }, label: t('openFileSide') + ': ' + value, title: value } }, } } @@ -32,7 +40,9 @@ export function registerChatMarkdownMentions(ctx: Context): () => Promise let active = true const wrapped: ChatFileMentions['forClosing'] = (owner, sessionId) => { const stock = original.call(service, owner, sessionId) - return active ? markdownMentionsFor(owner, stock) : stock + if (!active) return stock + const cwd = ctx.sessions.list.getSnapshot().byId[sessionId]?.cwd + return markdownMentionsFor(owner, stock, cwd, () => active) } service.forClosing = wrapped return () => { diff --git a/tests/chat-mentions.spec.tsx b/tests/chat-mentions.spec.tsx index 71e974b7..b34823a5 100644 --- a/tests/chat-mentions.spec.tsx +++ b/tests/chat-mentions.spec.tsx @@ -26,11 +26,14 @@ afterEach(async () => { 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 path of ['/Users/x/notes/a.md', '/notes/README.MARKDOWN', '//server/share/notes/a.md']) { + expect(isAbsoluteMarkdownPath(path, '/workspace'), 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) + for (const path of ['C:\\notes\\a.md', 'D:/notes/a.MarkDown', '\\\\server\\share\\notes\\a.md']) { + expect(isAbsoluteMarkdownPath(path, 'C:\\workspace'), 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', 'C:\\notes\\a.md', '\\\\server\\share\\notes\\a.md', '/notes/we\\ird.md']) { + expect(isAbsoluteMarkdownPath(value, '/workspace'), value).toBe(false) } }) @@ -50,6 +53,7 @@ describe('absolute Markdown chat mentions', () => { it('registers after the real Cordis service arrives and restores its method on release', async () => { const context = new CordisContext() + disposers.push(context.provide('sessions', { list: { getSnapshot: () => ({ byId: {} }) } } as never)) const disposeMentions = registerChatMarkdownMentions(context as unknown as Context) disposers.push(disposeMentions) expect(context.get('chatFileMentions')).toBeUndefined() @@ -67,12 +71,15 @@ describe('absolute Markdown chat mentions', () => { 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() + const existingLink = mentions?.resolve(path) + existingLink?.open() expect(opened).toEqual([path]) await disposeMentions() expect(service.forClosing).toBe(originalForClosing) expect(service.forClosing(owner, 's1')?.resolve(path)).toBeUndefined() + existingLink?.open() + expect(opened).toEqual([path]) }) it('stops extending results when a later host wrapper remains installed', async () => {