Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(dictation): drop OpenAI copy and smooth the mobile volume meter (#7934)

Wolf Mermelsteinb0df9228fd04
diff --git a/apps/web/src/features/dictation/browser/audio-recorder.test.ts b/apps/web/src/features/dictation/browser/audio-recorder.test.ts
index 6a3ee0cc63..97f672af08 100644
--- a/apps/web/src/features/dictation/browser/audio-recorder.test.ts
+++ b/apps/web/src/features/dictation/browser/audio-recorder.test.ts
@@ -4,6 +4,7 @@ import {
MAX_RECORDING_MS,
RECORDING_CHUNK_MS,
} from '../core/recording';
+import { VOLUME_INTERVAL_MS } from '../core/volume';
import {
AudioRecorder,
AudioRecorderManager,
@@ -12,7 +13,9 @@ import {
let now = 0;
let deadline: (() => void) | undefined;
+let sampleLevel: (() => void) | undefined;
const cancelDeadline = vi.fn();
+const stopSampling = vi.fn();
const clock: RecordingClock = {
now: () => now,
schedule: (callback, delay) => {
@@ -20,6 +23,11 @@ const clock: RecordingClock = {
deadline = callback;
return cancelDeadline;
},
+ repeat: (callback, interval) => {
+ expect(interval).toBe(VOLUME_INTERVAL_MS);
+ sampleLevel = callback;
+ return stopSampling;
+ },
};
let manager: AudioRecorderManager;
@@ -101,6 +109,7 @@ beforeEach(() => {
frequencyByte = 0;
now = 0;
deadline = undefined;
+ sampleLevel = undefined;
manager = new AudioRecorderManager(clock);
frames.length = 0;
FakeMediaRecorder.latest = undefined;
@@ -130,20 +139,48 @@ describe('AudioRecorder', () => {
expect(AudioRecorder.isSupported()).toBe(false);
});
- it('collects chunks, meters each one, and delivers the recording on stop', async () => {
+ it('meters on its own cadence, independent of chunk delivery', async () => {
frequencyByte = 255;
const sinks = callbacks();
const recorder = manager.createSession(sinks);
await recorder.start();
const media = latest();
- expect(media.timeslice).toBe(RECORDING_CHUNK_MS);
- expect(media.mimeType).toBe('audio/mp4');
media.chunk('one');
+ media.chunk('two');
+ expect(sinks.onLevel).not.toHaveBeenCalled();
+
+ sampleLevel?.();
frequencyByte = 0;
for (const frame of frames.splice(0)) frame(0);
+ sampleLevel?.();
+ sampleLevel?.();
+ expect(sinks.onLevel.mock.calls.map(([level]) => level)).toEqual([1, 0, 0]);
+
+ recorder.stop();
+ sampleLevel?.();
+ expect(sinks.onLevel).toHaveBeenCalledTimes(3);
+ media.finish();
+ expect(stopSampling).toHaveBeenCalledOnce();
+ });
+
+ it('stops metering when cancelled', async () => {
+ const recorder = manager.createSession(callbacks());
+ await recorder.start();
+ recorder.cancel();
+ expect(stopSampling).toHaveBeenCalledOnce();
+ });
+
+ it('collects chunks and delivers the recording on stop', async () => {
+ const sinks = callbacks();
+ const recorder = manager.createSession(sinks);
+ await recorder.start();
+ const media = latest();
+ expect(media.timeslice).toBe(RECORDING_CHUNK_MS);
+ expect(media.mimeType).toBe('audio/mp4');
+
+ media.chunk('one');
media.chunk('two');
- expect(sinks.onLevel.mock.calls.map(([level]) => level)).toEqual([1, 0]);
recorder.stop();
expect(sinks.onRecording).not.toHaveBeenCalled();
@@ -213,9 +250,6 @@ describe('AudioRecorder', () => {
expect(sinks.onRecording).not.toHaveBeenCalled();
media.finish();
expect(sinks.onRecording).toHaveBeenCalledOnce();
- expect(sinks.onLevel).toHaveBeenCalledTimes(2);
- media.chunk('late');
- expect(sinks.onLevel).toHaveBeenCalledTimes(2);
});
it('stops at its deadline even when no chunks arrive', async () => {
diff --git a/apps/web/src/features/dictation/browser/audio-recorder.ts b/apps/web/src/features/dictation/browser/audio-recorder.ts
index 2c92c81498..85416adf6c 100644
--- a/apps/web/src/features/dictation/browser/audio-recorder.ts
+++ b/apps/web/src/features/dictation/browser/audio-recorder.ts
@@ -7,6 +7,7 @@ import {
RECORDING_CHUNK_MS,
type RecorderHandle,
} from '../core/recording';
+import { VOLUME_INTERVAL_MS } from '../core/volume';
const MIME_TYPES = [
'audio/webm;codecs=opus',
@@ -17,10 +18,11 @@ const AUDIO_BITS_PER_SECOND = 64_000;
/** Stop early enough that the final chunk still fits under the server limit. */
const SIZE_HEADROOM_BYTES = 64 * 1024;
-/** A monotonic clock and cancellable deadline, supplied explicitly in tests. */
+/** A monotonic clock and cancellable timers, supplied explicitly in tests. */
export interface RecordingClock {
now(): number;
schedule(callback: () => void, delay: number): () => void;
+ repeat(callback: () => void, interval: number): () => void;
}
const browserClock: RecordingClock = {
@@ -29,6 +31,10 @@ const browserClock: RecordingClock = {
const timeout = window.setTimeout(callback, delay);
return () => window.clearTimeout(timeout);
},
+ repeat: (callback, interval) => {
+ const timer = window.setInterval(callback, interval);
+ return () => window.clearInterval(timer);
+ },
};
type State = 'idle' | 'starting' | 'recording' | 'stopping' | 'done';
@@ -97,8 +103,8 @@ export class AudioRecorder implements RecorderHandle {
mimeType,
audioBitsPerSecond: AUDIO_BITS_PER_SECOND,
});
- const readLevel = this.startMeter(stream);
- recorder.ondataavailable = ({ data }) => this.onChunk(data, readLevel());
+ this.startMeter(stream);
+ recorder.ondataavailable = ({ data }) => this.onChunk(data);
recorder.onerror = () =>
this.fail(new Error('Audio recording failed. Please try again.'));
recorder.onstop = () => this.onStop();
@@ -137,15 +143,22 @@ export class AudioRecorder implements RecorderHandle {
this.callbacks.onInterrupted();
}
- private startMeter(stream: MediaStream): () => number {
- let amplitude = () => 0;
+ /**
+ * Sample on a fixed cadence: iOS Safari delivers MediaRecorder chunks late
+ * and in bursts, so chunk arrival is too uneven to pace the timeline.
+ */
+ private startMeter(stream: MediaStream) {
this.releaseMeter = createRoot((dispose) => {
// Library level is 0..100; the analyser is never routed to speakers.
const [level] = createAmplitudeFromStream(stream);
- amplitude = level;
- return dispose;
+ const stopSampling = this.clock.repeat(() => {
+ if (this.state === 'recording') this.callbacks.onLevel?.(level() / 100);
+ }, VOLUME_INTERVAL_MS);
+ return () => {
+ stopSampling();
+ dispose();
+ };
});
- return () => amplitude() / 100;
}
private stopAtLimit() {
@@ -154,7 +167,7 @@ export class AudioRecorder implements RecorderHandle {
this.stop();
}
- private onChunk(data: Blob, level: number) {
+ private onChunk(data: Blob) {
if (!this.recording) return;
// Delayed/final chunks can exceed the headroom; never retain or upload them.
if (this.size + data.size > MAX_RECORDING_BYTES) {
@@ -166,7 +179,6 @@ export class AudioRecorder implements RecorderHandle {
if (data.size) this.chunks.push(data);
this.size += data.size;
if (this.state !== 'recording') return;
- this.callbacks.onLevel?.(level);
if (
this.clock.now() - this.startedAt >= MAX_RECORDING_MS ||
this.size >= MAX_RECORDING_BYTES - SIZE_HEADROOM_BYTES
diff --git a/apps/web/src/features/dictation/core/recording.ts b/apps/web/src/features/dictation/core/recording.ts
index 1f950f5847..82d0ef6521 100644
--- a/apps/web/src/features/dictation/core/recording.ts
+++ b/apps/web/src/features/dictation/core/recording.ts
@@ -1,15 +1,15 @@
import type { Span } from '@macro-inc/observability';
-import { VOLUME_INTERVAL_MS, type VolumeLevel } from './volume';
+import type { VolumeLevel } from './volume';
/** Mirrors the server body limit for `/dictation/transcribe`. */
export const MAX_RECORDING_BYTES = 8 * 1024 * 1024;
/** Leave room for codec padding below the server's five-minute limit. */
export const MAX_RECORDING_MS = 5 * 60 * 1000 - 1_000;
/** Requested chunk cadence; browsers may deliver chunks late. */
-export const RECORDING_CHUNK_MS = VOLUME_INTERVAL_MS;
+export const RECORDING_CHUNK_MS = 200;
export type AudioRecorderCallbacks = {
- /** Microphone level for the chunk that just finished, while recording. */
+ /** Microphone level, sampled every `VOLUME_INTERVAL_MS` while recording. */
onLevel?: (level: VolumeLevel) => void;
/** Final encoded audio, including the browser's last data event. */
onRecording: (audio: Blob) => void;
diff --git a/apps/web/src/features/dictation/core/volume.ts b/apps/web/src/features/dictation/core/volume.ts
index 4a79cf0d03..b7ab691a8c 100644
--- a/apps/web/src/features/dictation/core/volume.ts
+++ b/apps/web/src/features/dictation/core/volume.ts
@@ -1,5 +1,5 @@
/** Requested sampling cadence; keep a bounded history even during long recordings. */
-export const VOLUME_INTERVAL_MS = 200;
+export const VOLUME_INTERVAL_MS = 80;
export const MAX_VOLUME_SAMPLES = 512;
/** Normalized 0..1 microphone level for one interval. */
diff --git a/apps/web/src/features/dictation/primitives/create-recorded-dictation.ts b/apps/web/src/features/dictation/primitives/create-recorded-dictation.ts
index eec6ef6215..83cc096323 100644
--- a/apps/web/src/features/dictation/primitives/create-recorded-dictation.ts
+++ b/apps/web/src/features/dictation/primitives/create-recorded-dictation.ts
@@ -71,7 +71,7 @@ export function createRecordedDictation(options: {
const current = new AbortController();
upload = current;
setPhase('finishing');
- setMessage('Transcribing with OpenAI…');
+ setMessage('');
trace?.event('dictation.upload_started', {
attempt: ++attempt,
audioBytes: blob.size,
@@ -120,11 +120,7 @@ export function createRecordedDictation(options: {
return;
}
setPhase('review');
- setMessage(
- limitReached
- ? 'Recording limit reached. Select the checkmark to transcribe with OpenAI.'
- : 'Recording stopped. Select the checkmark to transcribe with OpenAI.'
- );
+ setMessage(limitReached ? 'Recording limit reached.' : '');
};
const start = async () => {
@@ -230,7 +226,7 @@ export function createRecordedDictation(options: {
disabled: () => phase() !== 'idle',
label: () =>
options.supported
- ? 'Start dictation with OpenAI Whisper'
+ ? 'Start dictation'
: 'Dictation is unavailable in this browser',
};
}
diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.md
index becb2d2439..aefb0d96ef 100644
--- a/docs/AGENT_GUIDE/ai-chat.md
+++ b/docs/AGENT_GUIDE/ai-chat.md
@@ -754,7 +754,7 @@ preserve the submitted tool selection.
## Composer anatomy (a11y)
AI chat (including Home and doc-scoped chat) and agent session composers have
-a **Start dictation with OpenAI Whisper** microphone beside Send. It records
+a **Start dictation** microphone beside Send. It records
in memory and uploads to the
authenticated `/dictation/transcribe` storage endpoint only on confirmation.
Whisper is available on all plans without consuming chat credits; its server
@@ -764,13 +764,15 @@ no browser speech-recognition or language-pack installation flows.
While dictating, a scrolling microphone-volume timeline and **Cancel dictation** / **Use dictation**
replace the composer controls. Cancel (or Escape) preserves the original draft.
-Bars sample microphone volume as recording chunks arrive (normally every 200ms): silence stays dotted, louder speech
+Bars sample microphone volume on a fixed timer (every 80ms), independent of recording chunks: silence stays dotted, louder speech
creates taller bars, and earlier levels move left without changing height.
Volume analysis stays on-device and stops on confirm, cancel, error, or close.
Use dictation stops recording, waits for Whisper, and appends plain text to
the draft without sending it. Existing rich text and attachments remain intact.
If the browser stops listening on its own, **Ready** waits for confirmation.
While **Finishing…**, the checkmark is disabled and Cancel remains available.
+No provider name or transcription hint appears below the composer; only the
+recording limit and failures do.
Mobile chat stays expanded when focus moves into dictation controls.
Starting dictation in another composer releases the previous session without
moving focus back to it. Closing the composer releases the microphone. Capture failures