cc-pets 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CC_BRIDGE.md +5 -2
- package/CC_BRIDGE.zh-CN.md +4 -2
- package/CHANGELOG.md +85 -0
- package/CHANGELOG.zh-CN.md +85 -0
- package/CONTRIBUTING.md +28 -11
- package/CONTRIBUTING.zh-CN.md +23 -11
- package/README.md +127 -27
- package/README.zh-CN.md +104 -30
- package/Resources/zh-Hans.lproj/Localizable.strings +573 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +89 -31
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +18 -4
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +90 -21
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +269 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +13 -7
- package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +71 -47
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +16 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +110 -12
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +68 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
- package/Sources/CCPets/Pet/CCPetsImageLoader.h +5 -0
- package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +10 -2
- package/Sources/CCPets/Pet/CCPetsPetSize.h +10 -0
- package/Sources/CCPets/Pet/CCPetsPetSize.m +21 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +18 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +53 -22
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +42 -2
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +632 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +31 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
- package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +3 -0
- package/Sources/CCPets/UI/MenuPetSizeView.h +5 -0
- package/Sources/CCPets/UI/MenuPetSizeView.m +73 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +57 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +163 -6
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +616 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +10 -2
- package/bin/codex-with-pet +13 -5
- package/package.json +1 -1
- package/scripts/build.sh +7 -3
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +0 -226
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsImageLoader.h +0 -4
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1806
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// 用量页内部共用:计数读取、数字格式和指标枚举,图表、部件和页面各自 import。
|
|
2
|
+
// 都是 static inline,每个编译单元各自一份,不导出符号。
|
|
3
|
+
#import <Foundation/Foundation.h>
|
|
4
|
+
#import "CCPetsL10n.h"
|
|
5
|
+
#import "CCPetsUsage.h"
|
|
6
|
+
#import "CCPetsUsagePage.h"
|
|
7
|
+
|
|
8
|
+
static inline unsigned long long Count(NSDictionary *counts, NSString *key) {
|
|
9
|
+
return [counts[key] unsignedLongLongValue];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static inline NSString *FormatCount(unsigned long long value) {
|
|
13
|
+
static NSNumberFormatter *formatter;
|
|
14
|
+
static dispatch_once_t once;
|
|
15
|
+
dispatch_once(&once, ^{
|
|
16
|
+
formatter = [NSNumberFormatter new];
|
|
17
|
+
formatter.numberStyle = NSNumberFormatterDecimalStyle;
|
|
18
|
+
});
|
|
19
|
+
return [formatter stringFromNumber:@(value)];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 和指标条的五格一一对应,分段控件的次序也是这个。
|
|
23
|
+
typedef NS_ENUM(NSInteger, UsageMetric) {
|
|
24
|
+
UsageMetricTotal,
|
|
25
|
+
UsageMetricInput,
|
|
26
|
+
UsageMetricOutput,
|
|
27
|
+
UsageMetricCachedInput,
|
|
28
|
+
UsageMetricRequests,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
static inline unsigned long long MetricValue(UsageMetric metric, NSDictionary *counts) {
|
|
32
|
+
switch (metric) {
|
|
33
|
+
case UsageMetricInput: return Count(counts, @"input");
|
|
34
|
+
case UsageMetricOutput: return Count(counts, @"output");
|
|
35
|
+
case UsageMetricCachedInput: return Count(counts, @"cacheRead");
|
|
36
|
+
case UsageMetricRequests: return Count(counts, @"requests");
|
|
37
|
+
default: return CCPetsTotalTokens(counts);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static inline NSString *MetricTitle(UsageMetric metric) {
|
|
42
|
+
switch (metric) {
|
|
43
|
+
case UsageMetricInput: return L(@"Input");
|
|
44
|
+
case UsageMetricOutput: return L(@"Output");
|
|
45
|
+
case UsageMetricCachedInput: return L(@"Cached Input");
|
|
46
|
+
case UsageMetricRequests: return L(@"Requests");
|
|
47
|
+
default: return L(@"Total Usage");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static inline NSString *MetricText(UsageMetric metric, unsigned long long value) {
|
|
52
|
+
return metric == UsageMetricRequests ? FormatCount(value) : CCPetsFormatTokens(value);
|
|
53
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#import "CCPetsUsagePage.h"
|
|
2
|
+
#import "CCPetsProfiles.h"
|
|
3
|
+
#import "CCPetsUsageShared.h"
|
|
4
|
+
|
|
5
|
+
#pragma mark - 数据
|
|
6
|
+
|
|
7
|
+
// 统计这么多天,切换 7 / 30 / 90 天只在本地截取。
|
|
8
|
+
static const NSInteger UsageMaximumRangeDays = 90;
|
|
9
|
+
// 结果在这段时间内算新鲜:来回切页面不重算,隔一阵再打开能看到新用量。
|
|
10
|
+
static const NSTimeInterval UsageStoreFreshness = 120;
|
|
11
|
+
|
|
12
|
+
NSNotificationName const CCPetsUsageStoreDidChangeNotification = @"CCPetsUsageStoreDidChange";
|
|
13
|
+
static NSDictionary<NSString *, NSDictionary *> *UsageStoreResults;
|
|
14
|
+
static NSDictionary<NSString *, NSDictionary *> *UsageStoreProfileResults;
|
|
15
|
+
static NSTimeInterval UsageStoreLoadedAt;
|
|
16
|
+
// 结果所对应的"今天"零点。跨过午夜后区间要整体后移一天,哪怕结果才算出来一分钟也得重算。
|
|
17
|
+
static NSTimeInterval UsageStoreLoadedDayStart;
|
|
18
|
+
static NSString *UsageStoreProfileFingerprint;
|
|
19
|
+
static BOOL UsageStoreLoading;
|
|
20
|
+
|
|
21
|
+
@implementation CCPetsUsageStore
|
|
22
|
+
|
|
23
|
+
+ (NSDictionary<NSString *, NSDictionary *> *)results {
|
|
24
|
+
return UsageStoreResults;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
+ (NSDictionary<NSString *, NSDictionary *> *)profileResults {
|
|
28
|
+
return UsageStoreProfileResults;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static NSDictionary *MergeModelUsageResults(NSArray<NSDictionary *> *values) {
|
|
32
|
+
if (values.count == 0) return @{ @"models": @{}, @"daily": @[] };
|
|
33
|
+
NSMutableDictionary<NSNumber *, NSMutableDictionary *> *days = [NSMutableDictionary dictionary];
|
|
34
|
+
NSMutableDictionary<NSString *, NSMutableDictionary *> *totals = [NSMutableDictionary dictionary];
|
|
35
|
+
for (NSDictionary *usage in values) {
|
|
36
|
+
for (NSDictionary *day in usage[@"daily"] ?: @[]) {
|
|
37
|
+
NSNumber *stamp = day[@"timestamp"];
|
|
38
|
+
if (!stamp) continue;
|
|
39
|
+
NSMutableDictionary *models = days[stamp];
|
|
40
|
+
if (!models) { models = [NSMutableDictionary dictionary]; days[stamp] = models; }
|
|
41
|
+
for (NSString *model in day[@"models"] ?: @{}) {
|
|
42
|
+
NSMutableDictionary *counts = models[model];
|
|
43
|
+
if (!counts) { counts = [NSMutableDictionary dictionary]; models[model] = counts; }
|
|
44
|
+
NSDictionary *incoming = day[@"models"][model];
|
|
45
|
+
for (NSString *key in @[@"input", @"cacheRead", @"cacheWrite", @"output", @"reasoning", @"requests"]) {
|
|
46
|
+
counts[key] = @(Count(counts, key) + Count(incoming, key));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
NSMutableArray *daily = [NSMutableArray array];
|
|
52
|
+
for (NSNumber *stamp in [days.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
|
|
53
|
+
NSDictionary *models = days[stamp];
|
|
54
|
+
[daily addObject:@{ @"timestamp": stamp, @"models": models }];
|
|
55
|
+
for (NSString *model in models) {
|
|
56
|
+
NSMutableDictionary *sum = totals[model];
|
|
57
|
+
if (!sum) { sum = [NSMutableDictionary dictionary]; totals[model] = sum; }
|
|
58
|
+
for (NSString *key in @[@"input", @"cacheRead", @"cacheWrite", @"output", @"reasoning", @"requests"]) {
|
|
59
|
+
sum[key] = @(Count(sum, key) + Count(models[model], key));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return @{ @"models": totals, @"daily": daily };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
+ (BOOL)loading {
|
|
67
|
+
return UsageStoreLoading;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 区间是 [今天往前数 89 天的零点, 明天零点),含今天。
|
|
71
|
+
+ (void)loadIfStale {
|
|
72
|
+
if (UsageStoreLoading) return;
|
|
73
|
+
NSMutableArray<NSString *> *identity = [NSMutableArray array];
|
|
74
|
+
for (NSDictionary *profile in CCPetsProfiles()) {
|
|
75
|
+
[identity addObject:[NSString stringWithFormat:@"%@|%@", profile[@"id"], profile[@"home"]]];
|
|
76
|
+
}
|
|
77
|
+
NSString *fingerprint = [identity componentsJoinedByString:@"\n"];
|
|
78
|
+
NSCalendar *calendar = NSCalendar.currentCalendar;
|
|
79
|
+
NSDate *today = [calendar startOfDayForDate:NSDate.date];
|
|
80
|
+
if (UsageStoreResults && [UsageStoreProfileFingerprint isEqualToString:fingerprint] &&
|
|
81
|
+
NSDate.date.timeIntervalSince1970 - UsageStoreLoadedAt < UsageStoreFreshness &&
|
|
82
|
+
UsageStoreLoadedDayStart == today.timeIntervalSince1970) return;
|
|
83
|
+
UsageStoreLoading = YES;
|
|
84
|
+
NSDate *end = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:today options:0];
|
|
85
|
+
NSDate *start = [calendar dateByAddingUnit:NSCalendarUnitDay value:-UsageMaximumRangeDays toDate:end options:0];
|
|
86
|
+
// 串行队列:上一轮还没算完时不会两份同时写同一批缓存文件。
|
|
87
|
+
static dispatch_queue_t queue;
|
|
88
|
+
static dispatch_once_t once;
|
|
89
|
+
dispatch_once(&once, ^{
|
|
90
|
+
queue = dispatch_queue_create("com.ccpets.model-usage", DISPATCH_QUEUE_SERIAL);
|
|
91
|
+
});
|
|
92
|
+
dispatch_async(queue, ^{
|
|
93
|
+
NSMutableDictionary *results = [NSMutableDictionary dictionary];
|
|
94
|
+
NSMutableDictionary *profileResults = [NSMutableDictionary dictionary];
|
|
95
|
+
for (NSString *provider in @[@"claude", @"codex"]) {
|
|
96
|
+
NSArray<NSDictionary *> *profiles = CCPetsProfilesForProvider(provider);
|
|
97
|
+
if (profiles.count == 0) continue;
|
|
98
|
+
NSMutableArray<NSDictionary *> *providerValues = [NSMutableArray array];
|
|
99
|
+
for (NSDictionary *profile in profiles) {
|
|
100
|
+
NSDictionary *usage = CCPetsModelUsageAtHome(provider, profile[@"home"],
|
|
101
|
+
start.timeIntervalSince1970, end.timeIntervalSince1970);
|
|
102
|
+
if (!usage) continue;
|
|
103
|
+
profileResults[profile[@"id"]] = usage;
|
|
104
|
+
[providerValues addObject:usage];
|
|
105
|
+
}
|
|
106
|
+
results[provider] = MergeModelUsageResults(providerValues);
|
|
107
|
+
}
|
|
108
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
109
|
+
UsageStoreResults = results;
|
|
110
|
+
UsageStoreProfileResults = profileResults;
|
|
111
|
+
UsageStoreLoadedAt = NSDate.date.timeIntervalSince1970;
|
|
112
|
+
UsageStoreLoadedDayStart = today.timeIntervalSince1970;
|
|
113
|
+
UsageStoreProfileFingerprint = fingerprint;
|
|
114
|
+
UsageStoreLoading = NO;
|
|
115
|
+
[NSNotificationCenter.defaultCenter postNotificationName:CCPetsUsageStoreDidChangeNotification object:nil];
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 用量页的小部件:折行容器、图例项、模型表行、占比条。
|
|
4
|
+
|
|
5
|
+
// 自动换行的横排容器:图例项多了就折到下一行,不撑宽页面。
|
|
6
|
+
@interface CCPetsFlowView : NSView
|
|
7
|
+
@property(nonatomic) CGFloat spacing;
|
|
8
|
+
@property(nonatomic) CGFloat lineSpacing;
|
|
9
|
+
@end
|
|
10
|
+
|
|
11
|
+
// 图例项:色点 + 模型名。可点的图例就是筛选器,点了只看这个模型。
|
|
12
|
+
@interface CCPetsLegendItem : NSView
|
|
13
|
+
@property(nonatomic) BOOL dimmed;
|
|
14
|
+
@property(nonatomic) BOOL selected;
|
|
15
|
+
@property(nonatomic, copy) void (^onClick)(void);
|
|
16
|
+
- (instancetype)initWithTitle:(NSString *)title color:(NSColor *)color;
|
|
17
|
+
@end
|
|
18
|
+
|
|
19
|
+
// 模型表的一行:色块 + 模型名 + 来源,占比条,四列数字。整行可点,和图例同一个筛选。
|
|
20
|
+
@interface CCPetsUsageModelRow : NSView
|
|
21
|
+
@property(nonatomic) BOOL selected;
|
|
22
|
+
@property(nonatomic) BOOL dimmed;
|
|
23
|
+
@property(nonatomic, copy) void (^onClick)(void);
|
|
24
|
+
@end
|
|
25
|
+
|
|
26
|
+
// 占比条:底轨 + 模型色。
|
|
27
|
+
@interface CCPetsShareBar : NSView
|
|
28
|
+
@property(nonatomic) double fraction;
|
|
29
|
+
@property(nonatomic) NSColor *color;
|
|
30
|
+
@end
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
#import "CCPetsUsageWidgets.h"
|
|
2
|
+
#import "CCPetsUIStyle.h"
|
|
3
|
+
|
|
4
|
+
@interface CCPetsFlowView ()
|
|
5
|
+
@property(nonatomic) CGFloat laidOutHeight;
|
|
6
|
+
@end
|
|
7
|
+
|
|
8
|
+
@implementation CCPetsFlowView
|
|
9
|
+
|
|
10
|
+
- (BOOL)isFlipped {
|
|
11
|
+
return YES;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
- (CGFloat)heightForWidth:(CGFloat)width layout:(BOOL)apply {
|
|
15
|
+
CGFloat x = 0, y = 0, lineHeight = 0;
|
|
16
|
+
for (NSView *view in self.subviews) {
|
|
17
|
+
NSSize size = view.fittingSize;
|
|
18
|
+
if (x > 0 && x + size.width > width) {
|
|
19
|
+
x = 0;
|
|
20
|
+
y += lineHeight + self.lineSpacing;
|
|
21
|
+
lineHeight = 0;
|
|
22
|
+
}
|
|
23
|
+
if (apply) view.frame = NSMakeRect(x, y, size.width, size.height);
|
|
24
|
+
x += size.width + self.spacing;
|
|
25
|
+
lineHeight = MAX(lineHeight, size.height);
|
|
26
|
+
}
|
|
27
|
+
return y + lineHeight;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 折行后的高度变了才通知自动布局;每次都通知会让布局一轮接一轮地重排。
|
|
31
|
+
- (void)layout {
|
|
32
|
+
[super layout];
|
|
33
|
+
CGFloat height = [self heightForWidth:NSWidth(self.bounds) layout:YES];
|
|
34
|
+
if (fabs(height - self.laidOutHeight) > 0.5) {
|
|
35
|
+
self.laidOutHeight = height;
|
|
36
|
+
[self invalidateIntrinsicContentSize];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
- (NSSize)intrinsicContentSize {
|
|
41
|
+
CGFloat width = NSWidth(self.bounds) > 0 ? NSWidth(self.bounds) : 600;
|
|
42
|
+
return NSMakeSize(NSViewNoIntrinsicMetric, [self heightForWidth:width layout:NO]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
- (void)setFrameSize:(NSSize)size {
|
|
46
|
+
BOOL widthChanged = fabs(size.width - NSWidth(self.frame)) > 0.5;
|
|
47
|
+
[super setFrameSize:size];
|
|
48
|
+
if (widthChanged) [self invalidateIntrinsicContentSize];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@end
|
|
52
|
+
|
|
53
|
+
@interface CCPetsLegendItem ()
|
|
54
|
+
@property(nonatomic) CCPetsColorDot *dot;
|
|
55
|
+
@property(nonatomic) NSTextField *label;
|
|
56
|
+
@property(nonatomic) NSColor *color;
|
|
57
|
+
@end
|
|
58
|
+
|
|
59
|
+
@implementation CCPetsLegendItem
|
|
60
|
+
|
|
61
|
+
- (instancetype)initWithTitle:(NSString *)title color:(NSColor *)color {
|
|
62
|
+
if ((self = [super initWithFrame:NSZeroRect])) {
|
|
63
|
+
self.wantsLayer = YES;
|
|
64
|
+
self.color = color;
|
|
65
|
+
CCPetsColorDot *dot = [[CCPetsColorDot alloc] initWithColor:color diameter:8];
|
|
66
|
+
NSTextField *label = CCPetsLabel(title, 11, NSFontWeightMedium, CCPetsSecondaryInkColor());
|
|
67
|
+
label.font = [NSFont monospacedSystemFontOfSize:11 weight:NSFontWeightMedium];
|
|
68
|
+
for (NSView *view in @[dot, label]) {
|
|
69
|
+
view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
70
|
+
[self addSubview:view];
|
|
71
|
+
}
|
|
72
|
+
[NSLayoutConstraint activateConstraints:@[
|
|
73
|
+
[dot.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:8],
|
|
74
|
+
[dot.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
|
|
75
|
+
[label.leadingAnchor constraintEqualToAnchor:dot.trailingAnchor constant:6],
|
|
76
|
+
[label.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-8],
|
|
77
|
+
[label.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
|
|
78
|
+
[self.heightAnchor constraintEqualToConstant:24],
|
|
79
|
+
]];
|
|
80
|
+
self.dot = dot;
|
|
81
|
+
self.label = label;
|
|
82
|
+
}
|
|
83
|
+
return self;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
- (void)setDimmed:(BOOL)dimmed {
|
|
87
|
+
_dimmed = dimmed;
|
|
88
|
+
self.alphaValue = dimmed ? 0.45 : 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
- (void)setSelected:(BOOL)selected {
|
|
92
|
+
_selected = selected;
|
|
93
|
+
self.label.textColor = selected ? CCPetsInkColor() : CCPetsSecondaryInkColor();
|
|
94
|
+
self.label.font = [NSFont monospacedSystemFontOfSize:11 weight:selected ? NSFontWeightBold : NSFontWeightMedium];
|
|
95
|
+
self.needsDisplay = YES;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
- (BOOL)wantsUpdateLayer {
|
|
99
|
+
return YES;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
- (void)updateLayer {
|
|
103
|
+
self.layer.cornerRadius = 6;
|
|
104
|
+
self.layer.backgroundColor = self.selected ? CCPetsSelectionColor().CGColor : NSColor.clearColor.CGColor;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
- (void)mouseUp:(NSEvent *)event {
|
|
108
|
+
if (self.onClick) self.onClick();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
- (void)mouseDown:(NSEvent *)event {
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
- (void)resetCursorRects {
|
|
115
|
+
if (self.onClick) [self addCursorRect:self.bounds cursor:NSCursor.pointingHandCursor];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
- (BOOL)isAccessibilityElement {
|
|
119
|
+
return YES;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
- (NSAccessibilityRole)accessibilityRole {
|
|
123
|
+
return self.onClick ? NSAccessibilityButtonRole : NSAccessibilityStaticTextRole;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
- (NSString *)accessibilityLabel {
|
|
127
|
+
return self.label.stringValue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
- (BOOL)accessibilityPerformPress {
|
|
131
|
+
if (self.onClick) self.onClick();
|
|
132
|
+
return self.onClick != nil;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@end
|
|
136
|
+
|
|
137
|
+
@implementation CCPetsUsageModelRow
|
|
138
|
+
|
|
139
|
+
- (instancetype)initWithFrame:(NSRect)frame {
|
|
140
|
+
if ((self = [super initWithFrame:frame])) self.wantsLayer = YES;
|
|
141
|
+
return self;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
- (BOOL)wantsUpdateLayer {
|
|
145
|
+
return YES;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
- (void)updateLayer {
|
|
149
|
+
self.layer.backgroundColor = self.selected ? CCPetsSelectionColor().CGColor : NSColor.clearColor.CGColor;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
- (void)setSelected:(BOOL)selected {
|
|
153
|
+
_selected = selected;
|
|
154
|
+
self.needsDisplay = YES;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
- (void)setDimmed:(BOOL)dimmed {
|
|
158
|
+
_dimmed = dimmed;
|
|
159
|
+
for (NSView *view in self.subviews) view.alphaValue = dimmed ? 0.45 : 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
- (void)mouseDown:(NSEvent *)event {
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
- (void)mouseUp:(NSEvent *)event {
|
|
166
|
+
if (self.onClick) self.onClick();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
- (BOOL)acceptsFirstMouse:(NSEvent *)event {
|
|
170
|
+
return YES;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
- (BOOL)isAccessibilityElement {
|
|
174
|
+
return YES;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
- (NSAccessibilityRole)accessibilityRole {
|
|
178
|
+
return NSAccessibilityButtonRole;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
- (BOOL)accessibilityPerformPress {
|
|
182
|
+
if (self.onClick) self.onClick();
|
|
183
|
+
return YES;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@end
|
|
187
|
+
|
|
188
|
+
@implementation CCPetsShareBar
|
|
189
|
+
- (NSSize)intrinsicContentSize {
|
|
190
|
+
return NSMakeSize(NSViewNoIntrinsicMetric, 4);
|
|
191
|
+
}
|
|
192
|
+
- (void)drawRect:(NSRect)dirtyRect {
|
|
193
|
+
NSRect bounds = self.bounds;
|
|
194
|
+
[CCPetsRingTrackColor() set];
|
|
195
|
+
[[NSBezierPath bezierPathWithRoundedRect:bounds xRadius:2 yRadius:2] fill];
|
|
196
|
+
NSRect fill = bounds;
|
|
197
|
+
fill.size.width = MAX(3, bounds.size.width * MIN(1, MAX(0, self.fraction)));
|
|
198
|
+
[self.color set];
|
|
199
|
+
[[NSBezierPath bezierPathWithRoundedRect:fill xRadius:2 yRadius:2] fill];
|
|
200
|
+
}
|
|
201
|
+
@end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 主窗口「工作台」几页(概览、会话)要的数据,由 AppDelegate 提供。和 CCPetsSettingsActions
|
|
4
|
+
// 一样只声明契约:页面不 import AppDelegate 的私有头,AppDelegate 也不在声明上采纳协议
|
|
5
|
+
// (方法分散在几个 category 里),签名一致由 tests/test.sh 的检查守着。
|
|
6
|
+
//
|
|
7
|
+
// 返回的都是快照:普通字典和数组,页面拿去渲染,不回写。
|
|
8
|
+
@protocol CCPetsWorkbenchSource <NSObject>
|
|
9
|
+
|
|
10
|
+
// 在线会话,等审批的排最前,其余按最近活动倒序。每项:
|
|
11
|
+
// id(会话键,交回 focusWorkbenchSessionWithID: 用)、provider(Claude / Codex)、
|
|
12
|
+
// name(CC Bridge 里的会话名,没开 Bridge 时没有)、state(Agent 状态)、status(状态文字)、
|
|
13
|
+
// tool(正在用的工具,可能为空)、terminal(终端名)、updatedAt(最近一条事件的时间戳)、
|
|
14
|
+
// pendingMessages(这个会话信箱里还没投递的消息数)。
|
|
15
|
+
- (NSArray<NSDictionary *> *)workbenchSessions;
|
|
16
|
+
|
|
17
|
+
// CC Bridge 消息,只有元数据,没有正文。键:
|
|
18
|
+
// enabled(Bridge 是否开启)、
|
|
19
|
+
// pending(数组:session、name、count —— 卡在信箱里、要用户去那个终端开口才会送进去的消息)、
|
|
20
|
+
// deliveries(数组:from、to、toSession、at、unseen —— 最近送达的消息,新的在前)。
|
|
21
|
+
- (NSDictionary *)workbenchMessages;
|
|
22
|
+
|
|
23
|
+
// 订阅额度,每个已登记账号一项(未登记的平台保留一张卡片)。键:provider(Claude / Codex)、
|
|
24
|
+
// profileId / profileName(账号身份与名称;未登记时没有)、
|
|
25
|
+
// profile(账号配置)、usage(与悬停面板相同的完整额度和本地 Token)、history(该账号额度历史)、
|
|
26
|
+
// apiMode(是否显示 API 用量)、online / hasData(在线与数据状态)、state / status(状态与显示文案)、
|
|
27
|
+
// fiveRemaining / weekRemaining(剩余百分比)、fiveReset / weekReset(重置时间)、pace / paceLevel(本周节奏)、
|
|
28
|
+
// hasDiagnosticIssues(该账号确实有 warn / fail 诊断结果,缺少额度不算问题)。
|
|
29
|
+
- (NSArray<NSDictionary *> *)workbenchQuotas;
|
|
30
|
+
|
|
31
|
+
// 跳到会话所在的终端。
|
|
32
|
+
- (void)focusWorkbenchSessionWithID:(NSString *)sessionID;
|
|
33
|
+
// 跳到 CC Bridge 会话所在的终端(收件人、信箱积压的那个会话)。
|
|
34
|
+
- (void)focusBridgeSessionWithID:(NSString *)sessionID;
|
|
35
|
+
// 用户在主窗口看过消息了:和打开右键会话菜单一样,清掉桌宠上的未读角标。
|
|
36
|
+
- (void)markWorkbenchMessagesSeen;
|
|
37
|
+
|
|
38
|
+
@end
|
|
39
|
+
|
|
40
|
+
extern NSNotificationName const CCPetsWorkbenchQuotasDidChangeNotification;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 主窗口「工作台」组里的两页。数据都从 AppDelegate 现取快照(CCPetsWorkbench.h),页面只读。
|
|
4
|
+
|
|
5
|
+
// 概览:宠物和它此刻的状态、今天的用量、诊断提醒、订阅额度。会话和消息不在这里重复列,
|
|
6
|
+
// 它们有自己的一页。
|
|
7
|
+
@interface CCPetsOverviewPage : NSViewController
|
|
8
|
+
@end
|
|
9
|
+
|
|
10
|
+
// 会话:在线的 Agent 会话(等审批的置顶,一键跳回终端),以及 CC Bridge 的消息往来。
|
|
11
|
+
// 页面开着时每几秒重读一次快照(会话状态一变也会立刻刷新);窗口不可见时停表。
|
|
12
|
+
@interface CCPetsSessionsPage : NSViewController
|
|
13
|
+
@end
|