Files
yun-xue-tang-assistant/runner/notify.mjs
T
voidandClaude Sonnet 4.6 0eb4c40629 feat: 每日积分模式与 Bark 完成提醒
- 按课时类型计分:文档 1 / 视频 3 / 音频 2 / 微课 2,每次 3 分,单日封顶 24 分;满额换类,无内容即停
- 积分计数落 localStorage,跨刷新/重启保留、跨天清零;面板与无头 TUI 都显示今日积分
- 运行器自己收尾时通过 Bark 推送今日积分、完成门数、用时和结束原因;手动停止与 --check 不推送
- 新增 --points-mode / --bark-* 参数及对应 YUN_XUE_TANG_* 环境变量
- 补充 points / notify 离线回归测试与 OpenSpec 配置

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-01 22:19:52 +08:00

115 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Bark 推送:运行器自己收尾时把结果发到手机。
//
// 只覆盖「运行器自己结束」的场景(正常刷完 / 长时间无进展 / 登录失败),
// 手动 Ctrl+C 主动停止不发:人就在键盘前,不需要再提醒一次。
// 通知是附加信息:发送失败只记告警,绝不影响退出码,也不阻塞下一次会话。
//
// Bark 接口:GET https://api.day.app/<key>/<title>/<body>?group=<分组>&ttl=<秒>
/** 单次请求超时:Bark 正常在 1 秒内返回,卡住就当失败,别拖慢收尾 */
export const BARK_TIMEOUT_MS = 8000;
function withParam(url, key, val) {
if (val === '' || val == null) return url;
// 地址里已经写了同名参数(例如配置里直接粘了完整 curl 地址)就不再追加
if (new RegExp('[?&]' + key + '=').test(url)) return url;
return url + (url.includes('?') ? '&' : '?') + key + '=' + encodeURIComponent(val);
}
/**
* 拼 Bark 请求地址。
* base 可以是:
* · 含设备 key 的根地址:https://api.day.app/KEY
* · 带 {title}/{body} 占位符的完整模板:https://api.day.app/KEY/{title}/{body}?group=x
*/
export function buildBarkUrl(base, title, body, opts = {}) {
let url = String(base || '').trim();
if (!url) return '';
const t = encodeURIComponent(title || '');
const b = encodeURIComponent(body || '');
if (/\{title\}|\{body\}/.test(url)) {
url = url.replace(/\{title\}/g, t).replace(/\{body\}/g, b);
} else {
url = url.replace(/\/+$/, '') + '/' + t + '/' + b;
}
url = withParam(url, 'group', opts.group);
url = withParam(url, 'ttl', opts.ttl ? String(opts.ttl) : '');
return url;
}
/** 收尾通知的标题与正文:把本次刷课的结论压缩成手机上能一眼看懂的两三行 */
export function buildFinishMessage(result) {
const r = result || {};
const kind = r.kind || 'done';
const title = kind === 'login'
? '云学堂助手:登录失败'
: kind === 'stalled' ? '云学堂助手:长时间无进展'
: '云学堂助手:任务完成';
const lines = [];
// 积分模式关闭时 snapshot 里仍是 0/24,直接贴出来会让人以为今天一条都没刷到
if (r.points && r.points.enabled === false) lines.push('每日积分模式:关');
else if (r.points && r.points.text) lines.push(String(r.points.text));
// 登录失败时还没开始刷,「完成 0 门 · 用时 0 秒」只会让人以为跑过一轮
if (kind !== 'login') {
const secs = Number(r.elapsed) || 0;
const used = secs >= 60
? Math.floor(secs / 60) + ' 分 ' + (secs % 60) + ' 秒'
: secs + ' 秒';
lines.push('完成 ' + (Number(r.completed) || 0) + ' 门 · 用时 ' + used);
}
if (r.finishReason) lines.push(String(r.finishReason));
return { title, body: lines.join('\n') };
}
/**
* 发送 Bark 通知。永远不抛异常。
* @returns {Promise<{ok?:boolean, skipped?:boolean, status?:number}>}
*/
export async function sendBark(opts) {
const o = opts || {};
const log = o.log || (() => {});
const warn = o.warn || (() => {});
const url = buildBarkUrl(o.url, o.title, o.body, { group: o.group, ttl: o.ttl });
if (!url) return { skipped: true };
if (typeof fetch !== 'function') {
warn('当前 Node 没有全局 fetch(需要 Node 18+),跳过 Bark 通知');
return { skipped: true };
}
try {
const res = await fetch(url, {
method: 'GET',
signal: AbortSignal.timeout(o.timeoutMs || BARK_TIMEOUT_MS),
});
let data = null;
try { data = await res.json(); } catch (e) { /* 非 JSON 响应按状态码判断 */ }
const code = data && data.code != null ? Number(data.code) : 200;
if (!res.ok || code !== 200) {
warn('Bark 通知发送失败:HTTP ' + res.status +
(data && data.message ? '(' + data.message + ')' : ''));
return { ok: false, status: res.status };
}
log('已发送 Bark 通知:' + o.title);
return { ok: true, status: res.status };
} catch (e) {
warn('Bark 通知发送失败:' + (e && e.message ? e.message : e));
return { ok: false };
}
}
/** 按运行器配置发送收尾通知;没配 barkUrl 时是空操作 */
export function notifyRunFinished(cfg, result, io) {
if (!cfg || !cfg.BARK_URL) return Promise.resolve({ skipped: true });
const msg = buildFinishMessage(result);
return sendBark({
url: cfg.BARK_URL,
group: cfg.BARK_GROUP,
ttl: cfg.BARK_TTL,
title: msg.title,
body: msg.body,
log: io && io.log,
warn: io && io.warn,
});
}