Skip to content

Commit 34e9c56

Browse files
authored
feat(web): play audio inline and open any file the agent shares in the viewer (#21363)
1 parent 7bafc40 commit 34e9c56

28 files changed

Lines changed: 747 additions & 67 deletions

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ describe("AssetAccess", () => {
221221
["recording.webm", "video/webm"],
222222
["report.html", "text/html"],
223223
["report.pdf", "application/pdf"],
224+
["tone.wav", "audio/wav"],
225+
["tone.webm-audio", "audio/webm"],
224226
] as const) {
225227
const filePath = path.join(outside, name);
226228
yield* fs.writeFileString(filePath, "media");
@@ -245,6 +247,65 @@ describe("AssetAccess", () => {
245247
}).pipe(Effect.provide(layerTest)),
246248
);
247249

250+
it.effect("downloads any linked file with the same exact-file identity checks as media", () =>
251+
Effect.gen(function* () {
252+
const fs = yield* FileSystem.FileSystem;
253+
const path = yield* Path.Path;
254+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-download-root-" });
255+
const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-download-outside-" });
256+
for (const name of [
257+
"data.csv",
258+
"report.docx",
259+
"sheet.xlsx",
260+
"slides.pptx",
261+
"archive.zip",
262+
"no-extension",
263+
]) {
264+
for (const directory of [root, outside]) {
265+
const filePath = path.join(directory, name);
266+
yield* fs.writeFileString(filePath, "shared file bytes");
267+
const resource = {
268+
_tag: "media-file" as const,
269+
threadId: ThreadId.make("thread-1"),
270+
path: directory === root ? name : filePath,
271+
disposition: "attachment" as const,
272+
};
273+
const result = yield* issueAssetUrl({ resource, workspaceRoot: root });
274+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
275+
const separator = suffix.indexOf("/");
276+
const token = suffix.slice(0, separator);
277+
const asset = yield* resolveAsset(token, suffix.slice(separator + 1));
278+
expect(asset).toMatchObject({
279+
kind: "file",
280+
download: true,
281+
fileName: name,
282+
mimeType: "application/octet-stream",
283+
});
284+
if (asset?.kind !== "file") throw new Error("Expected a downloadable file");
285+
const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset));
286+
expect(response.headers.get("content-disposition")).toContain("attachment;");
287+
expect(response.headers.get("content-type")).toBe("application/octet-stream");
288+
expect(yield* Effect.promise(() => response.text())).toBe("shared file bytes");
289+
expect(yield* resolveAsset(token, "sibling.docx")).toBeNull();
290+
expect(yield* resolveAsset(token, `../${name}`)).toBeNull();
291+
expect(yield* resolveAsset(`${token}tampered`, name)).toBeNull();
292+
yield* fs.rename(filePath, `${filePath}.original`);
293+
yield* fs.writeFileString(filePath, "replacement bytes");
294+
expect(yield* resolveAsset(token, name)).toBeNull();
295+
}
296+
}
297+
const directoryError = yield* issueAssetUrl({
298+
resource: {
299+
_tag: "media-file",
300+
threadId: ThreadId.make("thread-1"),
301+
path: root,
302+
disposition: "attachment",
303+
},
304+
}).pipe(Effect.flip);
305+
expect(directoryError._tag).toBe("AssetWorkspaceAssetNotFoundError");
306+
}).pipe(Effect.provide(layerTest)),
307+
);
308+
248309
it.effect("resolves relative media paths from the thread workspace, including outside it", () =>
249310
Effect.gen(function* () {
250311
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
@@ -120,6 +120,7 @@ const AssetClaimsSchema = Schema.Union([
120120
version: Schema.Literal(1),
121121
kind: Schema.Literal("media-file-exact"),
122122
filePath: Schema.String,
123+
download: Schema.optionalKey(Schema.Boolean),
123124
device: Schema.String,
124125
inode: Schema.String,
125126
expiresAt: Schema.Number,
@@ -386,7 +387,9 @@ const finalizeAbsoluteMediaFileAsset = Effect.fn("AssetAccess.finalizeAbsoluteMe
386387
checkedPath: input.requestedPath,
387388
});
388389
}
389-
if (hostPreviewMimeTypeFromExtension(path.extname(canonicalFile)) === null) {
390+
const download =
391+
input.resource._tag === "media-file" && input.resource.disposition === "attachment";
392+
if (!download && hostPreviewMimeTypeFromExtension(path.extname(canonicalFile)) === null) {
390393
return yield* new AssetPreviewTypeValidationError({ resource: input.resource });
391394
}
392395
const wantsDimensions = HEADER_IMAGE_EXTENSIONS.has(path.extname(canonicalFile).toLowerCase());
@@ -421,6 +424,7 @@ const finalizeAbsoluteMediaFileAsset = Effect.fn("AssetAccess.finalizeAbsoluteMe
421424
version: 1 as const,
422425
kind: "media-file-exact" as const,
423426
filePath: canonicalFile,
427+
...(download ? { download: true } : {}),
424428
...opened.identity,
425429
expiresAt: input.expiresAt,
426430
},
@@ -985,7 +989,9 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
985989
Effect.orElseSucceed(() => null),
986990
);
987991
if (canonicalFile !== claims.filePath) return null;
988-
const mimeType = hostPreviewMimeTypeFromExtension(path.extname(canonicalFile));
992+
const mimeType = claims.download
993+
? "application/octet-stream"
994+
: hostPreviewMimeTypeFromExtension(path.extname(canonicalFile));
989995
if (!mimeType) return null;
990996
const file = yield* openMediaFile(canonicalFile, claims).pipe(
991997
Effect.tapError((cause) =>
@@ -994,7 +1000,13 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
9941000
Effect.orElseSucceed(() => null),
9951001
);
9961002
return file
997-
? ({ kind: "file", path: canonicalFile, mimeType, file } satisfies ResolvedAsset)
1003+
? ({
1004+
kind: "file",
1005+
path: canonicalFile,
1006+
mimeType,
1007+
file,
1008+
...(claims.download ? { download: true, fileName: path.basename(canonicalFile) } : {}),
1009+
} satisfies ResolvedAsset)
9981010
: null;
9991011
}
10001012
if (claims.kind === "workspace-file-exact") {

0 commit comments

Comments
 (0)