feat: M1 — LinUCB bandit, RemotePolicy, Web Push, event bus
ML serving: - LinUCB contextual bandit (disjoint, d=5 features: hour_sin/cos, is_overdue, task_age, priority) - /score endpoint replaces stub random; /reward endpoint for online learning - Per-user model state persisted to disk as JSON (survives restarts) - venv at ml/serving/.venv; start with pnpm dev from ml/serving Recommender: - Todoist fetch now extracts features (is_overdue, task_age_days, priority) - RemotePolicy calls ml/serving with 3s timeout; falls back to RandomPolicy - Reward sent to /reward on feedback (done=+1, snooze=0, dismiss=-1) Web Push: - VAPID keys in config; push_subscriptions table in DB - POST/DELETE /api/push/subscribe; GET /api/push/vapid-public-key - Service worker (public/sw.js): push → showNotification, notificationclick → focus/open - "notify me" button on tip page; registers SW + subscribes on permission grant Event bus: - services/api/src/events/bus.ts: typed EventEmitter wrapper - Subjects: signals.tip.served, signals.tip.feedback, signals.task.synced - Same publish/subscribe API NATS JetStream will implement — swap is mechanical Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
25
apps/web/public/sw.js
Normal file
25
apps/web/public/sw.js
Normal file
@@ -0,0 +1,25 @@
|
||||
self.addEventListener('push', (event) => {
|
||||
const data = event.data?.json() ?? {};
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(data.title ?? 'oO', {
|
||||
body: data.body ?? '',
|
||||
icon: '/icon-192.png',
|
||||
badge: '/icon-192.png',
|
||||
data: { url: data.url ?? '/tip' },
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', (event) => {
|
||||
event.notification.close();
|
||||
event.waitUntil(
|
||||
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
|
||||
for (const client of list) {
|
||||
if (client.url.includes(self.location.origin) && 'focus' in client) {
|
||||
return client.focus();
|
||||
}
|
||||
}
|
||||
return clients.openWindow(event.notification.data?.url ?? '/tip');
|
||||
})
|
||||
);
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState, useRef, useCallback } from 'react';
|
||||
import { getRecommendation, sendFeedback } from '@/lib/api';
|
||||
import { getRecommendation, sendFeedback, getVapidPublicKey, subscribePush } from '@/lib/api';
|
||||
import type { Tip } from '@oo/shared-types';
|
||||
|
||||
type State = 'loading' | 'tip' | 'empty' | 'actions' | 'done';
|
||||
@@ -30,6 +30,7 @@ export default function TipPage() {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const holdTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const [pressed, setPressed] = useState(false);
|
||||
const [pushState, setPushState] = useState<'idle' | 'subscribed' | 'denied'>('idle');
|
||||
|
||||
// Fade in after state change settles
|
||||
useEffect(() => {
|
||||
@@ -60,6 +61,31 @@ export default function TipPage() {
|
||||
|
||||
useEffect(() => { loadTip(); }, [loadTip]);
|
||||
|
||||
// Check existing push permission on mount
|
||||
useEffect(() => {
|
||||
if (typeof Notification !== 'undefined' && Notification.permission === 'granted') {
|
||||
setPushState('subscribed');
|
||||
} else if (typeof Notification !== 'undefined' && Notification.permission === 'denied') {
|
||||
setPushState('denied');
|
||||
}
|
||||
}, []);
|
||||
|
||||
const requestPush = useCallback(async () => {
|
||||
if (!('serviceWorker' in navigator) || !('PushManager' in window)) return;
|
||||
const permission = await Notification.requestPermission();
|
||||
if (permission !== 'granted') { setPushState('denied'); return; }
|
||||
try {
|
||||
const reg = await navigator.serviceWorker.register('/sw.js');
|
||||
const vapidKey = await getVapidPublicKey();
|
||||
const sub = await reg.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: vapidKey,
|
||||
});
|
||||
await subscribePush(sub.toJSON());
|
||||
setPushState('subscribed');
|
||||
} catch { setPushState('denied'); }
|
||||
}, []);
|
||||
|
||||
const react = async (action: 'done' | 'dismiss' | 'snooze') => {
|
||||
if (!tip) return;
|
||||
setVisible(false);
|
||||
@@ -161,6 +187,24 @@ export default function TipPage() {
|
||||
}}>
|
||||
hold to act
|
||||
</p>
|
||||
{pushState === 'idle' && (
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); requestPush(); }}
|
||||
style={{
|
||||
marginTop: '2.5rem',
|
||||
background: 'transparent',
|
||||
border: 'none',
|
||||
color: 'rgba(255,255,255,0.18)',
|
||||
fontSize: '0.65rem',
|
||||
letterSpacing: '0.12em',
|
||||
textTransform: 'uppercase',
|
||||
cursor: 'pointer',
|
||||
padding: 0,
|
||||
}}
|
||||
>
|
||||
notify me
|
||||
</button>
|
||||
)}
|
||||
</Fade>
|
||||
)}
|
||||
|
||||
|
||||
@@ -62,3 +62,22 @@ export async function deleteAccount() {
|
||||
export async function logout() {
|
||||
return apiFetch<{ ok: boolean }>('/auth/logout', { method: 'POST' });
|
||||
}
|
||||
|
||||
export async function getVapidPublicKey(): Promise<string> {
|
||||
const { key } = await apiFetch<{ key: string }>('/push/vapid-public-key');
|
||||
return key;
|
||||
}
|
||||
|
||||
export async function subscribePush(subscription: PushSubscriptionJSON) {
|
||||
return apiFetch<{ ok: boolean }>('/push/subscribe', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(subscription),
|
||||
});
|
||||
}
|
||||
|
||||
export async function unsubscribePush(endpoint: string) {
|
||||
return apiFetch<{ ok: boolean }>('/push/subscribe', {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify({ endpoint }),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user