agent-usage-all-in-one 0.6.0 → 0.6.2
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/dist/cli.js +254 -160
- package/dist/cli.js.map +1 -1
- package/dist/web/_app/immutable/chunks/{BfTUk1IE.js → BnorHpdS.js} +1 -1
- package/dist/web/_app/immutable/entry/{app.Cr-3S01G.js → app.CKvzgqLm.js} +2 -2
- package/dist/web/_app/immutable/entry/start.CFE_FdYs.js +1 -0
- package/dist/web/_app/immutable/nodes/{1.BYavEZ8T.js → 1.hmq3VhPk.js} +1 -1
- package/dist/web/_app/immutable/nodes/{2.ehiOyFDQ.js → 2.DJI_veG-.js} +5 -5
- package/dist/web/_app/version.json +1 -1
- package/dist/web/index.html +6 -6
- package/package.json +1 -1
- package/dist/web/_app/immutable/entry/start.BiLBur5x.js +0 -1
package/dist/cli.js
CHANGED
|
@@ -4390,6 +4390,186 @@ var init_opencode_go_connector = __esm({
|
|
|
4390
4390
|
}
|
|
4391
4391
|
});
|
|
4392
4392
|
|
|
4393
|
+
// src/connectors/grok-build/grok-build-connector.ts
|
|
4394
|
+
import { z as z6 } from "zod";
|
|
4395
|
+
function incompleteTranscriptFailure3() {
|
|
4396
|
+
return {
|
|
4397
|
+
code: "local-transcript-scan-incomplete",
|
|
4398
|
+
message: "Some local Grok history could not be read.",
|
|
4399
|
+
recovery: "Agent Usage will retry automatically without removing stored history."
|
|
4400
|
+
};
|
|
4401
|
+
}
|
|
4402
|
+
function grokBuildBillingDomain() {
|
|
4403
|
+
return {
|
|
4404
|
+
id: "grok-build-subscription",
|
|
4405
|
+
displayName: "Grok Build / SuperGrok shared pool"
|
|
4406
|
+
};
|
|
4407
|
+
}
|
|
4408
|
+
function isGrokOfficialModel(model) {
|
|
4409
|
+
if (!model) return false;
|
|
4410
|
+
const normalized = model.trim().toLowerCase();
|
|
4411
|
+
if (normalized === "grok") return true;
|
|
4412
|
+
if (normalized.startsWith("grok-") || normalized.startsWith("grok/")) {
|
|
4413
|
+
return true;
|
|
4414
|
+
}
|
|
4415
|
+
return false;
|
|
4416
|
+
}
|
|
4417
|
+
function resolveGrokBillingDomain(model, customEndpoints) {
|
|
4418
|
+
if (!model) return "grok-build-subscription";
|
|
4419
|
+
const normalized = model.trim().toLowerCase();
|
|
4420
|
+
if (customEndpoints?.has(normalized)) {
|
|
4421
|
+
return customEndpoints.get(normalized);
|
|
4422
|
+
}
|
|
4423
|
+
if (isGrokOfficialModel(model)) {
|
|
4424
|
+
return "grok-build-subscription";
|
|
4425
|
+
}
|
|
4426
|
+
return "custom";
|
|
4427
|
+
}
|
|
4428
|
+
function grokBuildBillingDomains(usage = []) {
|
|
4429
|
+
const primary = grokBuildBillingDomain();
|
|
4430
|
+
const observed = [...new Set(usage.map((o) => o.billingDomainId))].filter((id) => id !== primary.id).sort();
|
|
4431
|
+
return [
|
|
4432
|
+
primary,
|
|
4433
|
+
...observed.map((id) => ({
|
|
4434
|
+
id,
|
|
4435
|
+
displayName: id === "custom" ? "Custom endpoints" : id
|
|
4436
|
+
}))
|
|
4437
|
+
];
|
|
4438
|
+
}
|
|
4439
|
+
function mapBillingQuota(billing) {
|
|
4440
|
+
const config = billing.config;
|
|
4441
|
+
if (!config) return [];
|
|
4442
|
+
const usedPercent = config.creditUsagePercent ?? derivePercent(config.used?.val, config.monthlyLimit?.val);
|
|
4443
|
+
if (usedPercent === null) return [];
|
|
4444
|
+
const periodType = config.currentPeriod?.type;
|
|
4445
|
+
const period = nativePeriod(periodType, config.currentPeriod ? void 0 : "monthly");
|
|
4446
|
+
const resetsAt = config.currentPeriod?.end ?? config.billingPeriodEnd ?? null;
|
|
4447
|
+
return [
|
|
4448
|
+
{
|
|
4449
|
+
id: `grok-build:${period.id}`,
|
|
4450
|
+
billingDomainId: "grok-build-subscription",
|
|
4451
|
+
label: period.label,
|
|
4452
|
+
usedPercent,
|
|
4453
|
+
windowDurationMinutes: nativeWindowDurationMinutes(
|
|
4454
|
+
config.currentPeriod?.start ?? config.billingPeriodStart,
|
|
4455
|
+
resetsAt,
|
|
4456
|
+
period.id
|
|
4457
|
+
),
|
|
4458
|
+
resetsAt,
|
|
4459
|
+
authority: "official-client",
|
|
4460
|
+
scope: "account-wide",
|
|
4461
|
+
status: billing.subscriptionTier ?? null
|
|
4462
|
+
}
|
|
4463
|
+
];
|
|
4464
|
+
}
|
|
4465
|
+
function nativeWindowDurationMinutes(startsAt, resetsAt, period) {
|
|
4466
|
+
const start = startsAt ? Date.parse(startsAt) : Number.NaN;
|
|
4467
|
+
const end = resetsAt ? Date.parse(resetsAt) : Number.NaN;
|
|
4468
|
+
if (Number.isFinite(start) && Number.isFinite(end) && end > start) {
|
|
4469
|
+
return Math.round((end - start) / 6e4);
|
|
4470
|
+
}
|
|
4471
|
+
if (period === "weekly") return 10080;
|
|
4472
|
+
if (period === "monthly") return 43200;
|
|
4473
|
+
return null;
|
|
4474
|
+
}
|
|
4475
|
+
function nativePeriod(type, fallback) {
|
|
4476
|
+
if (type === "USAGE_PERIOD_TYPE_WEEKLY") return { id: "weekly", label: "Weekly limit" };
|
|
4477
|
+
if (type === "USAGE_PERIOD_TYPE_MONTHLY" || fallback === "monthly") {
|
|
4478
|
+
return { id: "monthly", label: "Monthly limit" };
|
|
4479
|
+
}
|
|
4480
|
+
return { id: "usage", label: "Usage" };
|
|
4481
|
+
}
|
|
4482
|
+
function derivePercent(used, limit) {
|
|
4483
|
+
if (used === void 0 || limit === void 0 || limit <= 0) return null;
|
|
4484
|
+
return Math.min(100, Math.max(0, used / limit * 100));
|
|
4485
|
+
}
|
|
4486
|
+
function safeFailure5(error) {
|
|
4487
|
+
if (error instanceof Error && "code" in error && typeof error.code === "string" && "recovery" in error && typeof error.recovery === "string") {
|
|
4488
|
+
return { code: error.code, message: error.message, recovery: error.recovery };
|
|
4489
|
+
}
|
|
4490
|
+
return {
|
|
4491
|
+
code: "grok-billing-adapter-failed",
|
|
4492
|
+
message: "Grok Build subscription quota is unavailable.",
|
|
4493
|
+
recovery: "Open Grok Build and run /usage, then update Grok Build before retrying."
|
|
4494
|
+
};
|
|
4495
|
+
}
|
|
4496
|
+
var centSchema, usagePeriodSchema, billingConfigSchema, grokBillingResponseSchema, GrokBuildConnector;
|
|
4497
|
+
var init_grok_build_connector = __esm({
|
|
4498
|
+
"src/connectors/grok-build/grok-build-connector.ts"() {
|
|
4499
|
+
"use strict";
|
|
4500
|
+
centSchema = z6.object({ val: z6.number().default(0) }).passthrough();
|
|
4501
|
+
usagePeriodSchema = z6.object({
|
|
4502
|
+
type: z6.string().optional(),
|
|
4503
|
+
start: z6.string().optional(),
|
|
4504
|
+
end: z6.string().optional()
|
|
4505
|
+
}).passthrough();
|
|
4506
|
+
billingConfigSchema = z6.object({
|
|
4507
|
+
creditUsagePercent: z6.number().min(0).max(100).optional(),
|
|
4508
|
+
currentPeriod: usagePeriodSchema.optional(),
|
|
4509
|
+
monthlyLimit: centSchema.optional(),
|
|
4510
|
+
used: centSchema.optional(),
|
|
4511
|
+
billingPeriodStart: z6.string().optional(),
|
|
4512
|
+
billingPeriodEnd: z6.string().optional(),
|
|
4513
|
+
isUnifiedBillingUser: z6.boolean().optional()
|
|
4514
|
+
}).passthrough();
|
|
4515
|
+
grokBillingResponseSchema = z6.object({
|
|
4516
|
+
config: billingConfigSchema.nullable(),
|
|
4517
|
+
onDemandEnabled: z6.boolean().nullable().optional(),
|
|
4518
|
+
subscriptionTier: z6.string().optional(),
|
|
4519
|
+
sourceObservedAt: z6.string().datetime({ offset: true }).optional()
|
|
4520
|
+
}).passthrough();
|
|
4521
|
+
GrokBuildConnector = class {
|
|
4522
|
+
id = "grok";
|
|
4523
|
+
displayName = "Grok";
|
|
4524
|
+
consentId = "grok";
|
|
4525
|
+
#billingClient;
|
|
4526
|
+
#historyClient;
|
|
4527
|
+
#clock;
|
|
4528
|
+
constructor(options) {
|
|
4529
|
+
this.#billingClient = options.billingClient;
|
|
4530
|
+
this.#historyClient = options.historyClient;
|
|
4531
|
+
this.#clock = options.clock ?? (() => /* @__PURE__ */ new Date());
|
|
4532
|
+
}
|
|
4533
|
+
async collect(options = { mode: "incremental" }) {
|
|
4534
|
+
const warnings = [];
|
|
4535
|
+
let quotaBuckets = [];
|
|
4536
|
+
let observedAt = this.#clock().toISOString();
|
|
4537
|
+
try {
|
|
4538
|
+
const billing = await this.#billingClient.readBilling();
|
|
4539
|
+
observedAt = billing.sourceObservedAt ?? observedAt;
|
|
4540
|
+
quotaBuckets = mapBillingQuota(billing);
|
|
4541
|
+
if (quotaBuckets.length === 0) {
|
|
4542
|
+
warnings.push({
|
|
4543
|
+
code: "grok-subscription-quota-unavailable",
|
|
4544
|
+
message: "Grok Build subscription quota is unavailable.",
|
|
4545
|
+
recovery: "Open Grok Build and run /usage, then retry refresh."
|
|
4546
|
+
});
|
|
4547
|
+
}
|
|
4548
|
+
} catch (error) {
|
|
4549
|
+
warnings.push(safeFailure5(error));
|
|
4550
|
+
}
|
|
4551
|
+
const history = this.#historyClient ? await this.#historyClient.readUsage(options) : { usage: [], costs: [], complete: true };
|
|
4552
|
+
if (!history.complete) warnings.push(incompleteTranscriptFailure3());
|
|
4553
|
+
return {
|
|
4554
|
+
provider: { id: this.id, displayName: this.displayName },
|
|
4555
|
+
billingDomains: grokBuildBillingDomains(history.usage),
|
|
4556
|
+
quotaBuckets,
|
|
4557
|
+
usage: history.usage,
|
|
4558
|
+
...history.usage.length > 0 && history.complete ? {
|
|
4559
|
+
usageReconciliation: {
|
|
4560
|
+
authoritativeIdPrefixes: ["grok-transcript:"],
|
|
4561
|
+
retiredIdPrefixes: ["grok-otel:", "grok-headless:"]
|
|
4562
|
+
}
|
|
4563
|
+
} : {},
|
|
4564
|
+
costs: history.costs,
|
|
4565
|
+
warnings,
|
|
4566
|
+
observedAt
|
|
4567
|
+
};
|
|
4568
|
+
}
|
|
4569
|
+
};
|
|
4570
|
+
}
|
|
4571
|
+
});
|
|
4572
|
+
|
|
4393
4573
|
// src/server/zstd-frames.ts
|
|
4394
4574
|
import { zstdDecompressSync } from "zlib";
|
|
4395
4575
|
async function readZstdFramedText(source) {
|
|
@@ -4483,6 +4663,34 @@ import { createReadStream } from "fs";
|
|
|
4483
4663
|
import { mkdir as mkdir2, opendir, readFile as readFile3, rename as rename2, stat, writeFile as writeFile2 } from "fs/promises";
|
|
4484
4664
|
import { dirname, join as join3 } from "path";
|
|
4485
4665
|
import { createInterface } from "readline";
|
|
4666
|
+
function loadGrokConfigCustomModels(configContent) {
|
|
4667
|
+
const customModels = /* @__PURE__ */ new Map();
|
|
4668
|
+
const modelSections = [
|
|
4669
|
+
...configContent.matchAll(/\[model\.(?:"([^"]+)"|([a-zA-Z0-9_-]+))\]([\s\S]*?)(?=\n\[|$)/g)
|
|
4670
|
+
];
|
|
4671
|
+
for (const m of modelSections) {
|
|
4672
|
+
const key = m[1] || m[2];
|
|
4673
|
+
const body = m[3];
|
|
4674
|
+
const modelMatch = body.match(/model\s*=\s*"([^"]+)"/);
|
|
4675
|
+
const baseUrlMatch = body.match(/base_url\s*=\s*"([^"]+)"/);
|
|
4676
|
+
const providerMatch = body.match(/model_provider\s*=\s*"([^"]+)"/);
|
|
4677
|
+
if (baseUrlMatch || providerMatch) {
|
|
4678
|
+
const domain = providerMatch ? providerMatch[1].trim() : "custom";
|
|
4679
|
+
if (key) {
|
|
4680
|
+
customModels.set(key.trim().toLowerCase(), domain);
|
|
4681
|
+
const stripped = key.trim().toLowerCase().replace(/-(high|medium|low)$/, "");
|
|
4682
|
+
if (stripped) customModels.set(stripped, domain);
|
|
4683
|
+
}
|
|
4684
|
+
if (modelMatch) {
|
|
4685
|
+
const mName = modelMatch[1].trim().toLowerCase();
|
|
4686
|
+
customModels.set(mName, domain);
|
|
4687
|
+
const stripped = mName.replace(/-(high|medium|low)$/, "");
|
|
4688
|
+
if (stripped) customModels.set(stripped, domain);
|
|
4689
|
+
}
|
|
4690
|
+
}
|
|
4691
|
+
}
|
|
4692
|
+
return customModels;
|
|
4693
|
+
}
|
|
4486
4694
|
function isCachedTranscriptFile(value) {
|
|
4487
4695
|
const record = asObject(value);
|
|
4488
4696
|
if (!record || !Array.isArray(record.records)) return false;
|
|
@@ -4524,7 +4732,7 @@ function isParsedTranscriptRecord(value) {
|
|
|
4524
4732
|
function nonNegativeSafeInteger(value) {
|
|
4525
4733
|
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
4526
4734
|
}
|
|
4527
|
-
function parseGrokTranscriptLine(line) {
|
|
4735
|
+
function parseGrokTranscriptLine(line, grokCustomModels) {
|
|
4528
4736
|
const record = parseObject(line);
|
|
4529
4737
|
const params = asObject(record?.params);
|
|
4530
4738
|
const update = asObject(params?.update);
|
|
@@ -4568,11 +4776,12 @@ function parseGrokTranscriptLine(line) {
|
|
|
4568
4776
|
const dedupeKey = `${sessionId}:${promptId ?? stableId(line)}:${model}`;
|
|
4569
4777
|
const directCost = grokCostUsd(totals.costUsdTicks);
|
|
4570
4778
|
const reportedCostUsd = directCost ?? (remainingCost !== null && untickedTokens > 0 ? remainingCost * (grokRecordedTokens(totals) / untickedTokens) : null);
|
|
4779
|
+
const billingDomainId = resolveGrokBillingDomain(model, grokCustomModels);
|
|
4571
4780
|
return {
|
|
4572
4781
|
dedupeKey,
|
|
4573
4782
|
observation: {
|
|
4574
4783
|
id: `grok-transcript:${stableId(dedupeKey)}`,
|
|
4575
|
-
billingDomainId
|
|
4784
|
+
billingDomainId,
|
|
4576
4785
|
model,
|
|
4577
4786
|
sessionId,
|
|
4578
4787
|
observedAt,
|
|
@@ -4897,6 +5106,7 @@ var init_local_transcript_usage_client = __esm({
|
|
|
4897
5106
|
"src/server/local-transcript-usage-client.ts"() {
|
|
4898
5107
|
"use strict";
|
|
4899
5108
|
init_token_normalization();
|
|
5109
|
+
init_grok_build_connector();
|
|
4900
5110
|
init_zstd_frames();
|
|
4901
5111
|
DSH_PRIMARY_BILLING_DOMAIN_ID = "deepseek-official";
|
|
4902
5112
|
DSH_SESSION_FORMAT_VERSION = 0;
|
|
@@ -4910,6 +5120,8 @@ var init_local_transcript_usage_client = __esm({
|
|
|
4910
5120
|
#cachePath;
|
|
4911
5121
|
#fileCache = /* @__PURE__ */ new Map();
|
|
4912
5122
|
#cacheLoaded = false;
|
|
5123
|
+
#grokCustomModels = /* @__PURE__ */ new Map();
|
|
5124
|
+
#grokCustomModelsLoaded = false;
|
|
4913
5125
|
constructor(options) {
|
|
4914
5126
|
this.#provider = options.provider;
|
|
4915
5127
|
this.#roots = options.roots;
|
|
@@ -4918,6 +5130,19 @@ var init_local_transcript_usage_client = __esm({
|
|
|
4918
5130
|
this.#cachePath = options.cachePath;
|
|
4919
5131
|
}
|
|
4920
5132
|
async readUsage(options = { mode: "incremental" }) {
|
|
5133
|
+
if (this.#provider === "grok" && !this.#grokCustomModelsLoaded) {
|
|
5134
|
+
this.#grokCustomModelsLoaded = true;
|
|
5135
|
+
for (const root of this.#roots) {
|
|
5136
|
+
try {
|
|
5137
|
+
const configPath = join3(dirname(root), "config.toml");
|
|
5138
|
+
const content = await readFile3(configPath, "utf8");
|
|
5139
|
+
for (const [k, v] of loadGrokConfigCustomModels(content)) {
|
|
5140
|
+
this.#grokCustomModels.set(k, v);
|
|
5141
|
+
}
|
|
5142
|
+
} catch {
|
|
5143
|
+
}
|
|
5144
|
+
}
|
|
5145
|
+
}
|
|
4921
5146
|
await this.#loadCache();
|
|
4922
5147
|
if (options.mode === "hard-rebuild") this.#fileCache.clear();
|
|
4923
5148
|
const cutoff = this.#clock().getTime() - this.#lookbackDays * 24 * 60 * 60 * 1e3;
|
|
@@ -4965,6 +5190,22 @@ var init_local_transcript_usage_client = __esm({
|
|
|
4965
5190
|
const cacheKey = stableId(file.path);
|
|
4966
5191
|
const cached = this.#fileCache.get(cacheKey);
|
|
4967
5192
|
if (cached && cached.size === file.size && cached.mtimeMs === file.mtimeMs) {
|
|
5193
|
+
if (this.#provider === "grok") {
|
|
5194
|
+
const remapped = cached.records.map((r) => {
|
|
5195
|
+
const expectedDomain = resolveGrokBillingDomain(
|
|
5196
|
+
r.observation.model,
|
|
5197
|
+
this.#grokCustomModels
|
|
5198
|
+
);
|
|
5199
|
+
if (r.observation.billingDomainId !== expectedDomain) {
|
|
5200
|
+
return {
|
|
5201
|
+
...r,
|
|
5202
|
+
observation: { ...r.observation, billingDomainId: expectedDomain }
|
|
5203
|
+
};
|
|
5204
|
+
}
|
|
5205
|
+
return r;
|
|
5206
|
+
});
|
|
5207
|
+
return { records: remapped, complete: true, unsupportedFormat: false };
|
|
5208
|
+
}
|
|
4968
5209
|
return { records: cached.records, complete: true, unsupportedFormat: false };
|
|
4969
5210
|
}
|
|
4970
5211
|
const records = [];
|
|
@@ -5008,7 +5249,7 @@ var init_local_transcript_usage_client = __esm({
|
|
|
5008
5249
|
if (this.#provider === "dsh") {
|
|
5009
5250
|
return DSH_LINE_HINTS.some((hint) => line.includes(hint)) ? parseDshTranscriptLine(line, dshState) : [];
|
|
5010
5251
|
}
|
|
5011
|
-
return line.includes('"turn_completed"') ? parseGrokTranscriptLine(line) : [];
|
|
5252
|
+
return line.includes('"turn_completed"') ? parseGrokTranscriptLine(line, this.#grokCustomModels) : [];
|
|
5012
5253
|
}
|
|
5013
5254
|
async #loadCache() {
|
|
5014
5255
|
if (this.#cacheLoaded) return;
|
|
@@ -5063,7 +5304,7 @@ function unsupportedFormatFailure() {
|
|
|
5063
5304
|
recovery: "Update Agent Usage; stored dsh history from earlier scans is retained."
|
|
5064
5305
|
};
|
|
5065
5306
|
}
|
|
5066
|
-
function
|
|
5307
|
+
function safeFailure6(error) {
|
|
5067
5308
|
if (error instanceof Error && "code" in error && typeof error.code === "string" && "recovery" in error && typeof error.recovery === "string") {
|
|
5068
5309
|
return { code: error.code, message: error.message, recovery: error.recovery };
|
|
5069
5310
|
}
|
|
@@ -5095,7 +5336,7 @@ var init_dsh_connector = __esm({
|
|
|
5095
5336
|
try {
|
|
5096
5337
|
history = await this.#historyClient.readUsage(options);
|
|
5097
5338
|
} catch (error) {
|
|
5098
|
-
warnings.push(
|
|
5339
|
+
warnings.push(safeFailure6(error));
|
|
5099
5340
|
}
|
|
5100
5341
|
if (history?.unsupportedFormat) warnings.push(unsupportedFormatFailure());
|
|
5101
5342
|
else if (history && !history.complete) warnings.push(incompleteSessionLogFailure());
|
|
@@ -5344,7 +5585,7 @@ function incompleteSessionScanFailure() {
|
|
|
5344
5585
|
recovery: "Agent Usage will retry automatically on the next scan without removing stored history."
|
|
5345
5586
|
};
|
|
5346
5587
|
}
|
|
5347
|
-
function
|
|
5588
|
+
function safeFailure7() {
|
|
5348
5589
|
return {
|
|
5349
5590
|
code: "antigravity-sqlite-read-failed",
|
|
5350
5591
|
message: "Failed to read local Antigravity conversation stores.",
|
|
@@ -5386,7 +5627,7 @@ var init_antigravity_connector = __esm({
|
|
|
5386
5627
|
try {
|
|
5387
5628
|
history = await this.#historyClient.readUsage(options);
|
|
5388
5629
|
} catch {
|
|
5389
|
-
warnings.push(
|
|
5630
|
+
warnings.push(safeFailure7());
|
|
5390
5631
|
}
|
|
5391
5632
|
if (history && !history.complete) {
|
|
5392
5633
|
warnings.push(incompleteSessionScanFailure());
|
|
@@ -5814,155 +6055,6 @@ var init_antigravity_sqlite_usage_client = __esm({
|
|
|
5814
6055
|
}
|
|
5815
6056
|
});
|
|
5816
6057
|
|
|
5817
|
-
// src/connectors/grok-build/grok-build-connector.ts
|
|
5818
|
-
import { z as z6 } from "zod";
|
|
5819
|
-
function incompleteTranscriptFailure3() {
|
|
5820
|
-
return {
|
|
5821
|
-
code: "local-transcript-scan-incomplete",
|
|
5822
|
-
message: "Some local Grok history could not be read.",
|
|
5823
|
-
recovery: "Agent Usage will retry automatically without removing stored history."
|
|
5824
|
-
};
|
|
5825
|
-
}
|
|
5826
|
-
function grokBuildBillingDomain() {
|
|
5827
|
-
return {
|
|
5828
|
-
id: "grok-build-subscription",
|
|
5829
|
-
displayName: "Grok Build / SuperGrok shared pool"
|
|
5830
|
-
};
|
|
5831
|
-
}
|
|
5832
|
-
function mapBillingQuota(billing) {
|
|
5833
|
-
const config = billing.config;
|
|
5834
|
-
if (!config) return [];
|
|
5835
|
-
const usedPercent = config.creditUsagePercent ?? derivePercent(config.used?.val, config.monthlyLimit?.val);
|
|
5836
|
-
if (usedPercent === null) return [];
|
|
5837
|
-
const periodType = config.currentPeriod?.type;
|
|
5838
|
-
const period = nativePeriod(periodType, config.currentPeriod ? void 0 : "monthly");
|
|
5839
|
-
const resetsAt = config.currentPeriod?.end ?? config.billingPeriodEnd ?? null;
|
|
5840
|
-
return [
|
|
5841
|
-
{
|
|
5842
|
-
id: `grok-build:${period.id}`,
|
|
5843
|
-
billingDomainId: "grok-build-subscription",
|
|
5844
|
-
label: period.label,
|
|
5845
|
-
usedPercent,
|
|
5846
|
-
windowDurationMinutes: nativeWindowDurationMinutes(
|
|
5847
|
-
config.currentPeriod?.start ?? config.billingPeriodStart,
|
|
5848
|
-
resetsAt,
|
|
5849
|
-
period.id
|
|
5850
|
-
),
|
|
5851
|
-
resetsAt,
|
|
5852
|
-
authority: "official-client",
|
|
5853
|
-
scope: "account-wide",
|
|
5854
|
-
status: billing.subscriptionTier ?? null
|
|
5855
|
-
}
|
|
5856
|
-
];
|
|
5857
|
-
}
|
|
5858
|
-
function nativeWindowDurationMinutes(startsAt, resetsAt, period) {
|
|
5859
|
-
const start = startsAt ? Date.parse(startsAt) : Number.NaN;
|
|
5860
|
-
const end = resetsAt ? Date.parse(resetsAt) : Number.NaN;
|
|
5861
|
-
if (Number.isFinite(start) && Number.isFinite(end) && end > start) {
|
|
5862
|
-
return Math.round((end - start) / 6e4);
|
|
5863
|
-
}
|
|
5864
|
-
if (period === "weekly") return 10080;
|
|
5865
|
-
if (period === "monthly") return 43200;
|
|
5866
|
-
return null;
|
|
5867
|
-
}
|
|
5868
|
-
function nativePeriod(type, fallback) {
|
|
5869
|
-
if (type === "USAGE_PERIOD_TYPE_WEEKLY") return { id: "weekly", label: "Weekly limit" };
|
|
5870
|
-
if (type === "USAGE_PERIOD_TYPE_MONTHLY" || fallback === "monthly") {
|
|
5871
|
-
return { id: "monthly", label: "Monthly limit" };
|
|
5872
|
-
}
|
|
5873
|
-
return { id: "usage", label: "Usage" };
|
|
5874
|
-
}
|
|
5875
|
-
function derivePercent(used, limit) {
|
|
5876
|
-
if (used === void 0 || limit === void 0 || limit <= 0) return null;
|
|
5877
|
-
return Math.min(100, Math.max(0, used / limit * 100));
|
|
5878
|
-
}
|
|
5879
|
-
function safeFailure7(error) {
|
|
5880
|
-
if (error instanceof Error && "code" in error && typeof error.code === "string" && "recovery" in error && typeof error.recovery === "string") {
|
|
5881
|
-
return { code: error.code, message: error.message, recovery: error.recovery };
|
|
5882
|
-
}
|
|
5883
|
-
return {
|
|
5884
|
-
code: "grok-billing-adapter-failed",
|
|
5885
|
-
message: "Grok Build subscription quota is unavailable.",
|
|
5886
|
-
recovery: "Open Grok Build and run /usage, then update Grok Build before retrying."
|
|
5887
|
-
};
|
|
5888
|
-
}
|
|
5889
|
-
var centSchema, usagePeriodSchema, billingConfigSchema, grokBillingResponseSchema, GrokBuildConnector;
|
|
5890
|
-
var init_grok_build_connector = __esm({
|
|
5891
|
-
"src/connectors/grok-build/grok-build-connector.ts"() {
|
|
5892
|
-
"use strict";
|
|
5893
|
-
centSchema = z6.object({ val: z6.number().default(0) }).passthrough();
|
|
5894
|
-
usagePeriodSchema = z6.object({
|
|
5895
|
-
type: z6.string().optional(),
|
|
5896
|
-
start: z6.string().optional(),
|
|
5897
|
-
end: z6.string().optional()
|
|
5898
|
-
}).passthrough();
|
|
5899
|
-
billingConfigSchema = z6.object({
|
|
5900
|
-
creditUsagePercent: z6.number().min(0).max(100).optional(),
|
|
5901
|
-
currentPeriod: usagePeriodSchema.optional(),
|
|
5902
|
-
monthlyLimit: centSchema.optional(),
|
|
5903
|
-
used: centSchema.optional(),
|
|
5904
|
-
billingPeriodStart: z6.string().optional(),
|
|
5905
|
-
billingPeriodEnd: z6.string().optional(),
|
|
5906
|
-
isUnifiedBillingUser: z6.boolean().optional()
|
|
5907
|
-
}).passthrough();
|
|
5908
|
-
grokBillingResponseSchema = z6.object({
|
|
5909
|
-
config: billingConfigSchema.nullable(),
|
|
5910
|
-
onDemandEnabled: z6.boolean().nullable().optional(),
|
|
5911
|
-
subscriptionTier: z6.string().optional(),
|
|
5912
|
-
sourceObservedAt: z6.string().datetime({ offset: true }).optional()
|
|
5913
|
-
}).passthrough();
|
|
5914
|
-
GrokBuildConnector = class {
|
|
5915
|
-
id = "grok";
|
|
5916
|
-
displayName = "Grok";
|
|
5917
|
-
consentId = "grok";
|
|
5918
|
-
#billingClient;
|
|
5919
|
-
#historyClient;
|
|
5920
|
-
#clock;
|
|
5921
|
-
constructor(options) {
|
|
5922
|
-
this.#billingClient = options.billingClient;
|
|
5923
|
-
this.#historyClient = options.historyClient;
|
|
5924
|
-
this.#clock = options.clock ?? (() => /* @__PURE__ */ new Date());
|
|
5925
|
-
}
|
|
5926
|
-
async collect(options = { mode: "incremental" }) {
|
|
5927
|
-
const warnings = [];
|
|
5928
|
-
let quotaBuckets = [];
|
|
5929
|
-
let observedAt = this.#clock().toISOString();
|
|
5930
|
-
try {
|
|
5931
|
-
const billing = await this.#billingClient.readBilling();
|
|
5932
|
-
observedAt = billing.sourceObservedAt ?? observedAt;
|
|
5933
|
-
quotaBuckets = mapBillingQuota(billing);
|
|
5934
|
-
if (quotaBuckets.length === 0) {
|
|
5935
|
-
warnings.push({
|
|
5936
|
-
code: "grok-subscription-quota-unavailable",
|
|
5937
|
-
message: "Grok Build subscription quota is unavailable.",
|
|
5938
|
-
recovery: "Open Grok Build and run /usage, then retry refresh."
|
|
5939
|
-
});
|
|
5940
|
-
}
|
|
5941
|
-
} catch (error) {
|
|
5942
|
-
warnings.push(safeFailure7(error));
|
|
5943
|
-
}
|
|
5944
|
-
const history = this.#historyClient ? await this.#historyClient.readUsage(options) : { usage: [], costs: [], complete: true };
|
|
5945
|
-
if (!history.complete) warnings.push(incompleteTranscriptFailure3());
|
|
5946
|
-
return {
|
|
5947
|
-
provider: { id: this.id, displayName: this.displayName },
|
|
5948
|
-
billingDomains: [grokBuildBillingDomain()],
|
|
5949
|
-
quotaBuckets,
|
|
5950
|
-
usage: history.usage,
|
|
5951
|
-
...history.usage.length > 0 && history.complete ? {
|
|
5952
|
-
usageReconciliation: {
|
|
5953
|
-
authoritativeIdPrefixes: ["grok-transcript:"],
|
|
5954
|
-
retiredIdPrefixes: ["grok-otel:", "grok-headless:"]
|
|
5955
|
-
}
|
|
5956
|
-
} : {},
|
|
5957
|
-
costs: history.costs,
|
|
5958
|
-
warnings,
|
|
5959
|
-
observedAt
|
|
5960
|
-
};
|
|
5961
|
-
}
|
|
5962
|
-
};
|
|
5963
|
-
}
|
|
5964
|
-
});
|
|
5965
|
-
|
|
5966
6058
|
// src/connectors/grok-build/grok-telemetry.ts
|
|
5967
6059
|
function parseGrokOtlpMetrics(payload, receivedAt) {
|
|
5968
6060
|
const resources = extractOtlpResources(payload);
|
|
@@ -6023,7 +6115,7 @@ function parseGrokOtlpMetrics(payload, receivedAt) {
|
|
|
6023
6115
|
return snapshot(
|
|
6024
6116
|
[...usage.entries()].map(([key, item]) => ({
|
|
6025
6117
|
id: `grok-otel:${key}`,
|
|
6026
|
-
billingDomainId:
|
|
6118
|
+
billingDomainId: resolveGrokBillingDomain(item.model),
|
|
6027
6119
|
model: item.model,
|
|
6028
6120
|
sessionId: item.sessionId,
|
|
6029
6121
|
observedAt: item.timestamp,
|
|
@@ -6050,7 +6142,7 @@ function parseGrokOtlpMetrics(payload, receivedAt) {
|
|
|
6050
6142
|
function snapshot(usage, warnings, receivedAt) {
|
|
6051
6143
|
return {
|
|
6052
6144
|
provider: { id: "grok", displayName: "Grok" },
|
|
6053
|
-
billingDomains:
|
|
6145
|
+
billingDomains: grokBuildBillingDomains(usage),
|
|
6054
6146
|
quotaBuckets: [],
|
|
6055
6147
|
usage,
|
|
6056
6148
|
costs: [],
|
|
@@ -8387,7 +8479,7 @@ function allDomainHistories(providers) {
|
|
|
8387
8479
|
provider,
|
|
8388
8480
|
domain,
|
|
8389
8481
|
history: domain.history,
|
|
8390
|
-
includedInHeadline: provider.id === "dsh" ? true : domain.id === provider.summaryBillingDomainId
|
|
8482
|
+
includedInHeadline: provider.id === "dsh" || provider.id === "grok" && domain.id !== "xai-api" ? true : domain.id === provider.summaryBillingDomainId
|
|
8391
8483
|
}))
|
|
8392
8484
|
);
|
|
8393
8485
|
}
|
|
@@ -9629,6 +9721,7 @@ var init_sqlite_usage_repository = __esm({
|
|
|
9629
9721
|
model, usage_observation_id, priced_tokens, line_items_json, calculated_at
|
|
9630
9722
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
9631
9723
|
ON CONFLICT(provider_id, id) DO UPDATE SET
|
|
9724
|
+
billing_domain_id = excluded.billing_domain_id,
|
|
9632
9725
|
amount = excluded.amount,
|
|
9633
9726
|
price_snapshot_rates_json = excluded.price_snapshot_rates_json,
|
|
9634
9727
|
line_items_json = excluded.line_items_json,
|
|
@@ -10199,10 +10292,11 @@ var init_sqlite_usage_repository = __esm({
|
|
|
10199
10292
|
} : { status: "healthy", errorCode: null, message: null, recovery: null };
|
|
10200
10293
|
let providerTokenTotals = summaryDomain?.tokenTotals ?? emptyTotals;
|
|
10201
10294
|
let providerTokenEvidence = summaryDomain?.tokenEvidence ?? emptyEvidence;
|
|
10202
|
-
if (provider.id === "dsh" && billingDomains3.length > 1) {
|
|
10295
|
+
if ((provider.id === "dsh" || provider.id === "grok") && billingDomains3.length > 1) {
|
|
10203
10296
|
const combinedTotals = zeroTokenTotals();
|
|
10204
10297
|
const combinedEvidence = emptyTokenEvidence();
|
|
10205
10298
|
for (const d of billingDomains3) {
|
|
10299
|
+
if (provider.id === "grok" && d.id === "xai-api") continue;
|
|
10206
10300
|
combinedTotals.total += d.tokenTotals.total;
|
|
10207
10301
|
combinedTotals.input += d.tokenTotals.input;
|
|
10208
10302
|
combinedTotals.output += d.tokenTotals.output;
|