@kairyou/agent-tools 0.6.0 → 0.7.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.
@@ -2,6 +2,934 @@
2
2
 
3
3
  // integrations/usage/cli.mjs
4
4
  import { queryAgentProviderUsage } from "./core.mjs";
5
+
6
+ // integrations/usage/lib/config.mjs
7
+ import { readFile, writeFile, mkdir, open, stat } from "node:fs/promises";
8
+ import { existsSync } from "node:fs";
9
+ import { dirname, join } from "node:path";
10
+ import { homedir } from "node:os";
11
+
12
+ // node_modules/jsonc-parser/lib/esm/impl/scanner.js
13
+ function createScanner(text, ignoreTrivia = false) {
14
+ const len = text.length;
15
+ let pos = 0, value = "", tokenOffset = 0, token = 16, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0;
16
+ function scanHexDigits(count, exact) {
17
+ let digits = 0;
18
+ let value2 = 0;
19
+ while (digits < count || !exact) {
20
+ let ch = text.charCodeAt(pos);
21
+ if (ch >= 48 && ch <= 57) {
22
+ value2 = value2 * 16 + ch - 48;
23
+ } else if (ch >= 65 && ch <= 70) {
24
+ value2 = value2 * 16 + ch - 65 + 10;
25
+ } else if (ch >= 97 && ch <= 102) {
26
+ value2 = value2 * 16 + ch - 97 + 10;
27
+ } else {
28
+ break;
29
+ }
30
+ pos++;
31
+ digits++;
32
+ }
33
+ if (digits < count) {
34
+ value2 = -1;
35
+ }
36
+ return value2;
37
+ }
38
+ function setPosition(newPosition) {
39
+ pos = newPosition;
40
+ value = "";
41
+ tokenOffset = 0;
42
+ token = 16;
43
+ scanError = 0;
44
+ }
45
+ function scanNumber() {
46
+ let start = pos;
47
+ if (text.charCodeAt(pos) === 48) {
48
+ pos++;
49
+ } else {
50
+ pos++;
51
+ while (pos < text.length && isDigit(text.charCodeAt(pos))) {
52
+ pos++;
53
+ }
54
+ }
55
+ if (pos < text.length && text.charCodeAt(pos) === 46) {
56
+ pos++;
57
+ if (pos < text.length && isDigit(text.charCodeAt(pos))) {
58
+ pos++;
59
+ while (pos < text.length && isDigit(text.charCodeAt(pos))) {
60
+ pos++;
61
+ }
62
+ } else {
63
+ scanError = 3;
64
+ return text.substring(start, pos);
65
+ }
66
+ }
67
+ let end = pos;
68
+ if (pos < text.length && (text.charCodeAt(pos) === 69 || text.charCodeAt(pos) === 101)) {
69
+ pos++;
70
+ if (pos < text.length && text.charCodeAt(pos) === 43 || text.charCodeAt(pos) === 45) {
71
+ pos++;
72
+ }
73
+ if (pos < text.length && isDigit(text.charCodeAt(pos))) {
74
+ pos++;
75
+ while (pos < text.length && isDigit(text.charCodeAt(pos))) {
76
+ pos++;
77
+ }
78
+ end = pos;
79
+ } else {
80
+ scanError = 3;
81
+ }
82
+ }
83
+ return text.substring(start, end);
84
+ }
85
+ function scanString() {
86
+ let result = "", start = pos;
87
+ while (true) {
88
+ if (pos >= len) {
89
+ result += text.substring(start, pos);
90
+ scanError = 2;
91
+ break;
92
+ }
93
+ const ch = text.charCodeAt(pos);
94
+ if (ch === 34) {
95
+ result += text.substring(start, pos);
96
+ pos++;
97
+ break;
98
+ }
99
+ if (ch === 92) {
100
+ result += text.substring(start, pos);
101
+ pos++;
102
+ if (pos >= len) {
103
+ scanError = 2;
104
+ break;
105
+ }
106
+ const ch2 = text.charCodeAt(pos++);
107
+ switch (ch2) {
108
+ case 34:
109
+ result += '"';
110
+ break;
111
+ case 92:
112
+ result += "\\";
113
+ break;
114
+ case 47:
115
+ result += "/";
116
+ break;
117
+ case 98:
118
+ result += "\b";
119
+ break;
120
+ case 102:
121
+ result += "\f";
122
+ break;
123
+ case 110:
124
+ result += "\n";
125
+ break;
126
+ case 114:
127
+ result += "\r";
128
+ break;
129
+ case 116:
130
+ result += " ";
131
+ break;
132
+ case 117:
133
+ const ch3 = scanHexDigits(4, true);
134
+ if (ch3 >= 0) {
135
+ result += String.fromCharCode(ch3);
136
+ } else {
137
+ scanError = 4;
138
+ }
139
+ break;
140
+ default:
141
+ scanError = 5;
142
+ }
143
+ start = pos;
144
+ continue;
145
+ }
146
+ if (ch >= 0 && ch <= 31) {
147
+ if (isLineBreak(ch)) {
148
+ result += text.substring(start, pos);
149
+ scanError = 2;
150
+ break;
151
+ } else {
152
+ scanError = 6;
153
+ }
154
+ }
155
+ pos++;
156
+ }
157
+ return result;
158
+ }
159
+ function scanNext() {
160
+ value = "";
161
+ scanError = 0;
162
+ tokenOffset = pos;
163
+ lineStartOffset = lineNumber;
164
+ prevTokenLineStartOffset = tokenLineStartOffset;
165
+ if (pos >= len) {
166
+ tokenOffset = len;
167
+ return token = 17;
168
+ }
169
+ let code = text.charCodeAt(pos);
170
+ if (isWhiteSpace(code)) {
171
+ do {
172
+ pos++;
173
+ value += String.fromCharCode(code);
174
+ code = text.charCodeAt(pos);
175
+ } while (isWhiteSpace(code));
176
+ return token = 15;
177
+ }
178
+ if (isLineBreak(code)) {
179
+ pos++;
180
+ value += String.fromCharCode(code);
181
+ if (code === 13 && text.charCodeAt(pos) === 10) {
182
+ pos++;
183
+ value += "\n";
184
+ }
185
+ lineNumber++;
186
+ tokenLineStartOffset = pos;
187
+ return token = 14;
188
+ }
189
+ switch (code) {
190
+ // tokens: []{}:,
191
+ case 123:
192
+ pos++;
193
+ return token = 1;
194
+ case 125:
195
+ pos++;
196
+ return token = 2;
197
+ case 91:
198
+ pos++;
199
+ return token = 3;
200
+ case 93:
201
+ pos++;
202
+ return token = 4;
203
+ case 58:
204
+ pos++;
205
+ return token = 6;
206
+ case 44:
207
+ pos++;
208
+ return token = 5;
209
+ // strings
210
+ case 34:
211
+ pos++;
212
+ value = scanString();
213
+ return token = 10;
214
+ // comments
215
+ case 47:
216
+ const start = pos - 1;
217
+ if (text.charCodeAt(pos + 1) === 47) {
218
+ pos += 2;
219
+ while (pos < len) {
220
+ if (isLineBreak(text.charCodeAt(pos))) {
221
+ break;
222
+ }
223
+ pos++;
224
+ }
225
+ value = text.substring(start, pos);
226
+ return token = 12;
227
+ }
228
+ if (text.charCodeAt(pos + 1) === 42) {
229
+ pos += 2;
230
+ const safeLength = len - 1;
231
+ let commentClosed = false;
232
+ while (pos < safeLength) {
233
+ const ch = text.charCodeAt(pos);
234
+ if (ch === 42 && text.charCodeAt(pos + 1) === 47) {
235
+ pos += 2;
236
+ commentClosed = true;
237
+ break;
238
+ }
239
+ pos++;
240
+ if (isLineBreak(ch)) {
241
+ if (ch === 13 && text.charCodeAt(pos) === 10) {
242
+ pos++;
243
+ }
244
+ lineNumber++;
245
+ tokenLineStartOffset = pos;
246
+ }
247
+ }
248
+ if (!commentClosed) {
249
+ pos++;
250
+ scanError = 1;
251
+ }
252
+ value = text.substring(start, pos);
253
+ return token = 13;
254
+ }
255
+ value += String.fromCharCode(code);
256
+ pos++;
257
+ return token = 16;
258
+ // numbers
259
+ case 45:
260
+ value += String.fromCharCode(code);
261
+ pos++;
262
+ if (pos === len || !isDigit(text.charCodeAt(pos))) {
263
+ return token = 16;
264
+ }
265
+ // found a minus, followed by a number so
266
+ // we fall through to proceed with scanning
267
+ // numbers
268
+ case 48:
269
+ case 49:
270
+ case 50:
271
+ case 51:
272
+ case 52:
273
+ case 53:
274
+ case 54:
275
+ case 55:
276
+ case 56:
277
+ case 57:
278
+ value += scanNumber();
279
+ return token = 11;
280
+ // literals and unknown symbols
281
+ default:
282
+ while (pos < len && isUnknownContentCharacter(code)) {
283
+ pos++;
284
+ code = text.charCodeAt(pos);
285
+ }
286
+ if (tokenOffset !== pos) {
287
+ value = text.substring(tokenOffset, pos);
288
+ switch (value) {
289
+ case "true":
290
+ return token = 8;
291
+ case "false":
292
+ return token = 9;
293
+ case "null":
294
+ return token = 7;
295
+ }
296
+ return token = 16;
297
+ }
298
+ value += String.fromCharCode(code);
299
+ pos++;
300
+ return token = 16;
301
+ }
302
+ }
303
+ function isUnknownContentCharacter(code) {
304
+ if (isWhiteSpace(code) || isLineBreak(code)) {
305
+ return false;
306
+ }
307
+ switch (code) {
308
+ case 125:
309
+ case 93:
310
+ case 123:
311
+ case 91:
312
+ case 34:
313
+ case 58:
314
+ case 44:
315
+ case 47:
316
+ return false;
317
+ }
318
+ return true;
319
+ }
320
+ function scanNextNonTrivia() {
321
+ let result;
322
+ do {
323
+ result = scanNext();
324
+ } while (result >= 12 && result <= 15);
325
+ return result;
326
+ }
327
+ return {
328
+ setPosition,
329
+ getPosition: () => pos,
330
+ scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
331
+ getToken: () => token,
332
+ getTokenValue: () => value,
333
+ getTokenOffset: () => tokenOffset,
334
+ getTokenLength: () => pos - tokenOffset,
335
+ getTokenStartLine: () => lineStartOffset,
336
+ getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset,
337
+ getTokenError: () => scanError
338
+ };
339
+ }
340
+ function isWhiteSpace(ch) {
341
+ return ch === 32 || ch === 9;
342
+ }
343
+ function isLineBreak(ch) {
344
+ return ch === 10 || ch === 13;
345
+ }
346
+ function isDigit(ch) {
347
+ return ch >= 48 && ch <= 57;
348
+ }
349
+ var CharacterCodes;
350
+ (function(CharacterCodes2) {
351
+ CharacterCodes2[CharacterCodes2["lineFeed"] = 10] = "lineFeed";
352
+ CharacterCodes2[CharacterCodes2["carriageReturn"] = 13] = "carriageReturn";
353
+ CharacterCodes2[CharacterCodes2["space"] = 32] = "space";
354
+ CharacterCodes2[CharacterCodes2["_0"] = 48] = "_0";
355
+ CharacterCodes2[CharacterCodes2["_1"] = 49] = "_1";
356
+ CharacterCodes2[CharacterCodes2["_2"] = 50] = "_2";
357
+ CharacterCodes2[CharacterCodes2["_3"] = 51] = "_3";
358
+ CharacterCodes2[CharacterCodes2["_4"] = 52] = "_4";
359
+ CharacterCodes2[CharacterCodes2["_5"] = 53] = "_5";
360
+ CharacterCodes2[CharacterCodes2["_6"] = 54] = "_6";
361
+ CharacterCodes2[CharacterCodes2["_7"] = 55] = "_7";
362
+ CharacterCodes2[CharacterCodes2["_8"] = 56] = "_8";
363
+ CharacterCodes2[CharacterCodes2["_9"] = 57] = "_9";
364
+ CharacterCodes2[CharacterCodes2["a"] = 97] = "a";
365
+ CharacterCodes2[CharacterCodes2["b"] = 98] = "b";
366
+ CharacterCodes2[CharacterCodes2["c"] = 99] = "c";
367
+ CharacterCodes2[CharacterCodes2["d"] = 100] = "d";
368
+ CharacterCodes2[CharacterCodes2["e"] = 101] = "e";
369
+ CharacterCodes2[CharacterCodes2["f"] = 102] = "f";
370
+ CharacterCodes2[CharacterCodes2["g"] = 103] = "g";
371
+ CharacterCodes2[CharacterCodes2["h"] = 104] = "h";
372
+ CharacterCodes2[CharacterCodes2["i"] = 105] = "i";
373
+ CharacterCodes2[CharacterCodes2["j"] = 106] = "j";
374
+ CharacterCodes2[CharacterCodes2["k"] = 107] = "k";
375
+ CharacterCodes2[CharacterCodes2["l"] = 108] = "l";
376
+ CharacterCodes2[CharacterCodes2["m"] = 109] = "m";
377
+ CharacterCodes2[CharacterCodes2["n"] = 110] = "n";
378
+ CharacterCodes2[CharacterCodes2["o"] = 111] = "o";
379
+ CharacterCodes2[CharacterCodes2["p"] = 112] = "p";
380
+ CharacterCodes2[CharacterCodes2["q"] = 113] = "q";
381
+ CharacterCodes2[CharacterCodes2["r"] = 114] = "r";
382
+ CharacterCodes2[CharacterCodes2["s"] = 115] = "s";
383
+ CharacterCodes2[CharacterCodes2["t"] = 116] = "t";
384
+ CharacterCodes2[CharacterCodes2["u"] = 117] = "u";
385
+ CharacterCodes2[CharacterCodes2["v"] = 118] = "v";
386
+ CharacterCodes2[CharacterCodes2["w"] = 119] = "w";
387
+ CharacterCodes2[CharacterCodes2["x"] = 120] = "x";
388
+ CharacterCodes2[CharacterCodes2["y"] = 121] = "y";
389
+ CharacterCodes2[CharacterCodes2["z"] = 122] = "z";
390
+ CharacterCodes2[CharacterCodes2["A"] = 65] = "A";
391
+ CharacterCodes2[CharacterCodes2["B"] = 66] = "B";
392
+ CharacterCodes2[CharacterCodes2["C"] = 67] = "C";
393
+ CharacterCodes2[CharacterCodes2["D"] = 68] = "D";
394
+ CharacterCodes2[CharacterCodes2["E"] = 69] = "E";
395
+ CharacterCodes2[CharacterCodes2["F"] = 70] = "F";
396
+ CharacterCodes2[CharacterCodes2["G"] = 71] = "G";
397
+ CharacterCodes2[CharacterCodes2["H"] = 72] = "H";
398
+ CharacterCodes2[CharacterCodes2["I"] = 73] = "I";
399
+ CharacterCodes2[CharacterCodes2["J"] = 74] = "J";
400
+ CharacterCodes2[CharacterCodes2["K"] = 75] = "K";
401
+ CharacterCodes2[CharacterCodes2["L"] = 76] = "L";
402
+ CharacterCodes2[CharacterCodes2["M"] = 77] = "M";
403
+ CharacterCodes2[CharacterCodes2["N"] = 78] = "N";
404
+ CharacterCodes2[CharacterCodes2["O"] = 79] = "O";
405
+ CharacterCodes2[CharacterCodes2["P"] = 80] = "P";
406
+ CharacterCodes2[CharacterCodes2["Q"] = 81] = "Q";
407
+ CharacterCodes2[CharacterCodes2["R"] = 82] = "R";
408
+ CharacterCodes2[CharacterCodes2["S"] = 83] = "S";
409
+ CharacterCodes2[CharacterCodes2["T"] = 84] = "T";
410
+ CharacterCodes2[CharacterCodes2["U"] = 85] = "U";
411
+ CharacterCodes2[CharacterCodes2["V"] = 86] = "V";
412
+ CharacterCodes2[CharacterCodes2["W"] = 87] = "W";
413
+ CharacterCodes2[CharacterCodes2["X"] = 88] = "X";
414
+ CharacterCodes2[CharacterCodes2["Y"] = 89] = "Y";
415
+ CharacterCodes2[CharacterCodes2["Z"] = 90] = "Z";
416
+ CharacterCodes2[CharacterCodes2["asterisk"] = 42] = "asterisk";
417
+ CharacterCodes2[CharacterCodes2["backslash"] = 92] = "backslash";
418
+ CharacterCodes2[CharacterCodes2["closeBrace"] = 125] = "closeBrace";
419
+ CharacterCodes2[CharacterCodes2["closeBracket"] = 93] = "closeBracket";
420
+ CharacterCodes2[CharacterCodes2["colon"] = 58] = "colon";
421
+ CharacterCodes2[CharacterCodes2["comma"] = 44] = "comma";
422
+ CharacterCodes2[CharacterCodes2["dot"] = 46] = "dot";
423
+ CharacterCodes2[CharacterCodes2["doubleQuote"] = 34] = "doubleQuote";
424
+ CharacterCodes2[CharacterCodes2["minus"] = 45] = "minus";
425
+ CharacterCodes2[CharacterCodes2["openBrace"] = 123] = "openBrace";
426
+ CharacterCodes2[CharacterCodes2["openBracket"] = 91] = "openBracket";
427
+ CharacterCodes2[CharacterCodes2["plus"] = 43] = "plus";
428
+ CharacterCodes2[CharacterCodes2["slash"] = 47] = "slash";
429
+ CharacterCodes2[CharacterCodes2["formFeed"] = 12] = "formFeed";
430
+ CharacterCodes2[CharacterCodes2["tab"] = 9] = "tab";
431
+ })(CharacterCodes || (CharacterCodes = {}));
432
+
433
+ // node_modules/jsonc-parser/lib/esm/impl/string-intern.js
434
+ var cachedSpaces = new Array(20).fill(0).map((_, index) => {
435
+ return " ".repeat(index);
436
+ });
437
+ var maxCachedValues = 200;
438
+ var cachedBreakLinesWithSpaces = {
439
+ " ": {
440
+ "\n": new Array(maxCachedValues).fill(0).map((_, index) => {
441
+ return "\n" + " ".repeat(index);
442
+ }),
443
+ "\r": new Array(maxCachedValues).fill(0).map((_, index) => {
444
+ return "\r" + " ".repeat(index);
445
+ }),
446
+ "\r\n": new Array(maxCachedValues).fill(0).map((_, index) => {
447
+ return "\r\n" + " ".repeat(index);
448
+ })
449
+ },
450
+ " ": {
451
+ "\n": new Array(maxCachedValues).fill(0).map((_, index) => {
452
+ return "\n" + " ".repeat(index);
453
+ }),
454
+ "\r": new Array(maxCachedValues).fill(0).map((_, index) => {
455
+ return "\r" + " ".repeat(index);
456
+ }),
457
+ "\r\n": new Array(maxCachedValues).fill(0).map((_, index) => {
458
+ return "\r\n" + " ".repeat(index);
459
+ })
460
+ }
461
+ };
462
+
463
+ // node_modules/jsonc-parser/lib/esm/impl/parser.js
464
+ var ParseOptions;
465
+ (function(ParseOptions2) {
466
+ ParseOptions2.DEFAULT = {
467
+ allowTrailingComma: false
468
+ };
469
+ })(ParseOptions || (ParseOptions = {}));
470
+ function parse(text, errors = [], options = ParseOptions.DEFAULT) {
471
+ let currentProperty = null;
472
+ let currentParent = [];
473
+ const previousParents = [];
474
+ function onValue(value) {
475
+ if (Array.isArray(currentParent)) {
476
+ currentParent.push(value);
477
+ } else if (currentProperty !== null) {
478
+ currentParent[currentProperty] = value;
479
+ }
480
+ }
481
+ const visitor = {
482
+ onObjectBegin: () => {
483
+ const object = {};
484
+ onValue(object);
485
+ previousParents.push(currentParent);
486
+ currentParent = object;
487
+ currentProperty = null;
488
+ },
489
+ onObjectProperty: (name) => {
490
+ currentProperty = name;
491
+ },
492
+ onObjectEnd: () => {
493
+ currentParent = previousParents.pop();
494
+ },
495
+ onArrayBegin: () => {
496
+ const array = [];
497
+ onValue(array);
498
+ previousParents.push(currentParent);
499
+ currentParent = array;
500
+ currentProperty = null;
501
+ },
502
+ onArrayEnd: () => {
503
+ currentParent = previousParents.pop();
504
+ },
505
+ onLiteralValue: onValue,
506
+ onError: (error, offset, length) => {
507
+ errors.push({ error, offset, length });
508
+ }
509
+ };
510
+ visit(text, visitor, options);
511
+ return currentParent[0];
512
+ }
513
+ function visit(text, visitor, options = ParseOptions.DEFAULT) {
514
+ const _scanner = createScanner(text, false);
515
+ const _jsonPath = [];
516
+ let suppressedCallbacks = 0;
517
+ function toNoArgVisit(visitFunction) {
518
+ return visitFunction ? () => suppressedCallbacks === 0 && visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
519
+ }
520
+ function toOneArgVisit(visitFunction) {
521
+ return visitFunction ? (arg) => suppressedCallbacks === 0 && visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
522
+ }
523
+ function toOneArgVisitWithPath(visitFunction) {
524
+ return visitFunction ? (arg) => suppressedCallbacks === 0 && visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true;
525
+ }
526
+ function toBeginVisit(visitFunction) {
527
+ return visitFunction ? () => {
528
+ if (suppressedCallbacks > 0) {
529
+ suppressedCallbacks++;
530
+ } else {
531
+ let cbReturn = visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice());
532
+ if (cbReturn === false) {
533
+ suppressedCallbacks = 1;
534
+ }
535
+ }
536
+ } : () => true;
537
+ }
538
+ function toEndVisit(visitFunction) {
539
+ return visitFunction ? () => {
540
+ if (suppressedCallbacks > 0) {
541
+ suppressedCallbacks--;
542
+ }
543
+ if (suppressedCallbacks === 0) {
544
+ visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter());
545
+ }
546
+ } : () => true;
547
+ }
548
+ const onObjectBegin = toBeginVisit(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toEndVisit(visitor.onObjectEnd), onArrayBegin = toBeginVisit(visitor.onArrayBegin), onArrayEnd = toEndVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError);
549
+ const disallowComments = options && options.disallowComments;
550
+ const allowTrailingComma = options && options.allowTrailingComma;
551
+ function scanNext() {
552
+ while (true) {
553
+ const token = _scanner.scan();
554
+ switch (_scanner.getTokenError()) {
555
+ case 4:
556
+ handleError(
557
+ 14
558
+ /* ParseErrorCode.InvalidUnicode */
559
+ );
560
+ break;
561
+ case 5:
562
+ handleError(
563
+ 15
564
+ /* ParseErrorCode.InvalidEscapeCharacter */
565
+ );
566
+ break;
567
+ case 3:
568
+ handleError(
569
+ 13
570
+ /* ParseErrorCode.UnexpectedEndOfNumber */
571
+ );
572
+ break;
573
+ case 1:
574
+ if (!disallowComments) {
575
+ handleError(
576
+ 11
577
+ /* ParseErrorCode.UnexpectedEndOfComment */
578
+ );
579
+ }
580
+ break;
581
+ case 2:
582
+ handleError(
583
+ 12
584
+ /* ParseErrorCode.UnexpectedEndOfString */
585
+ );
586
+ break;
587
+ case 6:
588
+ handleError(
589
+ 16
590
+ /* ParseErrorCode.InvalidCharacter */
591
+ );
592
+ break;
593
+ }
594
+ switch (token) {
595
+ case 12:
596
+ case 13:
597
+ if (disallowComments) {
598
+ handleError(
599
+ 10
600
+ /* ParseErrorCode.InvalidCommentToken */
601
+ );
602
+ } else {
603
+ onComment();
604
+ }
605
+ break;
606
+ case 16:
607
+ handleError(
608
+ 1
609
+ /* ParseErrorCode.InvalidSymbol */
610
+ );
611
+ break;
612
+ case 15:
613
+ case 14:
614
+ break;
615
+ default:
616
+ return token;
617
+ }
618
+ }
619
+ }
620
+ function handleError(error, skipUntilAfter = [], skipUntil = []) {
621
+ onError(error);
622
+ if (skipUntilAfter.length + skipUntil.length > 0) {
623
+ let token = _scanner.getToken();
624
+ while (token !== 17) {
625
+ if (skipUntilAfter.indexOf(token) !== -1) {
626
+ scanNext();
627
+ break;
628
+ } else if (skipUntil.indexOf(token) !== -1) {
629
+ break;
630
+ }
631
+ token = scanNext();
632
+ }
633
+ }
634
+ }
635
+ function parseString(isValue) {
636
+ const value = _scanner.getTokenValue();
637
+ if (isValue) {
638
+ onLiteralValue(value);
639
+ } else {
640
+ onObjectProperty(value);
641
+ _jsonPath.push(value);
642
+ }
643
+ scanNext();
644
+ return true;
645
+ }
646
+ function parseLiteral() {
647
+ switch (_scanner.getToken()) {
648
+ case 11:
649
+ const tokenValue = _scanner.getTokenValue();
650
+ let value = Number(tokenValue);
651
+ if (isNaN(value)) {
652
+ handleError(
653
+ 2
654
+ /* ParseErrorCode.InvalidNumberFormat */
655
+ );
656
+ value = 0;
657
+ }
658
+ onLiteralValue(value);
659
+ break;
660
+ case 7:
661
+ onLiteralValue(null);
662
+ break;
663
+ case 8:
664
+ onLiteralValue(true);
665
+ break;
666
+ case 9:
667
+ onLiteralValue(false);
668
+ break;
669
+ default:
670
+ return false;
671
+ }
672
+ scanNext();
673
+ return true;
674
+ }
675
+ function parseProperty() {
676
+ if (_scanner.getToken() !== 10) {
677
+ handleError(3, [], [
678
+ 2,
679
+ 5
680
+ /* SyntaxKind.CommaToken */
681
+ ]);
682
+ return false;
683
+ }
684
+ parseString(false);
685
+ if (_scanner.getToken() === 6) {
686
+ onSeparator(":");
687
+ scanNext();
688
+ if (!parseValue()) {
689
+ handleError(4, [], [
690
+ 2,
691
+ 5
692
+ /* SyntaxKind.CommaToken */
693
+ ]);
694
+ }
695
+ } else {
696
+ handleError(5, [], [
697
+ 2,
698
+ 5
699
+ /* SyntaxKind.CommaToken */
700
+ ]);
701
+ }
702
+ _jsonPath.pop();
703
+ return true;
704
+ }
705
+ function parseObject() {
706
+ onObjectBegin();
707
+ scanNext();
708
+ let needsComma = false;
709
+ while (_scanner.getToken() !== 2 && _scanner.getToken() !== 17) {
710
+ if (_scanner.getToken() === 5) {
711
+ if (!needsComma) {
712
+ handleError(4, [], []);
713
+ }
714
+ onSeparator(",");
715
+ scanNext();
716
+ if (_scanner.getToken() === 2 && allowTrailingComma) {
717
+ break;
718
+ }
719
+ } else if (needsComma) {
720
+ handleError(6, [], []);
721
+ }
722
+ if (!parseProperty()) {
723
+ handleError(4, [], [
724
+ 2,
725
+ 5
726
+ /* SyntaxKind.CommaToken */
727
+ ]);
728
+ }
729
+ needsComma = true;
730
+ }
731
+ onObjectEnd();
732
+ if (_scanner.getToken() !== 2) {
733
+ handleError(7, [
734
+ 2
735
+ /* SyntaxKind.CloseBraceToken */
736
+ ], []);
737
+ } else {
738
+ scanNext();
739
+ }
740
+ return true;
741
+ }
742
+ function parseArray() {
743
+ onArrayBegin();
744
+ scanNext();
745
+ let isFirstElement = true;
746
+ let needsComma = false;
747
+ while (_scanner.getToken() !== 4 && _scanner.getToken() !== 17) {
748
+ if (_scanner.getToken() === 5) {
749
+ if (!needsComma) {
750
+ handleError(4, [], []);
751
+ }
752
+ onSeparator(",");
753
+ scanNext();
754
+ if (_scanner.getToken() === 4 && allowTrailingComma) {
755
+ break;
756
+ }
757
+ } else if (needsComma) {
758
+ handleError(6, [], []);
759
+ }
760
+ if (isFirstElement) {
761
+ _jsonPath.push(0);
762
+ isFirstElement = false;
763
+ } else {
764
+ _jsonPath[_jsonPath.length - 1]++;
765
+ }
766
+ if (!parseValue()) {
767
+ handleError(4, [], [
768
+ 4,
769
+ 5
770
+ /* SyntaxKind.CommaToken */
771
+ ]);
772
+ }
773
+ needsComma = true;
774
+ }
775
+ onArrayEnd();
776
+ if (!isFirstElement) {
777
+ _jsonPath.pop();
778
+ }
779
+ if (_scanner.getToken() !== 4) {
780
+ handleError(8, [
781
+ 4
782
+ /* SyntaxKind.CloseBracketToken */
783
+ ], []);
784
+ } else {
785
+ scanNext();
786
+ }
787
+ return true;
788
+ }
789
+ function parseValue() {
790
+ switch (_scanner.getToken()) {
791
+ case 3:
792
+ return parseArray();
793
+ case 1:
794
+ return parseObject();
795
+ case 10:
796
+ return parseString(true);
797
+ default:
798
+ return parseLiteral();
799
+ }
800
+ }
801
+ scanNext();
802
+ if (_scanner.getToken() === 17) {
803
+ if (options.allowEmptyContent) {
804
+ return true;
805
+ }
806
+ handleError(4, [], []);
807
+ return false;
808
+ }
809
+ if (!parseValue()) {
810
+ handleError(4, [], []);
811
+ return false;
812
+ }
813
+ if (_scanner.getToken() !== 17) {
814
+ handleError(9, [], []);
815
+ }
816
+ return true;
817
+ }
818
+
819
+ // node_modules/jsonc-parser/lib/esm/main.js
820
+ var ScanError;
821
+ (function(ScanError2) {
822
+ ScanError2[ScanError2["None"] = 0] = "None";
823
+ ScanError2[ScanError2["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment";
824
+ ScanError2[ScanError2["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString";
825
+ ScanError2[ScanError2["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber";
826
+ ScanError2[ScanError2["InvalidUnicode"] = 4] = "InvalidUnicode";
827
+ ScanError2[ScanError2["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter";
828
+ ScanError2[ScanError2["InvalidCharacter"] = 6] = "InvalidCharacter";
829
+ })(ScanError || (ScanError = {}));
830
+ var SyntaxKind;
831
+ (function(SyntaxKind2) {
832
+ SyntaxKind2[SyntaxKind2["OpenBraceToken"] = 1] = "OpenBraceToken";
833
+ SyntaxKind2[SyntaxKind2["CloseBraceToken"] = 2] = "CloseBraceToken";
834
+ SyntaxKind2[SyntaxKind2["OpenBracketToken"] = 3] = "OpenBracketToken";
835
+ SyntaxKind2[SyntaxKind2["CloseBracketToken"] = 4] = "CloseBracketToken";
836
+ SyntaxKind2[SyntaxKind2["CommaToken"] = 5] = "CommaToken";
837
+ SyntaxKind2[SyntaxKind2["ColonToken"] = 6] = "ColonToken";
838
+ SyntaxKind2[SyntaxKind2["NullKeyword"] = 7] = "NullKeyword";
839
+ SyntaxKind2[SyntaxKind2["TrueKeyword"] = 8] = "TrueKeyword";
840
+ SyntaxKind2[SyntaxKind2["FalseKeyword"] = 9] = "FalseKeyword";
841
+ SyntaxKind2[SyntaxKind2["StringLiteral"] = 10] = "StringLiteral";
842
+ SyntaxKind2[SyntaxKind2["NumericLiteral"] = 11] = "NumericLiteral";
843
+ SyntaxKind2[SyntaxKind2["LineCommentTrivia"] = 12] = "LineCommentTrivia";
844
+ SyntaxKind2[SyntaxKind2["BlockCommentTrivia"] = 13] = "BlockCommentTrivia";
845
+ SyntaxKind2[SyntaxKind2["LineBreakTrivia"] = 14] = "LineBreakTrivia";
846
+ SyntaxKind2[SyntaxKind2["Trivia"] = 15] = "Trivia";
847
+ SyntaxKind2[SyntaxKind2["Unknown"] = 16] = "Unknown";
848
+ SyntaxKind2[SyntaxKind2["EOF"] = 17] = "EOF";
849
+ })(SyntaxKind || (SyntaxKind = {}));
850
+ var parse2 = parse;
851
+ var ParseErrorCode;
852
+ (function(ParseErrorCode2) {
853
+ ParseErrorCode2[ParseErrorCode2["InvalidSymbol"] = 1] = "InvalidSymbol";
854
+ ParseErrorCode2[ParseErrorCode2["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
855
+ ParseErrorCode2[ParseErrorCode2["PropertyNameExpected"] = 3] = "PropertyNameExpected";
856
+ ParseErrorCode2[ParseErrorCode2["ValueExpected"] = 4] = "ValueExpected";
857
+ ParseErrorCode2[ParseErrorCode2["ColonExpected"] = 5] = "ColonExpected";
858
+ ParseErrorCode2[ParseErrorCode2["CommaExpected"] = 6] = "CommaExpected";
859
+ ParseErrorCode2[ParseErrorCode2["CloseBraceExpected"] = 7] = "CloseBraceExpected";
860
+ ParseErrorCode2[ParseErrorCode2["CloseBracketExpected"] = 8] = "CloseBracketExpected";
861
+ ParseErrorCode2[ParseErrorCode2["EndOfFileExpected"] = 9] = "EndOfFileExpected";
862
+ ParseErrorCode2[ParseErrorCode2["InvalidCommentToken"] = 10] = "InvalidCommentToken";
863
+ ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
864
+ ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
865
+ ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
866
+ ParseErrorCode2[ParseErrorCode2["InvalidUnicode"] = 14] = "InvalidUnicode";
867
+ ParseErrorCode2[ParseErrorCode2["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
868
+ ParseErrorCode2[ParseErrorCode2["InvalidCharacter"] = 16] = "InvalidCharacter";
869
+ })(ParseErrorCode || (ParseErrorCode = {}));
870
+
871
+ // integrations/usage/lib/config.mjs
872
+ var CODEX_HOME = process.env.CODEX_HOME || join(homedir(), ".codex");
873
+ var AGENT_TOOLS_HOME = process.env.AGENT_TOOLS_HOME || join(homedir(), ".agent-tools");
874
+ var AUTH_PATH = join(CODEX_HOME, "auth.json");
875
+ var CODEX_CONFIG_PATH = join(CODEX_HOME, "config.toml");
876
+ var AGENT_CONFIG_PATH = join(AGENT_TOOLS_HOME, "config.jsonc");
877
+ var DEBUG_PATH = join(AGENT_TOOLS_HOME, "logs", "usage-debug.log");
878
+ var ROUTE_CACHE_PATH = join(AGENT_TOOLS_HOME, "cache", "usage-routes.json");
879
+ var SNAPSHOT_PATH = join(AGENT_TOOLS_HOME, "cache", "usage-snapshot.json");
880
+ var REFRESH_STATE_PATH = join(AGENT_TOOLS_HOME, "cache", "usage-refresh-state.json");
881
+ async function readTextIfExists(path) {
882
+ if (!existsSync(path)) return "";
883
+ return readFile(path, "utf8");
884
+ }
885
+ var agentConfigCache;
886
+ async function agentConfig() {
887
+ if (agentConfigCache) return agentConfigCache;
888
+ try {
889
+ const raw = await readTextIfExists(AGENT_CONFIG_PATH);
890
+ if (!raw.trim()) {
891
+ agentConfigCache = {};
892
+ return agentConfigCache;
893
+ }
894
+ const errors = [];
895
+ const parsed = parse2(raw.replace(/^\uFEFF/, ""), errors, { allowTrailingComma: true });
896
+ agentConfigCache = errors.length === 0 && parsed?.providerUsage || {};
897
+ } catch {
898
+ agentConfigCache = {};
899
+ }
900
+ return agentConfigCache;
901
+ }
902
+ var DEBUG_LOG_MAX_BYTES = 256 * 1024;
903
+ var DEBUG_LOG_KEEP_BYTES = 128 * 1024;
904
+ async function rotateDebugLogIfNeeded() {
905
+ try {
906
+ const { size } = await stat(DEBUG_PATH);
907
+ if (size <= DEBUG_LOG_MAX_BYTES) return;
908
+ const handle = await open(DEBUG_PATH, "r");
909
+ try {
910
+ const buffer = Buffer.alloc(DEBUG_LOG_KEEP_BYTES);
911
+ await handle.read(buffer, 0, DEBUG_LOG_KEEP_BYTES, size - DEBUG_LOG_KEEP_BYTES);
912
+ await writeFile(DEBUG_PATH, buffer.toString("utf8").replace(/^[^\n]*\n?/, ""));
913
+ } finally {
914
+ await handle.close();
915
+ }
916
+ } catch {
917
+ }
918
+ }
919
+ async function debugLog(event) {
920
+ const config = await agentConfig();
921
+ if (process.env.PROVIDER_USAGE_DEBUG !== "1" && config.debug !== true) return;
922
+ await mkdir(dirname(DEBUG_PATH), { recursive: true });
923
+ await rotateDebugLogIfNeeded();
924
+ const line = JSON.stringify({
925
+ at: (/* @__PURE__ */ new Date()).toISOString(),
926
+ ...event
927
+ });
928
+ await writeFile(DEBUG_PATH, `${line}
929
+ `, { flag: "a" });
930
+ }
931
+
932
+ // integrations/usage/cli.mjs
5
933
  function parseAgent(argv) {
6
934
  for (let index = 0; index < argv.length; index += 1) {
7
935
  const arg = argv[index];
@@ -20,6 +948,8 @@ if (agent !== "claude" && agent !== "codex") {
20
948
  const result = await queryAgentProviderUsage(agent);
21
949
  if (result?.text) process.stdout.write(`${result.text}
22
950
  `);
23
- } catch {
951
+ } catch (error) {
952
+ await debugLog({ source: "cli", agent, error: error?.message || String(error) }).catch(() => {
953
+ });
24
954
  }
25
955
  }