cc-pets 2.4.0 → 2.6.0-beta.0
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/CC_BRIDGE.md +93 -14
- package/CC_BRIDGE.zh-CN.md +71 -11
- package/CHANGELOG.md +55 -0
- package/CHANGELOG.zh-CN.md +56 -1
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/PROVIDER_PROTOCOL.md +5 -1
- package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
- package/README.md +116 -34
- package/README.zh-CN.md +99 -35
- package/Resources/AgentIcons/antigravity.icns +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +99 -26
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +51 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +120 -17
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +24 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +24 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +21 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +33 -17
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +43 -7
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +7 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +7 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +24 -22
- package/Sources/CCPets/App/CCPetsAppDelegate.h +10 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.m +18 -8
- package/Sources/CCPets/App/CCPetsVersion.h +4 -0
- package/Sources/CCPets/App/CCPetsVersion.m +34 -0
- package/Sources/CCPets/App/main.m +11 -0
- package/Sources/CCPets/Bridge/CCPetsBridge.m +41 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeAntigravityApp.h +4 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeAntigravityApp.m +119 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +3 -1
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +272 -50
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +12 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +341 -95
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +227 -95
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +35 -5
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +310 -42
- package/Sources/CCPets/Bridge/CCPetsBridgeTerminalTitle.h +12 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeTerminalTitle.m +105 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityConfig.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityConfig.m +462 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityEvents.m +180 -0
- package/Sources/CCPets/Integrations/CCPetsBridgeTitleRelay.h +3 -0
- package/Sources/CCPets/Integrations/CCPetsBridgeTitleRelay.m +463 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +1 -1
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +38 -8
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +15 -5
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +2 -1
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +112 -81
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +11 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +217 -45
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/CCPetsPreviews.m +16 -8
- package/Sources/CCPets/Pet/PetView+Menu.m +64 -5
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsDoctor.m +126 -37
- package/Sources/CCPets/Support/CCPetsPaths.h +5 -0
- package/Sources/CCPets/Support/CCPetsPaths.m +15 -0
- package/Sources/CCPets/Support/CCPetsPreferences.h +14 -1
- package/Sources/CCPets/Support/CCPetsPreferences.m +36 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +533 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +98 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +7 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +205 -8
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +3 -1
- package/Sources/CCPets/UI/CCPetsUsagePage.h +4 -3
- package/Sources/CCPets/UI/CCPetsUsagePage.m +69 -43
- package/Sources/CCPets/UI/CCPetsUsageStore.m +11 -1
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +6 -4
- package/Sources/CCPets/Usage/CCPetsAntigravityQuota.m +115 -0
- package/Sources/CCPets/Usage/CCPetsNormalizedUsage.h +5 -0
- package/Sources/CCPets/Usage/CCPetsNormalizedUsage.m +43 -0
- package/Sources/CCPets/Usage/CCPetsProviderRegistry+Testing.h +13 -0
- package/Sources/CCPets/Usage/CCPetsProviderRegistry.h +25 -0
- package/Sources/CCPets/Usage/CCPetsProviderRegistry.m +94 -0
- package/Sources/CCPets/Usage/CCPetsQuotaHistory.h +1 -1
- package/Sources/CCPets/Usage/CCPetsQuotaHistory.m +11 -8
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +2 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +206 -14
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +4 -4
- package/Sources/CCPets/Usage/CCPetsUsage.h +7 -2
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +2 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +29 -4
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.h +4 -4
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +90 -7
- package/Sources/CCPets/Usage/QuotaDashboardView+CompactRow.m +81 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +5 -8
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +26 -4
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +99 -99
- package/Sources/CCPets/Usage/QuotaDashboardView.h +9 -10
- package/Sources/CCPets/Usage/QuotaDashboardView.m +232 -79
- package/THIRD_PARTY_NOTICES.md +5 -4
- package/THIRD_PARTY_NOTICES.zh-CN.md +6 -5
- package/bin/agy-with-pet +82 -0
- package/bin/cc-pets +1 -1
- package/bin/claude-with-pet +15 -0
- package/bin/codex-with-pet +17 -2
- package/package.json +5 -3
- package/scripts/install-shell-integration.sh +5 -0
|
@@ -57,6 +57,8 @@ NSData *SessionTailData(NSURL *url);
|
|
|
57
57
|
NSDictionary *LatestQuotaInRecentSessions(NSURL *sessionsURL, NSURL *skipURL);
|
|
58
58
|
|
|
59
59
|
// Token 窗口聚合、摘要缓存与月度归档(CCPetsUsage+TokenTotals.m)
|
|
60
|
+
unsigned long long EnumerateUsageLines(NSFileHandle *handle, BOOL includeTail,
|
|
61
|
+
void (^consume)(NSData *line));
|
|
60
62
|
BOOL IsPseudoModelName(NSString *model);
|
|
61
63
|
NSDictionary *CodexBackgroundModelDays(NSString *home, NSTimeInterval start, NSTimeInterval end);
|
|
62
64
|
NSDictionary *TokenSessionSummaryTracking(NSURL *url, NSMutableDictionary *cache,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#import "CCPetsUsage+Internal.h"
|
|
2
2
|
#import "CCPetsPaths.h"
|
|
3
|
+
#import "CCPetsProfiles.h"
|
|
3
4
|
|
|
4
5
|
#pragma mark - 按模型的用量
|
|
5
6
|
|
|
@@ -160,6 +161,121 @@ static NSDictionary<NSString *, NSDictionary *> *ClaudeModelDays(NSTimeInterval
|
|
|
160
161
|
return result;
|
|
161
162
|
}
|
|
162
163
|
|
|
164
|
+
static unsigned long long AntigravityCount(id value) {
|
|
165
|
+
return [value isKindOfClass:NSNumber.class] && [value doubleValue] >= 0
|
|
166
|
+
? [value unsignedLongLongValue] : 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static NSString *AntigravityBaseModelName(NSString *model) {
|
|
170
|
+
NSRange separator = [model rangeOfString:@"-" options:NSBackwardsSearch];
|
|
171
|
+
if (separator.location == NSNotFound || separator.location == 0) return model;
|
|
172
|
+
NSString *suffix = [model substringFromIndex:NSMaxRange(separator)];
|
|
173
|
+
return [@[@"low", @"medium", @"high", @"xhigh", @"max"] containsObject:suffix]
|
|
174
|
+
? [model substringToIndex:separator.location] : model;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static NSDictionary *AntigravityUsageByBaseModel(NSDictionary *models) {
|
|
178
|
+
NSMutableDictionary<NSString *, NSMutableDictionary *> *grouped = [NSMutableDictionary dictionary];
|
|
179
|
+
for (NSString *model in models) {
|
|
180
|
+
NSString *baseModel = AntigravityBaseModelName(model);
|
|
181
|
+
NSMutableDictionary *counts = grouped[baseModel] ?: (grouped[baseModel] = [NSMutableDictionary dictionary]);
|
|
182
|
+
AddModelCounts(counts, models[model]);
|
|
183
|
+
}
|
|
184
|
+
return grouped;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static NSDictionary *AntigravityTranscriptDays(NSURL *url) {
|
|
188
|
+
NSFileHandle *handle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
|
|
189
|
+
if (!handle) return nil;
|
|
190
|
+
NSMutableDictionary<NSNumber *, NSDictionary *> *requests = [NSMutableDictionary dictionary];
|
|
191
|
+
// The transcript stores the uncached input and cache reads as separate fields.
|
|
192
|
+
static const char modelSource[] = "\"MODEL\"";
|
|
193
|
+
NSData *marker = [NSData dataWithBytes:modelSource length:sizeof(modelSource) - 1];
|
|
194
|
+
@try {
|
|
195
|
+
EnumerateUsageLines(handle, YES, ^(NSData *line) {
|
|
196
|
+
@autoreleasepool {
|
|
197
|
+
if ([line rangeOfData:marker options:0 range:NSMakeRange(0, line.length)].location == NSNotFound) return;
|
|
198
|
+
id value = [NSJSONSerialization JSONObjectWithData:line options:0 error:nil];
|
|
199
|
+
if (![value isKindOfClass:NSDictionary.class]) return;
|
|
200
|
+
NSDictionary *record = value;
|
|
201
|
+
if (![record[@"source"] isEqual:@"MODEL"] || ![record[@"status"] isEqual:@"DONE"]) return;
|
|
202
|
+
if (![record[@"input_tokens"] isKindOfClass:NSNumber.class] &&
|
|
203
|
+
![record[@"cache_read_tokens"] isKindOfClass:NSNumber.class] &&
|
|
204
|
+
![record[@"cache_write_tokens"] isKindOfClass:NSNumber.class] &&
|
|
205
|
+
![record[@"output_tokens"] isKindOfClass:NSNumber.class]) return;
|
|
206
|
+
NSNumber *step = [record[@"step_index"] isKindOfClass:NSNumber.class] ? record[@"step_index"] : nil;
|
|
207
|
+
NSString *model = [record[@"model"] isKindOfClass:NSString.class] ? record[@"model"] : nil;
|
|
208
|
+
NSDate *date = ISO8601Timestamp(record[@"created_at"]);
|
|
209
|
+
if (!step || !date || IsPseudoModelName(model)) return;
|
|
210
|
+
unsigned long long output = AntigravityCount(record[@"output_tokens"]);
|
|
211
|
+
requests[step] = @{@"date": date, @"model": model,
|
|
212
|
+
@"counts": ModelCountsMake(AntigravityCount(record[@"input_tokens"]),
|
|
213
|
+
AntigravityCount(record[@"cache_read_tokens"]),
|
|
214
|
+
AntigravityCount(record[@"cache_write_tokens"]), output,
|
|
215
|
+
MIN(output, AntigravityCount(record[@"thinking_tokens"])), 1)};
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
} @catch (NSException *exception) {
|
|
219
|
+
return nil;
|
|
220
|
+
} @finally {
|
|
221
|
+
[handle closeFile];
|
|
222
|
+
}
|
|
223
|
+
NSCalendar *calendar = NSCalendar.currentCalendar;
|
|
224
|
+
NSMutableDictionary<NSString *, NSMutableDictionary *> *days = [NSMutableDictionary dictionary];
|
|
225
|
+
for (NSDictionary *request in requests.allValues) {
|
|
226
|
+
NSString *day = DayLabel([calendar startOfDayForDate:request[@"date"]].timeIntervalSince1970);
|
|
227
|
+
NSString *model = request[@"model"];
|
|
228
|
+
NSMutableDictionary *models = days[day] ?: (days[day] = [NSMutableDictionary dictionary]);
|
|
229
|
+
NSMutableDictionary *counts = models[model] ?: (models[model] = [NSMutableDictionary dictionary]);
|
|
230
|
+
AddModelCounts(counts, request[@"counts"]);
|
|
231
|
+
}
|
|
232
|
+
return days;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
static NSDictionary *AntigravityModelDays(NSTimeInterval start, NSTimeInterval end,
|
|
236
|
+
NSMutableDictionary *cache, NSURL *brainURL) {
|
|
237
|
+
NSFileManager *files = NSFileManager.defaultManager;
|
|
238
|
+
NSArray<NSURL *> *sessions = [files contentsOfDirectoryAtURL:brainURL
|
|
239
|
+
includingPropertiesForKeys:@[NSURLIsDirectoryKey, NSURLIsSymbolicLinkKey]
|
|
240
|
+
options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
|
|
241
|
+
NSString *first = DayLabel(start), *last = DayLabel(end);
|
|
242
|
+
NSString *timeZone = NSTimeZone.localTimeZone.name;
|
|
243
|
+
NSMutableSet<NSString *> *live = [NSMutableSet set];
|
|
244
|
+
NSMutableDictionary<NSString *, NSMutableDictionary *> *days = [NSMutableDictionary dictionary];
|
|
245
|
+
for (NSURL *session in sessions) {
|
|
246
|
+
@autoreleasepool {
|
|
247
|
+
NSDictionary *sessionInfo = [session resourceValuesForKeys:@[NSURLIsDirectoryKey, NSURLIsSymbolicLinkKey] error:nil];
|
|
248
|
+
if (![sessionInfo[NSURLIsDirectoryKey] boolValue] || [sessionInfo[NSURLIsSymbolicLinkKey] boolValue]) continue;
|
|
249
|
+
NSURL *url = [session URLByAppendingPathComponent:@".system_generated/logs/transcript.jsonl"];
|
|
250
|
+
NSDictionary *info = [files attributesOfItemAtPath:url.path error:nil];
|
|
251
|
+
NSDate *modified = info[NSFileModificationDate];
|
|
252
|
+
if (![info[NSFileType] isEqual:NSFileTypeRegular] || !modified || modified.timeIntervalSince1970 < start) continue;
|
|
253
|
+
[live addObject:url.path];
|
|
254
|
+
NSNumber *size = info[NSFileSize];
|
|
255
|
+
NSDictionary *entry = cache[url.path];
|
|
256
|
+
NSDictionary *transcriptDays = entry[@"days"];
|
|
257
|
+
if (![entry[@"modified"] isEqual:modified] || ![entry[@"size"] isEqual:size] ||
|
|
258
|
+
![entry[@"timeZone"] isEqual:timeZone] || ![transcriptDays isKindOfClass:NSDictionary.class]) {
|
|
259
|
+
transcriptDays = AntigravityTranscriptDays(url);
|
|
260
|
+
if (!transcriptDays) continue;
|
|
261
|
+
cache[url.path] = @{@"modified": modified, @"size": size, @"timeZone": timeZone, @"days": transcriptDays};
|
|
262
|
+
}
|
|
263
|
+
for (NSString *day in transcriptDays) {
|
|
264
|
+
if ([day compare:first] == NSOrderedAscending || [day compare:last] != NSOrderedAscending) continue;
|
|
265
|
+
NSMutableDictionary *models = days[day] ?: (days[day] = [NSMutableDictionary dictionary]);
|
|
266
|
+
for (NSString *model in transcriptDays[day]) {
|
|
267
|
+
NSMutableDictionary *counts = models[model] ?: (models[model] = [NSMutableDictionary dictionary]);
|
|
268
|
+
AddModelCounts(counts, transcriptDays[day][model]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
for (NSString *path in cache.allKeys) {
|
|
274
|
+
if (![live containsObject:path]) [cache removeObjectForKey:path];
|
|
275
|
+
}
|
|
276
|
+
return days;
|
|
277
|
+
}
|
|
278
|
+
|
|
163
279
|
// 历史按日留一份,因为源头会清:Claude Code 默认约 30 天后清理转录,那之后这些天只能靠这份。
|
|
164
280
|
// 每个已经过去的自然日,先比整天的 Token 总量:源头现在数出来的不比存下的少,说明源头没被清,
|
|
165
281
|
// 它就是一次完整的重算,整天换成它——同一份用量换了模型名(unknown 后来认出了模型)时,
|
|
@@ -179,6 +295,23 @@ static const NSInteger ModelArchiveSchemaVersion = 3;
|
|
|
179
295
|
// 再留几天余量。读取只取请求区间里的日子,留得更久也没有路径能读到。两边要一起改。
|
|
180
296
|
static const NSInteger ModelArchiveRetentionDays = 100;
|
|
181
297
|
|
|
298
|
+
static NSString *AntigravityBrainSubdirectory(NSString *provider) {
|
|
299
|
+
if ([provider isEqualToString:CCPetsProfileProviderAntigravity]) return @"antigravity-cli/brain";
|
|
300
|
+
if ([provider isEqualToString:@"antigravity-app"]) return @"antigravity/brain";
|
|
301
|
+
return nil;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
BOOL CCPetsAntigravityAppUsageAvailable(void) {
|
|
305
|
+
NSString *provider = @"antigravity-app";
|
|
306
|
+
NSString *home = DefaultAntigravityAppConfigDirectory().stringByStandardizingPath.stringByResolvingSymlinksInPath;
|
|
307
|
+
NSString *source = [home stringByAppendingPathComponent:AntigravityBrainSubdirectory(provider)];
|
|
308
|
+
NSString *archive = TokenSummaryCachePath([provider stringByAppendingString:@"-model-days"], source);
|
|
309
|
+
BOOL isDirectory = NO;
|
|
310
|
+
NSFileManager *files = NSFileManager.defaultManager;
|
|
311
|
+
return ([files fileExistsAtPath:source isDirectory:&isDirectory] && isDirectory) ||
|
|
312
|
+
[files fileExistsAtPath:archive];
|
|
313
|
+
}
|
|
314
|
+
|
|
182
315
|
// 版本 1 的归档不能直接丢:源头被清掉的那些天只剩这一份。把时间戳键换成日期就能接着用。
|
|
183
316
|
static NSMutableDictionary *LoadModelArchive(NSString *path, BOOL isClaude, BOOL *outMigrated) {
|
|
184
317
|
NSMutableDictionary *archive = LoadTokenSummaryCache(path);
|
|
@@ -229,32 +362,38 @@ static NSMutableDictionary *LoadModelArchive(NSString *path, BOOL isClaude, BOOL
|
|
|
229
362
|
}
|
|
230
363
|
|
|
231
364
|
NSDictionary *CCPetsModelUsage(NSString *provider, NSTimeInterval start, NSTimeInterval end) {
|
|
232
|
-
NSString *home =
|
|
233
|
-
|
|
365
|
+
NSString *home = DefaultCodexHomeDirectory();
|
|
366
|
+
if ([provider isEqualToString:CCPetsProfileProviderClaude]) home = DefaultClaudeConfigDirectory();
|
|
367
|
+
if ([provider isEqualToString:CCPetsProfileProviderAntigravity]) home = DefaultAntigravityConfigDirectory();
|
|
368
|
+
if ([provider isEqualToString:@"antigravity-app"]) home = DefaultAntigravityAppConfigDirectory();
|
|
234
369
|
return CCPetsModelUsageAtHome(provider, home, start, end);
|
|
235
370
|
}
|
|
236
371
|
|
|
237
|
-
NSDictionary *
|
|
372
|
+
static NSDictionary *ModelUsageAtHome(NSString *provider, NSString *home,
|
|
238
373
|
NSTimeInterval start, NSTimeInterval end) {
|
|
239
|
-
BOOL isClaude = [provider isEqualToString
|
|
240
|
-
|
|
374
|
+
BOOL isClaude = [provider isEqualToString:CCPetsProfileProviderClaude];
|
|
375
|
+
BOOL isCodex = [provider isEqualToString:CCPetsProfileProviderCodex];
|
|
376
|
+
NSString *antigravitySubdirectory = AntigravityBrainSubdirectory(provider);
|
|
377
|
+
if (!isClaude && !isCodex && !antigravitySubdirectory) return nil;
|
|
241
378
|
NSString *canonicalHome = home.stringByStandardizingPath;
|
|
379
|
+
if (canonicalHome.length == 0) return nil;
|
|
380
|
+
if ([provider isEqualToString:@"antigravity-app"]) canonicalHome = canonicalHome.stringByResolvingSymlinksInPath;
|
|
381
|
+
NSString *subdirectory = antigravitySubdirectory ?: (isClaude ? @"projects" : @"sessions");
|
|
242
382
|
NSURL *sourceURL = [NSURL fileURLWithPath:[canonicalHome stringByAppendingPathComponent:
|
|
243
|
-
|
|
383
|
+
subdirectory]];
|
|
244
384
|
NSString *source = sourceURL.path;
|
|
245
|
-
NSString *cachePath = TokenSummaryCachePath(
|
|
246
|
-
|
|
247
|
-
NSString *archivePath = TokenSummaryCachePath(
|
|
248
|
-
isClaude ? @"claude-model-days" : @"codex-model-days", source);
|
|
385
|
+
NSString *cachePath = TokenSummaryCachePath([provider stringByAppendingString:@"-models"], source);
|
|
386
|
+
NSString *archivePath = TokenSummaryCachePath([provider stringByAppendingString:@"-model-days"], source);
|
|
249
387
|
NSMutableDictionary *cache = LoadTokenSummaryCache(cachePath);
|
|
250
388
|
if (isClaude) MakeCachedRecordsMutable(cache);
|
|
251
389
|
BOOL archiveChanged = NO;
|
|
252
390
|
NSMutableDictionary *archive = LoadModelArchive(archivePath, isClaude, &archiveChanged);
|
|
253
391
|
|
|
254
|
-
NSDictionary<NSString *, NSDictionary *> *fresh
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
392
|
+
NSDictionary<NSString *, NSDictionary *> *fresh;
|
|
393
|
+
if (isClaude) fresh = ClaudeModelDays(start, end, cache, sourceURL);
|
|
394
|
+
else if (isCodex) fresh = CodexModelDays(start, end, cache, sourceURL);
|
|
395
|
+
else fresh = AntigravityModelDays(start, end, cache, sourceURL);
|
|
396
|
+
NSDictionary *backgroundDays = isCodex ? CodexBackgroundModelDays(canonicalHome, start, end) : @{};
|
|
258
397
|
|
|
259
398
|
NSMutableDictionary *archivedDays = [archive[@"days"] mutableCopy];
|
|
260
399
|
NSTimeInterval todayStart = [NSCalendar.currentCalendar
|
|
@@ -285,6 +424,9 @@ NSDictionary *CCPetsModelUsageAtHome(NSString *provider, NSString *home,
|
|
|
285
424
|
}
|
|
286
425
|
models = combined;
|
|
287
426
|
}
|
|
427
|
+
// Reconcile live and archived counts by the original IDs before grouping effort levels.
|
|
428
|
+
// When an old transcript disappears, the archive can still fill in just its missing variant.
|
|
429
|
+
if (antigravitySubdirectory) models = AntigravityUsageByBaseModel(models);
|
|
288
430
|
// 后台补录独立持久化,不进入取大保留的日归档。对应 rollout 迟到时可以撤销补录,
|
|
289
431
|
// 不会把它锁在历史最大值里,也不会影响未来主会话日志清理后的保留逻辑。
|
|
290
432
|
if ([backgroundDays[key] count] > 0) {
|
|
@@ -321,3 +463,53 @@ NSDictionary *CCPetsModelUsageAtHome(NSString *provider, NSString *home,
|
|
|
321
463
|
if (archiveChanged) SaveTokenSummaryCache(archive, archivePath);
|
|
322
464
|
return @{@"models": totals, @"daily": daily};
|
|
323
465
|
}
|
|
466
|
+
|
|
467
|
+
NSDictionary *CCPetsModelUsageAtHome(NSString *provider, NSString *home, NSTimeInterval start, NSTimeInterval end) {
|
|
468
|
+
if (AntigravityBrainSubdirectory(provider)) {
|
|
469
|
+
// The usage monitor and the Usage page both read Antigravity's model cache.
|
|
470
|
+
@synchronized (CCPetsProfileProviderAntigravity) {
|
|
471
|
+
return ModelUsageAtHome(provider, home, start, end);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return ModelUsageAtHome(provider, home, start, end);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
static NSDictionary *AntigravityTokenUsage(NSString *provider, NSString *configDirectory) {
|
|
478
|
+
NSCalendar *calendar = NSCalendar.currentCalendar;
|
|
479
|
+
NSDate *today = [calendar startOfDayForDate:NSDate.date];
|
|
480
|
+
NSDate *end = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:today options:0];
|
|
481
|
+
NSDate *start = [calendar dateByAddingUnit:NSCalendarUnitDay value:-90 toDate:end options:0];
|
|
482
|
+
NSDate *weekStart = [calendar dateByAddingUnit:NSCalendarUnitDay value:-6 toDate:today options:0];
|
|
483
|
+
NSDictionary *history = CCPetsModelUsageAtHome(provider, configDirectory,
|
|
484
|
+
start.timeIntervalSince1970, end.timeIntervalSince1970);
|
|
485
|
+
if (![history[@"models"] count]) return nil;
|
|
486
|
+
NSDictionary *windows = CalendarMonthWindows(end.timeIntervalSince1970);
|
|
487
|
+
NSMutableArray<NSDictionary *> *series = [NSMutableArray array];
|
|
488
|
+
NSMutableArray<NSDictionary *> *month = [NSMutableArray array];
|
|
489
|
+
for (NSDictionary *day in history[@"daily"]) {
|
|
490
|
+
NSMutableDictionary *counts = [NSMutableDictionary dictionary];
|
|
491
|
+
for (NSDictionary *model in [day[@"models"] allValues]) AddModelCounts(counts, model);
|
|
492
|
+
NSDictionary *tokens = @{@"timestamp": day[@"timestamp"], @"totals": @{
|
|
493
|
+
@"input_tokens": counts[@"input"] ?: @0, @"output_tokens": counts[@"output"] ?: @0,
|
|
494
|
+
@"cached_input_tokens": counts[@"cacheRead"] ?: @0, @"cache_write_input_tokens": counts[@"cacheWrite"] ?: @0,
|
|
495
|
+
@"request_count": counts[@"requests"] ?: @0, @"total_tokens": @(CCPetsTotalTokens(counts))}};
|
|
496
|
+
[series addObject:tokens];
|
|
497
|
+
if ([day[@"timestamp"] doubleValue] >= [windows[@"monthStart"] doubleValue]) [month addObject:tokens];
|
|
498
|
+
}
|
|
499
|
+
return @{@"today": SummedDailyTokenSeriesInRange(series, today.timeIntervalSince1970, end.timeIntervalSince1970),
|
|
500
|
+
@"recentWeek": SummedDailyTokenSeriesInRange(series, weekStart.timeIntervalSince1970, end.timeIntervalSince1970),
|
|
501
|
+
@"recentWeekStartsAt": @(weekStart.timeIntervalSince1970), @"month": SummedDailyTokenSeries(month),
|
|
502
|
+
@"monthComparable": SummedDailyTokenSeriesInRange(month, [windows[@"monthStart"] doubleValue],
|
|
503
|
+
[windows[@"monthComparableEnd"] doubleValue]),
|
|
504
|
+
@"previousMonthToDate": SummedDailyTokenSeriesInRange(series, [windows[@"previousMonthStart"] doubleValue],
|
|
505
|
+
[windows[@"previousMonthEnd"] doubleValue]), @"monthDaily": month,
|
|
506
|
+
@"monthComparableEndsAt": windows[@"monthComparableEnd"]};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
NSDictionary *AntigravityTokenUsageForConfigDirectory(NSString *configDirectory) {
|
|
510
|
+
return AntigravityTokenUsage(CCPetsProfileProviderAntigravity, configDirectory);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
NSDictionary *AntigravityAppTokenUsageForConfigDirectory(NSString *configDirectory) {
|
|
514
|
+
return AntigravityTokenUsage(@"antigravity-app", configDirectory);
|
|
515
|
+
}
|
|
@@ -11,7 +11,7 @@ static NSTimeInterval LineTimestamp(NSData *line) {
|
|
|
11
11
|
// 按块切行喂给 consume,返回消费掉的字节数(截止最后一个换行)。返回值就是下一次可以
|
|
12
12
|
// 安全续读的偏移:结尾那半行还没写完,不能算进去。includeTail=YES 时残行照样交给
|
|
13
13
|
// consume(一次性整篇扫描的调用方要它,最后一行未必有换行结尾)。
|
|
14
|
-
|
|
14
|
+
unsigned long long EnumerateUsageLines(NSFileHandle *handle, BOOL includeTail,
|
|
15
15
|
void (^consume)(NSData *)) {
|
|
16
16
|
NSMutableData *pending = [NSMutableData data];
|
|
17
17
|
unsigned long long consumed = 0;
|
|
@@ -181,7 +181,7 @@ NSDictionary *TokenSessionSummaryTracking(NSURL *url, NSMutableDictionary *cache
|
|
|
181
181
|
NSFileHandle *handle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
|
|
182
182
|
if (!handle) return nil;
|
|
183
183
|
if (offset > 0) [handle seekToFileOffset:offset];
|
|
184
|
-
unsigned long long consumed =
|
|
184
|
+
unsigned long long consumed = EnumerateUsageLines(handle, NO, ^(NSData *line) {
|
|
185
185
|
// 会话起点取文件第一条带 timestamp 的行,不限于 token_count:它决定这个会话算不算
|
|
186
186
|
// 整段落在窗口内。找到之后就不再对普通行做 JSON 解析。
|
|
187
187
|
if (!start) {
|
|
@@ -307,7 +307,7 @@ static NSDictionary *PartialTokenTotals(NSURL *url, NSTimeInterval start, NSTime
|
|
|
307
307
|
baseline = nil;
|
|
308
308
|
inside = nil;
|
|
309
309
|
};
|
|
310
|
-
|
|
310
|
+
EnumerateUsageLines(handle, YES, ^(NSData *line) {
|
|
311
311
|
NSDictionary *record = TokenRecordFromLine(line);
|
|
312
312
|
NSDictionary *total = record[@"total"];
|
|
313
313
|
if (!total) return;
|
|
@@ -603,7 +603,7 @@ NSMutableDictionary *LoadMonthlyUsageArchive(NSString *path) {
|
|
|
603
603
|
// heap-buffer-overflow, WRITE of size 48, 0 bytes after 6640-byte region
|
|
604
604
|
// 分配点 __CFBinaryPlistCreateObjectFiltered <- LoadTokenSummaryCache
|
|
605
605
|
// 越界写破坏堆之后进程不会当场死,而是在之后任意一次内存操作上倒下,因此崩溃点看起来
|
|
606
|
-
// 和这里毫无关系(实际报告里落在
|
|
606
|
+
// 和这里毫无关系(实际报告里落在 EnumerateUsageLines 和 SaveTokenSummaryCache 上)。
|
|
607
607
|
//
|
|
608
608
|
// 可靠的做法只有一条:落盘缓存一读回来就把 records 换成本进程创建的可变副本,
|
|
609
609
|
// 之后 ClaudeRecordsForFile 的原地追加才成立。只替换 records,其余字段原样保留——
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
#import <Foundation/Foundation.h>
|
|
2
2
|
|
|
3
|
-
//
|
|
3
|
+
// Agent CLI 的额度读取。
|
|
4
4
|
int RecordClaudeUsage(void);
|
|
5
5
|
NSDictionary *ClaudeRateLimits(void);
|
|
6
6
|
NSDictionary *ClaudeRateLimitsForConfigDirectory(NSString *configDirectory);
|
|
7
7
|
NSDictionary *LatestClaudeUsage(void);
|
|
8
8
|
NSDictionary *LatestUsage(void);
|
|
9
|
+
NSDictionary *AntigravityRateLimitsForConfigDirectory(NSString *configDirectory);
|
|
10
|
+
NSDictionary *AntigravityTokenUsageForConfigDirectory(NSString *configDirectory);
|
|
11
|
+
NSDictionary *AntigravityAppTokenUsageForConfigDirectory(NSString *configDirectory);
|
|
12
|
+
BOOL CCPetsAntigravityAppUsageAvailable(void);
|
|
9
13
|
// 清掉 resets_at 已经翻篇(或不可能属于当前窗口)的额度窗口,缺 resets_at 的保留。
|
|
10
14
|
NSDictionary *UsageByRemovingExpiredQuotaWindows(NSDictionary *usage);
|
|
11
15
|
|
|
@@ -38,7 +42,8 @@ NSDictionary *UsageByRemovingExpiredQuotaWindows(NSDictionary *usage);
|
|
|
38
42
|
- (NSDictionary *)refreshForSessionURLs:(NSArray<NSURL *> *)urls;
|
|
39
43
|
@end
|
|
40
44
|
|
|
41
|
-
// 按模型的用量,provider 为 "claude"
|
|
45
|
+
// 按模型的用量,provider 为 "claude"、"codex"、"antigravity"(CLI)或 "antigravity-app"。
|
|
46
|
+
// 统计 [start, end) 之间的自然日
|
|
42
47
|
// (start / end 应是日零点),数据来自本机会话日志,不联网。会读整段区间的会话文件,首次可能耗时
|
|
43
48
|
// 数百毫秒到数秒,只在后台队列调用。结果:
|
|
44
49
|
// models: @{模型名: counts} 区间合计
|
|
@@ -8,3 +8,5 @@ NSString *CCPetsProfileAuthenticationMode(NSDictionary *profile);
|
|
|
8
8
|
NSString *CCPetsUsageModePreferenceKey(NSDictionary *profile);
|
|
9
9
|
BOOL CCPetsProfileShowsAPIUsage(NSDictionary *profile);
|
|
10
10
|
BOOL CCPetsProfileCanSwitchUsageMode(NSDictionary *profile);
|
|
11
|
+
BOOL CCPetsAntigravityUsesGeminiAPI(NSString *configDirectory);
|
|
12
|
+
BOOL CCPetsAntigravityQuotaReportEnabled(NSString *configDirectory);
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#import "CCPetsCLISupport.h"
|
|
3
3
|
#import "CCPetsJSON.h"
|
|
4
4
|
#import "CCPetsPaths.h"
|
|
5
|
+
#import "CCPetsProfiles.h"
|
|
5
6
|
|
|
6
7
|
NSNotificationName const CCPetsAuthenticationModesDidChangeNotification = @"CCPetsAuthenticationModesDidChange";
|
|
7
8
|
static NSMutableDictionary<NSString *, NSDictionary *> *AuthenticationModes;
|
|
@@ -18,6 +19,21 @@ static NSDictionary *ReadJSONObject(NSString *path) {
|
|
|
18
19
|
return [object isKindOfClass:NSDictionary.class] ? object : nil;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
static NSDictionary *AntigravitySettings(NSString *configDirectory) {
|
|
23
|
+
return configDirectory.length > 0 ? ReadJSONObject(
|
|
24
|
+
[configDirectory stringByAppendingPathComponent:@"antigravity-cli/settings.json"]) : nil;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
BOOL CCPetsAntigravityUsesGeminiAPI(NSString *configDirectory) {
|
|
28
|
+
return [AntigravitySettings(configDirectory)[@"modelProvider"] isEqual:@"gemini"];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
BOOL CCPetsAntigravityQuotaReportEnabled(NSString *configDirectory) {
|
|
32
|
+
NSDictionary *settings = AntigravitySettings(configDirectory);
|
|
33
|
+
// A disabled slash command can turn /usage into an ordinary model prompt.
|
|
34
|
+
return ![settings[@"modelProvider"] isEqual:@"gemini"] && ![settings[@"disableSlashCommands"] isEqual:@YES];
|
|
35
|
+
}
|
|
36
|
+
|
|
21
37
|
static NSString *ClaudeConfiguredMode(NSString *home) {
|
|
22
38
|
id settings = CCPetsReadConfig([home stringByAppendingPathComponent:@"settings.json"], NULL);
|
|
23
39
|
if (![settings isKindOfClass:CCPetsJSONObject.class]) return @"unknown";
|
|
@@ -61,6 +77,9 @@ static BOOL CodexHasCustomProvider(NSString *home) {
|
|
|
61
77
|
static NSString *ProbeAuthenticationMode(NSDictionary *profile, NSString *searchPath) {
|
|
62
78
|
NSString *provider = profile[@"provider"], *home = profile[@"home"];
|
|
63
79
|
if (!CCPetsDirectoryExists(home)) return @"unknown";
|
|
80
|
+
if ([provider isEqualToString:CCPetsProfileProviderAntigravity]) {
|
|
81
|
+
return CCPetsAntigravityUsesGeminiAPI(home) ? @"api" : @"unknown";
|
|
82
|
+
}
|
|
64
83
|
BOOL claude = [provider isEqualToString:@"claude"];
|
|
65
84
|
NSString *fallback = claude ? ClaudeConfiguredMode(home) : @"unknown";
|
|
66
85
|
if (!claude) {
|
|
@@ -124,7 +143,9 @@ void CCPetsRefreshProfileAuthenticationModes(NSArray<NSDictionary *> *profiles)
|
|
|
124
143
|
NSDictionary *previous;
|
|
125
144
|
@synchronized(CCPetsAuthenticationModesDidChangeNotification) { previous = AuthenticationModes[key]; }
|
|
126
145
|
if (previous && now - [previous[@"checkedAt"] doubleValue] < 60) continue;
|
|
127
|
-
if (!searchPath
|
|
146
|
+
if (!searchPath && ![profile[@"provider"] isEqualToString:CCPetsProfileProviderAntigravity]) {
|
|
147
|
+
searchPath = CCPetsLoginShellPath() ?: @"";
|
|
148
|
+
}
|
|
128
149
|
NSString *mode = ProbeAuthenticationMode(profile, searchPath);
|
|
129
150
|
@synchronized(CCPetsAuthenticationModesDidChangeNotification) {
|
|
130
151
|
if (!AuthenticationModes) AuthenticationModes = [NSMutableDictionary dictionary];
|
|
@@ -143,10 +164,15 @@ NSString *CCPetsProfileAuthenticationMode(NSDictionary *profile) {
|
|
|
143
164
|
}
|
|
144
165
|
}
|
|
145
166
|
|
|
167
|
+
static NSString *ProviderUsageModePreferenceKey(NSString *provider) {
|
|
168
|
+
if ([provider isEqualToString:CCPetsProfileProviderAntigravity]) return AntigravityUsageDisplayModeKey;
|
|
169
|
+
return [provider isEqualToString:CCPetsProfileProviderCodex] ? CodexUsageDisplayModeKey : ClaudeUsageDisplayModeKey;
|
|
170
|
+
}
|
|
171
|
+
|
|
146
172
|
NSString *CCPetsUsageModePreferenceKey(NSDictionary *profile) {
|
|
147
173
|
NSString *profileID = profile[@"id"];
|
|
148
174
|
if (profileID.length > 0) return [@"CCPetsUsageDisplayMode." stringByAppendingString:profileID];
|
|
149
|
-
return
|
|
175
|
+
return ProviderUsageModePreferenceKey(profile[@"provider"]);
|
|
150
176
|
}
|
|
151
177
|
|
|
152
178
|
BOOL CCPetsProfileCanSwitchUsageMode(NSDictionary *profile) {
|
|
@@ -157,7 +183,6 @@ BOOL CCPetsProfileShowsAPIUsage(NSDictionary *profile) {
|
|
|
157
183
|
if (!CCPetsProfileCanSwitchUsageMode(profile)) return YES;
|
|
158
184
|
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
159
185
|
NSString *mode = [defaults stringForKey:CCPetsUsageModePreferenceKey(profile)];
|
|
160
|
-
if (!mode) mode = [defaults stringForKey:
|
|
161
|
-
? CodexUsageDisplayModeKey : ClaudeUsageDisplayModeKey];
|
|
186
|
+
if (!mode) mode = [defaults stringForKey:ProviderUsageModePreferenceKey(profile[@"provider"])];
|
|
162
187
|
return [mode isEqualToString:@"api"];
|
|
163
188
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#import <Foundation/Foundation.h>
|
|
2
2
|
|
|
3
3
|
@interface CCPetsUsageMonitor : NSObject
|
|
4
|
-
@property(copy) void (^changeHandler)(NSDictionary
|
|
5
|
-
@property(readonly) NSDictionary *
|
|
6
|
-
|
|
7
|
-
//
|
|
4
|
+
@property(copy) void (^changeHandler)(NSDictionary<NSString *, NSDictionary *> *usageByProvider);
|
|
5
|
+
@property(readonly, copy) NSDictionary<NSString *, NSDictionary *> *usageByProvider;
|
|
6
|
+
// profileId → 当前用量;profiles 保持用户添加顺序。默认账号同时保留在 usageByProvider 中。
|
|
7
|
+
// 默认 Antigravity 账号的 appTokenUsage 单独记录 App 的本机用量。
|
|
8
8
|
@property(readonly) NSDictionary<NSString *, NSDictionary *> *profileUsages;
|
|
9
9
|
@property(readonly) NSArray<NSDictionary *> *profiles;
|
|
10
10
|
- (void)start;
|
|
@@ -17,17 +17,22 @@
|
|
|
17
17
|
// 会让桌宠窗口迟迟不出现、切换展示模式时整个界面卡死。
|
|
18
18
|
@interface CCPetsUsageMonitor ()
|
|
19
19
|
@property dispatch_queue_t queue;
|
|
20
|
+
@property dispatch_queue_t antigravityQueue;
|
|
20
21
|
@property CodexUsageReader *codexReader;
|
|
21
22
|
@property CCPetsCodexRateLimitsReader *codexRateLimitsReader;
|
|
22
23
|
@property ClaudeUsageReader *claudeReader;
|
|
23
|
-
@property(readwrite) NSDictionary *
|
|
24
|
-
|
|
24
|
+
@property(readwrite, copy) NSDictionary<NSString *, NSDictionary *> *usageByProvider;
|
|
25
|
+
// Reader 内部仍使用具名入口,外部统一读取 usageByProvider。
|
|
26
|
+
@property NSDictionary *codexUsage;
|
|
27
|
+
@property NSDictionary *claudeUsage;
|
|
25
28
|
@property(readwrite) NSDictionary<NSString *, NSDictionary *> *profileUsages;
|
|
26
29
|
@property(readwrite) NSArray<NSDictionary *> *profiles;
|
|
27
30
|
@property NSMutableDictionary<NSString *, ClaudeUsageReader *> *claudeProfileReaders;
|
|
28
31
|
@property NSMutableDictionary<NSString *, CodexUsageReader *> *codexProfileReaders;
|
|
29
32
|
@property NSMutableDictionary<NSString *, CCPetsCodexRateLimitsReader *> *codexProfileRateReaders;
|
|
30
33
|
@property NSMutableDictionary<NSString *, NSDictionary *> *codexProfileLiveUsages;
|
|
34
|
+
@property NSMutableDictionary<NSString *, NSDictionary *> *antigravityProfileUsages;
|
|
35
|
+
@property NSMutableDictionary<NSString *, NSObject *> *antigravityPendingRequests;
|
|
31
36
|
@property NSMutableDictionary<NSString *, NSString *> *profileReaderHomes;
|
|
32
37
|
@property NSDictionary *codexLiveUsage;
|
|
33
38
|
@property FSEventStreamRef codexStream;
|
|
@@ -45,6 +50,12 @@
|
|
|
45
50
|
// 窗口翻篇由 resets_at 兜底。只有"额度受限"标记有寿命——它没有窗口归属,实时查询连续
|
|
46
51
|
// 失败时不能让一次旧的受限结论一直挂着。
|
|
47
52
|
static const NSTimeInterval CodexLiveUsageTTL = 5 * 60.0;
|
|
53
|
+
static const NSTimeInterval AntigravityQuotaUsageTTL = 5 * 60.0;
|
|
54
|
+
|
|
55
|
+
static BOOL AntigravityHomeMatches(NSString *home, NSString *other) {
|
|
56
|
+
return [home.stringByStandardizingPath.stringByResolvingSymlinksInPath isEqualToString:
|
|
57
|
+
other.stringByStandardizingPath.stringByResolvingSymlinksInPath];
|
|
58
|
+
}
|
|
48
59
|
|
|
49
60
|
// 默认账号、自定义 Home、缓存恢复和实时回调共用同一条合并路径。
|
|
50
61
|
// 用采样时间计龄,轮询旧缓存不会延长受限标记的寿命;窗口百分比仍按新旧快照择优。
|
|
@@ -132,11 +143,23 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
132
143
|
}
|
|
133
144
|
|
|
134
145
|
@implementation CCPetsUsageMonitor
|
|
146
|
+
- (NSDictionary *)codexUsage { return self.usageByProvider[@"Codex"]; }
|
|
147
|
+
- (NSDictionary *)claudeUsage { return self.usageByProvider[@"Claude"]; }
|
|
148
|
+
- (void)setCodexUsage:(NSDictionary *)usage { [self setUsage:usage forProvider:@"Codex"]; }
|
|
149
|
+
- (void)setClaudeUsage:(NSDictionary *)usage { [self setUsage:usage forProvider:@"Claude"]; }
|
|
150
|
+
- (void)setUsage:(NSDictionary *)usage forProvider:(NSString *)name {
|
|
151
|
+
NSMutableDictionary *updated = [self.usageByProvider mutableCopy] ?: [NSMutableDictionary dictionary];
|
|
152
|
+
updated[name] = usage;
|
|
153
|
+
self.usageByProvider = updated;
|
|
154
|
+
}
|
|
135
155
|
- (instancetype)init {
|
|
136
156
|
if ((self = [super init])) {
|
|
157
|
+
_usageByProvider = @{};
|
|
137
158
|
_queue = dispatch_queue_create("com.universewang.cc-pets.usage",
|
|
138
159
|
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
|
|
139
160
|
QOS_CLASS_UTILITY, 0));
|
|
161
|
+
_antigravityQueue = dispatch_queue_create("com.universewang.cc-pets.usage.antigravity",
|
|
162
|
+
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0));
|
|
140
163
|
_codexReader = [CodexUsageReader new];
|
|
141
164
|
_codexRateLimitsReader = [CCPetsCodexRateLimitsReader new];
|
|
142
165
|
_claudeReader = [ClaudeUsageReader new];
|
|
@@ -144,6 +167,8 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
144
167
|
_codexProfileReaders = [NSMutableDictionary dictionary];
|
|
145
168
|
_codexProfileRateReaders = [NSMutableDictionary dictionary];
|
|
146
169
|
_codexProfileLiveUsages = [NSMutableDictionary dictionary];
|
|
170
|
+
_antigravityProfileUsages = [NSMutableDictionary dictionary];
|
|
171
|
+
_antigravityPendingRequests = [NSMutableDictionary dictionary];
|
|
147
172
|
_profileReaderHomes = [NSMutableDictionary dictionary];
|
|
148
173
|
// 桌宠是唯一的常驻读取者,摘要缓存由它维护并落盘。
|
|
149
174
|
_codexReader.persistsCache = YES;
|
|
@@ -173,11 +198,10 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
173
198
|
// 回调交给主线程:下游全是 UI(面板重排、气泡定位、系统通知)。
|
|
174
199
|
- (void)publishChange {
|
|
175
200
|
[self syncDefaultProfileUsages];
|
|
176
|
-
void (^handler)(NSDictionary *, NSDictionary *) = self.changeHandler;
|
|
201
|
+
void (^handler)(NSDictionary<NSString *, NSDictionary *> *) = self.changeHandler;
|
|
177
202
|
if (!handler) return;
|
|
178
|
-
NSDictionary *
|
|
179
|
-
|
|
180
|
-
dispatch_async(dispatch_get_main_queue(), ^{ handler(codex, claude); });
|
|
203
|
+
NSDictionary<NSString *, NSDictionary *> *usages = self.usageByProvider;
|
|
204
|
+
dispatch_async(dispatch_get_main_queue(), ^{ handler(usages); });
|
|
181
205
|
}
|
|
182
206
|
|
|
183
207
|
// 默认账号的卡片读的是 profileUsages,但 FSEvents、statusline 监听和 App Server 回调
|
|
@@ -206,12 +230,58 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
206
230
|
}
|
|
207
231
|
if (changed) self.profileUsages = usages;
|
|
208
232
|
}
|
|
233
|
+
|
|
234
|
+
- (void)requestAntigravityUsageForProfile:(NSString *)profileID home:(NSString *)home {
|
|
235
|
+
if (self.antigravityPendingRequests[profileID]) return;
|
|
236
|
+
NSObject *request = [NSObject new];
|
|
237
|
+
self.antigravityPendingRequests[profileID] = request;
|
|
238
|
+
dispatch_queue_t destination = self.queue;
|
|
239
|
+
BOOL defaultCLI = AntigravityHomeMatches(home, DefaultAntigravityConfigDirectory());
|
|
240
|
+
BOOL includesApp = AntigravityHomeMatches(home, DefaultAntigravityAppConfigDirectory());
|
|
241
|
+
__weak typeof(self) weakSelf = self;
|
|
242
|
+
dispatch_async(self.antigravityQueue, ^{
|
|
243
|
+
NSDictionary *quota = AntigravityRateLimitsForConfigDirectory(home);
|
|
244
|
+
NSDictionary *tokens = AntigravityTokenUsageForConfigDirectory(home);
|
|
245
|
+
NSDictionary *appTokens = includesApp && CCPetsAntigravityAppUsageAvailable()
|
|
246
|
+
? AntigravityAppTokenUsageForConfigDirectory(home) : nil;
|
|
247
|
+
dispatch_async(destination, ^{
|
|
248
|
+
typeof(self) strongSelf = weakSelf;
|
|
249
|
+
if (!strongSelf || !strongSelf.started ||
|
|
250
|
+
strongSelf.antigravityPendingRequests[profileID] != request) return;
|
|
251
|
+
[strongSelf.antigravityPendingRequests removeObjectForKey:profileID];
|
|
252
|
+
if (![strongSelf.profileReaderHomes[profileID] isEqualToString:home]) return;
|
|
253
|
+
|
|
254
|
+
NSDictionary *effectiveQuota = quota;
|
|
255
|
+
NSDictionary *previous = strongSelf.antigravityProfileUsages[profileID];
|
|
256
|
+
if (!quota && !CCPetsAntigravityUsesGeminiAPI(home) && [previous[@"sampledAt"] isKindOfClass:NSNumber.class] &&
|
|
257
|
+
NSDate.date.timeIntervalSince1970 - [previous[@"sampledAt"] doubleValue] < AntigravityQuotaUsageTTL) {
|
|
258
|
+
effectiveQuota = UsageByRemovingExpiredQuotaWindows(previous);
|
|
259
|
+
}
|
|
260
|
+
NSMutableDictionary *snapshot = [effectiveQuota mutableCopy] ?: [NSMutableDictionary dictionary];
|
|
261
|
+
snapshot[@"tokenUsage"] = tokens;
|
|
262
|
+
snapshot[@"appTokenUsage"] = appTokens;
|
|
263
|
+
NSDictionary *usage = snapshot.count > 0 ? [snapshot copy] : nil;
|
|
264
|
+
strongSelf.antigravityProfileUsages[profileID] = usage;
|
|
265
|
+
NSMutableDictionary *updated = [strongSelf.profileUsages mutableCopy];
|
|
266
|
+
updated[profileID] = usage;
|
|
267
|
+
strongSelf.profileUsages = updated;
|
|
268
|
+
if (defaultCLI) [strongSelf setUsage:usage forProvider:@"Antigravity"];
|
|
269
|
+
if (includesApp) {
|
|
270
|
+
[strongSelf setUsage:appTokens ? @{@"tokenUsage": appTokens} : nil forProvider:@"Antigravity App"];
|
|
271
|
+
}
|
|
272
|
+
[strongSelf publishChange];
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
209
277
|
- (void)refreshProfileUsages {
|
|
210
278
|
NSArray<NSDictionary *> *profiles = CCPetsProfiles();
|
|
211
279
|
NSMutableDictionary<NSString *, NSDictionary *> *usages = [NSMutableDictionary dictionary];
|
|
212
280
|
NSMutableSet<NSString *> *liveIDs = [NSMutableSet set];
|
|
213
281
|
NSString *defaultClaude = DefaultClaudeConfigDirectory().stringByStandardizingPath;
|
|
214
282
|
NSString *defaultCodex = DefaultCodexHomeDirectory().stringByStandardizingPath;
|
|
283
|
+
NSDictionary *defaultAntigravityUsage = nil;
|
|
284
|
+
NSDictionary *appTokens = nil;
|
|
215
285
|
for (NSDictionary *profile in profiles) {
|
|
216
286
|
NSString *profileID = profile[@"id"], *provider = profile[@"provider"];
|
|
217
287
|
NSString *home = [profile[@"home"] stringByStandardizingPath];
|
|
@@ -224,6 +294,8 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
224
294
|
[self.codexProfileRateReaders[profileID] stop];
|
|
225
295
|
[self.codexProfileRateReaders removeObjectForKey:profileID];
|
|
226
296
|
[self.codexProfileLiveUsages removeObjectForKey:profileID];
|
|
297
|
+
[self.antigravityProfileUsages removeObjectForKey:profileID];
|
|
298
|
+
[self.antigravityPendingRequests removeObjectForKey:profileID];
|
|
227
299
|
}
|
|
228
300
|
self.profileReaderHomes[profileID] = home;
|
|
229
301
|
NSDictionary *usage = nil;
|
|
@@ -271,6 +343,11 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
271
343
|
}
|
|
272
344
|
[rateReader refresh];
|
|
273
345
|
}
|
|
346
|
+
} else if ([provider isEqualToString:CCPetsProfileProviderAntigravity]) {
|
|
347
|
+
usage = self.antigravityProfileUsages[profileID];
|
|
348
|
+
if (AntigravityHomeMatches(home, DefaultAntigravityConfigDirectory())) defaultAntigravityUsage = usage;
|
|
349
|
+
if (AntigravityHomeMatches(home, DefaultAntigravityAppConfigDirectory())) appTokens = usage[@"appTokenUsage"];
|
|
350
|
+
[self requestAntigravityUsageForProfile:profileID home:home];
|
|
274
351
|
}
|
|
275
352
|
if (usage) usages[profileID] = usage;
|
|
276
353
|
}
|
|
@@ -287,8 +364,13 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
287
364
|
}
|
|
288
365
|
}
|
|
289
366
|
for (NSString *profileID in self.profileReaderHomes.allKeys.copy) {
|
|
290
|
-
if (
|
|
367
|
+
if ([liveIDs containsObject:profileID]) continue;
|
|
368
|
+
[self.profileReaderHomes removeObjectForKey:profileID];
|
|
369
|
+
[self.antigravityProfileUsages removeObjectForKey:profileID];
|
|
370
|
+
[self.antigravityPendingRequests removeObjectForKey:profileID];
|
|
291
371
|
}
|
|
372
|
+
[self setUsage:defaultAntigravityUsage forProvider:@"Antigravity"];
|
|
373
|
+
[self setUsage:appTokens ? @{@"tokenUsage": appTokens} : nil forProvider:@"Antigravity App"];
|
|
292
374
|
self.profiles = profiles;
|
|
293
375
|
self.profileUsages = usages;
|
|
294
376
|
}
|
|
@@ -424,6 +506,7 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
424
506
|
dispatch_source_cancel(self.claudeSource);
|
|
425
507
|
self.claudeSource = nil;
|
|
426
508
|
}
|
|
509
|
+
[self.antigravityPendingRequests removeAllObjects];
|
|
427
510
|
}
|
|
428
511
|
- (void)dealloc {
|
|
429
512
|
[self stop];
|