cc-pets 2.5.0 → 2.6.0-beta.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/CC_BRIDGE.md +47 -5
- package/CC_BRIDGE.zh-CN.md +42 -5
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/README.md +48 -19
- package/README.zh-CN.md +46 -17
- package/Resources/AgentIcons/antigravity.icns +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +64 -15
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +7 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +11 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +16 -5
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +11 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +21 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +27 -16
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +43 -7
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +2 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +3 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +20 -22
- package/Sources/CCPets/App/CCPetsAppDelegate.m +14 -6
- 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 +7 -5
- 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 +172 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +25 -7
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +17 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +7 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +92 -11
- 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/CCPetsIntegrations.h +1 -1
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +37 -7
- 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 +5 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +163 -40
- package/Sources/CCPets/Pet/CCPetsPreviews.m +16 -8
- package/Sources/CCPets/Pet/PetView+Menu.m +56 -5
- 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 +4 -1
- package/Sources/CCPets/Support/CCPetsPreferences.m +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +10 -5
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +9 -6
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +3 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +189 -8
- 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 +1 -1
- 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
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#import "CCPetsUsage+Internal.h"
|
|
2
|
+
#import "CCPetsCLISupport.h"
|
|
3
|
+
#import "CCPetsPaths.h"
|
|
4
|
+
#import "CCPetsUsageMode.h"
|
|
5
|
+
#import <math.h>
|
|
6
|
+
|
|
7
|
+
static NSDictionary *JSONObject(NSData *data) {
|
|
8
|
+
id value = data.length ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
9
|
+
return [value isKindOfClass:NSDictionary.class] ? value : nil;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static NSString *WindowKey(id value) {
|
|
13
|
+
if (![value isKindOfClass:NSString.class]) return nil;
|
|
14
|
+
NSString *label = [[[value lowercaseString] componentsSeparatedByCharactersInSet:
|
|
15
|
+
NSCharacterSet.alphanumericCharacterSet.invertedSet] componentsJoinedByString:@""];
|
|
16
|
+
for (NSString *term in @[@"rolling", @"quota", @"window", @"limit", @"period"]) {
|
|
17
|
+
label = [label stringByReplacingOccurrencesOfString:term withString:@""];
|
|
18
|
+
}
|
|
19
|
+
if ([@[@"5h", @"5hour", @"5hours", @"fivehour", @"fivehours", @"300m"] containsObject:label]) return @"fiveHour";
|
|
20
|
+
if ([@[@"week", @"weekly", @"1week", @"7d", @"7day", @"7days", @"sevenday", @"sevendays", @"168h"] containsObject:label]) return @"week";
|
|
21
|
+
return nil;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static NSDictionary *UsageFromAntigravityQuotaReport(NSDictionary *report) {
|
|
25
|
+
if (![report[@"status"] isEqual:@"SUCCESS"]) return nil;
|
|
26
|
+
NSDictionary *command = [report[@"command"] isKindOfClass:NSDictionary.class] ? report[@"command"] : nil;
|
|
27
|
+
if (![command[@"name"] isEqual:@"usage"] && ![command[@"name"] isEqual:@"quota"]) return nil;
|
|
28
|
+
NSDictionary *data = [command[@"data"] isKindOfClass:NSDictionary.class] ? command[@"data"] : nil;
|
|
29
|
+
NSArray *groups = [data[@"groups"] isKindOfClass:NSArray.class] ? data[@"groups"] : nil;
|
|
30
|
+
if (!groups) return nil;
|
|
31
|
+
|
|
32
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
33
|
+
NSMutableDictionary *usage = [@{@"sampledAt": @(now)} mutableCopy];
|
|
34
|
+
NSMutableArray *quotaGroups = [NSMutableArray array];
|
|
35
|
+
for (id value in groups) {
|
|
36
|
+
if (![value isKindOfClass:NSDictionary.class]) continue;
|
|
37
|
+
NSDictionary *group = value;
|
|
38
|
+
NSString *name = [group[@"name"] isKindOfClass:NSString.class] ? group[@"name"] : nil;
|
|
39
|
+
NSArray *buckets = [group[@"buckets"] isKindOfClass:NSArray.class] ? group[@"buckets"] : nil;
|
|
40
|
+
if (!name || !buckets) continue;
|
|
41
|
+
NSMutableArray *clean = [NSMutableArray array];
|
|
42
|
+
for (id candidate in buckets) {
|
|
43
|
+
if (![candidate isKindOfClass:NSDictionary.class]) continue;
|
|
44
|
+
NSDictionary *bucket = candidate;
|
|
45
|
+
NSMutableDictionary *details = [NSMutableDictionary dictionary];
|
|
46
|
+
for (NSString *field in @[@"id", @"name", @"description", @"window", @"reset_time"]) {
|
|
47
|
+
if ([bucket[field] isKindOfClass:NSString.class]) details[field] = bucket[field];
|
|
48
|
+
}
|
|
49
|
+
for (NSString *field in @[@"remaining_fraction", @"remaining_amount", @"disabled"]) {
|
|
50
|
+
if ([bucket[field] isKindOfClass:NSNumber.class]) details[field] = bucket[field];
|
|
51
|
+
}
|
|
52
|
+
[clean addObject:details];
|
|
53
|
+
NSString *key = WindowKey(details[@"window"]) ?: WindowKey(details[@"name"]);
|
|
54
|
+
NSNumber *remaining = details[@"remaining_fraction"];
|
|
55
|
+
if (!key || !remaining || !isfinite(remaining.doubleValue) || [details[@"disabled"] boolValue]) continue;
|
|
56
|
+
if (remaining.doubleValue < 0 || remaining.doubleValue > 1) continue;
|
|
57
|
+
NSDate *reset = ISO8601Timestamp(details[@"reset_time"]);
|
|
58
|
+
if (reset && reset.timeIntervalSince1970 <= now) continue;
|
|
59
|
+
double used = (1.0 - remaining.doubleValue) * 100.0;
|
|
60
|
+
// The overview and menu bar show the most constrained bucket in each window.
|
|
61
|
+
if (usage[key] && [usage[key][@"used_percent"] doubleValue] >= used) continue;
|
|
62
|
+
NSMutableDictionary *window = [@{@"used_percent": @(used), @"group": name,
|
|
63
|
+
@"window_minutes": [key isEqual:@"week"] ? @10080 : @300} mutableCopy];
|
|
64
|
+
if (reset) window[@"resets_at"] = @(reset.timeIntervalSince1970);
|
|
65
|
+
usage[key] = window;
|
|
66
|
+
}
|
|
67
|
+
[quotaGroups addObject:@{@"name": name, @"buckets": clean}];
|
|
68
|
+
}
|
|
69
|
+
usage[@"quotaGroups"] = quotaGroups;
|
|
70
|
+
return usage;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
NSDictionary *AntigravityRateLimitsForConfigDirectory(NSString *configDirectory) {
|
|
74
|
+
if (configDirectory.length == 0 || !CCPetsAntigravityQuotaReportEnabled(configDirectory)) return nil;
|
|
75
|
+
NSString *onboardingPath = [configDirectory stringByAppendingPathComponent:@"antigravity-cli/cache/onboarding.json"];
|
|
76
|
+
NSDictionary *onboarding = JSONObject([NSData dataWithContentsOfFile:onboardingPath]);
|
|
77
|
+
if (![onboarding[@"onboardingComplete"] isEqual:@YES] &&
|
|
78
|
+
![onboarding[@"consumerOnboardingComplete"] isEqual:@YES] &&
|
|
79
|
+
![onboarding[@"enterpriseOnboardingComplete"] isEqual:@YES]) return nil;
|
|
80
|
+
|
|
81
|
+
NSString *searchPath = CCPetsEnvironmentValue(@"PATH");
|
|
82
|
+
NSString *executable = CCPetsFindRealExecutableInPath(@"agy", @"AGY_REAL_BIN", searchPath);
|
|
83
|
+
if (!executable) {
|
|
84
|
+
searchPath = CCPetsLoginShellPath();
|
|
85
|
+
executable = CCPetsFindRealExecutableInPath(@"agy", @"AGY_REAL_BIN", searchPath);
|
|
86
|
+
}
|
|
87
|
+
if (!executable) return nil;
|
|
88
|
+
NSMutableArray *arguments = [NSMutableArray array];
|
|
89
|
+
NSString *standardRoot = [CCPetsHomeDirectory() stringByAppendingPathComponent:@".gemini"];
|
|
90
|
+
if (![configDirectory.stringByStandardizingPath isEqual:standardRoot.stringByStandardizingPath]) {
|
|
91
|
+
// This launcher flag was observed in agy 1.5.12. A version that doesn't recognize
|
|
92
|
+
// it fails before the query; avoid falling back to the default profile's quota.
|
|
93
|
+
[arguments addObject:[@"--gemini_dir=" stringByAppendingString:configDirectory]];
|
|
94
|
+
}
|
|
95
|
+
[arguments addObjectsFromArray:@[@"-p", @"/usage", @"--output-format", @"json"]];
|
|
96
|
+
NSMutableDictionary *environment = [NSProcessInfo.processInfo.environment mutableCopy];
|
|
97
|
+
if (searchPath.length) environment[@"PATH"] = searchPath;
|
|
98
|
+
// An app launched by one Agent can inherit that session's API or gateway settings.
|
|
99
|
+
for (NSString *key in environment.allKeys) {
|
|
100
|
+
if ([key hasPrefix:@"AGY_LLM_GATEWAY_"] || [@[@"GEMINI_API_KEY", @"GOOGLE_GEMINI_BASE_URL",
|
|
101
|
+
@"AGY_ADC_AUTH", @"GOOGLE_APPLICATION_CREDENTIALS"] containsObject:key]) [environment removeObjectForKey:key];
|
|
102
|
+
}
|
|
103
|
+
environment[@"AGY_CLI_DISABLE_AUTO_UPDATE"] = @"1";
|
|
104
|
+
// Earlier builds can treat /usage as a normal prompt and spend inference quota.
|
|
105
|
+
NSString *versionOutput = nil;
|
|
106
|
+
if (CCPetsRun(executable, @[@"--version"], configDirectory, environment, 4, &versionOutput, NULL) != 0) return nil;
|
|
107
|
+
NSRange versionRange = [versionOutput rangeOfString:@"[0-9]+\\.[0-9]+\\.[0-9]+" options:NSRegularExpressionSearch];
|
|
108
|
+
if (versionRange.location == NSNotFound ||
|
|
109
|
+
[[versionOutput substringWithRange:versionRange] compare:@"1.1.11" options:NSNumericSearch] == NSOrderedAscending) return nil;
|
|
110
|
+
|
|
111
|
+
NSString *output = nil;
|
|
112
|
+
int result = CCPetsRun(executable, arguments, configDirectory, environment, 8, &output, NULL);
|
|
113
|
+
if (result != 0) return nil;
|
|
114
|
+
return UsageFromAntigravityQuotaReport(JSONObject([output dataUsingEncoding:NSUTF8StringEncoding]));
|
|
115
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#import "CCPetsNormalizedUsage.h"
|
|
2
|
+
|
|
3
|
+
static NSDictionary *Dictionary(id value) {
|
|
4
|
+
return [value isKindOfClass:NSDictionary.class] ? value : nil;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
NSDictionary *CCPetsTrendWindow(NSArray<NSDictionary *> *windows) {
|
|
8
|
+
for (NSDictionary *window in windows) {
|
|
9
|
+
if ([window[@"trend"] boolValue]) return window;
|
|
10
|
+
}
|
|
11
|
+
return windows.lastObject;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
NSDictionary *CCPetsNormalizedUsage(CCPetsProviderDescriptor *provider, NSDictionary *rawUsage, BOOL showsAPIUsage) {
|
|
15
|
+
if (provider.normalizer) return provider.normalizer(rawUsage ?: @{}, showsAPIUsage);
|
|
16
|
+
NSString *billing = showsAPIUsage ? @"api" : (provider.billing ?: @"none");
|
|
17
|
+
NSDictionary *tokens = Dictionary(rawUsage[@"tokenUsage"]);
|
|
18
|
+
BOOL exhausted = [rawUsage[@"exhaustedAt"] isKindOfClass:NSNumber.class];
|
|
19
|
+
BOOL hasWindowData = exhausted;
|
|
20
|
+
NSMutableArray *windows = [NSMutableArray array];
|
|
21
|
+
for (NSDictionary *spec in provider.windowSpecs) {
|
|
22
|
+
NSString *key = spec[@"key"];
|
|
23
|
+
NSDictionary *quota = Dictionary(rawUsage[key]);
|
|
24
|
+
NSDictionary *totals = Dictionary(tokens[key]);
|
|
25
|
+
id used = quota[provider.usedKey];
|
|
26
|
+
NSMutableDictionary *window = [spec mutableCopy];
|
|
27
|
+
window[@"quota"] = quota ?: @{};
|
|
28
|
+
window[@"tokens"] = totals ?: @{};
|
|
29
|
+
window[@"trend"] = @([spec[@"trend"] boolValue]);
|
|
30
|
+
if ([used isKindOfClass:NSNumber.class]) {
|
|
31
|
+
window[@"used"] = @(fmax(0, fmin(100, [used doubleValue])));
|
|
32
|
+
}
|
|
33
|
+
if (window[@"used"] || totals) hasWindowData = YES;
|
|
34
|
+
[windows addObject:[window copy]];
|
|
35
|
+
}
|
|
36
|
+
NSMutableDictionary *summary = [NSMutableDictionary dictionary];
|
|
37
|
+
for (NSString *key in @[@"month", @"monthComparable", @"previousMonthToDate", @"today", @"recentWeek", @"monthDaily"]) {
|
|
38
|
+
summary[key] = tokens[key];
|
|
39
|
+
}
|
|
40
|
+
BOOL hasData = [billing isEqualToString:@"subscription"] ? hasWindowData : Dictionary(tokens[@"month"]) != nil;
|
|
41
|
+
return @{@"billing": billing, @"windows": [windows copy], @"tokens": [summary copy],
|
|
42
|
+
@"exhausted": @(exhausted), @"hasData": @(hasData)};
|
|
43
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#import "CCPetsProviderRegistry.h"
|
|
2
|
+
|
|
3
|
+
@interface CCPetsProviderDescriptor (Testing)
|
|
4
|
+
- (instancetype)initWithName:(NSString *)name key:(NSString *)key
|
|
5
|
+
usedKey:(NSString *)usedKey profileProvider:(NSString *)profileProvider
|
|
6
|
+
standardColorRGB:(NSArray<NSNumber *> *)standardColorRGB glassColorRGB:(NSArray<NSNumber *> *)glassColorRGB
|
|
7
|
+
logoBundleIdentifier:(NSString *)logoBundleIdentifier logoResourceName:(NSString *)logoResourceName
|
|
8
|
+
usageDisplayModeDefaultsKey:(NSString *)usageDisplayModeDefaultsKey billing:(NSString *)billing
|
|
9
|
+
windowSpecs:(NSArray<NSDictionary *> *)windowSpecs normalizer:(CCPetsUsageNormalizer)normalizer;
|
|
10
|
+
@end
|
|
11
|
+
|
|
12
|
+
// 测试期间在主线程安装;传 nil 恢复产品注册表。
|
|
13
|
+
void CCPetsProviderRegistrySetOverride(NSArray<CCPetsProviderDescriptor *> *providers);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
typedef NSDictionary *(^CCPetsUsageNormalizer)(NSDictionary *rawUsage, BOOL showsAPIUsage);
|
|
4
|
+
|
|
5
|
+
// name 是面板和偏好设置使用的显示键;key / profileProvider 保留磁盘中的小写标识。
|
|
6
|
+
@interface CCPetsProviderDescriptor : NSObject
|
|
7
|
+
@property(readonly, copy) NSString *name;
|
|
8
|
+
@property(readonly, copy) NSString *key;
|
|
9
|
+
@property(readonly, copy) NSString *usedKey;
|
|
10
|
+
@property(readonly, copy) NSString *billing;
|
|
11
|
+
@property(readonly, copy) NSArray<NSDictionary *> *windowSpecs;
|
|
12
|
+
@property(readonly, copy) CCPetsUsageNormalizer normalizer;
|
|
13
|
+
@property(readonly, copy) NSString *profileProvider;
|
|
14
|
+
// Foundation-only 元数据;颜色分量顺序为 red、green、blue。
|
|
15
|
+
@property(readonly, copy) NSArray<NSNumber *> *standardColorRGB;
|
|
16
|
+
@property(readonly, copy) NSArray<NSNumber *> *glassColorRGB;
|
|
17
|
+
@property(readonly, copy) NSString *logoBundleIdentifier;
|
|
18
|
+
@property(readonly, copy) NSString *logoResourceName;
|
|
19
|
+
@property(readonly, copy) NSString *usageDisplayModeDefaultsKey;
|
|
20
|
+
@end
|
|
21
|
+
|
|
22
|
+
// 展示顺序固定:Codex、Claude、Antigravity。
|
|
23
|
+
NSArray<CCPetsProviderDescriptor *> *CCPetsProviderDescriptors(void);
|
|
24
|
+
CCPetsProviderDescriptor *CCPetsProviderForName(NSString *name);
|
|
25
|
+
CCPetsProviderDescriptor *CCPetsProviderForProfileProvider(NSString *profileProvider);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#import "CCPetsProviderRegistry.h"
|
|
2
|
+
#import "CCPetsProfiles.h"
|
|
3
|
+
#import "CCPetsPaths.h"
|
|
4
|
+
|
|
5
|
+
@interface CCPetsProviderDescriptor ()
|
|
6
|
+
- (instancetype)initWithName:(NSString *)name key:(NSString *)key
|
|
7
|
+
usedKey:(NSString *)usedKey profileProvider:(NSString *)profileProvider
|
|
8
|
+
standardColorRGB:(NSArray<NSNumber *> *)standardColorRGB glassColorRGB:(NSArray<NSNumber *> *)glassColorRGB
|
|
9
|
+
logoBundleIdentifier:(NSString *)logoBundleIdentifier logoResourceName:(NSString *)logoResourceName
|
|
10
|
+
usageDisplayModeDefaultsKey:(NSString *)usageDisplayModeDefaultsKey billing:(NSString *)billing
|
|
11
|
+
windowSpecs:(NSArray<NSDictionary *> *)windowSpecs normalizer:(CCPetsUsageNormalizer)normalizer;
|
|
12
|
+
@end
|
|
13
|
+
|
|
14
|
+
@implementation CCPetsProviderDescriptor
|
|
15
|
+
- (instancetype)initWithName:(NSString *)name key:(NSString *)key
|
|
16
|
+
usedKey:(NSString *)usedKey profileProvider:(NSString *)profileProvider
|
|
17
|
+
standardColorRGB:(NSArray<NSNumber *> *)standardColorRGB glassColorRGB:(NSArray<NSNumber *> *)glassColorRGB
|
|
18
|
+
logoBundleIdentifier:(NSString *)logoBundleIdentifier logoResourceName:(NSString *)logoResourceName
|
|
19
|
+
usageDisplayModeDefaultsKey:(NSString *)usageDisplayModeDefaultsKey billing:(NSString *)billing
|
|
20
|
+
windowSpecs:(NSArray<NSDictionary *> *)windowSpecs normalizer:(CCPetsUsageNormalizer)normalizer {
|
|
21
|
+
if ((self = [super init])) {
|
|
22
|
+
_billing = [billing copy];
|
|
23
|
+
_windowSpecs = [windowSpecs copy];
|
|
24
|
+
_normalizer = [normalizer copy];
|
|
25
|
+
_name = [name copy];
|
|
26
|
+
_key = [key copy];
|
|
27
|
+
_usedKey = [usedKey copy];
|
|
28
|
+
_profileProvider = [profileProvider copy];
|
|
29
|
+
_standardColorRGB = [standardColorRGB copy];
|
|
30
|
+
_glassColorRGB = [glassColorRGB copy];
|
|
31
|
+
_logoBundleIdentifier = [logoBundleIdentifier copy];
|
|
32
|
+
_logoResourceName = [logoResourceName copy];
|
|
33
|
+
_usageDisplayModeDefaultsKey = [usageDisplayModeDefaultsKey copy];
|
|
34
|
+
}
|
|
35
|
+
return self;
|
|
36
|
+
}
|
|
37
|
+
@end
|
|
38
|
+
|
|
39
|
+
static NSArray<CCPetsProviderDescriptor *> *overrideProviders;
|
|
40
|
+
|
|
41
|
+
void CCPetsProviderRegistrySetOverride(NSArray<CCPetsProviderDescriptor *> *providers) {
|
|
42
|
+
overrideProviders = [providers copy];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
NSArray<CCPetsProviderDescriptor *> *CCPetsProviderDescriptors(void) {
|
|
46
|
+
if (overrideProviders) return overrideProviders;
|
|
47
|
+
static NSArray<CCPetsProviderDescriptor *> *providers;
|
|
48
|
+
static dispatch_once_t onceToken;
|
|
49
|
+
dispatch_once(&onceToken, ^{
|
|
50
|
+
// 这里的 L 只为多语言检查器标记英文源 key,绘制时才按当前语言翻译。
|
|
51
|
+
#define L(source) source
|
|
52
|
+
NSArray *windows = @[
|
|
53
|
+
@{@"key": @"fiveHour", @"titleKey": L(@"5h window"), @"minutes": @300, @"showDate": @NO},
|
|
54
|
+
@{@"key": @"week", @"titleKey": L(@"7d window"), @"minutes": @10080, @"showDate": @YES, @"trend": @YES}
|
|
55
|
+
];
|
|
56
|
+
#undef L
|
|
57
|
+
providers = @[
|
|
58
|
+
[[CCPetsProviderDescriptor alloc] initWithName:@"Codex" key:@"codex"
|
|
59
|
+
usedKey:@"used_percent" profileProvider:CCPetsProfileProviderCodex
|
|
60
|
+
standardColorRGB:@[@0.30, @0.82, @0.90] glassColorRGB:@[@0.30, @0.82, @0.90]
|
|
61
|
+
logoBundleIdentifier:@"com.openai.codex" logoResourceName:@"icon-chatgpt.icns"
|
|
62
|
+
usageDisplayModeDefaultsKey:CodexUsageDisplayModeKey billing:@"subscription"
|
|
63
|
+
windowSpecs:windows normalizer:nil],
|
|
64
|
+
// 原生玻璃背后只有一层压暗,原来的紫在上面偏暗,glass 色提亮到和青色相近的明度。
|
|
65
|
+
[[CCPetsProviderDescriptor alloc] initWithName:@"Claude" key:@"claude"
|
|
66
|
+
usedKey:@"used_percentage" profileProvider:CCPetsProfileProviderClaude
|
|
67
|
+
standardColorRGB:@[@0.66, @0.43, @0.94] glassColorRGB:@[@0.80, @0.66, @1.00]
|
|
68
|
+
logoBundleIdentifier:@"com.anthropic.claudefordesktop" logoResourceName:@"electron.icns"
|
|
69
|
+
usageDisplayModeDefaultsKey:ClaudeUsageDisplayModeKey billing:@"subscription"
|
|
70
|
+
windowSpecs:windows normalizer:nil],
|
|
71
|
+
[[CCPetsProviderDescriptor alloc] initWithName:@"Antigravity" key:@"antigravity"
|
|
72
|
+
usedKey:@"used_percent" profileProvider:CCPetsProfileProviderAntigravity
|
|
73
|
+
standardColorRGB:@[@0.28, @0.63, @0.98] glassColorRGB:@[@0.40, @0.71, @1.00]
|
|
74
|
+
logoBundleIdentifier:@"com.google.antigravity" logoResourceName:@"icon.icns"
|
|
75
|
+
usageDisplayModeDefaultsKey:AntigravityUsageDisplayModeKey billing:@"subscription"
|
|
76
|
+
windowSpecs:windows normalizer:nil]
|
|
77
|
+
];
|
|
78
|
+
});
|
|
79
|
+
return providers;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
CCPetsProviderDescriptor *CCPetsProviderForName(NSString *name) {
|
|
83
|
+
for (CCPetsProviderDescriptor *provider in CCPetsProviderDescriptors()) {
|
|
84
|
+
if ([provider.name isEqualToString:name]) return provider;
|
|
85
|
+
}
|
|
86
|
+
return nil;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
CCPetsProviderDescriptor *CCPetsProviderForProfileProvider(NSString *profileProvider) {
|
|
90
|
+
for (CCPetsProviderDescriptor *provider in CCPetsProviderDescriptors()) {
|
|
91
|
+
if ([provider.profileProvider isEqualToString:profileProvider]) return provider;
|
|
92
|
+
}
|
|
93
|
+
return nil;
|
|
94
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 本地额度历史始终记录并展示。
|
|
4
4
|
// RecordQuotaHistory 返回当前文档,调用方直接复用,避免一轮刷新里重复读盘。
|
|
5
5
|
NSDictionary *QuotaHistoryDocument(void);
|
|
6
|
-
NSDictionary *RecordQuotaHistory(NSDictionary
|
|
6
|
+
NSDictionary *RecordQuotaHistory(NSDictionary<NSString *, NSDictionary *> *usageByProvider);
|
|
7
7
|
// 每个点保留 timestamp 与 weekRemaining,绘图层才能按真实时间轴展示缺口和重置。
|
|
8
8
|
NSArray<NSDictionary *> *QuotaHistorySeries(NSDictionary *document, NSString *provider);
|
|
9
9
|
NSDictionary *RecordProfileQuotaHistory(NSDictionary<NSString *, NSDictionary *> *profileUsages,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#import "CCPetsQuotaHistory.h"
|
|
2
|
+
#import "CCPetsProviderRegistry.h"
|
|
2
3
|
#import "CCPetsPaths.h"
|
|
3
4
|
#import <sys/stat.h>
|
|
4
5
|
|
|
@@ -44,11 +45,14 @@ static NSDictionary *HistoryProviderSample(NSDictionary *usage, NSString *usedKe
|
|
|
44
45
|
|
|
45
46
|
// 返回当前的历史文档,供调用方直接取用:原先每次刷新要读盘+解析三次
|
|
46
47
|
// (这里一次、两条曲线各一次),现在整轮只读一次。
|
|
47
|
-
NSDictionary *RecordQuotaHistory(NSDictionary
|
|
48
|
+
NSDictionary *RecordQuotaHistory(NSDictionary<NSString *, NSDictionary *> *usageByProvider) {
|
|
48
49
|
NSDictionary *document = QuotaHistoryDocument();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
NSMutableDictionary *providerSamples = [NSMutableDictionary dictionary];
|
|
51
|
+
for (CCPetsProviderDescriptor *provider in CCPetsProviderDescriptors()) {
|
|
52
|
+
NSDictionary *sample = HistoryProviderSample(usageByProvider[provider.name], provider.usedKey);
|
|
53
|
+
if (sample.count > 0) providerSamples[provider.key] = sample;
|
|
54
|
+
}
|
|
55
|
+
if (providerSamples.count == 0) return document;
|
|
52
56
|
|
|
53
57
|
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
54
58
|
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
@@ -62,8 +66,7 @@ NSDictionary *RecordQuotaHistory(NSDictionary *codexUsage, NSDictionary *claudeU
|
|
|
62
66
|
if (last && now - [last[@"timestamp"] doubleValue] < 15 * 60) return document;
|
|
63
67
|
|
|
64
68
|
NSMutableDictionary *sample = [@{@"timestamp": @(now)} mutableCopy];
|
|
65
|
-
|
|
66
|
-
if (claude.count > 0) sample[@"claude"] = claude;
|
|
69
|
+
[sample addEntriesFromDictionary:providerSamples];
|
|
67
70
|
[samples addObject:sample];
|
|
68
71
|
if (samples.count > 672) {
|
|
69
72
|
[samples removeObjectsInRange:NSMakeRange(0, samples.count - 672)];
|
|
@@ -114,8 +117,8 @@ NSDictionary *RecordProfileQuotaHistory(NSDictionary<NSString *, NSDictionary *>
|
|
|
114
117
|
for (NSDictionary *profile in profiles) {
|
|
115
118
|
NSString *profileID = profile[@"id"];
|
|
116
119
|
NSDictionary *usage = profileUsages[profileID];
|
|
117
|
-
NSString *usedKey =
|
|
118
|
-
|
|
120
|
+
NSString *usedKey = CCPetsProviderForProfileProvider(profile[@"provider"]).usedKey
|
|
121
|
+
?: @"used_percentage";
|
|
119
122
|
NSDictionary *sample = HistoryProviderSample(usage, usedKey);
|
|
120
123
|
if (profileID.length > 0 && sample.count > 0) profileSamples[profileID] = sample;
|
|
121
124
|
}
|
|
@@ -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
|
+
}
|