@ponbac/token-ledger 0.6.1 → 0.7.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 +3 -1
  2. package/dist/main.js +24 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -455,6 +455,8 @@ 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.
@@ -773,7 +775,7 @@ For manual publication:
773
775
  1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
774
776
  2. Run `bun run check`. The CLI test packs the package, installs it offline outside
775
777
  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).
778
+ 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.7.0.tgz` (filename follows version).
777
779
  4. Inspect `npm publish ./dist/npm --dry-run`.
778
780
  5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
779
781
  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.7.0",
42799
42813
  private: true,
42800
42814
  type: "module",
42801
42815
  bin: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ponbac/token-ledger",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "Local AI token usage and API-equivalent cost reports by project",
5
5
  "license": "MIT",
6
6
  "engines": {