cc-pets 1.2.5 → 1.2.7
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/Sources/CCPets/CCPetsAppDelegate.h +4 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +72 -1
- package/Sources/CCPets/CCPetsEvents.m +7 -0
- package/Sources/CCPets/CCPetsPaths.h +3 -0
- package/Sources/CCPets/CCPetsPaths.m +3 -0
- package/Sources/CCPets/CCPetsPreviews.m +20 -2
- package/Sources/CCPets/CCPetsQuotaHistory.h +2 -1
- package/Sources/CCPets/CCPetsQuotaHistory.m +7 -3
- package/Sources/CCPets/CCPetsSystemMonitor.h +8 -0
- package/Sources/CCPets/CCPetsSystemMonitor.m +260 -0
- package/Sources/CCPets/PetView.m +15 -0
- package/Sources/CCPets/QuotaDashboardView.h +8 -2
- package/Sources/CCPets/QuotaDashboardView.m +253 -35
- package/package.json +1 -1
- package/scripts/build.sh +1 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#import "PetView.h"
|
|
3
3
|
#import "QuotaDashboardView.h"
|
|
4
4
|
#import "CCPetsUsageMonitor.h"
|
|
5
|
+
#import "CCPetsSystemMonitor.h"
|
|
5
6
|
|
|
6
7
|
@interface AppDelegate : NSObject <NSApplicationDelegate>
|
|
7
8
|
@property NSPanel *panel;
|
|
@@ -45,6 +46,8 @@
|
|
|
45
46
|
@property NSTask *updateTask;
|
|
46
47
|
@property NSTimer *usageTimer;
|
|
47
48
|
@property NSTimer *quotaClockTimer;
|
|
49
|
+
@property NSTimer *systemMetricsTimer;
|
|
50
|
+
@property CCPetsSystemMonitor *systemMonitor;
|
|
48
51
|
- (void)showQuotaDashboard;
|
|
49
52
|
- (void)positionQuotaDashboard;
|
|
50
53
|
- (void)scheduleQuotaDashboardHide;
|
|
@@ -54,6 +57,7 @@
|
|
|
54
57
|
- (void)prunePendingApprovalRecords;
|
|
55
58
|
- (void)toggleStatusBubbleFromMenu:(NSButton *)sender;
|
|
56
59
|
- (void)refreshUsage:(id)sender;
|
|
60
|
+
- (void)toggleSystemMetric:(NSButton *)sender;
|
|
57
61
|
- (void)applyCodexUsage:(NSDictionary *)codexUsage claudeUsage:(NSDictionary *)claudeUsage;
|
|
58
62
|
- (void)switchPetToID:(NSString *)petID;
|
|
59
63
|
@end
|
|
@@ -260,6 +260,30 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
260
260
|
self.quotaView.historyEnabled = enabled;
|
|
261
261
|
[self refreshUsage:nil];
|
|
262
262
|
}
|
|
263
|
+
- (NSString *)systemMetricKeyForTag:(NSInteger)tag {
|
|
264
|
+
if (tag == 1) return SystemCPUEnabledKey;
|
|
265
|
+
if (tag == 2) return SystemTemperatureEnabledKey;
|
|
266
|
+
if (tag == 3) return SystemMemoryEnabledKey;
|
|
267
|
+
return nil;
|
|
268
|
+
}
|
|
269
|
+
- (void)applySystemMetricPreferences {
|
|
270
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
271
|
+
self.quotaView.systemCPUEnabled = [defaults boolForKey:SystemCPUEnabledKey];
|
|
272
|
+
self.quotaView.systemTemperatureEnabled =
|
|
273
|
+
[defaults boolForKey:SystemTemperatureEnabledKey];
|
|
274
|
+
self.quotaView.systemMemoryEnabled = [defaults boolForKey:SystemMemoryEnabledKey];
|
|
275
|
+
self.quotaView.needsDisplay = YES;
|
|
276
|
+
}
|
|
277
|
+
- (void)toggleSystemMetric:(NSButton *)sender {
|
|
278
|
+
NSString *key = [self systemMetricKeyForTag:sender.tag];
|
|
279
|
+
if (!key) return;
|
|
280
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
281
|
+
BOOL enabled = ![defaults boolForKey:key];
|
|
282
|
+
[defaults setBool:enabled forKey:key];
|
|
283
|
+
sender.state = enabled ? NSControlStateValueOn : NSControlStateValueOff;
|
|
284
|
+
[self applySystemMetricPreferences];
|
|
285
|
+
[self updateSystemMetricsTimer];
|
|
286
|
+
}
|
|
263
287
|
// 开关打开时立刻导入一次,不用等下次启动——用户点开关就是想现在看到那些素材。
|
|
264
288
|
- (void)toggleImportCodexPets:(NSButton *)sender {
|
|
265
289
|
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
@@ -372,7 +396,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
372
396
|
- (NSString *)statusDetailForState:(NSString *)state {
|
|
373
397
|
if ([state isEqualToString:@"starting"]) return @"正在准备当前会话…";
|
|
374
398
|
if ([state isEqualToString:@"idle"]) return @"会话已就绪,等待你的下一条指令。";
|
|
375
|
-
if ([state isEqualToString:@"thinking"]) return @"
|
|
399
|
+
if ([state isEqualToString:@"thinking"]) return @"正在分析需求…";
|
|
376
400
|
if ([state isEqualToString:@"auto_review"]) return @"正在由 Auto-review 审核,无需手动操作。";
|
|
377
401
|
if ([state isEqualToString:@"approval"]) return @"请回到终端确认操作。";
|
|
378
402
|
if ([state isEqualToString:@"subagent"]) return @"正在协调子 Agent。";
|
|
@@ -712,6 +736,11 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
712
736
|
}
|
|
713
737
|
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
714
738
|
[defaults registerDefaults:@{StatusBubbleExpandedKey: @YES}];
|
|
739
|
+
[defaults registerDefaults:@{
|
|
740
|
+
SystemCPUEnabledKey: @YES,
|
|
741
|
+
SystemTemperatureEnabledKey: @YES,
|
|
742
|
+
SystemMemoryEnabledKey: @YES
|
|
743
|
+
}];
|
|
715
744
|
if (![defaults boolForKey:StatusBubblePreferenceV2Key]) {
|
|
716
745
|
[defaults setBool:YES forKey:StatusBubbleExpandedKey];
|
|
717
746
|
[defaults setBool:YES forKey:StatusBubblePreferenceV2Key];
|
|
@@ -879,6 +908,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
879
908
|
[quotaRoot addSubview:glass];
|
|
880
909
|
self.quotaView = [[QuotaDashboardView alloc] initWithFrame:NSMakeRect(0, 0, quotaSize.width, quotaSize.height)];
|
|
881
910
|
self.quotaView.historyEnabled = [defaults boolForKey:HistoryEnabledKey];
|
|
911
|
+
[self applySystemMetricPreferences];
|
|
882
912
|
self.quotaView.codexLogo = OfficialAppIcon(@"com.openai.codex", @"icon-chatgpt.icns");
|
|
883
913
|
self.quotaView.claudeLogo = OfficialAppIcon(@"com.anthropic.claudefordesktop", @"electron.icns");
|
|
884
914
|
self.quotaView.hoverChanged = ^(BOOL hovering) {
|
|
@@ -896,6 +926,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
896
926
|
[self.panel setFrameOrigin:NSMakePoint(NSMaxX(visible) - size.width - 24, NSMinY(visible) + 18)];
|
|
897
927
|
[self.panel orderFrontRegardless];
|
|
898
928
|
self.usageMonitor = [CCPetsUsageMonitor new];
|
|
929
|
+
self.systemMonitor = [CCPetsSystemMonitor new];
|
|
899
930
|
self.agentEventPartialLine = [NSMutableData data];
|
|
900
931
|
self.pendingApprovalRecords = [NSMutableDictionary dictionary];
|
|
901
932
|
self.usageMonitor.changeHandler = ^(NSDictionary *codexUsage, NSDictionary *claudeUsage) {
|
|
@@ -949,6 +980,42 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
949
980
|
[self.panel orderFrontRegardless];
|
|
950
981
|
[self rescheduleUsageTimer];
|
|
951
982
|
[self startQuotaClock];
|
|
983
|
+
[self updateSystemMetricsTimer];
|
|
984
|
+
}
|
|
985
|
+
- (BOOL)hasEnabledSystemMetric {
|
|
986
|
+
return self.quotaView.systemCPUEnabled || self.quotaView.systemTemperatureEnabled ||
|
|
987
|
+
self.quotaView.systemMemoryEnabled;
|
|
988
|
+
}
|
|
989
|
+
- (void)refreshSystemMetrics:(id)sender {
|
|
990
|
+
if (![self hasEnabledSystemMetric]) return;
|
|
991
|
+
BOOL cpuEnabled = self.quotaView.systemCPUEnabled;
|
|
992
|
+
BOOL memoryEnabled = self.quotaView.systemMemoryEnabled;
|
|
993
|
+
BOOL temperatureEnabled = self.quotaView.systemTemperatureEnabled;
|
|
994
|
+
__weak typeof(self) weakSelf = self;
|
|
995
|
+
[self.systemMonitor sampleCPU:cpuEnabled memory:memoryEnabled
|
|
996
|
+
temperature:temperatureEnabled completion:^(NSNumber *cpu, NSNumber *memory,
|
|
997
|
+
NSNumber *temperature) {
|
|
998
|
+
typeof(self) strongSelf = weakSelf;
|
|
999
|
+
if (!strongSelf) return;
|
|
1000
|
+
if (cpuEnabled) strongSelf.quotaView.systemCPUPercent = cpu;
|
|
1001
|
+
if (memoryEnabled) strongSelf.quotaView.systemMemoryPercent = memory;
|
|
1002
|
+
if (temperatureEnabled) strongSelf.quotaView.systemTemperatureCelsius = temperature;
|
|
1003
|
+
strongSelf.quotaView.needsDisplay = YES;
|
|
1004
|
+
}];
|
|
1005
|
+
}
|
|
1006
|
+
- (void)updateSystemMetricsTimer {
|
|
1007
|
+
BOOL shouldRun = self.quotaPanel.isVisible && [self hasEnabledSystemMetric];
|
|
1008
|
+
if (!shouldRun) {
|
|
1009
|
+
[self.systemMetricsTimer invalidate];
|
|
1010
|
+
self.systemMetricsTimer = nil;
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
if (self.systemMetricsTimer.isValid) return;
|
|
1014
|
+
[self refreshSystemMetrics:nil];
|
|
1015
|
+
__weak typeof(self) weakSelf = self;
|
|
1016
|
+
self.systemMetricsTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES
|
|
1017
|
+
block:^(NSTimer *timer) { [weakSelf refreshSystemMetrics:timer]; }];
|
|
1018
|
+
self.systemMetricsTimer.tolerance = 0.4;
|
|
952
1019
|
}
|
|
953
1020
|
// "数据刷新" 显示的是相对时间,没有新数据也得自己走字。面板隐藏时不需要这个定时器,
|
|
954
1021
|
// 隐藏的窗口本来就不会重绘。
|
|
@@ -962,6 +1029,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
962
1029
|
if (!self.quotaPanel.isVisible) {
|
|
963
1030
|
[self.quotaClockTimer invalidate];
|
|
964
1031
|
self.quotaClockTimer = nil;
|
|
1032
|
+
[self updateSystemMetricsTimer];
|
|
965
1033
|
return;
|
|
966
1034
|
}
|
|
967
1035
|
self.quotaView.needsDisplay = YES;
|
|
@@ -1026,6 +1094,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
1026
1094
|
[self rescheduleUsageTimer];
|
|
1027
1095
|
[self.quotaClockTimer invalidate];
|
|
1028
1096
|
self.quotaClockTimer = nil;
|
|
1097
|
+
[self updateSystemMetricsTimer];
|
|
1029
1098
|
}
|
|
1030
1099
|
}
|
|
1031
1100
|
- (void)switchPetToID:(NSString *)petID {
|
|
@@ -1253,6 +1322,8 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
1253
1322
|
}
|
|
1254
1323
|
- (void)applicationWillTerminate:(NSNotification *)notification {
|
|
1255
1324
|
[self.usageMonitor stop];
|
|
1325
|
+
[self.systemMetricsTimer invalidate];
|
|
1326
|
+
self.systemMetricsTimer = nil;
|
|
1256
1327
|
if (self.agentEventSource) {
|
|
1257
1328
|
dispatch_source_cancel(self.agentEventSource);
|
|
1258
1329
|
self.agentEventSource = nil;
|
|
@@ -172,6 +172,13 @@ int RecordHookEvent(void) {
|
|
|
172
172
|
if (![payload isKindOfClass:NSDictionary.class]) return EXIT_SUCCESS;
|
|
173
173
|
NSString *event = payload[@"hook_event_name"];
|
|
174
174
|
if (![event isKindOfClass:NSString.class] || event.length == 0) return EXIT_SUCCESS;
|
|
175
|
+
// Claude Agent SDK / ACP 也会执行用户级 Hooks。IDE 或终端仅仅创建、销毁这类
|
|
176
|
+
// 后台会话时就会发 SessionStart / SessionEnd,但它们不代表用户启动了 Agent
|
|
177
|
+
// 任务或完成了一轮工作。真实 CLI 的启动状态由包装脚本显式写入,任务完成则由
|
|
178
|
+
// Stop 表示,因此这里忽略纯会话生命周期事件,避免普通终端开关触发误报。
|
|
179
|
+
if ([event isEqualToString:@"SessionStart"] || [event isEqualToString:@"SessionEnd"]) {
|
|
180
|
+
return EXIT_SUCCESS;
|
|
181
|
+
}
|
|
175
182
|
NSString *tool = [payload[@"tool_name"] isKindOfClass:NSString.class] ? payload[@"tool_name"] : @"";
|
|
176
183
|
NSString *session = SanitizedShortString(payload[@"session_id"], 128);
|
|
177
184
|
id toolResult = payload[@"tool_response"] ?: payload[@"tool_result"];
|
|
@@ -31,6 +31,9 @@ extern NSString *const NotificationApprovalKey;
|
|
|
31
31
|
extern NSString *const StatusBubbleExpandedKey;
|
|
32
32
|
extern NSString *const StatusBubblePreferenceV2Key;
|
|
33
33
|
extern NSString *const ImportCodexPetsKey;
|
|
34
|
+
extern NSString *const SystemCPUEnabledKey;
|
|
35
|
+
extern NSString *const SystemTemperatureEnabledKey;
|
|
36
|
+
extern NSString *const SystemMemoryEnabledKey;
|
|
34
37
|
|
|
35
38
|
extern const NSTimeInterval AgentStatusInactivityInterval;
|
|
36
39
|
extern const NSTimeInterval AgentStatusOrphanInterval;
|
|
@@ -169,6 +169,9 @@ NSString *const NotificationApprovalKey = @"CCPetsNotifyApproval";
|
|
|
169
169
|
NSString *const StatusBubbleExpandedKey = @"CCPetsStatusBubbleExpanded";
|
|
170
170
|
NSString *const StatusBubblePreferenceV2Key = @"CCPetsStatusBubblePreferenceV2";
|
|
171
171
|
NSString *const ImportCodexPetsKey = @"CCPetsImportCodexPets";
|
|
172
|
+
NSString *const SystemCPUEnabledKey = @"CCPetsSystemCPUEnabled";
|
|
173
|
+
NSString *const SystemTemperatureEnabledKey = @"CCPetsSystemTemperatureEnabled";
|
|
174
|
+
NSString *const SystemMemoryEnabledKey = @"CCPetsSystemMemoryEnabled";
|
|
172
175
|
const NSTimeInterval AgentStatusInactivityInterval = 60.0;
|
|
173
176
|
// 客户端进程已经消失、但状态气泡还停在它最后一条事件上时,多等这么久再清场。
|
|
174
177
|
// 留这段宽限是因为不经包装脚本启动的客户端(直接跑 claude / codex)没有 pid
|
|
@@ -33,8 +33,26 @@ int RenderQuotaDashboard(NSString *path) {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
view.historyEnabled = YES;
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
37
|
+
NSTimeInterval day = 24 * 60 * 60;
|
|
38
|
+
view.codexHistory = @[
|
|
39
|
+
@{@"timestamp": @(now - 6.5 * day), @"remaining": @88},
|
|
40
|
+
@{@"timestamp": @(now - 5.5 * day), @"remaining": @81},
|
|
41
|
+
@{@"timestamp": @(now - 4.5 * day), @"remaining": @75},
|
|
42
|
+
@{@"timestamp": @(now - 3.5 * day), @"remaining": @68},
|
|
43
|
+
@{@"timestamp": @(now - 2.5 * day), @"remaining": @62},
|
|
44
|
+
@{@"timestamp": @(now - 1.5 * day), @"remaining": @55},
|
|
45
|
+
@{@"timestamp": @(now - 0.5 * day), @"remaining": @49}
|
|
46
|
+
];
|
|
47
|
+
view.claudeHistory = @[
|
|
48
|
+
@{@"timestamp": @(now - 6.5 * day), @"remaining": @92},
|
|
49
|
+
@{@"timestamp": @(now - 5.5 * day), @"remaining": @86},
|
|
50
|
+
@{@"timestamp": @(now - 4.5 * day), @"remaining": @78},
|
|
51
|
+
@{@"timestamp": @(now - 3.5 * day), @"remaining": @72},
|
|
52
|
+
@{@"timestamp": @(now - 2.5 * day), @"remaining": @64},
|
|
53
|
+
@{@"timestamp": @(now - 1.5 * day), @"remaining": @58},
|
|
54
|
+
@{@"timestamp": @(now - 0.5 * day), @"remaining": @51}
|
|
55
|
+
];
|
|
38
56
|
view.activeAgentCount = 1;
|
|
39
57
|
// 预览里刻意只让一家在线,好把"有额度数据但客户端已退出"的离线态也画出来。
|
|
40
58
|
view.liveProviders = [NSSet setWithObject:@"Claude"];
|
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
// RecordQuotaHistory 返回当前文档,调用方直接复用,避免一轮刷新里重复读盘。
|
|
5
5
|
NSDictionary *QuotaHistoryDocument(void);
|
|
6
6
|
NSDictionary *RecordQuotaHistory(NSDictionary *codexUsage, NSDictionary *claudeUsage);
|
|
7
|
-
|
|
7
|
+
// 每个点保留 timestamp 与 weekRemaining,绘图层才能按真实时间轴展示缺口和重置。
|
|
8
|
+
NSArray<NSDictionary *> *QuotaHistorySeries(NSDictionary *document, NSString *provider);
|
|
@@ -75,15 +75,19 @@ NSDictionary *RecordQuotaHistory(NSDictionary *codexUsage, NSDictionary *claudeU
|
|
|
75
75
|
return updated;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
NSArray<
|
|
79
|
-
NSMutableArray<
|
|
78
|
+
NSArray<NSDictionary *> *QuotaHistorySeries(NSDictionary *document, NSString *provider) {
|
|
79
|
+
NSMutableArray<NSDictionary *> *series = [NSMutableArray array];
|
|
80
80
|
for (NSDictionary *sample in document[@"samples"]) {
|
|
81
81
|
if (![sample isKindOfClass:NSDictionary.class]) continue;
|
|
82
|
+
NSNumber *timestamp = [sample[@"timestamp"] isKindOfClass:NSNumber.class]
|
|
83
|
+
? sample[@"timestamp"] : nil;
|
|
82
84
|
NSDictionary *providerSample = [sample[provider] isKindOfClass:NSDictionary.class]
|
|
83
85
|
? sample[provider] : nil;
|
|
84
86
|
NSNumber *remaining = [providerSample[@"weekRemaining"] isKindOfClass:NSNumber.class]
|
|
85
87
|
? providerSample[@"weekRemaining"] : nil;
|
|
86
|
-
if (remaining)
|
|
88
|
+
if (timestamp && remaining) {
|
|
89
|
+
[series addObject:@{@"timestamp": timestamp, @"remaining": remaining}];
|
|
90
|
+
}
|
|
87
91
|
}
|
|
88
92
|
return series;
|
|
89
93
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
@interface CCPetsSystemMonitor : NSObject
|
|
4
|
+
- (void)sampleCPU:(BOOL)cpuEnabled memory:(BOOL)memoryEnabled
|
|
5
|
+
temperature:(BOOL)temperatureEnabled
|
|
6
|
+
completion:(void (^)(NSNumber *cpuPercent, NSNumber *memoryPercent,
|
|
7
|
+
NSNumber *temperatureCelsius))completion;
|
|
8
|
+
@end
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
#import "CCPetsSystemMonitor.h"
|
|
2
|
+
#import <IOKit/IOKitLib.h>
|
|
3
|
+
#import <dlfcn.h>
|
|
4
|
+
#import <mach/mach.h>
|
|
5
|
+
#import <mach/processor_info.h>
|
|
6
|
+
#import <mach/vm_statistics.h>
|
|
7
|
+
|
|
8
|
+
typedef struct {
|
|
9
|
+
uint8_t major;
|
|
10
|
+
uint8_t minor;
|
|
11
|
+
uint8_t build;
|
|
12
|
+
uint8_t reserved;
|
|
13
|
+
uint16_t release;
|
|
14
|
+
} CCPetsSMCVersion;
|
|
15
|
+
|
|
16
|
+
typedef struct {
|
|
17
|
+
uint16_t version;
|
|
18
|
+
uint16_t length;
|
|
19
|
+
uint32_t cpuPLimit;
|
|
20
|
+
uint32_t gpuPLimit;
|
|
21
|
+
uint32_t memPLimit;
|
|
22
|
+
} CCPetsSMCPLimitData;
|
|
23
|
+
|
|
24
|
+
typedef struct {
|
|
25
|
+
uint32_t dataSize;
|
|
26
|
+
uint32_t dataType;
|
|
27
|
+
uint8_t dataAttributes;
|
|
28
|
+
} CCPetsSMCKeyInfoData;
|
|
29
|
+
|
|
30
|
+
typedef struct {
|
|
31
|
+
uint32_t key;
|
|
32
|
+
CCPetsSMCVersion version;
|
|
33
|
+
CCPetsSMCPLimitData pLimitData;
|
|
34
|
+
CCPetsSMCKeyInfoData keyInfo;
|
|
35
|
+
uint8_t result;
|
|
36
|
+
uint8_t status;
|
|
37
|
+
uint8_t data8;
|
|
38
|
+
uint32_t data32;
|
|
39
|
+
uint8_t bytes[32];
|
|
40
|
+
} CCPetsSMCParamStruct;
|
|
41
|
+
|
|
42
|
+
static uint32_t CCPetsFourCC(NSString *text) {
|
|
43
|
+
if (text.length != 4) return 0;
|
|
44
|
+
uint32_t value = 0;
|
|
45
|
+
for (NSUInteger index = 0; index < 4; index++) {
|
|
46
|
+
value = (value << 8) | (uint8_t)[text characterAtIndex:index];
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static NSNumber *CCPetsReadSMCTemperature(NSString *key) {
|
|
52
|
+
io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault,
|
|
53
|
+
IOServiceMatching("AppleSMC"));
|
|
54
|
+
if (!service) return nil;
|
|
55
|
+
io_connect_t connection = IO_OBJECT_NULL;
|
|
56
|
+
kern_return_t result = IOServiceOpen(service, mach_task_self(), 0, &connection);
|
|
57
|
+
IOObjectRelease(service);
|
|
58
|
+
if (result != KERN_SUCCESS || !connection) return nil;
|
|
59
|
+
|
|
60
|
+
CCPetsSMCParamStruct input = {0};
|
|
61
|
+
CCPetsSMCParamStruct output = {0};
|
|
62
|
+
size_t outputSize = sizeof(output);
|
|
63
|
+
input.key = CCPetsFourCC(key);
|
|
64
|
+
input.data8 = 9; // kSMCGetKeyInfo
|
|
65
|
+
result = IOConnectCallStructMethod(connection, 2, &input, sizeof(input),
|
|
66
|
+
&output, &outputSize);
|
|
67
|
+
if (result != KERN_SUCCESS || output.keyInfo.dataSize == 0 ||
|
|
68
|
+
output.keyInfo.dataSize > sizeof(output.bytes)) {
|
|
69
|
+
IOServiceClose(connection);
|
|
70
|
+
return nil;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
CCPetsSMCKeyInfoData keyInfo = output.keyInfo;
|
|
74
|
+
memset(&input, 0, sizeof(input));
|
|
75
|
+
memset(&output, 0, sizeof(output));
|
|
76
|
+
outputSize = sizeof(output);
|
|
77
|
+
input.key = CCPetsFourCC(key);
|
|
78
|
+
input.keyInfo.dataSize = keyInfo.dataSize;
|
|
79
|
+
input.data8 = 5; // kSMCReadKey
|
|
80
|
+
result = IOConnectCallStructMethod(connection, 2, &input, sizeof(input),
|
|
81
|
+
&output, &outputSize);
|
|
82
|
+
IOServiceClose(connection);
|
|
83
|
+
if (result != KERN_SUCCESS) return nil;
|
|
84
|
+
|
|
85
|
+
double temperature = NAN;
|
|
86
|
+
uint32_t type = keyInfo.dataType;
|
|
87
|
+
if (type == CCPetsFourCC(@"sp78") && keyInfo.dataSize >= 2) {
|
|
88
|
+
temperature = (double)(int16_t)((output.bytes[0] << 8) | output.bytes[1]) / 256.0;
|
|
89
|
+
} else if (type == CCPetsFourCC(@"flt ") && keyInfo.dataSize >= sizeof(float)) {
|
|
90
|
+
uint32_t bits = ((uint32_t)output.bytes[0] << 24) |
|
|
91
|
+
((uint32_t)output.bytes[1] << 16) |
|
|
92
|
+
((uint32_t)output.bytes[2] << 8) | output.bytes[3];
|
|
93
|
+
float value = 0;
|
|
94
|
+
memcpy(&value, &bits, sizeof(value));
|
|
95
|
+
temperature = value;
|
|
96
|
+
}
|
|
97
|
+
return isfinite(temperature) && temperature >= 0 && temperature <= 125
|
|
98
|
+
? @(temperature) : nil;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Apple Silicon 新机型(包括 M5)不再暴露 AppleSMC,但会把芯片温度作为
|
|
102
|
+
// IOHIDEventService 的 Temperature 事件提供。相关函数没有公开头文件,运行时解析
|
|
103
|
+
// 可以在旧系统上安全降级,不会形成私有符号的静态链接依赖。
|
|
104
|
+
static NSNumber *CCPetsReadHIDDieTemperature(void) {
|
|
105
|
+
typedef CFTypeRef (*CreateClientFunction)(CFAllocatorRef);
|
|
106
|
+
typedef CFArrayRef (*CopyServicesFunction)(CFTypeRef);
|
|
107
|
+
typedef CFTypeRef (*CopyPropertyFunction)(CFTypeRef, CFStringRef);
|
|
108
|
+
typedef CFTypeRef (*CopyEventFunction)(CFTypeRef, int64_t, int32_t, int64_t);
|
|
109
|
+
typedef double (*GetFloatValueFunction)(CFTypeRef, int32_t);
|
|
110
|
+
|
|
111
|
+
void *framework = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit",
|
|
112
|
+
RTLD_LAZY | RTLD_LOCAL);
|
|
113
|
+
if (!framework) return nil;
|
|
114
|
+
CreateClientFunction createClient =
|
|
115
|
+
(CreateClientFunction)dlsym(framework, "IOHIDEventSystemClientCreate");
|
|
116
|
+
CopyServicesFunction copyServices =
|
|
117
|
+
(CopyServicesFunction)dlsym(framework, "IOHIDEventSystemClientCopyServices");
|
|
118
|
+
CopyPropertyFunction copyProperty =
|
|
119
|
+
(CopyPropertyFunction)dlsym(framework, "IOHIDServiceClientCopyProperty");
|
|
120
|
+
CopyEventFunction copyEvent =
|
|
121
|
+
(CopyEventFunction)dlsym(framework, "IOHIDServiceClientCopyEvent");
|
|
122
|
+
GetFloatValueFunction getFloatValue =
|
|
123
|
+
(GetFloatValueFunction)dlsym(framework, "IOHIDEventGetFloatValue");
|
|
124
|
+
if (!createClient || !copyServices || !copyProperty || !copyEvent || !getFloatValue) {
|
|
125
|
+
dlclose(framework);
|
|
126
|
+
return nil;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
CFTypeRef client = createClient(kCFAllocatorDefault);
|
|
130
|
+
CFArrayRef services = client ? copyServices(client) : nil;
|
|
131
|
+
double hottest = NAN;
|
|
132
|
+
for (CFIndex index = 0; services && index < CFArrayGetCount(services); index++) {
|
|
133
|
+
CFTypeRef service = CFArrayGetValueAtIndex(services, index);
|
|
134
|
+
CFTypeRef productValue = copyProperty(service, CFSTR("Product"));
|
|
135
|
+
NSString *product = productValue && CFGetTypeID(productValue) == CFStringGetTypeID()
|
|
136
|
+
? (__bridge NSString *)productValue : nil;
|
|
137
|
+
BOOL isDieSensor = [product.lowercaseString containsString:@"tdie"];
|
|
138
|
+
if (productValue) CFRelease(productValue);
|
|
139
|
+
if (!isDieSensor) continue;
|
|
140
|
+
|
|
141
|
+
// kIOHIDEventTypeTemperature = 15;Level 字段位于该类型的 field base。
|
|
142
|
+
CFTypeRef event = copyEvent(service, 15, 0, 0);
|
|
143
|
+
if (!event) continue;
|
|
144
|
+
double value = getFloatValue(event, 15 << 16);
|
|
145
|
+
CFRelease(event);
|
|
146
|
+
if (isfinite(value) && value >= 0 && value <= 125 &&
|
|
147
|
+
(!isfinite(hottest) || value > hottest)) hottest = value;
|
|
148
|
+
}
|
|
149
|
+
if (services) CFRelease(services);
|
|
150
|
+
if (client) CFRelease(client);
|
|
151
|
+
dlclose(framework);
|
|
152
|
+
return isfinite(hottest) ? @(hottest) : nil;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@interface CCPetsSystemMonitor ()
|
|
156
|
+
@property dispatch_queue_t queue;
|
|
157
|
+
@property uint64_t previousUserTicks;
|
|
158
|
+
@property uint64_t previousSystemTicks;
|
|
159
|
+
@property uint64_t previousIdleTicks;
|
|
160
|
+
@property uint64_t previousNiceTicks;
|
|
161
|
+
@property NSNumber *cachedTemperature;
|
|
162
|
+
@property NSTimeInterval temperatureSampledAt;
|
|
163
|
+
@end
|
|
164
|
+
|
|
165
|
+
@implementation CCPetsSystemMonitor
|
|
166
|
+
|
|
167
|
+
- (instancetype)init {
|
|
168
|
+
if ((self = [super init])) {
|
|
169
|
+
_queue = dispatch_queue_create("com.universewang.cc-pets.system-monitor",
|
|
170
|
+
DISPATCH_QUEUE_SERIAL);
|
|
171
|
+
}
|
|
172
|
+
return self;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
- (NSNumber *)cpuPercent {
|
|
176
|
+
processor_info_array_t info = NULL;
|
|
177
|
+
mach_msg_type_number_t count = 0;
|
|
178
|
+
natural_t processors = 0;
|
|
179
|
+
kern_return_t result = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO,
|
|
180
|
+
&processors, &info, &count);
|
|
181
|
+
if (result != KERN_SUCCESS || !info) return nil;
|
|
182
|
+
uint64_t user = 0, system = 0, idle = 0, nice = 0;
|
|
183
|
+
for (natural_t index = 0; index < processors; index++) {
|
|
184
|
+
processor_cpu_load_info_t load = (processor_cpu_load_info_t)info + index;
|
|
185
|
+
user += load->cpu_ticks[CPU_STATE_USER];
|
|
186
|
+
system += load->cpu_ticks[CPU_STATE_SYSTEM];
|
|
187
|
+
idle += load->cpu_ticks[CPU_STATE_IDLE];
|
|
188
|
+
nice += load->cpu_ticks[CPU_STATE_NICE];
|
|
189
|
+
}
|
|
190
|
+
vm_deallocate(mach_task_self(), (vm_address_t)info, count * sizeof(integer_t));
|
|
191
|
+
|
|
192
|
+
uint64_t previousTotal = self.previousUserTicks + self.previousSystemTicks +
|
|
193
|
+
self.previousIdleTicks + self.previousNiceTicks;
|
|
194
|
+
uint64_t total = user + system + idle + nice;
|
|
195
|
+
uint64_t totalDelta = total >= previousTotal ? total - previousTotal : 0;
|
|
196
|
+
uint64_t idleDelta = idle >= self.previousIdleTicks ? idle - self.previousIdleTicks : 0;
|
|
197
|
+
self.previousUserTicks = user;
|
|
198
|
+
self.previousSystemTicks = system;
|
|
199
|
+
self.previousIdleTicks = idle;
|
|
200
|
+
self.previousNiceTicks = nice;
|
|
201
|
+
if (totalDelta == 0) return nil;
|
|
202
|
+
return @(fmax(0, fmin(100, 100.0 * (double)(totalDelta - idleDelta) /
|
|
203
|
+
(double)totalDelta)));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
- (NSNumber *)memoryPercent {
|
|
207
|
+
mach_port_t host = mach_host_self();
|
|
208
|
+
vm_size_t pageSize = 0;
|
|
209
|
+
if (host_page_size(host, &pageSize) != KERN_SUCCESS) return nil;
|
|
210
|
+
vm_statistics64_data_t stats = {0};
|
|
211
|
+
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
|
|
212
|
+
if (host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&stats, &count) !=
|
|
213
|
+
KERN_SUCCESS) return nil;
|
|
214
|
+
uint64_t total = NSProcessInfo.processInfo.physicalMemory;
|
|
215
|
+
if (total == 0) return nil;
|
|
216
|
+
// 对齐活动监视器的“已使用内存”:物理内存减去空闲内存和可回收的文件缓存。
|
|
217
|
+
// file-backed 对应活动监视器里的“已缓存文件”,speculative 也属于可立即回收页。
|
|
218
|
+
uint64_t reclaimablePages = stats.free_count + stats.speculative_count +
|
|
219
|
+
stats.external_page_count;
|
|
220
|
+
uint64_t reclaimable = reclaimablePages * (uint64_t)pageSize;
|
|
221
|
+
uint64_t used = total > reclaimable ? total - reclaimable : 0;
|
|
222
|
+
return @(fmax(0, fmin(100, 100.0 * (double)used / (double)total)));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
- (NSNumber *)temperature {
|
|
226
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
227
|
+
NSTimeInterval interval = self.cachedTemperature ? 5 : 30;
|
|
228
|
+
if (self.temperatureSampledAt > 0 && now - self.temperatureSampledAt < interval) {
|
|
229
|
+
return self.cachedTemperature;
|
|
230
|
+
}
|
|
231
|
+
self.temperatureSampledAt = now;
|
|
232
|
+
NSNumber *temperature = CCPetsReadHIDDieTemperature();
|
|
233
|
+
if (!temperature) {
|
|
234
|
+
// Intel 和部分早期 Apple Silicon 仍通过 AppleSMC 暴露这些 key。
|
|
235
|
+
for (NSString *key in @[@"TC0P", @"TC0D", @"Tp09", @"Tp0T", @"Tp01", @"Tp05"]) {
|
|
236
|
+
NSNumber *value = CCPetsReadSMCTemperature(key);
|
|
237
|
+
if (value) {
|
|
238
|
+
temperature = value;
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
self.cachedTemperature = temperature;
|
|
244
|
+
return self.cachedTemperature;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
- (void)sampleCPU:(BOOL)cpuEnabled memory:(BOOL)memoryEnabled
|
|
248
|
+
temperature:(BOOL)temperatureEnabled
|
|
249
|
+
completion:(void (^)(NSNumber *, NSNumber *, NSNumber *))completion {
|
|
250
|
+
dispatch_async(self.queue, ^{
|
|
251
|
+
NSNumber *cpu = cpuEnabled ? [self cpuPercent] : nil;
|
|
252
|
+
NSNumber *memory = memoryEnabled ? [self memoryPercent] : nil;
|
|
253
|
+
NSNumber *temperature = temperatureEnabled ? [self temperature] : nil;
|
|
254
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
255
|
+
if (completion) completion(cpu, memory, temperature);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
@end
|
package/Sources/CCPets/PetView.m
CHANGED
|
@@ -534,6 +534,21 @@ static const CGFloat PetDragThreshold = 3.0;
|
|
|
534
534
|
tag:[option[@"tag"] integerValue]];
|
|
535
535
|
}
|
|
536
536
|
notificationItem.submenu = notificationMenu;
|
|
537
|
+
NSMenuItem *systemStatusItem = [menu addItemWithTitle:@"系统状态" action:nil keyEquivalent:@""];
|
|
538
|
+
NSMenu *systemStatusMenu = [NSMenu new];
|
|
539
|
+
NSArray<NSDictionary *> *systemStatusOptions = @[
|
|
540
|
+
@{@"title": @"CPU 占用", @"key": SystemCPUEnabledKey, @"tag": @1},
|
|
541
|
+
@{@"title": @"CPU 温度", @"key": SystemTemperatureEnabledKey, @"tag": @2},
|
|
542
|
+
@{@"title": @"内存占用", @"key": SystemMemoryEnabledKey, @"tag": @3}
|
|
543
|
+
];
|
|
544
|
+
for (NSDictionary *option in systemStatusOptions) {
|
|
545
|
+
[self addPersistentSwitchToMenu:systemStatusMenu
|
|
546
|
+
title:option[@"title"]
|
|
547
|
+
checked:[NSUserDefaults.standardUserDefaults boolForKey:option[@"key"]]
|
|
548
|
+
action:@selector(toggleSystemMetric:)
|
|
549
|
+
tag:[option[@"tag"] integerValue]];
|
|
550
|
+
}
|
|
551
|
+
systemStatusItem.submenu = systemStatusMenu;
|
|
537
552
|
NSMenuItem *updateItem = [menu addItemWithTitle:@"检查更新…" action:@selector(checkForUpdates:) keyEquivalent:@""];
|
|
538
553
|
updateItem.target = NSApp.delegate;
|
|
539
554
|
[menu addItem:NSMenuItem.separatorItem];
|
|
@@ -10,8 +10,8 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName);
|
|
|
10
10
|
@interface QuotaDashboardView : NSView
|
|
11
11
|
@property NSDictionary *codexUsage;
|
|
12
12
|
@property NSDictionary *claudeUsage;
|
|
13
|
-
@property NSArray<
|
|
14
|
-
@property NSArray<
|
|
13
|
+
@property NSArray<NSDictionary *> *codexHistory;
|
|
14
|
+
@property NSArray<NSDictionary *> *claudeHistory;
|
|
15
15
|
@property BOOL historyEnabled;
|
|
16
16
|
@property NSInteger activeAgentCount;
|
|
17
17
|
// 仍在运行的客户端所属的 provider("Codex" / "Claude")。卡片的在线徽章据此判定,
|
|
@@ -20,6 +20,12 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName);
|
|
|
20
20
|
// 老版本包装脚本写出的 pid 文件没有 provider 名。有这种客户端时无法断言某一家离线。
|
|
21
21
|
@property BOOL hasUnlabeledClient;
|
|
22
22
|
@property NSTimeInterval lastUpdatedAt;
|
|
23
|
+
@property BOOL systemCPUEnabled;
|
|
24
|
+
@property BOOL systemTemperatureEnabled;
|
|
25
|
+
@property BOOL systemMemoryEnabled;
|
|
26
|
+
@property NSNumber *systemCPUPercent;
|
|
27
|
+
@property NSNumber *systemTemperatureCelsius;
|
|
28
|
+
@property NSNumber *systemMemoryPercent;
|
|
23
29
|
- (BOOL)isProviderOnline:(NSString *)name;
|
|
24
30
|
@property NSImage *codexLogo;
|
|
25
31
|
@property NSImage *claudeLogo;
|
|
@@ -183,6 +183,72 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
183
183
|
- (NSColor *)agentStatusColor {
|
|
184
184
|
return self.activeAgentCount > 0 ? NSColor.systemGreenColor : self.secondaryColor;
|
|
185
185
|
}
|
|
186
|
+
- (void)drawSystemMetricsEndingAtX:(CGFloat)rightEdge y:(CGFloat)y {
|
|
187
|
+
NSMutableArray<NSDictionary *> *metrics = [NSMutableArray array];
|
|
188
|
+
if (self.systemCPUEnabled) {
|
|
189
|
+
NSString *value = self.systemCPUPercent
|
|
190
|
+
? [NSString stringWithFormat:@"%.0f%%", self.systemCPUPercent.doubleValue] : @"--";
|
|
191
|
+
[metrics addObject:@{@"symbol": @"cpu", @"label": [NSString stringWithFormat:@"CPU %@", value],
|
|
192
|
+
@"color": [NSColor colorWithRed:245.0 / 255.0 green:166.0 / 255.0
|
|
193
|
+
blue:35.0 / 255.0 alpha:1]}];
|
|
194
|
+
}
|
|
195
|
+
if (self.systemTemperatureEnabled) {
|
|
196
|
+
NSString *value = self.systemTemperatureCelsius
|
|
197
|
+
? [NSString stringWithFormat:@"%.0f°C", self.systemTemperatureCelsius.doubleValue] : @"--°C";
|
|
198
|
+
double temperature = self.systemTemperatureCelsius.doubleValue;
|
|
199
|
+
NSColor *color = temperature >= 90 ? NSColor.systemRedColor :
|
|
200
|
+
(temperature >= 80 ? [NSColor colorWithRed:1.00 green:0.35 blue:0.16 alpha:1] :
|
|
201
|
+
[NSColor colorWithRed:1.00 green:107.0 / 255.0 blue:74.0 / 255.0 alpha:1]);
|
|
202
|
+
[metrics addObject:@{@"symbol": @"thermometer.medium", @"label": value,
|
|
203
|
+
@"color": color}];
|
|
204
|
+
}
|
|
205
|
+
if (self.systemMemoryEnabled) {
|
|
206
|
+
NSString *value = self.systemMemoryPercent
|
|
207
|
+
? [NSString stringWithFormat:@"%.0f%%", self.systemMemoryPercent.doubleValue] : @"--";
|
|
208
|
+
[metrics addObject:@{@"symbol": @"memorychip", @"label": [NSString stringWithFormat:@"MEM %@", value],
|
|
209
|
+
@"color": [NSColor colorWithRed:32.0 / 255.0 green:214.0 / 255.0
|
|
210
|
+
blue:165.0 / 255.0 alpha:1]}];
|
|
211
|
+
}
|
|
212
|
+
NSFont *font = [NSFont systemFontOfSize:13 weight:NSFontWeightSemibold];
|
|
213
|
+
NSMutableAttributedString *statusLine = [NSMutableAttributedString new];
|
|
214
|
+
for (NSUInteger index = 0; index < metrics.count; index++) {
|
|
215
|
+
NSDictionary *metric = metrics[index];
|
|
216
|
+
NSString *label = metric[@"label"];
|
|
217
|
+
NSColor *color = metric[@"color"];
|
|
218
|
+
NSDictionary *attributes = @{NSFontAttributeName: font,
|
|
219
|
+
NSForegroundColorAttributeName: color};
|
|
220
|
+
NSImage *icon = [NSImage imageWithSystemSymbolName:metric[@"symbol"]
|
|
221
|
+
accessibilityDescription:label];
|
|
222
|
+
NSImageSymbolConfiguration *sizeConfiguration =
|
|
223
|
+
[NSImageSymbolConfiguration configurationWithPointSize:
|
|
224
|
+
[metric[@"symbol"] isEqualToString:@"thermometer.medium"] ? 16 : 14
|
|
225
|
+
weight:NSFontWeightSemibold];
|
|
226
|
+
NSImageSymbolConfiguration *colorConfiguration =
|
|
227
|
+
[NSImageSymbolConfiguration configurationWithHierarchicalColor:metric[@"color"]];
|
|
228
|
+
icon = [icon imageWithSymbolConfiguration:
|
|
229
|
+
[sizeConfiguration configurationByApplyingConfiguration:colorConfiguration]];
|
|
230
|
+
NSTextAttachment *attachment = [NSTextAttachment new];
|
|
231
|
+
attachment.image = icon;
|
|
232
|
+
CGFloat iconSize = [metric[@"symbol"] isEqualToString:@"thermometer.medium"] ? 18 : 16;
|
|
233
|
+
attachment.bounds = NSMakeRect(0, (font.capHeight - iconSize) / 2.0,
|
|
234
|
+
iconSize, iconSize);
|
|
235
|
+
[statusLine appendAttributedString:[NSAttributedString
|
|
236
|
+
attributedStringWithAttachment:attachment]];
|
|
237
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
238
|
+
initWithString:@" " attributes:attributes]];
|
|
239
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
240
|
+
initWithString:label attributes:attributes]];
|
|
241
|
+
if (index + 1 < metrics.count) {
|
|
242
|
+
NSDictionary *separatorAttributes = @{NSFontAttributeName:
|
|
243
|
+
[NSFont systemFontOfSize:12 weight:NSFontWeightRegular],
|
|
244
|
+
NSForegroundColorAttributeName: [NSColor colorWithWhite:0.42 alpha:0.7]};
|
|
245
|
+
[statusLine appendAttributedString:[[NSAttributedString alloc]
|
|
246
|
+
initWithString:@" │ " attributes:separatorAttributes]];
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
CGFloat width = ceil(statusLine.size.width);
|
|
250
|
+
[statusLine drawInRect:NSMakeRect(rightEdge - width, y, width + 1, 20)];
|
|
251
|
+
}
|
|
186
252
|
- (NSString *)formattedTokenCountForUsage:(NSDictionary *)usage key:(NSString *)key {
|
|
187
253
|
return [self formattedTokenCount:[self tokenTotals:usage key:key] ?: @{}];
|
|
188
254
|
}
|
|
@@ -226,21 +292,132 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
226
292
|
[color setFill];
|
|
227
293
|
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius] fill];
|
|
228
294
|
}
|
|
229
|
-
- (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
295
|
+
- (NSArray<NSDictionary *> *)curvePointsFromHistory:(NSArray<NSDictionary *> *)history
|
|
296
|
+
currentUsed:(NSNumber *)currentUsed {
|
|
297
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
298
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
299
|
+
NSMutableArray<NSDictionary *> *points = [NSMutableArray array];
|
|
300
|
+
if (self.historyEnabled) {
|
|
301
|
+
for (NSDictionary *sample in history) {
|
|
302
|
+
if (![sample isKindOfClass:NSDictionary.class]) continue;
|
|
303
|
+
NSNumber *timestamp = [sample[@"timestamp"] isKindOfClass:NSNumber.class]
|
|
304
|
+
? sample[@"timestamp"] : nil;
|
|
305
|
+
NSNumber *remaining = [sample[@"remaining"] isKindOfClass:NSNumber.class]
|
|
306
|
+
? sample[@"remaining"] : nil;
|
|
307
|
+
if (!timestamp || !remaining || timestamp.doubleValue < cutoff) continue;
|
|
308
|
+
double used = 100.0 - fmax(0, fmin(100, remaining.doubleValue));
|
|
309
|
+
[points addObject:@{@"timestamp": timestamp, @"used": @(used)}];
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// 终点必须和卡片上的当前官方额度同源;历史落盘最多会慢 15 分钟,不能直接拿它
|
|
313
|
+
// 冒充“当前”,否则同一张卡里会出现两个不同百分比。
|
|
314
|
+
if (currentUsed) {
|
|
315
|
+
[points addObject:@{@"timestamp": @(now), @"used": currentUsed}];
|
|
316
|
+
}
|
|
317
|
+
return points;
|
|
318
|
+
}
|
|
319
|
+
- (void)appendCurveFromPoint:(NSPoint)from toPoint:(NSPoint)to path:(NSBezierPath *)path {
|
|
320
|
+
CGFloat controlOffset = (to.x - from.x) / 3.0;
|
|
321
|
+
[path curveToPoint:to
|
|
322
|
+
controlPoint1:NSMakePoint(from.x + controlOffset, from.y)
|
|
323
|
+
controlPoint2:NSMakePoint(to.x - controlOffset, to.y)];
|
|
324
|
+
}
|
|
325
|
+
- (void)drawCurveSegment:(NSArray<NSValue *> *)segment inRect:(NSRect)rect color:(NSColor *)color {
|
|
326
|
+
if (segment.count == 0) return;
|
|
327
|
+
NSBezierPath *line = [NSBezierPath bezierPath];
|
|
328
|
+
NSPoint first = segment.firstObject.pointValue;
|
|
329
|
+
[line moveToPoint:first];
|
|
330
|
+
NSPoint previous = first;
|
|
331
|
+
for (NSUInteger index = 1; index < segment.count; index++) {
|
|
332
|
+
NSPoint point = segment[index].pointValue;
|
|
333
|
+
[self appendCurveFromPoint:previous toPoint:point path:line];
|
|
334
|
+
previous = point;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
NSBezierPath *fill = [line copy];
|
|
338
|
+
[fill lineToPoint:NSMakePoint(previous.x, NSMinY(rect))];
|
|
339
|
+
[fill lineToPoint:NSMakePoint(first.x, NSMinY(rect))];
|
|
340
|
+
[fill closePath];
|
|
341
|
+
[NSGraphicsContext saveGraphicsState];
|
|
342
|
+
[fill addClip];
|
|
343
|
+
NSGradient *area = [[NSGradient alloc]
|
|
344
|
+
initWithStartingColor:[color colorWithAlphaComponent:0.34]
|
|
345
|
+
endingColor:[color colorWithAlphaComponent:0.025]];
|
|
346
|
+
[area drawFromPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))
|
|
347
|
+
toPoint:NSMakePoint(NSMidX(rect), NSMinY(rect)) options:0];
|
|
348
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
349
|
+
|
|
350
|
+
[[color colorWithAlphaComponent:0.95] setStroke];
|
|
351
|
+
line.lineWidth = 2;
|
|
352
|
+
[line stroke];
|
|
353
|
+
}
|
|
354
|
+
- (void)drawUsageCurve:(NSArray<NSDictionary *> *)points inRect:(NSRect)rect
|
|
355
|
+
color:(NSColor *)color {
|
|
356
|
+
[[NSColor colorWithWhite:1 alpha:0.08] setStroke];
|
|
357
|
+
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(rect), NSMinY(rect))
|
|
358
|
+
toPoint:NSMakePoint(NSMaxX(rect), NSMinY(rect))];
|
|
359
|
+
if (points.count == 0) return;
|
|
360
|
+
|
|
361
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
362
|
+
NSTimeInterval cutoff = now - 7 * 24 * 60 * 60;
|
|
363
|
+
// 曲线始终连成一段:采样缺口只代表桌宠当时没在运行,不代表没有消耗,
|
|
364
|
+
// 缺口两端直接插值比断开更接近真实轨迹;窗口重置带来的回落本身也是真实变化。
|
|
365
|
+
NSMutableArray<NSValue *> *allPoints = [NSMutableArray arrayWithCapacity:points.count];
|
|
366
|
+
for (NSDictionary *sample in points) {
|
|
367
|
+
NSTimeInterval timestamp = [sample[@"timestamp"] doubleValue];
|
|
368
|
+
double used = fmax(0, fmin(100, [sample[@"used"] doubleValue]));
|
|
369
|
+
CGFloat x = NSMinX(rect) + NSWidth(rect) * (timestamp - cutoff) / (now - cutoff);
|
|
370
|
+
CGFloat y = NSMinY(rect) + NSHeight(rect) * used / 100.0;
|
|
371
|
+
[allPoints addObject:[NSValue valueWithPoint:NSMakePoint(
|
|
372
|
+
fmax(NSMinX(rect), fmin(NSMaxX(rect), x)), y)]];
|
|
373
|
+
}
|
|
374
|
+
[self drawCurveSegment:allPoints inRect:rect color:color];
|
|
375
|
+
}
|
|
376
|
+
- (NSDictionary *)paceStatusForQuota:(NSDictionary *)quota currentUsed:(NSNumber *)currentUsed
|
|
377
|
+
color:(NSColor *)providerColor {
|
|
378
|
+
if (!currentUsed) {
|
|
379
|
+
return @{@"label": @"数据不足", @"tip": @"等待官方额度数据",
|
|
380
|
+
@"color": self.secondaryColor};
|
|
243
381
|
}
|
|
382
|
+
NSNumber *reset = [quota[@"resets_at"] isKindOfClass:NSNumber.class]
|
|
383
|
+
? quota[@"resets_at"] : nil;
|
|
384
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
385
|
+
const NSTimeInterval sevenDaySeconds = 7 * 24 * 60 * 60;
|
|
386
|
+
NSTimeInterval start = reset ? reset.doubleValue - sevenDaySeconds : 0;
|
|
387
|
+
NSTimeInterval elapsed = now - start;
|
|
388
|
+
double projectedRemaining = NAN;
|
|
389
|
+
const NSTimeInterval paceObservationSeconds = 24 * 60 * 60;
|
|
390
|
+
if (reset && reset.doubleValue > now && elapsed >= paceObservationSeconds) {
|
|
391
|
+
double projectedUsed = currentUsed.doubleValue * sevenDaySeconds / elapsed;
|
|
392
|
+
projectedRemaining = 100.0 - projectedUsed;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
double remaining = 100.0 - currentUsed.doubleValue;
|
|
396
|
+
NSString *label = remaining >= 70 ? @"充足"
|
|
397
|
+
: (remaining >= 40 ? @"平稳" : (remaining >= 15 ? @"需关注" : @"高风险"));
|
|
398
|
+
NSString *tip = remaining >= 70 ? @"当前额度余量充足"
|
|
399
|
+
: (remaining >= 40 ? @"当前额度余量平稳"
|
|
400
|
+
: (remaining >= 15 ? @"建议关注额度余量" : @"当前额度余量较低"));
|
|
401
|
+
NSColor *statusColor = remaining >= 70 ? NSColor.systemGreenColor
|
|
402
|
+
: (remaining >= 40 ? providerColor
|
|
403
|
+
: (remaining >= 15 ? NSColor.systemOrangeColor : NSColor.systemRedColor));
|
|
404
|
+
// 当前窗口开始满 24 小时后再按消耗节奏预测。此前只看真实剩余额度,避免把
|
|
405
|
+
// 开窗头几个小时的突发任务线性外推到整整 7 天,造成“高风险”误报。
|
|
406
|
+
if (!isnan(projectedRemaining) &&
|
|
407
|
+
(remaining <= 5 || projectedRemaining < 0)) {
|
|
408
|
+
label = @"高风险";
|
|
409
|
+
tip = @"预计重置前额度用尽";
|
|
410
|
+
statusColor = NSColor.systemRedColor;
|
|
411
|
+
} else if (!isnan(projectedRemaining) && projectedRemaining < 10) {
|
|
412
|
+
label = @"需关注";
|
|
413
|
+
tip = @"建议减少高消耗任务";
|
|
414
|
+
statusColor = NSColor.systemOrangeColor;
|
|
415
|
+
} else if (!isnan(projectedRemaining) && projectedRemaining >= 30) {
|
|
416
|
+
label = @"充足";
|
|
417
|
+
tip = @"按当前节奏额度充足";
|
|
418
|
+
statusColor = NSColor.systemGreenColor;
|
|
419
|
+
}
|
|
420
|
+
return @{@"label": label, @"tip": tip, @"color": statusColor};
|
|
244
421
|
}
|
|
245
422
|
- (void)drawProvider:(NSString *)name usage:(NSDictionary *)usage usedKey:(NSString *)usedKey
|
|
246
423
|
color:(NSColor *)color cardRect:(NSRect)card {
|
|
@@ -308,10 +485,10 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
308
485
|
NSArray<NSDictionary *> *windows = @[
|
|
309
486
|
@{@"x": @(NSMinX(card) + 214), @"title": @"官方 5 小时窗口", @"quota": five ?: @{},
|
|
310
487
|
@"tokens": fiveTokens ?: @{}, @"used": fiveUsed ?: NSNull.null,
|
|
311
|
-
@"rolling": @"
|
|
488
|
+
@"rolling": @"无限制"},
|
|
312
489
|
@{@"x": @(NSMinX(card) + 394), @"title": @"官方 7 天窗口", @"quota": week ?: @{},
|
|
313
490
|
@"tokens": weekTokens ?: @{}, @"used": weekUsed ?: NSNull.null,
|
|
314
|
-
@"rolling": @"
|
|
491
|
+
@"rolling": @"无限制"}
|
|
315
492
|
];
|
|
316
493
|
for (NSDictionary *window in windows) {
|
|
317
494
|
CGFloat x = [window[@"x"] doubleValue];
|
|
@@ -339,24 +516,42 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
339
516
|
// 说清楚这一行的口径,比留一个 `--` 让人以为是渲染坏了要好。
|
|
340
517
|
NSString *resetValue = [self resetText:window[@"quota"]];
|
|
341
518
|
NSString *reset = [resetValue isEqualToString:@"--"]
|
|
342
|
-
? window[@"rolling"] : [NSString stringWithFormat:@"
|
|
519
|
+
? window[@"rolling"] : [NSString stringWithFormat:@"重置时间 %@", resetValue];
|
|
343
520
|
[reset drawInRect:NSMakeRect(x, NSMinY(card) + 22, 150, 18)
|
|
344
521
|
withAttributes:[self textAttributesWithSize:11 color:secondary weight:NSFontWeightRegular]];
|
|
345
522
|
}
|
|
346
523
|
|
|
347
524
|
CGFloat trendX = NSMinX(card) + 564;
|
|
348
|
-
[@"
|
|
525
|
+
[@"使用趋势" drawInRect:NSMakeRect(trendX, NSMinY(card) + 114, 145, 20)
|
|
349
526
|
withAttributes:[self textAttributesWithSize:13 color:secondary weight:NSFontWeightRegular]];
|
|
350
|
-
NSArray<
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
[
|
|
359
|
-
|
|
527
|
+
NSArray<NSDictionary *> *history = [name isEqualToString:@"Codex"]
|
|
528
|
+
? self.codexHistory : self.claudeHistory;
|
|
529
|
+
NSNumber *currentUsed = exhausted ? @100 : weekUsed;
|
|
530
|
+
NSArray<NSDictionary *> *points = [self curvePointsFromHistory:history currentUsed:currentUsed];
|
|
531
|
+
NSDictionary *pace = [self paceStatusForQuota:week currentUsed:currentUsed color:color];
|
|
532
|
+
NSColor *paceColor = pace[@"color"];
|
|
533
|
+
NSDictionary *pillAttributes = [self textAttributesWithSize:11 color:paceColor
|
|
534
|
+
weight:NSFontWeightSemibold];
|
|
535
|
+
CGFloat pillWidth = ceil([pace[@"label"] sizeWithAttributes:pillAttributes].width) + 18;
|
|
536
|
+
NSRect pill = NSMakeRect(trendX, NSMinY(card) + 88, pillWidth, 22);
|
|
537
|
+
[self fillRoundedRect:pill radius:11 color:[paceColor colorWithAlphaComponent:0.14]];
|
|
538
|
+
[pace[@"label"] drawInRect:NSInsetRect(pill, 9, 3) withAttributes:pillAttributes];
|
|
539
|
+
|
|
540
|
+
NSRect curveRect = NSMakeRect(trendX, NSMinY(card) + 43, 135, 38);
|
|
541
|
+
[self drawUsageCurve:points inRect:curveRect color:color];
|
|
542
|
+
double peak = 0;
|
|
543
|
+
for (NSDictionary *point in points) peak = fmax(peak, [point[@"used"] doubleValue]);
|
|
544
|
+
NSString *peakText = points.count > 0
|
|
545
|
+
? [NSString stringWithFormat:@"最高 %.0f%%", peak] : @"最高 --";
|
|
546
|
+
NSString *currentText = currentUsed
|
|
547
|
+
? [NSString stringWithFormat:@"当前 %.0f%%", currentUsed.doubleValue] : @"当前 --";
|
|
548
|
+
[peakText drawInRect:NSMakeRect(trendX, NSMinY(card) + 25, 72, 16)
|
|
549
|
+
withAttributes:[self textAttributesWithSize:10 color:secondary weight:NSFontWeightRegular]];
|
|
550
|
+
[currentText drawInRect:NSMakeRect(trendX + 66, NSMinY(card) + 25, 69, 16)
|
|
551
|
+
withAttributes:[self rightAlignedTextAttributesWithSize:10 color:secondary
|
|
552
|
+
weight:NSFontWeightRegular]];
|
|
553
|
+
[pace[@"tip"] drawInRect:NSMakeRect(trendX, NSMinY(card) + 7, 145, 16)
|
|
554
|
+
withAttributes:[self textAttributesWithSize:10 color:paceColor weight:NSFontWeightMedium]];
|
|
360
555
|
}
|
|
361
556
|
- (void)drawRect:(NSRect)dirtyRect {
|
|
362
557
|
[NSGraphicsContext saveGraphicsState];
|
|
@@ -408,6 +603,7 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
408
603
|
: @"暂无 Agent 在线";
|
|
409
604
|
[agentText drawInRect:NSMakeRect(190, QuotaLogicalHeight - 95, 160, 22)
|
|
410
605
|
withAttributes:[self textAttributesWithSize:14 color:agentColor weight:NSFontWeightMedium]];
|
|
606
|
+
[self drawSystemMetricsEndingAtX:QuotaLogicalWidth - 28 y:QuotaLogicalHeight - 95];
|
|
411
607
|
|
|
412
608
|
BOOL recentlyRefreshed = self.refreshedUntil > NSDate.date.timeIntervalSince1970;
|
|
413
609
|
CGFloat buttonAlpha = self.refreshPressed ? 0.18 : (self.refreshHovered ? 0.12 : 0.06);
|
|
@@ -435,8 +631,10 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
435
631
|
// 每格内是一张 2×2 表:标题独占一行,每个 Provider 独占一行;输入/输出标签放在
|
|
436
632
|
// 对应数值上方。这样既能完整展示 7 天统计起期,也不会挤乱两家的数据行。
|
|
437
633
|
NSArray<NSDictionary *> *cells = @[
|
|
438
|
-
@{@"icon": @"◔", @"
|
|
439
|
-
|
|
634
|
+
@{@"icon": @"◔", @"headerSymbol": @"calendar", @"key": @"today",
|
|
635
|
+
@"label": @"今日用量"},
|
|
636
|
+
@{@"icon": @"◉", @"headerSymbol": @"calendar.badge.clock", @"key": @"recentWeek",
|
|
637
|
+
@"label": [self recentWeekUsageLabel]}
|
|
440
638
|
];
|
|
441
639
|
NSColor *claudeColor = [NSColor colorWithRed:0.66 green:0.43 blue:0.94 alpha:1];
|
|
442
640
|
NSArray<NSDictionary *> *rows = @[
|
|
@@ -457,11 +655,31 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
457
655
|
CGFloat x = NSMinX([self logicalSummaryCellRectAtIndex:index]);
|
|
458
656
|
const CGFloat inputX = x + 176;
|
|
459
657
|
const CGFloat outputX = x + 276;
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
658
|
+
NSFont *headerFont = [NSFont systemFontOfSize:11 weight:NSFontWeightRegular];
|
|
659
|
+
NSDictionary *headerAttributes = @{NSFontAttributeName: headerFont,
|
|
660
|
+
NSForegroundColorAttributeName: secondary};
|
|
661
|
+
NSImage *headerIcon = [NSImage imageWithSystemSymbolName:cell[@"headerSymbol"]
|
|
662
|
+
accessibilityDescription:cell[@"label"]];
|
|
663
|
+
NSImageSymbolConfiguration *headerIconConfiguration =
|
|
664
|
+
[NSImageSymbolConfiguration configurationWithPointSize:11
|
|
665
|
+
weight:NSFontWeightRegular];
|
|
666
|
+
headerIconConfiguration = [headerIconConfiguration configurationByApplyingConfiguration:
|
|
667
|
+
[NSImageSymbolConfiguration configurationWithHierarchicalColor:secondary]];
|
|
668
|
+
headerIcon = [headerIcon imageWithSymbolConfiguration:headerIconConfiguration];
|
|
669
|
+
NSTextAttachment *headerAttachment = [NSTextAttachment new];
|
|
670
|
+
headerAttachment.image = headerIcon;
|
|
671
|
+
const CGFloat headerIconSize = 13;
|
|
672
|
+
headerAttachment.bounds = NSMakeRect(0,
|
|
673
|
+
(headerFont.capHeight - headerIconSize) / 2.0, headerIconSize, headerIconSize);
|
|
674
|
+
NSMutableAttributedString *header = [[NSMutableAttributedString alloc]
|
|
675
|
+
initWithAttributedString:[NSAttributedString
|
|
676
|
+
attributedStringWithAttachment:headerAttachment]];
|
|
677
|
+
[header appendAttributedString:[[NSAttributedString alloc]
|
|
678
|
+
initWithString:@" " attributes:headerAttributes]];
|
|
679
|
+
[header appendAttributedString:[[NSAttributedString alloc]
|
|
680
|
+
initWithString:cell[@"label"] attributes:headerAttributes]];
|
|
681
|
+
[header drawInRect:NSMakeRect(x + 24, NSMinY(summary) + 89,
|
|
682
|
+
NSWidth([self logicalSummaryCellRectAtIndex:index]) - 48, 18)];
|
|
465
683
|
for (NSDictionary *row in rows) {
|
|
466
684
|
NSDictionary *usage = row[@"usage"];
|
|
467
685
|
NSColor *rowColor = row[@"color"];
|
package/package.json
CHANGED