cc-pets 2.2.1 → 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 +60 -0
- package/CHANGELOG.zh-CN.md +60 -0
- package/CONTRIBUTING.md +13 -10
- package/CONTRIBUTING.zh-CN.md +11 -10
- package/README.md +113 -24
- package/README.zh-CN.md +89 -28
- package/Resources/zh-Hans.lproj/Localizable.strings +568 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +82 -23
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +17 -3
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +70 -22
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/{CCPetsAppDelegate+Settings.m → App/CCPetsAppDelegate+Settings.m} +86 -98
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +8 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +15 -0
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +107 -11
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +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/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +11 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +19 -2
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +4 -1
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +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/{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/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1839
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.h → Pet/CCPetsImageLoader.h} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.h → Pet/CCPetsPetSize.h} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.m → Pet/CCPetsPetSize.m} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.h → UI/MenuPetSizeView.h} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.m → UI/MenuPetSizeView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
#import "CCPetsUsageShared.h"
|
|
3
|
+
|
|
4
|
+
// 每日柱状图:横轴是天,纵轴是当前指标。"全部模型"时每根柱子按模型分色堆叠(用量大的在下),
|
|
5
|
+
// 只看一个模型时就是这个模型自己的颜色。鼠标移上去在柱子旁浮出当天的明细。
|
|
6
|
+
@interface CCPetsUsageChartView : NSView
|
|
7
|
+
// 每天零点的时间戳。
|
|
8
|
+
@property(nonatomic, copy) NSArray<NSNumber *> *days;
|
|
9
|
+
// 自下而上的堆叠层。每项:title、color、values(每天的指标值)、counts(每天的四栏,单模型明细用)。
|
|
10
|
+
@property(nonatomic, copy) NSArray<NSDictionary *> *series;
|
|
11
|
+
@property(nonatomic) UsageMetric metric;
|
|
12
|
+
// 只看一个模型:悬停明细列这个模型的各栏,而不是各模型。
|
|
13
|
+
@property(nonatomic) BOOL singleModel;
|
|
14
|
+
- (void)reloadData;
|
|
15
|
+
@end
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
#import "CCPetsUsageChartView.h"
|
|
2
|
+
#import "CCPetsUIStyle.h"
|
|
3
|
+
|
|
4
|
+
@interface CCPetsUsageChartView ()
|
|
5
|
+
@property(nonatomic) NSInteger hoverIndex;
|
|
6
|
+
@end
|
|
7
|
+
|
|
8
|
+
@implementation CCPetsUsageChartView
|
|
9
|
+
|
|
10
|
+
static const CGFloat ChartLeft = 50, ChartBottom = 24, ChartTop = 10, ChartRight = 4;
|
|
11
|
+
|
|
12
|
+
- (instancetype)initWithFrame:(NSRect)frame {
|
|
13
|
+
if ((self = [super initWithFrame:frame])) {
|
|
14
|
+
_hoverIndex = -1;
|
|
15
|
+
[self addTrackingArea:[[NSTrackingArea alloc] initWithRect:NSZeroRect
|
|
16
|
+
options:NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow |
|
|
17
|
+
NSTrackingInVisibleRect owner:self userInfo:nil]];
|
|
18
|
+
}
|
|
19
|
+
return self;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
- (BOOL)isFlipped {
|
|
23
|
+
return NO;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
- (void)reloadData {
|
|
27
|
+
self.hoverIndex = -1;
|
|
28
|
+
self.needsDisplay = YES;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 纵轴分四格,步长取 1 / 2 / 2.5 / 5 × 10^n:每条刻度都是整齐的数,不会出现 133M / 66.7M。
|
|
32
|
+
static double NiceStep(double peak) {
|
|
33
|
+
if (peak <= 0) return 1;
|
|
34
|
+
double raw = peak / 4;
|
|
35
|
+
double magnitude = pow(10, floor(log10(raw)));
|
|
36
|
+
for (NSNumber *step in @[@1, @2, @2.5, @5, @10]) {
|
|
37
|
+
if (raw <= step.doubleValue * magnitude) return step.doubleValue * magnitude;
|
|
38
|
+
}
|
|
39
|
+
return 10 * magnitude;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
- (NSRect)plotRect {
|
|
43
|
+
return NSMakeRect(ChartLeft, ChartBottom, NSWidth(self.bounds) - ChartLeft - ChartRight,
|
|
44
|
+
NSHeight(self.bounds) - ChartBottom - ChartTop);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
- (double)totalAtIndex:(NSUInteger)index {
|
|
48
|
+
double total = 0;
|
|
49
|
+
for (NSDictionary *layer in self.series) total += [layer[@"values"][index] doubleValue];
|
|
50
|
+
return total;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
- (void)drawRect:(NSRect)dirtyRect {
|
|
54
|
+
NSRect plot = [self plotRect];
|
|
55
|
+
NSUInteger count = self.days.count;
|
|
56
|
+
double peak = 0;
|
|
57
|
+
for (NSUInteger index = 0; index < count; index++) peak = MAX(peak, [self totalAtIndex:index]);
|
|
58
|
+
double step = NiceStep(peak);
|
|
59
|
+
double top = step * 4;
|
|
60
|
+
NSDictionary *axisAttributes = @{NSFontAttributeName: [NSFont monospacedDigitSystemFontOfSize:10
|
|
61
|
+
weight:NSFontWeightRegular], NSForegroundColorAttributeName: CCPetsTertiaryInkColor()};
|
|
62
|
+
|
|
63
|
+
// 网格:底线实线,其余虚线,都很淡,让柱子当主角。
|
|
64
|
+
for (int line = 0; line <= 4; line++) {
|
|
65
|
+
CGFloat y = round(NSMinY(plot) + plot.size.height * line / 4.0) + 0.5;
|
|
66
|
+
NSBezierPath *path = [NSBezierPath bezierPath];
|
|
67
|
+
[path moveToPoint:NSMakePoint(NSMinX(plot), y)];
|
|
68
|
+
[path lineToPoint:NSMakePoint(NSMaxX(plot), y)];
|
|
69
|
+
path.lineWidth = 1;
|
|
70
|
+
if (line > 0) {
|
|
71
|
+
CGFloat dash[] = {3, 3};
|
|
72
|
+
[path setLineDash:dash count:2 phase:0];
|
|
73
|
+
}
|
|
74
|
+
[CCPetsLineColor() set];
|
|
75
|
+
[path stroke];
|
|
76
|
+
NSString *label = MetricText(self.metric, (unsigned long long)(step * line));
|
|
77
|
+
NSSize size = [label sizeWithAttributes:axisAttributes];
|
|
78
|
+
[label drawAtPoint:NSMakePoint(NSMinX(plot) - size.width - 8, y - size.height / 2)
|
|
79
|
+
withAttributes:axisAttributes];
|
|
80
|
+
}
|
|
81
|
+
if (count == 0) return;
|
|
82
|
+
|
|
83
|
+
CGFloat slot = plot.size.width / count;
|
|
84
|
+
CGFloat barWidth = MAX(2, MIN(26, slot * 0.62));
|
|
85
|
+
if (self.hoverIndex >= 0 && self.hoverIndex < (NSInteger)count) {
|
|
86
|
+
[CCPetsHoverColor() set];
|
|
87
|
+
NSRectFillUsingOperation(NSMakeRect(NSMinX(plot) + slot * self.hoverIndex, NSMinY(plot), slot,
|
|
88
|
+
plot.size.height), NSCompositingOperationSourceOver);
|
|
89
|
+
}
|
|
90
|
+
for (NSUInteger index = 0; index < count; index++) {
|
|
91
|
+
CGFloat x = round(NSMinX(plot) + slot * index + (slot - barWidth) / 2);
|
|
92
|
+
CGFloat y = NSMinY(plot) + 1;
|
|
93
|
+
NSMutableArray<NSValue *> *segments = [NSMutableArray array];
|
|
94
|
+
NSMutableArray<NSColor *> *colors = [NSMutableArray array];
|
|
95
|
+
for (NSDictionary *layer in self.series) {
|
|
96
|
+
double value = [layer[@"values"][index] doubleValue];
|
|
97
|
+
if (value <= 0) continue;
|
|
98
|
+
CGFloat height = MAX(1, plot.size.height * value / top);
|
|
99
|
+
[segments addObject:[NSValue valueWithRect:NSMakeRect(x, y, barWidth, height)]];
|
|
100
|
+
[colors addObject:layer[@"color"]];
|
|
101
|
+
y += height;
|
|
102
|
+
}
|
|
103
|
+
// 堆叠段之间留 1pt 的底色缝,相邻两色分得清;最顶上一段圆角。
|
|
104
|
+
for (NSUInteger part = 0; part < segments.count; part++) {
|
|
105
|
+
NSRect rect = segments[part].rectValue;
|
|
106
|
+
BOOL last = part == segments.count - 1;
|
|
107
|
+
if (!last && rect.size.height > 2) rect.size.height -= 1;
|
|
108
|
+
[colors[part] set];
|
|
109
|
+
if (last && rect.size.height >= 3) {
|
|
110
|
+
CGFloat radius = MIN(2.5, barWidth / 3);
|
|
111
|
+
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
|
|
112
|
+
[path fill];
|
|
113
|
+
NSRectFill(NSMakeRect(rect.origin.x, rect.origin.y, rect.size.width, MIN(radius, rect.size.height)));
|
|
114
|
+
} else {
|
|
115
|
+
NSRectFill(rect);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
[self drawDateLabelsInPlot:plot slot:slot attributes:axisAttributes];
|
|
120
|
+
if (self.hoverIndex >= 0 && self.hoverIndex < (NSInteger)count) [self drawTooltipInPlot:plot slot:slot];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 横轴只标六个左右的日期,最后一个写"今天";天数多了标满会挤成一团。
|
|
124
|
+
- (void)drawDateLabelsInPlot:(NSRect)plot slot:(CGFloat)slot attributes:(NSDictionary *)attributes {
|
|
125
|
+
static NSDateFormatter *formatter;
|
|
126
|
+
static dispatch_once_t once;
|
|
127
|
+
dispatch_once(&once, ^{
|
|
128
|
+
formatter = [NSDateFormatter new];
|
|
129
|
+
formatter.dateFormat = @"M/d";
|
|
130
|
+
});
|
|
131
|
+
NSUInteger count = self.days.count;
|
|
132
|
+
NSUInteger labelStep = MAX(1, (NSUInteger)ceil(count / 6.0));
|
|
133
|
+
CGFloat lastRight = -CGFLOAT_MAX;
|
|
134
|
+
// 从今天往回标:保证"今天"一定出现,其余按步长倒推。
|
|
135
|
+
NSMutableArray<NSNumber *> *indexes = [NSMutableArray array];
|
|
136
|
+
for (NSInteger index = (NSInteger)count - 1; index >= 0; index -= (NSInteger)labelStep) [indexes insertObject:@(index) atIndex:0];
|
|
137
|
+
for (NSNumber *number in indexes) {
|
|
138
|
+
NSUInteger index = number.unsignedIntegerValue;
|
|
139
|
+
NSString *label = index == count - 1 ? L(@"today")
|
|
140
|
+
: [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.days[index].doubleValue]];
|
|
141
|
+
NSSize size = [label sizeWithAttributes:attributes];
|
|
142
|
+
CGFloat x = NSMinX(plot) + slot * index + slot / 2 - size.width / 2;
|
|
143
|
+
x = MIN(MAX(x, NSMinX(plot) - 4), NSMaxX(self.bounds) - size.width);
|
|
144
|
+
if (x < lastRight + 6) continue;
|
|
145
|
+
[label drawAtPoint:NSMakePoint(x, 5) withAttributes:attributes];
|
|
146
|
+
lastRight = x + size.width;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 悬停明细:深色日期条 + 每行一个色条、名字、数值 + 合计。全部模型时按当天量从大到小列各模型
|
|
151
|
+
// (当天为 0 的不列);单个模型时列它的输入、缓存写、输出、缓存读和请求。
|
|
152
|
+
- (void)drawTooltipInPlot:(NSRect)plot slot:(CGFloat)slot {
|
|
153
|
+
NSUInteger index = (NSUInteger)self.hoverIndex;
|
|
154
|
+
NSMutableArray<NSDictionary *> *rows = [NSMutableArray array];
|
|
155
|
+
if (self.singleModel) {
|
|
156
|
+
NSDictionary *layer = self.series.firstObject;
|
|
157
|
+
NSDictionary *counts = layer[@"counts"][index];
|
|
158
|
+
NSColor *color = layer[@"color"];
|
|
159
|
+
for (NSArray *part in @[@[L(@"Input"), @"input"], @[L(@"Cache Write"), @"cacheWrite"],
|
|
160
|
+
@[L(@"Output"), @"output"], @[L(@"Cached Input"), @"cacheRead"]]) {
|
|
161
|
+
[rows addObject:@{@"title": part[0], @"value": CCPetsFormatTokens(Count(counts, part[1])), @"color": color}];
|
|
162
|
+
}
|
|
163
|
+
[rows addObject:@{@"title": L(@"Requests"), @"value": FormatCount(Count(counts, @"requests")), @"color": color}];
|
|
164
|
+
} else {
|
|
165
|
+
NSMutableArray<NSDictionary *> *layers = [NSMutableArray array];
|
|
166
|
+
for (NSDictionary *layer in self.series) {
|
|
167
|
+
if ([layer[@"values"][index] doubleValue] > 0) [layers addObject:layer];
|
|
168
|
+
}
|
|
169
|
+
[layers sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
|
|
170
|
+
return [b[@"values"][index] compare:a[@"values"][index]];
|
|
171
|
+
}];
|
|
172
|
+
for (NSDictionary *layer in layers) {
|
|
173
|
+
[rows addObject:@{@"title": layer[@"title"], @"color": layer[@"color"],
|
|
174
|
+
@"value": MetricText(self.metric, [layer[@"values"][index] unsignedLongLongValue])}];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
NSString *total = MetricText(self.metric, (unsigned long long)[self totalAtIndex:index]);
|
|
178
|
+
|
|
179
|
+
static NSDateFormatter *formatter;
|
|
180
|
+
static dispatch_once_t once;
|
|
181
|
+
dispatch_once(&once, ^{
|
|
182
|
+
formatter = [NSDateFormatter new];
|
|
183
|
+
formatter.dateStyle = NSDateFormatterMediumStyle;
|
|
184
|
+
formatter.timeStyle = NSDateFormatterNoStyle;
|
|
185
|
+
});
|
|
186
|
+
NSString *date = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.days[index].doubleValue]];
|
|
187
|
+
|
|
188
|
+
NSDictionary *nameAttributes = @{NSFontAttributeName: [NSFont systemFontOfSize:11],
|
|
189
|
+
NSForegroundColorAttributeName: CCPetsSecondaryInkColor()};
|
|
190
|
+
NSDictionary *valueAttributes = @{NSFontAttributeName: [NSFont monospacedDigitSystemFontOfSize:11
|
|
191
|
+
weight:NSFontWeightMedium], NSForegroundColorAttributeName: CCPetsInkColor()};
|
|
192
|
+
NSDictionary *totalAttributes = @{NSFontAttributeName: [NSFont monospacedDigitSystemFontOfSize:11
|
|
193
|
+
weight:NSFontWeightSemibold], NSForegroundColorAttributeName: CCPetsInkColor()};
|
|
194
|
+
NSDictionary *dateAttributes = @{NSFontAttributeName: [NSFont systemFontOfSize:11 weight:NSFontWeightSemibold],
|
|
195
|
+
NSForegroundColorAttributeName: CCPetsCardColor()};
|
|
196
|
+
CGFloat width = [date sizeWithAttributes:dateAttributes].width + 24;
|
|
197
|
+
for (NSDictionary *row in rows) {
|
|
198
|
+
width = MAX(width, [row[@"title"] sizeWithAttributes:nameAttributes].width +
|
|
199
|
+
[row[@"value"] sizeWithAttributes:valueAttributes].width + 46);
|
|
200
|
+
}
|
|
201
|
+
NSString *totalTitle = self.singleModel ? [NSString stringWithFormat:@"%@ · %@", L(@"Total"),
|
|
202
|
+
[self metricTitle]] : L(@"Total");
|
|
203
|
+
NSDictionary *totalTitleAttributes = @{NSFontAttributeName: [NSFont systemFontOfSize:11 weight:NSFontWeightSemibold],
|
|
204
|
+
NSForegroundColorAttributeName: CCPetsInkColor()};
|
|
205
|
+
width = MAX(width, [totalTitle sizeWithAttributes:totalTitleAttributes].width +
|
|
206
|
+
[total sizeWithAttributes:totalAttributes].width + 36);
|
|
207
|
+
width = MIN(MAX(width, 150), 300);
|
|
208
|
+
const CGFloat headerHeight = 24, rowHeight = 18, padding = 6;
|
|
209
|
+
CGFloat height = headerHeight + padding + rows.count * rowHeight + 1 + rowHeight + padding;
|
|
210
|
+
|
|
211
|
+
// 放在柱子右边,放不下就放左边;竖直方向贴着绘图区顶部,超出时往下压。
|
|
212
|
+
CGFloat barCenter = NSMinX(plot) + slot * index + slot / 2;
|
|
213
|
+
CGFloat x = barCenter + slot / 2 + 8;
|
|
214
|
+
if (x + width > NSMaxX(self.bounds) - 2) x = barCenter - slot / 2 - 8 - width;
|
|
215
|
+
x = MAX(2, x);
|
|
216
|
+
CGFloat y = MAX(2, NSMaxY(plot) - height);
|
|
217
|
+
NSRect box = NSMakeRect(x, y, width, height);
|
|
218
|
+
|
|
219
|
+
[NSGraphicsContext saveGraphicsState];
|
|
220
|
+
NSShadow *shadow = [NSShadow new];
|
|
221
|
+
shadow.shadowColor = [NSColor colorWithWhite:0 alpha:0.18];
|
|
222
|
+
shadow.shadowBlurRadius = 10;
|
|
223
|
+
shadow.shadowOffset = NSMakeSize(0, -3);
|
|
224
|
+
[shadow set];
|
|
225
|
+
NSBezierPath *outline = [NSBezierPath bezierPathWithRoundedRect:box xRadius:8 yRadius:8];
|
|
226
|
+
[CCPetsCardColor() set];
|
|
227
|
+
[outline fill];
|
|
228
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
229
|
+
[CCPetsLineColor() set];
|
|
230
|
+
[outline stroke];
|
|
231
|
+
|
|
232
|
+
[NSGraphicsContext saveGraphicsState];
|
|
233
|
+
[outline addClip];
|
|
234
|
+
[CCPetsInkColor() set];
|
|
235
|
+
NSRectFill(NSMakeRect(NSMinX(box), NSMaxY(box) - headerHeight, width, headerHeight));
|
|
236
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
237
|
+
[date drawAtPoint:NSMakePoint(NSMinX(box) + 10, NSMaxY(box) - headerHeight + 5) withAttributes:dateAttributes];
|
|
238
|
+
|
|
239
|
+
CGFloat rowY = NSMaxY(box) - headerHeight - padding - rowHeight;
|
|
240
|
+
for (NSDictionary *row in rows) {
|
|
241
|
+
[(NSColor *)row[@"color"] set];
|
|
242
|
+
[[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(NSMinX(box) + 10, rowY + 3, 3, 12) xRadius:1.5 yRadius:1.5] fill];
|
|
243
|
+
NSString *title = row[@"title"];
|
|
244
|
+
CGFloat valueWidth = [row[@"value"] sizeWithAttributes:valueAttributes].width;
|
|
245
|
+
[title drawWithRect:NSMakeRect(NSMinX(box) + 19, rowY + 2, width - 19 - valueWidth - 18, 14)
|
|
246
|
+
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin
|
|
247
|
+
attributes:nameAttributes];
|
|
248
|
+
[row[@"value"] drawAtPoint:NSMakePoint(NSMaxX(box) - 10 - valueWidth, rowY + 2) withAttributes:valueAttributes];
|
|
249
|
+
rowY -= rowHeight;
|
|
250
|
+
}
|
|
251
|
+
[CCPetsLineColor() set];
|
|
252
|
+
NSRectFill(NSMakeRect(NSMinX(box) + 10, rowY + rowHeight, width - 20, 1));
|
|
253
|
+
rowY -= 1;
|
|
254
|
+
[totalTitle drawAtPoint:NSMakePoint(NSMinX(box) + 10, rowY + 2) withAttributes:totalTitleAttributes];
|
|
255
|
+
CGFloat totalWidth = [total sizeWithAttributes:totalAttributes].width;
|
|
256
|
+
[total drawAtPoint:NSMakePoint(NSMaxX(box) - 10 - totalWidth, rowY + 2) withAttributes:totalAttributes];
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
- (NSString *)metricTitle {
|
|
260
|
+
return MetricTitle(self.metric);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
- (NSInteger)indexForEvent:(NSEvent *)event {
|
|
264
|
+
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
|
|
265
|
+
NSRect plot = [self plotRect];
|
|
266
|
+
if (self.days.count == 0 || !NSPointInRect(point, NSInsetRect(plot, 0, -ChartTop))) return -1;
|
|
267
|
+
NSInteger index = (NSInteger)floor((point.x - NSMinX(plot)) / (plot.size.width / self.days.count));
|
|
268
|
+
return index >= 0 && index < (NSInteger)self.days.count ? index : -1;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
- (void)mouseMoved:(NSEvent *)event {
|
|
272
|
+
NSInteger index = [self indexForEvent:event];
|
|
273
|
+
if (index == self.hoverIndex) return;
|
|
274
|
+
self.hoverIndex = index;
|
|
275
|
+
self.needsDisplay = YES;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
- (void)mouseExited:(NSEvent *)event {
|
|
279
|
+
if (self.hoverIndex < 0) return;
|
|
280
|
+
self.hoverIndex = -1;
|
|
281
|
+
self.needsDisplay = YES;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
@end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 主窗口的「用量」页:本机 Claude Code / Codex 的 Token 用量,按模型分色堆叠的每日柱状图 + 模型表。
|
|
4
|
+
//
|
|
5
|
+
// 数据来自 CCPetsModelUsage(本机会话日志,不联网),由 CCPetsUsageStore 在后台按最长的 90 天算一次,
|
|
6
|
+
// 切换来源、区间、指标、模型都只在本地截取,不重算。页面显示期间每分钟问一次 loadIfStale,
|
|
7
|
+
// 结果过期才在后台重算。图表自绘,不用 Swift Charts。
|
|
8
|
+
@interface CCPetsUsagePage : NSViewController
|
|
9
|
+
@end
|
|
10
|
+
|
|
11
|
+
// 90 天的模型用量,用量页和概览页共用一份。数据层按请求的起点清理缓存,所以统一按最长窗口取,
|
|
12
|
+
// 两页各取各的窗口会让较早的会话缓存被删了又重读。
|
|
13
|
+
//
|
|
14
|
+
// results:provider(claude / codex)-> CCPetsModelUsage 的返回值;还没算完时为 nil。
|
|
15
|
+
extern NSNotificationName const CCPetsUsageStoreDidChangeNotification;
|
|
16
|
+
@interface CCPetsUsageStore : NSObject
|
|
17
|
+
@property(class, nonatomic, readonly) NSDictionary<NSString *, NSDictionary *> *results;
|
|
18
|
+
// profileId → 独立模型用量;providerResults 仍保留按 Provider 汇总,兼容概览页。
|
|
19
|
+
@property(class, nonatomic, readonly) NSDictionary<NSString *, NSDictionary *> *profileResults;
|
|
20
|
+
@property(class, nonatomic, readonly) BOOL loading;
|
|
21
|
+
// 没算过、结果超过两分钟、或者跨过了午夜就重算;正在算时不重复发起。算完发 CCPetsUsageStoreDidChangeNotification。
|
|
22
|
+
+ (void)loadIfStale;
|
|
23
|
+
@end
|
|
24
|
+
|
|
25
|
+
// 1234 -> 1.23K,1234567 -> 1.23M。
|
|
26
|
+
NSString *CCPetsFormatTokens(unsigned long long value);
|
|
27
|
+
// 一组按天的 { timestamp, models: { model: counts } } 里,所有模型某天的合计。
|
|
28
|
+
NSDictionary *CCPetsUsageDayTotals(NSDictionary *day);
|
|
29
|
+
// 若干份 counts 逐栏相加。
|
|
30
|
+
NSDictionary *CCPetsUsageSum(NSArray<NSDictionary *> *list);
|
|
31
|
+
// 指标条的五格(总用量、输入、输出、缓存输入、请求),给 CCPetsMetricRow 用。概览和用量页同一套口径和写法。
|
|
32
|
+
NSArray<NSArray<NSString *> *> *CCPetsUsageMetricItems(NSString *totalTitle, NSDictionary *sum, NSUInteger modelCount);
|