ccc-notifier 0.1.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/LICENSE +21 -0
- package/README.md +171 -0
- package/dist/budget-5H2GQRXH.js +71 -0
- package/dist/chunk-4JNZ7BHO.js +276 -0
- package/dist/chunk-64N5SGTT.js +1024 -0
- package/dist/chunk-DGXUSPS4.js +21 -0
- package/dist/chunk-IIYMGLV4.js +309 -0
- package/dist/chunk-KUJZYZSG.js +66 -0
- package/dist/chunk-L5MLVTA2.js +393 -0
- package/dist/chunk-OEG3AVU6.js +436 -0
- package/dist/chunk-TB5A7U7G.js +82 -0
- package/dist/chunk-TBFKGFZX.js +87 -0
- package/dist/cli.js +717 -0
- package/dist/dashboard-LKK6NOBN.js +14 -0
- package/dist/history-MWFHO5VR.js +114 -0
- package/dist/mute-2JW3NQXN.js +12 -0
- package/dist/setup-RT62VO37.js +15 -0
- package/dist/sweep-XKKODGMA.js +460 -0
- package/dist/track-PBHTVZ3L.js +168 -0
- package/package.json +53 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/env.ts
|
|
4
|
+
import { readFileSync } from "fs";
|
|
5
|
+
function isWSL() {
|
|
6
|
+
const forced = process.env.CCCN_FORCE_WSL;
|
|
7
|
+
if (forced === "1") return true;
|
|
8
|
+
if (forced === "0") return false;
|
|
9
|
+
if (process.platform !== "linux") return false;
|
|
10
|
+
if (process.env.WSL_DISTRO_NAME) return true;
|
|
11
|
+
try {
|
|
12
|
+
const v = readFileSync("/proc/version", "utf8").toLowerCase();
|
|
13
|
+
return v.includes("microsoft") || v.includes("wsl");
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
isWSL
|
|
21
|
+
};
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/store.ts
|
|
4
|
+
import {
|
|
5
|
+
existsSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
appendFileSync,
|
|
10
|
+
renameSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
statSync
|
|
13
|
+
} from "fs";
|
|
14
|
+
import { join } from "path";
|
|
15
|
+
import { homedir } from "os";
|
|
16
|
+
|
|
17
|
+
// src/types.ts
|
|
18
|
+
var DEFAULT_CONFIG = {
|
|
19
|
+
notify: { os: true, slack: null },
|
|
20
|
+
minNotifyUSD: 0,
|
|
21
|
+
costLabel: "api_equivalent",
|
|
22
|
+
fx: { fallbackRate: 150, cacheHours: 12 },
|
|
23
|
+
includeDailyTotal: true,
|
|
24
|
+
monthlyBudgetUSD: 0,
|
|
25
|
+
dashboard: { autoRegenerate: true, autoReloadSec: 30, days: 30 }
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/store.ts
|
|
29
|
+
var ERROR_LOG_MAX_BYTES = 1024 * 1024;
|
|
30
|
+
function paths() {
|
|
31
|
+
const home = process.env.CCCN_HOME || join(homedir(), ".ccc-notifier");
|
|
32
|
+
const cacheDir = join(home, "cache");
|
|
33
|
+
mkdirSync(home, { recursive: true });
|
|
34
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
35
|
+
return {
|
|
36
|
+
home,
|
|
37
|
+
configFile: join(home, "config.json"),
|
|
38
|
+
historyFile: join(home, "history.jsonl"),
|
|
39
|
+
cursorsFile: join(home, "cursors.json"),
|
|
40
|
+
cacheDir,
|
|
41
|
+
errorLog: join(home, "error.log"),
|
|
42
|
+
lastNotifyFile: join(home, "last-notify.json"),
|
|
43
|
+
muteFile: join(home, "muted.json")
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function isPlainObject(v) {
|
|
47
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
48
|
+
}
|
|
49
|
+
function mergeConfig(partial) {
|
|
50
|
+
const result = structuredClone(DEFAULT_CONFIG);
|
|
51
|
+
if (!isPlainObject(partial)) return result;
|
|
52
|
+
if (isPlainObject(partial.notify)) {
|
|
53
|
+
if ("os" in partial.notify) {
|
|
54
|
+
result.notify.os = partial.notify.os;
|
|
55
|
+
}
|
|
56
|
+
if ("slack" in partial.notify) {
|
|
57
|
+
result.notify.slack = partial.notify.slack;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if ("minNotifyUSD" in partial) {
|
|
61
|
+
result.minNotifyUSD = partial.minNotifyUSD;
|
|
62
|
+
}
|
|
63
|
+
if ("costLabel" in partial) {
|
|
64
|
+
result.costLabel = partial.costLabel;
|
|
65
|
+
}
|
|
66
|
+
if (isPlainObject(partial.fx)) {
|
|
67
|
+
if ("fallbackRate" in partial.fx) {
|
|
68
|
+
result.fx.fallbackRate = partial.fx.fallbackRate;
|
|
69
|
+
}
|
|
70
|
+
if ("cacheHours" in partial.fx) {
|
|
71
|
+
result.fx.cacheHours = partial.fx.cacheHours;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if ("includeDailyTotal" in partial) {
|
|
75
|
+
result.includeDailyTotal = partial.includeDailyTotal;
|
|
76
|
+
}
|
|
77
|
+
if ("monthlyBudgetUSD" in partial) {
|
|
78
|
+
const b = partial.monthlyBudgetUSD;
|
|
79
|
+
if (typeof b === "number" && Number.isFinite(b) && b >= 0) {
|
|
80
|
+
result.monthlyBudgetUSD = b;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (isPlainObject(partial.dashboard)) {
|
|
84
|
+
if ("autoRegenerate" in partial.dashboard) {
|
|
85
|
+
result.dashboard.autoRegenerate = partial.dashboard.autoRegenerate;
|
|
86
|
+
}
|
|
87
|
+
if ("autoReloadSec" in partial.dashboard) {
|
|
88
|
+
result.dashboard.autoReloadSec = partial.dashboard.autoReloadSec;
|
|
89
|
+
}
|
|
90
|
+
if ("days" in partial.dashboard) {
|
|
91
|
+
result.dashboard.days = partial.dashboard.days;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function readConfig() {
|
|
97
|
+
const p = paths();
|
|
98
|
+
if (!existsSync(p.configFile)) {
|
|
99
|
+
return structuredClone(DEFAULT_CONFIG);
|
|
100
|
+
}
|
|
101
|
+
let raw;
|
|
102
|
+
try {
|
|
103
|
+
raw = readFileSync(p.configFile, "utf8");
|
|
104
|
+
} catch (err) {
|
|
105
|
+
logError("readConfig", err);
|
|
106
|
+
return structuredClone(DEFAULT_CONFIG);
|
|
107
|
+
}
|
|
108
|
+
let parsed;
|
|
109
|
+
try {
|
|
110
|
+
parsed = JSON.parse(raw);
|
|
111
|
+
} catch (err) {
|
|
112
|
+
logError("readConfig", err);
|
|
113
|
+
return structuredClone(DEFAULT_CONFIG);
|
|
114
|
+
}
|
|
115
|
+
return mergeConfig(parsed);
|
|
116
|
+
}
|
|
117
|
+
function readMuteState() {
|
|
118
|
+
const p = paths();
|
|
119
|
+
if (!existsSync(p.muteFile)) return null;
|
|
120
|
+
try {
|
|
121
|
+
const parsed = JSON.parse(readFileSync(p.muteFile, "utf8"));
|
|
122
|
+
if (!isPlainObject(parsed) || !("until" in parsed)) return null;
|
|
123
|
+
const until = parsed.until;
|
|
124
|
+
if (until === null) return { until: null };
|
|
125
|
+
if (typeof until === "string" && !Number.isNaN(new Date(until).getTime())) {
|
|
126
|
+
return { until };
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
} catch (err) {
|
|
130
|
+
logError("readMuteState", err);
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function isMuted(now = /* @__PURE__ */ new Date()) {
|
|
135
|
+
const state = readMuteState();
|
|
136
|
+
if (state === null) return false;
|
|
137
|
+
if (state.until === null) return true;
|
|
138
|
+
return new Date(state.until).getTime() > now.getTime();
|
|
139
|
+
}
|
|
140
|
+
function writeMuteState(state) {
|
|
141
|
+
writeFileSync(paths().muteFile, `${JSON.stringify(state)}
|
|
142
|
+
`, "utf8");
|
|
143
|
+
}
|
|
144
|
+
function clearMuteState() {
|
|
145
|
+
rmSync(paths().muteFile, { force: true });
|
|
146
|
+
}
|
|
147
|
+
function loadCursor(transcriptPath) {
|
|
148
|
+
const p = paths();
|
|
149
|
+
if (!existsSync(p.cursorsFile)) return null;
|
|
150
|
+
let raw;
|
|
151
|
+
try {
|
|
152
|
+
raw = readFileSync(p.cursorsFile, "utf8");
|
|
153
|
+
} catch (err) {
|
|
154
|
+
logError("loadCursor", err);
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
let parsed;
|
|
158
|
+
try {
|
|
159
|
+
parsed = JSON.parse(raw);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
logError("loadCursor", err);
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
if (!isPlainObject(parsed)) {
|
|
165
|
+
logError("loadCursor", new Error("cursors.json root is not an object"));
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const cursor = parsed[transcriptPath];
|
|
169
|
+
return cursor ?? null;
|
|
170
|
+
}
|
|
171
|
+
function sanitizeCursor(raw) {
|
|
172
|
+
if (!isPlainObject(raw)) return null;
|
|
173
|
+
const { offset, lastUuid, lastTs, seenMessageKeys } = raw;
|
|
174
|
+
if (typeof offset !== "number" || !Number.isFinite(offset)) return null;
|
|
175
|
+
if (lastUuid !== null && typeof lastUuid !== "string") return null;
|
|
176
|
+
if (lastTs !== null && typeof lastTs !== "string") return null;
|
|
177
|
+
if (!Array.isArray(seenMessageKeys)) return null;
|
|
178
|
+
const keys = [];
|
|
179
|
+
for (const key of seenMessageKeys) {
|
|
180
|
+
if (typeof key !== "string") return null;
|
|
181
|
+
keys.push(key);
|
|
182
|
+
}
|
|
183
|
+
return { offset, lastUuid, lastTs, seenMessageKeys: keys };
|
|
184
|
+
}
|
|
185
|
+
function saveCursor(transcriptPath, c) {
|
|
186
|
+
const p = paths();
|
|
187
|
+
let dict = {};
|
|
188
|
+
if (existsSync(p.cursorsFile)) {
|
|
189
|
+
try {
|
|
190
|
+
const raw = readFileSync(p.cursorsFile, "utf8");
|
|
191
|
+
const parsed = JSON.parse(raw);
|
|
192
|
+
if (isPlainObject(parsed)) {
|
|
193
|
+
dict = parsed;
|
|
194
|
+
}
|
|
195
|
+
} catch (err) {
|
|
196
|
+
logError("saveCursor", err);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
dict[transcriptPath] = c;
|
|
200
|
+
const tmpFile = `${p.cursorsFile}.tmp`;
|
|
201
|
+
writeFileSync(tmpFile, JSON.stringify(dict), "utf8");
|
|
202
|
+
renameSync(tmpFile, p.cursorsFile);
|
|
203
|
+
}
|
|
204
|
+
function appendTurn(record) {
|
|
205
|
+
const p = paths();
|
|
206
|
+
appendFileSync(p.historyFile, JSON.stringify(record) + "\n", "utf8");
|
|
207
|
+
}
|
|
208
|
+
function readTurns(days) {
|
|
209
|
+
const p = paths();
|
|
210
|
+
if (!existsSync(p.historyFile)) return [];
|
|
211
|
+
let raw;
|
|
212
|
+
try {
|
|
213
|
+
raw = readFileSync(p.historyFile, "utf8");
|
|
214
|
+
} catch {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
const cutoff = typeof days === "number" ? Date.now() - days * 864e5 : null;
|
|
218
|
+
const result = [];
|
|
219
|
+
for (const line of raw.split("\n")) {
|
|
220
|
+
const trimmed = line.trim();
|
|
221
|
+
if (!trimmed) continue;
|
|
222
|
+
let rec;
|
|
223
|
+
try {
|
|
224
|
+
rec = JSON.parse(trimmed);
|
|
225
|
+
} catch {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (cutoff !== null) {
|
|
229
|
+
const ts = Date.parse(rec.ts);
|
|
230
|
+
if (!Number.isFinite(ts) || ts < cutoff) continue;
|
|
231
|
+
}
|
|
232
|
+
result.push(rec);
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
function todayTotalUSD() {
|
|
237
|
+
const now = /* @__PURE__ */ new Date();
|
|
238
|
+
const y = now.getFullYear();
|
|
239
|
+
const m = now.getMonth();
|
|
240
|
+
const d = now.getDate();
|
|
241
|
+
let total = 0;
|
|
242
|
+
for (const rec of readTurns()) {
|
|
243
|
+
const ts = new Date(rec.ts);
|
|
244
|
+
if (Number.isNaN(ts.getTime())) continue;
|
|
245
|
+
if (ts.getFullYear() === y && ts.getMonth() === m && ts.getDate() === d) {
|
|
246
|
+
total += rec.costUSD;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return total;
|
|
250
|
+
}
|
|
251
|
+
function currentMonthTotals() {
|
|
252
|
+
const now = /* @__PURE__ */ new Date();
|
|
253
|
+
const y = now.getFullYear();
|
|
254
|
+
const m = now.getMonth();
|
|
255
|
+
let usd = 0;
|
|
256
|
+
let jpy = 0;
|
|
257
|
+
let turns = 0;
|
|
258
|
+
for (const rec of readTurns()) {
|
|
259
|
+
const ts = new Date(rec.ts);
|
|
260
|
+
if (Number.isNaN(ts.getTime())) continue;
|
|
261
|
+
if (ts.getFullYear() === y && ts.getMonth() === m) {
|
|
262
|
+
const sa = rec.subagents?.costUSD ?? 0;
|
|
263
|
+
usd += rec.costUSD + sa;
|
|
264
|
+
jpy += rec.costJPY + sa * rec.fxRate;
|
|
265
|
+
turns += 1;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return { usd, jpy, turns };
|
|
269
|
+
}
|
|
270
|
+
function logError(context, err) {
|
|
271
|
+
try {
|
|
272
|
+
const p = paths();
|
|
273
|
+
const iso = (/* @__PURE__ */ new Date()).toISOString();
|
|
274
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
275
|
+
const stack = err instanceof Error ? err.stack : void 0;
|
|
276
|
+
let entry = `[${iso}] [${context}] ${message}
|
|
277
|
+
`;
|
|
278
|
+
if (stack) {
|
|
279
|
+
entry += `${stack}
|
|
280
|
+
`;
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const stat = statSync(p.errorLog);
|
|
284
|
+
if (stat.size > ERROR_LOG_MAX_BYTES) {
|
|
285
|
+
renameSync(p.errorLog, `${p.errorLog}.old`);
|
|
286
|
+
}
|
|
287
|
+
} catch {
|
|
288
|
+
}
|
|
289
|
+
appendFileSync(p.errorLog, entry, "utf8");
|
|
290
|
+
} catch {
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export {
|
|
295
|
+
paths,
|
|
296
|
+
readConfig,
|
|
297
|
+
readMuteState,
|
|
298
|
+
isMuted,
|
|
299
|
+
writeMuteState,
|
|
300
|
+
clearMuteState,
|
|
301
|
+
loadCursor,
|
|
302
|
+
sanitizeCursor,
|
|
303
|
+
saveCursor,
|
|
304
|
+
appendTurn,
|
|
305
|
+
readTurns,
|
|
306
|
+
todayTotalUSD,
|
|
307
|
+
currentMonthTotals,
|
|
308
|
+
logError
|
|
309
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
clearMuteState,
|
|
4
|
+
isMuted,
|
|
5
|
+
readMuteState,
|
|
6
|
+
writeMuteState
|
|
7
|
+
} from "./chunk-IIYMGLV4.js";
|
|
8
|
+
|
|
9
|
+
// src/mute.ts
|
|
10
|
+
function parseDuration(arg) {
|
|
11
|
+
const m = /^(\d+)([mhd])$/.exec(arg);
|
|
12
|
+
if (!m) return null;
|
|
13
|
+
const n = Number.parseInt(m[1], 10);
|
|
14
|
+
if (!Number.isFinite(n) || n <= 0) return null;
|
|
15
|
+
const unitMs = m[2] === "m" ? 6e4 : m[2] === "h" ? 36e5 : 864e5;
|
|
16
|
+
return n * unitMs;
|
|
17
|
+
}
|
|
18
|
+
function fmtMuteUntil(iso) {
|
|
19
|
+
const d = new Date(iso);
|
|
20
|
+
if (Number.isNaN(d.getTime())) return iso;
|
|
21
|
+
const pad = (v) => String(v).padStart(2, "0");
|
|
22
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
23
|
+
}
|
|
24
|
+
function runMute(args) {
|
|
25
|
+
const duration = args[0];
|
|
26
|
+
if (duration === void 0) {
|
|
27
|
+
writeMuteState({ until: null });
|
|
28
|
+
console.log("\u901A\u77E5\u3092\u505C\u6B62\u3057\u307E\u3057\u305F(\u7121\u671F\u9650)\u3002\u518D\u958B\u3059\u308B\u306B\u306F ccc-notifier unmute \u3092\u5B9F\u884C\u3057\u3066\u304F\u3060\u3055\u3044\u3002");
|
|
29
|
+
console.log("Notifications muted indefinitely. Run `ccc-notifier unmute` to resume.");
|
|
30
|
+
console.log("\u203B \u30B3\u30B9\u30C8\u306E\u8A18\u9332\u30FB\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9\u66F4\u65B0\u306F\u7D9A\u304D\u307E\u3059 / cost tracking continues.");
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
const ms = parseDuration(duration);
|
|
34
|
+
if (ms === null) {
|
|
35
|
+
console.error(`\u671F\u9593\u306E\u5F62\u5F0F\u304C\u4E0D\u6B63\u3067\u3059: ${duration}(\u4F8B: 30m / 2h / 1d)`);
|
|
36
|
+
console.error(`Invalid duration: ${duration} (examples: 30m / 2h / 1d)`);
|
|
37
|
+
return 1;
|
|
38
|
+
}
|
|
39
|
+
const until = new Date(Date.now() + ms).toISOString();
|
|
40
|
+
writeMuteState({ until });
|
|
41
|
+
console.log(`\u901A\u77E5\u3092\u505C\u6B62\u3057\u307E\u3057\u305F(${fmtMuteUntil(until)} \u307E\u3067)\u3002\u305D\u308C\u4EE5\u964D\u306F\u81EA\u52D5\u3067\u518D\u958B\u3057\u307E\u3059\u3002`);
|
|
42
|
+
console.log(`Notifications muted until ${fmtMuteUntil(until)} (local time), then resume automatically.`);
|
|
43
|
+
console.log("\u203B \u30B3\u30B9\u30C8\u306E\u8A18\u9332\u30FB\u30C0\u30C3\u30B7\u30E5\u30DC\u30FC\u30C9\u66F4\u65B0\u306F\u7D9A\u304D\u307E\u3059 / cost tracking continues.");
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
function runUnmute() {
|
|
47
|
+
const state = readMuteState();
|
|
48
|
+
if (state === null) {
|
|
49
|
+
console.log("\u901A\u77E5\u306F\u505C\u6B62\u3055\u308C\u3066\u3044\u307E\u305B\u3093(\u30DF\u30E5\u30FC\u30C8\u306A\u3057)\u3002/ Notifications are not muted.");
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
const wasActive = isMuted();
|
|
53
|
+
clearMuteState();
|
|
54
|
+
if (wasActive) {
|
|
55
|
+
console.log("\u901A\u77E5\u3092\u518D\u958B\u3057\u307E\u3057\u305F\u3002/ Notifications resumed.");
|
|
56
|
+
} else {
|
|
57
|
+
console.log("\u671F\u9650\u5207\u308C\u306E\u30DF\u30E5\u30FC\u30C8\u3092\u524A\u9664\u3057\u307E\u3057\u305F(\u901A\u77E5\u306F\u3059\u3067\u306B\u518D\u958B\u3057\u3066\u3044\u307E\u3059)\u3002/ Cleared an expired mute.");
|
|
58
|
+
}
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export {
|
|
63
|
+
fmtMuteUntil,
|
|
64
|
+
runMute,
|
|
65
|
+
runUnmute
|
|
66
|
+
};
|