Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

refactor: read desktop recording data from the state machine, share the state update and type the desktop requests

Luis Ortiz4df3ece3b2ae
diff --git a/apps/desktop/scripts/check-overlay.ts b/apps/desktop/scripts/check-overlay.ts
index 9c1fdde..08bfc39 100644
--- a/apps/desktop/scripts/check-overlay.ts
+++ b/apps/desktop/scripts/check-overlay.ts
@@ -1,7 +1,8 @@
import { join } from 'node:path';
import { CaptureState, captureMachine } from '@mimik/core/capture/machine';
import { app, BrowserWindow, globalShortcut, screen, webContents } from 'electron';
-import { clampToDisplays, defaultRegion, loadRegion, type Region, saveRegion } from '../src/main/capture/region';
+import type { Rect } from '@mimik/core/rect';
+import { clampToDisplays, defaultRegion, loadRegion, saveRegion } from '../src/main/capture/region';
import type { CaptureMode } from '../src/main/capture/settings';
import { CaptureOverlay } from '../src/main/overlay';
import { bindShortcuts, sameShortcuts, shortcutMap, unbindShortcuts } from '../src/main/shortcuts';
@@ -62,15 +63,18 @@ app.whenReady().then(async () => {
const pause = () => capture.send({ type: 'PAUSE_CAPTURE', reason: 'manual' });
const overlay: CaptureOverlay = new CaptureOverlay(
capture,
- (command) => commands.push(command),
+ (command) => {
+ commands.push(command);
+ if (command === 'stop' || command === 'discard') overlay.reset();
+ },
() => mode,
{
- introFrame: async (): Promise<Region> => overlay.region,
+ introFrame: async (): Promise<Rect> => overlay.region,
shortcuts: () => ({ startStop: 'Alt+Shift+R', capture: 'Alt+Shift+C' }),
},
);
- const stored: Region = { x: 120, y: 90, width: 640, height: 400 };
+ const stored: Rect = { x: 120, y: 90, width: 640, height: 400 };
const written = saveRegion(stored);
const read = loadRegion();
check(
diff --git a/apps/desktop/scripts/check-pipeline.ts b/apps/desktop/scripts/check-pipeline.ts
index 31a7ff2..9458b6e 100644
--- a/apps/desktop/scripts/check-pipeline.ts
+++ b/apps/desktop/scripts/check-pipeline.ts
@@ -30,16 +30,11 @@ import {
normaliseSettings,
saveSettings,
} from '../src/main/capture/settings';
-import type { Capture, Rect } from '../src/main/capture/screenshot';
+import type { Rect } from '@mimik/core/rect';
+import type { Capture } from '../src/main/capture/screenshot';
const MAC = process.platform === 'darwin';
-interface CheckResult {
- name: string;
- ok: boolean;
- detail: string;
-}
-
function bail(error: unknown): never {
process.stdout.write(`FAIL check-pipeline aborted: ${error instanceof Error ? error.stack : String(error)}\n`);
app.exit(1);
@@ -90,14 +85,14 @@ app.whenReady().then(async () => {
{ grab: async () => syntheticDisplay, settings: () => settings, lookup: () => Promise.resolve(null) },
);
- activeGuide = await ask<string>(win.webContents, 'mimik:capture:createGuide');
+ activeGuide = await ask(win.webContents, 'mimik:capture:createGuide', {});
const guideId = activeGuide;
await recorder.capture({ x: region.x + 120, y: region.y + 90 });
await recorder.capture({ x: region.x + 400, y: region.y + 300 });
- const results = await ask<CheckResult[]>(win.webContents, 'mimik:check:verify', guideId, 60_000);
+ const results = await ask(win.webContents, 'mimik:check:verify', guideId, 60_000);
- const blindGuide = await ask<string>(win.webContents, 'mimik:capture:createGuide');
+ const blindGuide = await ask(win.webContents, 'mimik:capture:createGuide', {});
let blindRequest: CaptureRequest | null = null;
const blind = new DesktopRecorder(
() => region,
@@ -109,7 +104,7 @@ app.whenReady().then(async () => {
{ grab: () => Promise.reject(new Error('display gone')), settings: () => settings, lookup: () => Promise.resolve(null) },
);
await blind.capture({ x: region.x + 60, y: region.y + 60 });
- const blindSteps = (await ask<string[] | null>(win.webContents, 'mimik:check:steps', blindGuide, 20_000)) ?? [];
+ const blindSteps = (await ask(win.webContents, 'mimik:check:steps', blindGuide, 20_000)) ?? [];
await ask(win.webContents, 'mimik:check:cleanup', [blindGuide], 20_000).catch(() => undefined);
results.push({
name: 'a failed screenshot still writes the step, without a picture',
@@ -637,7 +632,7 @@ app.whenReady().then(async () => {
detail: 'a real rectangle wins; no element, one covering the frame, or one overflowing it falls back to the click box',
});
- activeGuide = await ask<string>(win.webContents, 'mimik:capture:createGuide');
+ activeGuide = await ask(win.webContents, 'mimik:capture:createGuide', {});
settings = { ...REGION_MODE, screenshotDelayMs: 400 };
const before = Date.now();
@@ -649,8 +644,8 @@ app.whenReady().then(async () => {
detail: `${elapsed} ms for a 400 ms delay`,
});
- const probeSrc = await ask<string | null>(win.webContents, 'mimik:check:screenshotSrc', activeGuide, 20_000);
- const defaultLogo = await ask<boolean>(win.webContents, 'mimik:check:defaultLogo', undefined, 10_000);
+ const probeSrc = await ask(win.webContents, 'mimik:check:screenshotSrc', activeGuide, 20_000);
+ const defaultLogo = await ask(win.webContents, 'mimik:check:defaultLogo', undefined, 10_000);
results.push({
name: 'exports can load the default logo',
ok: defaultLogo === true,
@@ -666,7 +661,7 @@ app.whenReady().then(async () => {
await app_.loadFile(join(__dirname, '../renderer/index.html'));
let wired = 'no reply';
try {
- const id = await ask<string>(app_.webContents, 'mimik:capture:createGuide', {}, 20_000);
+ const id = await ask(app_.webContents, 'mimik:capture:createGuide', {}, 20_000);
wired = typeof id === 'string' && id.length > 0 ? id : `unexpected reply ${JSON.stringify(id)}`;
await ask(app_.webContents, 'mimik:check:cleanup', [id], 20_000).catch(() => undefined);
} catch (error) {
@@ -680,9 +675,9 @@ app.whenReady().then(async () => {
let titled = 'no reply';
try {
- const id = await ask<string>(app_.webContents, 'mimik:capture:createGuide', {}, 20_000);
+ const id = await ask(app_.webContents, 'mimik:capture:createGuide', {}, 20_000);
await ask(app_.webContents, 'mimik:capture:finishGuide', id, 20_000);
- titled = (await ask<string | null>(win.webContents, 'mimik:check:title', id, 20_000)) ?? 'no guide';
+ titled = (await ask(win.webContents, 'mimik:check:title', id, 20_000)) ?? 'no guide';
await ask(app_.webContents, 'mimik:check:cleanup', [id], 20_000).catch(() => undefined);
} catch (error) {
titled = error instanceof Error ? error.message : String(error);
@@ -705,16 +700,16 @@ app.whenReady().then(async () => {
devicePixelRatio: 1,
},
}, 20_000);
- const target = await ask<string>(app_.webContents, 'mimik:capture:createGuide', { staging: false }, 20_000);
+ const target = await ask(app_.webContents, 'mimik:capture:createGuide', { staging: false }, 20_000);
await labelled(target, 'First');
await labelled(target, 'Third');
- const before = await ask<string | null>(win.webContents, 'mimik:check:title', target, 20_000);
- const staging = await ask<string>(app_.webContents, 'mimik:capture:createGuide', { staging: true }, 20_000);
+ const before = await ask(win.webContents, 'mimik:check:title', target, 20_000);
+ const staging = await ask(app_.webContents, 'mimik:capture:createGuide', { staging: true }, 20_000);
await labelled(staging, 'Second');
await ask(app_.webContents, 'mimik:capture:mergeGuideInto', { guideId: staging, insertTargetGuideId: target, insertAtIndex: 1 }, 20_000);
- const order = (await ask<string[] | null>(win.webContents, 'mimik:check:steps', target, 20_000)) ?? [];
- const left = await ask<string[] | null>(win.webContents, 'mimik:check:steps', staging, 20_000);
- const after = await ask<string | null>(win.webContents, 'mimik:check:title', target, 20_000);
+ const order = (await ask(win.webContents, 'mimik:check:steps', target, 20_000)) ?? [];
+ const left = await ask(win.webContents, 'mimik:check:steps', staging, 20_000);
+ const after = await ask(win.webContents, 'mimik:check:title', target, 20_000);
merged = `${order.map((text) => text.match(/First|Second|Third/)?.[0] ?? text).join(', ')}; staging ${left === null ? 'gone' : 'left behind'}; title ${after === before ? 'kept' : `changed to ${after}`}`;
await ask(app_.webContents, 'mimik:check:cleanup', [target, staging], 20_000).catch(() => undefined);
} catch (error) {
@@ -728,12 +723,12 @@ app.whenReady().then(async () => {
let discarded = 'no reply';
try {
- const kept = await ask<string>(app_.webContents, 'mimik:capture:createGuide', {}, 20_000);
- const staging = await ask<string>(app_.webContents, 'mimik:capture:createGuide', { staging: true }, 20_000);
+ const kept = await ask(app_.webContents, 'mimik:capture:createGuide', {}, 20_000);
+ const staging = await ask(app_.webContents, 'mimik:capture:createGuide', { staging: true }, 20_000);
await ask(app_.webContents, 'mimik:capture:discardRecording', { guideId: kept, staging: false }, 20_000);
await ask(app_.webContents, 'mimik:capture:discardRecording', { guideId: staging, staging: true }, 20_000);
- const trashed = await ask<boolean | null>(win.webContents, 'mimik:check:trashed', kept, 20_000);
- const left = await ask<boolean | null>(win.webContents, 'mimik:check:trashed', staging, 20_000);
+ const trashed = await ask(win.webContents, 'mimik:check:trashed', kept, 20_000);
+ const left = await ask(win.webContents, 'mimik:check:trashed', staging, 20_000);
discarded = `${trashed ? 'in Trash' : 'not in Trash'}; staging ${left === null ? 'gone' : 'left behind'}`;
await ask(app_.webContents, 'mimik:check:cleanup', [kept, staging], 20_000).catch(() => undefined);
} catch (error) {
@@ -761,7 +756,7 @@ app.whenReady().then(async () => {
detail: String(fetched),
});
- const kept = await ask<string[]>(win.webContents, 'mimik:check:screenshotIds', undefined, 20_000);
+ const kept = await ask(win.webContents, 'mimik:check:screenshotIds', undefined, 20_000);
const swept = sweepScreenshots(kept);
results.push({
name: 'deleted guides leave no files',
diff --git a/apps/desktop/src/main/__tests__/ask.test.ts b/apps/desktop/src/main/__tests__/ask.test.ts
index 2d843f3..166fa1d 100644
--- a/apps/desktop/src/main/__tests__/ask.test.ts
+++ b/apps/desktop/src/main/__tests__/ask.test.ts
@@ -21,11 +21,11 @@ function renderer(reply: unknown): WebContents {
describe('ask', () => {
it('resolves with what the renderer answered', async () => {
- await expect(ask(renderer('guide-1'), 'mimik:capture:createGuide')).resolves.toBe('guide-1');
+ await expect(ask(renderer('guide-1'), 'mimik:capture:createGuide', {})).resolves.toBe('guide-1');
});
it('rejects when the renderer answered with the error its handler threw', async () => {
- await expect(ask(renderer({ error: 'QuotaExceededError' }), 'mimik:capture:createGuide')).rejects.toThrow(
+ await expect(ask(renderer({ error: 'QuotaExceededError' }), 'mimik:capture:createGuide', {})).rejects.toThrow(
'mimik:capture:createGuide: QuotaExceededError',
);
});
diff --git a/apps/desktop/src/main/ask.ts b/apps/desktop/src/main/ask.ts
index 5139452..bbdd679 100644
--- a/apps/desktop/src/main/ask.ts
+++ b/apps/desktop/src/main/ask.ts
@@ -1,8 +1,15 @@
import { ipcMain, type WebContents } from 'electron';
+import type { RendererRequest, RendererRequests } from './ipc';
let seq = 0;
-export function ask<T>(target: WebContents | null, channel: string, payload?: unknown, timeoutMs = 15_000): Promise<T> {
+export function ask<K extends RendererRequest>(
+ target: WebContents | null,
+ channel: K,
+ payload: RendererRequests[K][0],
+ timeoutMs = 15_000,
+): Promise<RendererRequests[K][1]> {
+ type T = RendererRequests[K][1];
if (!target || target.isDestroyed()) return Promise.reject(new Error(`${channel}: no renderer available`));
const replyChannel = `${channel}:reply:${++seq}`;
diff --git a/apps/desktop/src/main/capture/recorder.ts b/apps/desktop/src/main/capture/recorder.ts
index 334bedf..fbfd751 100644
--- a/apps/desktop/src/main/capture/recorder.ts
+++ b/apps/desktop/src/main/capture/recorder.ts
@@ -1,6 +1,8 @@
import { randomUUID } from 'node:crypto';
import type { CaptureImage } from '@mimik/core/capture/sink';
+import type { StepAction } from '@mimik/core/capture/step-action';
import type { ElementMeta } from '@mimik/core/guides/types';
+import type { Rect } from '@mimik/core/rect';
import { clipboard, screen } from 'electron';
import { shortcutLabel } from '../../renderer/lib/shortcut-label';
import { cursorPoint } from './displays';
@@ -17,8 +19,7 @@ import {
import { type FocusedWindowResult, focusedWindow, windowAt } from './focused-window';
import { type InputAction, InputHook, type KeyAction, type PointerAction } from './input-hook';
import { InputSession } from './input-session';
-import type { Region } from './region';
-import { type Frame, grabDisplay, type Rect } from './screenshot';
+import { type Frame, grabDisplay } from './screenshot';
import { writeScreenshot } from './screenshot-store';
import { type CaptureMode, type CaptureSettings, DEFAULT_CAPTURE_SETTINGS } from './settings';
@@ -67,7 +68,7 @@ export interface Point {
}
export interface CaptureRequest {
- action: string;
+ action: StepAction;
elementMeta: ElementMeta;
image?: CaptureImage;
inputValue?: string;
@@ -89,6 +90,7 @@ export interface RecorderHooks {
progress?: (fraction: number, ms: number) => void;
aimed?: (aim: { x: number; y: number; aspect: number }) => void;
saved?: (src: string) => void;
+ recording?: () => boolean;
}
export type RecorderStart = { ok: true } | { ok: false; reason: string; detail: string };
@@ -115,7 +117,7 @@ function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
-function inside(region: Region, point: Point): boolean {
+function inside(region: Rect, point: Point): boolean {
return (
point.x >= region.x &&
point.y >= region.y &&
@@ -128,12 +130,12 @@ export function isRepeatClick(previousAt: number | null, at: number): boolean {
return previousAt !== null && at - previousAt <= REPEAT_CLICK_MS;
}
-export function shouldCapture(settings: CaptureSettings, region: Region, point: Point): boolean {
+export function shouldCapture(settings: CaptureSettings, region: Rect, point: Point): boolean {
if (settings.captureMode !== 'area') return true;
return inside(region, point) || settings.keepClicksBeyondArea;
}
-export function frameFor(mode: CaptureMode, point: Point, region: Region, window: Rect | null): Rect {
+export function frameFor(mode: CaptureMode, point: Point, region: Rect, window: Rect | null): Rect {
const display = screen.getDisplayNearestPoint(point).bounds;
if (mode === 'screen') return display;
if (mode === 'window') return window && inside(window, point) ? window : display;
@@ -153,7 +155,7 @@ export function comboLabel(action: KeyAction, key: string, mac = process.platfor
return mac ? shortcutLabel([...held, key].join('+'), true) : [...held, key].join('+');
}
-export function clickAction(button: number): string {
+export function clickAction(button: number): StepAction {
return button === 2 ? 'auxclick' : 'click';
}
@@ -219,7 +221,6 @@ function centreOf(element: ScreenElement | null): Point | null {
export class DesktopRecorder {
private hook = new InputHook();
private queue: Promise<unknown> = Promise.resolve();
- private paused = false;
private running = false;
private lastClickAt: number | null = null;
private lastKey: { keycode: number; at: number } | null = null;
@@ -239,10 +240,11 @@ export class DesktopRecorder {
private readonly progress: (fraction: number, ms: number) => void;
private readonly aimed: (aim: { x: number; y: number; aspect: number }) => void;
private readonly saved: (src: string) => void;
+ private readonly recording: () => boolean;
private pending = 0;
constructor(
- private readonly region: () => Region,
+ private readonly region: () => Rect,
private readonly withHidden: <T>(fn: () => Promise<T>) => Promise<T>,
private readonly send: (request: CaptureRequest) => Promise<unknown>,
hooks: RecorderHooks = {},
@@ -262,6 +264,7 @@ export class DesktopRecorder {
this.progress = hooks.progress ?? (() => {});
this.aimed = hooks.aimed ?? (() => {});
this.saved = hooks.saved ?? (() => {});
+ this.recording = hooks.recording ?? (() => true);
}
async start(): Promise<RecorderStart> {
@@ -271,18 +274,12 @@ export class DesktopRecorder {
});
if (!started.ok) return { ok: false, reason: started.reason, detail: started.detail };
this.running = true;
- this.paused = false;
this.lastClickAt = null;
return { ok: true };
}
pause(): void {
this.finalizeInput();
- this.paused = true;
- }
-
- resume(): void {
- this.paused = false;
}
stop(): void {
@@ -290,11 +287,10 @@ export class DesktopRecorder {
this.hook.stop();
void this.release().catch(() => undefined);
this.running = false;
- this.paused = false;
}
get isRecording(): boolean {
- return this.running && !this.paused;
+ return this.running && this.recording();
}
onAction(action: InputAction): void {
@@ -313,7 +309,7 @@ export class DesktopRecorder {
this.clickAt(clickAction(action.button), point);
}
- private clickAt(action: string, point: Point): void {
+ private clickAt(action: StepAction, point: Point): void {
this.finalizeInput();
this.watchPassword();
const element = this.lookup(point);
@@ -525,7 +521,7 @@ export class DesktopRecorder {
}
private async write(
- action: string,
+ action: StepAction,
point: Point,
element: Promise<ScreenElement | null>,
taken: Promise<Frame>,
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index bfd565f..af5fef1 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises';
import { basename, join } from 'node:path';
import type { AiFailureUpdate } from '@mimik/core/capture/ai/errors';
import type { CaptureInsert } from '@mimik/core/capture/capture-insert';
+import { captureStateUpdate } from '@mimik/core/capture/capture-state-update';
import { isLive } from '@mimik/core/capture/is-live';
import { CaptureState, captureMachine } from '@mimik/core/capture/machine';
import { NARRATION_SETTLE_MS } from '@mimik/core/capture/voice/narration-settle-ms';
@@ -16,6 +17,7 @@ import { grabDisplay } from './capture/screenshot';
import { registerScreenshotProtocol, SCREENSHOT_SCHEME, sweepScreenshots } from './capture/screenshot-store';
import { type CaptureMode, type CaptureSettings, loadSettings, saveSettings } from './capture/settings';
import { mainI18n } from './i18n';
+import type { DesktopStateUpdate } from './ipc';
import { CaptureOverlay, type OverlayCommand, type OverlayNarration, type OverlayStep } from './overlay';
import {
askMicrophone,
@@ -36,7 +38,7 @@ let tray: Tray | null = null;
let overlay: CaptureOverlay | null = null;
let recorder: DesktopRecorder | null = null;
const capture = createActor(captureMachine).start();
-let currentGuideId: string | null = null;
+let ending = false;
let insert: CaptureInsert | null = null;
let describing = true;
let captureSettings: CaptureSettings | null = null;
@@ -258,19 +260,12 @@ function createTray(): void {
refreshTrayMenu();
}
-function captureStateUpdate(command: OverlayCommand | null, guideId: string | null) {
- const { value, context } = capture.getSnapshot();
- return {
- command,
- state: value,
- pauseReason: context.pauseReason,
- currentGuideId: guideId,
- stepCount: context.stepCount,
- };
+function stateUpdate(command: OverlayCommand | null, guideId: string | null): DesktopStateUpdate {
+ return { command, ...captureStateUpdate(capture.getSnapshot()), currentGuideId: guideId };
}
function broadcastState(command: OverlayCommand, guideId: string | null): void {
- mainWindow?.webContents.send('mimik:capture:stateUpdate', captureStateUpdate(command, guideId));
+ mainWindow?.webContents.send('mimik:capture:stateUpdate', stateUpdate(command, guideId));
}
function applyShortcuts(): void {
@@ -306,6 +301,37 @@ function onShortcut(name: ShortcutName): void {
recorder?.captureNow(screen.getCursorScreenPoint());
}
+function recordingGuideId(): string | null {
+ return capture.getSnapshot().context.currentGuideId;
+}
+
+async function endRecording(command: 'stop' | 'discard'): Promise<string | null> {
+ recorder?.stop();
+ await recorder?.drain();
+ const { currentGuideId: guideId, insertTargetGuideId, insertAtIndex } = capture.getSnapshot().context;
+ insert = null;
+ overlay?.reset();
+ if (!guideId) return null;
+ const app = mainWindow?.webContents ?? null;
+ if (command === 'discard') {
+ await ask(app, 'mimik:capture:discardRecording', { guideId, staging: insertTargetGuideId !== null }).catch(
+ () => undefined,
+ );
+ return null;
+ }
+ if (insertTargetGuideId !== null && insertAtIndex !== null) {
+ await ask(
+ app,
+ 'mimik:capture:mergeGuideInto',
+ { guideId, insertTargetGuideId, insertAtIndex },
+ FINISH_TIMEOUT_MS,
+ ).catch(() => undefined);
+ return insertTargetGuideId;
+ }
+ void ask(app, 'mimik:capture:finishGuide', guideId, FINISH_TIMEOUT_MS).catch(() => undefined);
+ return guideId;
+}
+
async function onOverlayCommand(command: OverlayCommand): Promise<void> {
let finished: string | null = null;
if (command.startsWith('mode:')) {
@@ -314,87 +340,56 @@ async function onOverlayCommand(command: OverlayCommand): Promise<void> {
return;
}
if (command === 'narration:start' || command === 'narration:stop') {
- if (command === 'narration:stop' || (await askMicrophone())) broadcastState(command, currentGuideId);
+ if (command === 'narration:stop' || (await askMicrophone())) broadcastState(command, recordingGuideId());
return;
}
if (command === 'deleteStep') {
const removed = steps.pop();
- if (removed && currentGuideId) {
+ const guideId = recordingGuideId();
+ if (removed && guideId) {
capture.send({ type: 'STEP_REMOVED' });
await ask(mainWindow?.webContents ?? null, 'mimik:capture:deleteStep', {
- guideId: currentGuideId,
+ guideId,
stepId: removed.id,
}).catch(() => undefined);
}
overlay?.showStep(steps.at(-1) ?? null);
return;
}
- if (command === 'start') {
+ if (command === 'stop' || command === 'discard') {
+ if (ending) return;
+ ending = true;
+ try {
+ finished = await endRecording(command);
+ } finally {
+ ending = false;
+ }
+ } else if (command === 'start') {
steps = [];
overlay?.setAiFailure(null);
- currentGuideId = capture.getSnapshot().context.currentGuideId;
+ const { currentGuideId: guideId, insertTargetGuideId } = capture.getSnapshot().context;
try {
await ask(mainWindow?.webContents ?? null, 'mimik:capture:createGuide', {
- guideId: currentGuideId,
- staging: insert !== null,
+ guideId,
+ staging: insertTargetGuideId !== null,
});
} catch (error) {
- currentGuideId = null;
overlay?.reset();
dialog.showErrorBox(mainI18n.t('desktop.cannotRecord'), error instanceof Error ? error.message : String(error));
return;
}
const started = await recorder?.start();
if (started && !started.ok) {
- currentGuideId = null;
overlay?.reset();
dialog.showErrorBox(mainI18n.t('desktop.cannotRecord'), started.detail);
return;
}
} else if (command === 'pause') {
recorder?.pause();
- } else if (command === 'resume') {
- recorder?.resume();
- } else if (command === 'stop') {
- recorder?.stop();
- await recorder?.drain();
- const target = insert;
- insert = null;
- if (currentGuideId && target) {
- await ask(
- mainWindow?.webContents ?? null,
- 'mimik:capture:mergeGuideInto',
- {
- guideId: currentGuideId,
- insertTargetGuideId: target.insertTargetGuideId,
- insertAtIndex: target.insertAtIndex,
- },
- FINISH_TIMEOUT_MS,
- ).catch(() => undefined);
- finished = target.insertTargetGuideId;
- } else if (currentGuideId) {
- finished = currentGuideId;
- void ask(mainWindow?.webContents ?? null, 'mimik:capture:finishGuide', finished, FINISH_TIMEOUT_MS).catch(
- () => undefined,
- );
- }
- currentGuideId = null;
- } else if (command === 'discard') {
- recorder?.stop();
- await recorder?.drain();
- const staging = insert !== null;
- insert = null;
- if (currentGuideId) {
- await ask(mainWindow?.webContents ?? null, 'mimik:capture:discardRecording', {
- guideId: currentGuideId,
- staging,
- }).catch(() => undefined);
- }
- currentGuideId = null;
} else if (command === 'disarm' || command === 'cancelEdit') {
insert = null;
}
- broadcastState(command, finished ?? currentGuideId);
+ broadcastState(command, finished ?? recordingGuideId());
applyShortcuts();
refreshTray();
if (command === 'stop' || command === 'discard' || command === 'disarm' || command === 'cancelEdit') {
@@ -487,11 +482,12 @@ if (!app.requestSingleInstanceLock()) {
return overlay ? overlay.withHidden(fn) : fn();
},
async (request) => {
- const reply = await ask<{ stepId?: string; title?: string; pending?: boolean }>(
- mainWindow?.webContents ?? null,
- 'mimik:capture:captureStep',
- { ...request, guideId: currentGuideId },
- ).catch(() => undefined);
+ const guideId = recordingGuideId();
+ if (!guideId) return undefined;
+ const reply = await ask(mainWindow?.webContents ?? null, 'mimik:capture:captureStep', {
+ ...request,
+ guideId,
+ }).catch(() => undefined);
if (reply?.stepId && reply.title) {
const step: OverlayStep = {
id: reply.stepId,
@@ -518,6 +514,7 @@ if (!app.requestSingleInstanceLock()) {
progress: (fraction, ms) => overlay?.setProgress(Math.round(fraction * (describing ? 45 : 100)), ms),
aimed: (aim) => overlay?.setPrint({ aim }),
saved: (src) => overlay?.setPrint({ src }),
+ recording: () => capture.getSnapshot().value === CaptureState.RECORDING,
},
);
@@ -555,7 +552,7 @@ if (!app.requestSingleInstanceLock()) {
},
);
ipcMain.handle('mimik:capture:region', () => overlay?.region);
- ipcMain.handle('mimik:capture:getState', () => captureStateUpdate(null, currentGuideId));
+ ipcMain.handle('mimik:capture:getState', () => stateUpdate(null, recordingGuideId()));
ipcMain.handle('mimik:capture:edit', (_event, target?: CaptureInsert) => {
whenPermitted(() => {
insert = target ?? null;
diff --git a/apps/desktop/src/main/ipc.ts b/apps/desktop/src/main/ipc.ts
new file mode 100644
index 0000000..b057e73
--- /dev/null
+++ b/apps/desktop/src/main/ipc.ts
@@ -0,0 +1,37 @@
+import type { CaptureStateUpdate } from '@mimik/core/capture/capture-state-update';
+import type { CaptureStepData } from '@mimik/core/capture/sink';
+import type { OverlayCommand } from './overlay';
+
+export interface DesktopStateUpdate extends CaptureStateUpdate {
+ command: OverlayCommand | null;
+}
+
+export type DesktopCaptureStepData = CaptureStepData & {
+ inputValue?: string;
+ zoomLevel?: number;
+};
+
+interface CheckResult {
+ name: string;
+ ok: boolean;
+ detail: string;
+}
+
+export interface RendererRequests {
+ 'mimik:capture:createGuide': [{ guideId?: string | null; staging?: boolean }, string];
+ 'mimik:capture:captureStep': [DesktopCaptureStepData, { stepId: string; title: string; pending: boolean }];
+ 'mimik:capture:deleteStep': [{ guideId: string; stepId: string }, boolean];
+ 'mimik:capture:discardRecording': [{ guideId: string; staging: boolean }, boolean];
+ 'mimik:capture:finishGuide': [string, boolean];
+ 'mimik:capture:mergeGuideInto': [{ guideId: string; insertTargetGuideId: string; insertAtIndex: number }, boolean];
+ 'mimik:check:cleanup': [string[], boolean];
+ 'mimik:check:defaultLogo': [undefined, boolean];
+ 'mimik:check:screenshotIds': [undefined, string[]];
+ 'mimik:check:screenshotSrc': [string, string | null];
+ 'mimik:check:steps': [string, string[] | null];
+ 'mimik:check:title': [string, string | null];
+ 'mimik:check:trashed': [string, boolean | null];
+ 'mimik:check:verify': [string, CheckResult[]];
+}
+
+export type RendererRequest = keyof RendererRequests;
diff --git a/apps/desktop/src/main/overlay/index.ts b/apps/desktop/src/main/overlay/index.ts
index 138371f..25dcc8c 100644
--- a/apps/desktop/src/main/overlay/index.ts
+++ b/apps/desktop/src/main/overlay/index.ts
@@ -9,9 +9,10 @@ import {
} from '@mimik/core/capture/machine';
import type { VoiceErrorReason } from '@mimik/core/capture/voice/voice-update';
import type { DescriptionSource } from '@mimik/core/guides/types';
+import type { Rect } from '@mimik/core/rect';
import { app, BrowserWindow, ipcMain, screen } from 'electron';
import type { Actor } from 'xstate';
-import { loadRegion, type Region, saveRegion } from '../capture/region';
+import { loadRegion, saveRegion } from '../capture/region';
import type { CaptureMode } from '../capture/settings';
export type CaptureActor = Actor<typeof captureMachine>;
@@ -73,7 +74,7 @@ export interface OverlayPrint {
export interface OverlayView {
state: CaptureStateValue;
pauseReason: PauseReason | null;
- region: Region;
+ region: Rect;
step: OverlayStep | null;
mode: CaptureMode;
busy: boolean;
@@ -86,7 +87,7 @@ export interface OverlayView {
}
export interface OverlayOptions {
- introFrame?: () => Promise<Region>;
+ introFrame?: () => Promise<Rect>;
insert?: () => CaptureInsert | null;
shortcuts?: () => OverlayShortcuts;
}
@@ -154,7 +155,7 @@ export class CaptureOverlay {
private intro: BrowserWindow | null = null;
private introDone: ((finished: boolean) => void) | null = null;
private editing = false;
- private rect: Region;
+ private rect: Rect;
private step: OverlayStep | null = null;
private size = { width: CONTROLS.width, height: CONTROLS.height };
private busy = false;
@@ -175,13 +176,13 @@ export class CaptureOverlay {
this.rect = loadRegion();
ipcMain.handle('mimik:overlay:region', () => this.rect);
ipcMain.handle('mimik:overlay:view', () => this.view());
- ipcMain.on('mimik:overlay:setRegion', (_event, next: Region) => this.setRegion(next));
+ ipcMain.on('mimik:overlay:setRegion', (_event, next: Rect) => this.setRegion(next));
ipcMain.on('mimik:overlay:command', (_event, command: OverlayCommand) => this.command(command));
ipcMain.on('mimik:overlay:size', (_event, width: number, height: number) => this.resize(width, height));
capture.subscribe(() => this.render());
}
- get region(): Region {
+ get region(): Rect {
return this.rect;
}
@@ -280,7 +281,7 @@ export class CaptureOverlay {
this.positionControls();
}
- private setRegion(next: Region): void {
+ private setRegion(next: Rect): void {
this.rect = saveRegion(next);
if (this.boundary && !this.boundary.isDestroyed()) this.boundary.setBounds(this.frameBounds());
this.positionControls();
@@ -417,11 +418,11 @@ export class CaptureOverlay {
this.onCommand('start');
}
- private displayUnderCursor(): Region {
+ private displayUnderCursor(): Rect {
return screen.getDisplayNearestPoint(screen.getCursorScreenPoint()).bounds;
}
- private playIntro(frame: Region): Promise<boolean> {
+ private playIntro(frame: Rect): Promise<boolean> {
return new Promise((resolve) => {
const win = overlayWindow(frame, 'intro', false);
this.intro = win;
@@ -520,7 +521,6 @@ export class CaptureOverlay {
this.capture.send({ type: 'RESUME_CAPTURE' });
this.onCommand(command);
} else if ((command === 'stop' || command === 'discard') && state !== CaptureState.IDLE) {
- this.reset();
this.onCommand(command);
}
}
@@ -564,5 +564,3 @@ export class CaptureOverlay {
ipcMain.removeAllListeners('mimik:overlay:size');
}
}
-
-export type { Region };
diff --git a/apps/desktop/src/preload/index.ts b/apps/desktop/src/preload/index.ts
index fa96277..aababfe 100644
--- a/apps/desktop/src/preload/index.ts
+++ b/apps/desktop/src/preload/index.ts
@@ -1,29 +1,16 @@
import type { AiFailureUpdate } from '@mimik/core/capture/ai/errors';
import type { CaptureInsert } from '@mimik/core/capture/capture-insert';
+import type { Rect } from '@mimik/core/rect';
import { contextBridge, ipcRenderer } from 'electron';
import type { CaptureSettings } from '../main/capture/settings';
-import type { OverlayCommand, OverlayNarration } from '../main/overlay';
+import type { DesktopStateUpdate, RendererRequest, RendererRequests } from '../main/ipc';
+import type { OverlayNarration } from '../main/overlay';
import type { CapturePermissions, MicrophoneAccess, PermissionKind } from '../main/permissions';
-export interface CaptureStateUpdate {
- command: OverlayCommand | null;
- state: string;
- pauseReason: string | null;
- currentGuideId: string | null;
- stepCount: number;
-}
-
-interface Region {
- x: number;
- y: number;
- width: number;
- height: number;
-}
-
const api = {
version: (): Promise<string> => ipcRenderer.invoke('mimik:version'),
capture: {
- region: (): Promise<Region> => ipcRenderer.invoke('mimik:capture:region'),
+ region: (): Promise<Rect> => ipcRenderer.invoke('mimik:capture:region'),
edit: (insert?: CaptureInsert): Promise<void> => ipcRenderer.invoke('mimik:capture:edit', insert),
arm: (insert?: CaptureInsert): Promise<void> => ipcRenderer.invoke('mimik:capture:arm', insert),
described: (
@@ -41,9 +28,9 @@ const api = {
onOpenSheet: (handler: () => void): void => {
ipcRenderer.on('mimik:capture:openSheet', () => handler());
},
- getState: (): Promise<CaptureStateUpdate> => ipcRenderer.invoke('mimik:capture:getState'),
- onStateUpdate: (handler: (update: CaptureStateUpdate) => void): void => {
- ipcRenderer.on('mimik:capture:stateUpdate', (_event, update: CaptureStateUpdate) => handler(update));
+ getState: (): Promise<DesktopStateUpdate> => ipcRenderer.invoke('mimik:capture:getState'),
+ onStateUpdate: (handler: (update: DesktopStateUpdate) => void): void => {
+ ipcRenderer.on('mimik:capture:stateUpdate', (_event, update: DesktopStateUpdate) => handler(update));
},
},
microphone: {
@@ -64,8 +51,11 @@ const api = {
fetch: (request: unknown): Promise<unknown> => ipcRenderer.invoke('mimik:ai:fetch', request),
abort: (id: string): void => ipcRenderer.send('mimik:ai:abort', id),
},
- onRequest: (channel: string, handler: (payload: unknown) => Promise<unknown>): void => {
- ipcRenderer.on(channel, async (_event, replyChannel: string, payload: unknown) => {
+ onRequest: <K extends RendererRequest>(
+ channel: K,
+ handler: (payload: RendererRequests[K][0]) => Promise<RendererRequests[K][1]> | RendererRequests[K][1],
+ ): void => {
+ ipcRenderer.on(channel, async (_event, replyChannel: string, payload: RendererRequests[K][0]) => {
try {
ipcRenderer.send(replyChannel, await handler(payload));
} catch (error) {
diff --git a/apps/desktop/src/preload/overlay.ts b/apps/desktop/src/preload/overlay.ts
index 68770cf..8ec28bd 100644
--- a/apps/desktop/src/preload/overlay.ts
+++ b/apps/desktop/src/preload/overlay.ts
@@ -1,10 +1,11 @@
+import type { Rect } from '@mimik/core/rect';
import { contextBridge, ipcRenderer } from 'electron';
-import type { OverlayView, Region } from '../main/overlay';
+import type { OverlayView } from '../main/overlay';
const api = {
- region: (): Promise<Region> => ipcRenderer.invoke('mimik:overlay:region'),
+ region: (): Promise<Rect> => ipcRenderer.invoke('mimik:overlay:region'),
view: (): Promise<OverlayView> => ipcRenderer.invoke('mimik:overlay:view'),
- setRegion: (region: Region): void => ipcRenderer.send('mimik:overlay:setRegion', region),
+ setRegion: (region: Rect): void => ipcRenderer.send('mimik:overlay:setRegion', region),
command: (command: string): void => ipcRenderer.send('mimik:overlay:command', command),
size: (width: number, height: number): void => ipcRenderer.send('mimik:overlay:size', width, height),
onUpdate: (handler: (view: OverlayView) => void): void => {
diff --git a/apps/desktop/src/renderer/capture-host.ts b/apps/desktop/src/renderer/capture-host.ts
index 4b93a31..77bfadc 100644
--- a/apps/desktop/src/renderer/capture-host.ts
+++ b/apps/desktop/src/renderer/capture-host.ts
@@ -1,7 +1,6 @@
import { finishGuide } from '@mimik/core/capture/ai/finish-guide';
import { mergeRecording } from '@mimik/core/capture/ai/merge-recording';
import { CaptureState } from '@mimik/core/capture/machine';
-import type { CaptureStepData } from '@mimik/core/capture/sink';
import { i18n, localStorage } from '@mimik/core/env';
import { getMostCommonApp } from '@mimik/core/guides/most-common-app';
import {
@@ -19,10 +18,8 @@ let narratingBeforePause = false;
allScreenshotIds().then((ids) => window.mimik.screenshots.sweep(ids));
-window.mimik.onRequest('mimik:capture:createGuide', (payload) =>
- sink.createGuide(payload as { guideId?: string; staging?: boolean }),
-);
-window.mimik.onRequest('mimik:capture:captureStep', (payload) => sink.captureStep(payload as CaptureStepData));
+window.mimik.onRequest('mimik:capture:createGuide', (payload) => sink.createGuide(payload));
+window.mimik.onRequest('mimik:capture:captureStep', (payload) => sink.captureStep(payload));
window.mimik.capture.onStateUpdate(async ({ command, state, currentGuideId }) => {
if (command === 'narration:start' || command === 'narration:stop') {
@@ -39,11 +36,11 @@ window.mimik.capture.onStateUpdate(async ({ command, state, currentGuideId }) =>
} else if (command === 'pause') {
narratingBeforePause = narration.update.phase === 'recording';
narration.stop();
- } else if (command === 'narration:stop') narration.abort();
+ } else if (command === 'narration:stop') narration.turnOff();
});
window.mimik.onRequest('mimik:capture:deleteStep', async (payload) => {
- const { guideId, stepId } = payload as { guideId: string; stepId: string };
+ const { guideId, stepId } = payload;
await deleteStep(guideId, stepId);
return true;
});
@@ -54,29 +51,25 @@ async function appTitle(guideId: string): Promise<string> {
}
window.mimik.onRequest('mimik:capture:finishGuide', async (payload) => {
- await finishGuide(payload as string, { fallbackTitle: appTitle, settleNarration: () => narration.settle() });
+ await finishGuide(payload, { fallbackTitle: appTitle, settleNarration: () => narration.settle() });
return true;
});
window.mimik.onRequest('mimik:capture:discardRecording', async (payload) => {
- const { guideId, staging } = payload as { guideId: string; staging: boolean };
- narration.abort();
+ const { guideId, staging } = payload;
+ narration.turnOff();
if (staging) await permanentlyDeleteGuide(guideId);
else await softDeleteGuide(guideId);
return true;
});
window.mimik.onRequest('mimik:capture:mergeGuideInto', async (payload) => {
- const { guideId, insertTargetGuideId, insertAtIndex } = payload as {
- guideId: string;
- insertTargetGuideId: string;
- insertAtIndex: number;
- };
+ const { guideId, insertTargetGuideId, insertAtIndex } = payload;
await mergeRecording(guideId, { insertTargetGuideId, insertAtIndex }, () => narration.settle());
return true;
});
window.mimik.onRequest('mimik:check:cleanup', async (payload) => {
- for (const id of payload as string[]) await permanentlyDeleteGuide(id);
+ for (const id of payload) await permanentlyDeleteGuide(id);
return true;
});
diff --git a/apps/desktop/src/renderer/capture-sink.ts b/apps/desktop/src/renderer/capture-sink.ts
index b2c2f51..40d1e26 100644
--- a/apps/desktop/src/renderer/capture-sink.ts
+++ b/apps/desktop/src/renderer/capture-sink.ts
@@ -1,18 +1,13 @@
import { describeStep } from '@mimik/core/capture/ai/describe-step';
import { SCREEN_STEP_DESCRIPTION_PROMPT } from '@mimik/core/capture/ai/prompts';
import { serializeScreenContext } from '@mimik/core/capture/ai/screen-context';
-import type { CaptureStepData } from '@mimik/core/capture/sink';
import { type StepNarration, writeStep } from '@mimik/core/capture/write-step';
import { localStorage } from '@mimik/core/env';
import { clearStepAiPending, createGuide, getStep, getStepsForGuide, saveScreenshot } from '@mimik/core/guides/service';
import { screenshotForElement } from '@mimik/core/screenshot/record';
+import type { DesktopCaptureStepData } from '../main/ipc';
import type { DesktopNarration } from './narration';
-export type DesktopCaptureStepData = CaptureStepData & {
- inputValue?: string;
- zoomLevel?: number;
-};
-
export class DesktopCaptureSink {
private readonly narration: StepNarration | null;
diff --git a/apps/desktop/src/renderer/check-pipeline.ts b/apps/desktop/src/renderer/check-pipeline.ts
index 4063ff1..73fb505 100644
--- a/apps/desktop/src/renderer/check-pipeline.ts
+++ b/apps/desktop/src/renderer/check-pipeline.ts
@@ -1,5 +1,4 @@
import './core-env';
-import type { CaptureStepData } from '@mimik/core/capture/sink';
import { assetUrl } from '@mimik/core/env';
import { exportGuideAsDOCX } from '@mimik/core/export/docx-export';
import { exportGuideAsHTML } from '@mimik/core/export/html-export';
@@ -21,10 +20,10 @@ interface CheckResult {
const sink = new DesktopCaptureSink();
window.mimik.onRequest('mimik:capture:createGuide', () => sink.createGuide());
-window.mimik.onRequest('mimik:capture:captureStep', (payload) => sink.captureStep(payload as CaptureStepData));
+window.mimik.onRequest('mimik:capture:captureStep', (payload) => sink.captureStep(payload));
window.mimik.onRequest('mimik:check:screenshotSrc', async (payload) => {
- const found = await getGuide(payload as string);
+ const found = await getGuide(payload);
const first = found ? [...found.screenshots.values()][0] : undefined;
return first?.src ?? null;
});
@@ -38,27 +37,27 @@ window.mimik.onRequest('mimik:check:defaultLogo', () =>
);
window.mimik.onRequest('mimik:check:cleanup', async (payload) => {
- for (const id of payload as string[]) await permanentlyDeleteGuide(id);
+ for (const id of payload) await permanentlyDeleteGuide(id);
return true;
});
window.mimik.onRequest('mimik:check:steps', async (payload) => {
- const found = await getGuide(payload as string);
+ const found = await getGuide(payload);
return found ? found.steps.map((step) => step.description) : null;
});
window.mimik.onRequest('mimik:check:trashed', async (payload) => {
- const found = await getGuide(payload as string);
+ const found = await getGuide(payload);
return found ? found.guide.deletedAt != null : null;
});
window.mimik.onRequest('mimik:check:title', async (payload) => {
- const found = await getGuide(payload as string);
+ const found = await getGuide(payload);
return found?.guide.title ?? null;
});
window.mimik.onRequest('mimik:check:verify', async (payload) => {
- const guideId = payload as string;
+ const guideId = payload;
const results: CheckResult[] = [];
const found = await getGuide(guideId);
diff --git a/packages/core/src/capture/capture-state-update.ts b/packages/core/src/capture/capture-state-update.ts
new file mode 100644
index 0000000..b8a6cc1
--- /dev/null
+++ b/packages/core/src/capture/capture-state-update.ts
@@ -0,0 +1,17 @@
+import type { CaptureSnapshot, CaptureStateValue, PauseReason } from './machine';
+
+export interface CaptureStateUpdate {
+ state: CaptureStateValue;
+ stepCount: number;
+ currentGuideId: string | null;
+ pauseReason: PauseReason | null;
+}
+
+export function captureStateUpdate(snapshot: CaptureSnapshot): CaptureStateUpdate {
+ return {
+ state: snapshot.value,
+ stepCount: snapshot.context.stepCount,
+ currentGuideId: snapshot.context.currentGuideId,
+ pauseReason: snapshot.context.pauseReason ?? null,
+ };
+}
diff --git a/src/blur/__tests__/restore.test.ts b/src/blur/__tests__/restore.test.ts
index 919f121..1eae864 100644
--- a/src/blur/__tests__/restore.test.ts
+++ b/src/blur/__tests__/restore.test.ts
@@ -1,9 +1,9 @@
import { describe, expect, it } from 'vitest';
+import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import { CaptureState } from '@/core/capture/machine';
-import type { GetStateResponse } from '@/lib/messaging';
import { shouldReopenBlur } from '../restore';
-const state = (over: Partial<GetStateResponse> = {}): GetStateResponse => ({
+const state = (over: Partial<CaptureStateUpdate> = {}): CaptureStateUpdate => ({
state: CaptureState.PAUSED,
stepCount: 1,
currentGuideId: 'guide-1',
diff --git a/src/blur/restore.ts b/src/blur/restore.ts
index 866410f..f5b2269 100644
--- a/src/blur/restore.ts
+++ b/src/blur/restore.ts
@@ -1,6 +1,6 @@
+import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import { CaptureState } from '@/core/capture/machine';
-import type { GetStateResponse } from '@/lib/messaging';
-export function shouldReopenBlur(state: GetStateResponse, isTopFrame: boolean): boolean {
+export function shouldReopenBlur(state: CaptureStateUpdate, isTopFrame: boolean): boolean {
return isTopFrame && state.state === CaptureState.PAUSED && state.pauseReason === 'blur';
}
diff --git a/src/capture/__tests__/session-sync.test.ts b/src/capture/__tests__/session-sync.test.ts
index 3a7731c..837ca0e 100644
--- a/src/capture/__tests__/session-sync.test.ts
+++ b/src/capture/__tests__/session-sync.test.ts
@@ -1,7 +1,7 @@
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import { CaptureState } from '@/core/capture/machine';
-import type { GetStateResponse } from '@/lib/messaging';
const sendMessage = vi.fn();
const startCapture = vi.fn((_guideId: string, _isTopFrame: boolean) => ({ stop: vi.fn() }));
@@ -15,7 +15,7 @@ const answerChildFrames = vi.fn(() => stopAnswering);
vi.mock('@/core/capture/dom/frame-placement', () => ({ answerChildFrames: () => answerChildFrames() }));
vi.mock('@mimik/core/logger', () => ({ logger: { info: vi.fn(), debug: vi.fn(), warn: vi.fn(), error: vi.fn() } }));
-async function bootFrame(state: GetStateResponse, onSynced?: (s: GetStateResponse) => void) {
+async function bootFrame(state: CaptureStateUpdate, onSynced?: (s: CaptureStateUpdate) => void) {
sendMessage.mockResolvedValue(state);
const { CaptureSession } = await import('../session');
const session = new CaptureSession(onSynced);
@@ -24,14 +24,14 @@ async function bootFrame(state: GetStateResponse, onSynced?: (s: GetStateRespons
return session;
}
-const recording: GetStateResponse = {
+const recording: CaptureStateUpdate = {
state: CaptureState.RECORDING,
stepCount: 2,
currentGuideId: 'guide-1',
pauseReason: null,
};
-const pausedForBlur: GetStateResponse = {
+const pausedForBlur: CaptureStateUpdate = {
state: CaptureState.PAUSED,
stepCount: 2,
currentGuideId: 'guide-1',
diff --git a/src/capture/session.ts b/src/capture/session.ts
index d28a648..49657bc 100644
--- a/src/capture/session.ts
+++ b/src/capture/session.ts
@@ -1,7 +1,8 @@
import { logger } from '@mimik/core/logger';
+import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import { answerChildFrames } from '@/core/capture/dom/frame-placement';
import { CaptureState } from '@/core/capture/machine';
-import { type GetStateResponse, sendMessage } from '@/lib/messaging';
+import { sendMessage } from '@/lib/messaging';
import { type CaptureHandle, startCapture } from './events/handlers';
const FRAME_ANSWER_GRACE_MS = 2000;
@@ -13,7 +14,7 @@ export class CaptureSession {
private frameAnswersTeardown: (() => void) | null = null;
private frameAnswerGrace: ReturnType<typeof setTimeout> | undefined;
- constructor(private readonly onSynced?: (state: GetStateResponse) => void) {
+ constructor(private readonly onSynced?: (state: CaptureStateUpdate) => void) {
this.syncWithBackground();
}
diff --git a/src/entrypoints/background/actor.ts b/src/entrypoints/background/actor.ts
index a1c0462..4fac210 100644
--- a/src/entrypoints/background/actor.ts
+++ b/src/entrypoints/background/actor.ts
@@ -1,6 +1,7 @@
import { logger } from '@mimik/core/logger';
import { createActor } from 'xstate';
-import { type CaptureSnapshot, type CaptureStateValue, captureMachine } from '@/core/capture/machine';
+import { captureStateUpdate } from '@/core/capture/capture-state-update';
+import { type CaptureSnapshot, captureMachine } from '@/core/capture/machine';
import { sessionStorage } from '@/lib/browser-api/session-storage';
import type { PanelStateUpdate } from '@/lib/port/types';
import type { ActorRef } from './types';
@@ -47,14 +48,7 @@ export async function initActor(): Promise<void> {
}
export function getStateUpdate(): PanelStateUpdate {
- const snap = actor.getSnapshot();
- return {
- type: 'STATE_UPDATE',
- state: snap.value as CaptureStateValue,
- stepCount: snap.context.stepCount,
- currentGuideId: snap.context.currentGuideId,
- pauseReason: snap.context.pauseReason ?? null,
- };
+ return { type: 'STATE_UPDATE', ...captureStateUpdate(actor.getSnapshot()) };
}
export function initActorFallback(): void {
diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts
index cbe14ad..30d2765 100644
--- a/src/lib/messaging.ts
+++ b/src/lib/messaging.ts
@@ -1,7 +1,7 @@
import { defineExtensionMessaging } from '@webext-core/messaging';
import type { GenerateGuideDescriptionResponse } from '@/core/capture/ai/guide-description';
import type { RewriteSelectionResponse } from '@/core/capture/ai/rewrite';
-import type { CaptureStateValue, PauseReason } from '@/core/capture/machine';
+import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import type {
CaptureStepData,
CaptureStepResponse,
@@ -12,13 +12,6 @@ import type {
} from '@/core/capture/sink';
import type { VoiceoverProviderKey } from '@/core/export/voiceover/providers';
-export interface GetStateResponse {
- state: CaptureStateValue;
- stepCount: number;
- currentGuideId: string | null;
- pauseReason: PauseReason | null;
-}
-
interface StartRecordingData {
url: string;
insertTargetGuideId?: string;
@@ -126,7 +119,7 @@ interface ResumeCaptureResponse {
}
interface MimikProtocol {
- getState(): GetStateResponse;
+ getState(): CaptureStateUpdate;
startRecording(data: StartRecordingData): StartRecordingResponse;
stopRecording(): StopRecordingResponse;
discardRecording(): DiscardRecordingResponse;
diff --git a/src/lib/port/types.ts b/src/lib/port/types.ts
index 89362ee..a66c608 100644
--- a/src/lib/port/types.ts
+++ b/src/lib/port/types.ts
@@ -1,14 +1,10 @@
import type { browser } from '#imports';
import type { AiFailureUpdate } from '@/core/capture/ai/errors';
-import type { CaptureStateValue, PauseReason } from '@/core/capture/machine';
+import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import type { VoiceUpdate } from '@/core/capture/voice/voice-update';
-export interface PanelStateUpdate {
+export interface PanelStateUpdate extends CaptureStateUpdate {
type: 'STATE_UPDATE';
- state: CaptureStateValue;
- stepCount: number;
- currentGuideId: string | null;
- pauseReason: PauseReason | null;
}
export interface PanelVoiceUpdate extends VoiceUpdate {