The typed RPC contract between dashboard and service worker. Every variant of RpcRequest and RpcResponse. · dashboard 与 SW 之间的类型化 RPC 协议。
Single channel: chrome.runtime.sendMessage from dashboard → SW. The SW responds via the callback. There are no SW-initiated messages today (the dashboard polls or subscribes to storage changes when it needs cross-tab updates).
Both directions are typed via discriminated unions in src/shared/rpc.ts:
export type RpcRequest = { kind: 'recommend.open' } | { kind: 'recommend.cleanup' } | … ;
export type RpcResponse = { ok: true; kind: 'recommend.open'; data: OpenCandidate[] } | … | { ok: false; error: string };The callRpc<R>() helper wraps sendMessage, rejects on chrome.runtime.lastError or ok === false, and resolves with the typed response.
Dashboard-side wrappers in src/dashboard/api/recommendations.ts provide convenience functions like fetchOpenRecommendations(), reportCleanupFeedback(), etc. Components should use these wrappers, not call callRpc directly.
The SW-side dispatch is a single switch (req.kind) in src/background/messaging.ts.
Get the top open-recommendation candidates for the current dashboard view.
Request: { kind: 'recommend.open' }
Response: { ok: true; kind: 'recommend.open'; data: OpenCandidate[] }Side effect: records bandit impressions for the returned candidates.
Wrapper: fetchOpenRecommendations(). Used by Suggestions.tsx, OracleHint.tsx.
Get up to 5 cleanup candidates above the SCORE_THRESHOLD (0.55).
Request: { kind: 'recommend.cleanup' }
Response: { ok: true; kind: 'recommend.cleanup'; data: CleanupCandidate[] }Side effect: records bandit impressions.
Wrapper: fetchCleanupRecommendations(). Used by InlineCleanupCard.tsx.
Get all cleanup candidates above threshold (capped at 50). Powers the smart-cleanup batch in TabWall.
Request: { kind: 'recommend.cleanup.all'; limit?: number }
Response: { ok: true; kind: 'recommend.cleanup.all'; data: CleanupCandidate[] }limit is clamped to 50 server-side regardless of client value. Side effect: records bandit impressions.
Wrapper: fetchAllCleanupCandidates(). Used by TabWall.tsx.
Send explicit user feedback on a cleanup suggestion. Trains both the LR head and the bandit arm.
Request: {
kind: 'feedback.cleanup';
domain: string;
reason: string; // from CleanupCandidate.reason
features: CleanupFeatures; // from CleanupCandidate.features
action: 'accepted' | 'dismissed' | 'snoozed' | 'dismissed-after-suggestion';
}
Response: { ok: true; kind: 'ack' }Action semantics (and weights in trainCleanupFeedback):
| Action | Label | Weight | Bandit |
|---|---|---|---|
'accepted' |
1 | 1.0 | accept |
'dismissed' |
0 | 1.0 | dismiss |
'snoozed' |
0 | 0.5 | ignore (β += 0.5) |
'dismissed-after-suggestion' |
0 | 2.0 | dismiss |
Wrapper: reportCleanupFeedback(). Used by InlineCleanupCard (accept/dismiss/snooze) and TabWall.flushSmartCleanupFeedback (accepted vs dismissed-after-suggestion based on whether the user kept the AI's auto-check).
Send explicit feedback on an open recommendation (e.g., user clicked an OracleHint slot).
Request: {
kind: 'feedback.open';
domain: string;
features: RecommendFeatures;
action: 'accepted' | 'dismissed' | 'ignored';
}
Response: { ok: true; kind: 'ack' }Wrapper: reportOpenFeedback(). Used by OracleHint, Suggestions.
The insights bundle (heatmap, top domains, etc.).
Request: { kind: 'insights.get' }
Response: { ok: true; kind: 'insights.get'; data: InsightsBundle }Wrapper: fetchInsights(). Used by Insights.tsx.
Today recap (5 stats: tabs first-seen today, domains, focus minutes, top domain, busiest hour).
Request: { kind: 'insights.today' }
Response: { ok: true; kind: 'insights.today'; data: TodayRecap }Wrapper: fetchTodayRecap(). Used by TodayRecap.tsx. Refreshes every 60 s while the dashboard is mounted.
Run Head A's predictor on a list of pinned URLs and return them re-ordered.
Request: { kind: 'pins.rerank'; pins: PinRerankInput[] }
Response: { ok: true; kind: 'pins.rerank'; data: PinRerankRow[] }Each PinRerankRow includes the original URL plus the model's score. Caller (in usePins.ts) reorders the displayed pins accordingly. Throttled — see the 6-h drag cooldown.
Wrapper: rerankPinsViaModel().
Request: { kind: 'stash.add'; items: StashInput[] }
Response: { ok: true; kind: 'stash.add'; data: number[] } // ids of created rowsRequest: { kind: 'stash.list' }
Response: { ok: true; kind: 'stash.list'; data: StashedTab[] }Request: { kind: 'stash.unstash'; id: number }
Response: { ok: true; kind: 'ack' }Removes from stash and opens the URL in a new tab.
Request: { kind: 'stash.delete'; ids: number[] }
Response: { ok: true; kind: 'ack' }Wrappers: stashItems, listStashedItems, unstashItem, deleteStashedItems. Used by TabWall, StashSection.
Request: { kind: 'workspace.list' }
Response: { ok: true; kind: 'workspace.list'; data: Workspace[] }Request: { kind: 'workspace.save'; name: string; tabs: WorkspaceTab[] }
Response: { ok: true; kind: 'workspace.save'; data: number } // new idRequest: {
kind: 'workspace.update';
id: number;
name?: string;
tabs?: WorkspaceTab[];
}
Response: { ok: true; kind: 'ack' }Either name or tabs (or both) — for rename or capture-current-tabs.
Request: { kind: 'workspace.delete'; id: number }
Response: { ok: true; kind: 'ack' }Request: {
kind: 'workspace.restore';
id: number;
mode: 'newWindow' | 'currentWindow';
}
Response: { ok: true; kind: 'ack' }Wrappers: listWorkspaces, saveWorkspace, renameWorkspace, updateWorkspaceTabs, deleteWorkspaceById, restoreWorkspace.
Request: { kind: 'closeTabs'; tabIds: number[] }
Response: { ok: true; kind: 'ack' }Wrapper: closeTabs() in useTabs.ts.
Used by Augur AI when it needs the SW to open a URL (avoids cross-origin issues).
Request: { kind: 'openUrl'; url: string }
Response: { ok: true; kind: 'ack' }Wrapper: openUrlViaSw().
Counts and timestamps shown in Settings → Data.
Request: { kind: 'data.summary' }
Response: { ok: true; kind: 'data.summary'; data: DataSummary }Writes every pending in-memory model change to kv, so a page that reads the tables next sees current state. Returns the feature-name arrays for the debug-bundle manifest.
Request: { kind: 'data.flush' }
Response: { ok: true; kind: 'data.flush'; data: { featureNames: { cleanup: string[]; recommend: string[] } } }Discards pending model writes and every in-memory model cache, so nothing stale is written back over tables the page just replaced and the next use reloads from the database. The page calls it before and after an import.
Request: { kind: 'data.invalidate' }
Response: { ok: true; kind: 'ack' }Backup, restore and the debug bundle no longer go through RPC: the former data.export, data.import and data.exportDebugBundle carried the whole database in one message and failed past Chrome's 64 MiB message cap. The page reads and writes IndexedDB directly (shared/backup.ts) between these two calls.
Destructive — clears all Dexie tables, chrome.storage.session, and the onboarding flag.
Request: { kind: 'data.wipe' }
Response: { ok: true; kind: 'ack' }Softer than wipe — clears only the model + bandit + embedding KV keys. Events and aggregates are preserved.
Request: { kind: 'data.resetModels' }
Response: { ok: true; kind: 'ack' }Manually trigger the first-install history bootstrap. Pass force: true to re-seed (deletes prior bootstrap-tagged events first).
Request: { kind: 'data.bootstrapHistory'; force?: boolean }
Response: {
ok: true;
kind: 'data.bootstrapHistory';
data: { events: number; domains: number; skipped: boolean; reason?: string };
}skipped: true means the bootstrap didn't run (already done, no chrome.history API, or empty history). The optional reason field explains which.
Wrapper: seedFromBrowserHistory(). Used by Settings → Data → "Seed from browser history".
Live model weights for the debug panel.
Request: { kind: 'model.inspect' }
Response: { ok: true; kind: 'model.inspect'; data: ModelInspection }ModelInspection includes per-feature weights (sorted by magnitude) for both heads, Platt calibration parameters, sample counts, top bandit arms with α/β/mean, and embedding stats with top nearest-neighbor previews.
Wrapper: fetchModelInspection(). Used by ModelDebugPanel.tsx.
Manually trigger a skip-gram embedding retrain (otherwise runs every 12 h via embeddingRetrain alarm).
Request: { kind: 'embedding.retrain' }
Response: { ok: true; kind: 'embedding.retrain'; data: { steps: number; vocab: number } }Wrapper: retrainEmbedding(). Used by Settings → Advanced.
Rebuild db.domains and db.cooccurrence from db.events from scratch. Used after a wipe-then-restore or manual data import.
Request: { kind: 'aggregate.rebuild' }
Response: { ok: true; kind: 'ack' }Wrapper: rebuildAggregates().
Four touch points:
src/shared/rpc.ts— add the request variant toRpcRequest, the response variant toRpcResponse.src/background/messaging.ts— add acasein the switch that calls intoml/or wherever and returns the typed response.src/dashboard/api/recommendations.ts— add a wrapper function so callers don't have to deal withr.ok && r.kind === '…'everywhere.- UI usage — call the wrapper.
Type rules:
- The
kindstring literal in the response must match the request kind. The narrowing in dashboard wrappers depends on this. datais the only payload field on success. No naked unions likedata | null— usenullonly for genuine "no data available" cases (fetchTodayRecapreturnsnullwhen no events today).- All errors flow through the
{ ok: false; error: string }variant.callRpcrejects on this so call sites can use try/catch or.catch().
- ARCHITECTURE.md — RPC's place in the bigger picture
- ML.md — what
recommend.*,feedback.*,model.inspectoperate on - STORAGE.md — what
data.*reads and writes