* fix(workspace-path): address opened files by absolute path `fileAddressFor` rewrote an absolute path inside the Session's directory into a relative address. A Sidebar tab opened that way named another file, or none, once the Session's directory moved and the page reloaded. The address now carries the absolute path whenever one is known: an absolute path is kept and a relative path is joined to the directory the caller supplies. A relative path stays relative only while that directory is unknown. * fix(ui-chat): resolve relative images from the Session's current directory A relative Markdown image used the Session header directory, so an image written while the Session worked in a worktree did not load. It now uses the directory the Session works in, the base relative file links already use. * fix(ui-reference): write picked and dropped files as absolute mentions A file or folder picked from the `@` list was sent as a path relative to the Session's directory, and a Desktop drop inside that directory was rewritten to a relative path. After the directory moved, the sent message named another file or none. A settled pick now joins the listed path to the Session's current directory, and a dropped or pasted file keeps the path the shell reports. Drill text stays relative. Hand-typed relative mentions are unchanged. * fix(deliverables): ask for absolute Markdown link and image targets The file-reference guidance allowed targets relative to the working directory. A relative target in an earlier reply named another file once the Session's directory moved, and a model working in a worktree sometimes wrote targets relative to the original project. The guidance now asks for absolute targets. The expected system prompts that assemble this section carry the new wording. * docs(notes): record how conversation file references survive directory moves * docs(notes): cite the earlier per-message implementation and its revert * test(snapshots): carry the absolute-target guidance into the session-send-message prompt * test(snapshots): add the subagent concurrency sentence to the timeline-follow-chat prompt The scenario's expected system prompt on master predates the subagent guidance sentence that states the shared concurrency limit, so its replay fails on master and on every branch that merges it. * docs(ui-conversation): remove the stale sentence on relative dropped paths --------- Co-authored-by: imccyu <[email protected]>
121 lines
5.6 KiB
TypeScript
121 lines
5.6 KiB
TypeScript
/**
|
|
* Browser-safe Workspace path and display helpers.
|
|
* @module @deepseek-ai/dsh-util-workspace-path
|
|
*/
|
|
import { sessionFileAddress } from './file-address.ts'
|
|
|
|
/** Whether a path uses a Windows drive or UNC prefix. */
|
|
function isWindowsStylePath(value: string): boolean {
|
|
return /^[A-Za-z]:[/\\]/.test(value) || value.startsWith('\\\\')
|
|
}
|
|
|
|
/**
|
|
* Whether a path is absolute in either spelling the Host accepts: POSIX (`/a/b`) or Windows drive or UNC.
|
|
* @param path - the path to classify.
|
|
* @returns `true` for an absolute path; `false` for a Workspace-relative one.
|
|
*/
|
|
export function isAbsoluteWorkspacePath(path: string): boolean {
|
|
return path.startsWith('/') || isWindowsStylePath(path)
|
|
}
|
|
|
|
/**
|
|
* Resolve a Workspace-relative path into the Host-facing spelling used by path operations.
|
|
* @param cwd - Session Workspace root, when known.
|
|
* @param path - Absolute or Workspace-relative path; leading `./` segments are dropped when joining.
|
|
* @returns an absolute path when a Workspace root is available, otherwise the original path.
|
|
*/
|
|
export function resolveWorkspacePath(cwd: string | undefined, path: string): string {
|
|
if (isAbsoluteWorkspacePath(path)) return path
|
|
if (cwd === undefined || cwd === '') return path
|
|
const separator = isWindowsStylePath(cwd) && cwd.includes('\\') ? '\\' : '/'
|
|
const base = cwd.replace(/[/\\]+$/, '')
|
|
const relative = path.replace(/^(?:\.?[/\\])+/, '')
|
|
return `${base}${separator}${relative}`
|
|
}
|
|
|
|
/**
|
|
* Abbreviate a POSIX home directory for display.
|
|
* @param path - Absolute or already-short display path.
|
|
* @param home - Host account home; absent skips abbreviation.
|
|
* @returns `~` or `~/…` for the POSIX home and its descendants, otherwise `path`.
|
|
*/
|
|
export function abbreviateHomePath(path: string, home?: string): string {
|
|
if (home === undefined || home === '') return path
|
|
if (isWindowsStylePath(path) || isWindowsStylePath(home)) return path
|
|
const root = home.replace(/\/+$/, '')
|
|
if (root === '' || root === '/') return path
|
|
if (path.replace(/\/+$/, '') === root) return '~'
|
|
if (path.startsWith(`${root}/`)) return `~${path.slice(root.length)}`
|
|
return path
|
|
}
|
|
|
|
/**
|
|
* Read the final non-empty segment of a Workspace path for display.
|
|
* Workspace-label surfaces use this helper instead of deriving another basename.
|
|
* @param path - Workspace directory path using POSIX or Windows separators.
|
|
* @returns the final segment, or an empty string for a separator-only path.
|
|
*/
|
|
export function workspaceTitleOf(path: string): string {
|
|
const trimmed = path.replace(/[/\\]+$/, '')
|
|
const separator = Math.max(trimmed.lastIndexOf('/'), trimmed.lastIndexOf('\\'))
|
|
return trimmed.slice(separator + 1)
|
|
}
|
|
|
|
/**
|
|
* Split a path for display: the directories through their last separator, and
|
|
* the final segment after it. Both `/` and `\` separate, so a Windows path
|
|
* splits where its own segments end; trailing separators are dropped first, so
|
|
* a directory path names its own last segment. A path with no separator, or a
|
|
* separator-only path, is all name.
|
|
* @param path - file or directory path using POSIX or Windows separators.
|
|
* @returns the directory prefix (possibly empty) and the final segment.
|
|
*/
|
|
export function pathPartsOf(path: string): { readonly directory: string; readonly name: string } {
|
|
const trimmed = path.replace(/[/\\]+$/, '')
|
|
if (trimmed === '') return { directory: '', name: path }
|
|
const cut = Math.max(trimmed.lastIndexOf('/'), trimmed.lastIndexOf('\\')) + 1
|
|
return { directory: trimmed.slice(0, cut), name: trimmed.slice(cut) }
|
|
}
|
|
|
|
export * from './file-address.ts'
|
|
|
|
/**
|
|
* The address of a file as a caller holds its path. The address carries the
|
|
* absolute path whenever one is known, so it names the same file after the
|
|
* Session's directory moves: an absolute path is kept, and a relative path is
|
|
* joined to `cwd`. A relative path without `cwd` stays relative, and the Host
|
|
* resolves it against the Session's directory at each read.
|
|
* @param sessionId - the Session the path is read in.
|
|
* @param cwd - the directory that Session works in, when known.
|
|
* @param path - absolute path or path relative to `cwd`, in either separator spelling.
|
|
* @returns the `dsh-resource://file/session/…` address.
|
|
*/
|
|
export function fileAddressFor(sessionId: string, cwd: string | undefined, path: string): string {
|
|
return sessionFileAddress(sessionId, resolveWorkspacePath(cwd, path))
|
|
}
|
|
|
|
/**
|
|
* Strip the workspace root from a workspace-rooted absolute path (display only).
|
|
* @param text - the path to shorten.
|
|
* @param cwd - session workspace root; absent or empty leaves the path unchanged.
|
|
* @returns the path relative to the workspace root, or unchanged when it is not rooted there.
|
|
*/
|
|
export function relativizeToCwd(text: string, cwd: string | undefined): string {
|
|
if (cwd === undefined || cwd === '') return text
|
|
const root = cwd.replace(/[/\\]+$/, '')
|
|
if (text.startsWith(`${root}/`) || text.startsWith(`${root}\\`)) return text.slice(root.length + 1)
|
|
return text
|
|
}
|
|
|
|
/**
|
|
* Address a decoded absolute file path through the authenticated file route.
|
|
* @param base - HTTP(S) application base, including its deployment prefix, or `dsh-app://app/`.
|
|
* @param path - Native file path; URL escapes in authored Markdown must already be decoded.
|
|
* @returns File URL, or undefined for unsupported transports and non-absolute paths.
|
|
*/
|
|
export function fileMediaUrl(base: string, path: string): string | undefined {
|
|
if ((!/^https?:/u.test(base) && !base.startsWith('dsh-app://app/')) || !isAbsoluteWorkspacePath(path)
|
|
|| /^[/\\]{2}/u.test(path) || /[\u0000-\u001f\u007f]/u.test(path)) return undefined
|
|
return new URL(`api/file?path=${encodeURIComponent(path)}`, base).href
|
|
}
|