| import { |
| CWD_CHANGED_PREFIX, |
| CWD_CLEARED_TEXT, |
| CWD_LINK_REGEX, |
| FILE_URI_PREFIX, |
| HOME_TILDE, |
| HOME_TILDE_PREFIX |
| } from '$lib/constants'; |
| import { PATH_SEPARATOR } from '$lib/constants/mcp-resource'; |
| import { TRAILING_SLASHES_REGEX } from '$lib/constants/url'; |
|
|
| export function lastPathSegment(p: string): string { |
| const trimmed = p.replace(TRAILING_SLASHES_REGEX, ''); |
| const idx = trimmed.lastIndexOf(PATH_SEPARATOR); |
|
|
| return idx === -1 ? trimmed : trimmed.slice(idx + 1); |
| } |
|
|
| |
| |
| export function abbreviateWorkingDir( |
| path: string | null | undefined, |
| home: string | null | undefined |
| ): string { |
| if (!path) return ''; |
|
|
| if (!home) return lastPathSegment(path); |
|
|
| if (path === home) return HOME_TILDE; |
|
|
| if (path.startsWith(home + PATH_SEPARATOR)) |
| return HOME_TILDE_PREFIX + path.slice(home.length + 1); |
|
|
| return lastPathSegment(path); |
| } |
|
|
| |
| |
| export function abbreviateHome(path: string, home: string | null | undefined): string { |
| if (!home) return path; |
|
|
| if (path === home) return HOME_TILDE; |
|
|
| if (path.startsWith(home + PATH_SEPARATOR)) |
| return HOME_TILDE_PREFIX + path.slice(home.length + 1); |
|
|
| return path; |
| } |
|
|
| export { CWD_CHANGED_PREFIX, CWD_CLEARED_TEXT } from '$lib/constants'; |
|
|
| export interface CwdMessageInfo { |
| |
| path: string | null; |
| |
| display: string; |
| } |
|
|
| |
| |
| |
| |
| |
| export function formatCwdMessage(cwd: string, home: string | null): string { |
| const display = abbreviateWorkingDir(cwd, home); |
|
|
| return `${CWD_CHANGED_PREFIX}[${FILE_URI_PREFIX}${cwd}](${display}).`; |
| } |
|
|
| |
| |
| |
| |
| |
| export function parseCwdMessage(content: string): CwdMessageInfo | null { |
| const trimmed = content.trim(); |
|
|
| if (trimmed === CWD_CLEARED_TEXT) { |
| return { display: '', path: null }; |
| } |
|
|
| if (trimmed.startsWith(CWD_CHANGED_PREFIX)) { |
| const rest = trimmed.slice(CWD_CHANGED_PREFIX.length); |
| |
| const link = rest.match(CWD_LINK_REGEX); |
|
|
| if (link) return { display: link[2], path: link[1] }; |
|
|
| return { display: rest, path: rest }; |
| } |
|
|
| return null; |
| } |
|
|