cc-pets 1.4.8 → 2.0.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/CHANGELOG.md +41 -280
- package/CHANGELOG.zh-CN.md +64 -0
- package/CONTRIBUTING.md +18 -15
- package/CONTRIBUTING.zh-CN.md +22 -0
- package/LICENSE +1 -1
- package/PROVIDER_PROTOCOL.md +50 -41
- package/PROVIDER_PROTOCOL.zh-CN.md +58 -0
- package/README.md +221 -526
- package/README.zh-CN.md +601 -0
- package/Resources/phrases.default.txt +48 -49
- package/SECURITY.md +13 -10
- package/SECURITY.zh-CN.md +18 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +2 -11
- package/Sources/CCPets/CCPetsCodexRateLimits.h +13 -0
- package/Sources/CCPets/CCPetsCodexRateLimits.m +383 -0
- package/Sources/CCPets/CCPetsPhrases.h +3 -26
- package/Sources/CCPets/CCPetsPhrases.m +4 -85
- package/Sources/CCPets/CCPetsPhrasesEditor.m +22 -50
- package/Sources/CCPets/CCPetsQuotaHistory.m +4 -4
- package/Sources/CCPets/CCPetsUsage.m +135 -14
- package/Sources/CCPets/CCPetsUsageMonitor.m +41 -3
- package/Sources/CCPets/PetView.m +17 -10
- package/Sources/CCPets/QuotaDashboardView.m +23 -14
- package/THIRD_PARTY_NOTICES.md +13 -7
- package/THIRD_PARTY_NOTICES.zh-CN.md +12 -0
- package/package.json +14 -5
- package/scripts/build.sh +1 -1
- package/scripts/install-git-hooks.sh +2 -3
- package/scripts/pet-store.mjs +21 -2
|
@@ -118,26 +118,6 @@ NSString *PetPhrasesCurrentPetFilePath(void) {
|
|
|
118
118
|
return PetPhrasesFilePathForPetID(PetPhrasesCurrentPet);
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
NSString *PetPhrasesLegacyFilePath(void) {
|
|
122
|
-
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_PHRASES_LEGACY_FILE"];
|
|
123
|
-
if (override.length > 0) return override.stringByStandardizingPath;
|
|
124
|
-
return [NSHomeDirectory() stringByAppendingPathComponent:@".cc-pets/phrases.txt"];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
BOOL PetPhrasesLegacyFileExists(void) {
|
|
128
|
-
return [NSFileManager.defaultManager fileExistsAtPath:PetPhrasesLegacyFilePath()];
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
BOOL PetPhrasesArchiveLegacyFile(void) {
|
|
132
|
-
NSFileManager *manager = NSFileManager.defaultManager;
|
|
133
|
-
NSString *source = PetPhrasesLegacyFilePath();
|
|
134
|
-
if (![manager fileExistsAtPath:source]) return YES;
|
|
135
|
-
NSString *target = [source stringByAppendingPathExtension:@"bak"];
|
|
136
|
-
// 已经有一份更早的备份就顶掉:它比这份更旧,留着只会让人分不清哪份是哪份。
|
|
137
|
-
if ([manager fileExistsAtPath:target]) [manager removeItemAtPath:target error:nil];
|
|
138
|
-
return [manager moveItemAtPath:source toPath:target error:nil];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
121
|
NSString *PetPhrasesDefaultFilePath(void) {
|
|
142
122
|
NSString *override = NSProcessInfo.processInfo.environment[@"CC_PETS_PHRASES_DEFAULT_FILE"];
|
|
143
123
|
if (override.length > 0) return override.stringByStandardizingPath;
|
|
@@ -159,20 +139,10 @@ BOOL PetPhrasesEnsureFileExists(void) {
|
|
|
159
139
|
if (defaultText.length == 0) return NO;
|
|
160
140
|
[manager createDirectoryAtPath:path.stringByDeletingLastPathComponent
|
|
161
141
|
withIntermediateDirectories:YES attributes:nil error:nil];
|
|
162
|
-
|
|
163
|
-
// 老用户:启动时就把老词库合并过来,不等他打开编辑器。
|
|
164
|
-
//
|
|
165
|
-
// 拖到编辑器里再迁移的话,从升级到他第一次打开编辑器这段时间,桌宠说的会是出厂
|
|
166
|
-
// 台词而不是他自己写的——没人会把这件事和"我该去点一下编辑台词"联系起来。
|
|
167
|
-
// 老文件这一步不删,等编辑器里问过用户再删。
|
|
168
|
-
NSString *legacyText = [NSString stringWithContentsOfFile:PetPhrasesLegacyFilePath()
|
|
169
|
-
encoding:NSUTF8StringEncoding error:nil];
|
|
170
|
-
NSString *text = legacyText.length > 0 ?
|
|
171
|
-
PetPhraseTextMergedFromLegacy(legacyText, defaultText) : defaultText;
|
|
172
|
-
return [text writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
|
|
142
|
+
return [defaultText writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
|
|
173
143
|
}
|
|
174
144
|
|
|
175
|
-
//
|
|
145
|
+
// 标签清单。顺序就是默认模板里的顺序,校验提示按这个顺序报,
|
|
176
146
|
// 用户对着文件从上往下找得到。
|
|
177
147
|
NSArray<NSString *> *PetPhraseStateTags(void) {
|
|
178
148
|
static NSArray<NSString *> *tags;
|
|
@@ -514,7 +484,7 @@ static PetPhraseIssue *MakeIssue(NSInteger line, PetPhraseIssueLevel level, NSSt
|
|
|
514
484
|
// 所以直接拦下不给保存。
|
|
515
485
|
// 提示 —— 其余全部。情绪句整节没有是合法的(就是那个情境不吭声),
|
|
516
486
|
// 被丢掉的行也只影响自己,说清楚就行,不挡用户。
|
|
517
|
-
//
|
|
487
|
+
// 小节头写成默认词库里的样子:[tag] 补空格对齐到 24 列,再跟中文说明。
|
|
518
488
|
static NSString *SectionHeaderLine(NSString *tag) {
|
|
519
489
|
NSString *head = [NSString stringWithFormat:@"[%@]", tag];
|
|
520
490
|
NSMutableString *line = [head mutableCopy];
|
|
@@ -538,7 +508,7 @@ static BOOL IsHeaderLine(NSString *rawLine, NSString *tag) {
|
|
|
538
508
|
return [line isEqualToString:[NSString stringWithFormat:@"[%@]", tag]];
|
|
539
509
|
}
|
|
540
510
|
|
|
541
|
-
//
|
|
511
|
+
// 标签名或括号被删掉一部分时,默认小节头后面的中文说明仍能唯一标识它属于哪个情境:
|
|
542
512
|
// [click_hear] # 连续点击后开心
|
|
543
513
|
// click_heart] # 连续点击后开心
|
|
544
514
|
// [] # 连续点击后开心
|
|
@@ -552,57 +522,6 @@ static BOOL IsDamagedHeaderLine(NSString *rawLine, NSString *tag) {
|
|
|
552
522
|
return [comment isEqualToString:PetPhraseTagDescription(tag)];
|
|
553
523
|
}
|
|
554
524
|
|
|
555
|
-
NSString *PetPhraseTextMergedFromLegacy(NSString *legacyText, NSString *defaultText) {
|
|
556
|
-
if (legacyText.length == 0) return defaultText;
|
|
557
|
-
|
|
558
|
-
NSDictionary<NSString *, NSArray<NSString *> *> *legacy = ParsePhrasesText(legacyText, nil);
|
|
559
|
-
// 老文件里独立成行的 replace 表示"只说我写的"。这个写法在新版里已经没有了,
|
|
560
|
-
// 但迁移时必须认——不认的话,用过它的人升级后会突然多出一堆他当初特意关掉的话。
|
|
561
|
-
BOOL replaceMode = NO;
|
|
562
|
-
for (NSString *rawLine in [legacyText componentsSeparatedByCharactersInSet:
|
|
563
|
-
NSCharacterSet.newlineCharacterSet]) {
|
|
564
|
-
NSString *line = PetLineWithoutTrailingComment([rawLine
|
|
565
|
-
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]);
|
|
566
|
-
if ([line isEqualToString:@"replace"]) { replaceMode = YES; break; }
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
// 拿出厂模板当骨架,逐节把内容换掉。注释、分节说明全都用新模板的,
|
|
570
|
-
// 老文件里的注释一概不搬。
|
|
571
|
-
NSMutableArray<NSString *> *output = [NSMutableArray array];
|
|
572
|
-
NSArray<NSString *> *lines = [defaultText componentsSeparatedByString:@"\n"];
|
|
573
|
-
NSSet<NSString *> *known = [NSSet setWithArray:PetPhraseAllTags()];
|
|
574
|
-
NSString *current = nil;
|
|
575
|
-
for (NSString *rawLine in lines) {
|
|
576
|
-
NSString *line = PetLineWithoutTrailingComment([rawLine
|
|
577
|
-
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]);
|
|
578
|
-
BOOL isHeader = [line hasPrefix:@"["] && [line hasSuffix:@"]"] &&
|
|
579
|
-
[known containsObject:[line substringWithRange:NSMakeRange(1, line.length - 2)]];
|
|
580
|
-
if (isHeader) {
|
|
581
|
-
current = [line substringWithRange:NSMakeRange(1, line.length - 2)];
|
|
582
|
-
[output addObject:rawLine];
|
|
583
|
-
NSArray<NSString *> *userLines = legacy[current];
|
|
584
|
-
NSMutableArray<NSString *> *merged = [NSMutableArray array];
|
|
585
|
-
// 出厂在前、用户的追加在后,顺序稳定,用户一眼能看出哪几句是自己写的。
|
|
586
|
-
//
|
|
587
|
-
// replace 模式只覆盖用户**写过**的那些小节,没写过的仍旧用出厂内容。
|
|
588
|
-
// 照搬老语义(没写的一律清空)会让 15 个 state_ 小节全空:老版那些小节空了
|
|
589
|
-
// 还有一层兜底文案顶着,新版把兜底删了,状态卡副行会直接残掉。
|
|
590
|
-
if (!replaceMode || userLines.count == 0) {
|
|
591
|
-
[merged addObjectsFromArray:ParsePhrasesText(defaultText, nil)[current] ?: @[]];
|
|
592
|
-
}
|
|
593
|
-
for (NSString *text in userLines) {
|
|
594
|
-
if (![merged containsObject:text]) [merged addObject:text];
|
|
595
|
-
}
|
|
596
|
-
[output addObjectsFromArray:merged];
|
|
597
|
-
continue;
|
|
598
|
-
}
|
|
599
|
-
// 小节内的原有台词由上面一次性写完,这里跳过;注释和空行照抄,骨架才立得住。
|
|
600
|
-
if (current != nil && line.length > 0) continue;
|
|
601
|
-
[output addObject:rawLine];
|
|
602
|
-
}
|
|
603
|
-
return [output componentsJoinedByString:@"\n"];
|
|
604
|
-
}
|
|
605
|
-
|
|
606
525
|
// 找 tag 的小节头在第几行,没有返回 -1。
|
|
607
526
|
static NSInteger HeaderIndex(NSArray<NSString *> *lines, NSString *tag) {
|
|
608
527
|
for (NSUInteger index = 0; index < lines.count; index++) {
|
|
@@ -10,7 +10,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
10
10
|
@property NSButton *saveButton;
|
|
11
11
|
@property NSButton *saveAndCloseButton;
|
|
12
12
|
@property NSButton *revertButton;
|
|
13
|
-
//
|
|
13
|
+
// 只在专属页出现:把默认台词整份填进来,给空白页一个起点。
|
|
14
14
|
@property NSButton *fillButton;
|
|
15
15
|
// 编辑的是通用词库还是某只宠物的专属词库。
|
|
16
16
|
@property NSSegmentedControl *scopeControl;
|
|
@@ -45,34 +45,6 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
45
45
|
[NSApp activateIgnoringOtherApps:YES];
|
|
46
46
|
[controller.window makeKeyAndOrderFront:nil];
|
|
47
47
|
[controller.window makeFirstResponder:controller.textView];
|
|
48
|
-
[controller offerToRemoveLegacyFile];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 老词库在启动时已经合并进新文件了(见 PetPhrasesEnsureFileExists),但老文件还留着。
|
|
52
|
-
// 在这里问一次要不要收起来:合并是我们替用户做的,动他的文件必须他自己点头。
|
|
53
|
-
//
|
|
54
|
-
// 他选"先留着"的话下次打开还会问。留着原名不动有两个坑:改它不会有任何效果;
|
|
55
|
-
// 而且哪天 speech.txt 丢了,启动时会再从它合并一遍,把迁移之后写的台词全冲掉。
|
|
56
|
-
- (void)offerToRemoveLegacyFile {
|
|
57
|
-
if (!PetPhrasesLegacyFileExists()) return;
|
|
58
|
-
|
|
59
|
-
NSString *legacy = PetPhrasesLegacyFilePath();
|
|
60
|
-
NSAlert *alert = [NSAlert new];
|
|
61
|
-
alert.messageText = @"旧的台词文件已更新";
|
|
62
|
-
alert.informativeText = [NSString stringWithFormat:
|
|
63
|
-
@"正在使用的台词全部保留并合并到 %@。\n\n"
|
|
64
|
-
"旧文件 %@ 已经失效,后续修改不会有任何效果。"
|
|
65
|
-
"可以把它改名成 %@.bak 收起来,内容还留在原地。",
|
|
66
|
-
PetPhrasesFilePath(), legacy, legacy.lastPathComponent];
|
|
67
|
-
[alert addButtonWithTitle:@"改名收起来"];
|
|
68
|
-
[alert addButtonWithTitle:@"先留着"];
|
|
69
|
-
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
70
|
-
if (PetPhrasesArchiveLegacyFile()) {
|
|
71
|
-
[self showStatus:[NSString stringWithFormat:@"旧文件已改名成 %@.bak。",
|
|
72
|
-
legacy.lastPathComponent] warning:NO];
|
|
73
|
-
} else {
|
|
74
|
-
[self showStatus:@"旧文件改名失败,可以手动改。" warning:YES];
|
|
75
|
-
}
|
|
76
48
|
}
|
|
77
49
|
|
|
78
50
|
#pragma mark - 界面
|
|
@@ -152,7 +124,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
152
124
|
saveAndClose.translatesAutoresizingMaskIntoConstraints = NO;
|
|
153
125
|
self.saveAndCloseButton = saveAndClose;
|
|
154
126
|
|
|
155
|
-
NSButton *revert = [NSButton buttonWithTitle:@"
|
|
127
|
+
NSButton *revert = [NSButton buttonWithTitle:@"恢复默认台词…" target:self
|
|
156
128
|
action:@selector(revertToDefault:)];
|
|
157
129
|
revert.translatesAutoresizingMaskIntoConstraints = NO;
|
|
158
130
|
self.revertButton = revert;
|
|
@@ -166,9 +138,9 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
166
138
|
self.scopeControl = scope;
|
|
167
139
|
|
|
168
140
|
// 专属页初次打开是空白的——空白在语义上是对的(全部回落到通用),但一个空白文本框
|
|
169
|
-
//
|
|
141
|
+
// 不告诉任何人该往里写什么。这个按钮就是那个起点:把默认台词整份填进来,
|
|
170
142
|
// 用户在上面改字、删掉不想接管的小节即可。
|
|
171
|
-
NSButton *fill = [NSButton buttonWithTitle:@"
|
|
143
|
+
NSButton *fill = [NSButton buttonWithTitle:@"填入默认模板" target:self
|
|
172
144
|
action:@selector(fillFromDefault:)];
|
|
173
145
|
fill.translatesAutoresizingMaskIntoConstraints = NO;
|
|
174
146
|
fill.hidden = YES;
|
|
@@ -253,10 +225,10 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
253
225
|
}
|
|
254
226
|
|
|
255
227
|
- (void)syncRevertButtonTitle {
|
|
256
|
-
// 专属词库没有"
|
|
257
|
-
self.revertButton.title = self.petScope ? @"清空这一页…" : @"
|
|
258
|
-
//
|
|
259
|
-
//
|
|
228
|
+
// 专属词库没有"默认"这一说,它的默认状态就是空文件(全部回落到通用)。
|
|
229
|
+
self.revertButton.title = self.petScope ? @"清空这一页…" : @"恢复默认台词…";
|
|
230
|
+
// 通用页不需要这个按钮:它本来就是从默认台词拷出来的,不存在空白无从下手的问题,
|
|
231
|
+
// 想拿回默认内容有"恢复默认台词…"。
|
|
260
232
|
self.fillButton.hidden = !self.petScope;
|
|
261
233
|
}
|
|
262
234
|
|
|
@@ -307,7 +279,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
307
279
|
if (self.petScope && text.length == 0) {
|
|
308
280
|
[self showStatus:[NSString stringWithFormat:
|
|
309
281
|
@"%@ · 还是空的,这只宠物现在说的全是通用台词。"
|
|
310
|
-
"
|
|
282
|
+
"只写想改的小节即可,或点右上角「填入默认模板」看看能写些什么。", path]
|
|
311
283
|
warning:NO];
|
|
312
284
|
return;
|
|
313
285
|
}
|
|
@@ -493,14 +465,14 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
493
465
|
};
|
|
494
466
|
}
|
|
495
467
|
|
|
496
|
-
#pragma mark -
|
|
468
|
+
#pragma mark - 填入默认台词
|
|
497
469
|
|
|
498
470
|
// 给空白的专属页一个起点,同时**不破坏回落**。
|
|
499
471
|
//
|
|
500
|
-
//
|
|
472
|
+
// 直接填入完整的默认台词是错的:那样 22 个小节全都"存在",这只宠物从此每一节都自己
|
|
501
473
|
// 管,再也不会回落到通用词库——正好毁掉专属词库的全部用法。
|
|
502
474
|
//
|
|
503
|
-
//
|
|
475
|
+
// 所以填进来的是整份**注释状态**的默认台词。用户看得见每一节可以写什么(这才是空白页
|
|
504
476
|
// 真正缺的东西),而注释行在解析时一行都不算数,所以填完之后这只宠物说的仍然全是通用
|
|
505
477
|
// 台词,什么都没变。想让哪一节归它自己管,把那一节的 # 去掉再改字。
|
|
506
478
|
//
|
|
@@ -533,14 +505,14 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
533
505
|
- (void)fillFromDefault:(id)sender {
|
|
534
506
|
NSString *text = [self commentedDefaultText];
|
|
535
507
|
if (text.length == 0) {
|
|
536
|
-
[self showStatus:@"
|
|
508
|
+
[self showStatus:@"找不到默认台词文件。" warning:YES];
|
|
537
509
|
return;
|
|
538
510
|
}
|
|
539
511
|
// 空白页直接填,不打断——那正是这个按钮存在的理由。已经写了东西才问一句。
|
|
540
512
|
if ((self.textView.string ?: @"").length > 0) {
|
|
541
513
|
NSAlert *alert = [NSAlert new];
|
|
542
514
|
alert.messageText = @"覆盖这一页?";
|
|
543
|
-
alert.informativeText = @"
|
|
515
|
+
alert.informativeText = @"这一页现在的内容会被整份默认台词替换(全部是注释状态)。"
|
|
544
516
|
"还没保存,可以按 ⌘Z 撤销。";
|
|
545
517
|
[alert addButtonWithTitle:@"覆盖"];
|
|
546
518
|
[alert addButtonWithTitle:@"取消"];
|
|
@@ -551,20 +523,20 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
551
523
|
if (![self.textView shouldChangeTextInRange:all replacementString:text]) return;
|
|
552
524
|
[self.textView.textStorage replaceCharactersInRange:all withString:text];
|
|
553
525
|
[self.textView didChangeText];
|
|
554
|
-
[self showStatus:@"
|
|
526
|
+
[self showStatus:@"已填入默认台词,整份都是注释、还不生效。"
|
|
555
527
|
"把想改的那一节行首的 # 去掉,它才归这只宠物管。" warning:NO];
|
|
556
528
|
}
|
|
557
529
|
|
|
558
|
-
#pragma mark -
|
|
530
|
+
#pragma mark - 恢复默认 / 清空
|
|
559
531
|
|
|
560
532
|
- (void)revertToDefault:(id)sender {
|
|
561
533
|
NSAlert *alert = [NSAlert new];
|
|
562
|
-
// 专属词库的"
|
|
563
|
-
//
|
|
564
|
-
alert.messageText = self.petScope ? @"清空这只宠物的专属台词?" : @"
|
|
534
|
+
// 专属词库的"默认状态"就是空白:清空 = 这只宠物全部回落到通用台词。
|
|
535
|
+
// 拿默认词库去填它是错的——那等于把 22 个小节全部标成"专属接管",再也不会回落。
|
|
536
|
+
alert.messageText = self.petScope ? @"清空这只宠物的专属台词?" : @"恢复默认台词?";
|
|
565
537
|
alert.informativeText = self.petScope ?
|
|
566
538
|
@"清空之后,这只宠物说的全部回到通用台词。还没保存,可以按 ⌘Z 撤销。" :
|
|
567
|
-
@"
|
|
539
|
+
@"编辑器里的内容会被默认台词替换。还没保存,可以按 ⌘Z 撤销。";
|
|
568
540
|
[alert addButtonWithTitle:self.petScope ? @"清空" : @"恢复"];
|
|
569
541
|
[alert addButtonWithTitle:@"取消"];
|
|
570
542
|
if ([alert runModal] != NSAlertFirstButtonReturn) return;
|
|
@@ -574,7 +546,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
574
546
|
text = [NSString stringWithContentsOfFile:PetPhrasesDefaultFilePath()
|
|
575
547
|
encoding:NSUTF8StringEncoding error:nil];
|
|
576
548
|
if (text.length == 0) {
|
|
577
|
-
[self showStatus:@"
|
|
549
|
+
[self showStatus:@"找不到默认台词文件。" warning:YES];
|
|
578
550
|
return;
|
|
579
551
|
}
|
|
580
552
|
}
|
|
@@ -584,7 +556,7 @@ static void (^PetPhrasesSpeakHandler)(NSString *);
|
|
|
584
556
|
[self.textView.textStorage replaceCharactersInRange:all withString:text];
|
|
585
557
|
[self.textView didChangeText];
|
|
586
558
|
[self showStatus:self.petScope ? @"已清空,按保存才会写进文件。" :
|
|
587
|
-
@"
|
|
559
|
+
@"已填入默认台词,按保存才会写进文件。" warning:NO];
|
|
588
560
|
}
|
|
589
561
|
|
|
590
562
|
@end
|
|
@@ -23,11 +23,11 @@ NSDictionary *QuotaHistoryDocument(void) {
|
|
|
23
23
|
static NSDictionary *HistoryProviderSample(NSDictionary *usage, NSString *usedKey) {
|
|
24
24
|
NSNumber *fiveHour = RemainingValue(usage, @"fiveHour", usedKey);
|
|
25
25
|
NSNumber *week = RemainingValue(usage, @"week", usedKey);
|
|
26
|
-
//
|
|
27
|
-
//
|
|
26
|
+
// usage_limit_exceeded 没有窗口归属,既不能证明两个窗口都归零,也不能证明上次快照
|
|
27
|
+
// 仍代表当前额度。受限期间不写历史点,等任意会话拿到更新的官方快照后再继续记录。
|
|
28
28
|
if ([usage[@"exhaustedAt"] isKindOfClass:NSNumber.class]) {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
fiveHour = nil;
|
|
30
|
+
week = nil;
|
|
31
31
|
}
|
|
32
32
|
NSMutableDictionary *sample = [NSMutableDictionary dictionary];
|
|
33
33
|
if (fiveHour) sample[@"fiveHourRemaining"] = fiveHour;
|
|
@@ -15,14 +15,60 @@ static const double SevenDayWindowMinutes = 10080;
|
|
|
15
15
|
// 把一份本来正常的快照判死。
|
|
16
16
|
static const NSTimeInterval QuotaResetSkewSeconds = 600.0;
|
|
17
17
|
|
|
18
|
+
// 一条限额错误刚发生、手上却一个可用窗口都没有时的保留时长。见 ExhaustionStillHolds。
|
|
19
|
+
static const NSTimeInterval RecentExhaustionGraceSeconds = 300.0;
|
|
20
|
+
|
|
18
21
|
// 一份快照要么描述当前窗口,要么就没有任何用处:resets_at 落在过去说明这个窗口早就翻篇,
|
|
19
22
|
// 落在一个窗长之外则根本不可能是它的重置时刻。2026-08-19 实测被顶进来的坏快照正是后者
|
|
20
23
|
// ——5 小时窗口的 resets_at 报到了十几小时之后。两种都在入口就拒绝,别让它进文件。
|
|
21
|
-
static BOOL
|
|
24
|
+
static BOOL ResetLooksCurrentAt(double reset, double windowMinutes, NSTimeInterval sampledAt) {
|
|
25
|
+
if (reset <= 0 || windowMinutes <= 0) return NO;
|
|
26
|
+
return reset > sampledAt && reset <= sampledAt + windowMinutes * 60.0 + QuotaResetSkewSeconds;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static BOOL QuotaWindowLooksCurrentAt(NSDictionary *quota, double windowMinutes,
|
|
30
|
+
NSTimeInterval sampledAt) {
|
|
22
31
|
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class] ? quota[@"resets_at"] : nil;
|
|
23
|
-
if (!reset
|
|
24
|
-
|
|
25
|
-
|
|
32
|
+
if (!reset) return NO;
|
|
33
|
+
return ResetLooksCurrentAt(reset.doubleValue, windowMinutes, sampledAt);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static BOOL QuotaWindowLooksCurrent(NSDictionary *quota, double windowMinutes, NSTimeInterval now) {
|
|
37
|
+
return QuotaWindowLooksCurrentAt(quota, windowMinutes, now);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Codex 的 rate_limits 是一份会话快照,不会在窗口自然重置时自行刷新。一个快照里的
|
|
41
|
+
// 5 小时和 7 天窗口因此可能处于不同状态:例如 5 小时的 resets_at 已过去几天,但 7 天
|
|
42
|
+
// 的仍在未来。不能因为后者还活着,就把前者的旧百分比继续当成当前额度展示。
|
|
43
|
+
//
|
|
44
|
+
// 缺 resets_at 的旧版记录暂时保留,兼容历史会话和只提供百分比的上游版本;只要服务端给了
|
|
45
|
+
// 重置时间,就必须用它校验窗口。这个函数放在读取层而不是绘制层,状态栏、历史曲线和气泡
|
|
46
|
+
// 台词才能使用同一份已校正的数据。
|
|
47
|
+
static NSDictionary *UsageByRemovingInactiveQuotaWindows(NSDictionary *usage) {
|
|
48
|
+
if (![usage isKindOfClass:NSDictionary.class]) return usage;
|
|
49
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
50
|
+
NSDictionary<NSString *, NSNumber *> *windows = @{
|
|
51
|
+
@"fiveHour": @(FiveHourWindowMinutes),
|
|
52
|
+
@"week": @(SevenDayWindowMinutes)
|
|
53
|
+
};
|
|
54
|
+
NSMutableDictionary *result = nil;
|
|
55
|
+
NSMutableDictionary *expired = nil;
|
|
56
|
+
for (NSString *key in windows) {
|
|
57
|
+
NSDictionary *quota = [usage[key] isKindOfClass:NSDictionary.class] ? usage[key] : nil;
|
|
58
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class]
|
|
59
|
+
? quota[@"resets_at"] : nil;
|
|
60
|
+
if (!reset || QuotaWindowLooksCurrent(quota, windows[key].doubleValue, now)) continue;
|
|
61
|
+
if (!result) result = [usage mutableCopy];
|
|
62
|
+
result[key] = NSNull.null;
|
|
63
|
+
// 百分比不再展示,但 resets_at 必须留下。窗口一旦被清空,"这个窗口已经重置过"
|
|
64
|
+
// 就再也无从得知——而缺失的窗口本来就用 NSNull 表示(官方没给),两者混在一起
|
|
65
|
+
// 分不开。ExhaustionStillHolds 要靠这份记录让受限标记按时失效,否则只剩一个
|
|
66
|
+
// 未到期的 7 天窗口时,一次限流会让卡片永久挂着"额度受限"。
|
|
67
|
+
if (!expired) expired = [NSMutableDictionary dictionary];
|
|
68
|
+
expired[key] = reset;
|
|
69
|
+
}
|
|
70
|
+
if (expired) result[@"expiredWindowResets"] = expired;
|
|
71
|
+
return result ?: usage;
|
|
26
72
|
}
|
|
27
73
|
|
|
28
74
|
// 同一个账号下的每个 Claude 会话都会把自己那次响应里的 rate_limits 抄到同一个文件上,
|
|
@@ -343,10 +389,68 @@ static NSDictionary *UsageFromCodexLine(NSData *lineData) {
|
|
|
343
389
|
};
|
|
344
390
|
}
|
|
345
391
|
|
|
346
|
-
//
|
|
392
|
+
// 受限事件没有窗口归属,只能给它一个有效期:快照里最短窗口的 resets_at 一过,那个窗口
|
|
393
|
+
// 必然已经重置,再挂着"额度受限"就是在声称一件我们已经不知道的事——而受限期间官方常年
|
|
394
|
+
// 只回 primary/secondary 全 null(见 UsageFromCodexLine),等"更新的快照"来推翻可能等不到。
|
|
395
|
+
// 真正用尽的是 7 天窗口时,5 小时窗口重置后仍会被拒,届时会写下新的 usage_limit_exceeded,
|
|
396
|
+
// 标记自己会贴回来。
|
|
397
|
+
static BOOL ExhaustionStillHolds(NSDictionary *usage, NSTimeInterval exhaustedAt) {
|
|
398
|
+
if (![usage isKindOfClass:NSDictionary.class] || exhaustedAt <= 0) return NO;
|
|
399
|
+
if (exhaustedAt < [usage[@"sampledAt"] doubleValue]) return NO;
|
|
400
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
401
|
+
NSTimeInterval earliestReset = 0;
|
|
402
|
+
BOOL foundReset = NO;
|
|
403
|
+
BOOL foundQuotaWithoutReset = NO;
|
|
404
|
+
NSDictionary<NSString *, NSNumber *> *windows = @{
|
|
405
|
+
@"fiveHour": @(FiveHourWindowMinutes),
|
|
406
|
+
@"week": @(SevenDayWindowMinutes)
|
|
407
|
+
};
|
|
408
|
+
NSDictionary *expired = [usage[@"expiredWindowResets"] isKindOfClass:NSDictionary.class]
|
|
409
|
+
? usage[@"expiredWindowResets"] : nil;
|
|
410
|
+
for (NSString *key in windows) {
|
|
411
|
+
NSDictionary *quota = [usage[key] isKindOfClass:NSDictionary.class] ? usage[key] : nil;
|
|
412
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class] ? quota[@"resets_at"] : nil;
|
|
413
|
+
// 关键是“限额错误发生时”这个 reset 是否还属于当前窗口,而不是它现在是否已过期。
|
|
414
|
+
// 例如错误发生后 5 小时窗口自然重置,标记应当清掉;反过来,数天前就过期的
|
|
415
|
+
// 5 小时快照不能让一条刚发生的错误立即失效。
|
|
416
|
+
if (!quota) {
|
|
417
|
+
// 窗口被 UsageByRemovingInactiveQuotaWindows 清掉了,用它留下的 resets_at 继续
|
|
418
|
+
// 判断。少了这一支,5 小时窗口重置后标记就再也没有失效的依据。
|
|
419
|
+
NSNumber *expiredReset = [expired[key] isKindOfClass:NSNumber.class] ? expired[key] : nil;
|
|
420
|
+
if (!expiredReset) continue;
|
|
421
|
+
foundReset = YES;
|
|
422
|
+
double value = expiredReset.doubleValue;
|
|
423
|
+
if (!ResetLooksCurrentAt(value, windows[key].doubleValue, exhaustedAt)) continue;
|
|
424
|
+
if (earliestReset == 0 || value < earliestReset) earliestReset = value;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (!reset) {
|
|
428
|
+
foundQuotaWithoutReset = YES;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
foundReset = YES;
|
|
432
|
+
if (!QuotaWindowLooksCurrentAt(quota, windows[key].doubleValue, exhaustedAt)) continue;
|
|
433
|
+
double value = reset.doubleValue;
|
|
434
|
+
if (earliestReset == 0 || value < earliestReset) earliestReset = value;
|
|
435
|
+
}
|
|
436
|
+
if (earliestReset > now) return YES;
|
|
437
|
+
// 有一个属于当前窗口的 reset 且它已经过去,说明额度确实重置了,标记必须清掉。
|
|
438
|
+
if (earliestReset > 0) return NO;
|
|
439
|
+
// 走到这里说明一个可用窗口都没有,推不出有效期。这恰恰是最需要受限提示的时刻——
|
|
440
|
+
// 上面注释里说的"受限期间官方常年只回 primary/secondary 全 null"就落在这个分支,
|
|
441
|
+
// app-server 也拿不到实时额度时更是如此。所以刚发生的错误先无条件保留一小段时间,
|
|
442
|
+
// 别让面板在用户正撞限额的当口显示一切正常。
|
|
443
|
+
if (now - exhaustedAt < RecentExhaustionGraceSeconds) return YES;
|
|
444
|
+
// 老版会话只给百分比、没有 resets_at 时无法推断受限状态的有效期;沿用之前的保守
|
|
445
|
+
// 行为,等更新的额度快照来清掉它。只要至少有一个明确的 reset,就以它为准。
|
|
446
|
+
return !foundReset && foundQuotaWithoutReset;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// 受限事件仍然成立时,把标记贴到 usage 上。事件本身没有窗口归属,面板只能提示"额度受限"
|
|
450
|
+
// 并把已有百分比标为快照,不能据此把 5 小时和 7 天窗口都判成 0。
|
|
347
451
|
static NSDictionary *UsageByMarkingExhaustion(NSDictionary *usage, NSTimeInterval exhaustedAt) {
|
|
348
|
-
if (![usage isKindOfClass:NSDictionary.class]
|
|
349
|
-
if (
|
|
452
|
+
if (![usage isKindOfClass:NSDictionary.class]) return usage;
|
|
453
|
+
if (!ExhaustionStillHolds(usage, exhaustedAt)) return usage;
|
|
350
454
|
NSMutableDictionary *result = [usage mutableCopy];
|
|
351
455
|
result[@"exhaustedAt"] = @(exhaustedAt);
|
|
352
456
|
return result;
|
|
@@ -514,10 +618,13 @@ static NSData *SessionTailData(NSURL *url) {
|
|
|
514
618
|
|
|
515
619
|
static BOOL UsageHasLiveWindow(NSDictionary *usage) {
|
|
516
620
|
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
517
|
-
|
|
621
|
+
NSDictionary<NSString *, NSNumber *> *windows = @{
|
|
622
|
+
@"fiveHour": @(FiveHourWindowMinutes),
|
|
623
|
+
@"week": @(SevenDayWindowMinutes)
|
|
624
|
+
};
|
|
625
|
+
for (NSString *key in windows) {
|
|
518
626
|
NSDictionary *quota = [usage[key] isKindOfClass:NSDictionary.class] ? usage[key] : nil;
|
|
519
|
-
|
|
520
|
-
if (reset && reset.doubleValue > now) return YES;
|
|
627
|
+
if (QuotaWindowLooksCurrent(quota, windows[key].doubleValue, now)) return YES;
|
|
521
628
|
}
|
|
522
629
|
return NO;
|
|
523
630
|
}
|
|
@@ -1222,6 +1329,7 @@ NSDictionary *LatestUsage(void) {
|
|
|
1222
1329
|
NSNumber *exhaustedAt = nil;
|
|
1223
1330
|
NSDictionary *usage = LatestUsageInData(tail, &exhaustedAt)
|
|
1224
1331
|
?: LatestQuotaInRecentSessions(sessions, newest);
|
|
1332
|
+
usage = UsageByRemovingInactiveQuotaWindows(usage);
|
|
1225
1333
|
// --status 这类一次性调用不带缓存也不写归档:它只需要当下这一份数字。
|
|
1226
1334
|
usage = UsageByAddingTokenTotals(usage, sessions, newest ? @[newest] : nil,
|
|
1227
1335
|
[NSMutableDictionary dictionary],
|
|
@@ -1285,6 +1393,7 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1285
1393
|
}
|
|
1286
1394
|
- (NSDictionary *)usageWithTokenTotals:(NSDictionary *)usage {
|
|
1287
1395
|
if (![usage isKindOfClass:NSDictionary.class]) usage = @{};
|
|
1396
|
+
usage = UsageByRemovingInactiveQuotaWindows(usage);
|
|
1288
1397
|
NSString *identity = TokenWindowIdentity(usage);
|
|
1289
1398
|
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
1290
1399
|
BOOL reusable = !self.forcesTokenAggregation && self.tokenUsage &&
|
|
@@ -1293,7 +1402,7 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1293
1402
|
if (reusable) {
|
|
1294
1403
|
NSMutableDictionary *result = [usage mutableCopy];
|
|
1295
1404
|
result[@"tokenUsage"] = self.tokenUsage;
|
|
1296
|
-
return
|
|
1405
|
+
return [self usageByApplyingExhaustion:result];
|
|
1297
1406
|
}
|
|
1298
1407
|
BOOL archiveChanged = NO;
|
|
1299
1408
|
// 正在增量读取的会话永远排在钉住列表最前面:它是最不该被漏掉的那一个,而漏掉它
|
|
@@ -1311,12 +1420,24 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1311
1420
|
self.tokenWindowIdentity = identity;
|
|
1312
1421
|
self.tokenComputedAt = now;
|
|
1313
1422
|
self.forcesTokenAggregation = NO;
|
|
1314
|
-
return
|
|
1423
|
+
return [self usageByApplyingExhaustion:result];
|
|
1315
1424
|
}
|
|
1316
|
-
// 耗尽事件只往前走:额度恢复由"更新的额度快照"
|
|
1425
|
+
// 耗尽事件只往前走:额度恢复由"更新的额度快照"或窗口重置表达(见 ExhaustionStillHolds)。
|
|
1317
1426
|
- (void)noteExhaustion:(NSNumber *)timestamp {
|
|
1318
1427
|
if (timestamp.doubleValue > self.exhaustedAt) self.exhaustedAt = timestamp.doubleValue;
|
|
1319
1428
|
}
|
|
1429
|
+
// 标记失效时把记下的时刻一并清掉:留着它只会在下一轮再比一次同样的大小,而窗口一旦重置,
|
|
1430
|
+
// 这个时刻就永远不该再让任何快照被判成受限了。
|
|
1431
|
+
- (NSDictionary *)usageByApplyingExhaustion:(NSDictionary *)usage {
|
|
1432
|
+
if (![usage isKindOfClass:NSDictionary.class]) return usage;
|
|
1433
|
+
if (!ExhaustionStillHolds(usage, self.exhaustedAt)) {
|
|
1434
|
+
// 快照只会向前走(NewerUsageSnapshot),所以失效一次就是永久失效;真再撞上限流,
|
|
1435
|
+
// noteExhaustion: 会记下新的时刻。
|
|
1436
|
+
self.exhaustedAt = 0;
|
|
1437
|
+
return usage;
|
|
1438
|
+
}
|
|
1439
|
+
return UsageByMarkingExhaustion(usage, self.exhaustedAt);
|
|
1440
|
+
}
|
|
1320
1441
|
// 最近被写过的会话,最新的排在前面。并发会话不会太多,留 8 个足够覆盖一批 FSEvents;
|
|
1321
1442
|
// 更早的那些反正也会被目录扫描按 mtime 捞回来,钉住它们只是白读文件。
|
|
1322
1443
|
static const NSUInteger MaximumPinnedSessions = 8;
|
|
@@ -1495,7 +1616,7 @@ static const NSUInteger MaximumPinnedSessions = 8;
|
|
|
1495
1616
|
if (!chosen) chosen = currentSession;
|
|
1496
1617
|
// 一个候选都没选中就不会再走到 usageWithTokenTotals,刚记下的耗尽标记得在这里贴上。
|
|
1497
1618
|
if (!chosen) {
|
|
1498
|
-
self.usage =
|
|
1619
|
+
self.usage = [self usageByApplyingExhaustion:self.usage];
|
|
1499
1620
|
return self.usage;
|
|
1500
1621
|
}
|
|
1501
1622
|
if (![chosen.path isEqualToString:self.sessionURL.path]) [self selectSessionURL:chosen];
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#import "CCPetsUsageMonitor.h"
|
|
2
2
|
#import "CCPetsUsage.h"
|
|
3
|
+
#import "CCPetsCodexRateLimits.h"
|
|
3
4
|
#import "CCPetsPaths.h"
|
|
4
5
|
#import <CoreServices/CoreServices.h>
|
|
5
6
|
#import <fcntl.h>
|
|
@@ -14,9 +15,12 @@
|
|
|
14
15
|
@interface CCPetsUsageMonitor ()
|
|
15
16
|
@property dispatch_queue_t queue;
|
|
16
17
|
@property CodexUsageReader *codexReader;
|
|
18
|
+
@property CCPetsCodexRateLimitsReader *codexRateLimitsReader;
|
|
17
19
|
@property ClaudeUsageReader *claudeReader;
|
|
18
20
|
@property(readwrite) NSDictionary *codexUsage;
|
|
19
21
|
@property(readwrite) NSDictionary *claudeUsage;
|
|
22
|
+
@property NSDictionary *codexLiveUsage;
|
|
23
|
+
@property NSTimeInterval codexLiveUsageUpdatedAt;
|
|
20
24
|
@property FSEventStreamRef codexStream;
|
|
21
25
|
@property dispatch_source_t claudeSource;
|
|
22
26
|
@property unsigned long long claudeSize;
|
|
@@ -24,8 +28,13 @@
|
|
|
24
28
|
@property BOOL started;
|
|
25
29
|
- (void)publishChange;
|
|
26
30
|
- (void)startClaudeSource;
|
|
31
|
+
- (NSDictionary *)usageByApplyingLiveCodexUsage:(NSDictionary *)sessionUsage;
|
|
27
32
|
@end
|
|
28
33
|
|
|
34
|
+
// App Server 是服务端当前额度的权威源;会话日志只在成功响应落盘时才更新。短暂失败时
|
|
35
|
+
// 留出一小段缓存寿命,既不因一次网络抖动闪回旧日志,也不会把实时值永久钉在界面上。
|
|
36
|
+
static const NSTimeInterval CodexLiveUsageTTL = 5 * 60.0;
|
|
37
|
+
|
|
29
38
|
static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
30
39
|
void *clientCallBackInfo, size_t numEvents, void *eventPaths,
|
|
31
40
|
const FSEventStreamEventFlags eventFlags[],
|
|
@@ -54,11 +63,13 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
54
63
|
}
|
|
55
64
|
}
|
|
56
65
|
if (requiresFullDiscovery) {
|
|
57
|
-
monitor.codexUsage = [monitor
|
|
66
|
+
monitor.codexUsage = [monitor usageByApplyingLiveCodexUsage:
|
|
67
|
+
[monitor.codexReader refreshWithFullDiscovery]];
|
|
58
68
|
} else if (changedSessions.count > 0) {
|
|
59
69
|
NSMutableArray<NSURL *> *urls = [NSMutableArray arrayWithCapacity:changedSessions.count];
|
|
60
70
|
for (NSString *path in changedSessions) [urls addObject:[NSURL fileURLWithPath:path]];
|
|
61
|
-
monitor.codexUsage = [monitor
|
|
71
|
+
monitor.codexUsage = [monitor usageByApplyingLiveCodexUsage:
|
|
72
|
+
[monitor.codexReader refreshForSessionURLs:urls]];
|
|
62
73
|
}
|
|
63
74
|
if (requiresFullDiscovery || changedSessions.count > 0) [monitor publishChange];
|
|
64
75
|
}
|
|
@@ -70,13 +81,37 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
70
81
|
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
|
|
71
82
|
QOS_CLASS_UTILITY, 0));
|
|
72
83
|
_codexReader = [CodexUsageReader new];
|
|
84
|
+
_codexRateLimitsReader = [CCPetsCodexRateLimitsReader new];
|
|
73
85
|
_claudeReader = [ClaudeUsageReader new];
|
|
74
86
|
// 桌宠是唯一的常驻读取者,摘要缓存由它维护并落盘。
|
|
75
87
|
_codexReader.persistsCache = YES;
|
|
76
88
|
_claudeReader.persistsCache = YES;
|
|
89
|
+
__weak typeof(self) weakSelf = self;
|
|
90
|
+
_codexRateLimitsReader.liveUsageHandler = ^(NSDictionary *liveUsage) {
|
|
91
|
+
dispatch_async(weakSelf.queue, ^{
|
|
92
|
+
typeof(self) strongSelf = weakSelf;
|
|
93
|
+
if (!strongSelf || !strongSelf.started) return;
|
|
94
|
+
strongSelf.codexLiveUsage = liveUsage;
|
|
95
|
+
strongSelf.codexLiveUsageUpdatedAt = NSDate.date.timeIntervalSince1970;
|
|
96
|
+
strongSelf.codexUsage = [strongSelf usageByApplyingLiveCodexUsage:
|
|
97
|
+
strongSelf.codexReader.usage ?: strongSelf.codexUsage];
|
|
98
|
+
[strongSelf publishChange];
|
|
99
|
+
});
|
|
100
|
+
};
|
|
77
101
|
}
|
|
78
102
|
return self;
|
|
79
103
|
}
|
|
104
|
+
|
|
105
|
+
- (NSDictionary *)usageByApplyingLiveCodexUsage:(NSDictionary *)sessionUsage {
|
|
106
|
+
if (self.codexLiveUsage && NSDate.date.timeIntervalSince1970 -
|
|
107
|
+
self.codexLiveUsageUpdatedAt > CodexLiveUsageTTL) {
|
|
108
|
+
self.codexLiveUsage = nil;
|
|
109
|
+
self.codexLiveUsageUpdatedAt = 0;
|
|
110
|
+
}
|
|
111
|
+
return self.codexLiveUsage
|
|
112
|
+
? CodexUsageByApplyingLiveUsage(sessionUsage, self.codexLiveUsage)
|
|
113
|
+
: sessionUsage;
|
|
114
|
+
}
|
|
80
115
|
// 回调交给主线程:下游全是 UI(面板重排、气泡定位、系统通知)。
|
|
81
116
|
- (void)publishChange {
|
|
82
117
|
void (^handler)(NSDictionary *, NSDictionary *) = self.changeHandler;
|
|
@@ -171,7 +206,9 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
171
206
|
- (void)refreshOnQueue {
|
|
172
207
|
if (!self.started) return;
|
|
173
208
|
// 手动刷新、面板打开和 120 秒兜底都走全量路径,Token 聚合的节流只约束 FSEvents 热路径。
|
|
174
|
-
self.codexUsage = [self
|
|
209
|
+
self.codexUsage = [self usageByApplyingLiveCodexUsage:
|
|
210
|
+
[self.codexReader refreshWithFullDiscovery]];
|
|
211
|
+
[self.codexRateLimitsReader refresh];
|
|
175
212
|
self.claudeUsage = [self readClaudeUsageChanged:NO forcingAggregation:YES];
|
|
176
213
|
if (!self.codexStream) [self startCodexStream];
|
|
177
214
|
if (!self.claudeSource) [self startClaudeSource];
|
|
@@ -186,6 +223,7 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
186
223
|
dispatch_sync(self.queue, ^{ [unsafeSelf teardownSources]; });
|
|
187
224
|
}
|
|
188
225
|
- (void)teardownSources {
|
|
226
|
+
[self.codexRateLimitsReader stop];
|
|
189
227
|
if (self.codexStream) {
|
|
190
228
|
FSEventStreamStop(self.codexStream);
|
|
191
229
|
FSEventStreamInvalidate(self.codexStream);
|