The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { AssetResource, ProjectReadFileError, ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { mediaFileReference } from "@t3tools/client-runtime/media-reference"; |
| 3 | import { isHostPath, workspaceRelativeFilePath } from "@t3tools/shared/path"; |
| 4 | import { msg } from "gt-react"; |
| 5 | |
| 6 | import { m, type MessageResolver } from "~/i18n/messages"; |
| 7 | |
| 8 | export function resolveFilePreviewPath(path: string | null, cwd: string): string | null { |
| 9 | if (path === null) return null; |
| 10 | return path === "." || workspaceRelativeFilePath(path, cwd) === "." ? null : path; |
| 11 | } |
| 12 | |
| 13 | export const isMarkdownPreviewFile = (path: string): boolean => |
| 14 | /\.(?:md|mdx|markdown|mdown|mkd)$/i.test(path); |
| 15 | |
| 16 | export function filePreviewReadErrorMessage( |
| 17 | error: ProjectReadFileError, |
| 18 | resolve: MessageResolver = m, |
| 19 | ): string { |
| 20 | switch (error.failure) { |
| 21 | case "path_not_file": |
| 22 | return resolve(msg("The path is a directory or special file, not a regular file.")); |
| 23 | case "binary_file": |
| 24 | return resolve(msg("The file is binary and cannot be displayed as text.")); |
| 25 | case "workspace_path_outside_root": |
| 26 | return resolve(msg("The requested path is outside the workspace.")); |
| 27 | case "resolved_path_outside_root": |
| 28 | return resolve(msg("The path resolves to a location outside the workspace.")); |
| 29 | case "operation_failed": |
| 30 | return error.operation === "realpath-workspace-root" |
| 31 | ? resolve(msg("The workspace folder could not be accessed.")) |
| 32 | : resolve( |
| 33 | msg("The file could not be accessed or read. It may be missing or inaccessible."), |
| 34 | ); |
| 35 | default: |
| 36 | return error.message; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | export function shouldShowFileExplorer(input: { |
| 41 | readonly relativePath: string | null; |
| 42 | readonly explorerOpen: boolean; |
| 43 | readonly attachmentOpen: boolean; |
| 44 | }): boolean { |
| 45 | if (input.attachmentOpen || (input.relativePath && isHostPath(input.relativePath))) { |
| 46 | return false; |
| 47 | } |
| 48 | return input.explorerOpen || input.relativePath === null; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * The server only signs `workspace-file` assets below the workspace root, so a |
| 53 | * file opened from outside it, such as an absolute link in the thread, goes |
| 54 | * through the host `media-file` resource the direct media viewer uses. |
| 55 | */ |
| 56 | export function workspaceAssetResource(input: { |
| 57 | readonly kind: "workspace-file" | "media-file"; |
| 58 | readonly threadRef: ScopedThreadRef; |
| 59 | readonly draft: boolean; |
| 60 | readonly workspaceRoot: string; |
| 61 | readonly absolutePath: string; |
| 62 | }): AssetResource { |
| 63 | if (input.draft) { |
| 64 | return { _tag: "draft-workspace-file", cwd: input.workspaceRoot, path: input.absolutePath }; |
| 65 | } |
| 66 | const insideWorkspace = |
| 67 | mediaFileReference(input.absolutePath, input.workspaceRoot).relativePath !== undefined; |
| 68 | return { |
| 69 | _tag: insideWorkspace ? input.kind : "media-file", |
| 70 | threadId: input.threadRef.threadId, |
| 71 | path: input.absolutePath, |
| 72 | }; |
| 73 | } |