ccc-notifier 0.3.0 → 0.4.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.
@@ -1,114 +0,0 @@
1
- #!/usr/bin/env node
2
- import {
3
- paths
4
- } from "./chunk-ECADO26T.js";
5
-
6
- // src/history.ts
7
- import { existsSync, readFileSync, writeFileSync, renameSync, rmSync } from "fs";
8
- import * as p from "@clack/prompts";
9
- function parseFlags(argv) {
10
- let days = null;
11
- let yes = false;
12
- for (let i = 0; i < argv.length; i++) {
13
- const a = argv[i];
14
- if (a === "--yes" || a === "-y") {
15
- yes = true;
16
- } else if (a === "--days") {
17
- const n = Number.parseInt(argv[i + 1] ?? "", 10);
18
- if (Number.isFinite(n) && n > 0) days = n;
19
- i++;
20
- } else if (a.startsWith("--days=")) {
21
- const n = Number.parseInt(a.slice("--days=".length), 10);
22
- if (Number.isFinite(n) && n > 0) days = n;
23
- }
24
- }
25
- return { days, yes };
26
- }
27
- function readLines(file) {
28
- const raw = readFileSync(file, "utf8");
29
- const lines = [];
30
- for (const line of raw.split("\n")) {
31
- if (!line.trim()) continue;
32
- let rec = null;
33
- try {
34
- rec = JSON.parse(line);
35
- } catch {
36
- rec = null;
37
- }
38
- lines.push({ raw: line, rec });
39
- }
40
- return lines;
41
- }
42
- function isTargeted(rec, cutoffMs) {
43
- if (cutoffMs === null) return true;
44
- const ts = Date.parse(rec.ts);
45
- if (!Number.isFinite(ts)) return false;
46
- return ts < cutoffMs;
47
- }
48
- function atomicWrite(file, content) {
49
- const tmp = `${file}.tmp`;
50
- writeFileSync(tmp, content, "utf8");
51
- renameSync(tmp, file);
52
- }
53
- async function runHistory(argv) {
54
- const [sub, ...rest] = argv;
55
- if (sub !== "clear" && sub !== "redact") {
56
- console.error(
57
- "\u4F7F\u3044\u65B9 / Usage: ccc-notifier history <clear|redact> [--days N] [--yes]\n clear \u2026 \u30EC\u30B3\u30FC\u30C9\u3054\u3068\u524A\u9664(\u30C1\u30E3\u30FC\u30C8\u30FB\u96C6\u8A08\u304B\u3089\u3082\u6D88\u3048\u308B) / delete records\n redact \u2026 \u30D7\u30ED\u30F3\u30D7\u30C8\u5168\u6587\u3060\u3051\u6D88\u3059(\u30B3\u30B9\u30C8\u30FB\u30C1\u30E3\u30FC\u30C8\u306F\u6B8B\u3059) / strip prompts only"
58
- );
59
- return 1;
60
- }
61
- const flags = parseFlags(rest);
62
- const file = paths().historyFile;
63
- if (!existsSync(file)) {
64
- console.log("\u5C65\u6B74\u304C\u3042\u308A\u307E\u305B\u3093(history.jsonl \u306F\u672A\u4F5C\u6210\u3067\u3059)\u3002");
65
- return 0;
66
- }
67
- const lines = readLines(file);
68
- const cutoff = flags.days !== null ? Date.now() - flags.days * 864e5 : null;
69
- const scope = flags.days !== null ? `${flags.days}\u65E5\u3088\u308A\u524D` : "\u5168\u671F\u9593";
70
- const targetSet = /* @__PURE__ */ new Set();
71
- for (let i = 0; i < lines.length; i++) {
72
- const rec = lines[i].rec;
73
- if (!rec || !isTargeted(rec, cutoff)) continue;
74
- if (sub === "redact" && !(typeof rec.prompt === "string" && rec.prompt.length > 0)) continue;
75
- targetSet.add(i);
76
- }
77
- if (targetSet.size === 0) {
78
- console.log(`\u5BFE\u8C61\u304C\u3042\u308A\u307E\u305B\u3093(${scope})\u3002`);
79
- return 0;
80
- }
81
- const action = sub === "clear" ? "\u30EC\u30B3\u30FC\u30C9\u3054\u3068\u524A\u9664" : "\u30D7\u30ED\u30F3\u30D7\u30C8\u5168\u6587\u3092\u6D88\u53BB";
82
- if (!flags.yes) {
83
- const confirmed = await p.confirm({
84
- message: `${scope}\u306E\u5C65\u6B74 ${targetSet.size} \u4EF6\u3092${action}\u3057\u307E\u3059\u3002\u5143\u306B\u623B\u305B\u307E\u305B\u3093\u3002\u3088\u308D\u3057\u3044\u3067\u3059\u304B?`,
85
- initialValue: false
86
- });
87
- if (p.isCancel(confirmed) || !confirmed) {
88
- p.cancel("\u30AD\u30E3\u30F3\u30BB\u30EB\u3057\u307E\u3057\u305F");
89
- return 0;
90
- }
91
- }
92
- if (sub === "clear") {
93
- const kept = lines.filter((_, i) => !targetSet.has(i)).map((l) => l.raw);
94
- if (kept.length === 0) {
95
- rmSync(file, { force: true });
96
- } else {
97
- atomicWrite(file, kept.join("\n") + "\n");
98
- }
99
- console.log(`\u5C65\u6B74 ${targetSet.size} \u4EF6\u3092\u524A\u9664\u3057\u307E\u3057\u305F(${scope})\u3002`);
100
- } else {
101
- const out = lines.map((l, i) => {
102
- if (!targetSet.has(i) || l.rec === null) return l.raw;
103
- return JSON.stringify({ ...l.rec, prompt: "" });
104
- });
105
- atomicWrite(file, out.join("\n") + "\n");
106
- console.log(
107
- `\u30D7\u30ED\u30F3\u30D7\u30C8\u5168\u6587\u3092 ${targetSet.size} \u4EF6\u6D88\u53BB\u3057\u307E\u3057\u305F(${scope}\u3002\u30B3\u30B9\u30C8\u96C6\u8A08\u30FB\u30C1\u30E3\u30FC\u30C8\u306F\u4FDD\u6301\u3055\u308C\u307E\u3059)\u3002`
108
- );
109
- }
110
- return 0;
111
- }
112
- export {
113
- runHistory
114
- };
@@ -1,184 +0,0 @@
1
- #!/usr/bin/env node
2
- import {
3
- notifyOS,
4
- notifySlack
5
- } from "./chunk-NV5UOHJA.js";
6
- import {
7
- writeDashboardHtml
8
- } from "./chunk-QX5KIRSU.js";
9
- import "./chunk-DGXUSPS4.js";
10
- import {
11
- aggregateCodexTurn,
12
- collectSubagentUsage
13
- } from "./chunk-DSV75EF7.js";
14
- import {
15
- aggregateNewTurn,
16
- computeCost,
17
- getUsdJpy,
18
- loadPriceTable
19
- } from "./chunk-LHKBGA5K.js";
20
- import "./chunk-J5QAYTFE.js";
21
- import {
22
- appendTurn,
23
- isMuted,
24
- loadCursor,
25
- logError,
26
- paths,
27
- readConfig,
28
- sanitizeCursor,
29
- saveCursor,
30
- todayTotalUSD
31
- } from "./chunk-ECADO26T.js";
32
-
33
- // src/track.ts
34
- import { join } from "path";
35
- function isRecord(v) {
36
- return typeof v === "object" && v !== null && !Array.isArray(v);
37
- }
38
- function emptyBuckets() {
39
- return { input: 0, output: 0, cacheWrite5m: 0, cacheWrite1h: 0, cacheRead: 0 };
40
- }
41
- function sumBuckets(usage) {
42
- const total = emptyBuckets();
43
- for (const b of Object.values(usage)) {
44
- total.input += b.input;
45
- total.output += b.output;
46
- total.cacheWrite5m += b.cacheWrite5m;
47
- total.cacheWrite1h += b.cacheWrite1h;
48
- total.cacheRead += b.cacheRead;
49
- }
50
- return total;
51
- }
52
- function collectModels(main, sidechain) {
53
- const models = [];
54
- for (const m of Object.keys(main)) {
55
- if (!models.includes(m)) models.push(m);
56
- }
57
- for (const m of Object.keys(sidechain)) {
58
- if (!models.includes(m)) models.push(m);
59
- }
60
- return models;
61
- }
62
- function withCodexModel(agg, payloadModel) {
63
- const model = typeof payloadModel === "string" && payloadModel.length > 0 ? payloadModel : null;
64
- if (model === null) return agg;
65
- const buckets = Object.values(agg.main)[0] ?? emptyBuckets();
66
- return { ...agg, main: { [model]: buckets } };
67
- }
68
- async function runTrack(stdinText, opts) {
69
- try {
70
- let parsed;
71
- try {
72
- parsed = JSON.parse(stdinText);
73
- } catch {
74
- return;
75
- }
76
- if (!isRecord(parsed)) return;
77
- const input = parsed;
78
- const transcriptPath = input.transcript_path;
79
- if (typeof transcriptPath !== "string") return;
80
- const cfg = readConfig();
81
- const cursor = sanitizeCursor(loadCursor(transcriptPath));
82
- const isCodex = opts?.codex === true;
83
- let agg = isCodex ? await aggregateCodexTurn(transcriptPath, cursor) : await aggregateNewTurn(transcriptPath, cursor);
84
- if (agg === null) return;
85
- if (isCodex) {
86
- agg = withCodexModel(agg, input.model);
87
- }
88
- let sa = null;
89
- if (!isCodex) {
90
- try {
91
- sa = await collectSubagentUsage(transcriptPath);
92
- } catch (err) {
93
- logError("track:subagents", err);
94
- sa = null;
95
- }
96
- }
97
- const cacheDir = paths().cacheDir;
98
- const table = await loadPriceTable(cacheDir, { offline: true });
99
- const breakdown = computeCost(agg.main, agg.sidechain, table);
100
- const fx = await getUsdJpy(cfg, cacheDir);
101
- const sessionId = agg.sessionId || (typeof input.session_id === "string" ? input.session_id : "") || "";
102
- const project = agg.cwd ?? (typeof input.cwd === "string" ? input.cwd : void 0) ?? "";
103
- const sidechainHasModels = Object.keys(agg.sidechain).length > 0;
104
- const record = {
105
- schemaVersion: 1,
106
- ts: agg.lastTs ?? (/* @__PURE__ */ new Date()).toISOString(),
107
- sessionId,
108
- project,
109
- gitBranch: agg.gitBranch,
110
- models: collectModels(agg.main, agg.sidechain),
111
- tokens: sumBuckets(agg.main),
112
- sidechainTokens: sidechainHasModels ? sumBuckets(agg.sidechain) : null,
113
- apiCalls: agg.apiCalls,
114
- costUSD: breakdown.usd,
115
- costByModel: breakdown.byModel,
116
- // モデル別 USD(main+sidechain 合算、丸めない)
117
- costJPY: breakdown.usd * fx.rate,
118
- // 丸めない(表示時に丸める)
119
- fxRate: fx.rate,
120
- fxSource: fx.source,
121
- prompt: agg.prompt ?? ""
122
- };
123
- if (isCodex) {
124
- record.source = "codex";
125
- }
126
- if (breakdown.unknownModels.length > 0) {
127
- record.unknownModels = breakdown.unknownModels;
128
- }
129
- if (sa !== null && sa.apiCalls > 0) {
130
- const saBreakdown = computeCost(sa.perModel, {}, table);
131
- record.subagents = {
132
- costUSD: saBreakdown.usd,
133
- costByModel: saBreakdown.byModel,
134
- tokens: sumBuckets(sa.perModel),
135
- apiCalls: sa.apiCalls,
136
- agentFiles: sa.agentFiles
137
- };
138
- if (saBreakdown.unknownModels.length > 0) {
139
- const merged = record.unknownModels ? [...record.unknownModels] : [];
140
- for (const m of saBreakdown.unknownModels) {
141
- if (!merged.includes(m)) merged.push(m);
142
- }
143
- record.unknownModels = merged;
144
- }
145
- }
146
- appendTurn(record);
147
- saveCursor(transcriptPath, agg.newCursor);
148
- if (sa !== null) {
149
- for (const nc of sa.newCursors) {
150
- saveCursor(nc.path, nc.cursor);
151
- }
152
- }
153
- const tasks = [];
154
- if ((cfg.notify.os || cfg.notify.slack !== null) && record.costUSD >= cfg.minNotifyUSD && !isMuted()) {
155
- const todayUSD = cfg.includeDailyTotal ? todayTotalUSD() : void 0;
156
- tasks.push(notifyOS(record, cfg, todayUSD));
157
- tasks.push(notifySlack(record, cfg, todayUSD));
158
- }
159
- if (cfg.dashboard.autoRegenerate) {
160
- tasks.push(
161
- (async () => {
162
- try {
163
- writeDashboardHtml({
164
- days: null,
165
- // 全履歴を埋め込む(粒度切替・過去・通算をブラウザ側で扱うため)
166
- outPath: join(paths().home, "report.html"),
167
- autoReloadSec: cfg.dashboard.autoReloadSec
168
- });
169
- } catch (err) {
170
- logError("track:dashboard", err);
171
- }
172
- })()
173
- );
174
- }
175
- if (tasks.length > 0) {
176
- await Promise.allSettled(tasks);
177
- }
178
- } catch (err) {
179
- logError("track", err);
180
- }
181
- }
182
- export {
183
- runTrack
184
- };