cc-pets 1.2.4 → 1.2.6
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/Sources/CCPets/CCPetsAppDelegate.m +1 -1
- package/Sources/CCPets/CCPetsPreviews.m +20 -2
- package/Sources/CCPets/CCPetsQuotaHistory.h +2 -1
- package/Sources/CCPets/CCPetsQuotaHistory.m +7 -3
- package/Sources/CCPets/CCPetsUsage.m +125 -78
- package/Sources/CCPets/QuotaDashboardView.h +2 -2
- package/Sources/CCPets/QuotaDashboardView.m +161 -32
- package/package.json +1 -1
- package/scripts/install-claude-hooks.mjs +34 -3
|
@@ -372,7 +372,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
372
372
|
- (NSString *)statusDetailForState:(NSString *)state {
|
|
373
373
|
if ([state isEqualToString:@"starting"]) return @"正在准备当前会话…";
|
|
374
374
|
if ([state isEqualToString:@"idle"]) return @"会话已就绪,等待你的下一条指令。";
|
|
375
|
-
if ([state isEqualToString:@"thinking"]) return @"
|
|
375
|
+
if ([state isEqualToString:@"thinking"]) return @"正在分析需求…";
|
|
376
376
|
if ([state isEqualToString:@"auto_review"]) return @"正在由 Auto-review 审核,无需手动操作。";
|
|
377
377
|
if ([state isEqualToString:@"approval"]) return @"请回到终端确认操作。";
|
|
378
378
|
if ([state isEqualToString:@"subagent"]) return @"正在协调子 Agent。";
|
|
@@ -33,8 +33,26 @@ int RenderQuotaDashboard(NSString *path) {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
view.historyEnabled = YES;
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
37
|
+
NSTimeInterval day = 24 * 60 * 60;
|
|
38
|
+
view.codexHistory = @[
|
|
39
|
+
@{@"timestamp": @(now - 6.5 * day), @"remaining": @88},
|
|
40
|
+
@{@"timestamp": @(now - 5.5 * day), @"remaining": @81},
|
|
41
|
+
@{@"timestamp": @(now - 4.5 * day), @"remaining": @75},
|
|
42
|
+
@{@"timestamp": @(now - 3.5 * day), @"remaining": @68},
|
|
43
|
+
@{@"timestamp": @(now - 2.5 * day), @"remaining": @62},
|
|
44
|
+
@{@"timestamp": @(now - 1.5 * day), @"remaining": @55},
|
|
45
|
+
@{@"timestamp": @(now - 0.5 * day), @"remaining": @49}
|
|
46
|
+
];
|
|
47
|
+
view.claudeHistory = @[
|
|
48
|
+
@{@"timestamp": @(now - 6.5 * day), @"remaining": @92},
|
|
49
|
+
@{@"timestamp": @(now - 5.5 * day), @"remaining": @86},
|
|
50
|
+
@{@"timestamp": @(now - 4.5 * day), @"remaining": @78},
|
|
51
|
+
@{@"timestamp": @(now - 3.5 * day), @"remaining": @72},
|
|
52
|
+
@{@"timestamp": @(now - 2.5 * day), @"remaining": @64},
|
|
53
|
+
@{@"timestamp": @(now - 1.5 * day), @"remaining": @58},
|
|
54
|
+
@{@"timestamp": @(now - 0.5 * day), @"remaining": @51}
|
|
55
|
+
];
|
|
38
56
|
view.activeAgentCount = 1;
|
|
39
57
|
// 预览里刻意只让一家在线,好把"有额度数据但客户端已退出"的离线态也画出来。
|
|
40
58
|
view.liveProviders = [NSSet setWithObject:@"Claude"];
|
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
// RecordQuotaHistory 返回当前文档,调用方直接复用,避免一轮刷新里重复读盘。
|
|
5
5
|
NSDictionary *QuotaHistoryDocument(void);
|
|
6
6
|
NSDictionary *RecordQuotaHistory(NSDictionary *codexUsage, NSDictionary *claudeUsage);
|
|
7
|
-
|
|
7
|
+
// 每个点保留 timestamp 与 weekRemaining,绘图层才能按真实时间轴展示缺口和重置。
|
|
8
|
+
NSArray<NSDictionary *> *QuotaHistorySeries(NSDictionary *document, NSString *provider);
|
|
@@ -75,15 +75,19 @@ NSDictionary *RecordQuotaHistory(NSDictionary *codexUsage, NSDictionary *claudeU
|
|
|
75
75
|
return updated;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
NSArray<
|
|
79
|
-
NSMutableArray<
|
|
78
|
+
NSArray<NSDictionary *> *QuotaHistorySeries(NSDictionary *document, NSString *provider) {
|
|
79
|
+
NSMutableArray<NSDictionary *> *series = [NSMutableArray array];
|
|
80
80
|
for (NSDictionary *sample in document[@"samples"]) {
|
|
81
81
|
if (![sample isKindOfClass:NSDictionary.class]) continue;
|
|
82
|
+
NSNumber *timestamp = [sample[@"timestamp"] isKindOfClass:NSNumber.class]
|
|
83
|
+
? sample[@"timestamp"] : nil;
|
|
82
84
|
NSDictionary *providerSample = [sample[provider] isKindOfClass:NSDictionary.class]
|
|
83
85
|
? sample[provider] : nil;
|
|
84
86
|
NSNumber *remaining = [providerSample[@"weekRemaining"] isKindOfClass:NSNumber.class]
|
|
85
87
|
? providerSample[@"weekRemaining"] : nil;
|
|
86
|
-
if (remaining)
|
|
88
|
+
if (timestamp && remaining) {
|
|
89
|
+
[series addObject:@{@"timestamp": timestamp, @"remaining": remaining}];
|
|
90
|
+
}
|
|
87
91
|
}
|
|
88
92
|
return series;
|
|
89
93
|
}
|
|
@@ -449,80 +449,129 @@ static NSDictionary *LatestQuotaInRecentSessions(NSURL *sessionsURL, NSURL *skip
|
|
|
449
449
|
return newest;
|
|
450
450
|
}
|
|
451
451
|
|
|
452
|
-
static
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
452
|
+
static NSTimeInterval LineTimestamp(NSData *line) {
|
|
453
|
+
if (line.length == 0) return 0;
|
|
454
|
+
id root = [NSJSONSerialization JSONObjectWithData:line options:0 error:nil];
|
|
455
|
+
if (![root isKindOfClass:NSDictionary.class]) return 0;
|
|
456
|
+
NSDate *date = ISO8601Timestamp(((NSDictionary *)root)[@"timestamp"]);
|
|
457
|
+
return date ? date.timeIntervalSince1970 : 0;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// 按块切行喂给 consume,返回消费掉的字节数(截止最后一个换行)。返回值就是下一次可以
|
|
461
|
+
// 安全续读的偏移:结尾那半行还没写完,不能算进去。includeTail=YES 时残行照样交给
|
|
462
|
+
// consume(一次性整篇扫描的调用方要它,最后一行未必有换行结尾)。
|
|
463
|
+
static unsigned long long EnumerateLines(NSFileHandle *handle, BOOL includeTail,
|
|
464
|
+
void (^consume)(NSData *)) {
|
|
465
|
+
NSMutableData *pending = [NSMutableData data];
|
|
466
|
+
unsigned long long consumed = 0;
|
|
467
|
+
while (YES) {
|
|
468
|
+
NSData *chunk = [handle readDataOfLength:256 * 1024];
|
|
469
|
+
if (chunk.length == 0) break;
|
|
470
|
+
[pending appendData:chunk];
|
|
471
|
+
const uint8_t *bytes = pending.bytes;
|
|
472
|
+
NSUInteger lineStart = 0;
|
|
473
|
+
for (NSUInteger index = 0; index < pending.length; index++) {
|
|
474
|
+
if (bytes[index] != '\n') continue;
|
|
475
|
+
if (index > lineStart) {
|
|
476
|
+
consume([pending subdataWithRange:NSMakeRange(lineStart, index - lineStart)]);
|
|
477
|
+
}
|
|
478
|
+
lineStart = index + 1;
|
|
479
|
+
}
|
|
480
|
+
if (lineStart > 0) {
|
|
481
|
+
consumed += lineStart;
|
|
482
|
+
pending = [[pending subdataWithRange:NSMakeRange(
|
|
483
|
+
lineStart, pending.length - lineStart)] mutableCopy];
|
|
463
484
|
}
|
|
464
|
-
start = index + 1;
|
|
465
485
|
}
|
|
466
|
-
|
|
486
|
+
if (includeTail && pending.length > 0) consume(pending);
|
|
487
|
+
return consumed;
|
|
467
488
|
}
|
|
468
489
|
|
|
469
|
-
static
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
while (start > 0 && bytes[start - 1] != '\n') start -= 1;
|
|
475
|
-
if (end > start) {
|
|
476
|
-
NSDictionary *record = TokenRecordFromLine(
|
|
477
|
-
[data subdataWithRange:NSMakeRange(start, end - start)]);
|
|
478
|
-
if (record[@"total"]) return record;
|
|
479
|
-
}
|
|
480
|
-
end = start > 0 ? start - 1 : 0;
|
|
490
|
+
static void AddTokenCounts(NSMutableDictionary *target, NSDictionary *counts) {
|
|
491
|
+
for (NSString *key in TokenKeys()) {
|
|
492
|
+
unsigned long long current = [target[key] unsignedLongLongValue];
|
|
493
|
+
unsigned long long addition = [counts[key] unsignedLongLongValue];
|
|
494
|
+
if (counts[key]) target[key] = @(current + addition);
|
|
481
495
|
}
|
|
482
|
-
return nil;
|
|
483
496
|
}
|
|
484
497
|
|
|
498
|
+
// total_token_usage 是会话内累计值,但同一个 rollout 文件里它会中途归零重来:resume 或
|
|
499
|
+
// 上下文压缩之后进程从头开始计数。本机近 14 天 106 个会话里 3 个如此,最狠的一个从
|
|
500
|
+
// 1098 万掉回 11 万——只认末行 total 的话,归零之前那一整段全被吞掉(该会话真实用量
|
|
501
|
+
// 约 1146 万,末行只有 47 万,漏掉 96%)。因此把会话按归零点切成若干段:段内仍然用
|
|
502
|
+
// 累计值(对重复写入的 token_count 幂等),段与段之间相加。
|
|
503
|
+
static BOOL TokenTotalsDidReset(NSDictionary *totals, NSDictionary *previous) {
|
|
504
|
+
for (NSString *key in TokenKeys()) {
|
|
505
|
+
if (!totals[key] || !previous[key]) continue;
|
|
506
|
+
if ([totals[key] unsignedLongLongValue] < [previous[key] unsignedLongLongValue]) return YES;
|
|
507
|
+
}
|
|
508
|
+
return NO;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
static NSDictionary *SummedSegmentTotals(NSArray<NSDictionary *> *segments) {
|
|
512
|
+
NSMutableDictionary *total = [NSMutableDictionary dictionary];
|
|
513
|
+
for (NSDictionary *segment in segments) AddTokenCounts(total, segment);
|
|
514
|
+
return total;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// 分段必须整篇扫描(末行 total 不再够用),因此改成按偏移增量追读:活跃会话每轮只解析
|
|
518
|
+
// 新增的那几行,段状态(各段末尾的累计值)留在缓存里续接。
|
|
485
519
|
static NSDictionary *TokenSessionSummary(NSURL *url, NSMutableDictionary *cache) {
|
|
486
520
|
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:url.path error:nil];
|
|
487
521
|
unsigned long long size = [attributes[NSFileSize] unsignedLongLongValue];
|
|
488
522
|
NSDictionary *cached = cache[url.path];
|
|
523
|
+
// 没有 token_count 的会话(旧格式、被截断、刚创建)也会留下缓存条目,否则每一轮都要
|
|
524
|
+
// 把整篇重读一遍。文件长大后 size 变化自然会让缓存失效。
|
|
489
525
|
if (cached && [cached[@"size"] unsignedLongLongValue] == size) {
|
|
490
|
-
return
|
|
526
|
+
return (cached[@"start"] && cached[@"total"]) ? cached : nil;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
unsigned long long offset = 0;
|
|
530
|
+
NSMutableArray<NSDictionary *> *segments = [NSMutableArray array];
|
|
531
|
+
__block NSNumber *start = nil;
|
|
532
|
+
__block NSTimeInterval last = 0;
|
|
533
|
+
if (cached) {
|
|
534
|
+
unsigned long long cachedOffset = [cached[@"offset"] unsignedLongLongValue];
|
|
535
|
+
// offset > size 只可能是文件被改写(截断/重建),旧的段全部作废,整篇重来。
|
|
536
|
+
if (cachedOffset <= size) {
|
|
537
|
+
offset = cachedOffset;
|
|
538
|
+
start = [cached[@"start"] isKindOfClass:NSNumber.class] ? cached[@"start"] : nil;
|
|
539
|
+
last = [cached[@"end"] doubleValue];
|
|
540
|
+
if ([cached[@"segments"] isKindOfClass:NSArray.class]) {
|
|
541
|
+
[segments addObjectsFromArray:cached[@"segments"]];
|
|
542
|
+
}
|
|
543
|
+
}
|
|
491
544
|
}
|
|
492
545
|
|
|
493
546
|
NSFileHandle *handle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
|
|
494
547
|
if (!handle) return nil;
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
548
|
+
if (offset > 0) [handle seekToFileOffset:offset];
|
|
549
|
+
unsigned long long consumed = EnumerateLines(handle, NO, ^(NSData *line) {
|
|
550
|
+
// 会话起点取文件第一条带 timestamp 的行,不限于 token_count:它决定这个会话算不算
|
|
551
|
+
// 整段落在窗口内。找到之后就不再对普通行做 JSON 解析。
|
|
552
|
+
if (!start) {
|
|
553
|
+
NSTimeInterval timestamp = LineTimestamp(line);
|
|
554
|
+
if (timestamp > 0) start = @(timestamp);
|
|
555
|
+
}
|
|
556
|
+
NSDictionary *record = TokenRecordFromLine(line);
|
|
557
|
+
NSDictionary *total = record[@"total"];
|
|
558
|
+
if (!total) return;
|
|
559
|
+
last = [record[@"timestamp"] doubleValue];
|
|
560
|
+
NSDictionary *previous = segments.lastObject;
|
|
561
|
+
if (!previous || TokenTotalsDidReset(total, previous)) [segments addObject:total];
|
|
562
|
+
else segments[segments.count - 1] = total;
|
|
563
|
+
});
|
|
500
564
|
[handle closeFile];
|
|
501
565
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
return nil;
|
|
566
|
+
NSMutableDictionary *summary = [@{@"size": @(size), @"offset": @(offset + consumed)} mutableCopy];
|
|
567
|
+
if (start) summary[@"start"] = start;
|
|
568
|
+
if (segments.count > 0) {
|
|
569
|
+
summary[@"segments"] = [segments copy];
|
|
570
|
+
summary[@"end"] = @(last);
|
|
571
|
+
summary[@"total"] = SummedSegmentTotals(segments);
|
|
509
572
|
}
|
|
510
|
-
NSMutableDictionary *summary = [@{
|
|
511
|
-
@"size": @(size),
|
|
512
|
-
@"start": first[@"timestamp"],
|
|
513
|
-
@"end": latest[@"timestamp"],
|
|
514
|
-
@"total": latest[@"total"]
|
|
515
|
-
} mutableCopy];
|
|
516
573
|
cache[url.path] = summary;
|
|
517
|
-
return summary;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
static void AddTokenCounts(NSMutableDictionary *target, NSDictionary *counts) {
|
|
521
|
-
for (NSString *key in TokenKeys()) {
|
|
522
|
-
unsigned long long current = [target[key] unsignedLongLongValue];
|
|
523
|
-
unsigned long long addition = [counts[key] unsignedLongLongValue];
|
|
524
|
-
if (counts[key]) target[key] = @(current + addition);
|
|
525
|
-
}
|
|
574
|
+
return (start && summary[@"total"]) ? summary : nil;
|
|
526
575
|
}
|
|
527
576
|
|
|
528
577
|
// 累计值相减,而不是累加窗口内的 last_token_usage:Codex 会把同一份 token_count
|
|
@@ -540,40 +589,34 @@ static NSDictionary *TokenCountsBySubtracting(NSDictionary *totals, NSDictionary
|
|
|
540
589
|
return result;
|
|
541
590
|
}
|
|
542
591
|
|
|
592
|
+
// 跨窗口起点的会话只算窗口内的增量。分段规则与 TokenSessionSummary 一致:累计值一旦
|
|
593
|
+
// 归零,窗口起点之前取到的基线就作废了,后面那一段要从 0 起算,段与段的增量再相加。
|
|
543
594
|
static NSDictionary *PartialTokenTotals(NSURL *url, NSTimeInterval start, NSTimeInterval end) {
|
|
544
595
|
NSFileHandle *handle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
|
|
545
596
|
if (!handle) return nil;
|
|
597
|
+
NSMutableDictionary *result = [NSMutableDictionary dictionary];
|
|
598
|
+
__block NSDictionary *previous = nil;
|
|
546
599
|
__block NSDictionary *baseline = nil;
|
|
547
600
|
__block NSDictionary *inside = nil;
|
|
548
|
-
|
|
601
|
+
// 窗口内一条 token_count 都没有的段不贡献任何用量。
|
|
602
|
+
void (^flush)(void) = ^{
|
|
603
|
+
if (inside) AddTokenCounts(result, TokenCountsBySubtracting(inside, baseline));
|
|
604
|
+
baseline = nil;
|
|
605
|
+
inside = nil;
|
|
606
|
+
};
|
|
607
|
+
EnumerateLines(handle, YES, ^(NSData *line) {
|
|
549
608
|
NSDictionary *record = TokenRecordFromLine(line);
|
|
550
609
|
NSDictionary *total = record[@"total"];
|
|
551
610
|
if (!total) return;
|
|
611
|
+
if (previous && TokenTotalsDidReset(total, previous)) flush();
|
|
612
|
+
previous = total;
|
|
552
613
|
NSTimeInterval timestamp = [record[@"timestamp"] doubleValue];
|
|
553
614
|
if (timestamp < start) baseline = total;
|
|
554
615
|
else if (timestamp < end) inside = total;
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
while (YES) {
|
|
558
|
-
NSData *chunk = [handle readDataOfLength:256 * 1024];
|
|
559
|
-
if (chunk.length == 0) break;
|
|
560
|
-
[pending appendData:chunk];
|
|
561
|
-
const uint8_t *bytes = pending.bytes;
|
|
562
|
-
NSUInteger lineStart = 0;
|
|
563
|
-
for (NSUInteger index = 0; index < pending.length; index++) {
|
|
564
|
-
if (bytes[index] != '\n') continue;
|
|
565
|
-
consume([pending subdataWithRange:NSMakeRange(lineStart, index - lineStart)]);
|
|
566
|
-
lineStart = index + 1;
|
|
567
|
-
}
|
|
568
|
-
if (lineStart > 0) {
|
|
569
|
-
pending = [[pending subdataWithRange:NSMakeRange(
|
|
570
|
-
lineStart, pending.length - lineStart)] mutableCopy];
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
if (pending.length > 0) consume(pending);
|
|
616
|
+
});
|
|
617
|
+
flush();
|
|
574
618
|
[handle closeFile];
|
|
575
|
-
|
|
576
|
-
return inside ? TokenCountsBySubtracting(inside, baseline) : @{};
|
|
619
|
+
return result;
|
|
577
620
|
}
|
|
578
621
|
|
|
579
622
|
// 聚合区间对齐官方重置时间:[resets_at - 窗长, resets_at)。
|
|
@@ -1268,8 +1311,12 @@ static NSDictionary *ClaudeTokenTotals(NSArray<NSData *> *recordSets, NSTimeInte
|
|
|
1268
1311
|
};
|
|
1269
1312
|
}
|
|
1270
1313
|
- (NSDictionary *)refresh {
|
|
1271
|
-
|
|
1272
|
-
|
|
1314
|
+
// 额度快照是 statusline hook 写出来的(见 RecordClaudeUsage):第三方 statusline 注入不
|
|
1315
|
+
// 进去、或者账号本身(API key / Bedrock / Vertex)就不带 rate_limits 时,这份文件永远
|
|
1316
|
+
// 不存在。而 Token 是本机从转录里数出来的,跟官方额度没有半点关系,不能被它一票否决
|
|
1317
|
+
// ——原先这里直接 return nil,面板上 Claude 整行(百分比、已用 Token、今日输入输出)
|
|
1318
|
+
// 全是 `--`,看起来像统计坏了。缺额度时百分比照样 `--`,用量正常显示。
|
|
1319
|
+
NSDictionary *limits = ClaudeRateLimits() ?: @{@"fiveHour": NSNull.null, @"week": NSNull.null};
|
|
1273
1320
|
NSString *identity = TokenWindowIdentity(limits);
|
|
1274
1321
|
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
1275
1322
|
BOOL reusable = !self.forcesTokenAggregation && self.tokenUsage &&
|
|
@@ -10,8 +10,8 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName);
|
|
|
10
10
|
@interface QuotaDashboardView : NSView
|
|
11
11
|
@property NSDictionary *codexUsage;
|
|
12
12
|
@property NSDictionary *claudeUsage;
|
|
13
|
-
@property NSArray<
|
|
14
|
-
@property NSArray<
|
|
13
|
+
@property NSArray<NSDictionary *> *codexHistory;
|
|
14
|
+
@property NSArray<NSDictionary *> *claudeHistory;
|
|
15
15
|
@property BOOL historyEnabled;
|
|
16
16
|
@property NSInteger activeAgentCount;
|
|
17
17
|
// 仍在运行的客户端所属的 provider("Codex" / "Claude")。卡片的在线徽章据此判定,
|
|
@@ -168,7 +168,7 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
168
168
|
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
|
|
169
169
|
formatter.timeZone = NSTimeZone.localTimeZone;
|
|
170
170
|
formatter.dateFormat = @"yyyy-MM-dd";
|
|
171
|
-
return [NSString stringWithFormat:@"近 7
|
|
171
|
+
return [NSString stringWithFormat:@"近 7 天累计用量(%@ 至今·滚动记录)",
|
|
172
172
|
[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:start.doubleValue]]];
|
|
173
173
|
}
|
|
174
174
|
// 在线 = 这一家还有活着的客户端,和有没有额度数据无关:额度会一直缓存着,
|
|
@@ -226,21 +226,132 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
226
226
|
[color setFill];
|
|
227
227
|
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius] fill];
|
|
228
228
|
}
|
|
229
|
-
- (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
229
|
+
- (NSArray<NSDictionary *> *)curvePointsFromHistory:(NSArray<NSDictionary *> *)history
|
|
230
|
+
currentUsed:(NSNumber *)currentUsed {
|
|
231
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
232
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
233
|
+
NSMutableArray<NSDictionary *> *points = [NSMutableArray array];
|
|
234
|
+
if (self.historyEnabled) {
|
|
235
|
+
for (NSDictionary *sample in history) {
|
|
236
|
+
if (![sample isKindOfClass:NSDictionary.class]) continue;
|
|
237
|
+
NSNumber *timestamp = [sample[@"timestamp"] isKindOfClass:NSNumber.class]
|
|
238
|
+
? sample[@"timestamp"] : nil;
|
|
239
|
+
NSNumber *remaining = [sample[@"remaining"] isKindOfClass:NSNumber.class]
|
|
240
|
+
? sample[@"remaining"] : nil;
|
|
241
|
+
if (!timestamp || !remaining || timestamp.doubleValue < cutoff) continue;
|
|
242
|
+
double used = 100.0 - fmax(0, fmin(100, remaining.doubleValue));
|
|
243
|
+
[points addObject:@{@"timestamp": timestamp, @"used": @(used)}];
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// 终点必须和卡片上的当前官方额度同源;历史落盘最多会慢 15 分钟,不能直接拿它
|
|
247
|
+
// 冒充“当前”,否则同一张卡里会出现两个不同百分比。
|
|
248
|
+
if (currentUsed) {
|
|
249
|
+
[points addObject:@{@"timestamp": @(now), @"used": currentUsed}];
|
|
250
|
+
}
|
|
251
|
+
return points;
|
|
252
|
+
}
|
|
253
|
+
- (void)appendCurveFromPoint:(NSPoint)from toPoint:(NSPoint)to path:(NSBezierPath *)path {
|
|
254
|
+
CGFloat controlOffset = (to.x - from.x) / 3.0;
|
|
255
|
+
[path curveToPoint:to
|
|
256
|
+
controlPoint1:NSMakePoint(from.x + controlOffset, from.y)
|
|
257
|
+
controlPoint2:NSMakePoint(to.x - controlOffset, to.y)];
|
|
258
|
+
}
|
|
259
|
+
- (void)drawCurveSegment:(NSArray<NSValue *> *)segment inRect:(NSRect)rect color:(NSColor *)color {
|
|
260
|
+
if (segment.count == 0) return;
|
|
261
|
+
NSBezierPath *line = [NSBezierPath bezierPath];
|
|
262
|
+
NSPoint first = segment.firstObject.pointValue;
|
|
263
|
+
[line moveToPoint:first];
|
|
264
|
+
NSPoint previous = first;
|
|
265
|
+
for (NSUInteger index = 1; index < segment.count; index++) {
|
|
266
|
+
NSPoint point = segment[index].pointValue;
|
|
267
|
+
[self appendCurveFromPoint:previous toPoint:point path:line];
|
|
268
|
+
previous = point;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
NSBezierPath *fill = [line copy];
|
|
272
|
+
[fill lineToPoint:NSMakePoint(previous.x, NSMinY(rect))];
|
|
273
|
+
[fill lineToPoint:NSMakePoint(first.x, NSMinY(rect))];
|
|
274
|
+
[fill closePath];
|
|
275
|
+
[NSGraphicsContext saveGraphicsState];
|
|
276
|
+
[fill addClip];
|
|
277
|
+
NSGradient *area = [[NSGradient alloc]
|
|
278
|
+
initWithStartingColor:[color colorWithAlphaComponent:0.34]
|
|
279
|
+
endingColor:[color colorWithAlphaComponent:0.025]];
|
|
280
|
+
[area drawFromPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))
|
|
281
|
+
toPoint:NSMakePoint(NSMidX(rect), NSMinY(rect)) options:0];
|
|
282
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
283
|
+
|
|
284
|
+
[[color colorWithAlphaComponent:0.95] setStroke];
|
|
285
|
+
line.lineWidth = 2;
|
|
286
|
+
[line stroke];
|
|
287
|
+
}
|
|
288
|
+
- (void)drawUsageCurve:(NSArray<NSDictionary *> *)points inRect:(NSRect)rect
|
|
289
|
+
color:(NSColor *)color {
|
|
290
|
+
[[NSColor colorWithWhite:1 alpha:0.08] setStroke];
|
|
291
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))
|
|
292
|
+
toPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
|
|
293
|
+
if (points.count == 0) return;
|
|
294
|
+
|
|
295
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
296
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
297
|
+
// 曲线始终连成一段:采样缺口只代表桌宠当时没在运行,不代表没有消耗,
|
|
298
|
+
// 缺口两端直接插值比断开更接近真实轨迹;窗口重置带来的回落本身也是真实变化。
|
|
299
|
+
NSMutableArray<NSValue *> *allPoints = [NSMutableArray arrayWithCapacity:points.count];
|
|
300
|
+
for (NSDictionary *sample in points) {
|
|
301
|
+
NSTimeInterval timestamp = [sample[@"timestamp"] doubleValue];
|
|
302
|
+
double used = fmax(0, fmin(100, [sample[@"used"] doubleValue]));
|
|
303
|
+
CGFloat x = NSMinX(rect) + NSWidth(rect) * (timestamp - cutoff) / (now - cutoff);
|
|
304
|
+
CGFloat y = NSMinY(rect) + NSHeight(rect) * used / 100.0;
|
|
305
|
+
[allPoints addObject:[NSValue valueWithPoint:NSMakePoint(
|
|
306
|
+
fmax(NSMinX(rect), fmin(NSMaxX(rect), x)), y)]];
|
|
243
307
|
}
|
|
308
|
+
[self drawCurveSegment:allPoints inRect:rect color:color];
|
|
309
|
+
}
|
|
310
|
+
- (NSDictionary *)paceStatusForQuota:(NSDictionary *)quota currentUsed:(NSNumber *)currentUsed
|
|
311
|
+
color:(NSColor *)providerColor {
|
|
312
|
+
if (!currentUsed) {
|
|
313
|
+
return @{@"label": @"数据不足", @"tip": @"等待官方额度数据",
|
|
314
|
+
@"color": self.secondaryColor};
|
|
315
|
+
}
|
|
316
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class]
|
|
317
|
+
? quota[@"resets_at"] : nil;
|
|
318
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
319
|
+
const NSTimeInterval sevenDaySeconds = 7 * 24 * 60 * 60;
|
|
320
|
+
NSTimeInterval start = reset ? reset.doubleValue - sevenDaySeconds : 0;
|
|
321
|
+
NSTimeInterval elapsed = now - start;
|
|
322
|
+
double projectedRemaining = NAN;
|
|
323
|
+
const NSTimeInterval paceObservationSeconds = 24 * 60 * 60;
|
|
324
|
+
if (reset && reset.doubleValue > now && elapsed >= paceObservationSeconds) {
|
|
325
|
+
double projectedUsed = currentUsed.doubleValue * sevenDaySeconds / elapsed;
|
|
326
|
+
projectedRemaining = 100.0 - projectedUsed;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
double remaining = 100.0 - currentUsed.doubleValue;
|
|
330
|
+
NSString *label = remaining >= 70 ? @"充足"
|
|
331
|
+
: (remaining >= 40 ? @"平稳" : (remaining >= 15 ? @"需关注" : @"高风险"));
|
|
332
|
+
NSString *tip = remaining >= 70 ? @"当前额度余量充足"
|
|
333
|
+
: (remaining >= 40 ? @"当前额度余量平稳"
|
|
334
|
+
: (remaining >= 15 ? @"建议关注额度余量" : @"当前额度余量较低"));
|
|
335
|
+
NSColor *statusColor = remaining >= 70 ? NSColor.systemGreenColor
|
|
336
|
+
: (remaining >= 40 ? providerColor
|
|
337
|
+
: (remaining >= 15 ? NSColor.systemOrangeColor : NSColor.systemRedColor));
|
|
338
|
+
// 当前窗口开始满 24 小时后再按消耗节奏预测。此前只看真实剩余额度,避免把
|
|
339
|
+
// 开窗头几个小时的突发任务线性外推到整整 7 天,造成“高风险”误报。
|
|
340
|
+
if (!isnan(projectedRemaining) &&
|
|
341
|
+
(remaining <= 5 || projectedRemaining < 0)) {
|
|
342
|
+
label = @"高风险";
|
|
343
|
+
tip = @"预计重置前额度用尽";
|
|
344
|
+
statusColor = NSColor.systemRedColor;
|
|
345
|
+
} else if (!isnan(projectedRemaining) && projectedRemaining < 10) {
|
|
346
|
+
label = @"需关注";
|
|
347
|
+
tip = @"建议减少高消耗任务";
|
|
348
|
+
statusColor = NSColor.systemOrangeColor;
|
|
349
|
+
} else if (!isnan(projectedRemaining) && projectedRemaining >= 30) {
|
|
350
|
+
label = @"充足";
|
|
351
|
+
tip = @"按当前节奏额度充足";
|
|
352
|
+
statusColor = NSColor.systemGreenColor;
|
|
353
|
+
}
|
|
354
|
+
return @{@"label": label, @"tip": tip, @"color": statusColor};
|
|
244
355
|
}
|
|
245
356
|
- (void)drawProvider:(NSString *)name usage:(NSDictionary *)usage usedKey:(NSString *)usedKey
|
|
246
357
|
color:(NSColor *)color cardRect:(NSRect)card {
|
|
@@ -306,12 +417,12 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
306
417
|
// 层级:官方剩余额度是订阅限额的权威值,占主位;本机 Token 是参考量,降一级并用
|
|
307
418
|
// 强调色区分;重置时间最小。反过来会让最抢眼的数字恰好是最不该照着做决定的那个。
|
|
308
419
|
NSArray<NSDictionary *> *windows = @[
|
|
309
|
-
@{@"x": @(NSMinX(card) + 214), @"title": @"5 小时窗口", @"quota": five ?: @{},
|
|
420
|
+
@{@"x": @(NSMinX(card) + 214), @"title": @"官方 5 小时窗口", @"quota": five ?: @{},
|
|
310
421
|
@"tokens": fiveTokens ?: @{}, @"used": fiveUsed ?: NSNull.null,
|
|
311
|
-
@"rolling": @"
|
|
312
|
-
@{@"x": @(NSMinX(card) + 394), @"title": @"7 天窗口", @"quota": week ?: @{},
|
|
422
|
+
@"rolling": @"无限制"},
|
|
423
|
+
@{@"x": @(NSMinX(card) + 394), @"title": @"官方 7 天窗口", @"quota": week ?: @{},
|
|
313
424
|
@"tokens": weekTokens ?: @{}, @"used": weekUsed ?: NSNull.null,
|
|
314
|
-
@"rolling": @"
|
|
425
|
+
@"rolling": @"无限制"}
|
|
315
426
|
];
|
|
316
427
|
for (NSDictionary *window in windows) {
|
|
317
428
|
CGFloat x = [window[@"x"] doubleValue];
|
|
@@ -339,24 +450,42 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
339
450
|
// 说清楚这一行的口径,比留一个 `--` 让人以为是渲染坏了要好。
|
|
340
451
|
NSString *resetValue = [self resetText:window[@"quota"]];
|
|
341
452
|
NSString *reset = [resetValue isEqualToString:@"--"]
|
|
342
|
-
? window[@"rolling"] : [NSString stringWithFormat:@"
|
|
453
|
+
? window[@"rolling"] : [NSString stringWithFormat:@"重置时间 %@", resetValue];
|
|
343
454
|
[reset drawInRect:NSMakeRect(x, NSMinY(card) + 22, 150, 18)
|
|
344
455
|
withAttributes:[self textAttributesWithSize:11 color:secondary weight:NSFontWeightRegular]];
|
|
345
456
|
}
|
|
346
457
|
|
|
347
458
|
CGFloat trendX = NSMinX(card) + 564;
|
|
348
|
-
[@"
|
|
459
|
+
[@"使用趋势" drawInRect:NSMakeRect(trendX, NSMinY(card) + 114, 145, 20)
|
|
349
460
|
withAttributes:[self textAttributesWithSize:13 color:secondary weight:NSFontWeightRegular]];
|
|
350
|
-
NSArray<
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
[
|
|
359
|
-
|
|
461
|
+
NSArray<NSDictionary *> *history = [name isEqualToString:@"Codex"]
|
|
462
|
+
? self.codexHistory : self.claudeHistory;
|
|
463
|
+
NSNumber *currentUsed = exhausted ? @100 : weekUsed;
|
|
464
|
+
NSArray<NSDictionary *> *points = [self curvePointsFromHistory:history currentUsed:currentUsed];
|
|
465
|
+
NSDictionary *pace = [self paceStatusForQuota:week currentUsed:currentUsed color:color];
|
|
466
|
+
NSColor *paceColor = pace[@"color"];
|
|
467
|
+
NSDictionary *pillAttributes = [self textAttributesWithSize:11 color:paceColor
|
|
468
|
+
weight:NSFontWeightSemibold];
|
|
469
|
+
CGFloat pillWidth = ceil([pace[@"label"] sizeWithAttributes:pillAttributes].width) + 18;
|
|
470
|
+
NSRect pill = NSMakeRect(trendX, NSMinY(card) + 88, pillWidth, 22);
|
|
471
|
+
[self fillRoundedRect:pill radius:11 color:[paceColor colorWithAlphaComponent:0.14]];
|
|
472
|
+
[pace[@"label"] drawInRect:NSInsetRect(pill, 9, 3) withAttributes:pillAttributes];
|
|
473
|
+
|
|
474
|
+
NSRect curveRect = NSMakeRect(trendX, NSMinY(card) + 43, 135, 38);
|
|
475
|
+
[self drawUsageCurve:points inRect:curveRect color:color];
|
|
476
|
+
double peak = 0;
|
|
477
|
+
for (NSDictionary *point in points) peak = fmax(peak, [point[@"used"] doubleValue]);
|
|
478
|
+
NSString *peakText = points.count > 0
|
|
479
|
+
? [NSString stringWithFormat:@"最高 %.0f%%", peak] : @"最高 --";
|
|
480
|
+
NSString *currentText = currentUsed
|
|
481
|
+
? [NSString stringWithFormat:@"当前 %.0f%%", currentUsed.doubleValue] : @"当前 --";
|
|
482
|
+
[peakText drawInRect:NSMakeRect(trendX, NSMinY(card) + 25, 72, 16)
|
|
483
|
+
withAttributes:[self textAttributesWithSize:10 color:secondary weight:NSFontWeightRegular]];
|
|
484
|
+
[currentText drawInRect:NSMakeRect(trendX + 66, NSMinY(card) + 25, 69, 16)
|
|
485
|
+
withAttributes:[self rightAlignedTextAttributesWithSize:10 color:secondary
|
|
486
|
+
weight:NSFontWeightRegular]];
|
|
487
|
+
[pace[@"tip"] drawInRect:NSMakeRect(trendX, NSMinY(card) + 7, 145, 16)
|
|
488
|
+
withAttributes:[self textAttributesWithSize:10 color:paceColor weight:NSFontWeightMedium]];
|
|
360
489
|
}
|
|
361
490
|
- (void)drawRect:(NSRect)dirtyRect {
|
|
362
491
|
[NSGraphicsContext saveGraphicsState];
|
|
@@ -399,7 +528,7 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
399
528
|
NSColor *agentColor = self.agentStatusColor;
|
|
400
529
|
[@"Agent Usage" drawInRect:NSMakeRect(28, QuotaLogicalHeight - 65, 260, 42)
|
|
401
530
|
withAttributes:[self textAttributesWithSize:30 color:primary weight:NSFontWeightBold]];
|
|
402
|
-
[@"
|
|
531
|
+
[@"实时状态与滚动用量" drawInRect:NSMakeRect(30, QuotaLogicalHeight - 94, 130, 22)
|
|
403
532
|
withAttributes:[self textAttributesWithSize:14 color:secondary weight:NSFontWeightRegular]];
|
|
404
533
|
[agentColor setFill];
|
|
405
534
|
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(174, QuotaLogicalHeight - 88, 9, 9)] fill];
|
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
6
7
|
|
|
7
8
|
const binary = process.argv[2];
|
|
8
9
|
if (!binary) {
|
|
@@ -165,9 +166,37 @@ const statusLineScript = resolveStatusLineScript(originalStatusLineCommand);
|
|
|
165
166
|
const statusLineInstalled = Boolean(
|
|
166
167
|
statusLineScript && installStatusLinePrelude(statusLineScript, createdStatusLine)
|
|
167
168
|
);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
|
|
170
|
+
// 就地往用户脚本里插 prelude 只对"绝对路径的可编辑脚本"成立。第三方状态栏基本都不是这个
|
|
171
|
+
// 形态(`bun x ccstatusline`、`npx …`、任何带参数或管道的命令行),resolveStatusLineScript
|
|
172
|
+
// 一律拒绝。以前这里只打印一行警告就算了,而 statusline 是 Claude 额度的唯一来源
|
|
173
|
+
// (转录 jsonl 和 stats-cache.json 里都没有 rate_limits),于是这类用户的额度永远是 `--`。
|
|
174
|
+
// 回退到包装器:command 换成 claude-statusline-with-pet + base64 编码的原命令,它先把 stdin
|
|
175
|
+
// 喂给 cc-pets --claude-usage,再原样转发给原命令执行。卸载侧本来就认这个形态(marker +
|
|
176
|
+
// 末尾的 base64),能原样还原回去。
|
|
177
|
+
const statusLineWrapper = path.resolve(
|
|
178
|
+
path.dirname(fileURLToPath(import.meta.url)), "..", "bin", "claude-statusline-with-pet");
|
|
179
|
+
let statusLineWrapped = false;
|
|
180
|
+
if (!statusLineInstalled && originalStatusLineCommand && fs.existsSync(statusLineWrapper)) {
|
|
181
|
+
try {
|
|
182
|
+
fs.chmodSync(statusLineWrapper, fs.statSync(statusLineWrapper).mode | 0o100);
|
|
183
|
+
} catch {
|
|
184
|
+
// 包内文件不可写(只读安装)时权限位通常本来就是对的,交给下面的可执行判断兜底。
|
|
185
|
+
}
|
|
186
|
+
if (fs.existsSync(statusLineWrapper)) {
|
|
187
|
+
const encoded = Buffer.from(originalStatusLineCommand, "utf8").toString("base64");
|
|
188
|
+
config.statusLine = {
|
|
189
|
+
...config.statusLine,
|
|
190
|
+
type: "command",
|
|
191
|
+
command: `${statusLineMarker} ${shellQuote(statusLineWrapper)} '${encoded}'`
|
|
192
|
+
};
|
|
193
|
+
statusLineWrapped = true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!statusLineInstalled && !statusLineWrapped) {
|
|
198
|
+
console.warn(`未接入 Claude status line:${originalStatusLineCommand || "(未配置)"}`);
|
|
199
|
+
console.warn("Claude Hooks 仍会正常工作,但桌宠拿不到 Claude 额度百分比(Token 用量不受影响)。");
|
|
171
200
|
}
|
|
172
201
|
|
|
173
202
|
const temporaryPath = `${settingsPath}.cc-pets.tmp`;
|
|
@@ -177,4 +206,6 @@ fs.chmodSync(settingsPath, 0o600);
|
|
|
177
206
|
console.log(`Claude Code Hooks 已安装到 ${settingsPath}`);
|
|
178
207
|
if (statusLineInstalled) {
|
|
179
208
|
console.log("Claude 额度采集已接入现有 status line。首次 API 响应后桌宠会显示额度。");
|
|
209
|
+
} else if (statusLineWrapped) {
|
|
210
|
+
console.log("Claude 额度采集已通过包装器接入 status line,原命令仍照常执行。首次 API 响应后桌宠会显示额度。");
|
|
180
211
|
}
|