@ponbac/token-ledger 0.10.0 → 0.11.1
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 +46 -7
- package/dist/main.js +321 -44
- 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
|
|
@@ -424,6 +447,22 @@ their common Git directory. Relative config paths resolve from the config file;
|
|
|
424
447
|
`~/` resolves from the current developer's home. Directory mappings respect path
|
|
425
448
|
boundaries, so `/work/app` never matches `/work/application`.
|
|
426
449
|
|
|
450
|
+
The CLI remembers network repository identities in its local application data directory
|
|
451
|
+
(`$XDG_DATA_HOME/token-ledger/repositories`, normally
|
|
452
|
+
`~/.local/share/token-ledger/repositories`; on Windows under `%LOCALAPPDATA%`, and on
|
|
453
|
+
macOS under `~/Library/Application Support`). Once a scan has saved a checkout or
|
|
454
|
+
worktree's network origin, later scans keep its repository even after that directory is deleted.
|
|
455
|
+
The nearest live or remembered repository wins, and explicit mappings take precedence; an existing directory
|
|
456
|
+
without Git is never assigned from an old identity. Each directory is keyed by a
|
|
457
|
+
hash, and saved identities omit credentials. Observing a local or absent origin
|
|
458
|
+
invalidates its former network identity, including after the directory is deleted.
|
|
459
|
+
The same applies to a remembered root observed to exist without Git metadata.
|
|
460
|
+
A history read or write failure stops
|
|
461
|
+
a real upload rather than replacing known attribution with missing metadata.
|
|
462
|
+
Local reports and dry runs warn about history failures and continue; they also
|
|
463
|
+
remember identities when the history directory is writable.
|
|
464
|
+
This history cannot recover directories deleted before the first scan with it.
|
|
465
|
+
|
|
427
466
|
Grok logs currently lack project metadata in the usage parser. Copilot CLI can put
|
|
428
467
|
repository metadata on a parent agent span rather than the counted request span;
|
|
429
468
|
Token Ledger associates the two. If an export has no repository metadata, use a
|
|
@@ -923,7 +962,7 @@ For manual publication:
|
|
|
923
962
|
1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
|
|
924
963
|
2. Run `bun run check`. The CLI test packs the package, installs it offline outside
|
|
925
964
|
the repository with lifecycle scripts disabled, and exercises the installed CLI.
|
|
926
|
-
3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.
|
|
965
|
+
3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.11.1.tgz` (filename follows version).
|
|
927
966
|
4. Inspect `npm publish ./dist/npm --dry-run`.
|
|
928
967
|
5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
|
|
929
968
|
prompt if requested.
|
package/dist/main.js
CHANGED
|
@@ -32791,6 +32791,10 @@ var Day = exports_Schema.String.check(exports_Schema.isPattern(/^\d{4}-\d{2}-\d{
|
|
|
32791
32791
|
return Number.isFinite(date2.getTime()) && date2.toISOString().slice(0, 10) === s;
|
|
32792
32792
|
}));
|
|
32793
32793
|
var ReportRequest = exports_Schema.Struct({
|
|
32794
|
+
repositoryHistory: exports_Schema.optionalKey(exports_Schema.Struct({
|
|
32795
|
+
directory: exports_Schema.NonEmptyString,
|
|
32796
|
+
required: exports_Schema.Boolean
|
|
32797
|
+
})),
|
|
32794
32798
|
billingHints: exports_Schema.optionalKey(exports_Schema.Struct({ directory: exports_Schema.NonEmptyString, required: exports_Schema.Boolean })),
|
|
32795
32799
|
since: Day,
|
|
32796
32800
|
until: Day,
|
|
@@ -35011,6 +35015,63 @@ function priceRecord(record3, vendor) {
|
|
|
35011
35015
|
return { usd, base };
|
|
35012
35016
|
}
|
|
35013
35017
|
|
|
35018
|
+
// packages/core/src/repository-history.ts
|
|
35019
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
35020
|
+
class RepositoryHistoryError extends exports_Schema.TaggedError()("RepositoryHistoryError", { message: exports_Schema.String }) {
|
|
35021
|
+
}
|
|
35022
|
+
var Entry = exports_Schema.Struct({
|
|
35023
|
+
version: exports_Schema.Literal(1),
|
|
35024
|
+
repository: exports_Schema.NullOr(exports_Schema.NonEmptyString.check(exports_Schema.isPattern(/^[^/\s@]+\/[^\r\n]+$/u)))
|
|
35025
|
+
});
|
|
35026
|
+
var repositoryHistory = exports_Effect.fn("RepositoryHistory.open")(function* (config, onWarning) {
|
|
35027
|
+
const fs = yield* exports_FileSystem.FileSystem;
|
|
35028
|
+
const path = yield* exports_Path.Path;
|
|
35029
|
+
const pending = new Map;
|
|
35030
|
+
const loaded = new Map;
|
|
35031
|
+
const directory = config?.directory;
|
|
35032
|
+
const filename = (root) => `${createHash3("sha256").update(path.resolve(root)).digest("hex")}.json`;
|
|
35033
|
+
const recover = (error2) => config?.required ? exports_Effect.fail(error2) : onWarning?.(error2.message) ?? exports_Effect.void;
|
|
35034
|
+
const read2 = exports_Effect.fn("RepositoryHistory.read")(function* (root) {
|
|
35035
|
+
if (directory === undefined)
|
|
35036
|
+
return null;
|
|
35037
|
+
const key = filename(root);
|
|
35038
|
+
const observed = pending.get(key);
|
|
35039
|
+
if (observed !== undefined)
|
|
35040
|
+
return observed;
|
|
35041
|
+
const cached3 = loaded.get(key);
|
|
35042
|
+
if (cached3 !== undefined)
|
|
35043
|
+
return cached3;
|
|
35044
|
+
const file2 = path.join(directory, key);
|
|
35045
|
+
const saved = yield* fs.readFileString(file2).pipe(exports_Effect.catchTag("PlatformError", (error2) => exports_Predicate.isTagged(error2.reason, "NotFound") ? exports_Effect.succeed(null) : exports_Effect.fail(error2)), exports_Effect.flatMap((text2) => text2 === null ? exports_Effect.succeed(null) : exports_Schema.decodeUnknownEffect(exports_Schema.fromJsonString(Entry))(text2)), exports_Effect.mapError(() => new RepositoryHistoryError({
|
|
35046
|
+
message: `Cannot read repository history at ${file2}. Fix its permissions or repair its contents before syncing. If it is damaged beyond repair, remove only this file; attribution for that deleted directory will then be unknown until restored.`
|
|
35047
|
+
})), exports_Effect.catchTag("RepositoryHistoryError", (error2) => recover(error2).pipe(exports_Effect.as(null))));
|
|
35048
|
+
loaded.set(key, saved);
|
|
35049
|
+
return saved;
|
|
35050
|
+
});
|
|
35051
|
+
const remember = (root, repository) => {
|
|
35052
|
+
const entry = exports_Schema.decodeUnknownOption(Entry)({ version: 1, repository });
|
|
35053
|
+
if (directory !== undefined && exports_Option.isSome(entry))
|
|
35054
|
+
pending.set(filename(root), entry.value);
|
|
35055
|
+
};
|
|
35056
|
+
const flush = exports_Effect.fn("RepositoryHistory.save")(function* () {
|
|
35057
|
+
if (directory === undefined || pending.size === 0)
|
|
35058
|
+
return;
|
|
35059
|
+
yield* exports_Effect.gen(function* () {
|
|
35060
|
+
yield* fs.makeDirectory(directory, { recursive: true, mode: 448 });
|
|
35061
|
+
for (const [key, entry] of pending) {
|
|
35062
|
+
const temporary = yield* fs.makeTempFileScoped({ directory, prefix: ".repository-" });
|
|
35063
|
+
yield* fs.writeFileString(temporary, JSON.stringify(entry) + `
|
|
35064
|
+
`);
|
|
35065
|
+
yield* fs.chmod(temporary, 384);
|
|
35066
|
+
yield* fs.rename(temporary, path.join(directory, key));
|
|
35067
|
+
}
|
|
35068
|
+
}).pipe(exports_Effect.scoped, exports_Effect.mapError(() => new RepositoryHistoryError({
|
|
35069
|
+
message: `Cannot save repository history in ${directory}.${config?.required ? " Sync stopped to preserve attribution." : ""}`
|
|
35070
|
+
})), exports_Effect.catchTag("RepositoryHistoryError", recover));
|
|
35071
|
+
});
|
|
35072
|
+
return { read: read2, remember, flush };
|
|
35073
|
+
});
|
|
35074
|
+
|
|
35014
35075
|
// packages/core/src/projects.ts
|
|
35015
35076
|
var scpRemote = /^(?:[^/@\s]+@)?(\[[^\]\s]+\]|[^/:\\\s]+):((?!\/\/).+)$/;
|
|
35016
35077
|
function repositoryIdentity(remote) {
|
|
@@ -35029,9 +35090,10 @@ function isNetworkRemote(remote) {
|
|
|
35029
35090
|
const parsed = URL.parse(value5);
|
|
35030
35091
|
return parsed !== null && parsed.host !== "" ? ["http:", "https:", "ssh:", "git:", "ftp:", "ftps:"].includes(parsed.protocol) : scpRemote.test(value5);
|
|
35031
35092
|
}
|
|
35032
|
-
var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings) {
|
|
35093
|
+
var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings, historyConfig, onWarning) {
|
|
35033
35094
|
const fs = yield* exports_FileSystem.FileSystem;
|
|
35034
35095
|
const path = yield* exports_Path.Path;
|
|
35096
|
+
const history = yield* repositoryHistory(historyConfig, onWarning);
|
|
35035
35097
|
const resolved = new Map;
|
|
35036
35098
|
const readOptional = (file2) => fs.readFileString(file2).pipe(exports_Effect.option);
|
|
35037
35099
|
const discover = exports_Effect.fn("Projects.discover")(function* (cwd) {
|
|
@@ -35040,14 +35102,14 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
|
|
|
35040
35102
|
return cached3;
|
|
35041
35103
|
let current = cwd;
|
|
35042
35104
|
let root = cwd;
|
|
35043
|
-
let
|
|
35105
|
+
let repository = null;
|
|
35044
35106
|
while (true) {
|
|
35045
35107
|
const git = path.join(current, ".git");
|
|
35046
|
-
const stat3 = yield* fs.stat(git).pipe(exports_Effect.
|
|
35047
|
-
if (
|
|
35108
|
+
const stat3 = yield* fs.stat(git).pipe(exports_Effect.result);
|
|
35109
|
+
if (exports_Result.isSuccess(stat3)) {
|
|
35048
35110
|
root = current;
|
|
35049
35111
|
let gitDirectory = git;
|
|
35050
|
-
if (stat3.
|
|
35112
|
+
if (stat3.success.type === "File") {
|
|
35051
35113
|
const pointer = yield* readOptional(git);
|
|
35052
35114
|
const match10 = /^gitdir:\s*(.+)\s*$/m.exec(exports_Option.getOrElse(pointer, () => ""));
|
|
35053
35115
|
if (match10?.[1])
|
|
@@ -35061,26 +35123,40 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
|
|
|
35061
35123
|
const config = yield* readOptional(path.join(gitDirectory, "config"));
|
|
35062
35124
|
const origin = /\[remote "origin"\]([^[]*)/.exec(exports_Option.getOrElse(config, () => ""));
|
|
35063
35125
|
const url = /^\s*url\s*=\s*(.+)$/m.exec(origin?.[1] ?? "");
|
|
35064
|
-
if (url?.[1])
|
|
35065
|
-
remote = url[1].trim();
|
|
35126
|
+
if (url?.[1]) {
|
|
35127
|
+
const remote = url[1].trim();
|
|
35128
|
+
repository = RepositoryReference.Remote({ value: remote });
|
|
35129
|
+
}
|
|
35130
|
+
if (exports_Option.isSome(config))
|
|
35131
|
+
history.remember(current, url?.[1] && isNetworkRemote(url[1].trim()) ? repositoryIdentity(url[1].trim()) : null);
|
|
35066
35132
|
break;
|
|
35067
35133
|
}
|
|
35134
|
+
if (historyConfig !== undefined && exports_Predicate.isTagged(stat3.failure.reason, "NotFound")) {
|
|
35135
|
+
const exists2 = yield* fs.exists(current).pipe(exports_Effect.option);
|
|
35136
|
+
if (exports_Option.isSome(exists2)) {
|
|
35137
|
+
const previous = yield* history.read(current);
|
|
35138
|
+
if (previous !== null && !exists2.value) {
|
|
35139
|
+
root = current;
|
|
35140
|
+
repository = previous.repository === null ? null : RepositoryReference.Identity({ value: previous.repository });
|
|
35141
|
+
break;
|
|
35142
|
+
}
|
|
35143
|
+
if (previous !== null && previous.repository !== null)
|
|
35144
|
+
history.remember(current, null);
|
|
35145
|
+
}
|
|
35146
|
+
}
|
|
35068
35147
|
const parent = path.dirname(current);
|
|
35069
35148
|
if (parent === current)
|
|
35070
35149
|
break;
|
|
35071
35150
|
current = parent;
|
|
35072
35151
|
}
|
|
35073
|
-
const identity3 = { root,
|
|
35152
|
+
const identity3 = { root, repository };
|
|
35074
35153
|
resolved.set(cwd, identity3);
|
|
35075
35154
|
return identity3;
|
|
35076
35155
|
});
|
|
35077
|
-
|
|
35156
|
+
const resolve7 = exports_Effect.fn("Projects.resolve")(function* (record3, fixedProject) {
|
|
35078
35157
|
if (fixedProject !== undefined)
|
|
35079
35158
|
return { project: fixedProject, shareable: true };
|
|
35080
35159
|
const cwd = record3.cwd;
|
|
35081
|
-
const identity3 = cwd === null ? null : yield* discover(cwd);
|
|
35082
|
-
const rawRemote = record3.repository?.value ?? identity3?.remote ?? null;
|
|
35083
|
-
const remote = rawRemote === null ? null : repositoryIdentity(rawRemote);
|
|
35084
35160
|
let bestPath = null;
|
|
35085
35161
|
for (const mapping of mappings) {
|
|
35086
35162
|
for (const prefix3 of mapping.paths) {
|
|
@@ -35092,17 +35168,21 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
|
|
|
35092
35168
|
}
|
|
35093
35169
|
if (bestPath !== null)
|
|
35094
35170
|
return { project: bestPath.project, shareable: true };
|
|
35171
|
+
const identity3 = record3.repository !== null || cwd === null ? null : yield* discover(cwd);
|
|
35172
|
+
const reference = record3.repository ?? identity3?.repository ?? null;
|
|
35173
|
+
const remote = reference === null ? null : exports_Predicate.isTagged(reference, "Identity") ? reference.value : repositoryIdentity(reference.value);
|
|
35095
35174
|
for (const mapping of mappings) {
|
|
35096
35175
|
if (remote !== null && mapping.repositories.some((entry) => repositoryIdentity(entry) === remote))
|
|
35097
35176
|
return { project: mapping.project, shareable: true };
|
|
35098
35177
|
}
|
|
35099
|
-
if (
|
|
35178
|
+
if (reference !== null && remote !== null)
|
|
35100
35179
|
return {
|
|
35101
35180
|
project: remote,
|
|
35102
|
-
shareable: remote !== "" && (
|
|
35181
|
+
shareable: remote !== "" && (exports_Predicate.isTagged(reference, "Identity") || isNetworkRemote(reference.value))
|
|
35103
35182
|
};
|
|
35104
35183
|
return { project: identity3?.root ?? "Unassigned", shareable: false };
|
|
35105
35184
|
});
|
|
35185
|
+
return { resolve: resolve7, flush: history.flush };
|
|
35106
35186
|
});
|
|
35107
35187
|
|
|
35108
35188
|
// packages/core/src/providers/shared.ts
|
|
@@ -35111,6 +35191,7 @@ var empty14 = {
|
|
|
35111
35191
|
inherited: [],
|
|
35112
35192
|
deferred: [],
|
|
35113
35193
|
hints: [],
|
|
35194
|
+
excluded: [],
|
|
35114
35195
|
malformed: 0,
|
|
35115
35196
|
skipped: 0,
|
|
35116
35197
|
warnings: []
|
|
@@ -35334,6 +35415,7 @@ var CopilotAttributes = exports_Schema.Struct({
|
|
|
35334
35415
|
"gen_ai.usage.cache_creation.input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35335
35416
|
"gen_ai.usage.cache_read_input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35336
35417
|
"gen_ai.usage.cache_creation_input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35418
|
+
"gen_ai.usage.cache_write.input_tokens": exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35337
35419
|
"github.copilot.git.repository": exports_Schema.optionalKey(exports_Schema.String),
|
|
35338
35420
|
"copilot_chat.repo.remote_url": exports_Schema.optionalKey(exports_Schema.String),
|
|
35339
35421
|
"session.id": exports_Schema.optionalKey(exports_Schema.String)
|
|
@@ -35366,10 +35448,12 @@ var Header = exports_Schema.Struct({
|
|
|
35366
35448
|
"gen_ai.operation.name": exports_Schema.optionalKey(exports_Schema.String)
|
|
35367
35449
|
})
|
|
35368
35450
|
});
|
|
35451
|
+
var requestId = (responseId, tokens) => `response:${responseId}#${tokens.input + tokens.cacheRead + tokens.cacheWrite}:${tokens.output}`;
|
|
35452
|
+
var isOwnerAndName = (repository) => /^[A-Za-z0-9-]+\/(?!\.{1,2}$)[A-Za-z0-9_.-]+$/.test(repository);
|
|
35369
35453
|
function repositoryReference(attributes) {
|
|
35370
35454
|
const repository = attributes["github.copilot.git.repository"];
|
|
35371
35455
|
if (repository !== undefined)
|
|
35372
|
-
return
|
|
35456
|
+
return isOwnerAndName(repository) ? RepositoryReference.Identity({ value: repository }) : RepositoryReference.Remote({ value: repository });
|
|
35373
35457
|
const remote = attributes["copilot_chat.repo.remote_url"];
|
|
35374
35458
|
return remote === undefined ? null : RepositoryReference.Remote({ value: remote });
|
|
35375
35459
|
}
|
|
@@ -35410,7 +35494,7 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35410
35494
|
if (input === undefined || output === undefined)
|
|
35411
35495
|
return skipped;
|
|
35412
35496
|
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;
|
|
35497
|
+
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
35498
|
if (cacheRead + cacheWrite > input)
|
|
35415
35499
|
return skipped;
|
|
35416
35500
|
const ms = span2.endTime[0] * 1000 + Math.floor(span2.endTime[1] / 1e6);
|
|
@@ -35439,12 +35523,16 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35439
35523
|
for (const { spanKey, responseId, isSpan } of candidates)
|
|
35440
35524
|
if (!isSpan && spanKey !== undefined && responseId !== undefined)
|
|
35441
35525
|
eventResponses.set(spanKey, (eventResponses.get(spanKey) ?? new Set).add(responseId));
|
|
35442
|
-
const
|
|
35526
|
+
const responses = candidates.map((candidate) => {
|
|
35443
35527
|
const shared = candidate.isSpan && candidate.responseId === undefined && candidate.spanKey !== undefined ? eventResponses.get(candidate.spanKey) : undefined;
|
|
35444
|
-
|
|
35528
|
+
return shared?.size === 1 ? [...shared][0] : candidate.responseId;
|
|
35529
|
+
});
|
|
35530
|
+
const identified = candidates.map((candidate, index2) => {
|
|
35531
|
+
const responseId = responses[index2];
|
|
35445
35532
|
if (responseId === undefined)
|
|
35446
35533
|
return candidate;
|
|
35447
|
-
const
|
|
35534
|
+
const id2 = requestId(responseId, candidate.record.tokens);
|
|
35535
|
+
const record3 = { ...candidate.record, id: id2 };
|
|
35448
35536
|
const routed = chatgptRequests.get(responseId);
|
|
35449
35537
|
if (routed === undefined)
|
|
35450
35538
|
return { ...candidate, responseId, record: record3 };
|
|
@@ -35455,8 +35543,7 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35455
35543
|
hinted: routed.repository === null ? null : RepositoryReference.Remote({ value: routed.repository })
|
|
35456
35544
|
};
|
|
35457
35545
|
});
|
|
35458
|
-
const
|
|
35459
|
-
for (const candidate of identified) {
|
|
35546
|
+
const inheritedRepository = (candidate) => {
|
|
35460
35547
|
let repository = candidate.record.repository;
|
|
35461
35548
|
let key = candidate.spanKey;
|
|
35462
35549
|
const visited = new Set;
|
|
@@ -35466,6 +35553,11 @@ function copilotParser(file2, chatgptRequests = new Map) {
|
|
|
35466
35553
|
repository = ancestor?.repository ?? null;
|
|
35467
35554
|
key = ancestor?.parent;
|
|
35468
35555
|
}
|
|
35556
|
+
return repository;
|
|
35557
|
+
};
|
|
35558
|
+
const preferred = new Map;
|
|
35559
|
+
for (const candidate of identified) {
|
|
35560
|
+
const repository = inheritedRepository(candidate);
|
|
35469
35561
|
const resolved = { ...candidate, record: { ...candidate.record, repository } };
|
|
35470
35562
|
const previous = preferred.get(resolved.record.id);
|
|
35471
35563
|
if (previous === undefined)
|
|
@@ -35537,6 +35629,144 @@ var copilotDatabaseLines = exports_Effect.fn("Copilot.readDatabase")(function* (
|
|
|
35537
35629
|
}).pipe(exports_Effect.scoped);
|
|
35538
35630
|
});
|
|
35539
35631
|
|
|
35632
|
+
// packages/core/src/providers/copilot-session.ts
|
|
35633
|
+
var Workspace = exports_Schema.Struct({
|
|
35634
|
+
cwd: exports_Schema.optionalKey(exports_Schema.String),
|
|
35635
|
+
repository: exports_Schema.optionalKey(exports_Schema.String),
|
|
35636
|
+
repositoryHost: exports_Schema.optionalKey(exports_Schema.String),
|
|
35637
|
+
hostType: exports_Schema.optionalKey(exports_Schema.String),
|
|
35638
|
+
pendingGitContext: exports_Schema.optionalKey(exports_Schema.Boolean)
|
|
35639
|
+
});
|
|
35640
|
+
var SessionStart = exports_Schema.Struct({
|
|
35641
|
+
type: exports_Schema.Literal("session.start"),
|
|
35642
|
+
data: exports_Schema.Struct({
|
|
35643
|
+
sessionId: exports_Schema.NonEmptyString,
|
|
35644
|
+
context: exports_Schema.optionalKey(Workspace)
|
|
35645
|
+
})
|
|
35646
|
+
});
|
|
35647
|
+
var ContextChanged = exports_Schema.Struct({
|
|
35648
|
+
type: exports_Schema.Literal("session.context_changed"),
|
|
35649
|
+
data: Workspace
|
|
35650
|
+
});
|
|
35651
|
+
var Resumed = exports_Schema.Struct({
|
|
35652
|
+
type: exports_Schema.Literal("session.resume"),
|
|
35653
|
+
data: exports_Schema.Struct({ context: exports_Schema.optionalKey(Workspace) })
|
|
35654
|
+
});
|
|
35655
|
+
var UsageEvent = exports_Schema.Struct({
|
|
35656
|
+
type: exports_Schema.Literal("session.usage_record"),
|
|
35657
|
+
timestamp: exports_Schema.String,
|
|
35658
|
+
data: exports_Schema.Struct({
|
|
35659
|
+
usage: exports_Schema.Struct({
|
|
35660
|
+
model: exports_Schema.NonEmptyString,
|
|
35661
|
+
inputTokens: exports_Schema.Natural,
|
|
35662
|
+
outputTokens: exports_Schema.Natural,
|
|
35663
|
+
cacheReadTokens: exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35664
|
+
cacheWriteTokens: exports_Schema.optionalKey(exports_Schema.Natural),
|
|
35665
|
+
isByok: exports_Schema.optionalKey(exports_Schema.Boolean),
|
|
35666
|
+
apiCallId: exports_Schema.optionalKey(exports_Schema.NonEmptyString),
|
|
35667
|
+
accounting: exports_Schema.optionalKey(exports_Schema.Struct({
|
|
35668
|
+
sourceSessionId: exports_Schema.optionalKey(exports_Schema.NonEmptyString),
|
|
35669
|
+
usageId: exports_Schema.optionalKey(exports_Schema.NonEmptyString)
|
|
35670
|
+
}))
|
|
35671
|
+
})
|
|
35672
|
+
})
|
|
35673
|
+
});
|
|
35674
|
+
var Header2 = exports_Schema.Struct({ type: exports_Schema.String });
|
|
35675
|
+
var decodeHeader = exports_Schema.decodeUnknownOption(Header2);
|
|
35676
|
+
var decodeStart = exports_Schema.decodeUnknownOption(SessionStart);
|
|
35677
|
+
var decodeChange = exports_Schema.decodeUnknownOption(ContextChanged);
|
|
35678
|
+
var decodeResume = exports_Schema.decodeUnknownOption(Resumed);
|
|
35679
|
+
var decodeUsage = exports_Schema.decodeUnknownOption(UsageEvent);
|
|
35680
|
+
function insideSession(entry) {
|
|
35681
|
+
return entry.split(/[\\/]/).at(-3) === "session-state";
|
|
35682
|
+
}
|
|
35683
|
+
var accounting = [
|
|
35684
|
+
'"session.start"',
|
|
35685
|
+
'"session.resume"',
|
|
35686
|
+
'"session.context_changed"',
|
|
35687
|
+
'"session.usage_record"'
|
|
35688
|
+
];
|
|
35689
|
+
function githubRemote(workspace) {
|
|
35690
|
+
const { hostType, repository, repositoryHost = "github.com" } = workspace;
|
|
35691
|
+
return hostType === "github" && repository !== undefined && isOwnerAndName(repository) && /^[a-z0-9.-]+(?::\d+)?$/i.test(repositoryHost) ? RepositoryReference.Remote({ value: `https://${repositoryHost}/${repository}` }) : null;
|
|
35692
|
+
}
|
|
35693
|
+
function isSessionEvents(file2) {
|
|
35694
|
+
return insideSession(file2) && file2.split(/[\\/]/).at(-1) === "events.jsonl";
|
|
35695
|
+
}
|
|
35696
|
+
function copilotSessionParser(file2, chatgptRequests) {
|
|
35697
|
+
let session;
|
|
35698
|
+
let cwd = null;
|
|
35699
|
+
let repository = null;
|
|
35700
|
+
const settle = (workspace) => {
|
|
35701
|
+
if (workspace?.pendingGitContext === true)
|
|
35702
|
+
return;
|
|
35703
|
+
cwd = workspace?.cwd || null;
|
|
35704
|
+
repository = workspace === undefined ? null : githubRemote(workspace);
|
|
35705
|
+
};
|
|
35706
|
+
return {
|
|
35707
|
+
parse(line) {
|
|
35708
|
+
if (!accounting.some((type4) => line.includes(type4)))
|
|
35709
|
+
return empty14;
|
|
35710
|
+
const raw2 = exports_Option.getOrNull(decodeJson(line));
|
|
35711
|
+
const type3 = exports_Option.getOrNull(decodeHeader(raw2))?.type;
|
|
35712
|
+
if (type3 === "session.start") {
|
|
35713
|
+
const event2 = exports_Option.getOrNull(decodeStart(raw2));
|
|
35714
|
+
if (event2 === null)
|
|
35715
|
+
return empty14;
|
|
35716
|
+
session = event2.data.sessionId;
|
|
35717
|
+
settle(event2.data.context);
|
|
35718
|
+
return empty14;
|
|
35719
|
+
}
|
|
35720
|
+
if (type3 === "session.context_changed") {
|
|
35721
|
+
settle(exports_Option.getOrNull(decodeChange(raw2))?.data ?? undefined);
|
|
35722
|
+
return empty14;
|
|
35723
|
+
}
|
|
35724
|
+
if (type3 === "session.resume") {
|
|
35725
|
+
const context3 = exports_Option.getOrNull(decodeResume(raw2))?.data.context;
|
|
35726
|
+
if (context3 !== undefined)
|
|
35727
|
+
settle(context3);
|
|
35728
|
+
return empty14;
|
|
35729
|
+
}
|
|
35730
|
+
if (type3 !== "session.usage_record")
|
|
35731
|
+
return empty14;
|
|
35732
|
+
const event = exports_Option.getOrNull(decodeUsage(raw2));
|
|
35733
|
+
if (event === null)
|
|
35734
|
+
return skipped;
|
|
35735
|
+
const usage = event.data.usage;
|
|
35736
|
+
const usageId = usage.accounting?.usageId;
|
|
35737
|
+
const cacheRead = usage.cacheReadTokens ?? 0;
|
|
35738
|
+
const cacheWrite = usage.cacheWriteTokens ?? 0;
|
|
35739
|
+
const tokens = {
|
|
35740
|
+
input: usage.inputTokens - cacheRead - cacheWrite,
|
|
35741
|
+
output: usage.outputTokens,
|
|
35742
|
+
cacheRead,
|
|
35743
|
+
cacheWrite
|
|
35744
|
+
};
|
|
35745
|
+
const id2 = usage.apiCallId !== undefined ? requestId(usage.apiCallId, tokens) : usageId === undefined ? undefined : `usage:${usageId}`;
|
|
35746
|
+
const routed = usage.apiCallId === undefined ? undefined : chatgptRequests?.get(usage.apiCallId);
|
|
35747
|
+
if (usage.isByok === true && chatgptRequests !== undefined && routed === undefined)
|
|
35748
|
+
return id2 === undefined ? empty14 : { ...empty14, excluded: [id2] };
|
|
35749
|
+
const ms = timestamp(event.timestamp);
|
|
35750
|
+
if (ms === null || id2 === undefined || cacheRead + cacheWrite > usage.inputTokens)
|
|
35751
|
+
return skipped;
|
|
35752
|
+
const record3 = {
|
|
35753
|
+
provider: "copilot",
|
|
35754
|
+
id: id2,
|
|
35755
|
+
session: usage.accounting?.sourceSessionId ?? session ?? file2,
|
|
35756
|
+
timestamp: ms,
|
|
35757
|
+
model: usage.model,
|
|
35758
|
+
cwd,
|
|
35759
|
+
repository: repository ?? (routed === undefined || routed.repository === null ? null : RepositoryReference.Remote({ value: routed.repository })),
|
|
35760
|
+
tokens
|
|
35761
|
+
};
|
|
35762
|
+
return {
|
|
35763
|
+
...empty14,
|
|
35764
|
+
records: [routed === undefined ? record3 : { ...record3, billingProvider: "codex" }]
|
|
35765
|
+
};
|
|
35766
|
+
}
|
|
35767
|
+
};
|
|
35768
|
+
}
|
|
35769
|
+
|
|
35540
35770
|
// packages/core/src/providers/grok.ts
|
|
35541
35771
|
var Counts2 = exports_Schema.Struct({
|
|
35542
35772
|
inputTokens: exports_Schema.Natural,
|
|
@@ -35816,7 +36046,7 @@ function opencodeHistory(nativePath) {
|
|
|
35816
36046
|
|
|
35817
36047
|
// packages/core/src/providers/pi.ts
|
|
35818
36048
|
var decodeKind = exports_Schema.decodeUnknownOption(exports_Schema.Struct({ type: exports_Schema.String }));
|
|
35819
|
-
var
|
|
36049
|
+
var decodeHeader2 = exports_Schema.decodeUnknownOption(exports_Schema.Struct({
|
|
35820
36050
|
id: exports_Schema.optionalKey(exports_Schema.String),
|
|
35821
36051
|
timestamp: exports_Schema.optionalKey(exports_Schema.String),
|
|
35822
36052
|
cwd: exports_Schema.optionalKey(exports_Schema.String),
|
|
@@ -35856,7 +36086,7 @@ function piParser(file2) {
|
|
|
35856
36086
|
if (type3 === "session") {
|
|
35857
36087
|
if (sawHeader)
|
|
35858
36088
|
return empty14;
|
|
35859
|
-
const header = exports_Option.getOrNull(
|
|
36089
|
+
const header = exports_Option.getOrNull(decodeHeader2(raw2));
|
|
35860
36090
|
if (header === null)
|
|
35861
36091
|
return malformed;
|
|
35862
36092
|
sawHeader = true;
|
|
@@ -35914,7 +36144,7 @@ function piParser(file2) {
|
|
|
35914
36144
|
}
|
|
35915
36145
|
|
|
35916
36146
|
// packages/core/src/sync.ts
|
|
35917
|
-
import { createHash as
|
|
36147
|
+
import { createHash as createHash4 } from "node:crypto";
|
|
35918
36148
|
var hour = 3600000;
|
|
35919
36149
|
var UtcInstant = exports_Schema.String.check(exports_Schema.isPattern(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/), exports_Schema.makeFilter((s) => {
|
|
35920
36150
|
const ms = Date.parse(s);
|
|
@@ -36055,7 +36285,7 @@ function hourRange(range3) {
|
|
|
36055
36285
|
};
|
|
36056
36286
|
}
|
|
36057
36287
|
function sessionKey(provider, session) {
|
|
36058
|
-
return
|
|
36288
|
+
return createHash4("sha256").update(`${provider}\x00${session}`).digest("hex").slice(0, 32);
|
|
36059
36289
|
}
|
|
36060
36290
|
function sessionBuckets(pricing) {
|
|
36061
36291
|
const totals = new Map;
|
|
@@ -36154,9 +36384,9 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36154
36384
|
const request3 = yield* exports_Schema.decodeUnknownEffect(ReportRequest)(input).pipe(exports_Effect.mapError(() => new InvalidRequest({
|
|
36155
36385
|
message: "Expected an ordered inclusive day window, a named time zone, and valid sources, mappings, and prices."
|
|
36156
36386
|
})));
|
|
36157
|
-
if (request3.billingHints !== undefined && !path.isAbsolute(request3.billingHints.directory) || request3.sources.some((source) => !path.isAbsolute(source.path)) || request3.projects.some((mapping) => mapping.paths.some((prefix3) => !path.isAbsolute(prefix3)))) {
|
|
36387
|
+
if (request3.repositoryHistory !== undefined && !path.isAbsolute(request3.repositoryHistory.directory) || request3.billingHints !== undefined && !path.isAbsolute(request3.billingHints.directory) || request3.sources.some((source) => !path.isAbsolute(source.path)) || request3.projects.some((mapping) => mapping.paths.some((prefix3) => !path.isAbsolute(prefix3)))) {
|
|
36158
36388
|
return yield* new InvalidRequest({
|
|
36159
|
-
message: "Source, billing hint, and project mapping paths must be absolute."
|
|
36389
|
+
message: "Source, repository history, billing hint, and project mapping paths must be absolute."
|
|
36160
36390
|
});
|
|
36161
36391
|
}
|
|
36162
36392
|
return request3;
|
|
@@ -36164,15 +36394,18 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36164
36394
|
const scan2 = exports_Effect.fn("Ledger.scan")(function* (request3, range3, locate, collect2, notice, onWarning) {
|
|
36165
36395
|
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
36396
|
const chatgptRequests = exports_Result.isSuccess(hints) ? hints.success : new Map;
|
|
36397
|
+
const routing = notice !== undefined && request3.billingHints !== undefined && exports_Result.isSuccess(hints) ? chatgptRequests : undefined;
|
|
36167
36398
|
if (exports_Result.isFailure(hints))
|
|
36168
36399
|
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
|
-
const
|
|
36400
|
+
const projects = yield* projectResolver(request3.projects, request3.repositoryHistory, onWarning).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path));
|
|
36170
36401
|
const seen = new Set;
|
|
36171
36402
|
const visitedFiles = new Set;
|
|
36172
36403
|
const settledFiles = new Set;
|
|
36173
36404
|
const coverage = [];
|
|
36174
36405
|
const inherited = [];
|
|
36175
36406
|
const deferred = [];
|
|
36407
|
+
const copilotCopies = [];
|
|
36408
|
+
const copilotExcluded = new Set;
|
|
36176
36409
|
const opencode = opencodeHistory((value5) => path.normalize(value5));
|
|
36177
36410
|
for (const source of request3.sources) {
|
|
36178
36411
|
let files = 0;
|
|
@@ -36191,20 +36424,26 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36191
36424
|
return;
|
|
36192
36425
|
}
|
|
36193
36426
|
seen.add(id2);
|
|
36194
|
-
collect2(record3, yield*
|
|
36427
|
+
collect2(record3, yield* projects.resolve(record3, source.project), slot);
|
|
36195
36428
|
});
|
|
36196
|
-
const accept2 = exports_Effect.fn("Ledger.acceptRecords")(function* (result6) {
|
|
36429
|
+
const accept2 = exports_Effect.fn("Ledger.acceptRecords")(function* (result6, fromSession = false) {
|
|
36197
36430
|
malformedLines += result6.malformed;
|
|
36198
36431
|
skippedRecords += result6.skipped;
|
|
36199
36432
|
for (const warning of result6.warnings)
|
|
36200
36433
|
warnings.add(warning);
|
|
36434
|
+
for (const id2 of result6.excluded)
|
|
36435
|
+
copilotExcluded.add(id2);
|
|
36201
36436
|
for (const hint of result6.hints) {
|
|
36202
36437
|
if (locate(hint.timestamp) !== undefined)
|
|
36203
36438
|
notice?.({ provider: source.provider, ...hint });
|
|
36204
36439
|
}
|
|
36205
36440
|
for (const record3 of result6.records) {
|
|
36206
36441
|
const slot = locate(record3.timestamp);
|
|
36207
|
-
if (slot
|
|
36442
|
+
if (slot === undefined || totalTokens(record3.tokens) === 0)
|
|
36443
|
+
continue;
|
|
36444
|
+
if (source.provider === "copilot")
|
|
36445
|
+
copilotCopies.push({ record: record3, slot, fromSession, place });
|
|
36446
|
+
else
|
|
36208
36447
|
yield* place(record3, slot);
|
|
36209
36448
|
}
|
|
36210
36449
|
for (const record3 of result6.inherited) {
|
|
@@ -36237,7 +36476,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36237
36476
|
return;
|
|
36238
36477
|
}
|
|
36239
36478
|
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);
|
|
36479
|
+
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
36480
|
let lineNumber = 0;
|
|
36242
36481
|
files++;
|
|
36243
36482
|
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 +36484,9 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36245
36484
|
lineNumber++;
|
|
36246
36485
|
if (!line.trim())
|
|
36247
36486
|
return;
|
|
36248
|
-
yield* accept2(parser.parse(line, lineNumber));
|
|
36487
|
+
yield* accept2(parser.parse(line, lineNumber), parser !== copilot);
|
|
36249
36488
|
})));
|
|
36250
|
-
if (
|
|
36489
|
+
if (parser !== copilot && parser.finish !== undefined)
|
|
36251
36490
|
yield* accept2(parser.finish());
|
|
36252
36491
|
});
|
|
36253
36492
|
const pending = [source.path];
|
|
@@ -36273,6 +36512,8 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36273
36512
|
}
|
|
36274
36513
|
}).pipe(exports_Effect.result);
|
|
36275
36514
|
if (exports_Result.isFailure(result6)) {
|
|
36515
|
+
if (exports_Predicate.isTagged(result6.failure, "RepositoryHistoryError"))
|
|
36516
|
+
return yield* result6.failure;
|
|
36276
36517
|
if (entry === source.path && exports_Predicate.isTagged(result6.failure, "PlatformError") && exports_Predicate.isTagged(result6.failure.reason, "NotFound"))
|
|
36277
36518
|
missingRoot = true;
|
|
36278
36519
|
failedFiles++;
|
|
@@ -36301,14 +36542,34 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36301
36542
|
warnings: [...warnings]
|
|
36302
36543
|
}));
|
|
36303
36544
|
}
|
|
36545
|
+
const requests = new Map;
|
|
36546
|
+
for (const copy4 of copilotCopies)
|
|
36547
|
+
requests.set(copy4.record.id, [...requests.get(copy4.record.id) ?? [], copy4]);
|
|
36548
|
+
for (const [id2, copies] of requests) {
|
|
36549
|
+
const primary = copies.find((copy4) => copy4.fromSession) ?? copies[0];
|
|
36550
|
+
if (primary === undefined)
|
|
36551
|
+
continue;
|
|
36552
|
+
if (copilotExcluded.has(id2))
|
|
36553
|
+
seen.add(`copilot:${id2}`);
|
|
36554
|
+
const repository = primary.record.repository ?? copies.find((copy4) => copy4.record.repository !== null)?.record.repository ?? null;
|
|
36555
|
+
yield* primary.place({ ...primary.record, repository }, primary.slot);
|
|
36556
|
+
for (const copy4 of copies)
|
|
36557
|
+
if (copy4 !== primary)
|
|
36558
|
+
yield* copy4.place(copy4.record, copy4.slot);
|
|
36559
|
+
}
|
|
36304
36560
|
for (const legacy of deferred)
|
|
36305
36561
|
yield* legacy;
|
|
36306
36562
|
for (const copy4 of inherited)
|
|
36307
36563
|
yield* copy4;
|
|
36564
|
+
yield* projects.flush();
|
|
36308
36565
|
return coverage.map((summarize) => summarize());
|
|
36309
36566
|
});
|
|
36310
|
-
const report = exports_Effect.fn("Ledger.report")(function* (input) {
|
|
36311
|
-
const
|
|
36567
|
+
const report = exports_Effect.fn("Ledger.report")(function* (input, onWarning) {
|
|
36568
|
+
const decoded = yield* decode5(input);
|
|
36569
|
+
const request3 = decoded.repositoryHistory === undefined ? decoded : {
|
|
36570
|
+
...decoded,
|
|
36571
|
+
repositoryHistory: { ...decoded.repositoryHistory, required: false }
|
|
36572
|
+
};
|
|
36312
36573
|
const window = dayWindow(request3.since, request3.until, request3.timeZone);
|
|
36313
36574
|
const rows = new Map;
|
|
36314
36575
|
const coverage = yield* scan2(request3, window, window.dayOf, (record3, { project }, day) => {
|
|
@@ -36322,7 +36583,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
|
|
|
36322
36583
|
pricePerMillion: base,
|
|
36323
36584
|
...addUsage(rows.get(key), record3.tokens, usd)
|
|
36324
36585
|
});
|
|
36325
|
-
});
|
|
36586
|
+
}, undefined, onWarning);
|
|
36326
36587
|
return UsageReport.make({
|
|
36327
36588
|
version: 2,
|
|
36328
36589
|
since: request3.since,
|
|
@@ -36425,7 +36686,9 @@ function acceptsFile(path, source, file2) {
|
|
|
36425
36686
|
return file2.endsWith("/updates.jsonl") || file2.endsWith("\\updates.jsonl");
|
|
36426
36687
|
if (source.provider === "opencode")
|
|
36427
36688
|
return opencodeFile(path, file2) !== "database" || opencodeDatabaseName.test(path.basename(file2));
|
|
36428
|
-
|
|
36689
|
+
if (source.provider === "copilot")
|
|
36690
|
+
return insideSession(file2) ? isSessionEvents(file2) : file2.endsWith(".jsonl") || file2.endsWith(".db");
|
|
36691
|
+
return file2.endsWith(".jsonl");
|
|
36429
36692
|
}
|
|
36430
36693
|
function opencodeFile(path, file2) {
|
|
36431
36694
|
const name = path.basename(file2);
|
|
@@ -36447,10 +36710,14 @@ var opencodeElsewhere = new Set([
|
|
|
36447
36710
|
"tool-output"
|
|
36448
36711
|
]);
|
|
36449
36712
|
function descends(path, source, directory) {
|
|
36450
|
-
|
|
36713
|
+
if (directory === source.path)
|
|
36714
|
+
return true;
|
|
36715
|
+
if (source.provider === "copilot")
|
|
36716
|
+
return !insideSession(directory);
|
|
36717
|
+
return source.provider !== "opencode" || !opencodeElsewhere.has(path.basename(directory));
|
|
36451
36718
|
}
|
|
36452
36719
|
function settledBefore(path, source, file2, info2, cutoff) {
|
|
36453
|
-
if (source.path === file2 || source.provider === "copilot" || source.provider === "opencode" && opencodeFile(path, file2) !== "message")
|
|
36720
|
+
if (source.path === file2 || source.provider === "copilot" && !isSessionEvents(file2) || source.provider === "opencode" && opencodeFile(path, file2) !== "message")
|
|
36454
36721
|
return false;
|
|
36455
36722
|
return exports_Option.isSome(info2.mtime) && info2.mtime.value.getTime() < cutoff;
|
|
36456
36723
|
}
|
|
@@ -43753,7 +44020,7 @@ var runWith2 = (command, config) => {
|
|
|
43753
44020
|
// packages/cli/package.json
|
|
43754
44021
|
var package_default = {
|
|
43755
44022
|
name: "@ponbac/token-ledger",
|
|
43756
|
-
version: "0.
|
|
44023
|
+
version: "0.11.1",
|
|
43757
44024
|
private: true,
|
|
43758
44025
|
type: "module",
|
|
43759
44026
|
bin: {
|
|
@@ -43789,10 +44056,14 @@ var dataHome = exports_Effect.gen(function* () {
|
|
|
43789
44056
|
const path = yield* exports_Path.Path;
|
|
43790
44057
|
return yield* exports_Config.String("XDG_DATA_HOME").pipe(exports_Config.option, exports_Effect.map(exports_Option.filter((directory) => path.isAbsolute(directory))));
|
|
43791
44058
|
});
|
|
43792
|
-
function
|
|
44059
|
+
function tokenLedgerDataDirectory(platform, home, localAppData, xdgDataHome) {
|
|
43793
44060
|
const path = platform === "win32" ? win322 : posix2;
|
|
43794
44061
|
const root = exports_Match.value(platform).pipe(exports_Match.when("win32", () => exports_Option.getOrElse(exports_Option.filter(localAppData, (directory) => path.isAbsolute(directory)), () => path.join(home, "AppData", "Local"))), exports_Match.when("darwin", () => path.join(home, "Library", "Application Support")), exports_Match.orElse(() => exports_Option.getOrElse(xdgDataHome, () => path.join(home, ".local", "share"))));
|
|
43795
|
-
return path.join(root, "token-ledger"
|
|
44062
|
+
return path.join(root, "token-ledger");
|
|
44063
|
+
}
|
|
44064
|
+
function billingHintsDirectory(platform, home, localAppData, xdgDataHome) {
|
|
44065
|
+
const path = platform === "win32" ? win322 : posix2;
|
|
44066
|
+
return path.join(tokenLedgerDataDirectory(platform, home, localAppData, xdgDataHome), "billing-hints");
|
|
43796
44067
|
}
|
|
43797
44068
|
// node_modules/.bun/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
|
|
43798
44069
|
function createScanner(text2, ignoreTrivia = false) {
|
|
@@ -47177,6 +47448,7 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
|
|
|
47177
47448
|
const grokHome = yield* exports_Config.String("GROK_HOME").pipe(exports_Config.withDefault(path.join(home, ".grok")));
|
|
47178
47449
|
const copilotHome = yield* exports_Config.String("COPILOT_HOME").pipe(exports_Config.withDefault(path.join(home, ".copilot")));
|
|
47179
47450
|
const copilotFile = yield* exports_Config.String("COPILOT_OTEL_FILE_EXPORTER_PATH").pipe(exports_Config.option);
|
|
47451
|
+
const copilotSessions = path.join(expand(copilotHome), "session-state");
|
|
47180
47452
|
const piHome = yield* exports_Config.String("PI_CODING_AGENT_DIR").pipe(exports_Config.withDefault(path.join(home, ".pi", "agent")));
|
|
47181
47453
|
const piSessions = yield* exports_Config.String("PI_CODING_AGENT_SESSION_DIR").pipe(exports_Config.option);
|
|
47182
47454
|
const opencodeData = path.join(exports_Option.getOrElse(yield* dataHome, () => path.join(home, ".local", "share")), "opencode");
|
|
@@ -47186,6 +47458,7 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
|
|
|
47186
47458
|
{ provider: "codex", path: path.join(expand(codexHome), "archived_sessions") },
|
|
47187
47459
|
{ provider: "claude", path: path.join(expand(claudeHome), "projects") },
|
|
47188
47460
|
{ provider: "grok", path: path.join(expand(grokHome), "sessions") },
|
|
47461
|
+
...(yield* fs.exists(copilotSessions)) ? [{ provider: "copilot", path: copilotSessions }] : [],
|
|
47189
47462
|
...(yield* copilotSources(path.join(expand(copilotHome), "otel"), exports_Option.map(copilotFile, (value5) => expand(value5)))).map((source) => ({ provider: "copilot", path: source })),
|
|
47190
47463
|
{
|
|
47191
47464
|
provider: "pi",
|
|
@@ -47231,6 +47504,10 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
|
|
|
47231
47504
|
if (pricing.status === "unavailable")
|
|
47232
47505
|
yield* exports_Console.error("Pricing unavailable: unknown rates remain unpriced. Configure prices or rerun online.");
|
|
47233
47506
|
const request3 = yield* ReportRequest.makeEffect({
|
|
47507
|
+
repositoryHistory: {
|
|
47508
|
+
directory: path.join(tokenLedgerDataDirectory(process.platform, home, yield* exports_Config.String("LOCALAPPDATA").pipe(exports_Config.option), yield* dataHome), "repositories"),
|
|
47509
|
+
required: !flags.dryRun
|
|
47510
|
+
},
|
|
47234
47511
|
billingHints: configuration.billingHintsDirectory === undefined ? {
|
|
47235
47512
|
directory: billingHintsDirectory(process.platform, home, yield* exports_Config.String("LOCALAPPDATA").pipe(exports_Config.option), yield* dataHome),
|
|
47236
47513
|
required: false
|
|
@@ -47255,7 +47532,7 @@ var report = exports_Command.make("report", {
|
|
|
47255
47532
|
}, exports_Effect.fn("CLI.report")(function* (flags) {
|
|
47256
47533
|
const { request: request3 } = yield* usageRequest(flags, (today) => `${today.slice(0, 7)}-01`);
|
|
47257
47534
|
const ledger = yield* Ledger;
|
|
47258
|
-
const result6 = yield* ledger.report(request3);
|
|
47535
|
+
const result6 = yield* ledger.report(request3, exports_Console.error);
|
|
47259
47536
|
const output = exports_Match.value(flags.json ? "json" : flags.format).pipe(exports_Match.when("json", () => JSON.stringify(result6, null, 2)), exports_Match.when("csv", () => csv(result6)), exports_Match.when("table", () => table2(result6, process.stdout.columns ?? 120, process.stdout.isTTY && process.stdout.hasColors())), exports_Match.exhaustive);
|
|
47260
47537
|
yield* exports_Console.log(output);
|
|
47261
47538
|
for (const source of result6.coverage) {
|