@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 03:37 UTC

main
ts 112 lines 3.64 KB
Raw
1 import type { AssetResource, ThreadId } from "@t3tools/contracts";
2 import {
3 audioMimeTypeFromExtension,
4 mediaMimeType,
5 mediaMimeTypeFromExtension,
6 } from "@t3tools/shared/mediaPreview";
7
8 import {
9 classifyMarkdownImageSource,
10 markdownImageSourceFragment,
11 type MarkdownImageSource,
12 } from "./markdownImages.ts";
13 import { fileBasename } from "@t3tools/shared/path";
14 import { splitFilePathPosition } from "@t3tools/shared/fileLinks";
15 import { splitMarkdownLinkSearchAndHash } from "@t3tools/shared/markdownLinks";
16 import {
17 mediaFileReference,
18 mediaReferenceFileName,
19 mediaUrlReference,
20 type MediaReference,
21 } from "./mediaReference.ts";
22
23 export type MediaSourceResource = Extract<AssetResource, { readonly _tag: "media-file" }>;
24
25 export type ResolvedMediaSource = {
26 readonly kind: "image" | "video";
27 readonly mimeType: string;
28 readonly name: string;
29 readonly reference?: MediaReference;
30 readonly srcFragment: string;
31 } & (
32 | {
33 readonly access: "direct";
34 readonly uri: string;
35 }
36 | {
37 readonly access: "environment";
38 readonly resource: MediaSourceResource;
39 }
40 | {
41 readonly access: "unavailable";
42 }
43 );
44
45 export interface ResolveMediaSourceInput {
46 readonly threadId: ThreadId | undefined;
47 readonly workspaceRoot?: string | null | undefined;
48 readonly resolvedFilePath?: string | undefined;
49 /** Image syntax can target an endpoint without a recognizable extension. */
50 readonly imageEmbed?: boolean | undefined;
51 }
52
53 function classify(source: string, input: ResolveMediaSourceInput): MarkdownImageSource {
54 return input.resolvedFilePath === undefined
55 ? classifyMarkdownImageSource(source, input.workspaceRoot)
56 : { _tag: "WorkspaceFile", path: input.resolvedFilePath };
57 }
58
59 export function resolveMediaSource(
60 source: string,
61 input: ResolveMediaSourceInput,
62 ): ResolvedMediaSource | null {
63 const classified = classify(source, input);
64 if (classified._tag === "Blocked") return null;
65
66 const path =
67 classified._tag === "Direct"
68 ? splitMarkdownLinkSearchAndHash(classified.uri).path
69 : splitFilePathPosition(classified.path).path;
70 // Local paths have already been decoded. Do not interpret literal #, ?, or % characters again.
71 // A data URI carries no file name; its last path segment is the payload.
72 const basename = /^data:/i.test(path) ? "" : fileBasename(path);
73 const extensionIndex = basename.lastIndexOf(".");
74 const detectedMimeType =
75 classified._tag === "Direct"
76 ? mediaMimeType(classified.uri)
77 : extensionIndex < 0
78 ? null
79 : (mediaMimeTypeFromExtension(basename.slice(extensionIndex)) ??
80 audioMimeTypeFromExtension(basename.slice(extensionIndex)));
81 const mimeType = detectedMimeType ?? (input.imageEmbed ? "image/*" : null);
82 if (mimeType === null || mimeType.startsWith("audio/")) return null;
83 const kind = mimeType.startsWith("video/") ? "video" : "image";
84
85 const reference =
86 classified._tag === "Direct"
87 ? mediaUrlReference(classified.uri)
88 : mediaFileReference(path, input.workspaceRoot);
89 const name = (reference && mediaReferenceFileName(reference)) || basename || kind;
90 const common = {
91 kind,
92 mimeType,
93 name,
94 ...(reference ? { reference } : {}),
95 srcFragment: markdownImageSourceFragment(source),
96 } as const;
97
98 if (classified._tag === "Direct") {
99 return { ...common, access: "direct", uri: classified.uri };
100 }
101 if (input.threadId === undefined) return { ...common, access: "unavailable" };
102 return {
103 ...common,
104 access: "environment",
105 resource: {
106 _tag: "media-file",
107 threadId: input.threadId,
108 path,
109 ...(classified.relative ? { hostOnly: true as const } : {}),
110 },
111 };
112 }