Skip to content

Commit 49a7b61

Browse files
committed
feat(web): play audio inline and open shared files in the viewer
1 parent b727408 commit 49a7b61

14 files changed

Lines changed: 294 additions & 28 deletions

File tree

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ describe("AssetAccess", () => {
266266
["recording.webm", "video/webm"],
267267
["report.html", "text/html"],
268268
["report.pdf", "application/pdf"],
269+
["tone.wav", "audio/wav"],
270+
["tone.webm-audio", "audio/webm"],
269271
] as const) {
270272
const filePath = path.join(outside, name);
271273
yield* fs.writeFileString(filePath, "media");
@@ -290,6 +292,65 @@ describe("AssetAccess", () => {
290292
}).pipe(Effect.provide(layerTest)),
291293
);
292294

295+
it.effect("downloads any linked file with the same exact-file identity checks as media", () =>
296+
Effect.gen(function* () {
297+
const fs = yield* FileSystem.FileSystem;
298+
const path = yield* Path.Path;
299+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-download-root-" });
300+
const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-download-outside-" });
301+
for (const name of [
302+
"data.csv",
303+
"report.docx",
304+
"sheet.xlsx",
305+
"slides.pptx",
306+
"archive.zip",
307+
"no-extension",
308+
]) {
309+
for (const directory of [root, outside]) {
310+
const filePath = path.join(directory, name);
311+
yield* fs.writeFileString(filePath, "shared file bytes");
312+
const resource = {
313+
_tag: "media-file" as const,
314+
threadId: ThreadId.make("thread-1"),
315+
path: directory === root ? name : filePath,
316+
disposition: "attachment" as const,
317+
};
318+
const result = yield* issueAssetUrl({ resource, workspaceRoot: root });
319+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
320+
const separator = suffix.indexOf("/");
321+
const token = suffix.slice(0, separator);
322+
const asset = yield* resolveAsset(token, suffix.slice(separator + 1));
323+
expect(asset).toMatchObject({
324+
kind: "file",
325+
download: true,
326+
fileName: name,
327+
mimeType: "application/octet-stream",
328+
});
329+
if (asset?.kind !== "file") throw new Error("Expected a downloadable file");
330+
const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset));
331+
expect(response.headers.get("content-disposition")).toContain("attachment;");
332+
expect(response.headers.get("content-type")).toBe("application/octet-stream");
333+
expect(yield* Effect.promise(() => response.text())).toBe("shared file bytes");
334+
expect(yield* resolveAsset(token, "sibling.docx")).toBeNull();
335+
expect(yield* resolveAsset(token, `../${name}`)).toBeNull();
336+
expect(yield* resolveAsset(`${token}tampered`, name)).toBeNull();
337+
yield* fs.rename(filePath, `${filePath}.original`);
338+
yield* fs.writeFileString(filePath, "replacement bytes");
339+
expect(yield* resolveAsset(token, name)).toBeNull();
340+
}
341+
}
342+
const directoryError = yield* issueAssetUrl({
343+
resource: {
344+
_tag: "media-file",
345+
threadId: ThreadId.make("thread-1"),
346+
path: root,
347+
disposition: "attachment",
348+
},
349+
}).pipe(Effect.flip);
350+
expect(directoryError._tag).toBe("AssetWorkspaceAssetNotFoundError");
351+
}).pipe(Effect.provide(layerTest)),
352+
);
353+
293354
it.effect("reports pixel dimensions from an image header and nothing for other files", () =>
294355
Effect.gen(function* () {
295356
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const AssetClaimsSchema = Schema.Union([
115115
version: Schema.Literal(1),
116116
kind: Schema.Literal("media-file-exact"),
117117
filePath: Schema.String,
118+
download: Schema.optionalKey(Schema.Boolean),
118119
device: Schema.String,
119120
inode: Schema.String,
120121
expiresAt: Schema.Number,
@@ -361,7 +362,9 @@ const finalizeAbsoluteMediaFileAsset = Effect.fn("AssetAccess.finalizeAbsoluteMe
361362
if (!canonicalFile) {
362363
return yield* new AssetWorkspaceAssetNotFoundError({ resource: input.resource });
363364
}
364-
if (hostPreviewMimeTypeFromExtension(path.extname(canonicalFile)) === null) {
365+
const download =
366+
input.resource._tag === "media-file" && input.resource.disposition === "attachment";
367+
if (!download && hostPreviewMimeTypeFromExtension(path.extname(canonicalFile)) === null) {
365368
return yield* new AssetPreviewTypeValidationError({ resource: input.resource });
366369
}
367370
const wantsDimensions = HEADER_IMAGE_EXTENSIONS.has(path.extname(canonicalFile).toLowerCase());
@@ -392,6 +395,7 @@ const finalizeAbsoluteMediaFileAsset = Effect.fn("AssetAccess.finalizeAbsoluteMe
392395
version: 1 as const,
393396
kind: "media-file-exact" as const,
394397
filePath: canonicalFile,
398+
...(download ? { download: true } : {}),
395399
...opened.identity,
396400
expiresAt: input.expiresAt,
397401
},
@@ -931,7 +935,9 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
931935
Effect.orElseSucceed(() => null),
932936
);
933937
if (canonicalFile !== claims.filePath) return null;
934-
const mimeType = hostPreviewMimeTypeFromExtension(path.extname(canonicalFile));
938+
const mimeType = claims.download
939+
? "application/octet-stream"
940+
: hostPreviewMimeTypeFromExtension(path.extname(canonicalFile));
935941
if (!mimeType) return null;
936942
const file = yield* openMediaFile(canonicalFile, claims).pipe(
937943
Effect.tapError((cause) =>
@@ -940,7 +946,13 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
940946
Effect.orElseSucceed(() => null),
941947
);
942948
return file
943-
? ({ kind: "file", path: canonicalFile, mimeType, file } satisfies ResolvedAsset)
949+
? ({
950+
kind: "file",
951+
path: canonicalFile,
952+
mimeType,
953+
file,
954+
...(claims.download ? { download: true, fileName: path.basename(canonicalFile) } : {}),
955+
} satisfies ResolvedAsset)
944956
: null;
945957
}
946958
if (claims.kind === "workspace-file-exact") {

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/media-reference";
7676
import { MISSING_FILE_ACCESS_MESSAGE } from "@t3tools/client-runtime/state/assets";
7777
import { mediaKindFromPath, mediaMimeTypeFromExtension } from "@t3tools/shared/filePreview";
78+
import { InlineAudioPreview } from "./files/InlineAudioPreview";
7879
import * as Cause from "effect/Cause";
7980
import * as Option from "effect/Option";
8081
import { AsyncResult, Atom } from "effect/reactivity";
@@ -2931,6 +2932,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
29312932
href &&
29322933
faviconHost !== null &&
29332934
mediaKindFromPath(href) !== null &&
2935+
mediaKindFromPath(href) !== "audio" &&
29342936
!event.defaultPrevented &&
29352937
!event.metaKey &&
29362938
!event.ctrlKey &&
@@ -3137,6 +3139,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
31373139
imageBaseDir,
31383140
threadRef,
31393141
renderContextReference,
3142+
fileLinkChip,
31403143
} = use(ChatMarkdownRendererContext);
31413144
const imageExpand = use(MarkdownLinkContext) ? undefined : expandMedia;
31423145
const contextReference = typeof src === "string" ? parseComposerContextHref(src) : null;
@@ -3162,7 +3165,39 @@ const CHAT_MARKDOWN_COMPONENTS = {
31623165
const { className, style: _style, width, height, ...imageProps } = props;
31633166
const authoredSizeStyle = authoredImageSizeStyle(width, height);
31643167
const imageSource = classifyMarkdownImageSource(classifiedSrc, imageBaseDir ?? cwd);
3165-
const kind = mediaKindFromPath(classifiedSrc) ?? "image";
3168+
const mediaKind = mediaKindFromPath(classifiedSrc);
3169+
if (mediaKind === "audio") {
3170+
const filePath = imageSource._tag === "WorkspaceFile" ? imageSource.path : null;
3171+
const name =
3172+
(filePath ?? classifiedSrc.split(/[?#]/, 1)[0] ?? "").split(/[\\/]/).at(-1) ||
3173+
altText ||
3174+
"Audio";
3175+
return (
3176+
<InlineAudioPreview
3177+
key={classifiedSrc}
3178+
name={name}
3179+
copyMarkdown={copyMarkdown}
3180+
{...(filePath && threadRef
3181+
? {
3182+
environmentId: threadRef.environmentId,
3183+
resource: {
3184+
_tag: "media-file" as const,
3185+
threadId: threadRef.threadId,
3186+
path: filePath,
3187+
},
3188+
onOpen: () => useRightPanelStore.getState().openFile(threadRef, filePath),
3189+
}
3190+
: imageSource._tag === "Direct"
3191+
? { src: resolveProtocolRelativeMediaUrl(imageSource.uri) }
3192+
: {})}
3193+
/>
3194+
);
3195+
}
3196+
if (mediaKind === null && imageSource._tag === "WorkspaceFile") {
3197+
const fileLinkMeta = resolveMarkdownFileLinkMeta(srcString, cwd, imageBaseDir ?? cwd);
3198+
if (fileLinkMeta) return fileLinkChip(fileLinkMeta, copyMarkdown, srcString);
3199+
}
3200+
const kind = mediaKind ?? "image";
31663201
const directUri = imageSource._tag === "Direct" ? imageSource.uri : null;
31673202
const githubMediaUrl =
31683203
directUri === null ? null : githubMediaFetchUrl(resolveProtocolRelativeMediaUrl(directUri));

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { WorkLogBlock, WorkLogButton, WorkLogDetails, WorkLogList, WorkLogRow }
1010
import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
1111
import type { WorktreeSetupSnapshot } from "@t3tools/contracts";
1212
import { ReadOnlySourcePreview } from "../files/AttachmentFilePreview";
13+
import { InlineAudioPreview } from "../files/InlineAudioPreview";
14+
import { filePreviewKind } from "@t3tools/shared/filePreview";
1315
import { useRightPanelStore } from "~/rightPanelStore";
1416
import {
1517
getQuestionAnswerPreview,
@@ -2124,7 +2126,12 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
21242126
const userImages = (messageWithPreviews.attachments ?? []).filter(isImageAttachment);
21252127
const userFiles = (row.message.attachments ?? []).filter(isFileAttachment);
21262128
const userVideos = userFiles.filter(isVideoAttachment);
2127-
const otherUserFiles = userFiles.filter((file) => !isVideoAttachment(file));
2129+
const userAudio = userFiles.filter(
2130+
(file) => file.downloadable !== false && filePreviewKind(file) === "audio",
2131+
);
2132+
const otherUserFiles = userFiles.filter(
2133+
(file) => !isVideoAttachment(file) && !userAudio.includes(file),
2134+
);
21282135
const unknownAttachments = (row.message.attachments ?? []).filter(
21292136
(attachment) => !isImageAttachment(attachment) && !isFileAttachment(attachment),
21302137
);
@@ -2195,6 +2202,8 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
21952202
};
21962203
const renderContextReference = (reference: ChatMarkdownContextReference) => {
21972204
const record = asKnownContextRecord(resolvedContext.recordsById.get(reference.contextId));
2205+
if (record?.kind === "file" && userAudio.some((file) => file.id === record.attachmentId))
2206+
return null;
21982207
const annotationImage =
21992208
record?.kind === "preview-annotation"
22002209
? resolvePreviewAnnotationImage({
@@ -2327,6 +2336,25 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
23272336
))}
23282337
</div>
23292338
)}
2339+
{userAudio.length > 0 ? (
2340+
<div className="mb-2 flex max-w-full flex-col gap-1">
2341+
{userAudio.map((file) => (
2342+
<InlineAudioPreview
2343+
key={file.id}
2344+
name={file.name}
2345+
environmentId={ctx.activeThreadEnvironmentId}
2346+
resource={{
2347+
_tag: "attachment",
2348+
attachmentId: file.id,
2349+
fileName: file.name,
2350+
mimeType: file.mimeType,
2351+
disposition: "inline",
2352+
}}
2353+
onOpen={() => ctx.onFileOpen(file)}
2354+
/>
2355+
))}
2356+
</div>
2357+
) : null}
23302358
{unchippedFiles.length > 0 || unknownAttachments.length > 0 ? (
23312359
<div className="mb-2 flex flex-col gap-1">
23322360
{unchippedFiles.map((file) => {

‎apps/web/src/components/files/AttachmentFilePreview.tsx‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { filePreviewDelimiter } from "@t3tools/shared/delimitedPreview";
2-
import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
2+
import type { AssetResource, EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
33
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
44
import { readFilePreviewResponse } from "@t3tools/client-runtime/file-preview";
55
import { settlePromise } from "@t3tools/client-runtime/state/runtime";
@@ -53,7 +53,9 @@ export function AttachmentFilePreview(props: {
5353
mimeType: string;
5454
sizeBytes: number;
5555
file?: Blob | null;
56-
asset?: { environmentId: EnvironmentId; attachmentId: string };
56+
asset?:
57+
| { environmentId: EnvironmentId; attachmentId: string }
58+
| { environmentId: EnvironmentId; resource: Extract<AssetResource, { _tag: "media-file" }> };
5759
htmlRender?: boolean;
5860
threadRef?: ScopedThreadRef;
5961
origin?: string;
@@ -64,18 +66,22 @@ export function AttachmentFilePreview(props: {
6466
const delimiter = filePreviewDelimiter(props);
6567
const renderedMode =
6668
kind === "markdown" ? "markdown" : kind === "html" ? "html" : delimiter ? "table" : null;
67-
const attachmentId = props.asset?.attachmentId;
69+
const attachmentId =
70+
props.asset && "attachmentId" in props.asset ? props.asset.attachmentId : undefined;
6871
const { copyToClipboard, isCopied } = useCopyToClipboard({ target: props.name });
6972
// useAssetUrlRefresh keys its callback on these objects, so they must stay cached.
70-
const resource = attachmentId
71-
? {
72-
_tag: "attachment" as const,
73-
attachmentId,
74-
fileName: props.name,
75-
mimeType: props.mimeType,
76-
disposition: "inline" as const,
77-
}
78-
: null;
73+
const resource =
74+
props.asset && "resource" in props.asset
75+
? props.asset.resource
76+
: attachmentId
77+
? {
78+
_tag: "attachment" as const,
79+
attachmentId,
80+
fileName: props.name,
81+
mimeType: props.mimeType,
82+
disposition: "inline" as const,
83+
}
84+
: null;
7985
const downloadResource = resource ? { ...resource, disposition: "attachment" as const } : null;
8086
const refresh = useAssetUrlRefresh(
8187
props.file ? null : (props.asset?.environmentId ?? null),

‎apps/web/src/components/files/AudioPreview.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ export function AudioPreview(props: {
22
readonly src: string;
33
readonly name: string;
44
readonly onError?: () => void;
5+
readonly compact?: boolean;
56
}) {
7+
const Container = props.compact ? "span" : "div";
68
return (
7-
<div className="flex min-h-0 flex-1 items-center justify-center p-6">
9+
<Container
10+
className={
11+
props.compact
12+
? "flex min-h-0 flex-1 items-center px-2 pb-2"
13+
: "flex min-h-0 flex-1 items-center justify-center p-6"
14+
}
15+
>
816
<audio
917
controls
1018
preload="metadata"
1119
src={props.src}
1220
aria-label={props.name}
13-
className="w-full max-w-xl"
21+
className={props.compact ? "h-10 w-full min-w-0" : "w-full max-w-xl"}
1422
onError={props.onError}
1523
/>
16-
</div>
24+
</Container>
1725
);
1826
}

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,23 @@ export default function FilePreviewPanel({
13481348
title={relativePath}
13491349
workspaceMutationId={workspaceMutationId}
13501350
/>
1351+
) : relativePath && absolutePath && file.readError?.failure === "binary_file" ? (
1352+
<AttachmentFilePreview
1353+
key={`${environmentId}:${absolutePath}`}
1354+
name={relativePath.split(/[\\/]/).at(-1) || relativePath}
1355+
mimeType="application/octet-stream"
1356+
sizeBytes={0}
1357+
asset={{
1358+
environmentId,
1359+
resource: {
1360+
_tag: "media-file",
1361+
threadId: threadRef.threadId,
1362+
path: absolutePath,
1363+
disposition: "attachment",
1364+
},
1365+
}}
1366+
origin="File"
1367+
/>
13511368
) : relativePath && file.error && file.data === null ? (
13521369
<div
13531370
role="alert"

0 commit comments

Comments
 (0)