cc-pets 2.2.0 → 2.3.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 +5 -2
- package/CC_BRIDGE.zh-CN.md +4 -2
- package/CHANGELOG.md +85 -0
- package/CHANGELOG.zh-CN.md +85 -0
- package/CONTRIBUTING.md +28 -11
- package/CONTRIBUTING.zh-CN.md +23 -11
- package/README.md +127 -27
- package/README.zh-CN.md +104 -30
- package/Resources/zh-Hans.lproj/Localizable.strings +573 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +89 -31
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +18 -4
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +90 -21
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +269 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +13 -7
- package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +71 -47
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +16 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +110 -12
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +68 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
- package/Sources/CCPets/Pet/CCPetsImageLoader.h +5 -0
- package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +10 -2
- package/Sources/CCPets/Pet/CCPetsPetSize.h +10 -0
- package/Sources/CCPets/Pet/CCPetsPetSize.m +21 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +18 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +53 -22
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +42 -2
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +632 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +31 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
- package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +3 -0
- package/Sources/CCPets/UI/MenuPetSizeView.h +5 -0
- package/Sources/CCPets/UI/MenuPetSizeView.m +73 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +57 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +163 -6
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +616 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +10 -2
- package/bin/codex-with-pet +13 -5
- package/package.json +1 -1
- package/scripts/build.sh +7 -3
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +0 -226
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsImageLoader.h +0 -4
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1806
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
#import "QuotaDashboardView+Private.h"
|
|
2
|
+
|
|
3
|
+
const CGFloat QuotaLogicalWidth = 760;
|
|
4
|
+
const CGFloat QuotaLogicalHeight = 590;
|
|
5
|
+
const CGFloat QuotaScale = 0.58;
|
|
6
|
+
|
|
7
|
+
CGFloat QuotaLogicalHeightForProviderCount(NSUInteger count) {
|
|
8
|
+
if (count == 0) return QuotaHeaderHeight + QuotaEmptyNoticeHeight + 16;
|
|
9
|
+
CGFloat cardHeight = QuotaBaseCardHeight + QuotaReclaimedFooterHeight / count;
|
|
10
|
+
return QuotaHeaderHeight + QuotaSummaryHeight +
|
|
11
|
+
count * (QuotaBlockGap + cardHeight) + QuotaBottomPadding;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
16
|
+
NSURL *appURL = [NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:bundleIdentifier];
|
|
17
|
+
NSMutableArray<NSString *> *paths = [NSMutableArray array];
|
|
18
|
+
if (appURL) {
|
|
19
|
+
NSBundle *bundle = [NSBundle bundleWithURL:appURL];
|
|
20
|
+
NSString *resourcePath = [bundle pathForResource:resourceName.stringByDeletingPathExtension
|
|
21
|
+
ofType:resourceName.pathExtension];
|
|
22
|
+
if (resourcePath.length > 0) [paths addObject:resourcePath];
|
|
23
|
+
}
|
|
24
|
+
NSString *applicationName = [bundleIdentifier containsString:@"anthropic"] ? @"Claude.app" : @"ChatGPT.app";
|
|
25
|
+
[paths addObject:[@"/Applications" stringByAppendingPathComponent:
|
|
26
|
+
[applicationName stringByAppendingPathComponent:[@"Contents/Resources" stringByAppendingPathComponent:resourceName]]]];
|
|
27
|
+
[paths addObject:[[NSHomeDirectory() stringByAppendingPathComponent:@"Applications"] stringByAppendingPathComponent:
|
|
28
|
+
[applicationName stringByAppendingPathComponent:[@"Contents/Resources" stringByAppendingPathComponent:resourceName]]]];
|
|
29
|
+
for (NSString *path in paths) {
|
|
30
|
+
NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
|
|
31
|
+
if (image) return image;
|
|
32
|
+
}
|
|
33
|
+
return appURL ? [NSWorkspace.sharedWorkspace iconForFile:appURL.path] : nil;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@implementation QuotaDashboardView
|
|
38
|
+
- (instancetype)initWithFrame:(NSRect)frame {
|
|
39
|
+
if ((self = [super initWithFrame:frame])) {
|
|
40
|
+
_selectedProfileIDs = [NSMutableDictionary dictionary];
|
|
41
|
+
self.accessibilityElement = YES;
|
|
42
|
+
self.accessibilityRole = NSAccessibilityGroupRole;
|
|
43
|
+
self.accessibilityLabel = L(@"Agent usage account cards");
|
|
44
|
+
self.accessibilityHelp = L(@"Click the arrows or page dots, swipe horizontally, or use the left and right arrow keys to switch accounts.");
|
|
45
|
+
NSTrackingArea *tracking = [[NSTrackingArea alloc] initWithRect:NSZeroRect
|
|
46
|
+
options:NSTrackingMouseEnteredAndExited |
|
|
47
|
+
NSTrackingActiveAlways | NSTrackingInVisibleRect
|
|
48
|
+
owner:self userInfo:nil];
|
|
49
|
+
[self addTrackingArea:tracking];
|
|
50
|
+
}
|
|
51
|
+
return self;
|
|
52
|
+
}
|
|
53
|
+
- (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; }
|
|
54
|
+
- (BOOL)acceptsFirstResponder { return YES; }
|
|
55
|
+
- (void)mouseEntered:(NSEvent *)event {
|
|
56
|
+
if (self.hoverChanged) self.hoverChanged(YES);
|
|
57
|
+
}
|
|
58
|
+
- (void)mouseExited:(NSEvent *)event {
|
|
59
|
+
if (self.hoverChanged) self.hoverChanged(NO);
|
|
60
|
+
}
|
|
61
|
+
// detectedProviders 未赋值时按"两家都有"渲染。这是刻意的兜底:探测是 App 侧的额外信号,
|
|
62
|
+
// 拿不到时宁可多画一张卡,也不要让有额度的用户看不到自己的卡片。
|
|
63
|
+
- (NSArray<NSString *> *)visibleProviders {
|
|
64
|
+
NSArray<NSString *> *order = @[@"Codex", @"Claude"];
|
|
65
|
+
if (!self.detectedProviders) return order;
|
|
66
|
+
NSMutableArray<NSString *> *visible = [NSMutableArray array];
|
|
67
|
+
for (NSString *name in order) {
|
|
68
|
+
if ([self.detectedProviders containsObject:name]) [visible addObject:name];
|
|
69
|
+
}
|
|
70
|
+
return visible;
|
|
71
|
+
}
|
|
72
|
+
- (CGFloat)logicalHeight {
|
|
73
|
+
return QuotaLogicalHeightForProviderCount([self visibleProviders].count);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
- (NSString *)profileProviderForName:(NSString *)name {
|
|
77
|
+
return name.lowercaseString;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
- (NSArray<NSDictionary *> *)profilesForProvider:(NSString *)name {
|
|
81
|
+
NSString *provider = [self profileProviderForName:name];
|
|
82
|
+
NSMutableArray<NSDictionary *> *matches = [NSMutableArray array];
|
|
83
|
+
for (NSDictionary *profile in self.profiles ?: @[]) {
|
|
84
|
+
if ([profile[@"provider"] isEqualToString:provider]) [matches addObject:profile];
|
|
85
|
+
}
|
|
86
|
+
return matches;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
- (NSString *)selectionDefaultsKeyForProvider:(NSString *)name {
|
|
90
|
+
return [NSString stringWithFormat:@"CCPetsSelected%@ProfileID", name];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
- (NSDictionary *)selectedProfileForProvider:(NSString *)name {
|
|
94
|
+
NSArray<NSDictionary *> *profiles = [self profilesForProvider:name];
|
|
95
|
+
if (profiles.count == 0) return nil;
|
|
96
|
+
NSString *selected = self.selectedProfileIDs[name];
|
|
97
|
+
if (selected.length == 0) {
|
|
98
|
+
selected = [NSUserDefaults.standardUserDefaults stringForKey:
|
|
99
|
+
[self selectionDefaultsKeyForProvider:name]];
|
|
100
|
+
}
|
|
101
|
+
for (NSDictionary *profile in profiles) {
|
|
102
|
+
if ([profile[@"id"] isEqualToString:selected]) {
|
|
103
|
+
self.selectedProfileIDs[name] = selected;
|
|
104
|
+
return profile;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
NSDictionary *first = profiles.firstObject;
|
|
108
|
+
self.selectedProfileIDs[name] = first[@"id"];
|
|
109
|
+
return first;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
- (NSDictionary *)usageForProvider:(NSString *)name {
|
|
113
|
+
NSDictionary *profile = [self selectedProfileForProvider:name];
|
|
114
|
+
if (profile) return self.profileUsages[profile[@"id"]] ?: @{};
|
|
115
|
+
return [name isEqualToString:@"Codex"] ? (self.codexUsage ?: @{}) : (self.claudeUsage ?: @{});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
- (NSDictionary *)summedTokenBucket:(NSArray<NSDictionary *> *)buckets {
|
|
119
|
+
NSMutableDictionary<NSString *, NSNumber *> *sum = [NSMutableDictionary dictionary];
|
|
120
|
+
for (NSDictionary *bucket in buckets) {
|
|
121
|
+
if (![bucket isKindOfClass:NSDictionary.class]) continue;
|
|
122
|
+
[bucket enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
|
|
123
|
+
if ([value isKindOfClass:NSNumber.class]) {
|
|
124
|
+
sum[key] = @([sum[key] doubleValue] + [value doubleValue]);
|
|
125
|
+
}
|
|
126
|
+
}];
|
|
127
|
+
}
|
|
128
|
+
return sum;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
- (NSDictionary *)summaryUsageForProvider:(NSString *)name {
|
|
132
|
+
NSArray<NSDictionary *> *profiles = [self profilesForProvider:name];
|
|
133
|
+
if (profiles.count == 0) return [self usageForProvider:name];
|
|
134
|
+
NSMutableArray<NSDictionary *> *today = [NSMutableArray array];
|
|
135
|
+
NSMutableArray<NSDictionary *> *recentWeek = [NSMutableArray array];
|
|
136
|
+
for (NSDictionary *profile in profiles) {
|
|
137
|
+
NSDictionary *usage = self.profileUsages[profile[@"id"]];
|
|
138
|
+
NSDictionary *tokenUsage = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class]
|
|
139
|
+
? usage[@"tokenUsage"] : nil;
|
|
140
|
+
if ([tokenUsage[@"today"] isKindOfClass:NSDictionary.class]) [today addObject:tokenUsage[@"today"]];
|
|
141
|
+
if ([tokenUsage[@"recentWeek"] isKindOfClass:NSDictionary.class]) [recentWeek addObject:tokenUsage[@"recentWeek"]];
|
|
142
|
+
}
|
|
143
|
+
return @{@"tokenUsage": @{
|
|
144
|
+
@"today": [self summedTokenBucket:today],
|
|
145
|
+
@"recentWeek": [self summedTokenBucket:recentWeek]
|
|
146
|
+
}};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
- (NSString *)displayNameForProvider:(NSString *)name {
|
|
150
|
+
NSDictionary *profile = [self selectedProfileForProvider:name];
|
|
151
|
+
NSString *displayName = [profile[@"displayName"] isKindOfClass:NSString.class]
|
|
152
|
+
? profile[@"displayName"] : nil;
|
|
153
|
+
return displayName.length > 0 ? displayName : name.lowercaseString;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
- (NSUInteger)selectedIndexForProvider:(NSString *)name {
|
|
157
|
+
NSArray<NSDictionary *> *profiles = [self profilesForProvider:name];
|
|
158
|
+
NSString *selected = [self selectedProfileForProvider:name][@"id"];
|
|
159
|
+
NSUInteger index = [profiles indexOfObjectPassingTest:^BOOL(NSDictionary *profile, NSUInteger index, BOOL *stop) {
|
|
160
|
+
return [profile[@"id"] isEqualToString:selected];
|
|
161
|
+
}];
|
|
162
|
+
return index == NSNotFound ? 0 : index;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
- (void)moveProfileForProvider:(NSString *)name direction:(NSInteger)direction {
|
|
166
|
+
NSArray<NSDictionary *> *profiles = [self profilesForProvider:name];
|
|
167
|
+
if (profiles.count < 2 || direction == 0) return;
|
|
168
|
+
NSInteger index = (NSInteger)[self selectedIndexForProvider:name];
|
|
169
|
+
NSInteger count = (NSInteger)profiles.count;
|
|
170
|
+
NSInteger next = (index + (direction > 0 ? 1 : -1) + count) % count;
|
|
171
|
+
NSString *profileID = profiles[(NSUInteger)next][@"id"];
|
|
172
|
+
self.animatingCarouselProvider = name;
|
|
173
|
+
self.carouselAnimationDirection = direction > 0 ? 1 : -1;
|
|
174
|
+
self.carouselAnimationStartedAt = NSDate.date.timeIntervalSince1970;
|
|
175
|
+
self.selectedProfileIDs[name] = profileID;
|
|
176
|
+
[NSUserDefaults.standardUserDefaults setObject:profileID
|
|
177
|
+
forKey:[self selectionDefaultsKeyForProvider:name]];
|
|
178
|
+
self.activeCarouselProvider = name;
|
|
179
|
+
self.needsDisplay = YES;
|
|
180
|
+
[self advanceCarouselAnimation];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
- (void)advanceCarouselAnimation {
|
|
184
|
+
if (self.animatingCarouselProvider.length == 0) return;
|
|
185
|
+
self.needsDisplay = YES;
|
|
186
|
+
if (NSDate.date.timeIntervalSince1970 - self.carouselAnimationStartedAt >= 0.20) {
|
|
187
|
+
self.animatingCarouselProvider = nil;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
[NSObject cancelPreviousPerformRequestsWithTarget:self
|
|
191
|
+
selector:@selector(advanceCarouselAnimation) object:nil];
|
|
192
|
+
[self performSelector:@selector(advanceCarouselAnimation) withObject:nil afterDelay:1.0 / 60.0];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
- (NSRect)logicalCardRectForProvider:(NSString *)target {
|
|
196
|
+
CGFloat cardTop = [self logicalHeight] - QuotaHeaderHeight - QuotaSummaryHeight;
|
|
197
|
+
NSArray<NSString *> *providers = [self visibleProviders];
|
|
198
|
+
CGFloat cardHeight = QuotaCardHeightForProviderCount(providers.count);
|
|
199
|
+
for (NSString *name in providers) {
|
|
200
|
+
cardTop -= QuotaBlockGap;
|
|
201
|
+
NSRect card = NSMakeRect(20, cardTop - cardHeight,
|
|
202
|
+
QuotaLogicalWidth - 40, cardHeight);
|
|
203
|
+
if ([name isEqualToString:target]) return card;
|
|
204
|
+
cardTop -= cardHeight;
|
|
205
|
+
}
|
|
206
|
+
return NSZeroRect;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
- (NSString *)providerAtEvent:(NSEvent *)event includeArrowGutters:(BOOL)includeArrowGutters
|
|
210
|
+
logicalPoint:(NSPoint *)outPoint {
|
|
211
|
+
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
|
|
212
|
+
point.x /= QuotaScale;
|
|
213
|
+
point.y /= QuotaScale;
|
|
214
|
+
if (outPoint) *outPoint = point;
|
|
215
|
+
for (NSString *name in [self visibleProviders]) {
|
|
216
|
+
NSRect hit = [self logicalCardRectForProvider:name];
|
|
217
|
+
if (includeArrowGutters) {
|
|
218
|
+
hit.origin.x = 0;
|
|
219
|
+
hit.size.width = QuotaLogicalWidth;
|
|
220
|
+
}
|
|
221
|
+
if (NSPointInRect(point, hit)) return name;
|
|
222
|
+
}
|
|
223
|
+
return nil;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
- (NSRect)previousArrowHitRectForCard:(NSRect)card {
|
|
227
|
+
return NSMakeRect(0, NSMidY(card) - 34, NSMinX(card) + 32, 68);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
- (NSRect)nextArrowHitRectForCard:(NSRect)card {
|
|
231
|
+
return NSMakeRect(NSMaxX(card) - 32, NSMidY(card) - 34,
|
|
232
|
+
QuotaLogicalWidth - NSMaxX(card) + 32, 68);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
- (BOOL)selectProfileDotAtPoint:(NSPoint)point provider:(NSString *)provider card:(NSRect)card {
|
|
236
|
+
NSArray<NSDictionary *> *profiles = [self profilesForProvider:provider];
|
|
237
|
+
CGFloat dotsWidth = (profiles.count - 1) * QuotaCarouselDotGap + QuotaCarouselDotDiameter;
|
|
238
|
+
CGFloat dotsX = NSMinX(card) + 155 - dotsWidth / 2;
|
|
239
|
+
CGFloat firstCenterX = dotsX + QuotaCarouselDotDiameter / 2;
|
|
240
|
+
CGFloat contentOffset = (NSHeight(card) - QuotaBaseCardHeight) / 2;
|
|
241
|
+
CGFloat centerY = NSMinY(card) + contentOffset + 34 + QuotaCarouselDotDiameter / 2;
|
|
242
|
+
if (fabs(point.y - centerY) > 14) return NO;
|
|
243
|
+
NSInteger index = lround((point.x - firstCenterX) / QuotaCarouselDotGap);
|
|
244
|
+
if (index < 0 || index >= (NSInteger)profiles.count) return NO;
|
|
245
|
+
CGFloat centerX = firstCenterX + index * QuotaCarouselDotGap;
|
|
246
|
+
if (fabs(point.x - centerX) > QuotaCarouselDotGap / 2) return NO;
|
|
247
|
+
|
|
248
|
+
NSInteger oldIndex = (NSInteger)[self selectedIndexForProvider:provider];
|
|
249
|
+
if (index == oldIndex) return YES;
|
|
250
|
+
NSString *profileID = profiles[(NSUInteger)index][@"id"];
|
|
251
|
+
self.animatingCarouselProvider = provider;
|
|
252
|
+
self.carouselAnimationDirection = index > oldIndex ? 1 : -1;
|
|
253
|
+
self.carouselAnimationStartedAt = NSDate.date.timeIntervalSince1970;
|
|
254
|
+
self.selectedProfileIDs[provider] = profileID;
|
|
255
|
+
[NSUserDefaults.standardUserDefaults setObject:profileID
|
|
256
|
+
forKey:[self selectionDefaultsKeyForProvider:provider]];
|
|
257
|
+
self.activeCarouselProvider = provider;
|
|
258
|
+
self.needsDisplay = YES;
|
|
259
|
+
[self advanceCarouselAnimation];
|
|
260
|
+
return YES;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
- (void)mouseDown:(NSEvent *)event {
|
|
264
|
+
NSPoint point;
|
|
265
|
+
NSString *provider = [self providerAtEvent:event includeArrowGutters:YES logicalPoint:&point];
|
|
266
|
+
if (!provider || [self profilesForProvider:provider].count < 2) {
|
|
267
|
+
[super mouseDown:event];
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
[self.window makeFirstResponder:self];
|
|
271
|
+
self.activeCarouselProvider = provider;
|
|
272
|
+
NSRect card = [self logicalCardRectForProvider:provider];
|
|
273
|
+
if (NSPointInRect(point, [self previousArrowHitRectForCard:card])) {
|
|
274
|
+
[self moveProfileForProvider:provider direction:-1];
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (NSPointInRect(point, [self nextArrowHitRectForCard:card])) {
|
|
278
|
+
[self moveProfileForProvider:provider direction:1];
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if ([self selectProfileDotAtPoint:point provider:provider card:card]) return;
|
|
282
|
+
|
|
283
|
+
// 卡片其余区域允许按住左右拖动;超过阈值后每次拖动只切换一次。
|
|
284
|
+
self.dragCarouselProvider = provider;
|
|
285
|
+
self.dragCarouselStartPoint = point;
|
|
286
|
+
self.dragCarouselDidSwitch = NO;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
- (void)mouseDragged:(NSEvent *)event {
|
|
290
|
+
if (self.dragCarouselProvider.length == 0) {
|
|
291
|
+
[super mouseDragged:event];
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (self.dragCarouselDidSwitch) return;
|
|
295
|
+
NSPoint point;
|
|
296
|
+
[self providerAtEvent:event includeArrowGutters:YES logicalPoint:&point];
|
|
297
|
+
CGFloat deltaX = point.x - self.dragCarouselStartPoint.x;
|
|
298
|
+
if (fabs(deltaX) < 36) return;
|
|
299
|
+
self.dragCarouselDidSwitch = YES;
|
|
300
|
+
[self moveProfileForProvider:self.dragCarouselProvider direction:deltaX < 0 ? 1 : -1];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
- (void)mouseUp:(NSEvent *)event {
|
|
304
|
+
self.dragCarouselProvider = nil;
|
|
305
|
+
self.dragCarouselDidSwitch = NO;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
- (void)scrollWheel:(NSEvent *)event {
|
|
309
|
+
NSString *provider = [self providerAtEvent:event includeArrowGutters:YES logicalPoint:nil];
|
|
310
|
+
CGFloat delta = fabs(event.scrollingDeltaX) >= fabs(event.scrollingDeltaY)
|
|
311
|
+
? event.scrollingDeltaX : (event.modifierFlags & NSEventModifierFlagShift ? event.scrollingDeltaY : 0);
|
|
312
|
+
if (provider && delta != 0 && [self profilesForProvider:provider].count > 1) {
|
|
313
|
+
// 惯性阶段不再继续翻页,避免一次触控板滑动跳过多个账号。
|
|
314
|
+
if (event.momentumPhase != NSEventPhaseNone) return;
|
|
315
|
+
BOOL phasedGesture = event.phase != NSEventPhaseNone;
|
|
316
|
+
if (phasedGesture) {
|
|
317
|
+
if ((event.phase & NSEventPhaseBegan) != 0 ||
|
|
318
|
+
![self.scrollCarouselProvider isEqualToString:provider]) {
|
|
319
|
+
self.scrollCarouselProvider = provider;
|
|
320
|
+
self.scrollCarouselAccumulator = 0;
|
|
321
|
+
self.scrollCarouselDidSwitch = NO;
|
|
322
|
+
}
|
|
323
|
+
self.scrollCarouselAccumulator += delta;
|
|
324
|
+
CGFloat threshold = event.hasPreciseScrollingDeltas ? 12 : 1;
|
|
325
|
+
if (!self.scrollCarouselDidSwitch && fabs(self.scrollCarouselAccumulator) >= threshold) {
|
|
326
|
+
self.scrollCarouselDidSwitch = YES;
|
|
327
|
+
[self moveProfileForProvider:provider
|
|
328
|
+
direction:self.scrollCarouselAccumulator > 0 ? 1 : -1];
|
|
329
|
+
}
|
|
330
|
+
if ((event.phase & (NSEventPhaseEnded | NSEventPhaseCancelled)) != 0) {
|
|
331
|
+
self.scrollCarouselProvider = nil;
|
|
332
|
+
self.scrollCarouselAccumulator = 0;
|
|
333
|
+
self.scrollCarouselDidSwitch = NO;
|
|
334
|
+
}
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// 无 phase 的鼠标滚轮仍按时间去抖,每个滚轮刻度最多切换一次。
|
|
339
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
340
|
+
if (now - self.lastCarouselSwitchAt < 0.22) return;
|
|
341
|
+
self.lastCarouselSwitchAt = now;
|
|
342
|
+
[self moveProfileForProvider:provider direction:delta > 0 ? 1 : -1];
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
[super scrollWheel:event];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
- (void)keyDown:(NSEvent *)event {
|
|
349
|
+
if (self.activeCarouselProvider.length > 0 && (event.keyCode == 123 || event.keyCode == 124)) {
|
|
350
|
+
[self moveProfileForProvider:self.activeCarouselProvider direction:event.keyCode == 123 ? -1 : 1];
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
[super keyDown:event];
|
|
354
|
+
}
|
|
355
|
+
- (NSRect)logicalSummaryRect {
|
|
356
|
+
return NSMakeRect(20, [self logicalHeight] - QuotaHeaderHeight - QuotaSummaryHeight,
|
|
357
|
+
QuotaLogicalWidth - 40, QuotaSummaryHeight + 1 / QuotaScale);
|
|
358
|
+
}
|
|
359
|
+
// 刷新时间挪去了右上角,这一版块整宽留给两个统计格,各占一半。
|
|
360
|
+
- (NSRect)logicalSummaryCellRectAtIndex:(NSUInteger)index {
|
|
361
|
+
NSRect summary = [self logicalSummaryRect];
|
|
362
|
+
CGFloat width = NSWidth(summary) / 2;
|
|
363
|
+
return index < 2
|
|
364
|
+
? NSMakeRect(NSMinX(summary) + index * width, NSMinY(summary), width, NSHeight(summary))
|
|
365
|
+
: NSZeroRect;
|
|
366
|
+
}
|
|
367
|
+
- (NSDictionary *)quota:(NSDictionary *)usage key:(NSString *)key {
|
|
368
|
+
id value = usage[key];
|
|
369
|
+
return [value isKindOfClass:NSDictionary.class] ? value : nil;
|
|
370
|
+
}
|
|
371
|
+
- (NSDictionary *)tokenTotals:(NSDictionary *)usage key:(NSString *)key {
|
|
372
|
+
NSDictionary *tokenUsage = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class]
|
|
373
|
+
? usage[@"tokenUsage"] : nil;
|
|
374
|
+
return [tokenUsage[key] isKindOfClass:NSDictionary.class] ? tokenUsage[key] : nil;
|
|
375
|
+
}
|
|
376
|
+
- (NSString *)formattedTokenValue:(double)value {
|
|
377
|
+
if (value >= 1000000000) return [NSString stringWithFormat:@"%.2fB", value / 1000000000.0];
|
|
378
|
+
if (value >= 1000000) return [NSString stringWithFormat:@"%.2fM", value / 1000000.0];
|
|
379
|
+
if (value >= 1000) return [NSString stringWithFormat:@"%.1fK", value / 1000.0];
|
|
380
|
+
return [NSString stringWithFormat:@"%.0f", value];
|
|
381
|
+
}
|
|
382
|
+
- (NSString *)formattedTokenCount:(NSDictionary *)totals {
|
|
383
|
+
NSNumber *number = [totals[@"total_tokens"] isKindOfClass:NSNumber.class]
|
|
384
|
+
? totals[@"total_tokens"] : nil;
|
|
385
|
+
return number ? [self formattedTokenValue:number.doubleValue] : @"--";
|
|
386
|
+
}
|
|
387
|
+
- (NSString *)formattedRequestCount:(NSDictionary *)totals {
|
|
388
|
+
NSNumber *number = [totals[@"request_count"] isKindOfClass:NSNumber.class]
|
|
389
|
+
? totals[@"request_count"] : nil;
|
|
390
|
+
return number ? [NSNumberFormatter localizedStringFromNumber:number
|
|
391
|
+
numberStyle:NSNumberFormatterDecimalStyle] : @"--";
|
|
392
|
+
}
|
|
393
|
+
- (NSDictionary *)comparisonForCurrent:(double)current previous:(double)previous
|
|
394
|
+
color:(NSColor *)color {
|
|
395
|
+
if (previous <= 0) {
|
|
396
|
+
return @{ @"text": current > 0 ? L(@"vs last month: new") : L(@"vs last month: flat"),
|
|
397
|
+
@"color": current > 0 ? color : self.secondaryColor };
|
|
398
|
+
}
|
|
399
|
+
double change = (current - previous) * 100.0 / previous;
|
|
400
|
+
if (fabs(change) < 0.05) {
|
|
401
|
+
return @{ @"text": L(@"vs last month: flat"), @"color": self.secondaryColor };
|
|
402
|
+
}
|
|
403
|
+
return @{ @"text": [NSString stringWithFormat:L(@"vs last month %@%.1f%%"),
|
|
404
|
+
change > 0 ? @"↑" : @"↓", fabs(change)], @"color": color };
|
|
405
|
+
}
|
|
406
|
+
// 输入取 total - output,而不是把几个 input_* 字段加起来:两家的包含关系是相反的。
|
|
407
|
+
// Codex 的 cached_input_tokens 是 input_tokens 的子集(实测 input+output == total),
|
|
408
|
+
// 相加会把缓存部分算两遍;Claude 的 input_tokens 不含缓存读写,两者是并列项,不加又会漏。
|
|
409
|
+
// total - output 对两家都成立,也保证"输入 + 输出"正好等于卡片上那个已用 Token。
|
|
410
|
+
- (NSString *)formattedTokenSplit:(NSDictionary *)totals wantsOutput:(BOOL)wantsOutput {
|
|
411
|
+
NSNumber *total = [totals[@"total_tokens"] isKindOfClass:NSNumber.class]
|
|
412
|
+
? totals[@"total_tokens"] : nil;
|
|
413
|
+
if (!total) return @"--";
|
|
414
|
+
NSNumber *output = [totals[@"output_tokens"] isKindOfClass:NSNumber.class]
|
|
415
|
+
? totals[@"output_tokens"] : nil;
|
|
416
|
+
double outputValue = output ? output.doubleValue : 0;
|
|
417
|
+
return [self formattedTokenValue:
|
|
418
|
+
wantsOutput ? outputValue : fmax(0, total.doubleValue - outputValue)];
|
|
419
|
+
}
|
|
420
|
+
// 5 小时窗口只显示时分,7 天窗口保留日期。resets_at 是整分对齐的,无需带秒。
|
|
421
|
+
- (NSString *)resetText:(NSDictionary *)quota showDate:(BOOL)showDate {
|
|
422
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class] ? quota[@"resets_at"] : nil;
|
|
423
|
+
if (!reset) return @"--";
|
|
424
|
+
NSDateFormatter *formatter = [NSDateFormatter new];
|
|
425
|
+
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
|
|
426
|
+
formatter.timeZone = NSTimeZone.localTimeZone;
|
|
427
|
+
formatter.dateFormat = showDate ? @"M/d HH:mm" : @"HH:mm";
|
|
428
|
+
return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:reset.doubleValue]];
|
|
429
|
+
}
|
|
430
|
+
// 汇总条的最近 7 天是共享的自然日口径。优先显示聚合结果携带的真实边界;兼容旧缓存时,
|
|
431
|
+
// 再按同一规则(含今天在内的 7 个自然日)现场计算,避免界面暂时退回到没有起期。
|
|
432
|
+
- (NSString *)recentWeekUsageLabel {
|
|
433
|
+
NSNumber *start = nil;
|
|
434
|
+
for (NSDictionary *usage in @[self.codexUsage ?: @{}, self.claudeUsage ?: @{}]) {
|
|
435
|
+
NSDictionary *tokenUsage = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class]
|
|
436
|
+
? usage[@"tokenUsage"] : nil;
|
|
437
|
+
if ([tokenUsage[@"recentWeekStartsAt"] isKindOfClass:NSNumber.class]) {
|
|
438
|
+
start = tokenUsage[@"recentWeekStartsAt"];
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (!start) {
|
|
443
|
+
NSCalendar *calendar = NSCalendar.currentCalendar;
|
|
444
|
+
NSDate *midnight = [calendar startOfDayForDate:NSDate.date];
|
|
445
|
+
NSDate *date = [calendar dateByAddingUnit:NSCalendarUnitDay value:-6 toDate:midnight
|
|
446
|
+
options:0];
|
|
447
|
+
start = @((date ?: midnight).timeIntervalSince1970);
|
|
448
|
+
}
|
|
449
|
+
NSDateFormatter *formatter = [NSDateFormatter new];
|
|
450
|
+
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
|
|
451
|
+
formatter.timeZone = NSTimeZone.localTimeZone;
|
|
452
|
+
// 两个主题使用相同字号,统一采用紧凑日期,避免完整说明挤出单元格。
|
|
453
|
+
formatter.dateFormat = @"M/d";
|
|
454
|
+
NSString *date = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:start.doubleValue]];
|
|
455
|
+
return [NSString stringWithFormat:L(@"Last 7 days (since %@)"), date];
|
|
456
|
+
}
|
|
457
|
+
// 在线 = 这一家还有活着的客户端,和有没有额度数据无关:额度会一直缓存着,
|
|
458
|
+
// 拿它当在线信号会恒亮。身份不明的老客户端无法归属到某一家,保守地都算在线。
|
|
459
|
+
- (BOOL)isProviderOnline:(NSString *)name {
|
|
460
|
+
return [self isProfileOnline:[self selectedProfileForProvider:name] provider:name];
|
|
461
|
+
}
|
|
462
|
+
- (BOOL)isProfileOnline:(NSDictionary *)profile provider:(NSString *)name {
|
|
463
|
+
NSString *profileID = profile[@"id"];
|
|
464
|
+
if ([self.liveUnscopedProviders containsObject:name] && [self.liveProviders containsObject:name]) {
|
|
465
|
+
return YES;
|
|
466
|
+
}
|
|
467
|
+
// 只有同一个 Provider 已拿到精确 profileId 时才严格按账号判断。旧 Hook 或启动事件
|
|
468
|
+
// 没有 profileId 时继续使用 provider 级进程信号,不能被另一家 Agent 的 ID 误伤。
|
|
469
|
+
BOOL hasProfileAwareSessionForProvider = NO;
|
|
470
|
+
for (NSDictionary *candidate in [self profilesForProvider:name]) {
|
|
471
|
+
NSString *candidateID = candidate[@"id"];
|
|
472
|
+
if (candidateID.length > 0 && [self.liveProfileIDs containsObject:candidateID]) {
|
|
473
|
+
hasProfileAwareSessionForProvider = YES;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (profileID.length > 0 && hasProfileAwareSessionForProvider) {
|
|
478
|
+
return [self.liveProfileIDs containsObject:profileID] || self.hasUnlabeledClient;
|
|
479
|
+
}
|
|
480
|
+
return [self.liveProviders containsObject:name] || self.hasUnlabeledClient;
|
|
481
|
+
}
|
|
482
|
+
- (NSDictionary *)accountStateForProvider:(NSString *)name profile:(NSDictionary *)profile usage:(NSDictionary *)usage {
|
|
483
|
+
BOOL api = profile ? CCPetsProfileShowsAPIUsage(profile)
|
|
484
|
+
: ([name isEqualToString:@"Codex"] ? self.codexShowsAPIUsage : self.claudeShowsAPIUsage);
|
|
485
|
+
NSString *usedKey = [name isEqualToString:@"Claude"] ? @"used_percentage" : @"used_percent";
|
|
486
|
+
BOOL exhausted = [usage[@"exhaustedAt"] isKindOfClass:NSNumber.class];
|
|
487
|
+
BOOL hasData = api ? [self tokenTotals:usage key:@"month"] != nil
|
|
488
|
+
: ([self usedForQuota:[self quota:usage key:@"fiveHour"] usedKey:usedKey] ||
|
|
489
|
+
[self usedForQuota:[self quota:usage key:@"week"] usedKey:usedKey] ||
|
|
490
|
+
[self tokenTotals:usage key:@"fiveHour"] || [self tokenTotals:usage key:@"week"] || exhausted);
|
|
491
|
+
BOOL online = [self isProfileOnline:profile provider:name];
|
|
492
|
+
NSString *state = !api && exhausted ? @"limited" : (!hasData ? @"waiting" : (online ? @"online" : @"offline"));
|
|
493
|
+
NSString *text = !api && exhausted ? L(@"Rate limited")
|
|
494
|
+
: (!hasData ? L(@"Waiting for data") : (online ? L(@"● Online") : L(@"Offline")));
|
|
495
|
+
return @{@"apiMode": @(api), @"online": @(online), @"hasData": @(hasData), @"state": state, @"status": text};
|
|
496
|
+
}
|
|
497
|
+
- (NSDictionary *)accountSnapshotForProvider:(NSString *)name profile:(NSDictionary *)profile {
|
|
498
|
+
NSString *profileID = profile[@"id"];
|
|
499
|
+
NSDictionary *usage = profile ? (self.profileUsages[profileID] ?: @{}) : [self usageForProvider:name];
|
|
500
|
+
NSArray *history = profile ? (self.profileHistories[profileID] ?: @[])
|
|
501
|
+
: ([name isEqualToString:@"Codex"] ? self.codexHistory : self.claudeHistory);
|
|
502
|
+
NSMutableDictionary *snapshot = [[self accountStateForProvider:name profile:profile usage:usage] mutableCopy];
|
|
503
|
+
snapshot[@"provider"] = name;
|
|
504
|
+
snapshot[@"usage"] = usage ?: @{};
|
|
505
|
+
snapshot[@"history"] = history ?: @[];
|
|
506
|
+
if (profile) {
|
|
507
|
+
snapshot[@"profile"] = profile;
|
|
508
|
+
snapshot[@"profileId"] = profileID;
|
|
509
|
+
snapshot[@"profileName"] = profile[@"displayName"] ?: name.lowercaseString;
|
|
510
|
+
}
|
|
511
|
+
return snapshot;
|
|
512
|
+
}
|
|
513
|
+
- (NSDictionary *)paceStatusForQuota:(NSDictionary *)quota currentUsed:(NSNumber *)currentUsed
|
|
514
|
+
color:(NSColor *)providerColor {
|
|
515
|
+
if (!currentUsed) {
|
|
516
|
+
return @{@"label": L(@"Not enough data"), @"tip": L(@"Waiting for data"),
|
|
517
|
+
@"color": self.secondaryColor};
|
|
518
|
+
}
|
|
519
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class]
|
|
520
|
+
? quota[@"resets_at"] : nil;
|
|
521
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
522
|
+
const NSTimeInterval sevenDaySeconds = 7 * 24 * 60 * 60;
|
|
523
|
+
NSTimeInterval start = reset ? reset.doubleValue - sevenDaySeconds : 0;
|
|
524
|
+
NSTimeInterval elapsed = now - start;
|
|
525
|
+
double projectedRemaining = NAN;
|
|
526
|
+
const NSTimeInterval paceObservationSeconds = 24 * 60 * 60;
|
|
527
|
+
// 满 24 小时就全额外推的话,放大倍数还有 7 倍:窗口第 25 小时,任何已用超过 14.3%
|
|
528
|
+
// 的用户都会被判成"重置前用尽"。更糟的是两家的窗口起点差几个小时,就会出现一家
|
|
529
|
+
// 已外推、另一家还没到门槛的断层——剩 80% 的报高风险,剩 61% 的却显示平稳。
|
|
530
|
+
// 因此在 24 小时到窗口 1/3(56 小时)之间按观测时长加权,从"按当前已用"平滑过渡
|
|
531
|
+
// 到"完全信任外推",门槛处不再有跳变。
|
|
532
|
+
const NSTimeInterval paceFullTrustSeconds = sevenDaySeconds / 3.0;
|
|
533
|
+
if (reset && reset.doubleValue > now && elapsed >= paceObservationSeconds) {
|
|
534
|
+
double linearUsed = currentUsed.doubleValue * sevenDaySeconds / elapsed;
|
|
535
|
+
double confidence = fmin(1.0, (elapsed - paceObservationSeconds) /
|
|
536
|
+
(paceFullTrustSeconds - paceObservationSeconds));
|
|
537
|
+
double projectedUsed = currentUsed.doubleValue +
|
|
538
|
+
(linearUsed - currentUsed.doubleValue) * confidence;
|
|
539
|
+
projectedRemaining = 100.0 - projectedUsed;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// 四个档位从轻到重排成阶梯,后面的仲裁只在这上面挪档位,不再各写一套 label/tip/color。
|
|
543
|
+
double remaining = 100.0 - currentUsed.doubleValue;
|
|
544
|
+
NSArray<NSArray *> *ladder = @[
|
|
545
|
+
@[L(@"Plenty"), CCPetsLanguageIsSource() ? @"" : L(@"Plenty of quota left"), NSColor.systemGreenColor],
|
|
546
|
+
@[L(@"Steady"), CCPetsLanguageIsSource() ? @"" : L(@"Quota is holding steady"), providerColor],
|
|
547
|
+
@[L(@"Watch"), L(@"Watch usage"), NSColor.systemOrangeColor],
|
|
548
|
+
@[L(@"At risk"), L(@"Quota low"), NSColor.systemRedColor]
|
|
549
|
+
];
|
|
550
|
+
NSUInteger baseRank = remaining >= 70 ? 0
|
|
551
|
+
: (remaining >= 40 ? 1 : (remaining >= 15 ? 2 : 3));
|
|
552
|
+
|
|
553
|
+
// 当前窗口开始满 24 小时后才按消耗节奏预测;此前 projectedRemaining 是 NAN,只看事实。
|
|
554
|
+
NSInteger projectedRank = -1;
|
|
555
|
+
NSString *projectedTip = nil;
|
|
556
|
+
if (!isnan(projectedRemaining)) {
|
|
557
|
+
if (remaining <= 5 || projectedRemaining < 0) {
|
|
558
|
+
projectedRank = 3;
|
|
559
|
+
projectedTip = L(@"Runs out early");
|
|
560
|
+
} else if (projectedRemaining < 10) {
|
|
561
|
+
projectedRank = 2;
|
|
562
|
+
projectedTip = L(@"Ease off a bit");
|
|
563
|
+
} else if (projectedRemaining >= 30) {
|
|
564
|
+
projectedRank = 0;
|
|
565
|
+
projectedTip = L(@"Plenty to spare");
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// 仲裁:有预测就以预测为准,但事实划两条不可逾越的线。预测是关于未来的猜测,
|
|
570
|
+
// 剩余额度是此刻的事实;让猜测把事实盖掉,两个方向都会出错——
|
|
571
|
+
// 剩 80% 被外推成"高风险"是虚惊,剩 10% 被外推成"需关注"是漏报,后者更危险。
|
|
572
|
+
NSUInteger rank = projectedRank >= 0 ? (NSUInteger)projectedRank : baseRank;
|
|
573
|
+
BOOL demotedByFact = NO;
|
|
574
|
+
if (remaining < 15) rank = 3; // 只剩一成,再乐观的预测也压不下来
|
|
575
|
+
else if (remaining < 40 && rank < 1) rank = 1; // 不到四成就别标"充足"
|
|
576
|
+
if (remaining >= 70 && rank > 2) { // 还有七成,再难看的预测也不报"高风险"
|
|
577
|
+
rank = 2;
|
|
578
|
+
demotedByFact = YES;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
NSString *label = ladder[rank][0];
|
|
582
|
+
NSString *tip = ladder[rank][1];
|
|
583
|
+
if (demotedByFact) tip = L(@"Burning fast");
|
|
584
|
+
else if (projectedRank >= 0 && rank == (NSUInteger)projectedRank) tip = projectedTip;
|
|
585
|
+
return @{@"label": label, @"tip": tip, @"color": ladder[rank][2], @"level": @(rank)};
|
|
586
|
+
}
|
|
587
|
+
@end
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
English | [简体中文](./THIRD_PARTY_NOTICES.zh-CN.md)
|
|
4
4
|
|
|
5
5
|
The CC Pets distribution does not bundle character assets downloaded from PetDex.
|
|
6
|
-
The built-in
|
|
6
|
+
The built-in `ByteMochi.webp` is an original default asset created for this project. It
|
|
7
7
|
is unrelated to any existing anime, game, or commercial mascot and is distributed
|
|
8
8
|
with the project code under the MIT License.
|
|
9
9
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[English](./THIRD_PARTY_NOTICES.md) | 简体中文
|
|
4
4
|
|
|
5
|
-
CC Pets 发布包不捆绑 PetDex 下载的角色素材。内置的
|
|
5
|
+
CC Pets 发布包不捆绑 PetDex 下载的角色素材。内置的 `ByteMochi.webp` 是专为本项目创建的
|
|
6
6
|
原创默认素材,与任何现有动漫、游戏或商业吉祥物无关,并与项目代码一同按 MIT License 分发。
|
|
7
7
|
|
|
8
8
|
程序会在运行时尝试读取用户本机已安装的 Codex/ChatGPT 和 Claude 应用图标,仅用于界面识别;
|
package/bin/cc-pets
CHANGED
|
@@ -21,6 +21,7 @@ print_usage() {
|
|
|
21
21
|
cc-pets build Rebuild
|
|
22
22
|
cc-pets clean Clear caches (quit the pet first)
|
|
23
23
|
cc-pets doctor Check the installation and integrations, with suggested fixes
|
|
24
|
+
cc-pets open [page] Open the Main menu (pages: overview, sessions, usage, pet, accounts, alerts, general, diagnostics)
|
|
24
25
|
cc-pets paths [--json] List where settings, caches and data live, and their sizes
|
|
25
26
|
cc-pets pet <command> Manage pet sprites; see cc-pets pet help
|
|
26
27
|
cc-pets bridge <command> CC Bridge cross-session messaging; see cc-pets bridge --help
|
|
@@ -43,17 +44,18 @@ if [[ "${1:-}" == "install" ]]; then
|
|
|
43
44
|
fi
|
|
44
45
|
|
|
45
46
|
if [[ "${1:-}" == "pet" ]]; then
|
|
46
|
-
|
|
47
|
-
exec
|
|
47
|
+
[[ -x "${PET_BIN}" ]] || "${PROJECT_DIR}/scripts/build.sh" >/dev/null
|
|
48
|
+
exec "${PET_BIN}" "$@"
|
|
48
49
|
fi
|
|
49
50
|
|
|
50
51
|
if [[ "${1:-}" == "bridge" ]]; then
|
|
51
|
-
|
|
52
|
-
exec
|
|
52
|
+
[[ -x "${PET_BIN}" ]] || "${PROJECT_DIR}/scripts/build.sh" >/dev/null
|
|
53
|
+
exec "${PET_BIN}" "$@"
|
|
53
54
|
fi
|
|
54
55
|
|
|
55
|
-
if [[ "${1:-}" == "doctor" || "${1:-}" == "paths" ]]; then
|
|
56
|
-
|
|
56
|
+
if [[ "${1:-}" == "doctor" || "${1:-}" == "paths" || "${1:-}" == "open" ]]; then
|
|
57
|
+
[[ -x "${PET_BIN}" ]] || "${PROJECT_DIR}/scripts/build.sh" >/dev/null
|
|
58
|
+
exec "${PET_BIN}" "$@"
|
|
57
59
|
fi
|
|
58
60
|
|
|
59
61
|
if [[ "${1:-}" == "uninstall" ]]; then
|
package/bin/claude-with-pet
CHANGED
|
@@ -47,10 +47,18 @@ export CC_PETS_TERMINAL_TTY="${TTY:-}"
|
|
|
47
47
|
export CC_PETS_TERMINAL_PROGRAM="${TERM_PROGRAM:-}"
|
|
48
48
|
export CC_PETS_TERMINAL_SESSION="${TERM_SESSION_ID:-${ITERM_SESSION_ID:-}}"
|
|
49
49
|
export CC_PETS_TERMINAL_BUNDLE_ID="$("${PET_BIN}" --frontmost-bundle-id 2>/dev/null || true)"
|
|
50
|
+
# 从当前会话实际使用的 Home 解析已登记账号,让启动事件从第一帧就能归属到正确卡片。
|
|
51
|
+
CLAUDE_HOME="${CLAUDE_CONFIG_DIR:-${HOME}/.claude}"
|
|
52
|
+
CC_PETS_PROFILE_ID="$("${PET_BIN}" profile resolve --provider claude --home "${CLAUDE_HOME}" 2>/dev/null || true)"
|
|
53
|
+
if [[ -n "${CC_PETS_PROFILE_ID}" ]]; then
|
|
54
|
+
export CC_PETS_PROFILE_ID
|
|
55
|
+
CC_PETS_PROFILE_NAME="$("${PET_BIN}" profile resolve --provider claude --home "${CLAUDE_HOME}" --field name 2>/dev/null || true)"
|
|
56
|
+
[[ -n "${CC_PETS_PROFILE_NAME}" ]] && export CC_PETS_PROFILE_NAME
|
|
57
|
+
fi
|
|
50
58
|
|
|
51
59
|
mkdir -p "${CLIENT_DIR}"
|
|
52
|
-
#
|
|
53
|
-
printf '%s\n%s\n' 'Claude' "${CC_PETS_TERMINAL_TTY:t}" > "${CLIENT_FILE}"
|
|
60
|
+
# 依次记录 provider、TTY、profileId;旧版两行文件仍可按 provider 级在线兼容。
|
|
61
|
+
printf '%s\n%s\n%s\n' 'Claude' "${CC_PETS_TERMINAL_TTY:t}" "${CC_PETS_PROFILE_ID:-}" > "${CLIENT_FILE}"
|
|
54
62
|
|
|
55
63
|
print -rn -- '{"schemaVersion":1,"provider":"Claude","state":"starting"}' | \
|
|
56
64
|
"${PET_BIN}" --provider-event >/dev/null 2>&1 || true
|