web: notify failed and waiting runs from the browser Notification API (L5)
This commit is contained in:
+180
-1
@@ -174,6 +174,15 @@ import {
|
||||
decodeAttentionDismissals,
|
||||
encodeAttentionDismissals,
|
||||
} from "./attention.js";
|
||||
import {
|
||||
NOTIFICATION_RATIONALE,
|
||||
NOTIFY_STORAGE_KEY,
|
||||
createAlertLedger,
|
||||
decodeNotificationSettings,
|
||||
encodeNotificationSettings,
|
||||
notificationEnvironment,
|
||||
notificationPermissionState,
|
||||
} from "./notify.js";
|
||||
import {
|
||||
DEFAULT_SEARCH_KIND,
|
||||
DEFAULT_SEARCH_SORT,
|
||||
@@ -469,6 +478,10 @@ const state = {
|
||||
attentionDismissed: new Set(),
|
||||
attentionSheetOpen: false,
|
||||
attentionSnapshot: null,
|
||||
// Notifications (L5): whether the operator has already been asked, loaded once
|
||||
// at boot from localStorage. The browser owns the permission itself; this flag
|
||||
// only changes the honest wording in Settings and is never a claim of consent.
|
||||
notifyAsked: false,
|
||||
// Omni-search (S1/S2): the field/panel state, kept beside the view it never
|
||||
// changes, plus the pure debounce/stale-token sequencer. Opening or closing the
|
||||
// search leaves state.view, the selection and the loaded data untouched.
|
||||
@@ -540,6 +553,11 @@ const state = {
|
||||
chatSubscriptions: new Set(),
|
||||
};
|
||||
|
||||
// The run-notification ledger (L5). It is seeded from the authoritative run list
|
||||
// and from the subscribe snapshot, then observes pushed `run_event` frames; it
|
||||
// never feeds the runs list (that is R10's separate fold).
|
||||
const runAlerts = createAlertLedger();
|
||||
|
||||
let runPoll = null;
|
||||
|
||||
// One shared live clock for the runs list: it ticks only while a shown run is
|
||||
@@ -1006,6 +1024,76 @@ function loadAttentionDismissed() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fold one pushed `run_event` frame into the notification ledger (L5). It does
|
||||
* not touch the runs list (R10's fold is separate); it only decides whether this
|
||||
* transition raises exactly one notification. A run already failed/waiting when
|
||||
* seeded, a duplicate frame, a non-qualifying status and a key the operator
|
||||
* dismissed all produce nothing.
|
||||
*/
|
||||
function onRunAlertFrame(frame) {
|
||||
const alert = runAlerts.observe(frame, { dismissed: state.attentionDismissed });
|
||||
if (!alert || !notificationsAllowed()) return;
|
||||
try {
|
||||
const notification = new Notification(alert.title, { body: alert.body, tag: alert.key });
|
||||
notification.onclick = () => {
|
||||
window.focus();
|
||||
state.view = "runs";
|
||||
render();
|
||||
selectRun(alert.runId);
|
||||
};
|
||||
} catch {
|
||||
// Some browsers can throw even with permission granted; the in-page Needs
|
||||
// you surface remains the only honest channel and nothing is faked.
|
||||
}
|
||||
}
|
||||
|
||||
/** True only when a real browser notification can be constructed right now. */
|
||||
function notificationsAllowed() {
|
||||
return (
|
||||
typeof globalThis.Notification === "function" &&
|
||||
globalThis.isSecureContext === true &&
|
||||
globalThis.Notification.permission === "granted"
|
||||
);
|
||||
}
|
||||
|
||||
/** Persist only the asked flag; never a permission claim, endpoint or credential. */
|
||||
function persistNotifyAsked(asked) {
|
||||
state.notifyAsked = Boolean(asked);
|
||||
try {
|
||||
localStorage.setItem(NOTIFY_STORAGE_KEY, encodeNotificationSettings({ asked }));
|
||||
} catch {
|
||||
// Storage disabled or full: the in-memory flag still applies for this session.
|
||||
}
|
||||
}
|
||||
|
||||
/** The asked flag from storage at boot; anything unreadable means never asked. */
|
||||
function loadNotifyAsked() {
|
||||
try {
|
||||
return decodeNotificationSettings(localStorage.getItem(NOTIFY_STORAGE_KEY)).asked;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the run-event watch for this connected epoch: seed from the list that is
|
||||
* already in hand, treat the subscribe snapshot as seeds too (the server writes
|
||||
* it before the ack), then observe deltas. A refused subscribe only means no
|
||||
* pushes; the page keeps working and Settings stays honest.
|
||||
*/
|
||||
async function startRunAlertWatch() {
|
||||
runAlerts.seed(state.runs);
|
||||
runAlerts.beginSeed();
|
||||
try {
|
||||
await repo.subscribeRuns();
|
||||
} catch {
|
||||
// No live watch: notifications simply cannot raise on this connection.
|
||||
} finally {
|
||||
runAlerts.endSeed();
|
||||
}
|
||||
}
|
||||
|
||||
function render() {
|
||||
renderHeader();
|
||||
renderBanner();
|
||||
@@ -1111,7 +1199,12 @@ function tabButton(view, label) {
|
||||
*/
|
||||
function renderSettingsView() {
|
||||
const pane = h("div", { class: "settings" });
|
||||
pane.append(renderSettingsConnection(), renderSettingsWorkspace(), renderSettingsTemplates());
|
||||
pane.append(
|
||||
renderSettingsConnection(),
|
||||
renderSettingsWorkspace(),
|
||||
renderSettingsNotifications(),
|
||||
renderSettingsTemplates(),
|
||||
);
|
||||
return pane;
|
||||
}
|
||||
|
||||
@@ -1200,6 +1293,75 @@ function renderSettingsConnection() {
|
||||
return section;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Notifications section (L5). The Android rationale is shown verbatim; the
|
||||
* status line is the pure permission view for this page's real browser facts.
|
||||
* Only the explicit control asks for permission, never page load. When the API
|
||||
* is unavailable, the page is insecure or permission is denied, the wording says
|
||||
* plainly that the in-app Needs you surface is the only channel — no fake
|
||||
* notification is ever drawn.
|
||||
*/
|
||||
function renderSettingsNotifications() {
|
||||
const view = notificationPermissionState({
|
||||
...notificationEnvironment(),
|
||||
asked: state.notifyAsked,
|
||||
});
|
||||
const section = h("section", { class: "settings-section", id: "settingsNotifications" });
|
||||
section.append(h("h2", { class: "settings-title", text: "Notifications" }));
|
||||
section.append(h("p", { class: "settings-helper", text: NOTIFICATION_RATIONALE }));
|
||||
section.append(
|
||||
h("p", {
|
||||
id: "settingsNotificationState",
|
||||
class: `settings-notify-state settings-notify-${view.state}`,
|
||||
role: "status",
|
||||
text: view.body,
|
||||
}),
|
||||
);
|
||||
if (view.canRequest) {
|
||||
section.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "settings-actions" },
|
||||
h("button", {
|
||||
id: "settingsEnableNotifications",
|
||||
class: "primary",
|
||||
type: "button",
|
||||
text: view.actionLabel,
|
||||
onclick: requestNotifications,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
return section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask for notification permission from the operator's explicit click. The asked
|
||||
* flag is persisted first and the page re-renders from the browser's real answer;
|
||||
* a refused/dismissed prompt leaves the honest default/denied wording in place.
|
||||
*/
|
||||
function requestNotifications() {
|
||||
persistNotifyAsked(true);
|
||||
const ctor = globalThis.Notification;
|
||||
if (typeof ctor !== "function" || !globalThis.isSecureContext) {
|
||||
renderContent();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = ctor.requestPermission();
|
||||
if (result && typeof result.then === "function") {
|
||||
result.then(
|
||||
() => renderContent(),
|
||||
() => renderContent(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// A browser that refuses the request keeps the honest fallback wording.
|
||||
}
|
||||
renderContent();
|
||||
}
|
||||
|
||||
/** The trusted-network warning card, SettingsScreen.kt's TrustWarning verbatim. */
|
||||
function renderTrustWarning() {
|
||||
return h(
|
||||
@@ -6649,6 +6811,9 @@ async function onConnected() {
|
||||
// local, so that path is a single render and nothing is fetched twice).
|
||||
renderBanner();
|
||||
await loadCurrentView();
|
||||
// Seed from the authoritative list first, then watch: the subscribe snapshot
|
||||
// and its ack are treated as seeds, so a reconnect gap never notifies.
|
||||
await startRunAlertWatch();
|
||||
}
|
||||
|
||||
async function loadCurrentView(force = false) {
|
||||
@@ -6683,11 +6848,13 @@ async function loadCurrentView(force = false) {
|
||||
// history from one that has not arrived yet.
|
||||
const { runs, usage } = await repo.listRuns();
|
||||
state.runs = runs;
|
||||
runAlerts.seed(runs);
|
||||
if (usage) state.usage = usage;
|
||||
state.runsLoaded = true;
|
||||
} else {
|
||||
const { runs, usage } = await repo.listRuns();
|
||||
state.runs = runs;
|
||||
runAlerts.seed(runs);
|
||||
state.usage = usage;
|
||||
state.runsLoaded = true;
|
||||
}
|
||||
@@ -6931,6 +7098,10 @@ client.addEventListener("state", (event) => {
|
||||
// gone, so no thread keeps claiming to run until a fresh frame proves it.
|
||||
clearChatSubscriptions();
|
||||
settleChatThreads();
|
||||
// Drop the run-status baseline but keep this page session's notified keys:
|
||||
// the reconnect reseeds from the fresh list before it resubscribes, so a
|
||||
// transition hidden by the gap can never notify late.
|
||||
runAlerts.reset();
|
||||
}
|
||||
// A reconnect (not a first connect): re-attach the open chat's stream and
|
||||
// refresh its history wholesale, so a gap that swallowed the end of a turn is
|
||||
@@ -6949,6 +7120,13 @@ client.addEventListener("state", (event) => {
|
||||
client.addEventListener("frame", (event) => {
|
||||
const message = event.detail;
|
||||
if (!message) return;
|
||||
if (message.type === Protocol.RUN_EVENT) {
|
||||
// L5 only: observe the pushed transition for notifications. R10's runs-list
|
||||
// fold is deliberately not implemented here.
|
||||
const runFrame = message[Protocol.RUN_EVENT];
|
||||
if (runFrame) onRunAlertFrame(runFrame);
|
||||
return;
|
||||
}
|
||||
if (message.type === Protocol.CHAT_PERMISSION) {
|
||||
const permission = message[ChatKeys.PERMISSION];
|
||||
if (permission) onChatPermission(permission);
|
||||
@@ -7022,4 +7200,5 @@ window.addEventListener("online", onForegroundActive);
|
||||
// The attention surface's dismissed identities are loaded once here, before the
|
||||
// first render, so the bell's badge is correct from the very first paint.
|
||||
state.attentionDismissed = loadAttentionDismissed();
|
||||
state.notifyAsked = loadNotifyAsked();
|
||||
render();
|
||||
|
||||
Reference in New Issue
Block a user