Skip to content

Commit e5e7131

Browse files
kevlingoclaude
andcommitted
feat(web): let the ping play a sound file you already have
The four tones are synthesised, which is fine as a default and wrong as the only option: people have a ding they already like, and Windows ships a folder full of them. "Your own file" joins the sound picker. Browse opens a file dialog on the machine running the server, starting in the Windows Media folder, and the path is stored in server settings rather than client settings, so every attached client rings with the same sound and a phone can play a file only the host has. The file is served through the asset route as its own attention-sound resource. The existing media-file resource serves images, video and documents, and widening its mime map would widen every other caller with it; this one refuses anything whose extension is not audio Chromium can decode, and the token still carries the device and inode identity that stops it outliving the file it was minted for. A file that has been moved or renamed says so in Settings and falls back to a tone, because losing the chosen sound is a smaller failure than losing the ping. Claude Opus 5 in T3 Code. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 890a2a0 commit e5e7131

16 files changed

Lines changed: 342 additions & 25 deletions

File tree

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
openSystemSettings,
4242
probeRemoteEditors,
4343
pickFolder,
44+
pickAttentionSound,
4445
showAttentionNotification,
4546
pickProjectFavicon,
4647
pickThemeFiles,
@@ -124,6 +125,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
124125
yield* ipc.handle(openExternal);
125126
yield* ipc.handle(openSystemSettings);
126127
yield* ipc.handle(showAttentionNotification);
128+
yield* ipc.handle(pickAttentionSound);
127129
yield* ipc.handle(probeRemoteEditors);
128130
yield* ipc.handle(getUpdateState);
129131
yield* ipc.handle(setUpdateChannel);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ export const PREVIEW_RECORDING_FRAME_CHANNEL = "desktop:preview-recording-frame"
106106
export const PREVIEW_STATE_CHANGE_CHANNEL = "desktop:preview-state-change";
107107
export const PREVIEW_POINTER_EVENT_CHANNEL = "desktop:preview-pointer-event";
108108
export const SHOW_ATTENTION_NOTIFICATION_CHANNEL = "desktop:show-attention-notification";
109+
export const PICK_ATTENTION_SOUND_CHANNEL = "desktop:pick-attention-sound";

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
EDITORS,
77
EditorId,
88
PickedThemeFileSchema,
9+
ATTENTION_SOUND_FILE_EXTENSIONS,
910
DesktopAttentionNotificationSchema,
1011
PickFolderOptionsSchema,
1112
PRIMARY_LOCAL_ENVIRONMENT_ID,
@@ -15,6 +16,7 @@ import {
1516
type PickedThemeFile,
1617
} from "@t3tools/contracts";
1718
import { WORKSPACE_IMAGE_PREVIEW_EXTENSIONS } from "@t3tools/shared/filePreview";
19+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
1820
import { isCommandAvailable } from "@t3tools/shared/shell";
1921
import * as NodeOS from "node:os";
2022
import * as FileSystem from "effect/FileSystem";
@@ -301,6 +303,32 @@ export const openExternal = DesktopIpc.makeIpcMethod({
301303
}),
302304
});
303305

306+
export const pickAttentionSound = DesktopIpc.makeIpcMethod({
307+
channel: IpcChannels.PICK_ATTENTION_SOUND_CHANNEL,
308+
payload: Schema.Undefined,
309+
result: Schema.NullOr(Schema.String),
310+
handler: Effect.fn("desktop.ipc.window.pickAttentionSound")(function* () {
311+
const dialog = yield* ElectronDialog.ElectronDialog;
312+
const electronWindow = yield* ElectronWindow.ElectronWindow;
313+
const path = yield* Path.Path;
314+
const fileSystem = yield* FileSystem.FileSystem;
315+
const platform = yield* HostProcessPlatform;
316+
// Windows keeps its own notification sounds in the Media folder, which is
317+
// the first place someone looks for a ding they already like.
318+
const mediaDir = path.join(platform === "win32" ? "C:\\Windows" : NodeOS.homedir(), "Media");
319+
const mediaDirExists = yield* fileSystem
320+
.exists(mediaDir)
321+
.pipe(Effect.orElseSucceed(() => false));
322+
const paths = yield* dialog.pickFiles({
323+
owner: yield* electronWindow.focusedMainOrFirst,
324+
defaultPath: mediaDirExists ? Option.some(mediaDir) : Option.none(),
325+
filters: [{ name: "Audio", extensions: [...ATTENTION_SOUND_FILE_EXTENSIONS] }],
326+
multiple: false,
327+
});
328+
return paths[0] ?? null;
329+
}),
330+
});
331+
304332
export const showAttentionNotification = DesktopIpc.makeIpcMethod({
305333
channel: IpcChannels.SHOW_ATTENTION_NOTIFICATION_CHANNEL,
306334
payload: DesktopAttentionNotificationSchema,

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
141141
ipcRenderer.invoke(IpcChannels.SET_WSL_BACKEND_ENABLED_CHANNEL, enabled),
142142
setWslDistro: (distro) => ipcRenderer.invoke(IpcChannels.SET_WSL_DISTRO_CHANNEL, distro),
143143
setWslOnly: (enabled) => ipcRenderer.invoke(IpcChannels.SET_WSL_ONLY_CHANNEL, enabled),
144+
pickAttentionSound: () => ipcRenderer.invoke(IpcChannels.PICK_ATTENTION_SOUND_CHANNEL, undefined),
144145
showAttentionNotification: (input) =>
145146
ipcRenderer.invoke(IpcChannels.SHOW_ATTENTION_NOTIFICATION_CHANNEL, input),
146147
pickFolder: (options) => ipcRenderer.invoke(IpcChannels.PICK_FOLDER_CHANNEL, options),

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,48 @@ describe("AssetAccess", () => {
8383
}).pipe(Effect.provide(testLayer)),
8484
);
8585

86+
it.effect("serves the user's own notification sound, and only audio", () =>
87+
Effect.gen(function* () {
88+
const fs = yield* FileSystem.FileSystem;
89+
const path = yield* Path.Path;
90+
const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-attention-sound-" });
91+
92+
for (const [name, mimeType] of [
93+
["ding.wav", "audio/wav"],
94+
["ding.mp3", "audio/mpeg"],
95+
["ding.ogg", "audio/ogg"],
96+
] as const) {
97+
const filePath = path.join(outside, name);
98+
yield* fs.writeFileString(filePath, "audio");
99+
const canonicalFile = yield* fs.realPath(filePath);
100+
const result = yield* issueAssetUrl({
101+
resource: { _tag: "attention-sound", path: filePath },
102+
});
103+
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
104+
const separator = suffix.indexOf("/");
105+
const token = suffix.slice(0, separator);
106+
expect(yield* resolveAsset(token, suffix.slice(separator + 1))).toMatchObject({
107+
kind: "file",
108+
path: canonicalFile,
109+
mimeType,
110+
});
111+
// The token names one file: a sibling in the same folder is not part of
112+
// the grant, which is what keeps this from being a read-anything route.
113+
yield* fs.writeFileString(path.join(outside, "private.wav"), "private");
114+
expect(yield* resolveAsset(token, "private.wav")).toBeNull();
115+
}
116+
117+
// The extension check is the whole boundary, so it is worth an assertion
118+
// rather than trust: a settings file naming a script must not mint.
119+
const scriptPath = path.join(outside, "payload.ps1");
120+
yield* fs.writeFileString(scriptPath, "whoami");
121+
const rejected = yield* Effect.flip(
122+
issueAssetUrl({ resource: { _tag: "attention-sound", path: scriptPath } }),
123+
);
124+
expect(rejected).toBeInstanceOf(AssetPreviewTypeValidationError);
125+
}).pipe(Effect.provide(testLayer)),
126+
);
127+
86128
it.effect("reports pixel dimensions from an image header and nothing for other files", () =>
87129
Effect.gen(function* () {
88130
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AssetResource } from "@t3tools/contracts";
22
import {
3+
attentionSoundMimeTypeFromExtension,
34
AssetAttachmentNotFoundError,
45
AssetPreviewTypeValidationError,
56
AssetProjectFaviconInspectionError,
@@ -101,6 +102,14 @@ const AssetClaimsSchema = Schema.Union([
101102
inode: Schema.String,
102103
expiresAt: Schema.Number,
103104
}),
105+
Schema.Struct({
106+
version: Schema.Literal(1),
107+
kind: Schema.Literal("attention-sound-exact"),
108+
filePath: Schema.String,
109+
device: Schema.String,
110+
inode: Schema.String,
111+
expiresAt: Schema.Number,
112+
}),
104113
Schema.Struct({
105114
version: Schema.Literal(1),
106115
kind: Schema.Literal("attachment"),
@@ -336,6 +345,48 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
336345
fileName = path.basename(canonicalFile);
337346
break;
338347
}
348+
case "attention-sound": {
349+
// Audio only, and only a real file: the extension check is what keeps
350+
// this from becoming a second, wider read-anything route.
351+
const requestedPath = input.resource.path;
352+
if (!path.isAbsolute(requestedPath)) {
353+
return yield* new AssetWorkspaceContextNotFoundError({ resource: input.resource });
354+
}
355+
if (attentionSoundMimeTypeFromExtension(path.extname(requestedPath)) === null) {
356+
return yield* new AssetPreviewTypeValidationError({ resource: input.resource });
357+
}
358+
const canonicalSound = yield* resolveCanonicalFile(requestedPath).pipe(
359+
Effect.mapError(
360+
(cause) => new AssetWorkspaceAssetInspectionError({ resource: input.resource, cause }),
361+
),
362+
);
363+
if (!canonicalSound) {
364+
return yield* new AssetWorkspaceAssetNotFoundError({ resource: input.resource });
365+
}
366+
const openedSound = yield* openMediaFile(canonicalSound).pipe(
367+
Effect.map((file) =>
368+
file === null
369+
? null
370+
: { device: file.info.dev.toString(), inode: file.info.ino.toString() },
371+
),
372+
Effect.scoped,
373+
Effect.mapError(
374+
(cause) => new AssetWorkspaceAssetInspectionError({ resource: input.resource, cause }),
375+
),
376+
);
377+
if (!openedSound) {
378+
return yield* new AssetWorkspaceAssetNotFoundError({ resource: input.resource });
379+
}
380+
claims = {
381+
version: 1,
382+
kind: "attention-sound-exact",
383+
filePath: canonicalSound,
384+
...openedSound,
385+
expiresAt,
386+
};
387+
fileName = path.basename(canonicalSound);
388+
break;
389+
}
339390
case "workspace-file": {
340391
if (!input.workspaceRoot) {
341392
return yield* new AssetWorkspaceContextNotFoundError({
@@ -722,6 +773,26 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
722773
? ({ kind: "file", path: canonicalFile, mimeType, file } satisfies ResolvedAsset)
723774
: null;
724775
}
776+
if (claims.kind === "attention-sound-exact") {
777+
if (decodedPath !== path.basename(claims.filePath)) return null;
778+
const canonicalSound = yield* resolveCanonicalFile(claims.filePath).pipe(
779+
Effect.orElseSucceed(() => null),
780+
);
781+
if (canonicalSound !== claims.filePath) return null;
782+
const soundMimeType = attentionSoundMimeTypeFromExtension(path.extname(canonicalSound));
783+
if (!soundMimeType) return null;
784+
const soundFile = yield* openMediaFile(canonicalSound, claims).pipe(
785+
Effect.orElseSucceed(() => null),
786+
);
787+
return soundFile
788+
? ({
789+
kind: "file",
790+
path: canonicalSound,
791+
mimeType: soundMimeType,
792+
file: soundFile,
793+
} satisfies ResolvedAsset)
794+
: null;
795+
}
725796
if (claims.kind === "workspace-file-exact") {
726797
if (decodedPath !== path.basename(claims.relativePath)) return null;
727798
const exactWorkspaceFile = yield* resolveCanonicalWorkspaceFileForRequest({

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,7 @@ const makeWsRpcLayer = (
24012401
if (
24022402
input.resource._tag === "attachment" ||
24032403
input.resource._tag === "native-app-icon" ||
2404+
input.resource._tag === "attention-sound" ||
24042405
(input.resource._tag === "media-file" && path.isAbsolute(input.resource.path))
24052406
) {
24062407
return yield* issueAssetUrl({ resource: input.resource });

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
import { attentionPingKey, shouldRingAttentionPing } from "@t3tools/client-runtime/state/attention";
44
import type { AttentionPing, EnvironmentId } from "@t3tools/contracts";
5-
import { useEffect, useRef } from "react";
5+
import { useEffect, useMemo, useRef } from "react";
66

7+
import { useAssetUrlState } from "~/assets/assetUrls";
78
import { isElectron } from "~/env";
8-
import { playAttentionSound } from "~/lib/attentionSound";
9+
import { usePrimarySettings } from "~/hooks/useSettings";
10+
import { playAttentionSound, playAttentionSoundUrl } from "~/lib/attentionSound";
11+
import { attentionEnvironment } from "~/state/attention";
912
import { useEnvironments } from "~/state/environments";
1013
import { useEnvironmentQuery } from "~/state/query";
11-
import { attentionEnvironment } from "~/state/attention";
1214

1315
import { toastManager } from "./ui/toast";
1416

1517
/**
1618
* Rings once per attention ping, on every environment this client is attached
1719
* to.
1820
*
19-
* Three parts, because no single one is enough. The tone says "now" to someone
21+
* Three parts, because no single one is enough. The sound says "now" to someone
2022
* in the room. The toast says which session is asking, for someone who comes
2123
* back to a screen. The desktop notification is the only part that survives a
2224
* minimized window, and it goes to the Action Center where it waits.
@@ -27,6 +29,14 @@ import { toastManager } from "./ui/toast";
2729
function EnvironmentAttentionAlerts({ environmentId }: { readonly environmentId: EnvironmentId }) {
2830
const query = useEnvironmentQuery(attentionEnvironment.latestPing({ environmentId }));
2931
const ping = query.data as AttentionPing | null;
32+
const soundFile = usePrimarySettings((settings) => settings.agentAttentionSoundFile);
33+
// Resolved before any ping arrives, so ringing costs no round trip. The
34+
// signed URL refreshes with the atom whenever the chosen file changes.
35+
const soundResource = useMemo(
36+
() => (soundFile === null ? null : ({ _tag: "attention-sound", path: soundFile } as const)),
37+
[soundFile],
38+
);
39+
const soundUrl = useAssetUrlState(environmentId, soundResource);
3040
const lastRungKeyRef = useRef<string | null>(null);
3141

3242
useEffect(() => {
@@ -42,20 +52,32 @@ function EnvironmentAttentionAlerts({ environmentId }: { readonly environmentId:
4252
}
4353
lastRungKeyRef.current = attentionPingKey(ping);
4454

45-
const played = playAttentionSound(ping.sound);
55+
// The user's own file, when they picked one and the server could serve it.
56+
// Anything else, including a file that will not decode, falls back to a
57+
// tone: losing the chosen sound is a smaller failure than losing the ping.
58+
const playedPromise =
59+
ping.sound === "custom" && soundUrl._tag === "Success"
60+
? playAttentionSoundUrl(soundUrl.url).then(
61+
(played) => played || playAttentionSound(ping.sound),
62+
)
63+
: Promise.resolve(playAttentionSound(ping.sound));
64+
4665
toastManager.add({
4766
type: "info",
4867
title: `${ping.threadTitle} needs you`,
4968
description: ping.message,
5069
});
70+
5171
// The renderer already made the noise, so the OS notification stays silent
5272
// unless audio was refused, in which case its sound is the only one.
53-
void (isElectron ? window.desktopBridge?.showAttentionNotification : undefined)?.({
54-
title: `${ping.threadTitle} needs you`,
55-
body: ping.message,
56-
silent: played,
57-
});
58-
}, [ping]);
73+
void playedPromise.then((played) =>
74+
(isElectron ? window.desktopBridge?.showAttentionNotification : undefined)?.({
75+
title: `${ping.threadTitle} needs you`,
76+
body: ping.message,
77+
silent: played,
78+
}),
79+
);
80+
}, [ping, soundUrl]);
5981

6082
return null;
6183
}

0 commit comments

Comments
 (0)