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.
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ notifyOS,
4
+ notifySlack
5
+ } from "./chunk-4JNZ7BHO.js";
6
+ import {
7
+ writeDashboardHtml
8
+ } from "./chunk-64N5SGTT.js";
9
+ import "./chunk-DGXUSPS4.js";
10
+ import {
11
+ collectSubagentUsage
12
+ } from "./chunk-TB5A7U7G.js";
13
+ import {
14
+ aggregateNewTurn,
15
+ computeCost,
16
+ getUsdJpy,
17
+ loadPriceTable
18
+ } from "./chunk-OEG3AVU6.js";
19
+ import "./chunk-TBFKGFZX.js";
20
+ import {
21
+ appendTurn,
22
+ isMuted,
23
+ loadCursor,
24
+ logError,
25
+ paths,
26
+ readConfig,
27
+ sanitizeCursor,
28
+ saveCursor,
29
+ todayTotalUSD
30
+ } from "./chunk-IIYMGLV4.js";
31
+
32
+ // src/track.ts
33
+ import { join } from "path";
34
+ function isRecord(v) {
35
+ return typeof v === "object" && v !== null && !Array.isArray(v);
36
+ }
37
+ function emptyBuckets() {
38
+ return { input: 0, output: 0, cacheWrite5m: 0, cacheWrite1h: 0, cacheRead: 0 };
39
+ }
40
+ function sumBuckets(usage) {
41
+ const total = emptyBuckets();
42
+ for (const b of Object.values(usage)) {
43
+ total.input += b.input;
44
+ total.output += b.output;
45
+ total.cacheWrite5m += b.cacheWrite5m;
46
+ total.cacheWrite1h += b.cacheWrite1h;
47
+ total.cacheRead += b.cacheRead;
48
+ }
49
+ return total;
50
+ }
51
+ function collectModels(main, sidechain) {
52
+ const models = [];
53
+ for (const m of Object.keys(main)) {
54
+ if (!models.includes(m)) models.push(m);
55
+ }
56
+ for (const m of Object.keys(sidechain)) {
57
+ if (!models.includes(m)) models.push(m);
58
+ }
59
+ return models;
60
+ }
61
+ async function runTrack(stdinText) {
62
+ try {
63
+ let parsed;
64
+ try {
65
+ parsed = JSON.parse(stdinText);
66
+ } catch {
67
+ return;
68
+ }
69
+ if (!isRecord(parsed)) return;
70
+ const input = parsed;
71
+ const transcriptPath = input.transcript_path;
72
+ if (typeof transcriptPath !== "string") return;
73
+ const cfg = readConfig();
74
+ const cursor = sanitizeCursor(loadCursor(transcriptPath));
75
+ const agg = await aggregateNewTurn(transcriptPath, cursor);
76
+ if (agg === null) return;
77
+ let sa = null;
78
+ try {
79
+ sa = await collectSubagentUsage(transcriptPath);
80
+ } catch (err) {
81
+ logError("track:subagents", err);
82
+ sa = null;
83
+ }
84
+ const cacheDir = paths().cacheDir;
85
+ const table = await loadPriceTable(cacheDir, { offline: true });
86
+ const breakdown = computeCost(agg.main, agg.sidechain, table);
87
+ const fx = await getUsdJpy(cfg, cacheDir);
88
+ const sessionId = agg.sessionId || (typeof input.session_id === "string" ? input.session_id : "") || "";
89
+ const project = agg.cwd ?? (typeof input.cwd === "string" ? input.cwd : void 0) ?? "";
90
+ const sidechainHasModels = Object.keys(agg.sidechain).length > 0;
91
+ const record = {
92
+ schemaVersion: 1,
93
+ ts: agg.lastTs ?? (/* @__PURE__ */ new Date()).toISOString(),
94
+ sessionId,
95
+ project,
96
+ gitBranch: agg.gitBranch,
97
+ models: collectModels(agg.main, agg.sidechain),
98
+ tokens: sumBuckets(agg.main),
99
+ sidechainTokens: sidechainHasModels ? sumBuckets(agg.sidechain) : null,
100
+ apiCalls: agg.apiCalls,
101
+ costUSD: breakdown.usd,
102
+ costByModel: breakdown.byModel,
103
+ // モデル別 USD(main+sidechain 合算、丸めない)
104
+ costJPY: breakdown.usd * fx.rate,
105
+ // 丸めない(表示時に丸める)
106
+ fxRate: fx.rate,
107
+ fxSource: fx.source,
108
+ prompt: agg.prompt ?? ""
109
+ };
110
+ if (breakdown.unknownModels.length > 0) {
111
+ record.unknownModels = breakdown.unknownModels;
112
+ }
113
+ if (sa !== null && sa.apiCalls > 0) {
114
+ const saBreakdown = computeCost(sa.perModel, {}, table);
115
+ record.subagents = {
116
+ costUSD: saBreakdown.usd,
117
+ costByModel: saBreakdown.byModel,
118
+ tokens: sumBuckets(sa.perModel),
119
+ apiCalls: sa.apiCalls,
120
+ agentFiles: sa.agentFiles
121
+ };
122
+ if (saBreakdown.unknownModels.length > 0) {
123
+ const merged = record.unknownModels ? [...record.unknownModels] : [];
124
+ for (const m of saBreakdown.unknownModels) {
125
+ if (!merged.includes(m)) merged.push(m);
126
+ }
127
+ record.unknownModels = merged;
128
+ }
129
+ }
130
+ appendTurn(record);
131
+ saveCursor(transcriptPath, agg.newCursor);
132
+ if (sa !== null) {
133
+ for (const nc of sa.newCursors) {
134
+ saveCursor(nc.path, nc.cursor);
135
+ }
136
+ }
137
+ const tasks = [];
138
+ if (record.costUSD >= cfg.minNotifyUSD && !isMuted()) {
139
+ const todayUSD = cfg.includeDailyTotal ? todayTotalUSD() : void 0;
140
+ tasks.push(notifyOS(record, cfg, todayUSD));
141
+ tasks.push(notifySlack(record, cfg, todayUSD));
142
+ }
143
+ if (cfg.dashboard.autoRegenerate) {
144
+ tasks.push(
145
+ (async () => {
146
+ try {
147
+ writeDashboardHtml({
148
+ days: null,
149
+ // 全履歴を埋め込む(粒度切替・過去・通算をブラウザ側で扱うため)
150
+ outPath: join(paths().home, "report.html"),
151
+ autoReloadSec: cfg.dashboard.autoReloadSec
152
+ });
153
+ } catch (err) {
154
+ logError("track:dashboard", err);
155
+ }
156
+ })()
157
+ );
158
+ }
159
+ if (tasks.length > 0) {
160
+ await Promise.allSettled(tasks);
161
+ }
162
+ } catch (err) {
163
+ logError("track", err);
164
+ }
165
+ }
166
+ export {
167
+ runTrack
168
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "ccc-notifier",
3
+ "version": "0.1.0",
4
+ "description": "Claude Code Cost notifier: per-prompt cost notifications (USD/JPY) with local history and HTML dashboard",
5
+ "keywords": [
6
+ "claude",
7
+ "claude-code",
8
+ "anthropic",
9
+ "cost",
10
+ "usage",
11
+ "notifier",
12
+ "notification",
13
+ "cli",
14
+ "dashboard",
15
+ "slack"
16
+ ],
17
+ "license": "MIT",
18
+ "author": "shimabox (https://github.com/shimabox)",
19
+ "homepage": "https://github.com/shimabox/ccc-notifier#readme",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/shimabox/ccc-notifier.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/shimabox/ccc-notifier/issues"
26
+ },
27
+ "type": "module",
28
+ "engines": {
29
+ "node": ">=20"
30
+ },
31
+ "bin": {
32
+ "ccc-notifier": "dist/cli.js",
33
+ "cccn": "dist/cli.js"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "scripts": {
39
+ "build": "tsup",
40
+ "typecheck": "tsc --noEmit",
41
+ "test": "vitest run",
42
+ "prepublishOnly": "npm run build"
43
+ },
44
+ "dependencies": {
45
+ "@clack/prompts": "^1.7.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^26.1.0",
49
+ "tsup": "^8.5.1",
50
+ "typescript": "^6.0.3",
51
+ "vitest": "^4.1.10"
52
+ }
53
+ }