/** Web and Desktop share HTTP routes; Desktop exposes its Host URL for streams. */ export function hostWebSocketUrl( path: string, surface: { href: string; protocol: string; streamBaseUrl?: string } = { href: location.href, protocol: location.protocol, streamBaseUrl: (globalThis as any).__DSH_TRANSPORT__?.streamBaseUrl, }, ): string { const desktop = surface.protocol === 'dsh-app:' if (desktop && !surface.streamBaseUrl) throw new Error('Desktop Host stream URL unavailable') const url = new URL(path, desktop ? surface.streamBaseUrl : surface.href) if (url.protocol !== 'http:' && url.protocol !== 'https:') { throw new Error('Unsupported Host transport protocol: ' + url.protocol) } url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' return url.href } /** Desktop preview assets use the Host HTTP origin with a read-only capability. */ export function hostPreviewUrl( path: string, surface: { protocol: string; streamBaseUrl?: string } = { protocol: location.protocol, streamBaseUrl: (globalThis as any).__DSH_TRANSPORT__?.streamBaseUrl, }, ): string { if (!path.startsWith('/api/worktable/preview-static/')) throw new Error('Invalid preview path') if (surface.protocol !== 'dsh-app:') return path if (!surface.streamBaseUrl) throw new Error('Desktop Host preview URL unavailable') const url = new URL(path, surface.streamBaseUrl) if (url.protocol !== 'http:' && url.protocol !== 'https:') throw new Error('Unsupported Host preview protocol') // Opaque sandbox requests cannot pass Desktop's dsh-app Origin fence. This // read-only capability needs no Host cookie, so its assets use the Host origin. return url.href } /** Keep update instructions on the active profile; Desktop must use its bundled CLI. */ // 本仓发布在自建 Gitea:它不支持 GitHub 的 /releases/latest/download 简写, // 安装命令必须带具体 tag。tag 由调用方传入(等于检查到的新版本), // 不再写死当前版本——否则「有新版本」提示里给的却是自己这一版的地址。 const UPDATE_BASE = 'https://gitea.ququclean.com/xiaoqu/dsh-worktable/releases/download' export function upgradeCommandFor(tag: string, protocol = typeof location === 'undefined' ? 'http:' : location.protocol): string { const desktop = protocol === 'dsh-app:' const clean = String(tag || '').trim().replace(/^v/, '') const t = clean ? 'v' + clean : '' const url = t ? UPDATE_BASE + '/' + t + '/dsh-worktable.tgz' : UPDATE_BASE + '/' return 'dsh plugin --profile ' + (desktop ? 'desktop' : 'web') + ' add "' + url + '"' } export function worktableUpgrade(tag: string, protocol = typeof location === 'undefined' ? 'http:' : location.protocol) { const desktop = protocol === 'dsh-app:' const command = upgradeCommandFor(tag, protocol) const prompt = desktop ? '帮我升级 dsh-worktable。我当前使用官方桌面端:先定位该桌面端自带的 dsh CLI 并准备更新,再提醒我完整退出桌面端。确认退出后使用该 CLI 执行 ' + command + '。不要使用网页端 CLI,不要修改 web profile,不要自动杀进程或重启;完成后提醒我手动重新打开桌面端。' : '帮我升级 dsh-worktable:执行 ' + command + ',完成后提醒我重启 dsh web 并刷新页面' return { command, prompt, desktop } }