Electron bridge API
Complete integration
registerCodexElectronMain(options)
Registers the surface and native IPC handlers and returns one cleanup function. It creates one CodexElectronAttachmentRegistry shared by both handler sets, so renderer references are resolved to trusted paths only at the main-process surface boundary.
type CodexElectronMainOptions = {
surface: CodexSurface;
sender: IpcEventSender;
ipcMain: IpcMainPort;
clipboard: CodexNativeClipboard;
dialog: CodexNativeDialog;
shell: CodexNativeShell;
native?: CodexNativeMainOptions;
/** Rejects surface and native invocations before their handlers run when false. */
isTrustedSender?: (event: unknown) => boolean;
};Window policy
installCodexWindowPolicy(webContents, { rendererUrl, openExternal })keeps a window on its renderer. It denies new windows and navigations away fromrendererUrl(same origin for dev servers, same file for packagedfile:renderers), and passes onlyhttp(s),mailto, andtelURLs toopenExternal.isCodexRendererSender(event, rendererUrl)accepts only the main frame of a window currently showing the renderer. Pass it asisTrustedSender.isCodexRendererUrl(url, rendererUrl)andcodexExternalUrl(url)expose the underlying checks.
exposeCodexElectronPreload(contextBridge, ipcRenderer)
Available from @codex-app-sdk/electron/preload. Exposes both renderer APIs and returns them as CodexElectronRendererApis.
Surface IPC
registerCodexSurfaceIpccreateCodexSurfaceRendererApiCodexSurfaceRendererApi
The renderer variant narrows conversation creation to approvalPreset, model, reasoningEffort, and serviceTier.
State crosses IPC as full snapshots until the renderer calls getVersionedSnapshot(). When the surface supports state patches (CodexSurface does), the main process then streams small structural patches instead. useCodexSurface mirrors them in the renderer's own world, so unchanged messages keep their object identity and do not re-render. Renderers that never ask keep receiving snapshots.
Native IPC
registerCodexNativeIpccreateCodexNativeRendererApiexposeCodexNativeRendererApiCodexNativeRendererApi- attachment, clipboard, and transcription contracts
CodexElectronAttachmentRegistryfor custom composed integrations
CodexNativeMainOptions
type CodexNativeMainOptions = {
appleSpeechAssetsPath?: string;
maxAttachmentBytes?: number;
maxTotalAttachmentBytes?: number;
maxAudioBytes?: number;
maxImagePreviewBytes?: number;
startSpeechSession?: typeof startAppleSpeechSession;
prepareSpeechSession?: CodexSpeechSessionPreparer;
onSpeechOperation?: CodexSpeechOperationObserver;
transcribeAudio?: (audioData, options?) => Promise<AppleSpeechTranscriptionResult>;
};Streaming dictation
The default macOS preload exposes streamingTranscription on CodexHostCapabilities. createCodexNativeRendererApi and exposeCodexNativeRendererApi accept streamingTranscription?: boolean to enable or disable it; transcription: false disables both modes. Hosts with a custom batch-only service should set streamingTranscription: false.
type CodexStreamingTranscription = {
prepare?(options: { sessionId: string; locale?: string }): Promise<void>;
start(options: { sessionId: string; sampleRate: number; locale?: string }): Promise<void>;
append(sessionId: string, audio: ArrayBuffer): Promise<void>;
stop(sessionId: string): Promise<{ text: string; error?: string }>;
cancel(sessionId: string): Promise<void>;
onEvent(listener: (event: CodexSpeechSessionEvent) => void): () => void;
};Each audio chunk is mono Float32 little-endian PCM, delivered once, in order. Await each append before sending the next. Transcript events contain { type: 'transcript', sessionId, finalText, partialText }; replace the current snapshot on corrections. Error events contain { type: 'error', sessionId, error }. stop() drains pending recognition and returns the authoritative final text. cancel() discards it. Subscribe before starting and unsubscribe on teardown; ignore events from any other session ID.
The main bridge permits one active session per renderer and binds it to the invoking frame. Replacing that frame's document or the main document, renderer destruction, and bridge disposal cancel recording. Same-document navigation and unrelated iframe navigation leave recording active. Requests use the same isTrustedSender policy as other native APIs. Audio is limited to 256 KiB per chunk and maxAudioBytes total (25 MiB by default, about 6.8 minutes at 16 kHz). Use unique IDs for successive recordings.
Native recognition requires a supported Mac running macOS 26 or later and an available Apple speech model for the locale. Missing model assets can require a first-use download; recognition itself stays on device. Unsupported or failed native sessions report an error, not invented provisional text.
For source-mode development, point appleSpeechAssetsPath (or CODEX_APP_SDK_ASSETS_PATH) at the source SDK's packages/backend/assets so the native helper matches the source API. Packaged apps should use the helper from the same SDK version as their bridge.
Selecting a speech engine
The main entrypoint exports startAppleSpeechSession, startOpenAISpeechSession, and their session types. startSpeechSession is called for each recording, so hosts can read current settings and credentials without restarting the bridge:
import {
registerCodexNativeIpc,
startAppleSpeechSession,
startOpenAISpeechSession,
} from '@codex-app-sdk/electron';
registerCodexNativeIpc(dependencies, {
startSpeechSession: async (input, onEvent, nativeOptions) => {
if (readSpeechEngine() === 'openai') {
return startOpenAISpeechSession(input, onEvent, {
apiKey: await readSpeechApiKey(), // Host-owned secure storage, main process only.
signal: nativeOptions?.signal,
});
}
return startAppleSpeechSession(input, onEvent, nativeOptions);
},
}, attachments);An active recording retains its selected session until stop or cancel; later settings changes apply to the next recording. Enable transcription: true and streamingTranscription: true in exposeCodexElectronPreload (or createCodexNativeRendererApi) when supplying OpenAI streaming on any desktop platform. The renderer continues using the same PCM and transcript bridge; credentials never cross it. OpenAI streams audio to the provider; Apple recognition stays on the device. Engine selection and credential storage belong to the host. See the OpenAI service contract.
Parallel connection and microphone setup
Supply prepareSpeechSession and enable speechPreparation: true alongside transcription and streamingTranscription in preload to overlap the provider handshake with microphone setup. The composer calls prepare({ sessionId, locale? }) while opening capture, then start({ sessionId, sampleRate, locale? }) with the actual resolved microphone rate. It activates microphone delivery only after both are ready. No early audio is buffered or uploaded.
import {
prepareOpenAISpeechSession,
startAppleSpeechSession,
} from '@codex-app-sdk/electron';
const nativeOptions = {
prepareSpeechSession: async (input, onEvent, dependencies) => {
const engine = readSpeechEngine(); // Pin settings for this recording.
const model = readOpenAILivePreference() ? 'gpt-live-transcribe' : 'gpt-transcribe';
if (engine === 'openai') {
return prepareOpenAISpeechSession(input, onEvent, {
apiKey: await readSpeechApiKey(),
model,
signal: dependencies.signal,
});
}
return {
start: sampleRate => startAppleSpeechSession({ ...input, sampleRate }, onEvent, dependencies),
cancel: async () => {}, // The shared abort signal owns Apple startup/recording.
};
},
startSpeechSession, // Keep the existing callback for clients without preparation.
};CodexSpeechSessionPreparer receives { sessionId, locale? }, the existing speech event callback, and { signal, assetsPath? }. It returns Promise<CodexPreparedSpeechSession> whose start(sampleRate) resolves a CodexSpeechSession (append, stop, cancel) and whose cancel() releases preparation resources. Factories should respect the signal, and cancellation must be safe to repeat. The bridge cancels late resources even if a factory finishes after its renderer has gone away. Settings and credentials stay pinned from preparation through activation; read them before awaiting asynchronous work.
Existing hosts and clients without the optional prepare method retain sequential startup. A preparation failure or microphone failure cancels its sibling and releases capture; late completion cannot activate an old recording. Apple helper startup still receives the actual microphone rate and keeps its existing behavior.
Speech operation observation
registerCodexNativeIpc accepts an optional main-process observer, also available through registerCodexElectronMain({ native: { onSpeechOperation } }):
type CodexSpeechOperation = 'start' | 'stop' | 'cancel';
type CodexSpeechOperationOutcome = 'succeeded' | 'failed' | 'cancelled';
type CodexSpeechOperationObserver = (
operation: CodexSpeechOperation,
) => ((outcome: CodexSpeechOperationOutcome) => void) | void;The observer runs immediately before each actual streaming adapter operation. Its returned closure receives exactly one outcome when that operation settles. Concurrent operations have separate closures; duplicate stop/cancel requests do not create duplicate adapter operations or observations. Automatic cleanup cancellation is included. Invalid or untrusted requests, audio chunks, and batch transcription are not observed.
failed covers rejected operations and stop results with an error. cancelled covers successful cancellation and start/stop interrupted by session cancellation. The original result, error, and cancellation behavior are preserved; observer failures cannot alter them. No audio, transcript, session or renderer identifier, locale, error message, or additional properties cross this hook.
Callbacks must be fast and nonblocking. They are never awaited; thrown errors and rejected promises are ignored. Return nothing when observation is disabled or consent is unknown. This suppresses observation only, never dictation. Hosts own consent and may bind a permission epoch in the completion closure without adding identifiers to this API.
CodexNativeRendererApi.readImagePreview?(reference) lazily requests a bounded, non-SVG local image as a renderer-safe data URL. It returns null when the file is missing, unsupported, or larger than maxImagePreviewBytes (8 MiB by default). The optional method keeps custom/older preload implementations backward compatible.
Typed IPC primitives
For applications composing the integration with an existing IPC system:
TypedIpcMainTypedIpcRendererregisterIpcMainHandlersconnectIpcEventsToBussendIpcEventIpcRequest,IpcRequestArguments, andIpcRequestResult- main/renderer port and handler types
The helpers validate method names, argument shapes at the integration boundary, listener cleanup, and request/event typing.
The native attachment bridge carries bounded renderer-safe image previews. It does not expose local file:// image sources to the renderer; restored SDK-ingested temporary images are read on demand through the native bridge, while missing or reclaimed files fall back to a file chip.
Official remote-control pairing is intentionally a Node CodexSurface facade, not a default renderer IPC method. Hosts that expose pairing UI should define a narrow app-owned serialization boundary. See Remote control.
See the Electron integration guide and security boundary.