Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3765,7 +3765,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
<ComposerContextActionsContext value={composerContextActions}>
<ComposerPromptEditor
draftKey={composerDraftTargetKey}
ariaLabel="Message"
// Switching threads lands focus here, so the label names the thread.
ariaLabel={activeThread ? `Message ${activeThread.title}` : "Message"}
suggestionListId={composerSuggestionListId}
activeSuggestionId={
composerSuggestionListVisible && activeComposerMenuItem
Expand Down
32 changes: 21 additions & 11 deletions apps/web/src/components/chat/ModelPickerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
useEnvironmentModelPreferences,
useToggleEnvironmentModelFavorite,
} from "~/hooks/useSettings";
import { announce } from "~/lib/announce";
import { cn } from "~/lib/utils";
import { getVirtualizedScrollFadeClassName } from "../ui/scroll-area";
import { TooltipProvider } from "../ui/tooltip";
Expand Down Expand Up @@ -485,26 +486,26 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
modelSlug: string,
instanceId: ProviderInstanceId,
additive = false,
) => {
): string | null => {
if (getModelDisabledReason?.(instanceId, modelSlug)) {
return;
return null;
}
const options = modelOptionsByInstance.get(instanceId);
if (!options) {
return;
return null;
}
const entry = instanceEntries.find((candidate) => candidate.instanceId === instanceId);
if (!entry) {
return;
return null;
}
const resolvedModel = resolveSelectableModel(entry.driverKind, modelSlug, options);
if (resolvedModel) {
if (additive && onToggleModel) {
onToggleModel(instanceId, resolvedModel);
} else {
onInstanceModelChange(instanceId, resolvedModel);
}
if (!resolvedModel) return null;
if (additive && onToggleModel) {
onToggleModel(instanceId, resolvedModel);
} else {
onInstanceModelChange(instanceId, resolvedModel);
}
return resolvedModel;
};

const modelJumpShortcutContext = {
Expand Down Expand Up @@ -773,6 +774,10 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
});
setSearchQuery("");
handleSelectInstance(next);
announce(
next === "favorites" ? "Favorites" : (entryByInstanceId.get(next)?.displayName ?? next),
"model-picker",
);
return;
}
const jumpIndex = modelPickerJumpIndexFromCommand(command ?? "");
Expand All @@ -790,7 +795,12 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
if (!model) {
return;
}
handleModelSelect(model.slug, model.instanceId);
const selectedSlug = handleModelSelect(model.slug, model.instanceId);
if (selectedSlug === null) return;
const name = flatModels.find(
(item) => item.slug === selectedSlug && item.instanceId === model.instanceId,
)?.name;
announce(`Model: ${name ?? selectedSlug}`, "model-picker");
};

return (
Expand Down
36 changes: 35 additions & 1 deletion apps/web/src/components/chat/OpenInPicker.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { AuthOrchestrationOperateScope, EnvironmentId, type EditorId } from "@t3tools/contracts";
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
import { Cause } from "effect";
import { AsyncResult } from "effect/reactivity";
import { describe, expect, it, vi } from "vite-plus/test";

import { act, render as renderUi } from "../../../test/render";
import { act, fakeTimers, render as renderUi } from "../../../test/render";
import type { RemoteOpenState } from "../../remoteOpen";

const state = vi.hoisted(() => ({
Expand Down Expand Up @@ -181,3 +182,36 @@ describe("host editor access", () => {
expect((await pressOpenFavorite()).defaultPrevented).toBe(false);
});
});

describe("open in editor announcement", () => {
it("speaks only after the shortcut opened the editor, from the picker or the fallback", async () => {
await using fixture = await setup();
const { screen, renderPicker, pressOpenFavorite, FavoriteEditorFallback } = fixture;
state.allowed.add(selected);
await renderPicker();
using _timers = fakeTimers();
const regions = () => [...document.querySelectorAll('body > [aria-live="polite"]')];
const spoken = async () => {
await act(async () => vi.runAllTimers());
return regions()
.map((region) => region.textContent)
.join("");
};
const refuse = () =>
state.run.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("No editor"))));

refuse();
await pressOpenFavorite();
expect(await spoken()).toBe("");
await pressOpenFavorite();
expect(await spoken()).toBe("Opened in VS Code");

for (const region of regions()) region.textContent = "";
await act(async () => screen.rerender(<FavoriteEditorFallback />));
refuse();
await pressOpenFavorite();
expect(await spoken()).toBe("");
await pressOpenFavorite();
expect(await spoken()).toBe("Opened in VS Code");
});
});
31 changes: 17 additions & 14 deletions apps/web/src/components/chat/OpenInPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
RustRoverIcon,
WebStormIcon,
} from "../JetBrainsIcons";
import { announce } from "~/lib/announce";
import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
import { shellEnvironment } from "~/state/shell";
import { useAtomCommand } from "~/state/use-atom-command";
Expand Down Expand Up @@ -240,29 +241,28 @@ export const OpenInPicker = memo(function OpenInPicker({
const effectiveEditors = remote.mode === "local-exec" ? availableEditors : remoteCapableEditors;
const [preferredEditor, setPreferredEditor] = usePreferredEditor(effectiveEditors);

const openInEditor = (editorId: EditorId | null) => {
if (!openInCwd) return;
const openInEditor = async (editorId: EditorId | null): Promise<boolean> => {
if (!openInCwd) return false;
const editor = editorId ?? preferredEditor;
if (!editor) return;
if (remote.mode === "remote-unavailable") return;
if (!editor) return false;
if (remote.mode === "remote-unavailable") return false;
if (remote.mode === "remote-links") {
const url = buildRemoteOpenUrl({
editor,
...remote.host,
absolutePath: openInCwd,
isFile: openInCwdIsFile,
});
if (url === undefined) return;
if (url === undefined) return false;
// Only record hint-seen/preferred when the shell actually accepted
// the URL (an older desktop build can refuse the editor scheme).
void openRemoteEditorUrl(url).then((opened) => {
if (!opened) return;
markRemoteHintSeen();
setPreferredEditor(editor);
});
return;
const opened = await openRemoteEditorUrl(url);
if (!opened) return false;
markRemoteHintSeen();
setPreferredEditor(editor);
return true;
}
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return;
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
const result = openInEditorMutation({
environmentId,
input: {
Expand All @@ -272,7 +272,7 @@ export const OpenInPicker = memo(function OpenInPicker({
},
});
setPreferredEditor(editor);
return result;
return (await result)._tag === "Success";
};

const openFavoriteEditorShortcutLabel = shortcutLabelForCommand(
Expand All @@ -293,7 +293,10 @@ export const OpenInPicker = memo(function OpenInPicker({
return;

e.preventDefault();
void openInEditor(preferredEditor);
const label = editorLabelForPlatform(preferredEditor, navigator.platform);
void openInEditor(preferredEditor).then((opened) => {
if (opened) announce(`Opened in ${label}`);
});
};
window.addEventListener("keydown", handler, true);
return () => window.removeEventListener("keydown", handler, true);
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/components/chat/OpenInPickerShortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { EditorId, EnvironmentId, ResolvedKeybindingsConfig } from "@t3tool
import { useEffect } from "react";

import { usePreferredEditor } from "../../editorPreferences";
import { editorLabelForPlatform } from "../../editorLabels";
import { isOpenFavoriteEditorShortcut } from "../../keybindings";
import { announce } from "../../lib/announce";
import { useRemoteOpenState } from "../../remoteOpen";
import { shellEnvironment } from "../../state/shell";
import { useAtomCommand } from "../../state/use-atom-command";
Expand Down Expand Up @@ -31,12 +33,15 @@ export function useOpenFavoriteEditorShortcut({
if (!openInCwd || !preferredEditor) return;

event.preventDefault();
const label = editorLabelForPlatform(preferredEditor, navigator.platform);
void openInEditorMutation({
environmentId,
input: {
cwd: openInCwd,
editor: preferredEditor,
},
}).then((result) => {
if (result._tag === "Success") announce(`Opened in ${label}`);
});
};
window.addEventListener("keydown", handler);
Expand Down
8 changes: 5 additions & 3 deletions apps/web/src/components/chat/ProviderModelPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,11 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
terminalOpen={props.terminalOpen ?? false}
onRequestClose={() => setIsMenuOpen(false)}
{...(props.onOpenProviderSetup ? { onOpenProviderSetup: props.onOpenProviderSetup } : {})}
{...(props.getModelDisabledReason
? { getModelDisabledReason: props.getModelDisabledReason }
: {})}
getModelDisabledReason={(instanceId, model) =>
props.disabled
? "Model picker is disabled"
: (props.getModelDisabledReason?.(instanceId, model) ?? null)
}
onInstanceModelChange={handleInstanceModelChange}
/>
{props.selectedModels === undefined ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it, vi } from "vite-plus/test";

import { act, fakeTimers, render } from "../../../test/render";
import { useThreadUndoNotice } from "../../hooks/showThreadUndoNotice";
import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";

const spoken = () =>
[...document.querySelectorAll('body > [aria-live="polite"]')]
.map((region) => region.textContent)
.join("");

describe("SidebarThreadUndoNotice", () => {
it("speaks the notice, and drops it when the notice clears before it is spoken", async () => {
using _notice = { [Symbol.dispose]: () => useThreadUndoNotice.setState({ notice: null }) };
await using _screen = await render(<SidebarThreadUndoNotice />);
using _timers = fakeTimers();
const showSettled = () =>
act(async () =>
useThreadUndoNotice.setState({
notice: { action: "Settled", count: 1, undo: async () => {} },
}),
);

await showSettled();
await act(async () => vi.advanceTimersByTime(300));
expect(spoken()).toMatch(/^Settled 1 thread, /);

await act(async () => useThreadUndoNotice.setState({ notice: null }));
for (const region of document.querySelectorAll('body > [aria-live="polite"]')) {
region.textContent = "";
}
await showSettled();
await act(async () => useThreadUndoNotice.setState({ notice: null }));
await act(async () => vi.advanceTimersByTime(300));
expect(spoken()).toBe("");
});
});
24 changes: 17 additions & 7 deletions apps/web/src/components/sidebar/SidebarThreadUndoNotice.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useAtomValue } from "@effect/atom-react";
import { useEffect } from "react";

import { undoLatestThreadAction, useThreadUndoNotice } from "../../hooks/showThreadUndoNotice";
import { shortcutLabelForCommand } from "../../keybindings";
import { announce, cancelAnnouncement } from "../../lib/announce";
import { primaryServerKeybindingsAtom } from "../../state/server";
import { Alert, AlertDescription } from "../ui/alert";
import { InlineButton } from "../ui/button";
Expand All @@ -10,17 +12,25 @@ export function SidebarThreadUndoNotice() {
const notice = useThreadUndoNotice((state) => state.notice);
const keybindings = useAtomValue(primaryServerKeybindingsAtom);

if (!notice) return null;
const shortcut = shortcutLabelForCommand(keybindings, "thread.undo");
const noun = `${notice.action === "Discarded" ? "draft" : "thread"}${notice.count === 1 ? "" : "s"}`;
const undoLabel = shortcut ? `${shortcut} to undo` : "Undo";
const summary = notice
? `${notice.action} ${notice.count} ${notice.action === "Discarded" ? "draft" : "thread"}${notice.count === 1 ? "" : "s"}`
: null;

// A live region that mounts already filled is skipped by screen readers, and settling
// usually moves focus to the next thread at the same moment.
useEffect(() => {
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
if (summary) announce(`${summary}, ${undoLabel}`, "thread-undo-notice");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
else cancelAnnouncement("thread-undo-notice");
}, [summary, undoLabel]);

if (!summary) return null;

return (
<Alert role="status" variant="sidebar">
<Alert role="group" variant="sidebar">
<AlertDescription>
{notice.action} {notice.count} {noun},{" "}
<InlineButton onClick={undoLatestThreadAction}>
{shortcut ? `${shortcut} to undo` : "Undo"}
</InlineButton>
{summary}, <InlineButton onClick={undoLatestThreadAction}>{undoLabel}</InlineButton>
</AlertDescription>
</Alert>
);
Expand Down
62 changes: 62 additions & 0 deletions apps/web/src/lib/announce.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it, vi } from "vite-plus/test";

import { fakeTimers } from "../../test/render";
import { announce, cancelAnnouncement } from "./announce";

function spokenText() {
return Array.from(document.querySelectorAll('[aria-live="polite"]'), (region) =>
region.textContent?.trim(),
).filter(Boolean);
}

describe("announce", () => {
it("waits for focus to settle, then speaks waiting messages together", () => {
using _timers = fakeTimers();
const composer = document.createElement("textarea");
document.body.append(composer);
using _composer = { [Symbol.dispose]: () => composer.remove() };

announce("Settled 1 thread, Ctrl+Z to undo");
announce("Model: Claude Sonnet 5.5");
vi.advanceTimersByTime(200);
composer.focus();
vi.advanceTimersByTime(200);
expect(spokenText()).toEqual([]);

vi.advanceTimersByTime(50);
expect(spokenText()).toEqual(["Settled 1 thread, Ctrl+Z to undo. Model: Claude Sonnet 5.5"]);
});

it("speaks the same message twice in a row through the other region", () => {
using _timers = fakeTimers();
const regionTexts = () =>
Array.from(document.querySelectorAll('[aria-live="polite"]'), (region) => region.textContent);

announce("Claude");
vi.runAllTimers();
const first = regionTexts();
announce("Claude");
vi.runAllTimers();

expect(regionTexts()).toEqual([...first].reverse());
expect(regionTexts()).toContain("Claude");
});

it("replaces a waiting message from the same source", () => {
using _timers = fakeTimers();

announce("Settled 1 thread, Ctrl+Z to undo", "undo");
announce("Opening in VS Code");
announce("Settled 2 threads, Ctrl+Z to undo", "undo");
vi.runAllTimers();

expect(spokenText()).toEqual(["Opening in VS Code. Settled 2 threads, Ctrl+Z to undo"]);

announce("Settled 1 thread, Ctrl+Z to undo", "undo");
announce("Opening in Zed");
cancelAnnouncement("undo");
vi.runAllTimers();

expect(spokenText()).toEqual(["Opening in Zed"]);
});
});
Loading
Loading