@angelyeye/dsh-cost-tracker 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/config.js ADDED
@@ -0,0 +1,81 @@
1
+ // ============================================================
2
+ // DSH 花费统计插件 —— 配置层(纯逻辑,可独立测试)
3
+ //
4
+ // 峰谷计价提示相关的可持久化设置。默认值与参考项目 dsh-cost-meter
5
+ // 保持一致,便于用户获得一致的峰/谷切换提醒体验。
6
+ //
7
+ // 字段:
8
+ // peakEnabled 启用 DeepSeek 峰谷时段价格(影响计费 + 提示显隐)
9
+ // peakNotice 峰时高价时段显著提示(时段条显示)
10
+ // peakStyle 时段条样式:compact(简洁单行)/ classic(环形表盘,24h 中空圆环)
11
+ // peakShowTickLabels 环形表盘是否显示时间刻度(00:00–21:00),默认 true
12
+ // peakCompactStack 简洁样式下的「双行紧凑」:true 时改为上下布局(默认 false,左右单行)
13
+ // peakCompactOrder 双行紧凑的排布顺序:bar-first(时段条在上·文字在下,默认)/ text-first(文字在上·时段条在下)
14
+ // peakAlertEnabled 峰/谷切换前弹窗提醒
15
+ // peakAlertAhead 提前提醒分钟数(1-30,默认 2)
16
+ // peakAlertTarget 提醒类型:both(峰和谷)/ peak(进入峰时)/ offpeak(进入谷时)
17
+ // peakAlertPosition 弹窗位置:corner(右下角)/ center(屏幕中心)
18
+ // peakAlertWebNotify 同步发送浏览器系统通知
19
+ // peakEffectiveAt 峰谷计价生效时间(ISO 字符串,展示用)
20
+ // ============================================================
21
+
22
+ /** 默认峰谷计价生效时间(UTC;两档方案已即时生效,门控恒通过) */
23
+ export const DEFAULT_PEAK_EFFECTIVE_AT = '2026-08-01T00:00:00Z'
24
+
25
+ /** 峰值(读取)默认配置 */
26
+ export function defaultPeakConfig() {
27
+ return {
28
+ peakEnabled: true,
29
+ peakNotice: true,
30
+ peakStyle: 'compact',
31
+ peakShowTickLabels: true,
32
+ peakCompactStack: false,
33
+ peakCompactOrder: 'bar-first',
34
+ peakAlertEnabled: true,
35
+ peakAlertAhead: 2,
36
+ peakAlertTarget: 'both',
37
+ peakAlertPosition: 'corner',
38
+ peakAlertWebNotify: false,
39
+ peakEffectiveAt: DEFAULT_PEAK_EFFECTIVE_AT,
40
+ }
41
+ }
42
+
43
+ function bool(v, fallback) { return typeof v === 'boolean' ? v : fallback }
44
+ function intIn(v, lo, hi, fallback) { return typeof v === 'number' && Number.isInteger(v) && v >= lo && v <= hi ? v : fallback }
45
+
46
+ /**
47
+ * 规范化一份用户配置:只保留已知字段,非法值回退默认。
48
+ * @param {object} raw - 任意输入
49
+ * @returns 规范化后的配置对象
50
+ */
51
+ export function normalizePeakConfig(raw) {
52
+ const def = defaultPeakConfig()
53
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return def
54
+ const out = {
55
+ peakEnabled: bool(raw.peakEnabled, def.peakEnabled),
56
+ peakNotice: bool(raw.peakNotice, def.peakNotice),
57
+ peakStyle: raw.peakStyle === 'classic' ? 'classic' : 'compact',
58
+ peakShowTickLabels: bool(raw.peakShowTickLabels, def.peakShowTickLabels),
59
+ peakCompactStack: bool(raw.peakCompactStack, def.peakCompactStack),
60
+ peakCompactOrder: raw.peakCompactOrder === 'text-first' ? 'text-first' : 'bar-first',
61
+ peakAlertEnabled: bool(raw.peakAlertEnabled, def.peakAlertEnabled),
62
+ peakAlertAhead: intIn(raw.peakAlertAhead, 1, 30, def.peakAlertAhead),
63
+ peakAlertTarget: (raw.peakAlertTarget === 'peak' || raw.peakAlertTarget === 'offpeak') ? raw.peakAlertTarget : def.peakAlertTarget,
64
+ peakAlertPosition: raw.peakAlertPosition === 'center' ? 'center' : 'corner',
65
+ peakAlertWebNotify: bool(raw.peakAlertWebNotify, def.peakAlertWebNotify),
66
+ peakEffectiveAt: typeof raw.peakEffectiveAt === 'string' && raw.peakEffectiveAt.length > 0 ? raw.peakEffectiveAt : def.peakEffectiveAt,
67
+ }
68
+ return out
69
+ }
70
+
71
+ /**
72
+ * 峰谷计费是否已生效:peakEnabled 且当前时间 >= effectiveAt(生效前视为未启用)。
73
+ * @param {object} cfg - 规范化配置
74
+ * @param {number} now - epoch ms(缺省用当前时间)
75
+ */
76
+ export function peakEffective(cfg, now) {
77
+ if (!cfg || cfg.peakEnabled !== true) return false
78
+ const t = Date.parse((cfg && cfg.peakEffectiveAt) || '')
79
+ if (Number.isFinite(t) && now < t) return false
80
+ return true
81
+ }
@@ -0,0 +1,5 @@
1
+ # dsh-cost-tracker bundle patch: registers the plugin when installed via
2
+ # `dsh plugin add @angelyeye/dsh-cost-tracker` (or from this repo directly).
3
+ - insert:
4
+ - id: dsh-cost-tracker
5
+ name: "@angelyeye/dsh-cost-tracker"