cc-pets 2.2.1 → 2.4.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 +24 -2
- package/CC_BRIDGE.zh-CN.md +19 -2
- package/CHANGELOG.md +111 -0
- package/CHANGELOG.zh-CN.md +111 -0
- package/CONTRIBUTING.md +35 -10
- package/CONTRIBUTING.zh-CN.md +28 -10
- package/README.md +212 -32
- package/README.zh-CN.md +167 -36
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +699 -20
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +90 -25
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +17 -3
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +71 -23
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/{CCPetsAppDelegate+Settings.m → App/CCPetsAppDelegate+Settings.m} +87 -102
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +121 -51
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +17 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +107 -11
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +74 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +43 -23
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +832 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +59 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +576 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +507 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +106 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +897 -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/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +2 -1
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +148 -36
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -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} +11 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +19 -2
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +4 -1
- 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 +646 -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 +33 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +117 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +44 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +279 -0
- package/Sources/CCPets/UI/CCPetsGlassView.h +57 -0
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +60 -11
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +64 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +1412 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +135 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +865 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +288 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +712 -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/{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+CodexBackground.m +193 -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 +97 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +323 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +58 -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} +174 -19
- 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 +598 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +713 -0
- package/THIRD_PARTY_NOTICES.md +8 -4
- package/THIRD_PARTY_NOTICES.zh-CN.md +6 -2
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +12 -2
- package/bin/codex-with-pet +15 -5
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +31 -5
- package/scripts/compile-cached.zsh +133 -0
- 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/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsGlassView.h +0 -31
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1839
- 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/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.h → Pet/CCPetsImageLoader.h} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.h → Pet/CCPetsPetSize.h} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.m → Pet/CCPetsPetSize.m} +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/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +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/{MenuPetSizeView.h → UI/MenuPetSizeView.h} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.m → UI/MenuPetSizeView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
#import "CCPetsUsage.h"
|
|
3
3
|
#import "CCPetsCodexRateLimits.h"
|
|
4
4
|
#import "CCPetsPaths.h"
|
|
5
|
+
#import "CCPetsProfiles.h"
|
|
6
|
+
#import "CCPetsUsageMode.h"
|
|
5
7
|
#import <CoreServices/CoreServices.h>
|
|
6
8
|
#import <fcntl.h>
|
|
7
9
|
#import <unistd.h>
|
|
@@ -20,8 +22,14 @@
|
|
|
20
22
|
@property ClaudeUsageReader *claudeReader;
|
|
21
23
|
@property(readwrite) NSDictionary *codexUsage;
|
|
22
24
|
@property(readwrite) NSDictionary *claudeUsage;
|
|
25
|
+
@property(readwrite) NSDictionary<NSString *, NSDictionary *> *profileUsages;
|
|
26
|
+
@property(readwrite) NSArray<NSDictionary *> *profiles;
|
|
27
|
+
@property NSMutableDictionary<NSString *, ClaudeUsageReader *> *claudeProfileReaders;
|
|
28
|
+
@property NSMutableDictionary<NSString *, CodexUsageReader *> *codexProfileReaders;
|
|
29
|
+
@property NSMutableDictionary<NSString *, CCPetsCodexRateLimitsReader *> *codexProfileRateReaders;
|
|
30
|
+
@property NSMutableDictionary<NSString *, NSDictionary *> *codexProfileLiveUsages;
|
|
31
|
+
@property NSMutableDictionary<NSString *, NSString *> *profileReaderHomes;
|
|
23
32
|
@property NSDictionary *codexLiveUsage;
|
|
24
|
-
@property NSTimeInterval codexLiveUsageUpdatedAt;
|
|
25
33
|
@property FSEventStreamRef codexStream;
|
|
26
34
|
@property dispatch_source_t claudeSource;
|
|
27
35
|
@property unsigned long long claudeSize;
|
|
@@ -38,6 +46,27 @@
|
|
|
38
46
|
// 失败时不能让一次旧的受限结论一直挂着。
|
|
39
47
|
static const NSTimeInterval CodexLiveUsageTTL = 5 * 60.0;
|
|
40
48
|
|
|
49
|
+
// 默认账号、自定义 Home、缓存恢复和实时回调共用同一条合并路径。
|
|
50
|
+
// 用采样时间计龄,轮询旧缓存不会延长受限标记的寿命;窗口百分比仍按新旧快照择优。
|
|
51
|
+
static NSDictionary *ApplyLiveCodexUsage(NSDictionary *sessionUsage, NSDictionary *live) {
|
|
52
|
+
if (live[@"exhaustedAt"] && NSDate.date.timeIntervalSince1970 -
|
|
53
|
+
[live[@"sampledAt"] doubleValue] > CodexLiveUsageTTL) {
|
|
54
|
+
NSMutableDictionary *withoutExhaustion = [live mutableCopy];
|
|
55
|
+
[withoutExhaustion removeObjectForKey:@"exhaustedAt"];
|
|
56
|
+
live = withoutExhaustion;
|
|
57
|
+
}
|
|
58
|
+
return UsageByRemovingExpiredQuotaWindows(CodexUsageByApplyingLiveUsage(sessionUsage, live));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static BOOL HasRegisteredDefaultProfile(NSString *provider) {
|
|
62
|
+
NSString *target = ([provider isEqualToString:CCPetsProfileProviderClaude]
|
|
63
|
+
? DefaultClaudeConfigDirectory() : DefaultCodexHomeDirectory()).stringByStandardizingPath;
|
|
64
|
+
for (NSDictionary *profile in CCPetsProfilesForProvider(provider)) {
|
|
65
|
+
if ([[profile[@"home"] stringByStandardizingPath] isEqualToString:target]) return YES;
|
|
66
|
+
}
|
|
67
|
+
return NO;
|
|
68
|
+
}
|
|
69
|
+
|
|
41
70
|
static NSDictionary *LoadCodexLiveUsage(void) {
|
|
42
71
|
NSData *data = [NSData dataWithContentsOfFile:CodexLiveUsagePath()];
|
|
43
72
|
id value = data.length > 0 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
@@ -111,20 +140,23 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
111
140
|
_codexReader = [CodexUsageReader new];
|
|
112
141
|
_codexRateLimitsReader = [CCPetsCodexRateLimitsReader new];
|
|
113
142
|
_claudeReader = [ClaudeUsageReader new];
|
|
143
|
+
_claudeProfileReaders = [NSMutableDictionary dictionary];
|
|
144
|
+
_codexProfileReaders = [NSMutableDictionary dictionary];
|
|
145
|
+
_codexProfileRateReaders = [NSMutableDictionary dictionary];
|
|
146
|
+
_codexProfileLiveUsages = [NSMutableDictionary dictionary];
|
|
147
|
+
_profileReaderHomes = [NSMutableDictionary dictionary];
|
|
114
148
|
// 桌宠是唯一的常驻读取者,摘要缓存由它维护并落盘。
|
|
115
149
|
_codexReader.persistsCache = YES;
|
|
116
150
|
_claudeReader.persistsCache = YES;
|
|
117
151
|
// 桌宠随最后一个客户端退出,下次启动时 App Server 要几秒才回话。这段时间先用上次
|
|
118
152
|
// 存下的实时值,否则面板会先显示会话日志里可能已经过时十几个小时的快照。
|
|
119
153
|
_codexLiveUsage = LoadCodexLiveUsage();
|
|
120
|
-
_codexLiveUsageUpdatedAt = [_codexLiveUsage[@"sampledAt"] doubleValue];
|
|
121
154
|
__weak typeof(self) weakSelf = self;
|
|
122
155
|
_codexRateLimitsReader.liveUsageHandler = ^(NSDictionary *liveUsage) {
|
|
123
156
|
dispatch_async(weakSelf.queue, ^{
|
|
124
157
|
typeof(self) strongSelf = weakSelf;
|
|
125
158
|
if (!strongSelf || !strongSelf.started) return;
|
|
126
159
|
strongSelf.codexLiveUsage = liveUsage;
|
|
127
|
-
strongSelf.codexLiveUsageUpdatedAt = NSDate.date.timeIntervalSince1970;
|
|
128
160
|
SaveCodexLiveUsage(liveUsage);
|
|
129
161
|
strongSelf.codexUsage = [strongSelf usageByApplyingLiveCodexUsage:
|
|
130
162
|
strongSelf.codexReader.usage ?: strongSelf.codexUsage];
|
|
@@ -136,25 +168,130 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
136
168
|
}
|
|
137
169
|
|
|
138
170
|
- (NSDictionary *)usageByApplyingLiveCodexUsage:(NSDictionary *)sessionUsage {
|
|
139
|
-
|
|
140
|
-
if (!live) return sessionUsage;
|
|
141
|
-
if (live[@"exhaustedAt"] && NSDate.date.timeIntervalSince1970 -
|
|
142
|
-
self.codexLiveUsageUpdatedAt > CodexLiveUsageTTL) {
|
|
143
|
-
NSMutableDictionary *withoutExhaustion = [live mutableCopy];
|
|
144
|
-
[withoutExhaustion removeObjectForKey:@"exhaustedAt"];
|
|
145
|
-
live = withoutExhaustion;
|
|
146
|
-
}
|
|
147
|
-
// 存下来的实时值可能是好几个小时前的,窗口早已翻篇,合并后要再按 resets_at 过一遍。
|
|
148
|
-
return UsageByRemovingExpiredQuotaWindows(CodexUsageByApplyingLiveUsage(sessionUsage, live));
|
|
171
|
+
return ApplyLiveCodexUsage(sessionUsage, self.codexLiveUsage);
|
|
149
172
|
}
|
|
150
173
|
// 回调交给主线程:下游全是 UI(面板重排、气泡定位、系统通知)。
|
|
151
174
|
- (void)publishChange {
|
|
175
|
+
[self syncDefaultProfileUsages];
|
|
152
176
|
void (^handler)(NSDictionary *, NSDictionary *) = self.changeHandler;
|
|
153
177
|
if (!handler) return;
|
|
154
178
|
NSDictionary *codex = self.codexUsage;
|
|
155
179
|
NSDictionary *claude = self.claudeUsage;
|
|
156
180
|
dispatch_async(dispatch_get_main_queue(), ^{ handler(codex, claude); });
|
|
157
181
|
}
|
|
182
|
+
|
|
183
|
+
// 默认账号的卡片读的是 profileUsages,但 FSEvents、statusline 监听和 App Server 回调
|
|
184
|
+
// 只更新 codexUsage / claudeUsage。每次发布前把默认账号那一格对齐到最新值,
|
|
185
|
+
// 否则卡片要等下一轮全量刷新(最长 120 秒)才动。
|
|
186
|
+
- (void)syncDefaultProfileUsages {
|
|
187
|
+
if (self.profiles.count == 0) return;
|
|
188
|
+
NSString *defaultClaude = DefaultClaudeConfigDirectory().stringByStandardizingPath;
|
|
189
|
+
NSString *defaultCodex = DefaultCodexHomeDirectory().stringByStandardizingPath;
|
|
190
|
+
NSMutableDictionary<NSString *, NSDictionary *> *usages =
|
|
191
|
+
[self.profileUsages mutableCopy] ?: [NSMutableDictionary dictionary];
|
|
192
|
+
BOOL changed = NO;
|
|
193
|
+
for (NSDictionary *profile in self.profiles) {
|
|
194
|
+
NSString *profileID = profile[@"id"], *provider = profile[@"provider"];
|
|
195
|
+
NSString *home = [profile[@"home"] stringByStandardizingPath];
|
|
196
|
+
if (profileID.length == 0 || home.length == 0) continue;
|
|
197
|
+
NSDictionary *usage = nil;
|
|
198
|
+
if ([provider isEqualToString:CCPetsProfileProviderClaude] && [home isEqualToString:defaultClaude]) {
|
|
199
|
+
usage = self.claudeUsage;
|
|
200
|
+
} else if ([provider isEqualToString:CCPetsProfileProviderCodex] && [home isEqualToString:defaultCodex]) {
|
|
201
|
+
usage = self.codexUsage;
|
|
202
|
+
} else continue;
|
|
203
|
+
if (usages[profileID] == usage || [usages[profileID] isEqual:usage]) continue;
|
|
204
|
+
usages[profileID] = usage;
|
|
205
|
+
changed = YES;
|
|
206
|
+
}
|
|
207
|
+
if (changed) self.profileUsages = usages;
|
|
208
|
+
}
|
|
209
|
+
- (void)refreshProfileUsages {
|
|
210
|
+
NSArray<NSDictionary *> *profiles = CCPetsProfiles();
|
|
211
|
+
NSMutableDictionary<NSString *, NSDictionary *> *usages = [NSMutableDictionary dictionary];
|
|
212
|
+
NSMutableSet<NSString *> *liveIDs = [NSMutableSet set];
|
|
213
|
+
NSString *defaultClaude = DefaultClaudeConfigDirectory().stringByStandardizingPath;
|
|
214
|
+
NSString *defaultCodex = DefaultCodexHomeDirectory().stringByStandardizingPath;
|
|
215
|
+
for (NSDictionary *profile in profiles) {
|
|
216
|
+
NSString *profileID = profile[@"id"], *provider = profile[@"provider"];
|
|
217
|
+
NSString *home = [profile[@"home"] stringByStandardizingPath];
|
|
218
|
+
if (profileID.length == 0 || home.length == 0) continue;
|
|
219
|
+
[liveIDs addObject:profileID];
|
|
220
|
+
NSString *readerHome = self.profileReaderHomes[profileID];
|
|
221
|
+
if (readerHome.length > 0 && ![readerHome isEqualToString:home]) {
|
|
222
|
+
[self.claudeProfileReaders removeObjectForKey:profileID];
|
|
223
|
+
[self.codexProfileReaders removeObjectForKey:profileID];
|
|
224
|
+
[self.codexProfileRateReaders[profileID] stop];
|
|
225
|
+
[self.codexProfileRateReaders removeObjectForKey:profileID];
|
|
226
|
+
[self.codexProfileLiveUsages removeObjectForKey:profileID];
|
|
227
|
+
}
|
|
228
|
+
self.profileReaderHomes[profileID] = home;
|
|
229
|
+
NSDictionary *usage = nil;
|
|
230
|
+
if ([provider isEqualToString:CCPetsProfileProviderClaude]) {
|
|
231
|
+
if ([home isEqualToString:defaultClaude]) usage = self.claudeUsage;
|
|
232
|
+
else {
|
|
233
|
+
ClaudeUsageReader *reader = self.claudeProfileReaders[profileID];
|
|
234
|
+
if (!reader) {
|
|
235
|
+
reader = [[ClaudeUsageReader alloc] initWithConfigDirectory:home];
|
|
236
|
+
reader.persistsCache = YES;
|
|
237
|
+
self.claudeProfileReaders[profileID] = reader;
|
|
238
|
+
}
|
|
239
|
+
usage = [reader refreshWithFullAggregation];
|
|
240
|
+
}
|
|
241
|
+
} else if ([provider isEqualToString:CCPetsProfileProviderCodex]) {
|
|
242
|
+
if ([home isEqualToString:defaultCodex]) usage = self.codexUsage;
|
|
243
|
+
else {
|
|
244
|
+
CodexUsageReader *reader = self.codexProfileReaders[profileID];
|
|
245
|
+
if (!reader) {
|
|
246
|
+
reader = [[CodexUsageReader alloc] initWithCodexHome:home];
|
|
247
|
+
reader.persistsCache = YES;
|
|
248
|
+
self.codexProfileReaders[profileID] = reader;
|
|
249
|
+
}
|
|
250
|
+
usage = [reader refreshWithFullDiscovery];
|
|
251
|
+
NSDictionary *live = self.codexProfileLiveUsages[profileID];
|
|
252
|
+
usage = ApplyLiveCodexUsage(usage, live);
|
|
253
|
+
CCPetsCodexRateLimitsReader *rateReader = self.codexProfileRateReaders[profileID];
|
|
254
|
+
if (!rateReader) {
|
|
255
|
+
rateReader = [[CCPetsCodexRateLimitsReader alloc] initWithCodexHome:home];
|
|
256
|
+
__weak typeof(self) weakSelf = self;
|
|
257
|
+
NSString *capturedID = [profileID copy];
|
|
258
|
+
rateReader.liveUsageHandler = ^(NSDictionary *liveUsage) {
|
|
259
|
+
dispatch_async(weakSelf.queue, ^{
|
|
260
|
+
typeof(self) strongSelf = weakSelf;
|
|
261
|
+
if (!strongSelf || !strongSelf.started) return;
|
|
262
|
+
strongSelf.codexProfileLiveUsages[capturedID] = liveUsage;
|
|
263
|
+
NSMutableDictionary *updated = [strongSelf.profileUsages mutableCopy] ?: [NSMutableDictionary dictionary];
|
|
264
|
+
NSDictionary *session = strongSelf.codexProfileReaders[capturedID].usage;
|
|
265
|
+
updated[capturedID] = ApplyLiveCodexUsage(session, liveUsage);
|
|
266
|
+
strongSelf.profileUsages = updated;
|
|
267
|
+
[strongSelf publishChange];
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
self.codexProfileRateReaders[profileID] = rateReader;
|
|
271
|
+
}
|
|
272
|
+
[rateReader refresh];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (usage) usages[profileID] = usage;
|
|
276
|
+
}
|
|
277
|
+
for (NSString *profileID in self.claudeProfileReaders.allKeys.copy) {
|
|
278
|
+
if (![liveIDs containsObject:profileID]) [self.claudeProfileReaders removeObjectForKey:profileID];
|
|
279
|
+
}
|
|
280
|
+
for (NSString *profileID in self.codexProfileReaders.allKeys.copy) {
|
|
281
|
+
if (![liveIDs containsObject:profileID]) {
|
|
282
|
+
[self.codexProfileReaders removeObjectForKey:profileID];
|
|
283
|
+
[self.codexProfileRateReaders[profileID] stop];
|
|
284
|
+
[self.codexProfileRateReaders removeObjectForKey:profileID];
|
|
285
|
+
[self.codexProfileLiveUsages removeObjectForKey:profileID];
|
|
286
|
+
[self.profileReaderHomes removeObjectForKey:profileID];
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
for (NSString *profileID in self.profileReaderHomes.allKeys.copy) {
|
|
290
|
+
if (![liveIDs containsObject:profileID]) [self.profileReaderHomes removeObjectForKey:profileID];
|
|
291
|
+
}
|
|
292
|
+
self.profiles = profiles;
|
|
293
|
+
self.profileUsages = usages;
|
|
294
|
+
}
|
|
158
295
|
// changed=YES 表示额度文件确实变了(VNODE 事件),跳过 stat 比较;
|
|
159
296
|
// aggregate=YES 表示这是手动刷新/面板打开/兜底轮询,Token 聚合要绕过节流立即重算。
|
|
160
297
|
- (NSDictionary *)readClaudeUsageChanged:(BOOL)changed forcingAggregation:(BOOL)aggregate {
|
|
@@ -240,13 +377,28 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
240
377
|
}
|
|
241
378
|
- (void)refreshOnQueue {
|
|
242
379
|
if (!self.started) return;
|
|
380
|
+
CCPetsRefreshProfileAuthenticationModes(CCPetsProfiles());
|
|
243
381
|
// 手动刷新、面板打开和 120 秒兜底都走全量路径,Token 聚合的节流只约束 FSEvents 热路径。
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
if (
|
|
249
|
-
|
|
382
|
+
BOOL hasDefaultCodex = HasRegisteredDefaultProfile(CCPetsProfileProviderCodex);
|
|
383
|
+
BOOL hasDefaultClaude = HasRegisteredDefaultProfile(CCPetsProfileProviderClaude);
|
|
384
|
+
self.codexUsage = hasDefaultCodex ? [self usageByApplyingLiveCodexUsage:
|
|
385
|
+
[self.codexReader refreshWithFullDiscovery]] : nil;
|
|
386
|
+
if (hasDefaultCodex) [self.codexRateLimitsReader refresh];
|
|
387
|
+
self.claudeUsage = hasDefaultClaude
|
|
388
|
+
? [self readClaudeUsageChanged:NO forcingAggregation:YES] : nil;
|
|
389
|
+
[self refreshProfileUsages];
|
|
390
|
+
if (hasDefaultCodex && !self.codexStream) [self startCodexStream];
|
|
391
|
+
if (hasDefaultClaude && !self.claudeSource) [self startClaudeSource];
|
|
392
|
+
if (!hasDefaultCodex && self.codexStream) {
|
|
393
|
+
FSEventStreamStop(self.codexStream);
|
|
394
|
+
FSEventStreamInvalidate(self.codexStream);
|
|
395
|
+
FSEventStreamRelease(self.codexStream);
|
|
396
|
+
self.codexStream = NULL;
|
|
397
|
+
}
|
|
398
|
+
if (!hasDefaultClaude && self.claudeSource) {
|
|
399
|
+
dispatch_source_cancel(self.claudeSource);
|
|
400
|
+
self.claudeSource = nil;
|
|
401
|
+
}
|
|
250
402
|
[self publishChange];
|
|
251
403
|
}
|
|
252
404
|
// 同步收尾:退出时要保证事件源在进程消失之前真的停掉,而它们只能在读取队列上安全拆除。
|
|
@@ -259,6 +411,9 @@ static void CodexEventsCallback(ConstFSEventStreamRef streamRef,
|
|
|
259
411
|
}
|
|
260
412
|
- (void)teardownSources {
|
|
261
413
|
[self.codexRateLimitsReader stop];
|
|
414
|
+
for (CCPetsCodexRateLimitsReader *reader in self.codexProfileRateReaders.allValues) {
|
|
415
|
+
[reader stop];
|
|
416
|
+
}
|
|
262
417
|
if (self.codexStream) {
|
|
263
418
|
FSEventStreamStop(self.codexStream);
|
|
264
419
|
FSEventStreamInvalidate(self.codexStream);
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
#import "QuotaDashboardView+Private.h"
|
|
2
|
+
|
|
3
|
+
@implementation QuotaDashboardView (Primitives)
|
|
4
|
+
- (NSColor *)primaryColor {
|
|
5
|
+
return [NSColor colorWithWhite:0.97 alpha:1];
|
|
6
|
+
}
|
|
7
|
+
- (NSColor *)secondaryColor {
|
|
8
|
+
// 没有深色底托着时,0.68 的灰在亮壁纸上会发虚。
|
|
9
|
+
return [NSColor colorWithWhite:self.usesLiquidGlass ? 0.84 : 0.68 alpha:1];
|
|
10
|
+
}
|
|
11
|
+
// 原生玻璃背后只有一层压暗,原来的紫在上面偏暗,提亮到和青色相近的明度。
|
|
12
|
+
- (NSColor *)claudeColor {
|
|
13
|
+
return self.usesLiquidGlass ? [NSColor colorWithRed:0.80 green:0.66 blue:1.00 alpha:1]
|
|
14
|
+
: [NSColor colorWithRed:0.66 green:0.43 blue:0.94 alpha:1];
|
|
15
|
+
}
|
|
16
|
+
- (NSColor *)accentColor { return [NSColor colorWithRed:0.30 green:0.82 blue:0.90 alpha:1]; }
|
|
17
|
+
// 有 Agent 在线才用绿色,否则整块转灰,避免"暂无 Agent 在线"配一个绿点。
|
|
18
|
+
- (NSColor *)agentStatusColor {
|
|
19
|
+
return self.activeAgentCount > 0 ? NSColor.systemGreenColor : self.secondaryColor;
|
|
20
|
+
}
|
|
21
|
+
- (void)drawSystemMetricsEndingAtX:(CGFloat)rightEdge y:(CGFloat)y {
|
|
22
|
+
NSMutableArray<NSDictionary *> *metrics = [NSMutableArray array];
|
|
23
|
+
if (self.systemCPUEnabled) {
|
|
24
|
+
NSString *value = self.systemCPUPercent
|
|
25
|
+
? [NSString stringWithFormat:@"%.0f%%", self.systemCPUPercent.doubleValue] : @"--";
|
|
26
|
+
[metrics addObject:@{@"symbol": @"cpu", @"label": [NSString stringWithFormat:@"CPU %@", value],
|
|
27
|
+
@"color": [NSColor colorWithRed:245.0 / 255.0 green:166.0 / 255.0
|
|
28
|
+
blue:35.0 / 255.0 alpha:1]}];
|
|
29
|
+
}
|
|
30
|
+
if (self.systemTemperatureEnabled) {
|
|
31
|
+
NSString *value = self.systemTemperatureCelsius
|
|
32
|
+
? [NSString stringWithFormat:@"%.0f°C", self.systemTemperatureCelsius.doubleValue] : @"--°C";
|
|
33
|
+
double temperature = self.systemTemperatureCelsius.doubleValue;
|
|
34
|
+
NSColor *color = temperature >= 90 ? NSColor.systemRedColor :
|
|
35
|
+
(temperature >= 80 ? [NSColor colorWithRed:1.00 green:0.35 blue:0.16 alpha:1] :
|
|
36
|
+
[NSColor colorWithRed:1.00 green:107.0 / 255.0 blue:74.0 / 255.0 alpha:1]);
|
|
37
|
+
[metrics addObject:@{@"symbol": @"thermometer.medium", @"label": value,
|
|
38
|
+
@"color": color}];
|
|
39
|
+
}
|
|
40
|
+
if (self.systemMemoryEnabled) {
|
|
41
|
+
NSString *value = self.systemMemoryPercent
|
|
42
|
+
? [NSString stringWithFormat:@"%.0f%%", self.systemMemoryPercent.doubleValue] : @"--";
|
|
43
|
+
[metrics addObject:@{@"symbol": @"memorychip", @"label": [NSString stringWithFormat:@"MEM %@", value],
|
|
44
|
+
@"color": [NSColor colorWithRed:32.0 / 255.0 green:214.0 / 255.0
|
|
45
|
+
blue:165.0 / 255.0 alpha:1]}];
|
|
46
|
+
}
|
|
47
|
+
NSFont *font = [NSFont systemFontOfSize:13 weight:NSFontWeightSemibold];
|
|
48
|
+
NSMutableAttributedString *statusLine = [NSMutableAttributedString new];
|
|
49
|
+
for (NSUInteger index = 0; index < metrics.count; index++) {
|
|
50
|
+
NSDictionary *metric = metrics[index];
|
|
51
|
+
NSString *label = metric[@"label"];
|
|
52
|
+
NSColor *color = metric[@"color"];
|
|
53
|
+
NSDictionary *attributes = @{NSFontAttributeName: font,
|
|
54
|
+
NSForegroundColorAttributeName: [self textColor:color]};
|
|
55
|
+
NSImage *icon = [NSImage imageWithSystemSymbolName:metric[@"symbol"]
|
|
56
|
+
accessibilityDescription:label];
|
|
57
|
+
NSImageSymbolConfiguration *sizeConfiguration =
|
|
58
|
+
[NSImageSymbolConfiguration configurationWithPointSize:
|
|
59
|
+
[metric[@"symbol"] isEqualToString:@"thermometer.medium"] ? 16 : 14
|
|
60
|
+
weight:NSFontWeightSemibold];
|
|
61
|
+
NSImageSymbolConfiguration *colorConfiguration =
|
|
62
|
+
[NSImageSymbolConfiguration configurationWithHierarchicalColor:metric[@"color"]];
|
|
63
|
+
icon = [icon imageWithSymbolConfiguration:
|
|
64
|
+
[sizeConfiguration configurationByApplyingConfiguration:colorConfiguration]];
|
|
65
|
+
NSTextAttachment *attachment = [NSTextAttachment new];
|
|
66
|
+
attachment.image = icon;
|
|
67
|
+
CGFloat iconSize = [metric[@"symbol"] isEqualToString:@"thermometer.medium"] ? 18 : 16;
|
|
68
|
+
attachment.bounds = NSMakeRect(0, (font.capHeight - iconSize) / 2.0,
|
|
69
|
+
iconSize, iconSize);
|
|
70
|
+
[statusLine appendAttributedString:[NSAttributedString
|
|
71
|
+
attributedStringWithAttachment:attachment]];
|
|
72
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
73
|
+
initWithString:@" " attributes:attributes]];
|
|
74
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
75
|
+
initWithString:label attributes:attributes]];
|
|
76
|
+
if (index + 1 < metrics.count) {
|
|
77
|
+
NSDictionary *separatorAttributes = @{NSFontAttributeName:
|
|
78
|
+
[NSFont systemFontOfSize:12 weight:NSFontWeightRegular],
|
|
79
|
+
NSForegroundColorAttributeName: [NSColor colorWithWhite:0.42 alpha:0.7]};
|
|
80
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
81
|
+
initWithString:@" │ " attributes:separatorAttributes]];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
CGFloat width = ceil(statusLine.size.width);
|
|
85
|
+
[statusLine drawInRect:NSMakeRect(rightEdge - width, y, width + 1, 20)];
|
|
86
|
+
}
|
|
87
|
+
- (NSString *)formattedTokenSplitForUsage:(NSDictionary *)usage key:(NSString *)key
|
|
88
|
+
wantsOutput:(BOOL)wantsOutput {
|
|
89
|
+
return [self formattedTokenSplit:[self tokenTotals:usage key:key] ?: @{}
|
|
90
|
+
wantsOutput:wantsOutput];
|
|
91
|
+
}
|
|
92
|
+
- (NSNumber *)usedForQuota:(NSDictionary *)quota usedKey:(NSString *)usedKey {
|
|
93
|
+
NSNumber *used = [quota[usedKey] isKindOfClass:NSNumber.class] ? quota[usedKey] : nil;
|
|
94
|
+
return used ? @(fmax(0, fmin(100, used.doubleValue))) : nil;
|
|
95
|
+
}
|
|
96
|
+
- (NSString *)refreshAgeText {
|
|
97
|
+
if (self.lastUpdatedAt <= 0) return L(@"Waiting for data");
|
|
98
|
+
NSTimeInterval age = fmax(0, NSDate.date.timeIntervalSince1970 - self.lastUpdatedAt);
|
|
99
|
+
if (age < 5) return L(@"just now");
|
|
100
|
+
if (age < 60) return [NSString stringWithFormat:L(@"%.0fs ago"), age];
|
|
101
|
+
if (age < 3600) return [NSString stringWithFormat:L(@"%.0fm ago"), floor(age / 60)];
|
|
102
|
+
return [NSString stringWithFormat:L(@"%.0fh ago"), floor(age / 3600)];
|
|
103
|
+
}
|
|
104
|
+
// 两个主题共用字号、字重和等宽数字,切换主题时保持文字大小一致。
|
|
105
|
+
// 小字统一抬到逻辑 14(屏幕约 8.1pt),这是 440pt 宽面板在不改版式的前提下放得下的上限;
|
|
106
|
+
// 再往上要放大面板或删行。数字用等宽字形,跳动时不左右晃;汉字不受影响。
|
|
107
|
+
- (NSFont *)fontWithSize:(CGFloat)size weight:(NSFontWeight)weight {
|
|
108
|
+
if (size < 14) size = 14;
|
|
109
|
+
else if (size <= 16) size += 1;
|
|
110
|
+
if (weight < NSFontWeightMedium) weight = NSFontWeightMedium;
|
|
111
|
+
else if (weight < NSFontWeightSemibold) weight = NSFontWeightSemibold;
|
|
112
|
+
else if (weight < NSFontWeightBold) weight = NSFontWeightBold;
|
|
113
|
+
return [NSFont monospacedDigitSystemFontOfSize:size weight:weight];
|
|
114
|
+
}
|
|
115
|
+
// 局部衬底:只垫在卡片和汇总区下面,面板边缘和卡片间隙保持通透。衬底本身不能带
|
|
116
|
+
// 文字投影,否则卡片边缘会多一圈黑晕。
|
|
117
|
+
// 原生玻璃下在文字前画一个彩色小圆点,替代原来的彩色字;经典主题照旧画彩色字。
|
|
118
|
+
- (void)drawText:(NSString *)text inRect:(NSRect)rect size:(CGFloat)size
|
|
119
|
+
weight:(NSFontWeight)weight color:(NSColor *)color dotColor:(NSColor *)dotColor {
|
|
120
|
+
if (!self.usesLiquidGlass) {
|
|
121
|
+
NSMutableDictionary *attributes = [[self textAttributesWithSize:size color:color
|
|
122
|
+
weight:weight] mutableCopy];
|
|
123
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
124
|
+
style.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
125
|
+
attributes[NSParagraphStyleAttributeName] = style;
|
|
126
|
+
[text drawInRect:rect withAttributes:[self attributes:attributes fittingText:text
|
|
127
|
+
inWidth:NSWidth(rect)]];
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
NSMutableDictionary *attributes = [[self textAttributesWithSize:size color:color
|
|
131
|
+
weight:weight] mutableCopy];
|
|
132
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
133
|
+
style.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
134
|
+
attributes[NSParagraphStyleAttributeName] = style;
|
|
135
|
+
NSFont *font = attributes[NSFontAttributeName];
|
|
136
|
+
NSRect textRect = rect;
|
|
137
|
+
if (dotColor) {
|
|
138
|
+
CGFloat diameter = 9;
|
|
139
|
+
// drawInRect: 从矩形顶部开始排版,圆点对齐到首行小写字母的中线。
|
|
140
|
+
CGFloat baselineMid = NSMaxY(rect) - font.ascender + font.xHeight / 2.0;
|
|
141
|
+
[NSGraphicsContext saveGraphicsState];
|
|
142
|
+
[[NSShadow new] set];
|
|
143
|
+
[dotColor setFill];
|
|
144
|
+
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(NSMinX(rect), baselineMid - diameter / 2.0,
|
|
145
|
+
diameter, diameter)] fill];
|
|
146
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
147
|
+
textRect.origin.x += diameter + 6;
|
|
148
|
+
textRect.size.width -= diameter + 6;
|
|
149
|
+
}
|
|
150
|
+
[text drawInRect:textRect withAttributes:[self attributes:attributes fittingText:text
|
|
151
|
+
inWidth:NSWidth(textRect)]];
|
|
152
|
+
}
|
|
153
|
+
// 译文长短差得多,固定宽度的格子迟早有放不下的语言。放不下时先把字号往下收,最多收 2pt
|
|
154
|
+
// (liquid 下即从 14 收到 12)——比截成"…"好认;还放不下才交给截断。放得下就原样返回,
|
|
155
|
+
// 中文和多数英文不受影响。
|
|
156
|
+
- (NSDictionary *)attributes:(NSDictionary *)attributes fittingText:(NSString *)text
|
|
157
|
+
inWidth:(CGFloat)width {
|
|
158
|
+
NSFont *font = attributes[NSFontAttributeName];
|
|
159
|
+
if (!font || text.length == 0 || [text sizeWithAttributes:attributes].width <= width) {
|
|
160
|
+
return attributes;
|
|
161
|
+
}
|
|
162
|
+
NSMutableDictionary *fitted = [attributes mutableCopy];
|
|
163
|
+
for (CGFloat size = font.pointSize - 0.5; size >= font.pointSize - 2; size -= 0.5) {
|
|
164
|
+
fitted[NSFontAttributeName] = [NSFont fontWithDescriptor:font.fontDescriptor size:size];
|
|
165
|
+
if ([text sizeWithAttributes:fitted].width <= width) break;
|
|
166
|
+
}
|
|
167
|
+
return fitted;
|
|
168
|
+
}
|
|
169
|
+
// 风险提示只保留文字,和 Max / Now 使用同一条左边界。
|
|
170
|
+
- (void)drawTip:(NSString *)text inRect:(NSRect)rect color:(NSColor *)color {
|
|
171
|
+
if (text.length == 0) return;
|
|
172
|
+
[self drawText:text inRect:rect size:11 weight:NSFontWeightMedium color:color dotColor:nil];
|
|
173
|
+
}
|
|
174
|
+
- (void)fillLiquidScrim:(NSBezierPath *)path {
|
|
175
|
+
[NSGraphicsContext saveGraphicsState];
|
|
176
|
+
[[NSShadow new] set];
|
|
177
|
+
[[NSColor colorWithWhite:0 alpha:self.cardScrimAlpha] setFill];
|
|
178
|
+
[path fill];
|
|
179
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
180
|
+
}
|
|
181
|
+
- (NSColor *)textColor:(NSColor *)color {
|
|
182
|
+
if (!self.usesLiquidGlass) return color;
|
|
183
|
+
NSColor *rgb = [color colorUsingColorSpace:NSColorSpace.sRGBColorSpace];
|
|
184
|
+
if (!rgb) return color;
|
|
185
|
+
CGFloat r, g, b, a;
|
|
186
|
+
[rgb getRed:&r green:&g blue:&b alpha:&a];
|
|
187
|
+
CGFloat saturation = MAX(r, MAX(g, b)) - MIN(r, MIN(g, b));
|
|
188
|
+
if (saturation < 0.05) return [NSColor colorWithWhite:MAX(MAX(r, MAX(g, b)), 0.84) alpha:a];
|
|
189
|
+
// 彩色字在任意亮度的背景上对比都不稳定,原生玻璃下文字一律用白色;颜色的语义改由
|
|
190
|
+
// 字前的小圆点、图标和曲线承担(见 drawText:inRect:size:weight:color:dotColor:)。
|
|
191
|
+
return [NSColor colorWithWhite:0.96 alpha:a];
|
|
192
|
+
}
|
|
193
|
+
- (NSDictionary *)textAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight {
|
|
194
|
+
return @{NSFontAttributeName: [self fontWithSize:size weight:weight],
|
|
195
|
+
NSForegroundColorAttributeName: [self textColor:color]};
|
|
196
|
+
}
|
|
197
|
+
- (NSDictionary *)rightAlignedTextAttributesWithSize:(CGFloat)size color:(NSColor *)color
|
|
198
|
+
weight:(NSFontWeight)weight {
|
|
199
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
200
|
+
style.alignment = NSTextAlignmentRight;
|
|
201
|
+
return @{NSFontAttributeName: [self fontWithSize:size weight:weight],
|
|
202
|
+
NSForegroundColorAttributeName: [self textColor:color],
|
|
203
|
+
NSParagraphStyleAttributeName: style};
|
|
204
|
+
}
|
|
205
|
+
- (NSDictionary *)centeredTextAttributesWithSize:(CGFloat)size color:(NSColor *)color weight:(NSFontWeight)weight {
|
|
206
|
+
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
207
|
+
style.alignment = NSTextAlignmentCenter;
|
|
208
|
+
return @{NSFontAttributeName: [self fontWithSize:size weight:weight],
|
|
209
|
+
NSForegroundColorAttributeName: [self textColor:color],
|
|
210
|
+
NSParagraphStyleAttributeName: style};
|
|
211
|
+
}
|
|
212
|
+
- (void)fillRoundedRect:(NSRect)rect radius:(CGFloat)radius color:(NSColor *)color {
|
|
213
|
+
[color setFill];
|
|
214
|
+
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius] fill];
|
|
215
|
+
}
|
|
216
|
+
- (NSArray<NSDictionary *> *)curvePointsFromHistory:(NSArray<NSDictionary *> *)history
|
|
217
|
+
currentUsed:(NSNumber *)currentUsed {
|
|
218
|
+
// 历史只能补充当前快照,不能替代当前快照。当前窗口还没有采集到额度时继续画旧历史,
|
|
219
|
+
// 会让“等待数据”的卡片同时出现一条悬在中间的曲线;此时只保留图表底部基线。
|
|
220
|
+
if (!currentUsed) return @[];
|
|
221
|
+
|
|
222
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
223
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
224
|
+
NSMutableArray<NSDictionary *> *points = [NSMutableArray array];
|
|
225
|
+
for (NSDictionary *sample in history) {
|
|
226
|
+
if (![sample isKindOfClass:NSDictionary.class]) continue;
|
|
227
|
+
NSNumber *timestamp = [sample[@"timestamp"] isKindOfClass:NSNumber.class]
|
|
228
|
+
? sample[@"timestamp"] : nil;
|
|
229
|
+
NSNumber *remaining = [sample[@"remaining"] isKindOfClass:NSNumber.class]
|
|
230
|
+
? sample[@"remaining"] : nil;
|
|
231
|
+
if (!timestamp || !remaining || timestamp.doubleValue < cutoff) continue;
|
|
232
|
+
double used = 100.0 - fmax(0, fmin(100, remaining.doubleValue));
|
|
233
|
+
[points addObject:@{@"timestamp": timestamp, @"used": @(used)}];
|
|
234
|
+
}
|
|
235
|
+
// 终点必须和卡片上的当前官方额度同源;历史落盘最多会慢 15 分钟,不能直接拿它
|
|
236
|
+
// 冒充“当前”,否则同一张卡里会出现两个不同百分比。
|
|
237
|
+
[points addObject:@{@"timestamp": @(now), @"used": currentUsed}];
|
|
238
|
+
return points;
|
|
239
|
+
}
|
|
240
|
+
- (void)appendCurveFromPoint:(NSPoint)from toPoint:(NSPoint)to path:(NSBezierPath *)path {
|
|
241
|
+
CGFloat controlOffset = (to.x - from.x) / 3.0;
|
|
242
|
+
[path curveToPoint:to
|
|
243
|
+
controlPoint1:NSMakePoint(from.x + controlOffset, from.y)
|
|
244
|
+
controlPoint2:NSMakePoint(to.x - controlOffset, to.y)];
|
|
245
|
+
}
|
|
246
|
+
- (void)drawCurveSegment:(NSArray<NSValue *> *)segment inRect:(NSRect)rect color:(NSColor *)color {
|
|
247
|
+
if (segment.count == 0) return;
|
|
248
|
+
NSBezierPath *line = [NSBezierPath bezierPath];
|
|
249
|
+
NSPoint first = segment.firstObject.pointValue;
|
|
250
|
+
[line moveToPoint:first];
|
|
251
|
+
NSPoint previous = first;
|
|
252
|
+
for (NSUInteger index = 1; index < segment.count; index++) {
|
|
253
|
+
NSPoint point = segment[index].pointValue;
|
|
254
|
+
[self appendCurveFromPoint:previous toPoint:point path:line];
|
|
255
|
+
previous = point;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
NSBezierPath *fill = [line copy];
|
|
259
|
+
[fill lineToPoint:NSMakePoint(previous.x, NSMinY(rect))];
|
|
260
|
+
[fill lineToPoint:NSMakePoint(first.x, NSMinY(rect))];
|
|
261
|
+
[fill closePath];
|
|
262
|
+
[NSGraphicsContext saveGraphicsState];
|
|
263
|
+
[fill addClip];
|
|
264
|
+
NSGradient *area = [[NSGradient alloc]
|
|
265
|
+
initWithStartingColor:[color colorWithAlphaComponent:0.34]
|
|
266
|
+
endingColor:[color colorWithAlphaComponent:0.025]];
|
|
267
|
+
[area drawFromPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))
|
|
268
|
+
toPoint:NSMakePoint(NSMidX(rect), NSMinY(rect)) options:0];
|
|
269
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
270
|
+
|
|
271
|
+
[[color colorWithAlphaComponent:0.95] setStroke];
|
|
272
|
+
line.lineWidth = 2;
|
|
273
|
+
[line stroke];
|
|
274
|
+
}
|
|
275
|
+
- (void)drawUsageCurve:(NSArray<NSDictionary *> *)points inRect:(NSRect)rect
|
|
276
|
+
color:(NSColor *)color {
|
|
277
|
+
[[NSColor colorWithWhite:1 alpha:0.08] setStroke];
|
|
278
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))
|
|
279
|
+
toPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
|
|
280
|
+
if (points.count == 0) return;
|
|
281
|
+
|
|
282
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
283
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
284
|
+
// 曲线始终连成一段:采样缺口只代表桌宠当时没在运行,不代表没有消耗,
|
|
285
|
+
// 缺口两端直接插值比断开更接近真实轨迹;窗口重置带来的回落本身也是真实变化。
|
|
286
|
+
NSMutableArray<NSValue *> *allPoints = [NSMutableArray arrayWithCapacity:points.count];
|
|
287
|
+
for (NSDictionary *sample in points) {
|
|
288
|
+
NSTimeInterval timestamp = [sample[@"timestamp"] doubleValue];
|
|
289
|
+
double used = fmax(0, fmin(100, [sample[@"used"] doubleValue]));
|
|
290
|
+
CGFloat x = NSMinX(rect) + NSWidth(rect) * (timestamp - cutoff) / (now - cutoff);
|
|
291
|
+
CGFloat y = NSMinY(rect) + NSHeight(rect) * used / 100.0;
|
|
292
|
+
[allPoints addObject:[NSValue valueWithPoint:NSMakePoint(
|
|
293
|
+
fmax(NSMinX(rect), fmin(NSMaxX(rect), x)), y)]];
|
|
294
|
+
}
|
|
295
|
+
[self drawCurveSegment:allPoints inRect:rect color:color];
|
|
296
|
+
}
|
|
297
|
+
- (void)drawMonthlyTokenCurve:(NSArray<NSDictionary *> *)series inRect:(NSRect)rect
|
|
298
|
+
color:(NSColor *)color {
|
|
299
|
+
[[NSColor colorWithWhite:1 alpha:0.08] setStroke];
|
|
300
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))
|
|
301
|
+
toPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
|
|
302
|
+
if (series.count == 0) return;
|
|
303
|
+
// 横轴铺满整个自然月,曲线只画到今天:把月末还没到的日子当成 0 画出去,看起来会像
|
|
304
|
+
// 用量突然归零。留白本身就是"这个月还剩多少天"的刻度。
|
|
305
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
306
|
+
double peak = 0;
|
|
307
|
+
NSUInteger elapsed = 0;
|
|
308
|
+
for (NSDictionary *day in series) {
|
|
309
|
+
if ([day[@"timestamp"] doubleValue] > now) break;
|
|
310
|
+
NSDictionary *totals = [day[@"totals"] isKindOfClass:NSDictionary.class]
|
|
311
|
+
? day[@"totals"] : nil;
|
|
312
|
+
peak = fmax(peak, [totals[@"total_tokens"] doubleValue]);
|
|
313
|
+
elapsed += 1;
|
|
314
|
+
}
|
|
315
|
+
if (peak <= 0 || elapsed == 0) return;
|
|
316
|
+
NSMutableArray<NSValue *> *points = [NSMutableArray arrayWithCapacity:elapsed];
|
|
317
|
+
for (NSUInteger index = 0; index < elapsed; index++) {
|
|
318
|
+
NSDictionary *totals = [series[index][@"totals"] isKindOfClass:NSDictionary.class]
|
|
319
|
+
? series[index][@"totals"] : nil;
|
|
320
|
+
double value = [totals[@"total_tokens"] doubleValue];
|
|
321
|
+
CGFloat progress = series.count > 1 ? (CGFloat)index / (series.count - 1) : 1;
|
|
322
|
+
[points addObject:[NSValue valueWithPoint:NSMakePoint(
|
|
323
|
+
NSMinX(rect) + NSWidth(rect) * progress,
|
|
324
|
+
NSMinY(rect) + NSHeight(rect) * value / peak)]];
|
|
325
|
+
}
|
|
326
|
+
[self drawCurveSegment:points inRect:rect color:color];
|
|
327
|
+
}
|
|
328
|
+
@end
|