The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 220 lines 6.82 KB
Raw
1 import type { SnapShotSource } from "@t3tools/contracts";
2
3 import type { ComposerFileAttachment } from "../../composerDraftStore";
4 import { type ChatFileAttachment, type ChatImageAttachment, isVideoAttachment } from "../../types";
5 import type {
6 AssetCreateUrlResult,
7 AssetResource,
8 EnvironmentId,
9 ScopedThreadRef,
10 } from "@t3tools/contracts";
11 import { videoMimeType } from "@t3tools/shared/video";
12 import { msg } from "gt-react";
13 import { m } from "~/i18n/messages";
14 import { resolveMediaSource } from "@t3tools/client-runtime/media-source";
15 import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
16 import {
17 squashAtomCommandFailure,
18 type AtomCommandResult,
19 } from "@t3tools/client-runtime/state/runtime";
20 import { resolveExternalWebLinkHost } from "./externalLinkContextMenu";
21 import type { MediaActionSource } from "../media/MediaActions";
22 import { resolveProtocolRelativeMediaUrl } from "../media/mediaContent";
23
24 export interface ExpandedImageItem {
25 src: string | null;
26 name: string;
27 mimeType?: string;
28 type?: "video";
29 /** Revoke this owned object URL; other blob URLs belong to the timeline. */
30 ownsSrc?: boolean;
31 source?: SnapShotSource;
32 onCopySnapshot?: (() => Promise<void>) | undefined;
33 autoPlay?: boolean;
34 originalUrl?: string;
35 srcFragment?: string;
36 actionsSource?: MediaActionSource;
37 }
38
39 export interface ExpandedImagePreview {
40 images: ExpandedImageItem[];
41 index: number;
42 }
43
44 export async function resolveMarkdownMediaPreview(input: {
45 source: string;
46 resolvedFilePath?: string | undefined;
47 cwd?: string | undefined;
48 threadRef?: ScopedThreadRef | undefined;
49 httpBaseUrl?: string | undefined;
50 onOpenFile?: ((relativePath: string) => void) | undefined;
51 createAssetUrl: (input: {
52 environmentId: EnvironmentId;
53 input: { resource: AssetResource };
54 }) => Promise<AtomCommandResult<AssetCreateUrlResult, unknown>>;
55 }): Promise<ExpandedImagePreview | null> {
56 const media = resolveMediaSource(input.source, {
57 threadId: input.threadRef?.threadId,
58 workspaceRoot: input.cwd,
59 resolvedFilePath: input.resolvedFilePath,
60 });
61 if (media === null) return null;
62 const { kind, name, reference } = media;
63 const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
64
65 let src: string;
66 let asset: MediaActionSource["asset"];
67 if (media.access === "direct") {
68 src = resolveProtocolRelativeMediaUrl(media.uri);
69 } else {
70 if (media.access === "unavailable" || !input.threadRef || !input.httpBaseUrl) {
71 throw new Error(m(msg("Reconnect to this environment and open the media again.")));
72 }
73 asset = { environmentId: input.threadRef.environmentId, resource: media.resource };
74 const result = await input.createAssetUrl({
75 environmentId: asset.environmentId,
76 input: { resource: asset.resource },
77 });
78 if (result._tag === "Failure") throw squashAtomCommandFailure(result);
79 const assetUrl = resolveAssetUrl(input.httpBaseUrl, result.value.relativeUrl);
80 if (assetUrl === null) {
81 throw new Error(m(msg("The environment returned an invalid media URL.")));
82 }
83 src = assetUrl + media.srcFragment;
84 }
85 return {
86 images: [
87 {
88 src,
89 name,
90 ...(kind === "video" ? { type: "video", autoPlay: false } : {}),
91 ...(media.access === "direct" && resolveExternalWebLinkHost(media.uri) !== null
92 ? { originalUrl: media.uri }
93 : {}),
94 ...(media.srcFragment ? { srcFragment: media.srcFragment } : {}),
95 actionsSource: {
96 kind,
97 name,
98 src,
99 ...(reference ? { reference } : {}),
100 ...(asset ? { asset } : {}),
101 ...(relativePath && input.onOpenFile
102 ? { onOpenFile: () => input.onOpenFile?.(relativePath) }
103 : {}),
104 },
105 },
106 ],
107 index: 0,
108 };
109 }
110
111 export function buildAttachmentVideoAsset(
112 environmentId: EnvironmentId,
113 attachment: Pick<ChatFileAttachment, "id" | "name" | "mimeType">,
114 ): NonNullable<MediaActionSource["asset"]> {
115 return {
116 environmentId,
117 resource: {
118 _tag: "attachment" as const,
119 attachmentId: attachment.id,
120 fileName: attachment.name,
121 mimeType: videoMimeType(attachment) ?? attachment.mimeType,
122 },
123 };
124 }
125
126 export function buildAttachmentVideoPreview(
127 environmentId: EnvironmentId,
128 attachment: ChatFileAttachment,
129 ): ExpandedImagePreview | null {
130 if (!isVideoAttachment(attachment)) return null;
131 const src = attachment.previewUrl ?? null;
132 const asset =
133 attachment.downloadable === false
134 ? undefined
135 : buildAttachmentVideoAsset(environmentId, attachment);
136 if (src === null && asset === undefined) return null;
137 return {
138 images: [
139 {
140 src,
141 name: attachment.name,
142 type: "video",
143 actionsSource: {
144 kind: "video",
145 name: attachment.name,
146 src,
147 ...(asset ? { asset } : {}),
148 },
149 },
150 ],
151 index: 0,
152 };
153 }
154
155 export function expandedImageKey(preview: ExpandedImagePreview): string {
156 const item = preview.images[preview.index];
157 const asset = item?.actionsSource?.asset;
158 return `${item?.src ?? (asset ? JSON.stringify([asset.environmentId, asset.resource]) : "image")}:${preview.index}`;
159 }
160
161 export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () => void {
162 const url = URL.createObjectURL(file);
163 video.src = url;
164 return () => URL.revokeObjectURL(url);
165 }
166
167 export function buildExpandedImagePreview(
168 images: ReadonlyArray<ChatImageAttachment | ComposerFileAttachment>,
169 selectedImageId: string,
170 onCopySnapshot?: (id: string) => Promise<void>,
171 ): ExpandedImagePreview | null {
172 const selected = images.find((image) => image.id === selectedImageId);
173 if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) {
174 return {
175 images: [
176 {
177 src: URL.createObjectURL(selected.file),
178 name: selected.name,
179 type: "video",
180 ownsSrc: true,
181 },
182 ],
183 index: 0,
184 };
185 }
186 const previewableImages = images.flatMap((image) =>
187 image.type === "image" && image.previewUrl
188 ? [
189 {
190 id: image.id,
191 src: image.previewUrl,
192 name: image.name,
193 mimeType: image.mimeType,
194 source: image.source,
195 },
196 ]
197 : [],
198 );
199 if (previewableImages.length === 0) {
200 return null;
201 }
202 const selectedIndex = previewableImages.findIndex((image) => image.id === selectedImageId);
203 if (selectedIndex < 0) {
204 return null;
205 }
206 return {
207 images: previewableImages.map((image) => ({
208 src: image.src,
209 name: image.name,
210 mimeType: image.mimeType,
211 ...(image.source?.kind === "snap-shot"
212 ? {
213 source: image.source,
214 onCopySnapshot: onCopySnapshot ? () => onCopySnapshot(image.id) : undefined,
215 }
216 : {}),
217 })),
218 index: selectedIndex,
219 };
220 }