feat: NATS JetStream + Todoist background sync (#21, #22)

Issue 21 — event infrastructure:
- NormalizedEvent<T> + payload types in packages/shared-types/src/events/
- Bus.onPublish() hook for side-effect bridges
- NATS JetStream adapter (services/api/src/events/nats.ts): connects when
  NATS_URL is set, creates signals.> and feedback.> streams, bridges all
  in-process bus publishes to JetStream — no-ops gracefully when NATS is absent
- NATS service added to docker-compose (profile: events|full, port 4222/8222)

Issue 22 — Todoist background sync:
- services/api/src/signals/scheduler.ts: queries all active-token users every
  15 min (TODOIST_SYNC_INTERVAL_MS), fan-out via todoistSource.fetchSignals()
  which emits signals.task.synced; on-demand fetch remains as freshness fallback
- NATS_URL + TODOIST_SYNC_INTERVAL_MS added to config

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-18 01:18:51 +00:00
parent e3ca3ba733
commit 2a7380933c
10 changed files with 267 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
/**
* NormalizedEvent — the durable envelope for all events flowing through
* the system. Today: in-process EventEmitter. Tomorrow: NATS JetStream.
*
* Subject taxonomy:
* signals.task.synced — Todoist (or other source) task list refreshed
* signals.tip.served — tip returned to client
* signals.tip.feedback — user reacted (done / dismiss / snooze / helpful / not_helpful)
* signals.tip.reward_failed — reward delivery to ml/serving failed after retries
* signals.integration.token_expired — OAuth token needs reconnect
*/
export interface NormalizedEvent<T = unknown> {
/** NATS-style subject: domain.entity.verb */
subject: string;
/** ISO 8601 timestamp */
ts: string;
/** Monotonically increasing sequence number (in-process ring; JetStream seq in prod) */
seq: number;
payload: T;
}
// ── Payload types ────────────────────────────────────────────────────────────
export interface TaskSyncedPayload {
userId: string;
source: string; // e.g. 'todoist'
count: number;
syncedAt: string;
}
export interface TipServedPayload {
userId: string;
tipId: string;
policy: string;
servedAt: string;
}
export interface TipFeedbackPayload {
userId: string;
tipId: string;
action: 'done' | 'dismiss' | 'snooze' | 'helpful' | 'not_helpful';
reward: number;
dwellMs: number | null;
createdAt: string;
}
export interface TipRewardFailedPayload {
userId: string;
tipId: string;
reward: number;
attempts: number;
error: string;
failedAt: string;
}
export interface IntegrationTokenExpiredPayload {
userId: string;
provider: string;
detectedAt: string;
}

View File

@@ -3,3 +3,4 @@ export * from './http/auth.js';
export * from './http/integrations.js';
export * from './http/user.js';
export * from './http/signal.js';
export * from './events/index.js';