cc-pets 1.2.6 → 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 +71 -0
- package/Sources/CCPets/CCPetsEvents.m +7 -0
- package/Sources/CCPets/CCPetsPaths.h +3 -0
- package/Sources/CCPets/CCPetsPaths.m +3 -0
- 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 +6 -0
- package/Sources/CCPets/QuotaDashboardView.m +96 -7
- 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;
|
|
@@ -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
|
|
@@ -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];
|
|
@@ -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
|
}
|
|
@@ -537,6 +603,7 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
537
603
|
: @"暂无 Agent 在线";
|
|
538
604
|
[agentText drawInRect:NSMakeRect(190, QuotaLogicalHeight - 95, 160, 22)
|
|
539
605
|
withAttributes:[self textAttributesWithSize:14 color:agentColor weight:NSFontWeightMedium]];
|
|
606
|
+
[self drawSystemMetricsEndingAtX:QuotaLogicalWidth - 28 y:QuotaLogicalHeight - 95];
|
|
540
607
|
|
|
541
608
|
BOOL recentlyRefreshed = self.refreshedUntil > NSDate.date.timeIntervalSince1970;
|
|
542
609
|
CGFloat buttonAlpha = self.refreshPressed ? 0.18 : (self.refreshHovered ? 0.12 : 0.06);
|
|
@@ -564,8 +631,10 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
564
631
|
// 每格内是一张 2×2 表:标题独占一行,每个 Provider 独占一行;输入/输出标签放在
|
|
565
632
|
// 对应数值上方。这样既能完整展示 7 天统计起期,也不会挤乱两家的数据行。
|
|
566
633
|
NSArray<NSDictionary *> *cells = @[
|
|
567
|
-
@{@"icon": @"◔", @"
|
|
568
|
-
|
|
634
|
+
@{@"icon": @"◔", @"headerSymbol": @"calendar", @"key": @"today",
|
|
635
|
+
@"label": @"今日用量"},
|
|
636
|
+
@{@"icon": @"◉", @"headerSymbol": @"calendar.badge.clock", @"key": @"recentWeek",
|
|
637
|
+
@"label": [self recentWeekUsageLabel]}
|
|
569
638
|
];
|
|
570
639
|
NSColor *claudeColor = [NSColor colorWithRed:0.66 green:0.43 blue:0.94 alpha:1];
|
|
571
640
|
NSArray<NSDictionary *> *rows = @[
|
|
@@ -586,11 +655,31 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
586
655
|
CGFloat x = NSMinX([self logicalSummaryCellRectAtIndex:index]);
|
|
587
656
|
const CGFloat inputX = x + 176;
|
|
588
657
|
const CGFloat outputX = x + 276;
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
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)];
|
|
594
683
|
for (NSDictionary *row in rows) {
|
|
595
684
|
NSDictionary *usage = row[@"usage"];
|
|
596
685
|
NSColor *rowColor = row[@"color"];
|
package/package.json
CHANGED