@ponbac/token-ledger 0.6.1 → 0.8.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.
Files changed (3) hide show
  1. package/README.md +25 -16
  2. package/dist/main.js +67 -26
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -455,10 +455,12 @@ Copilot requests routed through a ChatGPT OAuth proxy can use
455
455
  [local billing hints](docs/billing-hints.md). On Windows the proxy appends metadata to
456
456
  `%LOCALAPPDATA%\token-ledger\billing-hints\chatgpt-proxy.jsonl`; no path configuration
457
457
  is needed. Usage stays attributed to Copilot, with ChatGPT/Codex as its subscription payer.
458
+ A hint can also name the request's repository, for custom endpoints whose Copilot
459
+ telemetry has none.
458
460
 
459
461
  `sync` uploads this machine's usage to a [toki2](https://github.com/ponbac/toki2)
460
462
  server, which combines every developer's machines for team reporting and billing.
461
- Run it on **every machine** where you use a coding agent, and schedule it daily.
463
+ Run it on **every machine** where you use a coding agent, and schedule it.
462
464
 
463
465
  ### Set up
464
466
 
@@ -505,24 +507,31 @@ If you map projects in a `token-ledger.json`, pass it with `--config`; scheduled
505
507
  do not run in your project directory. Days follow the system time zone; add
506
508
  `--time-zone Europe/Stockholm` when a scheduler's environment may lack one.
507
509
 
508
- ### Schedule a daily sync
510
+ ### Schedule a sync
509
511
 
510
- Schedule a daily sync to upload history before providers prune it:
512
+ Schedule a sync to upload history regularly, and before providers prune it:
511
513
 
512
514
  ```sh
513
515
  npx @ponbac/token-ledger@latest schedule install
514
516
  ```
515
517
 
516
- This registers a daily job with the operating system's own scheduler; token-ledger
517
- does not keep a process running. Run it again to change the job.
518
+ This registers a job with the operating system's own scheduler that syncs every two
519
+ hours, at 00:30, 02:30, and so on; token-ledger does not keep a process running. Run
520
+ it again with other flags to change the job: the new schedule replaces the old one.
518
521
 
519
- | Flag | Effect |
520
- | ----------------------- | ------------------------------------------------------- |
521
- | `--at 07:30` | Daily local time; the default is `12:30` |
522
- | `--config <file>` | Project mappings for the job, saved as an absolute path |
523
- | `--time-zone <zone>` | Pass a time zone when the scheduler may not have one |
524
- | `--provider <provider>` | Sync one provider only |
525
- | `--dry-run` | Print the files and commands, and change nothing |
522
+ ```sh
523
+ npx @ponbac/token-ledger@latest schedule install --every 1h # hourly, at :30
524
+ npx @ponbac/token-ledger@latest schedule install --every 24h --at 07:30 # once a day
525
+ ```
526
+
527
+ | Flag | Effect |
528
+ | ----------------------- | ------------------------------------------------------------------------ |
529
+ | `--every 1h` | `1h`, `2h`, `3h`, `4h`, `6h`, `8h`, `12h`, or `24h`; the default is `2h` |
530
+ | `--at 07:30` | A local time a sync runs at; the default is `12:30` |
531
+ | `--config <file>` | Project mappings for the job, saved as an absolute path |
532
+ | `--time-zone <zone>` | Pass a time zone when the scheduler may not have one |
533
+ | `--provider <provider>` | Sync one provider only |
534
+ | `--dry-run` | Print the files and commands, and change nothing |
526
535
 
527
536
  `schedule status` shows the scheduler's own report, including the last run, and
528
537
  `schedule uninstall` removes the job and keeps your settings. A run's output is in
@@ -535,7 +544,7 @@ launcher in your token-ledger settings directory waits for sync to finish and
535
544
  passes its exit code back to Task Scheduler. Windows Script Host must be enabled.
536
545
  To update a task installed by an older CLI version, run
537
546
  `npx.cmd --yes @ponbac/token-ledger@latest schedule install` again, including any
538
- `--at`, `--config`, `--time-zone`, or `--provider` options you used before. This
547
+ `--every`, `--at`, `--config`, `--time-zone`, or `--provider` options you used before. This
539
548
  replaces the existing task; your saved server and token are kept.
540
549
 
541
550
  | Platform | Scheduler | A missed run |
@@ -561,8 +570,8 @@ Without one, as on Alpine or in WSL with systemd disabled, use the cron entry be
561
570
  <details>
562
571
  <summary>Scheduling by hand</summary>
563
572
 
564
- `schedule install --dry-run` prints the exact files and commands for your machine.
565
- The examples below do the same with a login shell finding `npx`; if it is not on the
573
+ `schedule install --dry-run` prints the exact files and commands for your machine,
574
+ with the times for any `--every`. The examples below sync once a day. They do the same with a login shell finding `npx`; if it is not on the
566
575
  scheduler's `PATH`, use its absolute path or install the CLI globally with
567
576
  `npm install -g @ponbac/token-ledger` and run `token-ledger sync`.
568
577
 
@@ -773,7 +782,7 @@ For manual publication:
773
782
  1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
774
783
  2. Run `bun run check`. The CLI test packs the package, installs it offline outside
775
784
  the repository with lifecycle scripts disabled, and exercises the installed CLI.
776
- 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.6.1.tgz` (filename follows version).
785
+ 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.8.0.tgz` (filename follows version).
777
786
  4. Inspect `npm publish ./dist/npm --dry-run`.
778
787
  5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
779
788
  prompt if requested.
package/dist/main.js CHANGED
@@ -32694,7 +32694,8 @@ class BillingHintsError extends exports_Schema.TaggedError()("BillingHintsError"
32694
32694
  var Hint = exports_Schema.fromJsonString(exports_Schema.Struct({
32695
32695
  requestId: exports_Schema.NonEmptyString,
32696
32696
  billingProvider: exports_Schema.Literal("codex"),
32697
- authMode: exports_Schema.Literal("chatgpt")
32697
+ authMode: exports_Schema.Literal("chatgpt"),
32698
+ repository: exports_Schema.optionalKey(exports_Schema.NonEmptyString)
32698
32699
  }));
32699
32700
  var readBillingHints = exports_Effect.fn("BillingHints.read")(function* (location2) {
32700
32701
  const fs = yield* exports_FileSystem.FileSystem;
@@ -32702,10 +32703,10 @@ var readBillingHints = exports_Effect.fn("BillingHints.read")(function* (locatio
32702
32703
  const entries3 = yield* fs.readDirectory(location2.directory).pipe(exports_Effect.result);
32703
32704
  if (exports_Result.isFailure(entries3)) {
32704
32705
  if (!location2.required && exports_Predicate.isTagged(entries3.failure.reason, "NotFound"))
32705
- return new Set;
32706
+ return new Map;
32706
32707
  return yield* new BillingHintsError({ message: "Cannot read the billing hints directory." });
32707
32708
  }
32708
- const requests = new Set;
32709
+ const repositories = new Map;
32709
32710
  for (const entry of entries3.success.toSorted()) {
32710
32711
  if (!entry.endsWith(".jsonl"))
32711
32712
  continue;
@@ -32719,10 +32720,16 @@ var readBillingHints = exports_Effect.fn("BillingHints.read")(function* (locatio
32719
32720
  const hint = yield* exports_Schema.decodeUnknownEffect(Hint)(line).pipe(exports_Effect.mapError(() => new BillingHintsError({
32720
32721
  message: `Invalid row in ${JSON.stringify(entry)} at line ${lineNumber}.`
32721
32722
  })));
32722
- requests.add(hint.requestId);
32723
+ const named = repositories.get(hint.requestId) ?? new Set;
32724
+ if (hint.repository !== undefined)
32725
+ named.add(hint.repository);
32726
+ repositories.set(hint.requestId, named);
32723
32727
  }
32724
32728
  }
32725
- return requests;
32729
+ return new Map([...repositories].map(([requestId, named]) => [
32730
+ requestId,
32731
+ { repository: named.size === 1 ? [...named][0] ?? null : null }
32732
+ ]));
32726
32733
  });
32727
32734
 
32728
32735
  // packages/core/src/model.ts
@@ -34731,7 +34738,7 @@ function repositoryReference(attributes) {
34731
34738
  const remote = attributes["copilot_chat.repo.remote_url"];
34732
34739
  return remote === undefined ? null : RepositoryReference.Remote({ value: remote });
34733
34740
  }
34734
- function copilotParser(file2, chatgptRequests = new Set) {
34741
+ function copilotParser(file2, chatgptRequests = new Map) {
34735
34742
  const spans = new Map;
34736
34743
  const candidates = [];
34737
34744
  const warning = "Copilot coverage starts when telemetry was enabled; only inference requests are counted.";
@@ -34803,10 +34810,14 @@ function copilotParser(file2, chatgptRequests = new Set) {
34803
34810
  if (responseId === undefined)
34804
34811
  return candidate;
34805
34812
  const record3 = { ...candidate.record, id: `response:${responseId}` };
34813
+ const routed = chatgptRequests.get(responseId);
34814
+ if (routed === undefined)
34815
+ return { ...candidate, responseId, record: record3 };
34806
34816
  return {
34807
34817
  ...candidate,
34808
34818
  responseId,
34809
- record: chatgptRequests.has(responseId) ? { ...record3, billingProvider: "codex" } : record3
34819
+ record: { ...record3, billingProvider: "codex" },
34820
+ hinted: routed.repository === null ? null : RepositoryReference.Remote({ value: routed.repository })
34810
34821
  };
34811
34822
  });
34812
34823
  const preferred = new Map;
@@ -34835,6 +34846,9 @@ function copilotParser(file2, chatgptRequests = new Set) {
34835
34846
  });
34836
34847
  }
34837
34848
  }
34849
+ for (const [id2, merged] of preferred)
34850
+ if (merged.record.repository === null && merged.hinted)
34851
+ preferred.set(id2, { ...merged, record: { ...merged.record, repository: merged.hinted } });
34838
34852
  const records = identified.map((candidate) => preferred.get(candidate.record.id)?.record ?? candidate.record);
34839
34853
  candidates.length = 0;
34840
34854
  spans.clear();
@@ -35271,8 +35285,8 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35271
35285
  return request3;
35272
35286
  });
35273
35287
  const scan2 = exports_Effect.fn("Ledger.scan")(function* (request3, start, locate, collect2, notice, onWarning) {
35274
- const hints = yield* (notice !== undefined && request3.billingHints !== undefined && request3.sources.some((source) => source.provider === "copilot") ? readBillingHints(request3.billingHints).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path)) : exports_Effect.succeed(new Set)).pipe(exports_Effect.result);
35275
- const chatgptRequests = exports_Result.isSuccess(hints) ? hints.success : new Set;
35288
+ const hints = yield* (notice !== undefined && request3.billingHints !== undefined && request3.sources.some((source) => source.provider === "copilot") ? readBillingHints(request3.billingHints).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path)) : exports_Effect.succeed(new Map)).pipe(exports_Effect.result);
35289
+ const chatgptRequests = exports_Result.isSuccess(hints) ? hints.success : new Map;
35276
35290
  if (exports_Result.isFailure(hints))
35277
35291
  yield* onWarning?.(`Billing hints: ${hints.failure.message} Copilot will not be uploaded. Repair the hints and rerun sync with --since <first missed day>.`) ?? exports_Effect.void;
35278
35292
  const resolveProject = yield* projectResolver(request3.projects).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path));
@@ -42795,7 +42809,7 @@ var runWith2 = (command, config) => {
42795
42809
  // packages/cli/package.json
42796
42810
  var package_default = {
42797
42811
  name: "@ponbac/token-ledger",
42798
- version: "0.6.1",
42812
+ version: "0.8.0",
42799
42813
  private: true,
42800
42814
  type: "module",
42801
42815
  bin: {
@@ -43967,6 +43981,22 @@ class ScheduleError extends exports_Schema.TaggedError()("ScheduleError", {
43967
43981
  }) {
43968
43982
  }
43969
43983
  var TimeOfDay = exports_Schema.String.check(exports_Schema.isPattern(/^(?:[01]\d|2[0-3]):[0-5]\d$/));
43984
+ var intervals = [
43985
+ ["1h", 1],
43986
+ ["2h", 2],
43987
+ ["3h", 3],
43988
+ ["4h", 4],
43989
+ ["6h", 6],
43990
+ ["8h", 8],
43991
+ ["12h", 12],
43992
+ ["24h", 24]
43993
+ ];
43994
+ function syncTimes(job) {
43995
+ const hour2 = Number(job.at.slice(0, 2));
43996
+ const minute = job.at.slice(3);
43997
+ return Array.from({ length: 24 / job.every }, (_, index2) => `${String(hour2 % job.every + index2 * job.every).padStart(2, "0")}:${minute}`);
43998
+ }
43999
+ var describeTimes = (job) => job.every === 24 ? `daily at ${job.at}` : `every ${job.every === 1 ? "hour" : `${job.every} hours`}, at ${syncTimes(job).join(", ")}`;
43970
44000
  var Action3 = exports_Data.taggedEnum();
43971
44001
  var unit = "token-ledger-sync";
43972
44002
  var label = "com.github.ponbac.token-ledger.sync";
@@ -44024,10 +44054,10 @@ ExecStart=/bin/sh -lc ${systemdQuoted(posixScript(job2))}
44024
44054
  Action3.Write({
44025
44055
  file: timer,
44026
44056
  contents: `[Unit]
44027
- Description=Daily AI usage sync
44057
+ Description=AI usage sync ${job2.every === 24 ? "once a day" : `every ${job2.every}h`}
44028
44058
 
44029
44059
  [Timer]
44030
- OnCalendar=*-*-* ${job2.at}:00
44060
+ OnCalendar=*-*-* ${syncTimes(job2).map((time2) => time2.slice(0, 2)).join(",")}:${job2.at.slice(3)}:00
44031
44061
  Persistent=true
44032
44062
  RandomizedDelaySec=10m
44033
44063
 
@@ -44061,7 +44091,16 @@ function launchAgent(home, uid) {
44061
44091
  return {
44062
44092
  name: "launchd agent",
44063
44093
  install: (job) => posixJob(job).pipe(exports_Effect.map((job2) => {
44064
- const [hour2, minute] = job2.at.split(":").map(Number);
44094
+ const times = syncTimes(job2).map((time2) => {
44095
+ const [hour2, minute] = time2.split(":").map(Number);
44096
+ return ` <dict>
44097
+ <key>Hour</key>
44098
+ <integer>${hour2}</integer>
44099
+ <key>Minute</key>
44100
+ <integer>${minute}</integer>
44101
+ </dict>`;
44102
+ }).join(`
44103
+ `);
44065
44104
  return [
44066
44105
  Action3.Write({
44067
44106
  file: plist,
@@ -44080,12 +44119,9 @@ function launchAgent(home, uid) {
44080
44119
  <key>RunAtLoad</key>
44081
44120
  <true/>
44082
44121
  <key>StartCalendarInterval</key>
44083
- <dict>
44084
- <key>Hour</key>
44085
- <integer>${hour2}</integer>
44086
- <key>Minute</key>
44087
- <integer>${minute}</integer>
44088
- </dict>
44122
+ <array>
44123
+ ${times}
44124
+ </array>
44089
44125
  <key>StandardOutPath</key>
44090
44126
  <string>${xmlText(log3)}</string>
44091
44127
  <key>StandardErrorPath</key>
@@ -44127,7 +44163,10 @@ function taskScheduler(file2) {
44127
44163
  "$ErrorActionPreference = 'Stop'",
44128
44164
  "$ProgressPreference = 'SilentlyContinue'",
44129
44165
  `$action = New-ScheduledTaskAction -Execute 'wscript.exe' -Argument ${powershellString(`//B //Nologo //E:JScript "${file2}"`)}`,
44130
- `$trigger = New-ScheduledTaskTrigger -Daily -At '${job2.at}'`,
44166
+ `$trigger = New-ScheduledTaskTrigger -Daily -At '${syncTimes(job2)[0]}'`,
44167
+ ...job2.every === 24 ? [] : [
44168
+ `$trigger.Repetition = (New-ScheduledTaskTrigger -Once -At '${syncTimes(job2)[0]}' -RepetitionInterval (New-TimeSpan -Hours ${job2.every}) -RepetitionDuration (New-TimeSpan -Hours ${24 - job2.every})).Repetition`
44169
+ ],
44131
44170
  "$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries",
44132
44171
  `Register-ScheduledTask -TaskName ${powershellString(task)} -Action $action -Trigger $trigger -Settings $settings -Force | Out-Null`
44133
44172
  ].join(`
@@ -46025,7 +46064,8 @@ var login = exports_Command.make("login", {
46025
46064
  Next: token-ledger sync --dry-run, then token-ledger schedule install.`);
46026
46065
  })).pipe(exports_Command.withDescription("Save the toki2 server and API token in sync.json for sync and scheduled syncs"));
46027
46066
  var scheduleInstall = exports_Command.make("install", {
46028
- at: exports_Flag.String("at").pipe(exports_Flag.withDefault("12:30"), exports_Flag.withSchema(TimeOfDay), exports_Flag.withDescription("Daily local time as HH:MM; defaults to 12:30")),
46067
+ every: exports_Flag.ChoiceWithValue("every", intervals).pipe(exports_Flag.withDefault(2), exports_Flag.withDescription("Time between syncs; defaults to 2h")),
46068
+ at: exports_Flag.String("at").pipe(exports_Flag.withDefault("12:30"), exports_Flag.withSchema(TimeOfDay), exports_Flag.withDescription("A local time, as HH:MM, that a sync runs at; defaults to 12:30")),
46029
46069
  config: exports_Flag.String("config").pipe(exports_Flag.optional, exports_Flag.withDescription("JSON config for the scheduled sync; ./token-ledger.json is not read")),
46030
46070
  timeZone: exports_Flag.String("time-zone").pipe(exports_Flag.withSchema(exports_Schema.TimeZoneNamedFromString), exports_Flag.optional, exports_Flag.withDescription("IANA time zone for the scheduled sync; defaults to the system zone")),
46031
46071
  provider: exports_Flag.Literals("provider", ["all", "codex", "claude", "grok", "copilot", "pi"]).pipe(exports_Flag.withDefault("all")),
@@ -46039,6 +46079,7 @@ var scheduleInstall = exports_Command.make("install", {
46039
46079
  return yield* new CliError2({ message: `Cannot find the configuration ${config.value}.` });
46040
46080
  const actions = yield* scheduler.install({
46041
46081
  at: flags.at,
46082
+ every: flags.every,
46042
46083
  nodeDirectory: path.dirname(process.execPath),
46043
46084
  configHome: yield* configHome,
46044
46085
  dataHome: yield* dataHome,
@@ -46060,18 +46101,18 @@ var scheduleInstall = exports_Command.make("install", {
46060
46101
  message: "No saved server and API token. Run token-ledger login first: a scheduled sync cannot read --token or your shell's environment."
46061
46102
  });
46062
46103
  yield* perform(actions);
46063
- yield* exports_Console.log(`Scheduled a daily sync at ${flags.at} as a ${scheduler.name}. Inspect it with token-ledger schedule status.`);
46064
- })).pipe(exports_Command.withDescription("Install or replace the daily sync; it needs the server and token saved by login"));
46104
+ yield* exports_Console.log(`Scheduled a sync ${describeTimes(flags)} as a ${scheduler.name}, replacing any earlier schedule. Inspect it with token-ledger schedule status.`);
46105
+ })).pipe(exports_Command.withDescription("Install or replace the scheduled sync; it needs the server and token saved by login"));
46065
46106
  var scheduleStatus = exports_Command.make("status", {}, exports_Effect.fn("CLI.scheduleStatus")(function* () {
46066
46107
  const scheduler = yield* hostScheduler;
46067
46108
  yield* perform(scheduler.status);
46068
- })).pipe(exports_Command.withDescription("Show the scheduler's own report of the daily sync"));
46109
+ })).pipe(exports_Command.withDescription("Show the scheduler's own report of the scheduled sync"));
46069
46110
  var scheduleUninstall = exports_Command.make("uninstall", {}, exports_Effect.fn("CLI.scheduleUninstall")(function* () {
46070
46111
  const scheduler = yield* hostScheduler;
46071
46112
  yield* perform(scheduler.uninstall);
46072
46113
  yield* exports_Console.log(`Removed the ${scheduler.name}. Saved settings were kept.`);
46073
- })).pipe(exports_Command.withDescription("Remove the daily sync; saved settings stay"));
46074
- var schedule4 = exports_Command.make("schedule").pipe(exports_Command.withDescription("Run sync daily with systemd, launchd, or Task Scheduler"), exports_Command.withSubcommands([scheduleInstall, scheduleStatus, scheduleUninstall]));
46114
+ })).pipe(exports_Command.withDescription("Remove the scheduled sync; saved settings stay"));
46115
+ var schedule4 = exports_Command.make("schedule").pipe(exports_Command.withDescription("Run sync every few hours with systemd, launchd, or Task Scheduler"), exports_Command.withSubcommands([scheduleInstall, scheduleStatus, scheduleUninstall]));
46075
46116
  var copilotEnable = exports_Command.make("enable", {
46076
46117
  cli: exports_Flag.Boolean("cli").pipe(exports_Flag.withDefault(false), exports_Flag.withDescription("Copilot CLI: export its usage from every new terminal")),
46077
46118
  vscode: exports_Flag.Boolean("vscode").pipe(exports_Flag.withDefault(false), exports_Flag.withDescription("VS Code Copilot Chat: set its export in the user settings.json")),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ponbac/token-ledger",
3
- "version": "0.6.1",
3
+ "version": "0.8.0",
4
4
  "description": "Local AI token usage and API-equivalent cost reports by project",
5
5
  "license": "MIT",
6
6
  "engines": {