Files
memind/health-share-service.mjs
T
john 2baf29b3ae feat(health): complete P0 health channel — baseline engine, page-data, MindSpace UI
Deliver encrypted health zone, observation API, baseline maturity pipeline,
page-data bindings, and H5/WeChat channel integration for health P0.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-09 18:04:57 +08:00

50 lines
2.0 KiB
JavaScript

import { randomBytes } from 'node:crypto';
import { buildHealthTimeline, summarizeTimelineForAssess } from './health-timeline.mjs';
export function createHealthShareService({ observationService, eventEngine, baselineEngine } = {}) {
if (!observationService) throw new Error('createHealthShareService requires observationService');
const tokens = new Map();
return {
async createReadonlySnapshot(userId, { ttlMs = 7 * 24 * 60 * 60 * 1000, now = Date.now() } = {}) {
const observations = await observationService.list(userId, { limit: 500 });
const baselines = baselineEngine?.computeHealthBaselines
? baselineEngine.computeHealthBaselines(observations, { now })
: [];
const events = eventEngine?.evaluateHealthEvents
? eventEngine.evaluateHealthEvents(observations, baselines, { now, mode: 'active' })
: [];
const timeline = buildHealthTimeline(observations);
const token = randomBytes(16).toString('hex');
const snapshot = {
userId: String(userId),
createdAt: now,
expiresAt: now + ttlMs,
summary: summarizeTimelineForAssess(timeline),
timeline: timeline.slice(0, 14),
openEvents: events.filter((e) => e.severity === 'watch' || e.severity === 'alert').slice(0, 10),
baselines: baselines.filter((b) => b.windowDays === 30 && b.sampleCount >= 7).slice(0, 12),
};
tokens.set(token, snapshot);
return { token, expiresAt: snapshot.expiresAt };
},
getSnapshot(token) {
const snapshot = tokens.get(String(token));
if (!snapshot) return null;
if (Date.now() > snapshot.expiresAt) {
tokens.delete(String(token));
return null;
}
return snapshot;
},
revoke(token) {
return tokens.delete(String(token));
},
};
}
export function buildMedicationTimelineHint() {
return '用药 Timeline 将在 P3 与 schedule-assistant 整合;当前请先在提醒助手中管理用药计划。';
}