@hasna/economy 0.5.10 → 0.5.12
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/CHANGELOG.md +15 -0
- package/README.md +22 -0
- package/dist/chunks/{index-t777mc83.js → index-a5gdrfby.js} +1 -1
- package/dist/chunks/{index-w4r6xs02.js → index-bx0wbqyr.js} +1 -1
- package/dist/chunks/{index-twn8ncdp.js → index-mn8ybzv7.js} +1 -1
- package/dist/chunks/index-pgt6fhkb.js +1069 -0
- package/dist/chunks/{index-4v5r3c22.js → index-vavbzb1s.js} +1 -1
- package/dist/chunks/{serve-bqn5j46v.js → serve-462ch8yc.js} +2 -2
- package/dist/chunks/{tui-5s5w6yjd.js → tui-bmf8rp0p.js} +3 -3
- package/dist/chunks/watch-msg91cnj.js +307 -0
- package/dist/cli/commands/watch.d.ts +2 -0
- package/dist/cli/commands/watch.d.ts.map +1 -1
- package/dist/cli/index.js +37 -1071
- package/dist/index.js +1 -1
- package/dist/lib/cloud-ingest.d.ts.map +1 -1
- package/dist/lib/watch-collector.d.ts +11 -0
- package/dist/lib/watch-collector.d.ts.map +1 -0
- package/dist/lib/watch-paths.d.ts +2 -1
- package/dist/lib/watch-paths.d.ts.map +1 -1
- package/dist/mcp/index.js +29 -24
- package/dist/otel/index.js +30 -23
- package/dist/server/index.js +1 -1
- package/dist/server/service-worker.js +1 -1
- package/docs/cli.md +1 -1
- package/docs/ingest-journal.md +8 -0
- package/package.json +1 -1
- package/dist/chunks/watch-7pf8xj1q.js +0 -196
package/dist/index.js
CHANGED
|
@@ -111,7 +111,7 @@ import { randomUUID } from "crypto";
|
|
|
111
111
|
// package.json
|
|
112
112
|
var package_default = {
|
|
113
113
|
name: "@hasna/economy",
|
|
114
|
-
version: "0.5.
|
|
114
|
+
version: "0.5.12",
|
|
115
115
|
description: "AI coding cost tracker \u2014 CLI + MCP server + REST API for Claude Code, Codex, OpenCode, Cursor, Pi, and Hermes, with legacy Gemini CLI session and Gemini API billing ingestion",
|
|
116
116
|
repository: {
|
|
117
117
|
type: "git",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cloud-ingest.d.ts","sourceRoot":"","sources":["../../src/lib/cloud-ingest.ts"],"names":[],"mappings":"AA+BA,OAAO,KAAK,EAAE,aAAa,IAAI,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAA;AAGnE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAAqC,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAUhG;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAI3C;AAqED,qBAAa,aAAc,SAAQ,KAAK;IAC1B,QAAQ,CAAC,MAAM,EAAC,MAAM;IAAC,QAAQ,CAAC,IAAI,EAAC,MAAM;IAAC,QAAQ,CAAC,MAAM;gBAAlD,MAAM,EAAC,MAAM,EAAU,IAAI,EAAC,MAAM,EAAU,MAAM,SAAK;CAC7E;AAED,wBAAsB,UAAU,CAC9B,KAAK,EAAE,yBAAyB,EAChC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GACnD,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,kBAAkB,EAAE,CAAA;CAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"cloud-ingest.d.ts","sourceRoot":"","sources":["../../src/lib/cloud-ingest.ts"],"names":[],"mappings":"AA+BA,OAAO,KAAK,EAAE,aAAa,IAAI,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAGxE,OAAO,KAAK,EAA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AI,GAAE,uBAA4B,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAkC9I"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** One collector owns its timers and never overlaps durable ingestion calls. */
|
|
2
|
+
export interface WatchCollectorOptions {
|
|
3
|
+
intervalMs: number;
|
|
4
|
+
collect: () => Promise<void>;
|
|
5
|
+
onError: () => void;
|
|
6
|
+
}
|
|
7
|
+
export declare function createWatchCollector(options: WatchCollectorOptions): {
|
|
8
|
+
request: () => void;
|
|
9
|
+
stop(): Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=watch-collector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-collector.d.ts","sourceRoot":"","sources":["../../src/lib/watch-collector.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,MAAM,WAAW,qBAAqB;IACpC,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5B,OAAO,EAAE,MAAM,IAAI,CAAA;CACpB;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB;mBA6B7C,IAAI;YAaR,OAAO,CAAC,IAAI,CAAC;EAW9B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watch-paths.d.ts","sourceRoot":"","sources":["../../src/lib/watch-paths.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"watch-paths.d.ts","sourceRoot":"","sources":["../../src/lib/watch-paths.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAExC,+DAA+D;AAC/D,wBAAgB,aAAa,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,CASrD"}
|
package/dist/mcp/index.js
CHANGED
|
@@ -86,7 +86,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
86
86
|
// package.json
|
|
87
87
|
var package_default = {
|
|
88
88
|
name: "@hasna/economy",
|
|
89
|
-
version: "0.5.
|
|
89
|
+
version: "0.5.12",
|
|
90
90
|
description: "AI coding cost tracker \u2014 CLI + MCP server + REST API for Claude Code, Codex, OpenCode, Cursor, Pi, and Hermes, with legacy Gemini CLI session and Gemini API billing ingestion",
|
|
91
91
|
repository: {
|
|
92
92
|
type: "git",
|
|
@@ -3737,7 +3737,7 @@ async function syncAll(db, opts = {}) {
|
|
|
3737
3737
|
}
|
|
3738
3738
|
|
|
3739
3739
|
// src/lib/cloud-ingest.ts
|
|
3740
|
-
import { mkdirSync as mkdirSync2, readFileSync as readFileSync11, renameSync, writeFileSync as writeFileSync2 } from "fs";
|
|
3740
|
+
import { existsSync as existsSync12, mkdirSync as mkdirSync2, readFileSync as readFileSync11, renameSync, writeFileSync as writeFileSync2 } from "fs";
|
|
3741
3741
|
import { dirname, join as join14 } from "path";
|
|
3742
3742
|
|
|
3743
3743
|
// src/lib/sync-maintenance.ts
|
|
@@ -3900,30 +3900,35 @@ async function postIngest(cloud, body) {
|
|
|
3900
3900
|
const refused = journal.refusal(batch);
|
|
3901
3901
|
if (refused)
|
|
3902
3902
|
throw new IngestRefused(refused.status, refused.code, refused.reason);
|
|
3903
|
-
await maintenance.admit(batch);
|
|
3904
3903
|
const known = await journal.lookupReceipt(cloud, batch);
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
3904
|
+
let payload;
|
|
3905
|
+
if (known && existsSync12(join14(journal.directory, ".authority.json"))) {
|
|
3906
|
+
payload = existsSync12(journal.file(batch.idempotency_key, "intent")) ? journal.accept(batch, known) : known;
|
|
3907
|
+
} else {
|
|
3908
|
+
await maintenance.admit(batch);
|
|
3909
|
+
const fresh = journal.stage(batch);
|
|
3910
|
+
const sendFresh = async () => {
|
|
3911
|
+
let response;
|
|
3912
|
+
try {
|
|
3913
|
+
response = await cloud.client.transport.post("/ingest", batch, { retry: false });
|
|
3914
|
+
} catch (error) {
|
|
3915
|
+
const refusal = error instanceof HasnaHttpError2 ? definitiveIngestRefusal(error.status, error.body) : null;
|
|
3916
|
+
if (refusal) {
|
|
3917
|
+
journal.recordRefusal(batch, refusal);
|
|
3918
|
+
throw new IngestRefused(refusal.status, refusal.code, refusal.reason);
|
|
3919
|
+
}
|
|
3920
|
+
throw error;
|
|
3915
3921
|
}
|
|
3916
|
-
|
|
3917
|
-
}
|
|
3918
|
-
|
|
3919
|
-
}
|
|
3920
|
-
const payload = known ? journal.accept(batch, known) : fresh ? await sendFresh() : await journal.reconcileCommand(cloud, batch);
|
|
3922
|
+
return journal.accept(batch, response);
|
|
3923
|
+
};
|
|
3924
|
+
payload = known ? journal.accept(batch, known) : fresh ? await sendFresh() : await journal.reconcileCommand(cloud, batch);
|
|
3925
|
+
}
|
|
3921
3926
|
await verifyJournalAuthority(journal, cloud);
|
|
3922
3927
|
if (!payload)
|
|
3923
3928
|
throw new PendingIngestError([batch.idempotency_key]);
|
|
3924
|
-
verifyIngestReceipt(payload, batch);
|
|
3925
|
-
const counts =
|
|
3926
|
-
if (!counts || typeof counts !== "object" || Array.isArray(counts) || typeof
|
|
3929
|
+
const verified = verifyIngestReceipt(payload, batch);
|
|
3930
|
+
const counts = verified["ingested"];
|
|
3931
|
+
if (!counts || typeof counts !== "object" || Array.isArray(counts) || typeof verified["total"] !== "number" || !Number.isSafeInteger(verified["total"]) || verified["total"] < 0)
|
|
3927
3932
|
throw new Error("Invalid ingest receipt");
|
|
3928
3933
|
let sum = 0;
|
|
3929
3934
|
for (const [table, count] of Object.entries(counts)) {
|
|
@@ -3932,16 +3937,16 @@ async function postIngest(cloud, body) {
|
|
|
3932
3937
|
sum += count;
|
|
3933
3938
|
ingested[table] = (ingested[table] ?? 0) + count;
|
|
3934
3939
|
}
|
|
3935
|
-
if (sum !==
|
|
3940
|
+
if (sum !== verified["total"])
|
|
3936
3941
|
throw new Error("Invalid ingest receipt");
|
|
3937
3942
|
total += sum;
|
|
3938
|
-
const operation =
|
|
3943
|
+
const operation = verified["operation_key"];
|
|
3939
3944
|
if (operation !== undefined && (typeof operation !== "string" || !/^[a-f0-9]{64}$/.test(operation)))
|
|
3940
3945
|
throw new Error("Invalid ingest receipt");
|
|
3941
3946
|
completed.push({
|
|
3942
3947
|
idempotency_key: batch.idempotency_key,
|
|
3943
3948
|
...typeof operation === "string" ? { operation_key: operation } : {},
|
|
3944
|
-
replayed:
|
|
3949
|
+
replayed: verified["replayed"] === true
|
|
3945
3950
|
});
|
|
3946
3951
|
} catch (error) {
|
|
3947
3952
|
if (error instanceof PendingIngestError || error instanceof JournalMaintenanceError || error instanceof IngestRefused)
|
package/dist/otel/index.js
CHANGED
|
@@ -2370,6 +2370,8 @@ async function ingestOtelRows(db, rows) {
|
|
|
2370
2370
|
// src/lib/cloud-ingest.ts
|
|
2371
2371
|
init_database();
|
|
2372
2372
|
init_pricing();
|
|
2373
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync4, readFileSync as readFileSync3, renameSync, writeFileSync as writeFileSync2 } from "fs";
|
|
2374
|
+
import { dirname, join as join11 } from "path";
|
|
2373
2375
|
|
|
2374
2376
|
// src/ingest/collection-outcome.ts
|
|
2375
2377
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -3457,30 +3459,35 @@ async function postIngest(cloud, body) {
|
|
|
3457
3459
|
const refused = journal.refusal(batch);
|
|
3458
3460
|
if (refused)
|
|
3459
3461
|
throw new IngestRefused(refused.status, refused.code, refused.reason);
|
|
3460
|
-
await maintenance.admit(batch);
|
|
3461
3462
|
const known = await journal.lookupReceipt(cloud, batch);
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3463
|
+
let payload;
|
|
3464
|
+
if (known && existsSync3(join11(journal.directory, ".authority.json"))) {
|
|
3465
|
+
payload = existsSync3(journal.file(batch.idempotency_key, "intent")) ? journal.accept(batch, known) : known;
|
|
3466
|
+
} else {
|
|
3467
|
+
await maintenance.admit(batch);
|
|
3468
|
+
const fresh = journal.stage(batch);
|
|
3469
|
+
const sendFresh = async () => {
|
|
3470
|
+
let response;
|
|
3471
|
+
try {
|
|
3472
|
+
response = await cloud.client.transport.post("/ingest", batch, { retry: false });
|
|
3473
|
+
} catch (error) {
|
|
3474
|
+
const refusal = error instanceof HasnaHttpError2 ? definitiveIngestRefusal(error.status, error.body) : null;
|
|
3475
|
+
if (refusal) {
|
|
3476
|
+
journal.recordRefusal(batch, refusal);
|
|
3477
|
+
throw new IngestRefused(refusal.status, refusal.code, refusal.reason);
|
|
3478
|
+
}
|
|
3479
|
+
throw error;
|
|
3472
3480
|
}
|
|
3473
|
-
|
|
3474
|
-
}
|
|
3475
|
-
|
|
3476
|
-
}
|
|
3477
|
-
const payload = known ? journal.accept(batch, known) : fresh ? await sendFresh() : await journal.reconcileCommand(cloud, batch);
|
|
3481
|
+
return journal.accept(batch, response);
|
|
3482
|
+
};
|
|
3483
|
+
payload = known ? journal.accept(batch, known) : fresh ? await sendFresh() : await journal.reconcileCommand(cloud, batch);
|
|
3484
|
+
}
|
|
3478
3485
|
await verifyJournalAuthority(journal, cloud);
|
|
3479
3486
|
if (!payload)
|
|
3480
3487
|
throw new PendingIngestError([batch.idempotency_key]);
|
|
3481
|
-
verifyIngestReceipt(payload, batch);
|
|
3482
|
-
const counts =
|
|
3483
|
-
if (!counts || typeof counts !== "object" || Array.isArray(counts) || typeof
|
|
3488
|
+
const verified = verifyIngestReceipt(payload, batch);
|
|
3489
|
+
const counts = verified["ingested"];
|
|
3490
|
+
if (!counts || typeof counts !== "object" || Array.isArray(counts) || typeof verified["total"] !== "number" || !Number.isSafeInteger(verified["total"]) || verified["total"] < 0)
|
|
3484
3491
|
throw new Error("Invalid ingest receipt");
|
|
3485
3492
|
let sum = 0;
|
|
3486
3493
|
for (const [table, count] of Object.entries(counts)) {
|
|
@@ -3489,16 +3496,16 @@ async function postIngest(cloud, body) {
|
|
|
3489
3496
|
sum += count;
|
|
3490
3497
|
ingested[table] = (ingested[table] ?? 0) + count;
|
|
3491
3498
|
}
|
|
3492
|
-
if (sum !==
|
|
3499
|
+
if (sum !== verified["total"])
|
|
3493
3500
|
throw new Error("Invalid ingest receipt");
|
|
3494
3501
|
total += sum;
|
|
3495
|
-
const operation =
|
|
3502
|
+
const operation = verified["operation_key"];
|
|
3496
3503
|
if (operation !== undefined && (typeof operation !== "string" || !/^[a-f0-9]{64}$/.test(operation)))
|
|
3497
3504
|
throw new Error("Invalid ingest receipt");
|
|
3498
3505
|
completed.push({
|
|
3499
3506
|
idempotency_key: batch.idempotency_key,
|
|
3500
3507
|
...typeof operation === "string" ? { operation_key: operation } : {},
|
|
3501
|
-
replayed:
|
|
3508
|
+
replayed: verified["replayed"] === true
|
|
3502
3509
|
});
|
|
3503
3510
|
} catch (error) {
|
|
3504
3511
|
if (error instanceof PendingIngestError || error instanceof JournalMaintenanceError || error instanceof IngestRefused)
|
|
@@ -3523,7 +3530,7 @@ async function pushIngestRows(cloud, scratch) {
|
|
|
3523
3530
|
// package.json
|
|
3524
3531
|
var package_default = {
|
|
3525
3532
|
name: "@hasna/economy",
|
|
3526
|
-
version: "0.5.
|
|
3533
|
+
version: "0.5.12",
|
|
3527
3534
|
description: "AI coding cost tracker \u2014 CLI + MCP server + REST API for Claude Code, Codex, OpenCode, Cursor, Pi, and Hermes, with legacy Gemini CLI session and Gemini API billing ingestion",
|
|
3528
3535
|
repository: {
|
|
3529
3536
|
type: "git",
|
package/dist/server/index.js
CHANGED
|
@@ -5578,7 +5578,7 @@ function isAuthorizedRequest(req, path) {
|
|
|
5578
5578
|
// package.json
|
|
5579
5579
|
var package_default = {
|
|
5580
5580
|
name: "@hasna/economy",
|
|
5581
|
-
version: "0.5.
|
|
5581
|
+
version: "0.5.12",
|
|
5582
5582
|
description: "AI coding cost tracker \u2014 CLI + MCP server + REST API for Claude Code, Codex, OpenCode, Cursor, Pi, and Hermes, with legacy Gemini CLI session and Gemini API billing ingestion",
|
|
5583
5583
|
repository: {
|
|
5584
5584
|
type: "git",
|
|
@@ -7454,7 +7454,7 @@ import { randomUUID } from "crypto";
|
|
|
7454
7454
|
// package.json
|
|
7455
7455
|
var package_default = {
|
|
7456
7456
|
name: "@hasna/economy",
|
|
7457
|
-
version: "0.5.
|
|
7457
|
+
version: "0.5.12",
|
|
7458
7458
|
description: "AI coding cost tracker \u2014 CLI + MCP server + REST API for Claude Code, Codex, OpenCode, Cursor, Pi, and Hermes, with legacy Gemini CLI session and Gemini API billing ingestion",
|
|
7459
7459
|
repository: {
|
|
7460
7460
|
type: "git",
|
package/docs/cli.md
CHANGED
|
@@ -29,7 +29,7 @@ The supported coding-agent values are `claude`, `takumi`, `codex`, `gemini`, `op
|
|
|
29
29
|
| `economy brief` | Fleet brief with `--since <24h|7d|ISO-date>`, `--machine <id|all>`, or `--json`. |
|
|
30
30
|
| `economy usage [period]` | Quota/usage snapshots with `--agent`, `--limit`, `--verbose`, or `--json`. |
|
|
31
31
|
| `economy savings [period]` | Subscription-versus-API-equivalent savings with `--agent` or `--json`. |
|
|
32
|
-
| `economy watch` |
|
|
32
|
+
| `economy watch` | Stream stored costs. Explicit `--daemon` also collects local provider files through the configured store, including the hosted API. `--sync-interval` bounds collection frequency and periodic fallback; `--interval` controls report refresh. Also accepts `--agent`, `--notify`, and `--no-notifications`. Ordinary hosted watch remains read-only. This foreground command does not install a startup service. |
|
|
33
33
|
| `economy status` | Print one-line spend, fleet, storage, top-agent, and available quota status. |
|
|
34
34
|
| `economy doctor` | Check source paths/token availability, storage mode, pricing gaps, deduplication, and billing drift. |
|
|
35
35
|
| `economy transport` | Report the resolved client transport and credential SOURCE (never the key): the `/v1` authority, the URL/key source names, and the credential tier from the `@hasna/contracts` chain; `--json` for the full report. Exits 0 even when unconfigured — the refusal is the report. |
|
package/docs/ingest-journal.md
CHANGED
|
@@ -107,6 +107,14 @@ an eligible original principal; successful authentication as another principal
|
|
|
107
107
|
is insufficient. The same-key path checks hosted history before staging or
|
|
108
108
|
sending a new command.
|
|
109
109
|
|
|
110
|
+
When a scan retries an already retired batch, the verified authority anchor and
|
|
111
|
+
exact hosted receipt are sufficient: no new local intent, capacity admission or
|
|
112
|
+
Trash capture is needed. Existing local records still reconcile normally. An
|
|
113
|
+
empty journal without an anchor establishes one through the ordinary durable
|
|
114
|
+
record path, even if the server already knows the first batch. Failed lookups,
|
|
115
|
+
changed principals and unresolved commands still stop ingestion; they never
|
|
116
|
+
permit a replacement POST.
|
|
117
|
+
|
|
110
118
|
These are client source contracts. Publication, fleet readiness, actual hosted
|
|
111
119
|
capture/restore and independent release review require their own acceptance
|
|
112
120
|
evidence.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hasna/economy",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.12",
|
|
4
4
|
"description": "AI coding cost tracker — CLI + MCP server + REST API for Claude Code, Codex, OpenCode, Cursor, Pi, and Hermes, with legacy Gemini CLI session and Gemini API billing ingestion",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
import {
|
|
3
|
-
agentPaths,
|
|
4
|
-
syncAll
|
|
5
|
-
} from "./index-t777mc83.js";
|
|
6
|
-
import {
|
|
7
|
-
getStore,
|
|
8
|
-
isCloudStore
|
|
9
|
-
} from "./index-twn8ncdp.js";
|
|
10
|
-
import"./index-4v5r3c22.js";
|
|
11
|
-
import {
|
|
12
|
-
formatSummaryValue
|
|
13
|
-
} from "./index-zgvmrqyc.js";
|
|
14
|
-
import {
|
|
15
|
-
__require
|
|
16
|
-
} from "./index-p35cyhc4.js";
|
|
17
|
-
|
|
18
|
-
// src/cli/commands/watch.ts
|
|
19
|
-
import chalk from "chalk";
|
|
20
|
-
import { watch } from "fs";
|
|
21
|
-
|
|
22
|
-
// src/lib/watch-paths.ts
|
|
23
|
-
import { existsSync } from "fs";
|
|
24
|
-
function getWatchPaths() {
|
|
25
|
-
const p = agentPaths();
|
|
26
|
-
const candidates = [
|
|
27
|
-
p.claudeProjects,
|
|
28
|
-
p.takumiProjects,
|
|
29
|
-
p.codexDir,
|
|
30
|
-
p.geminiTmp,
|
|
31
|
-
p.geminiHistory,
|
|
32
|
-
p.opencodeMessages,
|
|
33
|
-
p.piSessions,
|
|
34
|
-
p.hermesDir
|
|
35
|
-
];
|
|
36
|
-
return candidates.filter((path) => existsSync(path));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// src/cli/commands/notification.ts
|
|
40
|
-
import { execFileSync } from "child_process";
|
|
41
|
-
function appleScriptString(value) {
|
|
42
|
-
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
43
|
-
}
|
|
44
|
-
function notificationScript(title, body) {
|
|
45
|
-
return `display notification ${appleScriptString(body)} with title ${appleScriptString(title)}`;
|
|
46
|
-
}
|
|
47
|
-
function sendNotification(title, body, execFile = execFileSync) {
|
|
48
|
-
try {
|
|
49
|
-
execFile("osascript", ["-e", notificationScript(title, body)], { stdio: "ignore" });
|
|
50
|
-
} catch {}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// src/cli/commands/watch.ts
|
|
54
|
-
function fmt(usd) {
|
|
55
|
-
return chalk.green(`$${usd.toFixed(4)}`);
|
|
56
|
-
}
|
|
57
|
-
function renderHeader(today, week, mode) {
|
|
58
|
-
process.stdout.write("\x1B[H\x1B[2J");
|
|
59
|
-
console.log(chalk.bold.cyan(" economy watch") + chalk.dim(` \u2014 stored usage stream (${mode})`));
|
|
60
|
-
console.log(chalk.dim(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
61
|
-
console.log(` Today: ${formatSummaryValue(today)} Week: ${formatSummaryValue(week)}`);
|
|
62
|
-
console.log(chalk.dim(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
63
|
-
console.log(" UTC; selected stored records only; source coverage unverified");
|
|
64
|
-
console.log(chalk.dim(" [agent] cost model tokens project"));
|
|
65
|
-
console.log(chalk.dim(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
66
|
-
}
|
|
67
|
-
function agentLabel(agent) {
|
|
68
|
-
if (agent === "claude")
|
|
69
|
-
return chalk.blue("[claude]");
|
|
70
|
-
if (agent === "codex")
|
|
71
|
-
return chalk.yellow("[codex] ");
|
|
72
|
-
if (agent === "gemini")
|
|
73
|
-
return chalk.green("[gemini]");
|
|
74
|
-
if (agent === "takumi")
|
|
75
|
-
return chalk.magenta("[takumi]");
|
|
76
|
-
if (agent === "opencode")
|
|
77
|
-
return chalk.cyan("[opncde]");
|
|
78
|
-
if (agent === "cursor")
|
|
79
|
-
return chalk.white("[cursor]");
|
|
80
|
-
if (agent === "pi")
|
|
81
|
-
return chalk.white("[pi ]");
|
|
82
|
-
if (agent === "hermes")
|
|
83
|
-
return chalk.white("[hermes]");
|
|
84
|
-
return chalk.gray(`[${agent.slice(0, 6).padEnd(6)}]`);
|
|
85
|
-
}
|
|
86
|
-
async function watchCosts(opts) {
|
|
87
|
-
const store = getStore();
|
|
88
|
-
const cloud = isCloudStore();
|
|
89
|
-
const db = cloud ? null : (await import("./sqlite-store-r219qrjt.js")).openDatabase();
|
|
90
|
-
let lastCheck = new Date(Date.now() - opts.interval * 1000).toISOString();
|
|
91
|
-
const lines = [];
|
|
92
|
-
const MAX_LINES = 20;
|
|
93
|
-
let sessionCumulativeCost = 0;
|
|
94
|
-
let notifyThresholdFired = 0;
|
|
95
|
-
let ingestPending = false;
|
|
96
|
-
let ingestTimer = null;
|
|
97
|
-
const daemon = opts.daemon && !cloud;
|
|
98
|
-
const mode = cloud ? "cloud" : daemon ? "daemon" : "poll";
|
|
99
|
-
const initialSummaryToday = await store.summary("today");
|
|
100
|
-
const initialSummaryWeek = await store.summary("week");
|
|
101
|
-
renderHeader(initialSummaryToday, initialSummaryWeek, mode);
|
|
102
|
-
if (daemon) {
|
|
103
|
-
const paths = getWatchPaths();
|
|
104
|
-
console.log(chalk.dim(`
|
|
105
|
-
Watching ${paths.length} paths \u2014 sync on change, refresh every ${opts.interval}s
|
|
106
|
-
`));
|
|
107
|
-
for (const p of paths) {
|
|
108
|
-
try {
|
|
109
|
-
watch(p, { recursive: true }, () => scheduleIngest());
|
|
110
|
-
} catch {
|
|
111
|
-
try {
|
|
112
|
-
watch(p, () => scheduleIngest());
|
|
113
|
-
} catch {}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
} else if (cloud) {
|
|
117
|
-
console.log(chalk.dim(`
|
|
118
|
-
Streaming from cloud API every ${opts.interval}s \u2014 Ctrl+C to exit
|
|
119
|
-
`));
|
|
120
|
-
} else {
|
|
121
|
-
console.log(chalk.dim(`
|
|
122
|
-
Polling every ${opts.interval}s \u2014 Ctrl+C to exit
|
|
123
|
-
`));
|
|
124
|
-
}
|
|
125
|
-
function scheduleIngest() {
|
|
126
|
-
ingestPending = true;
|
|
127
|
-
if (ingestTimer)
|
|
128
|
-
return;
|
|
129
|
-
ingestTimer = setTimeout(() => {
|
|
130
|
-
ingestTimer = null;
|
|
131
|
-
poll();
|
|
132
|
-
}, 500);
|
|
133
|
-
}
|
|
134
|
-
async function poll() {
|
|
135
|
-
const now = new Date().toISOString();
|
|
136
|
-
const shouldSync = !daemon || ingestPending;
|
|
137
|
-
ingestPending = false;
|
|
138
|
-
if (shouldSync && db)
|
|
139
|
-
await syncAll(db);
|
|
140
|
-
const newRequests = await store.recentRequests(lastCheck);
|
|
141
|
-
lastCheck = now;
|
|
142
|
-
for (const req of newRequests) {
|
|
143
|
-
if (opts.agent && req.agent !== opts.agent)
|
|
144
|
-
continue;
|
|
145
|
-
const tokens = req.input_tokens + req.output_tokens;
|
|
146
|
-
const tokStr = tokens >= 1000 ? `${(tokens / 1000).toFixed(1)}k` : String(tokens);
|
|
147
|
-
const line = ` ${agentLabel(req.agent)} ${fmt(req.cost_usd).padEnd(14)}${req.model.substring(0, 24).padEnd(26)}${tokStr.padEnd(10)}${req.session_id.substring(0, 12)}`;
|
|
148
|
-
lines.push(line);
|
|
149
|
-
if (lines.length > MAX_LINES)
|
|
150
|
-
lines.shift();
|
|
151
|
-
if (req.cost_usd > 1) {
|
|
152
|
-
sendNotification("economy: high cost", `$${req.cost_usd.toFixed(2)} on ${req.model}`);
|
|
153
|
-
}
|
|
154
|
-
if (opts.notify && opts.notify > 0) {
|
|
155
|
-
sessionCumulativeCost += req.cost_usd;
|
|
156
|
-
const crossedThresholds = Math.floor(sessionCumulativeCost / opts.notify);
|
|
157
|
-
if (crossedThresholds > notifyThresholdFired) {
|
|
158
|
-
notifyThresholdFired = crossedThresholds;
|
|
159
|
-
sendNotification("Cost Alert", `Economy: $${sessionCumulativeCost.toFixed(2)} spent this session`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const today = await store.summary("today");
|
|
164
|
-
const week = await store.summary("week");
|
|
165
|
-
renderHeader(today, week, mode);
|
|
166
|
-
for (const line of lines)
|
|
167
|
-
console.log(line);
|
|
168
|
-
if (lines.length === 0)
|
|
169
|
-
console.log(chalk.dim(" Waiting for new requests..."));
|
|
170
|
-
const suffix = daemon ? `file watch + ${opts.interval}s refresh` : cloud ? `cloud stream every ${opts.interval}s` : `polling every ${opts.interval}s`;
|
|
171
|
-
console.log(chalk.dim(`
|
|
172
|
-
Last updated: ${new Date().toLocaleTimeString()} \u2014 ${suffix} \u2014 Ctrl+C to exit`));
|
|
173
|
-
}
|
|
174
|
-
if (daemon)
|
|
175
|
-
ingestPending = true;
|
|
176
|
-
await poll();
|
|
177
|
-
const timer = setInterval(() => {
|
|
178
|
-
poll().catch((e) => {
|
|
179
|
-
console.log(chalk.dim(`
|
|
180
|
-
Poll error: ${e instanceof Error ? e.message : String(e)} \u2014 retrying next tick`));
|
|
181
|
-
});
|
|
182
|
-
}, opts.interval * 1000);
|
|
183
|
-
process.on("SIGINT", () => {
|
|
184
|
-
clearInterval(timer);
|
|
185
|
-
if (ingestTimer)
|
|
186
|
-
clearTimeout(ingestTimer);
|
|
187
|
-
console.log(chalk.dim(`
|
|
188
|
-
|
|
189
|
-
Stopped watching.`));
|
|
190
|
-
process.exit(0);
|
|
191
|
-
});
|
|
192
|
-
await new Promise(() => {});
|
|
193
|
-
}
|
|
194
|
-
export {
|
|
195
|
-
watchCosts
|
|
196
|
-
};
|