π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
feat(voice): define the offscreen microphone message contract
Luis Ortiz817a4bbb0262
diff --git a/src/lib/__tests__/voice-messages.test.ts b/src/lib/__tests__/voice-messages.test.tsnew file mode 100644index 0000000..ac5b903--- /dev/null+++ b/src/lib/__tests__/voice-messages.test.ts@@ -0,0 +1,61 @@+import { describe, expect, it } from 'vitest';+import {+ isVoiceMessageFor,+ VOICE_BACKGROUND_TARGET,+ VOICE_OFFSCREEN_TARGET,+ VoiceMessage,+ type VoiceStartRequest,+ voiceMessage,+} from '../voice-messages';++describe('voiceMessage', () => {+ it('stamps a timestamp so webext-core ignores the message instead of throwing', () => {+ const message = voiceMessage<VoiceStartRequest>({+ type: VoiceMessage.VOICE_START,+ target: VOICE_OFFSCREEN_TARGET,+ });+ expect(typeof message.timestamp).toBe('number');+ });++ it('keeps the fields it was given', () => {+ const message = voiceMessage<VoiceStartRequest>({+ type: VoiceMessage.VOICE_START,+ target: VOICE_OFFSCREEN_TARGET,+ deviceId: 'mic-1',+ });+ expect(message.type).toBe('VOICE_START');+ expect(message.target).toBe(VOICE_OFFSCREEN_TARGET);+ expect(message.deviceId).toBe('mic-1');+ });+});++describe('isVoiceMessageFor', () => {+ it('accepts a voice message addressed to the given target', () => {+ const message = voiceMessage<VoiceStartRequest>({+ type: VoiceMessage.VOICE_START,+ target: VOICE_OFFSCREEN_TARGET,+ });+ expect(isVoiceMessageFor(VOICE_OFFSCREEN_TARGET, message)).toBe(true);+ });++ it('rejects a voice message addressed to another target', () => {+ const message = voiceMessage<VoiceStartRequest>({+ type: VoiceMessage.VOICE_START,+ target: VOICE_OFFSCREEN_TARGET,+ });+ expect(isVoiceMessageFor(VOICE_BACKGROUND_TARGET, message)).toBe(false);+ });++ it('rejects messages from other protocols', () => {+ expect(isVoiceMessageFor(VOICE_OFFSCREEN_TARGET, { type: 'getState', timestamp: 1 })).toBe(false);+ expect(isVoiceMessageFor(VOICE_OFFSCREEN_TARGET, { target: VOICE_OFFSCREEN_TARGET })).toBe(false);+ expect(isVoiceMessageFor(VOICE_OFFSCREEN_TARGET, null)).toBe(false);+ expect(isVoiceMessageFor(VOICE_OFFSCREEN_TARGET, 'VOICE_START')).toBe(false);+ });++ it('rejects an inherited property masquerading as a message type', () => {+ expect(isVoiceMessageFor(VOICE_OFFSCREEN_TARGET, { type: 'constructor', target: VOICE_OFFSCREEN_TARGET })).toBe(+ false,+ );+ });+});diff --git a/src/lib/voice-messages.ts b/src/lib/voice-messages.tsnew file mode 100644index 0000000..3ad6b0f--- /dev/null+++ b/src/lib/voice-messages.ts@@ -0,0 +1,146 @@+import type { NarrationResult } from '@/core/capture/voice/types';++export const VOICE_OFFSCREEN_TARGET = 'mimik-offscreen';+export const VOICE_BACKGROUND_TARGET = 'mimik-background';+export const VOICE_SIDEPANEL_TARGET = 'mimik-sidepanel';++export type VoiceTarget =+ | typeof VOICE_OFFSCREEN_TARGET+ | typeof VOICE_BACKGROUND_TARGET+ | typeof VOICE_SIDEPANEL_TARGET;++export const VoiceMessage = {+ VOICE_START: 'VOICE_START',+ VOICE_STOP: 'VOICE_STOP',+ VOICE_ABORT: 'VOICE_ABORT',+ VOICE_STATUS: 'VOICE_STATUS',+ VOICE_PERMISSION_QUERY: 'VOICE_PERMISSION_QUERY',+ VOICE_EPOCH: 'VOICE_EPOCH',+ VOICE_LEVEL: 'VOICE_LEVEL',+ VOICE_ERROR: 'VOICE_ERROR',+ VOICE_PERMISSION_RESULT: 'VOICE_PERMISSION_RESULT',+} as const;++export type VoiceMessageType = (typeof VoiceMessage)[keyof typeof VoiceMessage];++export type VoiceErrorReason =+ | 'permission-denied'+ | 'no-device'+ | 'no-audio'+ | 'not-recording'+ | 'already-recording'+ | 'missing-api-key'+ | 'stream-ended'+ | 'unsupported'+ | 'unknown';++export interface VoiceEnvelope {+ type: VoiceMessageType;+ target: VoiceTarget;+ timestamp: number;+}++export interface VoiceStartRequest extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_START;+ target: typeof VOICE_OFFSCREEN_TARGET;+ deviceId?: string;+}++export type VoiceStartResponse =+ | { started: true; deviceId: string | null; usedFallbackDevice: boolean }+ | { started: false; reason: VoiceErrorReason; error: string };++export interface VoiceStepMark {+ stepId: string;+ timestamp: number;+}++export interface VoiceStopRequest extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_STOP;+ target: typeof VOICE_OFFSCREEN_TARGET;+ steps: VoiceStepMark[];+}++export type VoiceStopResponse =+ | { ok: true; result: NarrationResult; audioEpochMs: number; durationSeconds: number }+ | { ok: false; reason: VoiceErrorReason; error: string };++export interface VoiceAbortRequest extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_ABORT;+ target: typeof VOICE_OFFSCREEN_TARGET;+}++export interface VoiceAbortResponse {+ ok: true;+}++export interface VoiceStatusRequest extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_STATUS;+ target: typeof VOICE_OFFSCREEN_TARGET;+}++export interface VoiceStatusResponse {+ recording: boolean;+ audioEpochMs: number | null;+ sampleRate: number;+ samples: number;+ durationSeconds: number;+}++export type VoicePermissionState = 'granted' | 'denied' | 'prompt' | 'unknown';++export interface VoicePermissionQueryRequest extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_PERMISSION_QUERY;+ target: typeof VOICE_OFFSCREEN_TARGET;+}++export interface VoicePermissionQueryResponse {+ state: VoicePermissionState;+}++export interface VoiceEpochEvent extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_EPOCH;+ target: typeof VOICE_BACKGROUND_TARGET;+ audioEpochMs: number;+}++export interface VoiceLevelEvent extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_LEVEL;+ target: typeof VOICE_SIDEPANEL_TARGET;+ level: number;+ speaking: boolean;+}++export interface VoiceErrorEvent extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_ERROR;+ target: typeof VOICE_BACKGROUND_TARGET;+ reason: VoiceErrorReason;+ error: string;+}++export interface VoicePermissionResultEvent extends VoiceEnvelope {+ type: typeof VoiceMessage.VOICE_PERMISSION_RESULT;+ target: typeof VOICE_BACKGROUND_TARGET;+ state: 'granted' | 'denied';+}++export type VoiceRequest =+ | VoiceStartRequest+ | VoiceStopRequest+ | VoiceAbortRequest+ | VoiceStatusRequest+ | VoicePermissionQueryRequest;++export type VoiceEvent = VoiceEpochEvent | VoiceLevelEvent | VoiceErrorEvent | VoicePermissionResultEvent;++export function voiceMessage<T extends VoiceEnvelope>(message: Omit<T, 'timestamp'>): T {+ return { ...message, timestamp: Date.now() } as T;+}++export function isVoiceMessageFor(target: VoiceTarget, message: unknown): message is VoiceEnvelope {+ if (typeof message !== 'object' || message === null) return false;+ const candidate = message as Partial<VoiceEnvelope>;+ return (+ candidate.target === target && typeof candidate.type === 'string' && Object.hasOwn(VoiceMessage, candidate.type)+ );+}