cc-pets 1.2.9 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/Sources/CCPets/CCPetsAppDelegate.h +3 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +36 -2
- package/Sources/CCPets/CCPetsPaths.h +7 -0
- package/Sources/CCPets/CCPetsPaths.m +25 -0
- package/Sources/CCPets/CCPetsPreviews.m +4 -0
- package/Sources/CCPets/CCPetsUsage.h +5 -0
- package/Sources/CCPets/CCPetsUsage.m +435 -18
- package/Sources/CCPets/CCPetsUsageMonitor.m +36 -7
- package/Sources/CCPets/MenuToggleSwitch.h +4 -0
- package/Sources/CCPets/MenuToggleSwitch.m +48 -0
- package/Sources/CCPets/PetView.m +58 -5
- package/Sources/CCPets/QuotaDashboardView.h +2 -0
- package/Sources/CCPets/QuotaDashboardView.m +188 -18
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -326,7 +326,11 @@ Codex 侧同理:桌宠固定只统计 `~/.codex`,不读继承来的 `CODEX_H
|
|
|
326
326
|
分别展示 Codex、Claude 的「今日」与「最近 7 天」Token,并显示最近刷新时间。统计用的是与订阅无关的
|
|
327
327
|
自然日窗口(今日 = 本机零点至今,最近 7 天 = 含今天在内的 7 个自然日),因为两家的
|
|
328
328
|
`resets_at` 并不一致,按各自订阅窗口算出的数不在同一区间上,相加没有意义。每个 Agent 卡片按 5 小时和 7 天两列
|
|
329
|
-
展示官方剩余百分比(主值)、本机 Token 用量和重置时间,右侧是近 7
|
|
329
|
+
展示官方剩余百分比(主值)、本机 Token 用量和重置时间,右侧是近 7 天额度趋势。右键菜单的
|
|
330
|
+
“用量展示模式”可分别为 Codex、Claude 选择“订阅额度”或“API 用量”;API 用量模式只替换对应
|
|
331
|
+
卡片内部,展示本月调用次数、本月输入/输出 Token、较上月同期变化和本月按日 Token 趋势。
|
|
332
|
+
“调用次数”来自本机会话记录:Claude 按请求标识去重,Codex 按去重后的累计 Token 快照计数。
|
|
333
|
+
某个窗口
|
|
330
334
|
当前没有额度数据时该列标注"当前无活跃窗口"。卡片上的在线徽章表示该 Provider 是否还有
|
|
331
335
|
运行中的客户端(包装脚本写出的 pid 文件,或该 Provider 最近 60 秒内仍在发事件),与是否
|
|
332
336
|
已采集到额度数据无关;客户端退出后显示"离线",从未采集到数据时显示"等待数据"。经
|
|
@@ -45,6 +45,8 @@
|
|
|
45
45
|
@property BOOL updating;
|
|
46
46
|
@property NSTask *updateTask;
|
|
47
47
|
@property NSTimer *usageTimer;
|
|
48
|
+
// 上一次全量聚合的时刻:面板由悬停触发,短时间内反复进出热区不必每次重算。
|
|
49
|
+
@property NSTimeInterval lastUsageRefreshAt;
|
|
48
50
|
@property NSTimer *quotaClockTimer;
|
|
49
51
|
@property NSTimer *systemMetricsTimer;
|
|
50
52
|
@property CCPetsSystemMonitor *systemMonitor;
|
|
@@ -58,6 +60,7 @@
|
|
|
58
60
|
- (void)toggleStatusBubbleFromMenu:(NSButton *)sender;
|
|
59
61
|
- (void)refreshUsage:(id)sender;
|
|
60
62
|
- (void)toggleSystemMetric:(NSButton *)sender;
|
|
63
|
+
- (void)setUsageDisplayModeFromControl:(NSButton *)sender;
|
|
61
64
|
- (void)applyCodexUsage:(NSDictionary *)codexUsage claudeUsage:(NSDictionary *)claudeUsage;
|
|
62
65
|
- (void)switchPetToID:(NSString *)petID;
|
|
63
66
|
@end
|
|
@@ -266,6 +266,25 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
266
266
|
self.quotaView.systemMemoryEnabled = [defaults boolForKey:SystemMemoryEnabledKey];
|
|
267
267
|
self.quotaView.needsDisplay = YES;
|
|
268
268
|
}
|
|
269
|
+
- (void)applyUsageDisplayModePreferences {
|
|
270
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
271
|
+
self.quotaView.codexShowsAPIUsage =
|
|
272
|
+
[[defaults stringForKey:CodexUsageDisplayModeKey] isEqualToString:@"api"];
|
|
273
|
+
self.quotaView.claudeShowsAPIUsage =
|
|
274
|
+
[[defaults stringForKey:ClaudeUsageDisplayModeKey] isEqualToString:@"api"];
|
|
275
|
+
self.quotaView.needsDisplay = YES;
|
|
276
|
+
}
|
|
277
|
+
- (void)setUsageDisplayModeFromControl:(NSButton *)sender {
|
|
278
|
+
NSString *key = sender.tag == 1 ? CodexUsageDisplayModeKey
|
|
279
|
+
: (sender.tag == 2 ? ClaudeUsageDisplayModeKey : nil);
|
|
280
|
+
NSString *mode = sender.state == NSControlStateValueOn ? @"api" : @"subscription";
|
|
281
|
+
if (!key) return;
|
|
282
|
+
[NSUserDefaults.standardUserDefaults setObject:mode forKey:key];
|
|
283
|
+
[self applyUsageDisplayModePreferences];
|
|
284
|
+
// 展示模式决定了会话要回溯多远(订阅 8 天 / API 到上月月初),切换之后必须重新聚合,
|
|
285
|
+
// 否则刚打开 API 视图时手里只有一段按 8 天窗口扫出来的数据。
|
|
286
|
+
[self refreshUsage:nil];
|
|
287
|
+
}
|
|
269
288
|
- (void)toggleSystemMetric:(NSButton *)sender {
|
|
270
289
|
NSString *key = [self systemMetricKeyForTag:sender.tag];
|
|
271
290
|
if (!key) return;
|
|
@@ -727,7 +746,9 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
727
746
|
return;
|
|
728
747
|
}
|
|
729
748
|
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
730
|
-
[defaults registerDefaults:@{StatusBubbleExpandedKey: @YES
|
|
749
|
+
[defaults registerDefaults:@{StatusBubbleExpandedKey: @YES,
|
|
750
|
+
CodexUsageDisplayModeKey: @"subscription",
|
|
751
|
+
ClaudeUsageDisplayModeKey: @"subscription"}];
|
|
731
752
|
[defaults registerDefaults:@{
|
|
732
753
|
SystemCPUEnabledKey: @YES,
|
|
733
754
|
SystemTemperatureEnabledKey: @YES,
|
|
@@ -900,6 +921,7 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
900
921
|
[quotaRoot addSubview:glass];
|
|
901
922
|
self.quotaView = [[QuotaDashboardView alloc] initWithFrame:NSMakeRect(0, 0, quotaSize.width, quotaSize.height)];
|
|
902
923
|
[self applySystemMetricPreferences];
|
|
924
|
+
[self applyUsageDisplayModePreferences];
|
|
903
925
|
self.quotaView.codexLogo = OfficialAppIcon(@"com.openai.codex", @"icon-chatgpt.icns");
|
|
904
926
|
self.quotaView.claudeLogo = OfficialAppIcon(@"com.anthropic.claudefordesktop", @"electron.icns");
|
|
905
927
|
self.quotaView.hoverChanged = ^(BOOL hovering) {
|
|
@@ -965,9 +987,20 @@ static void TrimUpdateLog(NSString *path) {
|
|
|
965
987
|
selector:@selector(refreshUsage:) userInfo:nil repeats:YES];
|
|
966
988
|
self.usageTimer.tolerance = interval * 0.2;
|
|
967
989
|
}
|
|
990
|
+
// 全量聚合是同步的主线程活儿(本机实测数十毫秒,会话目录越大越久),而面板恰好由"鼠标
|
|
991
|
+
// 停在口袋上"触发——那正是用户可能马上按下并拖动的时刻。事件被这几十毫秒挡住,mouseDown
|
|
992
|
+
// 和 mouseDragged 就会成批迟到,手感上像是"鼠标走远了宠物才跟上"。
|
|
993
|
+
// 因此:左键按着的时候不刷(拖动期间没人看数字),刚刷过的也不重刷(进出热区会反复触发)。
|
|
994
|
+
static const NSTimeInterval UsageRefreshCoalesceWindow = 2.0;
|
|
995
|
+
- (BOOL)shouldRefreshUsageForDashboard {
|
|
996
|
+
if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft)) {
|
|
997
|
+
return NO;
|
|
998
|
+
}
|
|
999
|
+
return NSDate.date.timeIntervalSince1970 - self.lastUsageRefreshAt >= UsageRefreshCoalesceWindow;
|
|
1000
|
+
}
|
|
968
1001
|
- (void)showQuotaDashboard {
|
|
969
1002
|
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideQuotaDashboardIfNeeded) object:nil];
|
|
970
|
-
[self refreshUsage:nil];
|
|
1003
|
+
if ([self shouldRefreshUsageForDashboard]) [self refreshUsage:nil];
|
|
971
1004
|
[self positionQuotaDashboard];
|
|
972
1005
|
[self.quotaPanel orderFrontRegardless];
|
|
973
1006
|
[self.panel orderFrontRegardless];
|
|
@@ -1369,6 +1402,7 @@ static BOOL UsageShowsProviderInUse(NSDictionary *usage) {
|
|
|
1369
1402
|
self.quotaView.needsDisplay = YES;
|
|
1370
1403
|
}
|
|
1371
1404
|
- (void)refreshUsage:(id)sender {
|
|
1405
|
+
self.lastUsageRefreshAt = NSDate.date.timeIntervalSince1970;
|
|
1372
1406
|
[self.usageMonitor refreshNow];
|
|
1373
1407
|
}
|
|
1374
1408
|
- (void)applicationWillTerminate:(NSNotification *)notification {
|
|
@@ -14,6 +14,10 @@ NSString *CurrentClaudeConfigDirectory(void);
|
|
|
14
14
|
// 桌宠端用:只看主账号的 Codex 目录,不受继承来的 CODEX_HOME 影响。
|
|
15
15
|
NSString *DefaultCodexHomeDirectory(void);
|
|
16
16
|
NSString *ClaudeUsagePathForConfigDirectory(NSString *configDirectory);
|
|
17
|
+
// Token 会话摘要缓存的落盘位置,按 provider 与数据源目录分文件。
|
|
18
|
+
NSString *TokenSummaryCachePath(NSString *provider, NSString *sourceDirectory);
|
|
19
|
+
// 已结束自然月的用量归档,同样按 provider 与数据源目录分文件。
|
|
20
|
+
NSString *MonthlyUsageArchivePath(NSString *provider, NSString *sourceDirectory);
|
|
17
21
|
NSString *ApplicationSupportDirectory(void);
|
|
18
22
|
NSString *QuotaHistoryPath(void);
|
|
19
23
|
// 桌宠只认这一个素材目录。~/.petdex/pets 和 ~/.codex/pets 由别的工具写入,
|
|
@@ -41,6 +45,9 @@ extern NSString *const ImportCodexPetsKey;
|
|
|
41
45
|
extern NSString *const SystemCPUEnabledKey;
|
|
42
46
|
extern NSString *const SystemTemperatureEnabledKey;
|
|
43
47
|
extern NSString *const SystemMemoryEnabledKey;
|
|
48
|
+
// 每个 Provider 独立保存额度卡展示模式,值为 "subscription" 或 "api"。
|
|
49
|
+
extern NSString *const CodexUsageDisplayModeKey;
|
|
50
|
+
extern NSString *const ClaudeUsageDisplayModeKey;
|
|
44
51
|
// 曾经检测到过的 provider 集合。只增不减:探测有可能瞬时失败(配置目录被临时改名、
|
|
45
52
|
// 外置盘没挂上),而卡片突然消失比多显示一张空卡更像 bug。
|
|
46
53
|
extern NSString *const DetectedProvidersKey;
|
|
@@ -80,6 +80,29 @@ NSString *ClaudeUsagePath(void) {
|
|
|
80
80
|
return ClaudeUsagePathForConfigDirectory(CurrentClaudeConfigDirectory());
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
// Token 聚合的会话摘要缓存。摘要只依赖会话文件里已经写死的内容,进程退出后仍然有效,
|
|
84
|
+
// 但原先只存在内存里,于是每次启动都要把整个窗口的会话重新解析一遍。落盘之后启动只需
|
|
85
|
+
// stat 一遍文件,只有长大了的会话才追读新增字节。
|
|
86
|
+
// 按数据源目录分文件:同一台机器可以有多份 CODEX_HOME / 配置目录,缓存键是绝对路径,
|
|
87
|
+
// 混在一个文件里只会让彼此的条目每轮被当成过期项清掉。
|
|
88
|
+
// 放 Application Support 而不是 PetStateDirectory():后者默认是 NSTemporaryDirectory(),
|
|
89
|
+
// 系统会清理长期没被访问的文件,桌宠停开几天回来就又要重新解析一整个月。
|
|
90
|
+
NSString *TokenSummaryCachePath(NSString *provider, NSString *sourceDirectory) {
|
|
91
|
+
NSString *resolved = sourceDirectory.stringByStandardizingPath ?: @"";
|
|
92
|
+
NSString *name = [NSString stringWithFormat:@"%@-token-cache-%016llx.plist",
|
|
93
|
+
provider, (unsigned long long)StablePathHash(resolved)];
|
|
94
|
+
return [ApplicationSupportDirectory() stringByAppendingPathComponent:name];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 已经结束的自然月,用量再也不会变。归档下来之后上个月的会话文件就一次都不用再打开,
|
|
98
|
+
// 稳态下的扫描范围收窄到"本月"。
|
|
99
|
+
NSString *MonthlyUsageArchivePath(NSString *provider, NSString *sourceDirectory) {
|
|
100
|
+
NSString *resolved = sourceDirectory.stringByStandardizingPath ?: @"";
|
|
101
|
+
NSString *name = [NSString stringWithFormat:@"%@-monthly-usage-%016llx.plist",
|
|
102
|
+
provider, (unsigned long long)StablePathHash(resolved)];
|
|
103
|
+
return [ApplicationSupportDirectory() stringByAppendingPathComponent:name];
|
|
104
|
+
}
|
|
105
|
+
|
|
83
106
|
NSString *ClaudeUsageLockPath(void) {
|
|
84
107
|
return [ClaudeUsagePath() stringByAppendingPathExtension:@"lock"];
|
|
85
108
|
}
|
|
@@ -196,6 +219,8 @@ NSString *const ImportCodexPetsKey = @"CCPetsImportCodexPets";
|
|
|
196
219
|
NSString *const SystemCPUEnabledKey = @"CCPetsSystemCPUEnabled";
|
|
197
220
|
NSString *const SystemTemperatureEnabledKey = @"CCPetsSystemTemperatureEnabled";
|
|
198
221
|
NSString *const SystemMemoryEnabledKey = @"CCPetsSystemMemoryEnabled";
|
|
222
|
+
NSString *const CodexUsageDisplayModeKey = @"CCPetsCodexUsageDisplayMode";
|
|
223
|
+
NSString *const ClaudeUsageDisplayModeKey = @"CCPetsClaudeUsageDisplayMode";
|
|
199
224
|
NSString *const DetectedProvidersKey = @"CCPetsDetectedProviders";
|
|
200
225
|
const NSTimeInterval AgentStatusInactivityInterval = 60.0;
|
|
201
226
|
// 客户端进程已经消失、但状态气泡还停在它最后一条事件上时,多等这么久再清场。
|
|
@@ -8,6 +8,10 @@ int RenderQuotaDashboard(NSString *path) {
|
|
|
8
8
|
[NSApplication sharedApplication];
|
|
9
9
|
QuotaDashboardView *view = [[QuotaDashboardView alloc] initWithFrame:NSMakeRect(0, 0,
|
|
10
10
|
QuotaLogicalWidth * QuotaScale, QuotaLogicalHeight * QuotaScale)];
|
|
11
|
+
// 本地视觉验收可用 CC_PETS_PREVIEW_API=1 渲染 API 用量态,不改用户偏好。
|
|
12
|
+
BOOL previewsAPIUsage = [NSProcessInfo.processInfo.environment[@"CC_PETS_PREVIEW_API"] boolValue];
|
|
13
|
+
view.codexShowsAPIUsage = previewsAPIUsage;
|
|
14
|
+
view.claudeShowsAPIUsage = previewsAPIUsage;
|
|
11
15
|
view.codexLogo = OfficialAppIcon(@"com.openai.codex", @"icon-chatgpt.icns");
|
|
12
16
|
view.claudeLogo = OfficialAppIcon(@"com.anthropic.claudefordesktop", @"electron.icns");
|
|
13
17
|
view.codexUsage = LatestUsage() ?: @{
|
|
@@ -9,6 +9,9 @@ NSDictionary *LatestUsage(void);
|
|
|
9
9
|
// 增量跟读 ~/.claude/projects 下的会话转录,按官方 5 小时 / 7 天窗口聚合本地 Token。
|
|
10
10
|
@interface ClaudeUsageReader : NSObject
|
|
11
11
|
@property NSURL *projectsURL;
|
|
12
|
+
// 只有常驻的桌宠进程写摘要缓存。--status / --hook 这类一次性命令照样读它(省下冷解析),
|
|
13
|
+
// 但绝不能写:它们按各自当时的窗口把缓存剪一遍再存回去,等于把桌宠攒下的月度条目清掉。
|
|
14
|
+
@property BOOL persistsCache;
|
|
12
15
|
- (NSDictionary *)refresh;
|
|
13
16
|
- (NSDictionary *)refreshWithFullAggregation;
|
|
14
17
|
@end
|
|
@@ -16,6 +19,8 @@ NSDictionary *LatestUsage(void);
|
|
|
16
19
|
// 增量跟读当前 Codex 会话文件:只在会话切换时做一次冷读,之后按偏移追读。
|
|
17
20
|
@interface CodexUsageReader : NSObject
|
|
18
21
|
@property NSURL *sessionsURL;
|
|
22
|
+
// 见 ClaudeUsageReader.persistsCache。
|
|
23
|
+
@property BOOL persistsCache;
|
|
19
24
|
@property NSURL *sessionURL;
|
|
20
25
|
@property NSDictionary *usage;
|
|
21
26
|
@property unsigned long long offset;
|