cc-haha/desktop/src/lib/openWithContextForHref.ts
程序员阿江(Relakkes) 0035e7df3f feat(desktop): add open-with menu on previewable links in completed AI messages
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 17:40:45 +08:00

26 lines
1.1 KiB
TypeScript

import { classifyPreviewLink } from './previewLinkRouter'
import { previewFsUrl } from './handlePreviewLink'
import type { OpenWithContext } from './openWithItems'
function resolveAbsolute(workDir: string | undefined, p: string): string {
if (!workDir || p.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(p)) return p
return `${workDir.replace(/[\\/]+$/, '')}/${p.replace(/^[/\\]+/, '')}`
}
export function openWithContextForHref(
href: string,
opts: { sessionId: string; serverBaseUrl: string; workDir?: string },
): OpenWithContext | null {
const c = classifyPreviewLink(href)
if ((c.kind === 'browser-localhost' || c.kind === 'remote') && c.url) {
return { kind: 'url', url: c.url }
}
if (c.kind === 'file-preview' && c.path) {
return { kind: 'file', absolutePath: resolveAbsolute(opts.workDir, c.path), relPath: c.path, previewable: true }
}
if (c.kind === 'browser-file' && c.path) {
return { kind: 'file', absolutePath: resolveAbsolute(opts.workDir, c.path), inAppBrowserUrl: previewFsUrl(opts.serverBaseUrl, opts.sessionId, c.path) }
}
return null
}