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:
52
services/api/src/events/bus.ts
Normal file
52
services/api/src/events/bus.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* EventBus — in-process today, NATS JetStream tomorrow.
|
||||
*
|
||||
* To swap to NATS: replace the EventEmitter body with a NATS JetStream
|
||||
* publish call. Subjects and payload shapes are the durable contract.
|
||||
*
|
||||
* Subjects follow the pattern: <domain>.<entity>.<verb>
|
||||
* signals.tip.served — a tip was returned to the client
|
||||
* signals.tip.feedback — user reacted (done / dismiss / snooze)
|
||||
* signals.task.synced — Todoist task list refreshed for a user
|
||||
*/
|
||||
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
export type TipServedEvent = {
|
||||
userId: string;
|
||||
tipId: string;
|
||||
policy: string;
|
||||
servedAt: string;
|
||||
};
|
||||
|
||||
export type TipFeedbackEvent = {
|
||||
userId: string;
|
||||
tipId: string;
|
||||
action: 'done' | 'dismiss' | 'snooze';
|
||||
reward: number;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
export type TaskSyncedEvent = {
|
||||
userId: string;
|
||||
count: number;
|
||||
syncedAt: string;
|
||||
};
|
||||
|
||||
type EventMap = {
|
||||
'signals.tip.served': TipServedEvent;
|
||||
'signals.tip.feedback': TipFeedbackEvent;
|
||||
'signals.task.synced': TaskSyncedEvent;
|
||||
};
|
||||
|
||||
class Bus extends EventEmitter {
|
||||
publish<K extends keyof EventMap>(subject: K, payload: EventMap[K]): void {
|
||||
this.emit(subject, payload);
|
||||
}
|
||||
|
||||
subscribe<K extends keyof EventMap>(subject: K, handler: (payload: EventMap[K]) => void): void {
|
||||
this.on(subject, handler);
|
||||
}
|
||||
}
|
||||
|
||||
export const bus = new Bus();
|
||||
Reference in New Issue
Block a user