Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(ui): restore voice shortcut caret focus (#2220)

Call White2a4038705182
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index 580576423..8cbd459e4 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -89,6 +89,7 @@ jobs:
npm run test:inbox-return
npm run test:badge-triggers
npm run test:workbench-general -- sidebar-floating.spec.ts
+ npm run test:voice-shortcut
show-router-integration:
runs-on: ubuntu-latest
diff --git a/ui/e2e/voice-shortcut/fixture.html b/ui/e2e/voice-shortcut/fixture.html
new file mode 100644
index 000000000..cd848ab7b
--- /dev/null
+++ b/ui/e2e/voice-shortcut/fixture.html
@@ -0,0 +1,11 @@
+<!doctype html>
+<html>
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width" />
+ </head>
+ <body>
+ <div id="root"></div>
+ <script type="module" src="./fixture.tsx"></script>
+ </body>
+</html>
diff --git a/ui/e2e/voice-shortcut/fixture.tsx b/ui/e2e/voice-shortcut/fixture.tsx
new file mode 100644
index 000000000..1dab2e19e
--- /dev/null
+++ b/ui/e2e/voice-shortcut/fixture.tsx
@@ -0,0 +1,75 @@
+import { StrictMode, useEffect, useRef } from 'react';
+import { createRoot } from 'react-dom/client';
+import { Composer, type ComposerHandle } from '../../src/components/workbench/Composer';
+import { ToastProvider } from '../../src/context/ToastProvider';
+import i18n from '../../src/i18n';
+import '../../src/index.css';
+
+void i18n.changeLanguage('en');
+
+declare global {
+ interface Window {
+ voiceShortcutTest: {
+ sent: string[];
+ };
+ }
+}
+
+window.voiceShortcutTest = { sent: [] };
+
+window.fetch = async (input, init) => {
+ const url = new URL(
+ typeof input === 'string' ? input : input instanceof URL ? input.href : input.url,
+ location.origin,
+ );
+ if (url.pathname === '/api/asr/status') {
+ return new Response(JSON.stringify({ available: true, max_file_bytes: 25_000_000 }), {
+ headers: { 'Content-Type': 'application/json' },
+ });
+ }
+ if (url.pathname === '/api/cloud/token') {
+ return new Response(JSON.stringify({ error: 'fixture local ASR only' }), { status: 503 });
+ }
+ if (url.pathname === '/api/asr/transcribe') {
+ return new Response(JSON.stringify({ text: 'Send this transcript', cleanup: 'success' }), {
+ headers: { 'Content-Type': 'application/json' },
+ });
+ }
+ if (url.pathname === '/api/csrf-token') {
+ return new Response(JSON.stringify({ csrf_token: 'fixture' }), {
+ headers: { 'Content-Type': 'application/json' },
+ });
+ }
+ throw new Error(`Unexpected fixture request: ${init?.method ?? 'GET'} ${url.pathname}`);
+};
+
+export function VoiceShortcutHarness() {
+ const composerRef = useRef<ComposerHandle>(null);
+ useEffect(() => {
+ const onKeyDown = (event: KeyboardEvent) => {
+ composerRef.current?.handleVoiceShortcut(event, true);
+ };
+ window.addEventListener('keydown', onKeyDown, true);
+ return () => window.removeEventListener('keydown', onKeyDown, true);
+ }, []);
+ return (
+ <Composer
+ ref={composerRef}
+ sessionId="voice-shortcut-session"
+ onSend={(text) => {
+ window.voiceShortcutTest.sent.push(text);
+ return true;
+ }}
+ onSearchAgents={async () => []}
+ onSearchSessions={async () => []}
+ />
+ );
+}
+
+createRoot(document.getElementById('root')!).render(
+ <StrictMode>
+ <ToastProvider>
+ <VoiceShortcutHarness />
+ </ToastProvider>
+ </StrictMode>,
+);
diff --git a/ui/e2e/voice-shortcut/shortcut.spec.ts b/ui/e2e/voice-shortcut/shortcut.spec.ts
new file mode 100644
index 000000000..540b17c90
--- /dev/null
+++ b/ui/e2e/voice-shortcut/shortcut.spec.ts
@@ -0,0 +1,43 @@
+import { expect, test } from '@playwright/test';
+import en from '../../src/i18n/en.json' with { type: 'json' };
+
+declare global {
+ interface Window {
+ voiceShortcutTest: {
+ sent: string[];
+ };
+ }
+}
+
+test('shortcut completion restores the transcript caret and Enter sends', async ({ page }) => {
+ await page.goto('/e2e/voice-shortcut/fixture.html');
+ const textbox = page.getByRole('textbox');
+ await textbox.focus();
+ await page.keyboard.press('Alt+z');
+ await expect(page.getByRole('button', { name: en.chat.compose.stopRecording })).toBeVisible();
+
+ await page.waitForTimeout(1_100);
+ await page.keyboard.press('Alt+z');
+
+ await expect(textbox).toHaveText('Send this transcript');
+ await expect(textbox).toBeFocused();
+ await expect.poll(() => textbox.evaluate((element) => {
+ const selection = window.getSelection();
+ return {
+ active: document.activeElement === element,
+ text: selection?.toString() ?? '',
+ anchorOffset: selection?.anchorOffset ?? -1,
+ focusOffset: selection?.focusOffset ?? -1,
+ };
+ })).toEqual({
+ active: true,
+ text: '',
+ anchorOffset: 'Send this transcript'.length,
+ focusOffset: 'Send this transcript'.length,
+ });
+
+ await page.keyboard.press('Enter');
+ await expect.poll(() => page.evaluate(() => window.voiceShortcutTest.sent)).toEqual([
+ 'Send this transcript',
+ ]);
+});
diff --git a/ui/e2e/voice-shortcut/tsconfig.json b/ui/e2e/voice-shortcut/tsconfig.json
new file mode 100644
index 000000000..024c0d6bd
--- /dev/null
+++ b/ui/e2e/voice-shortcut/tsconfig.json
@@ -0,0 +1,6 @@
+{
+ "extends": "../../tsconfig.app.json",
+ "compilerOptions": { "types": ["vite/client", "node"] },
+ "include": ["*.ts", "*.tsx", "../../playwright.voice-shortcut.config.ts", "../../agentation.d.ts"],
+ "exclude": []
+}
diff --git a/ui/package.json b/ui/package.json
index 9a570eace..c65ade9ef 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -31,6 +31,7 @@
"test:onboarding-fidelity": "tsc -p e2e/onboarding-fidelity/tsconfig.json --noEmit && playwright test --config playwright.onboarding-fidelity.config.ts",
"test:workbench-general": "tsc -p e2e/workbench-general/tsconfig.json --noEmit && playwright test --config playwright.workbench-general.config.ts",
"test:workbench-general-build": "tsc -p e2e/workbench-general/tsconfig.json --noEmit && playwright test --config playwright.workbench-general-build.config.ts",
+ "test:voice-shortcut": "tsc -p e2e/voice-shortcut/tsconfig.json --noEmit && playwright test --config playwright.voice-shortcut.config.ts",
"validate:catalog": "node scripts/validate-scenario-catalog.mjs",
"validate:theme": "node scripts/validate-theme.mjs"
},
diff --git a/ui/playwright.voice-shortcut.config.ts b/ui/playwright.voice-shortcut.config.ts
new file mode 100644
index 000000000..06bd2a836
--- /dev/null
+++ b/ui/playwright.voice-shortcut.config.ts
@@ -0,0 +1,21 @@
+import { defineConfig, devices } from '@playwright/test';
+
+export default defineConfig({
+ testDir: './e2e/voice-shortcut',
+ outputDir: './e2e/.artifacts/voice-shortcut',
+ workers: 1,
+ retries: 0,
+ use: {
+ baseURL: 'http://127.0.0.1:5218',
+ permissions: ['microphone'],
+ launchOptions: {
+ args: ['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'],
+ },
+ },
+ webServer: {
+ command: 'npm run dev -- --host 127.0.0.1 --port 5218 --strictPort',
+ env: { VIBE_UI_BACKEND: 'http://127.0.0.1:9' },
+ url: 'http://127.0.0.1:5218/e2e/voice-shortcut/fixture.html',
+ },
+ projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
+});
diff --git a/ui/src/components/workbench/Composer.shortcut.test.tsx b/ui/src/components/workbench/Composer.shortcut.test.tsx
index eed10f109..76ce02270 100644
--- a/ui/src/components/workbench/Composer.shortcut.test.tsx
+++ b/ui/src/components/workbench/Composer.shortcut.test.tsx
@@ -251,11 +251,53 @@ describe('Composer voice shortcut', () => {
const expectedFocus = focus === 'moved' ? outside : focus === 'dialog' ? foreground : textbox;
await waitFor(() => expect(document.activeElement).toBe(expectedFocus));
if (expectedFocus !== textbox) return;
+ if (mentions) {
+ const selection = window.getSelection();
+ expect(selection?.anchorNode?.textContent).toBe('Send this transcript');
+ expect(selection?.focusNode?.textContent).toBe('Send this transcript');
+ expect(selection?.anchorOffset).toBe('Send this transcript'.length);
+ expect(selection?.focusOffset).toBe('Send this transcript'.length);
+ }
await user.keyboard('{Enter}');
await waitFor(() => expect(onSend).toHaveBeenCalledOnce());
expect(onSend.mock.calls[0][0]).toBe('Send this transcript');
});
+ it('preserves a plain-text insertion caret inside an existing draft', async () => {
+ const user = userEvent.setup();
+ const onSend = vi.fn();
+ voiceMocks.realtimeFinish.mockResolvedValue({
+ text: 'inserted text',
+ cleanup: 'success',
+ });
+ voiceMocks.finish.mockImplementation(() => {
+ voiceMocks.onSegment?.(new Blob(['audio']), { durationMs: 1000, final: true });
+ voiceMocks.onStopped?.('finish', { pendingSegmentCount: 0 });
+ });
+ renderComposer('textarea-caret-session', {
+ initialDraft: 'before after',
+ onSend,
+ });
+ const textbox = await screen.findByRole('textbox');
+ await waitFor(() => expect((textbox as HTMLTextAreaElement).value).toBe('before after'));
+ textbox.focus();
+ textbox.setSelectionRange('before '.length, 'before '.length);
+
+ await user.keyboard('{Alt>}z{/Alt}');
+ await screen.findByRole('button', { name: en.chat.compose.stopRecording });
+ await user.keyboard('{Alt>}z{/Alt}');
+
+ await waitFor(() => expect((textbox as HTMLTextAreaElement).value).toBe('before inserted text after'));
+ await waitFor(() => expect(document.activeElement).toBe(textbox));
+ const caret = 'before inserted text '.length;
+ expect(textbox.selectionStart).toBe(caret);
+ expect(textbox.selectionEnd).toBe(caret);
+
+ await user.keyboard('{Enter}');
+ await waitFor(() => expect(onSend).toHaveBeenCalledOnce());
+ expect(onSend.mock.calls[0][0]).toBe('before inserted text after');
+ });
+
it('does not start behind a foreground surface but always finishes an active recording', async () => {
const view = renderComposer('gated-shortcut-session');
const outside = screen.getByRole('button', { name: 'Outside composer' });
diff --git a/ui/src/components/workbench/Composer.tsx b/ui/src/components/workbench/Composer.tsx
index 56eedb491..7ae855a73 100644
--- a/ui/src/components/workbench/Composer.tsx
+++ b/ui/src/components/workbench/Composer.tsx
@@ -521,6 +521,7 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
target: HTMLElement;
previousFocus: Element | null;
} | null>(null);
+ const voiceTextareaCaretRef = useRef<{ start: number; end: number } | null>(null);
const finishVoiceControlRef = useRef<HTMLButtonElement | null>(null);
const recordingTickerRef = useRef<ReturnType<typeof setInterval> | null>(null);
const unmountedRef = useRef(false);
@@ -811,6 +812,7 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
setRealtimeAnnouncement('');
onDraftChange?.(result.text);
const caret = session.insertion.start + result.insertion.length;
+ voiceTextareaCaretRef.current = { start: caret, end: caret };
requestAnimationFrame(() => textareaRef.current?.setSelectionRange(caret, caret));
return true;
}, [onDraftChange, useMentions]);
@@ -823,6 +825,10 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
}
const preview = session.previewInsertion;
session.previewInsertion = undefined;
+ voiceTextareaCaretRef.current = {
+ start: session.insertion.start,
+ end: session.insertion.end,
+ };
if (preview === undefined || valueRef.current !== preview.text) return;
valueRef.current = session.insertion.text;
setValue(session.insertion.text);
@@ -1069,6 +1075,12 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
return;
}
const insertion = capturedInsertion ?? captureVoiceInsertion();
+ if (!useMentions) {
+ voiceTextareaCaretRef.current = {
+ start: insertion.start,
+ end: insertion.end,
+ };
+ }
const reply = readLatestAgentReply?.();
recordingStartRef.current = true;
setRecordingStarting(true);
@@ -1383,9 +1395,22 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
|| !activeElement.isConnected
)
) {
- target.focus({ preventScroll: true });
+ if (useMentions) {
+ // A DOM focus call is not enough for Lexical: it can leave the editor's
+ // internal selection detached from the contenteditable after a voice
+ // update completed while the editor was blurred. Restore through the
+ // editor bridge so the caret committed by commitVoicePreview is synced
+ // back to the DOM before the next Enter key.
+ mentionRef.current?.focus();
+ } else {
+ target.focus({ preventScroll: true });
+ if (target instanceof HTMLTextAreaElement && voiceTextareaCaretRef.current !== null) {
+ const { start, end } = voiceTextareaCaretRef.current;
+ target.setSelectionRange(start, end);
+ }
+ }
}
- }, [disabled, voiceDraftReadOnly]);
+ }, [disabled, useMentions, voiceDraftReadOnly]);
const handleVoiceShortcut = useCallback((event: KeyboardEvent, allowStart: boolean): boolean => {
if (
diff --git a/ui/src/components/workbench/MentionEditor.tsx b/ui/src/components/workbench/MentionEditor.tsx
index ab5f9101a..e56a7ba3f 100644
--- a/ui/src/components/workbench/MentionEditor.tsx
+++ b/ui/src/components/workbench/MentionEditor.tsx
@@ -3,6 +3,7 @@ import {
useCallback,
useEffect,
useImperativeHandle,
+ useLayoutEffect,
useRef,
useState,
type Ref,
@@ -292,6 +293,28 @@ function serializedOffsetForPoint(point: PointType): number | null {
}
function serializedPointAtOffset(target: number): SerializedPoint | null {
+ const pointAtEnd = (node: LexicalNode): SerializedPoint | null => {
+ if ($isTextNode(node)) {
+ return {
+ key: node.getKey(),
+ offset: node.getTextContentSize(),
+ type: 'text',
+ };
+ }
+ if ($isElementNode(node)) {
+ const last = node.getLastDescendant();
+ if ($isTextNode(last)) {
+ return {
+ key: last.getKey(),
+ offset: last.getTextContentSize(),
+ type: 'text',
+ };
+ }
+ return { key: node.getKey(), offset: node.getChildrenSize(), type: 'element' };
+ }
+ return null;
+ };
+
const visit = (node: LexicalNode, base: number, rootLevel = false): SerializedPoint | null => {
if ($isTextNode(node)) {
const serialized = nodeToMarkerText(node, []);
@@ -325,13 +348,8 @@ function serializedPointAtOffset(target: number): SerializedPoint | null {
return visit(children[index], offset);
}
if (target === childEnd) {
- if (rootLevel) {
- const child = children[index];
- return $isElementNode(child)
- ? { key: child.getKey(), offset: child.getChildrenSize(), type: 'element' }
- : visit(child, offset);
- }
- return { key: node.getKey(), offset: index + 1, type: 'element' };
+ return pointAtEnd(children[index])
+ ?? { key: node.getKey(), offset: index + 1, type: 'element' };
}
offset = childEnd;
if (rootLevel && index < children.length - 1) {
@@ -405,7 +423,10 @@ function EnterSubmitPlugin({
function EditablePlugin({ disabled }: { disabled: boolean }) {
const [editor] = useLexicalComposerContext();
- useEffect(() => {
+ // Voice completion restores focus in a sibling effect as soon as the draft
+ // becomes editable again. A passive effect leaves the contenteditable
+ // disabled for that render, so the browser ignores the focus request.
+ useLayoutEffect(() => {
editor.setEditable(!disabled);
}, [editor, disabled]);
return null;
@@ -519,7 +540,11 @@ function BootstrapPlugin({
};
return {
- focus: () => editor.focus(),
+ focus: () => {
+ const root = editor.getRootElement();
+ root?.focus({ preventScroll: true });
+ editor.focus();
+ },
clear: () => {
voicePreviewRef.current = null;
editor.update(() => {