@ponbac/token-ledger 0.10.0 → 0.11.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.
- package/README.md +30 -7
- package/dist/main.js +200 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,9 +29,11 @@ The default window is the current calendar month through today. `--since` and
|
|
|
29
29
|
`--until` are inclusive local calendar days in the system time zone; select another
|
|
30
30
|
IANA zone with `--time-zone`, such as `--time-zone Europe/Stockholm`. Days follow
|
|
31
31
|
daylight saving time, so a day can last 23 or 25 hours. Read every
|
|
32
|
-
supported source by omitting `--provider`. Copilot
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
supported source by omitting `--provider`. Copilot CLI and VS Code's Copilot harness
|
|
33
|
+
keep their own session logs, which are read without setup. VS Code's Local harness and
|
|
34
|
+
inline chat record nothing until Copilot's export is switched on: run
|
|
35
|
+
[`copilot enable`](#enable-copilots-usage-export) before starting sessions; installing
|
|
36
|
+
the CLI does not enable telemetry.
|
|
35
37
|
|
|
36
38
|
```sh
|
|
37
39
|
npx @ponbac/token-ledger@latest report --since 2026-09-01 --until 2026-09-30 --format csv > usage.csv
|
|
@@ -100,6 +102,7 @@ node packages/cli/dist/main.js report
|
|
|
100
102
|
| Codex | `~/.codex/sessions` and `archived_sessions` | Rollout token events, model switches, session and working-directory metadata |
|
|
101
103
|
| Claude Code | `~/.claude/projects` | Assistant usage records; repeated message blocks counted once |
|
|
102
104
|
| Grok Build | `~/.grok/sessions/**/updates.jsonl` | Saved completed turns and their model breakdowns |
|
|
105
|
+
| Copilot sessions | `~/.copilot/session-state/*/events.jsonl` | Request usage and the workspace of Copilot CLI and VS Code Copilot harness runs |
|
|
103
106
|
| Copilot CLI / VS Code Chat | `~/.copilot/otel` or `COPILOT_OTEL_FILE_EXPORTER_PATH` | JSONL request spans, VS Code inference events, and SQLite span exports |
|
|
104
107
|
| Pi | `~/.pi/agent/sessions` or `PI_CODING_AGENT_SESSION_DIR` | Assistant messages on every branch, plus usage, compaction, and summary entries |
|
|
105
108
|
| OpenCode | `~/.local/share/opencode` | Assistant messages in `opencode*.db` and legacy `storage/message` files |
|
|
@@ -139,11 +142,31 @@ hyphenated ID. OpenCode's own recorded costs are not used for estimates.
|
|
|
139
142
|
exactly zero to Codex, because OpenCode's ChatGPT sign-in clears every OpenAI model's
|
|
140
143
|
rates. Other routes, OpenCode Zen and API keys among them, are API-equivalent estimates.
|
|
141
144
|
|
|
145
|
+
### Copilot session logs
|
|
146
|
+
|
|
147
|
+
The Copilot runtime behind Copilot CLI and VS Code's Copilot harness, the default for new
|
|
148
|
+
VS Code chats since 1.143, keeps an event log per session in
|
|
149
|
+
`~/.copilot/session-state/<session>/events.jsonl` (`$COPILOT_HOME/session-state`). It
|
|
150
|
+
records each request's model and tokens whether or not an export is on, and the session's
|
|
151
|
+
working directory and GitHub repository, which name the project. Those logs also hold the
|
|
152
|
+
session's prompts, responses, and tool output: Token Ledger decodes only the session start
|
|
153
|
+
and usage events, and never reads the rest. Requests on your own key (BYOK) are not
|
|
154
|
+
Copilot's to bill and are left out of syncs, unless the local proxy routed them to a ChatGPT
|
|
155
|
+
plan; a report, which reads no proxy hints, counts them. A request also found in Copilot's export below is
|
|
156
|
+
counted once.
|
|
157
|
+
|
|
158
|
+
The Copilot harness runs in VS Code's Agent Host, which ignores the Chat export settings
|
|
159
|
+
below, so before session logs were read its requests went unrecorded. VS Code can also
|
|
160
|
+
export them through a device-wide `managed-settings.json`
|
|
161
|
+
([`telemetry.filePath`](https://code.visualstudio.com/docs/agents/guides/monitoring-agents));
|
|
162
|
+
that file needs admin rights, turns off VS Code's Claude harness, and is not needed here.
|
|
163
|
+
Its export is read like the others when it is in the export folder.
|
|
164
|
+
|
|
142
165
|
### Enable Copilot's usage export
|
|
143
166
|
|
|
144
|
-
|
|
145
|
-
and nothing is backfilled: sessions from before that are lost.
|
|
146
|
-
it on for Copilot CLI and VS Code Copilot Chat:
|
|
167
|
+
VS Code's Local harness and inline chat write no usage log until a local OpenTelemetry
|
|
168
|
+
file export is switched on, and nothing is backfilled: sessions from before that are lost.
|
|
169
|
+
One command switches it on for Copilot CLI and VS Code Copilot Chat:
|
|
147
170
|
|
|
148
171
|
```sh
|
|
149
172
|
npx @ponbac/token-ledger@latest copilot enable
|
|
@@ -923,7 +946,7 @@ For manual publication:
|
|
|
923
946
|
1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
|
|
924
947
|
2. Run `bun run check`. The CLI test packs the package, installs it offline outside
|
|
925
948
|
the repository with lifecycle scripts disabled, and exercises the installed CLI.
|
|
926
|
-
3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.
|
|
949
|
+
3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.11.0.tgz` (filename follows version).
|
|
927
950
|
4. Inspect `npm publish ./dist/npm --dry-run`.
|
|
928
951
|
5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
|
|
929
952
|
prompt if requested.
|
package/dist/main.js
CHANGED
|
@@ -35111,6 +35111,7 @@ var empty14 = {
|
|
|
35111
35111
|
inherited: [],
|
|
35112
35112
|
deferred: [],
|
|
35113
35113
|
hints: [],
|
|
35114
|
+
excluded: [],
|
|
35114
35115
|
malformed: 0,
|
|
35115
35116
|
skipped: 0,
|
|
35116
35117
|
warnings: []
|
|
@@ -35334,6 +35335,7 @@ var CopilotAttributes = exports_Schema.Struct({
|
|
|
35334
35335
|
"gen_ai.usage.cache_creation.input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35335
35336
|
"gen_ai.usage.cache_read_input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35336
35337
|
"gen_ai.usage.cache_creation_input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35338
|
+
"gen_ai.usage.cache_write.input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35337
35339
|
"github.copilot.git.repository": exports_Schema.optionalKey(exports_Schema.String),
|
|
35338
35340
|
"copilot_chat.repo.remote_url": exports_Schema.optionalKey(exports_Schema.String),
|
|
35339
35341
|
"session.id": exports_Schema.optionalKey(exports_Schema.String)
|
|
@@ -35366,10 +35368,12 @@ var Header = exports_Schema.Struct({
|
|
|
35366
35368
|
"gen_ai.operation.name": exports_Schema.optionalKey(exports_Schema.String)
|
|
35367
35369
|
})
|
|
35368
35370
|
});
|
|
35371
|
+
var requestId = (responseId, tokens) => `response:${responseId}#${tokens.input + tokens.cacheRead + tokens.cacheWrite}:${tokens.output}`;
|
|
35372
|
+
var isOwnerAndName = (repository) => /^[A-Za-z0-9-]+\/(?!\.{1,2}$)[A-Za-z0-9_.-]+$/.test(repository);
|
|
35369
35373
|
function repositoryReference(attributes) {
|
|
35370
35374
|
const repository = attributes["github.copilot.git.repository"];
|
|
35371
35375
|
if (repository !== undefined)
|
|
35372
|
-
return
|
|
35376
|
+
return isOwnerAndName(repository) ? RepositoryReference.Identity({ value: repository }) : RepositoryReference.Remote({ value: repository });
|
|
35373
35377
|
const remote = attributes["copilot_chat.repo.remote_url"];
|
|
35374
35378
|
return remote === undefined ? null : RepositoryReference.Remote({ value: remote });
|
|
35375
35379
|
}
|
|
@@ -35410,7 +35414,7 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35410
35414
|
if (input === undefined || output === undefined)
|
|
35411
35415
|
return skipped;
|
|
35412
35416
|
const cacheRead = a["gen_ai.usage.cache_read.input_tokens"] ?? a["gen_ai.usage.cache_read_input_tokens"] ?? 0;
|
|
35413
|
-
const cacheWrite = a["gen_ai.usage.cache_creation.input_tokens"] ?? a["gen_ai.usage.cache_creation_input_tokens"] ?? 0;
|
|
35417
|
+
const cacheWrite = a["gen_ai.usage.cache_creation.input_tokens"] ?? a["gen_ai.usage.cache_creation_input_tokens"] ?? a["gen_ai.usage.cache_write.input_tokens"] ?? 0;
|
|
35414
35418
|
if (cacheRead + cacheWrite > input)
|
|
35415
35419
|
return skipped;
|
|
35416
35420
|
const ms = span2.endTime[0] * 1000 + Math.floor(span2.endTime[1] / 1e6);
|
|
@@ -35439,12 +35443,16 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35439
35443
|
for (const { spanKey, responseId, isSpan } of candidates)
|
|
35440
35444
|
if (!isSpan && spanKey !== undefined && responseId !== undefined)
|
|
35441
35445
|
eventResponses.set(spanKey, (eventResponses.get(spanKey) ?? new Set).add(responseId));
|
|
35442
|
-
const
|
|
35446
|
+
const responses = candidates.map((candidate) => {
|
|
35443
35447
|
const shared = candidate.isSpan && candidate.responseId === undefined && candidate.spanKey !== undefined ? eventResponses.get(candidate.spanKey) : undefined;
|
|
35444
|
-
|
|
35448
|
+
return shared?.size === 1 ? [...shared][0] : candidate.responseId;
|
|
35449
|
+
});
|
|
35450
|
+
const identified = candidates.map((candidate, index2) => {
|
|
35451
|
+
const responseId = responses[index2];
|
|
35445
35452
|
if (responseId === undefined)
|
|
35446
35453
|
return candidate;
|
|
35447
|
-
const
|
|
35454
|
+
const id2 = requestId(responseId, candidate.record.tokens);
|
|
35455
|
+
const record3 = { ...candidate.record, id: id2 };
|
|
35448
35456
|
const routed = chatgptRequests.get(responseId);
|
|
35449
35457
|
if (routed === undefined)
|
|
35450
35458
|
return { ...candidate, responseId, record: record3 };
|
|
@@ -35455,8 +35463,7 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35455
35463
|
hinted: routed.repository === null ? null : RepositoryReference.Remote({ value: routed.repository })
|
|
35456
35464
|
};
|
|
35457
35465
|
});
|
|
35458
|
-
const
|
|
35459
|
-
for (const candidate of identified) {
|
|
35466
|
+
const inheritedRepository = (candidate) => {
|
|
35460
35467
|
let repository = candidate.record.repository;
|
|
35461
35468
|
let key = candidate.spanKey;
|
|
35462
35469
|
const visited = new Set;
|
|
@@ -35466,6 +35473,11 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35466
35473
|
repository = ancestor?.repository ?? null;
|
|
35467
35474
|
key = ancestor?.parent;
|
|
35468
35475
|
}
|
|
35476
|
+
return repository;
|
|
35477
|
+
};
|
|
35478
|
+
const preferred = new Map;
|
|
35479
|
+
for (const candidate of identified) {
|
|
35480
|
+
const repository = inheritedRepository(candidate);
|
|
35469
35481
|
const resolved = { ...candidate, record: { ...candidate.record, repository } };
|
|
35470
35482
|
const previous = preferred.get(resolved.record.id);
|
|
35471
35483
|
if (previous === undefined)
|
|
@@ -35537,6 +35549,144 @@ var copilotDatabaseLines = exports_Effect.fn("Copilot.readDatabase")(function* (
|
|
|
35537
35549
|
}).pipe(exports_Effect.scoped);
|
|
35538
35550
|
});
|
|
35539
35551
|
|
|
35552
|
+
// packages/core/src/providers/copilot-session.ts
|
|
35553
|
+
var Workspace = exports_Schema.Struct({
|
|
35554
|
+
cwd: exports_Schema.optionalKey(exports_Schema.String),
|
|
35555
|
+
repository: exports_Schema.optionalKey(exports_Schema.String),
|
|
35556
|
+
repositoryHost: exports_Schema.optionalKey(exports_Schema.String),
|
|
35557
|
+
hostType: exports_Schema.optionalKey(exports_Schema.String),
|
|
35558
|
+
pendingGitContext: exports_Schema.optionalKey(exports_Schema.Boolean)
|
|
35559
|
+
});
|
|
35560
|
+
var SessionStart = exports_Schema.Struct({
|
|
35561
|
+
type: exports_Schema.Literal("session.start"),
|
|
35562
|
+
data: exports_Schema.Struct({
|
|
35563
|
+
sessionId: exports_Schema.NonEmptyString,
|
|
35564
|
+
context: exports_Schema.optionalKey(Workspace)
|
|
35565
|
+
})
|
|
35566
|
+
});
|
|
35567
|
+
var ContextChanged = exports_Schema.Struct({
|
|
35568
|
+
type: exports_Schema.Literal("session.context_changed"),
|
|
35569
|
+
data: Workspace
|
|
35570
|
+
});
|
|
35571
|
+
var Resumed = exports_Schema.Struct({
|
|
35572
|
+
type: exports_Schema.Literal("session.resume"),
|
|
35573
|
+
data: exports_Schema.Struct({ context: exports_Schema.optionalKey(Workspace) })
|
|
35574
|
+
});
|
|
35575
|
+
var UsageEvent = exports_Schema.Struct({
|
|
35576
|
+
type: exports_Schema.Literal("session.usage_record"),
|
|
35577
|
+
timestamp: exports_Schema.String,
|
|
35578
|
+
data: exports_Schema.Struct({
|
|
35579
|
+
usage: exports_Schema.Struct({
|
|
35580
|
+
model: exports_Schema.NonEmptyString,
|
|
35581
|
+
inputTokens: exports_Schema.Natural,
|
|
35582
|
+
outputTokens: exports_Schema.Natural,
|
|
35583
|
+
cacheReadTokens: exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35584
|
+
cacheWriteTokens: exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35585
|
+
isByok: exports_Schema.optionalKey(exports_Schema.Boolean),
|
|
35586
|
+
apiCallId: exports_Schema.optionalKey(exports_Schema.NonEmptyString),
|
|
35587
|
+
accounting: exports_Schema.optionalKey(exports_Schema.Struct({
|
|
35588
|
+
sourceSessionId: exports_Schema.optionalKey(exports_Schema.NonEmptyString),
|
|
35589
|
+
usageId: exports_Schema.optionalKey(exports_Schema.NonEmptyString)
|
|
35590
|
+
}))
|
|
35591
|
+
})
|
|
35592
|
+
})
|
|
35593
|
+
});
|
|
35594
|
+
var Header2 = exports_Schema.Struct({ type: exports_Schema.String });
|
|
35595
|
+
var decodeHeader = exports_Schema.decodeUnknownOption(Header2);
|
|
35596
|
+
var decodeStart = exports_Schema.decodeUnknownOption(SessionStart);
|
|
35597
|
+
var decodeChange = exports_Schema.decodeUnknownOption(ContextChanged);
|
|
35598
|
+
var decodeResume = exports_Schema.decodeUnknownOption(Resumed);
|
|
35599
|
+
var decodeUsage = exports_Schema.decodeUnknownOption(UsageEvent);
|
|
35600
|
+
function insideSession(entry) {
|
|
35601
|
+
return entry.split(/[\\/]/).at(-3) === "session-state";
|
|
35602
|
+
}
|
|
35603
|
+
var accounting = [
|
|
35604
|
+
'"session.start"',
|
|
35605
|
+
'"session.resume"',
|
|
35606
|
+
'"session.context_changed"',
|
|
35607
|
+
'"session.usage_record"'
|
|
35608
|
+
];
|
|
35609
|
+
function githubRemote(workspace) {
|
|
35610
|
+
const { hostType, repository, repositoryHost = "github.com" } = workspace;
|
|
35611
|
+
return hostType === "github" && repository !== undefined && isOwnerAndName(repository) && /^[a-z0-9.-]+(?::\d+)?$/i.test(repositoryHost) ? RepositoryReference.Remote({ value: `https://${repositoryHost}/${repository}` }) : null;
|
|
35612
|
+
}
|
|
35613
|
+
function isSessionEvents(file2) {
|
|
35614
|
+
return insideSession(file2) && file2.split(/[\\/]/).at(-1) === "events.jsonl";
|
|
35615
|
+
}
|
|
35616
|
+
function copilotSessionParser(file2, chatgptRequests) {
|
|
35617
|
+
let session;
|
|
35618
|
+
let cwd = null;
|
|
35619
|
+
let repository = null;
|
|
35620
|
+
const settle = (workspace) => {
|
|
35621
|
+
if (workspace?.pendingGitContext === true)
|
|
35622
|
+
return;
|
|
35623
|
+
cwd = workspace?.cwd || null;
|
|
35624
|
+
repository = workspace === undefined ? null : githubRemote(workspace);
|
|
35625
|
+
};
|
|
35626
|
+
return {
|
|
35627
|
+
parse(line) {
|
|
35628
|
+
if (!accounting.some((type4) => line.includes(type4)))
|
|
35629
|
+
return empty14;
|
|
35630
|
+
const raw2 = exports_Option.getOrNull(decodeJson(line));
|
|
35631
|
+
const type3 = exports_Option.getOrNull(decodeHeader(raw2))?.type;
|
|
35632
|
+
if (type3 === "session.start") {
|
|
35633
|
+
const event2 = exports_Option.getOrNull(decodeStart(raw2));
|
|
35634
|
+
if (event2 === null)
|
|
35635
|
+
return empty14;
|
|
35636
|
+
session = event2.data.sessionId;
|
|
35637
|
+
settle(event2.data.context);
|
|
35638
|
+
return empty14;
|
|
35639
|
+
}
|
|
35640
|
+
if (type3 === "session.context_changed") {
|
|
35641
|
+
settle(exports_Option.getOrNull(decodeChange(raw2))?.data ?? undefined);
|
|
35642
|
+
return empty14;
|
|
35643
|
+
}
|
|
35644
|
+
if (type3 === "session.resume") {
|
|
35645
|
+
const context3 = exports_Option.getOrNull(decodeResume(raw2))?.data.context;
|
|
35646
|
+
if (context3 !== undefined)
|
|
35647
|
+
settle(context3);
|
|
35648
|
+
return empty14;
|
|
35649
|
+
}
|
|
35650
|
+
if (type3 !== "session.usage_record")
|
|
35651
|
+
return empty14;
|
|
35652
|
+
const event = exports_Option.getOrNull(decodeUsage(raw2));
|
|
35653
|
+
if (event === null)
|
|
35654
|
+
return skipped;
|
|
35655
|
+
const usage = event.data.usage;
|
|
35656
|
+
const usageId = usage.accounting?.usageId;
|
|
35657
|
+
const cacheRead = usage.cacheReadTokens ?? 0;
|
|
35658
|
+
const cacheWrite = usage.cacheWriteTokens ?? 0;
|
|
35659
|
+
const tokens = {
|
|
35660
|
+
input: usage.inputTokens - cacheRead - cacheWrite,
|
|
35661
|
+
output: usage.outputTokens,
|
|
35662
|
+
cacheRead,
|
|
35663
|
+
cacheWrite
|
|
35664
|
+
};
|
|
35665
|
+
const id2 = usage.apiCallId !== undefined ? requestId(usage.apiCallId, tokens) : usageId === undefined ? undefined : `usage:${usageId}`;
|
|
35666
|
+
const routed = usage.apiCallId === undefined ? undefined : chatgptRequests?.get(usage.apiCallId);
|
|
35667
|
+
if (usage.isByok === true && chatgptRequests !== undefined && routed === undefined)
|
|
35668
|
+
return id2 === undefined ? empty14 : { ...empty14, excluded: [id2] };
|
|
35669
|
+
const ms = timestamp(event.timestamp);
|
|
35670
|
+
if (ms === null || id2 === undefined || cacheRead + cacheWrite > usage.inputTokens)
|
|
35671
|
+
return skipped;
|
|
35672
|
+
const record3 = {
|
|
35673
|
+
provider: "copilot",
|
|
35674
|
+
id: id2,
|
|
35675
|
+
session: usage.accounting?.sourceSessionId ?? session ?? file2,
|
|
35676
|
+
timestamp: ms,
|
|
35677
|
+
model: usage.model,
|
|
35678
|
+
cwd,
|
|
35679
|
+
repository: repository ?? (routed === undefined || routed.repository === null ? null : RepositoryReference.Remote({ value: routed.repository })),
|
|
35680
|
+
tokens
|
|
35681
|
+
};
|
|
35682
|
+
return {
|
|
35683
|
+
...empty14,
|
|
35684
|
+
records: [routed === undefined ? record3 : { ...record3, billingProvider: "codex" }]
|
|
35685
|
+
};
|
|
35686
|
+
}
|
|
35687
|
+
};
|
|
35688
|
+
}
|
|
35689
|
+
|
|
35540
35690
|
// packages/core/src/providers/grok.ts
|
|
35541
35691
|
var Counts2 = exports_Schema.Struct({
|
|
35542
35692
|
inputTokens: exports_Schema.Natural,
|
|
@@ -35816,7 +35966,7 @@ function opencodeHistory(nativePath) {
|
|
|
35816
35966
|
|
|
35817
35967
|
// packages/core/src/providers/pi.ts
|
|
35818
35968
|
var decodeKind = exports_Schema.decodeUnknownOption(exports_Schema.Struct({ type: exports_Schema.String }));
|
|
35819
|
-
var
|
|
35969
|
+
var decodeHeader2 = exports_Schema.decodeUnknownOption(exports_Schema.Struct({
|
|
35820
35970
|
id: exports_Schema.optionalKey(exports_Schema.String),
|
|
35821
35971
|
timestamp: exports_Schema.optionalKey(exports_Schema.String),
|
|
35822
35972
|
cwd: exports_Schema.optionalKey(exports_Schema.String),
|
|
@@ -35856,7 +36006,7 @@ function piParser(file2) {
|
|
|
35856
36006
|
if (type3 === "session") {
|
|
35857
36007
|
if (sawHeader)
|
|
35858
36008
|
return empty14;
|
|
35859
|
-
const header = exports_Option.getOrNull(
|
|
36009
|
+
const header = exports_Option.getOrNull(decodeHeader2(raw2));
|
|
35860
36010
|
if (header === null)
|
|
35861
36011
|
return malformed;
|
|
35862
36012
|
sawHeader = true;
|
|
@@ -36164,6 +36314,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36164
36314
|
const scan2 = exports_Effect.fn("Ledger.scan")(function* (request3, range3, locate, collect2, notice, onWarning) {
|
|
36165
36315
|
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);
|
|
36166
36316
|
const chatgptRequests = exports_Result.isSuccess(hints) ? hints.success : new Map;
|
|
36317
|
+
const routing = notice !== undefined && request3.billingHints !== undefined && exports_Result.isSuccess(hints) ? chatgptRequests : undefined;
|
|
36167
36318
|
if (exports_Result.isFailure(hints))
|
|
36168
36319
|
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;
|
|
36169
36320
|
const resolveProject = yield* projectResolver(request3.projects).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path));
|
|
@@ -36173,6 +36324,8 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36173
36324
|
const coverage = [];
|
|
36174
36325
|
const inherited = [];
|
|
36175
36326
|
const deferred = [];
|
|
36327
|
+
const copilotCopies = [];
|
|
36328
|
+
const copilotExcluded = new Set;
|
|
36176
36329
|
const opencode = opencodeHistory((value5) => path.normalize(value5));
|
|
36177
36330
|
for (const source of request3.sources) {
|
|
36178
36331
|
let files = 0;
|
|
@@ -36193,18 +36346,24 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36193
36346
|
seen.add(id2);
|
|
36194
36347
|
collect2(record3, yield* resolveProject(record3, source.project), slot);
|
|
36195
36348
|
});
|
|
36196
|
-
const accept2 = exports_Effect.fn("Ledger.acceptRecords")(function* (result6) {
|
|
36349
|
+
const accept2 = exports_Effect.fn("Ledger.acceptRecords")(function* (result6, fromSession = false) {
|
|
36197
36350
|
malformedLines += result6.malformed;
|
|
36198
36351
|
skippedRecords += result6.skipped;
|
|
36199
36352
|
for (const warning of result6.warnings)
|
|
36200
36353
|
warnings.add(warning);
|
|
36354
|
+
for (const id2 of result6.excluded)
|
|
36355
|
+
copilotExcluded.add(id2);
|
|
36201
36356
|
for (const hint of result6.hints) {
|
|
36202
36357
|
if (locate(hint.timestamp) !== undefined)
|
|
36203
36358
|
notice?.({ provider: source.provider, ...hint });
|
|
36204
36359
|
}
|
|
36205
36360
|
for (const record3 of result6.records) {
|
|
36206
36361
|
const slot = locate(record3.timestamp);
|
|
36207
|
-
if (slot
|
|
36362
|
+
if (slot === undefined || totalTokens(record3.tokens) === 0)
|
|
36363
|
+
continue;
|
|
36364
|
+
if (source.provider === "copilot")
|
|
36365
|
+
copilotCopies.push({ record: record3, slot, fromSession, place });
|
|
36366
|
+
else
|
|
36208
36367
|
yield* place(record3, slot);
|
|
36209
36368
|
}
|
|
36210
36369
|
for (const record3 of result6.inherited) {
|
|
@@ -36237,7 +36396,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36237
36396
|
return;
|
|
36238
36397
|
}
|
|
36239
36398
|
visitedFiles.add(identity3);
|
|
36240
|
-
const parser = exports_Match.value(source.provider).pipe(exports_Match.when("codex", () => codexParser(canonical)), exports_Match.when("claude", () => claudeParser(canonical)), exports_Match.when("grok", () => grokParser(canonical)), exports_Match.when("copilot", () => copilot ?? copilotParser(canonical)), exports_Match.when("pi", () => piParser(canonical)), exports_Match.when("opencode", () => opencode.parser(opencodeFile(path, canonical))), exports_Match.exhaustive);
|
|
36399
|
+
const parser = exports_Match.value(source.provider).pipe(exports_Match.when("codex", () => codexParser(canonical)), exports_Match.when("claude", () => claudeParser(canonical)), exports_Match.when("grok", () => grokParser(canonical)), exports_Match.when("copilot", () => isSessionEvents(file2) ? copilotSessionParser(canonical, routing) : copilot ?? copilotParser(canonical)), exports_Match.when("pi", () => piParser(canonical)), exports_Match.when("opencode", () => opencode.parser(opencodeFile(path, canonical))), exports_Match.exhaustive);
|
|
36241
36400
|
let lineNumber = 0;
|
|
36242
36401
|
files++;
|
|
36243
36402
|
const lines = source.provider === "copilot" && canonical.endsWith(".db") ? exports_Stream.fromIterableEffect(copilotDatabaseLines(canonical)) : source.provider === "opencode" ? opencodeFile(path, canonical) === "database" ? exports_Stream.fromIterableEffect(opencodeDatabaseLines(canonical, range3.start, range3.end)) : exports_Stream.fromEffect(fs.readFileString(canonical)) : fs.stream(canonical).pipe(exports_Stream.decodeText, exports_Stream.splitLines);
|
|
@@ -36245,9 +36404,9 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36245
36404
|
lineNumber++;
|
|
36246
36405
|
if (!line.trim())
|
|
36247
36406
|
return;
|
|
36248
|
-
yield* accept2(parser.parse(line, lineNumber));
|
|
36407
|
+
yield* accept2(parser.parse(line, lineNumber), parser !== copilot);
|
|
36249
36408
|
})));
|
|
36250
|
-
if (
|
|
36409
|
+
if (parser !== copilot && parser.finish !== undefined)
|
|
36251
36410
|
yield* accept2(parser.finish());
|
|
36252
36411
|
});
|
|
36253
36412
|
const pending = [source.path];
|
|
@@ -36301,6 +36460,21 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36301
36460
|
warnings: [...warnings]
|
|
36302
36461
|
}));
|
|
36303
36462
|
}
|
|
36463
|
+
const requests = new Map;
|
|
36464
|
+
for (const copy4 of copilotCopies)
|
|
36465
|
+
requests.set(copy4.record.id, [...requests.get(copy4.record.id) ?? [], copy4]);
|
|
36466
|
+
for (const [id2, copies] of requests) {
|
|
36467
|
+
const primary = copies.find((copy4) => copy4.fromSession) ?? copies[0];
|
|
36468
|
+
if (primary === undefined)
|
|
36469
|
+
continue;
|
|
36470
|
+
if (copilotExcluded.has(id2))
|
|
36471
|
+
seen.add(`copilot:${id2}`);
|
|
36472
|
+
const repository = primary.record.repository ?? copies.find((copy4) => copy4.record.repository !== null)?.record.repository ?? null;
|
|
36473
|
+
yield* primary.place({ ...primary.record, repository }, primary.slot);
|
|
36474
|
+
for (const copy4 of copies)
|
|
36475
|
+
if (copy4 !== primary)
|
|
36476
|
+
yield* copy4.place(copy4.record, copy4.slot);
|
|
36477
|
+
}
|
|
36304
36478
|
for (const legacy of deferred)
|
|
36305
36479
|
yield* legacy;
|
|
36306
36480
|
for (const copy4 of inherited)
|
|
@@ -36425,7 +36599,9 @@ function acceptsFile(path, source, file2) {
|
|
|
36425
36599
|
return file2.endsWith("/updates.jsonl") || file2.endsWith("\\updates.jsonl");
|
|
36426
36600
|
if (source.provider === "opencode")
|
|
36427
36601
|
return opencodeFile(path, file2) !== "database" || opencodeDatabaseName.test(path.basename(file2));
|
|
36428
|
-
|
|
36602
|
+
if (source.provider === "copilot")
|
|
36603
|
+
return insideSession(file2) ? isSessionEvents(file2) : file2.endsWith(".jsonl") || file2.endsWith(".db");
|
|
36604
|
+
return file2.endsWith(".jsonl");
|
|
36429
36605
|
}
|
|
36430
36606
|
function opencodeFile(path, file2) {
|
|
36431
36607
|
const name = path.basename(file2);
|
|
@@ -36447,10 +36623,14 @@ var opencodeElsewhere = new Set([
|
|
|
36447
36623
|
"tool-output"
|
|
36448
36624
|
]);
|
|
36449
36625
|
function descends(path, source, directory) {
|
|
36450
|
-
|
|
36626
|
+
if (directory === source.path)
|
|
36627
|
+
return true;
|
|
36628
|
+
if (source.provider === "copilot")
|
|
36629
|
+
return !insideSession(directory);
|
|
36630
|
+
return source.provider !== "opencode" || !opencodeElsewhere.has(path.basename(directory));
|
|
36451
36631
|
}
|
|
36452
36632
|
function settledBefore(path, source, file2, info2, cutoff) {
|
|
36453
|
-
if (source.path === file2 || source.provider === "copilot" || source.provider === "opencode" && opencodeFile(path, file2) !== "message")
|
|
36633
|
+
if (source.path === file2 || source.provider === "copilot" && !isSessionEvents(file2) || source.provider === "opencode" && opencodeFile(path, file2) !== "message")
|
|
36454
36634
|
return false;
|
|
36455
36635
|
return exports_Option.isSome(info2.mtime) && info2.mtime.value.getTime() < cutoff;
|
|
36456
36636
|
}
|
|
@@ -43753,7 +43933,7 @@ var runWith2 = (command, config) => {
|
|
|
43753
43933
|
// packages/cli/package.json
|
|
43754
43934
|
var package_default = {
|
|
43755
43935
|
name: "@ponbac/token-ledger",
|
|
43756
|
-
version: "0.
|
|
43936
|
+
version: "0.11.0",
|
|
43757
43937
|
private: true,
|
|
43758
43938
|
type: "module",
|
|
43759
43939
|
bin: {
|
|
@@ -47177,6 +47357,7 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
|
|
|
47177
47357
|
const grokHome = yield* exports_Config.String("GROK_HOME").pipe(exports_Config.withDefault(path.join(home, ".grok")));
|
|
47178
47358
|
const copilotHome = yield* exports_Config.String("COPILOT_HOME").pipe(exports_Config.withDefault(path.join(home, ".copilot")));
|
|
47179
47359
|
const copilotFile = yield* exports_Config.String("COPILOT_OTEL_FILE_EXPORTER_PATH").pipe(exports_Config.option);
|
|
47360
|
+
const copilotSessions = path.join(expand(copilotHome), "session-state");
|
|
47180
47361
|
const piHome = yield* exports_Config.String("PI_CODING_AGENT_DIR").pipe(exports_Config.withDefault(path.join(home, ".pi", "agent")));
|
|
47181
47362
|
const piSessions = yield* exports_Config.String("PI_CODING_AGENT_SESSION_DIR").pipe(exports_Config.option);
|
|
47182
47363
|
const opencodeData = path.join(exports_Option.getOrElse(yield* dataHome, () => path.join(home, ".local", "share")), "opencode");
|
|
@@ -47186,6 +47367,7 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
|
|
|
47186
47367
|
{ provider: "codex", path: path.join(expand(codexHome), "archived_sessions") },
|
|
47187
47368
|
{ provider: "claude", path: path.join(expand(claudeHome), "projects") },
|
|
47188
47369
|
{ provider: "grok", path: path.join(expand(grokHome), "sessions") },
|
|
47370
|
+
...(yield* fs.exists(copilotSessions)) ? [{ provider: "copilot", path: copilotSessions }] : [],
|
|
47189
47371
|
...(yield* copilotSources(path.join(expand(copilotHome), "otel"), exports_Option.map(copilotFile, (value5) => expand(value5)))).map((source) => ({ provider: "copilot", path: source })),
|
|
47190
47372
|
{
|
|
47191
47373
|
provider: "pi",
|