Files
memind/wechat/handlers/health.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

74 lines
2.2 KiB
JavaScript

import { isMemindHealthEnabled } from '../../health-feature.mjs';
import { applyHealthWechatTurn } from '../../health-wechat-turn.mjs';
import { formatHealthAlertsPreamble, isHealthAlertsAckText } from '../../health-event-notification.mjs';
import { parseHealthAssetIdFromUrl } from '../../health-document-store.mjs';
export async function handleWechatHealthChannel({
enabled = isMemindHealthEnabled(),
userId,
intent,
inbound,
store,
observationService = null,
documentStore = null,
eventStore = null,
now = Date.now(),
} = {}) {
if (!enabled || !userId || !store) return null;
const existing = store.get(userId);
const session = existing?.expired ? null : existing?.session ?? null;
const text = String(intent?.agentText ?? intent?.displayText ?? '').trim();
const eventKey = String(inbound?.eventKey ?? '').trim();
const msgType = String(intent?.msgType ?? inbound?.msgType ?? 'text').toLowerCase();
const imageUrl = String(intent?.media?.publicUrl ?? intent?.imageUrl ?? '').trim();
if (session && isHealthAlertsAckText(text) && eventStore?.acknowledgeAll) {
await eventStore.acknowledgeAll(userId).catch(() => 0);
}
const result = applyHealthWechatTurn({
session,
text,
msgType,
eventKey,
now,
});
if (!result.handled) return null;
if (result.session) {
store.set(userId, result.session);
} else {
store.clear(userId);
}
if (result.commit?.type === 'document' && documentStore) {
if (imageUrl) {
await documentStore.insert(userId, {
confirmed: true,
imageUrl,
assetId: parseHealthAssetIdFromUrl(imageUrl),
source: 'wechat',
});
}
} else if (result.commit && observationService) {
await observationService.commitFromPending(
userId,
result.commit.values ?? result.commit,
result.commit.source ?? 'wechat',
);
}
let reply = result.reply;
const entered = !session && result.session;
if (entered && eventStore?.listUnreadAlerts) {
const alerts = await eventStore.listUnreadAlerts(userId, { limit: 10 }).catch(() => []);
const preamble = formatHealthAlertsPreamble(alerts);
if (preamble) {
reply = `${preamble}\n\n${reply ?? ''}`.trim();
}
}
return reply;
}