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:
60
packages/shared-types/src/events/index.ts
Normal file
60
packages/shared-types/src/events/index.ts
Normal 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;
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user