Back to Cherry Studio

IpcApi Reference

docs/references/ipc/README.md

2.0.05.0 KB
Original Source

IpcApi Reference

Entry point for IpcApi — Cherry Studio's unified, type-safe channel for RPC-over-IPC: command/capability calls from the renderer into the main process, plus typed main→renderer events.

IpcApi is the fifth parallel subsystem alongside BootConfig / Cache / Preference / DataApi. It does not absorb any of them — it collects the "business capability IPC" those four cannot cover (window/system/shell/notification/external-service/file commands).

Status: the Stage-0 framework (schema mechanism, IpcRouter, IpcApiService, preload forwarder, renderer facade, useIpcOn) has shipped, and the business-channel migration is essentially complete — IpcChannel.ts now holds only the data/IpcApi infrastructure channels plus the intentionally-not-migrated groups (v1-only, the frozen file cluster, deferred lan_transfer / micro-domains / copilot, the skill scaffold, and the Tab_MoveWindow / Python escape hatches; each carries a self-contained comment in IpcChannel.ts). The endgame collapse (shrinking IpcChannel.ts to ipc/channels.ts, removing BaseService.ipcHandle/ipcOn, narrowing window.electron) is still pending.

Quick Navigation

  • IpcApi Overview — paradigm split (RPC vs REST), surface narrowing + direction cheat sheet, no one-way R→M, layering, the two orthogonal axes, trust boundary, IpcContext, error model, security
  • IpcApi Usage — add a request (schema + handler), add an event (type + broadcast/send + useIpcOn), three-process end-to-end examples
  • IpcApi Schema Guide — route/event naming, *RequestSchemas/*EventSchemas, IpcRoute/IpcEventName, ESLint key validation
  • IpcApi Migration Guide — collecting scattered ipcMain.handle/this.ipcHandle/hand-written preload per domain, the send work-list, escape hatch (when a channel stays out), exposure-surface audit

Boundary — When To Use Which Subsystem

NeedSystemAPI
Read/write SQLite business dataDataApiuseQuery / useMutation
User setting (syncs across windows)PreferenceusePreference
Disposable / shared transient stateCacheuseCache / useSharedCache / usePersistCache
Pre-lifecycle boot configBootConfigusePreference('BootConfig.*')
Any other command-style call into main (window/system/shell/notification/external/file)IpcApiipcApi.request / useIpcOn

Decision rule: SQLite data → DataApi; user setting → Preference; losable/shared state → Cache; pre-lifecycle config → BootConfig; everything else, every imperative capability call into main → IpcApi. Same BeforeReady phase does not mean same responsibility — the boundary is responsibility (data/state/config vs command), not phase.

Naming Quick Reference

ConceptIdentifier
Product nameIpcApi
ChannelsIpcApi_Request (ipc-api:request) / IpcApi_Event (ipc-api:event)
Main coordinatorIpcApiService (request dispatch + broadcast/send)
Preload bridgewindow.api.ipcApi ({ request, on })
Renderer facadeipcApi (ipcApi.request('window.set_minimum_size', x)) + useIpcOn
Route / event namesdot snake_case, any depth ≥ 2 (file.read_doc, ai.agent.task.create); resource path first, verb last; payload fields stay camelCase
Request schemas*RequestSchemasipcRequestSchemas / IpcRoute
Event contracts (pure types)*EventSchemasIpcEventSchemas / IpcEventName
Router / handlers / errorIpcRouter / ipcHandlers / IpcError
Directoriessrc/{shared,main,renderer}/ipc/, src/preload/ipc.ts

Source Map

FileRole
src/shared/ipc/define.tsdefineRoute + RouteDef
src/shared/ipc/schemas/ipcSchemas.tsipcRequestSchemas / IpcRoute / IpcEventSchemas / IpcEventName
src/shared/ipc/types.tsInputFor / OutputFor / EventPayload / IpcHandlersFor / IpcContext / WindowId
src/shared/ipc/errors/IpcError.tsframework core: IpcError / IpcErrorCode (framework codes, single source of truth) / SerializedIpcError / IpcResult
src/shared/ipc/errors/<domain>.tsper-domain error-code maps (as const), imported directly by handler + renderer — errors/ has no aggregating barrel
src/main/ipc/IpcRouter.tsrequest router (key lookup + zod parse + dispatch)
src/main/ipc/IpcApiService.tsBeforeReady coordinator: handler registration + broadcast/send
src/main/core/security/validateSender.tssource-trust gate (validateSender / isAppRendererUrl), shared with the DataApi IpcAdapter, the Preference/Cache subsystem handlers, and the will-navigate guards
src/main/ipc/handlers/ipcHandlers.tsglobal ipcHandlers (exhaustive, the audited exposure surface)
src/preload/ipc.tsgeneric forwarder → window.api.ipcApi
src/renderer/ipc/index.tstyped facade ipcApi
src/renderer/ipc/useIpcOn.tsevent subscription hook