cc-pets 2.1.1 → 2.1.2
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 -0
- package/CC_BRIDGE.zh-CN.md +4 -0
- package/CHANGELOG.md +23 -1
- package/CHANGELOG.zh-CN.md +23 -1
- package/README.md +50 -12
- package/README.zh-CN.md +49 -13
- package/Sources/CCPets/CCPetsAppDelegate+AgentEvents.m +215 -0
- package/Sources/CCPets/CCPetsAppDelegate+AgentStatus.m +660 -0
- package/Sources/CCPets/CCPetsAppDelegate+Bridge.m +297 -0
- package/Sources/CCPets/CCPetsAppDelegate+Pets.m +187 -0
- package/Sources/CCPets/CCPetsAppDelegate+Private.h +322 -0
- package/Sources/CCPets/CCPetsAppDelegate+Quota.m +216 -0
- package/Sources/CCPets/CCPetsAppDelegate+Settings.m +205 -0
- package/Sources/CCPets/CCPetsAppDelegate+Speech.m +392 -0
- package/Sources/CCPets/CCPetsAppDelegate+Update.m +458 -0
- package/Sources/CCPets/CCPetsAppDelegate.h +13 -14
- package/Sources/CCPets/CCPetsAppDelegate.m +49 -2453
- package/Sources/CCPets/CCPetsCleanup.h +4 -0
- package/Sources/CCPets/CCPetsCleanup.m +32 -1
- package/Sources/CCPets/CCPetsPaths.h +0 -1
- package/Sources/CCPets/CCPetsPaths.m +0 -1
- package/Sources/CCPets/CCPetsUsage.h +0 -1
- package/Sources/CCPets/CCPetsUsage.m +0 -3
- package/Sources/CCPets/CCPetsVersion.h +7 -0
- package/Sources/CCPets/CCPetsVersion.m +82 -0
- package/Sources/CCPets/PetView.h +2 -0
- package/Sources/CCPets/PetView.m +16 -4
- package/Sources/CCPets/QuotaDashboardView.m +0 -3
- package/Sources/CCPets/main.m +13 -0
- package/bin/cc-pets +7 -1
- package/package.json +3 -2
- package/scripts/bridge/cli.mjs +9 -2
- package/scripts/bridge/core.mjs +1 -1
- package/scripts/bridge/install.mjs +23 -1
- package/scripts/bridge/process.mjs +22 -0
- package/scripts/doctor.mjs +456 -0
- package/scripts/install-claude-hooks.mjs +12 -18
- package/scripts/install-codex-hooks.mjs +3 -9
- package/scripts/integration-markers.mjs +51 -0
- package/scripts/pet-store.mjs +2 -1
- package/scripts/uninstall-integrations.mjs +16 -44
- package/scripts/uninstall-shell-integration.sh +3 -3
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// AppDelegate 按功能拆成多个 category(CCPetsAppDelegate+<模块>.m),这个头文件是它们之间的私有约定:
|
|
2
|
+
// 共享常量、共用的小控件类,以及跨文件调用的方法声明。只给 AppDelegate 自己的实现文件 import。
|
|
3
|
+
#import "CCPetsAppDelegate.h"
|
|
4
|
+
#import "CCPetsPaths.h"
|
|
5
|
+
#import "CCPetsVersion.h"
|
|
6
|
+
#import "CCPetsEvents.h"
|
|
7
|
+
#import "CCPetsQuotaHistory.h"
|
|
8
|
+
#import "CCPetsImageLoader.h"
|
|
9
|
+
#import "CCPetsPhrases.h"
|
|
10
|
+
#import "CCPetsPhrasesEditor.h"
|
|
11
|
+
#import "CCPetsUsage.h"
|
|
12
|
+
#import "CCPetsTerminalFocus.h"
|
|
13
|
+
#import "MenuChoiceRow.h"
|
|
14
|
+
#import "CCPetsGlassMenu.h"
|
|
15
|
+
#import "CCPetsBridge.h"
|
|
16
|
+
#import "MenuToggleSwitch.h"
|
|
17
|
+
#import <UserNotifications/UserNotifications.h>
|
|
18
|
+
#import <signal.h>
|
|
19
|
+
#import <sys/stat.h>
|
|
20
|
+
#import <fcntl.h>
|
|
21
|
+
#import <unistd.h>
|
|
22
|
+
#import <errno.h>
|
|
23
|
+
#import <sys/sysctl.h>
|
|
24
|
+
#import <stdlib.h>
|
|
25
|
+
|
|
26
|
+
static const NSTimeInterval PendingApprovalTTL = 24 * 60 * 60;
|
|
27
|
+
static const NSUInteger PendingApprovalLimit = 100;
|
|
28
|
+
static const NSUInteger AgentSessionRecordLimit = 20;
|
|
29
|
+
static const NSUInteger AgentSessionMenuLimit = 8;
|
|
30
|
+
static const CGFloat PetApprovalBadgeSize = 17.0;
|
|
31
|
+
static const CGFloat PetUpdateBadgeSize = 24.0;
|
|
32
|
+
// CC Bridge:轮询间隔、"最近消息"的时间窗、菜单里最多列几条。
|
|
33
|
+
static const NSTimeInterval BridgeRefreshInterval = 3.0;
|
|
34
|
+
static const NSTimeInterval BridgeRecentWindow = 30 * 60;
|
|
35
|
+
static const NSUInteger BridgeMenuDeliveryLimit = 5;
|
|
36
|
+
static const unsigned long long UpdateLogSizeLimit = 1024 * 1024;
|
|
37
|
+
static NSString *const CCPetsRepositorySlug = @"Sunnyshinnny776/cc-pets";
|
|
38
|
+
// 更新弹窗里最多列几条说明、每条最多几个字符;多出来的条目用一行「…」代替。
|
|
39
|
+
static const NSUInteger UpdateHighlightLimit = 3;
|
|
40
|
+
static const NSUInteger UpdateHighlightMaxLength = 50;
|
|
41
|
+
// CLI 每开一个终端都会 open 一次桌宠,这个间隔内不重复联网检查。
|
|
42
|
+
static const NSTimeInterval UpdateSilentCheckInterval = 10 * 60;
|
|
43
|
+
// 启动时的更新气泡比普通碎碎念停得久,给用户留出点它的时间。
|
|
44
|
+
static const NSTimeInterval UpdateBubbleDwell = 30.0;
|
|
45
|
+
// 碎碎念开着时,每次够条件说话有一半机会换成更新提醒;提醒气泡停得比闲话久一点,来得及点。
|
|
46
|
+
static const uint32_t UpdateReminderSharePercent = 50;
|
|
47
|
+
static const NSTimeInterval UpdateReminderDwell = 8.0;
|
|
48
|
+
static NSString *const PetInteractionPhrasesV1MigratedKey =
|
|
49
|
+
@"CCPetsInteractionPhrasesV1Migrated";
|
|
50
|
+
|
|
51
|
+
// 全量聚合是同步的主线程活儿(本机实测数十毫秒,会话目录越大越久),而面板恰好由"鼠标
|
|
52
|
+
// 停在口袋上"触发——那正是用户可能马上按下并拖动的时刻。事件被这几十毫秒挡住,mouseDown
|
|
53
|
+
// 和 mouseDragged 就会成批迟到,手感上像是"鼠标走远了宠物才跟上"。
|
|
54
|
+
// 因此:左键按着的时候不刷(拖动期间没人看数字),刚刷过的也不重刷(进出热区会反复触发)。
|
|
55
|
+
static const NSTimeInterval UsageRefreshCoalesceWindow = 2.0;
|
|
56
|
+
// 气泡停留时长。
|
|
57
|
+
static const NSTimeInterval PetSpeechDwell = 4.5;
|
|
58
|
+
// 气泡与状态卡的本体高度。
|
|
59
|
+
//
|
|
60
|
+
// 不要再加尾巴(三角、锥形、思考圆点都试过):药丸的圆角等于高度一半,整条边都是弧,
|
|
61
|
+
// 尾巴只能从弧上长出来,怎么调都像贴上去的;换成圆角矩形能配尾巴,但那要放弃
|
|
62
|
+
// layer.cornerRadius + kCACornerCurveContinuous 改用位图 maskImage 裁形,
|
|
63
|
+
// squircle 和 GPU 端矢量裁切的质感一起丢了,得不偿失。
|
|
64
|
+
// 结论:保持药丸 + 原生圆角,靠贴近宠物来表达归属。
|
|
65
|
+
static const CGFloat PetSpeechBodyHeight = 38.0;
|
|
66
|
+
static const CGFloat PetStatusBodyHeight = 58.0;
|
|
67
|
+
// 副行没东西可显示时的高度,只放得下标题一行。
|
|
68
|
+
static const CGFloat PetStatusSingleLineHeight = 40.0;
|
|
69
|
+
|
|
70
|
+
// 碎碎念的话痨程度。四道闸(每小时预算 / 两句间隔 / 静默门槛 / 出话概率)本来是
|
|
71
|
+
// 四个互相牵制的常数,单独调任何一个都不会真的变频繁——比如把冷却调到 0,
|
|
72
|
+
// 每小时 4 句的预算照样卡死。所以对用户只暴露一个档位,四个值一起走。
|
|
73
|
+
//
|
|
74
|
+
// normal 档就是改造前的原值,默认不变。
|
|
75
|
+
typedef struct {
|
|
76
|
+
NSInteger hourlyBudget;
|
|
77
|
+
NSTimeInterval cooldown;
|
|
78
|
+
NSTimeInterval quietSeconds; // 距上一次 agent 事件多久才算"没人打扰"
|
|
79
|
+
uint32_t idleChancePercent; // 每次判定(≤30 秒一次)的出话概率
|
|
80
|
+
} PetSpeechRate;
|
|
81
|
+
|
|
82
|
+
static const PetSpeechRate PetSpeechRateLow = {2, 480.0, 300.0, 15};
|
|
83
|
+
static const PetSpeechRate PetSpeechRateNormal = {4, 240.0, 180.0, 25};
|
|
84
|
+
static const PetSpeechRate PetSpeechRateHigh = {8, 120.0, 90.0, 45};
|
|
85
|
+
static const PetSpeechRate PetSpeechRateChatty = {20, 45.0, 45.0, 70};
|
|
86
|
+
|
|
87
|
+
// 档位写进 defaults 的值。字符串而不是整数:以后加档、调顺序都不会让老配置错位。
|
|
88
|
+
extern NSString *const PetSpeechFrequencyKey;
|
|
89
|
+
static NSString *const PetSpeechFrequencyLow = @"low";
|
|
90
|
+
static NSString *const PetSpeechFrequencyNormal = @"normal";
|
|
91
|
+
static NSString *const PetSpeechFrequencyHigh = @"high";
|
|
92
|
+
static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
93
|
+
|
|
94
|
+
// 卡片和气泡都按文案实际宽度伸缩。固定宽度下"收工!"和"到处翻资料呢。"占一样长的条,
|
|
95
|
+
// 前者会拖着一大截空白,看着像没加载完。
|
|
96
|
+
// 用控件自己的 fittingSize 量,不要用 sizeWithAttributes:。
|
|
97
|
+
// NSTextField 的 cell 有一点内部留白,纯按字符串量出来的宽度会比控件真正需要的小几个点,
|
|
98
|
+
// 结果就是明明算着"刚好够",显示出来最后一两个字变成省略号。
|
|
99
|
+
static inline CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
100
|
+
if (label.stringValue.length == 0) return 0;
|
|
101
|
+
return ceil(label.fittingSize.width) + 2;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@interface CCPetsStatusClickButton : NSButton
|
|
105
|
+
@end
|
|
106
|
+
|
|
107
|
+
// 待审批角标。等审批的会话按定义是停住不动的,它的时间戳只会越来越旧——状态卡
|
|
108
|
+
// 正文永远显示最新事件,会话列表又按时间倒序,两边都会把最该处理的那条推到看
|
|
109
|
+
// 不见的地方。角标把这个数字单独拎出来常驻。
|
|
110
|
+
@interface CCPetsApprovalBadgeView : NSView
|
|
111
|
+
@property(nonatomic) NSUInteger count;
|
|
112
|
+
// 默认审批红;CC Bridge 消息角标复用同一个视图,换成蓝色 / 橙色。
|
|
113
|
+
@property(nonatomic) NSColor *fillColor;
|
|
114
|
+
@end
|
|
115
|
+
|
|
116
|
+
@interface AppDelegate ()
|
|
117
|
+
- (NSString *)applicationSupportDirectory;
|
|
118
|
+
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message;
|
|
119
|
+
- (void)installEditMenu;
|
|
120
|
+
- (void)applicationDidFinishLaunching:(NSNotification *)notification;
|
|
121
|
+
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag;
|
|
122
|
+
- (void)screensDidSleep:(NSNotification *)notification;
|
|
123
|
+
- (void)screensDidWake:(NSNotification *)notification;
|
|
124
|
+
- (void)petWindowDidMove:(NSNotification *)notification;
|
|
125
|
+
- (void)applicationWillTerminate:(NSNotification *)notification;
|
|
126
|
+
@end
|
|
127
|
+
|
|
128
|
+
// 右键菜单里的各项设置开关
|
|
129
|
+
@interface AppDelegate (Settings)
|
|
130
|
+
- (void)toggleSystemMetric:(NSButton *)sender;
|
|
131
|
+
- (void)setUsageDisplayModeFromControl:(NSButton *)sender;
|
|
132
|
+
- (NSString *)systemMetricKeyForTag:(NSInteger)tag;
|
|
133
|
+
- (void)applySystemMetricPreferences;
|
|
134
|
+
- (void)applyUsageDisplayModePreferences;
|
|
135
|
+
- (void)toggleImportCodexPets:(NSButton *)sender;
|
|
136
|
+
- (void)showImportedCodexPetsAlert:(NSUInteger)imported;
|
|
137
|
+
- (NSString *)notificationKeyForTag:(NSInteger)tag;
|
|
138
|
+
- (void)setPanelTheme:(MenuChoiceRowView *)sender;
|
|
139
|
+
- (void)applyBubbleTextStyle;
|
|
140
|
+
- (void)setGlassDimLevel:(MenuChoiceRowView *)sender;
|
|
141
|
+
- (void)toggleSpeech:(NSButton *)sender;
|
|
142
|
+
- (void)togglePetInteraction:(NSButton *)sender;
|
|
143
|
+
- (void)setPetInteractionHeartThreshold:(NSMenuItem *)sender;
|
|
144
|
+
- (void)setPetInteractionAnnoyedThreshold:(NSMenuItem *)sender;
|
|
145
|
+
- (void)setPetInteractionInterval:(NSMenuItem *)sender;
|
|
146
|
+
- (void)setSpeechFrequency:(NSMenuItem *)sender;
|
|
147
|
+
- (void)editPhrasesFile:(id)sender;
|
|
148
|
+
- (void)toggleNotification:(NSButton *)sender;
|
|
149
|
+
@end
|
|
150
|
+
|
|
151
|
+
// Agent 状态卡、会话列表、审批与卡住提醒、终端回跳、系统通知
|
|
152
|
+
@interface AppDelegate (AgentStatus)
|
|
153
|
+
- (void)positionAgentStatus;
|
|
154
|
+
- (void)showAgentStatusForRecord:(NSDictionary *)record;
|
|
155
|
+
- (void)prunePendingApprovalRecords;
|
|
156
|
+
- (void)toggleStatusBubbleFromMenu:(NSButton *)sender;
|
|
157
|
+
- (void)sendNotificationWithTitle:(NSString *)title body:(NSString *)body;
|
|
158
|
+
- (void)notifyForRecord:(NSDictionary *)record;
|
|
159
|
+
- (NSString *)statusTextForState:(NSString *)state tool:(NSString *)tool;
|
|
160
|
+
- (NSString *)petVoiceTagForState:(NSString *)state tool:(NSString *)tool;
|
|
161
|
+
- (NSString *)statusDetailForState:(NSString *)state tool:(NSString *)tool;
|
|
162
|
+
- (NSColor *)statusColorForState:(NSString *)state;
|
|
163
|
+
- (NSString *)statusSymbolForState:(NSString *)state;
|
|
164
|
+
- (void)hideAgentStatusIfClientGone;
|
|
165
|
+
- (void)hideAgentStatus;
|
|
166
|
+
- (void)presentPendingApprovalRecord:(NSDictionary *)record;
|
|
167
|
+
- (BOOL)isTrailingRecord:(NSDictionary *)record afterState:(NSString *)state;
|
|
168
|
+
- (void)applyStatusPresentationForState:(NSString *)state provider:(NSString *)provider
|
|
169
|
+
tool:(NSString *)tool;
|
|
170
|
+
- (void)enterIdleStatus;
|
|
171
|
+
- (void)showAgentStatusForRecord:(NSDictionary *)record notify:(BOOL)shouldNotify;
|
|
172
|
+
- (NSString *)approvalKeyForRecord:(NSDictionary *)record;
|
|
173
|
+
- (void)displayAgentRecord:(NSDictionary *)record notify:(BOOL)shouldNotify;
|
|
174
|
+
- (NSString *)agentSessionKeyForRecord:(NSDictionary *)record;
|
|
175
|
+
- (NSString *)onlineAgentSessionKeyForProvider:(NSString *)provider tty:(NSString *)tty;
|
|
176
|
+
- (NSString *)onlineAgentSessionKeyForRecord:(NSDictionary *)record;
|
|
177
|
+
- (BOOL)isAgentSessionRecordLive:(NSDictionary *)record;
|
|
178
|
+
- (void)pruneOfflineAgentSessionRecords;
|
|
179
|
+
- (void)trackAgentSessionRecord:(NSDictionary *)record;
|
|
180
|
+
- (BOOL)isApprovalSessionRecord:(NSDictionary *)record;
|
|
181
|
+
- (NSArray<NSDictionary *> *)pendingApprovalSessionRecords;
|
|
182
|
+
- (NSTimeInterval)stallIntervalForState:(NSString *)state;
|
|
183
|
+
- (void)checkStalledAgentSessions;
|
|
184
|
+
- (void)presentStalledRecord:(NSDictionary *)record;
|
|
185
|
+
- (void)layoutApprovalBadge;
|
|
186
|
+
- (void)refreshApprovalBadge;
|
|
187
|
+
- (NSArray<NSDictionary *> *)recentAgentSessionRecords;
|
|
188
|
+
- (NSString *)terminalNameForTarget:(NSDictionary *)target;
|
|
189
|
+
- (void)focusAgentSessionRecord:(NSMenuItem *)sender;
|
|
190
|
+
- (void)showAgentSessionsMenu:(NSButton *)sender;
|
|
191
|
+
- (void)popUpAgentSessionsMenu:(NSMenu *)menu from:(NSButton *)sender;
|
|
192
|
+
- (BOOL)focusLatestAgentTerminal;
|
|
193
|
+
- (void)focusLatestAgentTerminal:(id)sender;
|
|
194
|
+
- (void)resizeStatusCardToFitText;
|
|
195
|
+
@end
|
|
196
|
+
|
|
197
|
+
// CC Bridge 开关、消息角标与菜单
|
|
198
|
+
@interface AppDelegate (Bridge)
|
|
199
|
+
- (void)runBridgeCommand:(NSArray<NSString *> *)arguments sender:(NSButton *)sender
|
|
200
|
+
successMessage:(NSString *)successMessage;
|
|
201
|
+
- (BOOL)currentBridgeStateForSwitch:(NSButton *)sender;
|
|
202
|
+
- (void)syncBridgeSwitch:(NSButton *)sender;
|
|
203
|
+
- (BOOL)targetBridgeStateForSwitch:(NSButton *)sender;
|
|
204
|
+
- (NSString *)bridgeApprovalGroupForTag:(NSInteger)tag;
|
|
205
|
+
- (void)toggleBridgeEnabled:(NSButton *)sender;
|
|
206
|
+
- (void)toggleBridgeApproval:(NSButton *)sender;
|
|
207
|
+
- (void)toggleBridgeWake:(NSButton *)sender;
|
|
208
|
+
- (void)toggleBridgeEditGuard:(NSButton *)sender;
|
|
209
|
+
- (void)toggleBridgeBadge:(NSButton *)sender;
|
|
210
|
+
- (void)toggleBridgeNotification:(NSButton *)sender;
|
|
211
|
+
- (void)refreshBridgeState:(NSTimer *)timer;
|
|
212
|
+
- (void)notifyNewBridgeDeliveries;
|
|
213
|
+
- (void)refreshBridgeBadge;
|
|
214
|
+
- (NSString *)bridgeSessionIdForRecord:(NSDictionary *)record;
|
|
215
|
+
- (NSDictionary *)bridgeSessionEntryForRecord:(NSDictionary *)record;
|
|
216
|
+
- (NSDictionary *)terminalTargetForBridgeSession:(NSString *)sessionId;
|
|
217
|
+
- (void)focusBridgeSession:(NSMenuItem *)sender;
|
|
218
|
+
- (void)addBridgeItemsToMenu:(NSMenu *)menu deliveries:(NSArray<NSDictionary *> *)deliveries
|
|
219
|
+
pending:(NSDictionary<NSString *, NSNumber *> *)pending;
|
|
220
|
+
@end
|
|
221
|
+
|
|
222
|
+
// 宠物素材发现、切换与删除
|
|
223
|
+
@interface AppDelegate (Pets)
|
|
224
|
+
- (void)switchPetToID:(NSString *)petID;
|
|
225
|
+
- (BOOL)deletePetWithID:(NSString *)petID;
|
|
226
|
+
- (NSDictionary *)petManifestInDirectory:(NSString *)directory;
|
|
227
|
+
- (NSInteger)spriteVersionInDirectory:(NSString *)directory fallbackDirectory:(NSString *)fallbackDirectory;
|
|
228
|
+
- (NSInteger)spriteRowCountForVersion:(NSInteger)version;
|
|
229
|
+
- (NSString *)spritePathInDirectory:(NSString *)directory;
|
|
230
|
+
- (NSArray<NSString *> *)petDirectoryNamesAtPath:(NSString *)root;
|
|
231
|
+
- (NSArray<NSDictionary *> *)discoverPetOptions;
|
|
232
|
+
- (NSDate *)petOptionsStamp;
|
|
233
|
+
- (void)invalidatePetOptionsCache;
|
|
234
|
+
- (NSArray<NSDictionary *> *)petOptions;
|
|
235
|
+
- (NSDictionary *)petOptionWithID:(NSString *)petID inOptions:(NSArray<NSDictionary *> *)options;
|
|
236
|
+
- (NSArray<NSDictionary *> *)waitForPetOptions;
|
|
237
|
+
@end
|
|
238
|
+
|
|
239
|
+
// 额度面板、用量刷新与系统指标
|
|
240
|
+
@interface AppDelegate (Quota)
|
|
241
|
+
- (void)showQuotaDashboard;
|
|
242
|
+
- (void)positionQuotaDashboard;
|
|
243
|
+
- (void)scheduleQuotaDashboardHide;
|
|
244
|
+
- (void)hideQuotaDashboardIfNeeded;
|
|
245
|
+
- (void)refreshUsage:(id)sender;
|
|
246
|
+
- (void)applyCodexUsage:(NSDictionary *)codexUsage claudeUsage:(NSDictionary *)claudeUsage;
|
|
247
|
+
- (void)rescheduleUsageTimer;
|
|
248
|
+
- (BOOL)shouldRefreshUsageForDashboard;
|
|
249
|
+
- (BOOL)hasEnabledSystemMetric;
|
|
250
|
+
- (void)refreshSystemMetrics:(id)sender;
|
|
251
|
+
- (void)updateSystemMetricsTimer;
|
|
252
|
+
- (void)startQuotaClock;
|
|
253
|
+
- (void)tickQuotaClock:(id)sender;
|
|
254
|
+
- (void)updateQuotaLiveState;
|
|
255
|
+
- (void)resizeQuotaDashboard;
|
|
256
|
+
- (void)refreshDetectedProviders;
|
|
257
|
+
@end
|
|
258
|
+
|
|
259
|
+
// agent 事件流读取与客户端存活检测
|
|
260
|
+
@interface AppDelegate (AgentEvents)
|
|
261
|
+
- (void)prepareAgentEventReader;
|
|
262
|
+
- (void)consumeAgentEventData:(NSData *)data;
|
|
263
|
+
- (void)processAgentEventData:(NSData *)data ignoreFirstPartial:(BOOL)ignoreFirstPartial recentOnly:(BOOL)recentOnly;
|
|
264
|
+
- (void)readNewAgentEvents;
|
|
265
|
+
- (void)startAgentEventReader;
|
|
266
|
+
- (void)ensureAgentEventReader:(id)sender;
|
|
267
|
+
- (void)refreshClientLifecycle:(id)sender;
|
|
268
|
+
@end
|
|
269
|
+
|
|
270
|
+
// 说话气泡与碎碎念
|
|
271
|
+
@interface AppDelegate (Speech)
|
|
272
|
+
- (BOOL)speechEnabled;
|
|
273
|
+
- (PetSpeechRate)speechRate;
|
|
274
|
+
- (NSTimeInterval)speechCooldownSeconds;
|
|
275
|
+
- (NSInteger)speechHourlyBudget;
|
|
276
|
+
- (BOOL)agentBusyForSpeech;
|
|
277
|
+
- (BOOL)canSpeakNow;
|
|
278
|
+
- (NSDictionary<NSString *, NSString *> *)speechSlots;
|
|
279
|
+
- (NSDictionary<NSString *, NSString *> *)speechSlotsWithTool:(NSString *)tool;
|
|
280
|
+
- (NSInteger)remainingFiveHourQuotaPercent;
|
|
281
|
+
- (NSString *)fiveHourResetTimeText;
|
|
282
|
+
- (void)considerQuotaSpeech;
|
|
283
|
+
- (void)buildSpeechPanelIfNeeded;
|
|
284
|
+
- (void)positionSpeechPanel;
|
|
285
|
+
- (void)speakWithTag:(NSString *)tag;
|
|
286
|
+
- (void)presentSpeechText:(NSString *)text;
|
|
287
|
+
- (void)restoreStatusDetail;
|
|
288
|
+
- (void)showSpeechBubbleWithText:(NSString *)text;
|
|
289
|
+
- (void)showSpeechBubbleWithText:(NSString *)text dwell:(NSTimeInterval)dwell;
|
|
290
|
+
- (void)consumeSpeechDebugTrigger;
|
|
291
|
+
- (void)hideSpeechBubble;
|
|
292
|
+
- (void)considerIdleSpeech;
|
|
293
|
+
- (void)considerSpeechForRecord:(NSDictionary *)record;
|
|
294
|
+
- (void)resizeSpeechBubbleToFitText;
|
|
295
|
+
@end
|
|
296
|
+
|
|
297
|
+
// 检查更新、自动更新、更新气泡 / 角标 / 弹窗,以及「关于」
|
|
298
|
+
@interface AppDelegate (Update)
|
|
299
|
+
- (NSDictionary *)updaterConfiguration;
|
|
300
|
+
- (BOOL)restartAfterUpdateToVersion:(NSString *)version configuration:(NSDictionary *)configuration;
|
|
301
|
+
- (void)startUpdateToVersion:(NSString *)version;
|
|
302
|
+
- (void)retryUpdate:(NSArray *)context;
|
|
303
|
+
- (void)startUpdateToVersion:(NSString *)version attempt:(NSInteger)attempt;
|
|
304
|
+
- (void)showAboutPanel:(id)sender;
|
|
305
|
+
- (void)fetchLatestReleaseWithCompletion:(void (^)(NSString *version, NSArray<NSString *> *highlights,
|
|
306
|
+
BOOL highlightsTruncated, NSString *errorMessage))completion;
|
|
307
|
+
- (void)checkForUpdates:(id)sender;
|
|
308
|
+
- (void)showPendingUpdate:(id)sender;
|
|
309
|
+
- (void)silentCheckForUpdate;
|
|
310
|
+
- (void)rememberPendingUpdate:(NSString *)version highlights:(NSArray<NSString *> *)highlights
|
|
311
|
+
truncated:(BOOL)truncated;
|
|
312
|
+
- (void)refreshUpdateBadge;
|
|
313
|
+
- (void)clearPendingUpdate;
|
|
314
|
+
- (NSView *)updateHighlightsAccessoryView;
|
|
315
|
+
- (void)showUpdateDialog;
|
|
316
|
+
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
|
|
317
|
+
- (void)applyUpdateBadgeStyle;
|
|
318
|
+
- (void)showUpdateBubble;
|
|
319
|
+
- (NSString *)updateReminderText;
|
|
320
|
+
- (void)showUpdateBubbleWithText:(NSString *)text dwell:(NSTimeInterval)dwell;
|
|
321
|
+
- (void)updateBubbleClicked:(id)sender;
|
|
322
|
+
@end
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// 额度面板、用量刷新与系统指标。
|
|
2
|
+
#import "CCPetsAppDelegate+Private.h"
|
|
3
|
+
|
|
4
|
+
// 额度字典"非空"不等于"这家真的在用"。Claude 的 reader 刻意永不返回 nil(见
|
|
5
|
+
// CCPetsUsage.m 里 -[ClaudeUsageReader refresh] 的注释:Token 是本机从转录数出来的,
|
|
6
|
+
// 不能被缺失的官方额度一票否决),什么都没有时照样返回
|
|
7
|
+
// {fiveHour: NSNull, week: NSNull, tokenUsage: 全 0},count 恒为 3。
|
|
8
|
+
// 拿 count > 0 当证据,Claude 就永远算"检测到",卡片再也去不掉。这里只认实质证据:
|
|
9
|
+
// 官方额度块真实存在,或者本机统计出过非零 Token。Codex 侧同一套判据也成立。
|
|
10
|
+
static BOOL UsageShowsProviderInUse(NSDictionary *usage) {
|
|
11
|
+
if (usage.count == 0) return NO;
|
|
12
|
+
for (NSString *key in @[@"fiveHour", @"week"]) {
|
|
13
|
+
if ([usage[key] isKindOfClass:NSDictionary.class]) return YES;
|
|
14
|
+
}
|
|
15
|
+
NSDictionary *tokenUsage = [usage[@"tokenUsage"] isKindOfClass:NSDictionary.class]
|
|
16
|
+
? usage[@"tokenUsage"] : nil;
|
|
17
|
+
for (NSString *key in @[@"fiveHour", @"week", @"today", @"recentWeek"]) {
|
|
18
|
+
NSDictionary *totals = [tokenUsage[key] isKindOfClass:NSDictionary.class]
|
|
19
|
+
? tokenUsage[key] : nil;
|
|
20
|
+
if ([totals[@"total_tokens"] doubleValue] > 0) return YES;
|
|
21
|
+
}
|
|
22
|
+
return NO;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@implementation AppDelegate (Quota)
|
|
26
|
+
- (void)rescheduleUsageTimer {
|
|
27
|
+
NSTimeInterval interval = self.quotaPanel.isVisible
|
|
28
|
+
? UsageRefreshIntervalVisible : UsageRefreshIntervalHidden;
|
|
29
|
+
if (self.usageTimer.isValid && self.usageTimer.timeInterval == interval) return;
|
|
30
|
+
[self.usageTimer invalidate];
|
|
31
|
+
self.usageTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:self
|
|
32
|
+
selector:@selector(refreshUsage:) userInfo:nil repeats:YES];
|
|
33
|
+
self.usageTimer.tolerance = interval * 0.2;
|
|
34
|
+
}
|
|
35
|
+
- (BOOL)shouldRefreshUsageForDashboard {
|
|
36
|
+
if (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft)) {
|
|
37
|
+
return NO;
|
|
38
|
+
}
|
|
39
|
+
return NSDate.date.timeIntervalSince1970 - self.lastUsageRefreshAt >= UsageRefreshCoalesceWindow;
|
|
40
|
+
}
|
|
41
|
+
- (void)showQuotaDashboard {
|
|
42
|
+
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideQuotaDashboardIfNeeded) object:nil];
|
|
43
|
+
if ([self shouldRefreshUsageForDashboard]) [self refreshUsage:nil];
|
|
44
|
+
[self positionQuotaDashboard];
|
|
45
|
+
[self.quotaPanel orderFrontRegardless];
|
|
46
|
+
[self.panel orderFrontRegardless];
|
|
47
|
+
[self rescheduleUsageTimer];
|
|
48
|
+
[self startQuotaClock];
|
|
49
|
+
[self updateSystemMetricsTimer];
|
|
50
|
+
}
|
|
51
|
+
- (BOOL)hasEnabledSystemMetric {
|
|
52
|
+
return self.quotaView.systemCPUEnabled || self.quotaView.systemTemperatureEnabled ||
|
|
53
|
+
self.quotaView.systemMemoryEnabled;
|
|
54
|
+
}
|
|
55
|
+
- (void)refreshSystemMetrics:(id)sender {
|
|
56
|
+
if (![self hasEnabledSystemMetric]) return;
|
|
57
|
+
BOOL cpuEnabled = self.quotaView.systemCPUEnabled;
|
|
58
|
+
BOOL memoryEnabled = self.quotaView.systemMemoryEnabled;
|
|
59
|
+
BOOL temperatureEnabled = self.quotaView.systemTemperatureEnabled;
|
|
60
|
+
__weak typeof(self) weakSelf = self;
|
|
61
|
+
[self.systemMonitor sampleCPU:cpuEnabled memory:memoryEnabled
|
|
62
|
+
temperature:temperatureEnabled completion:^(NSNumber *cpu, NSNumber *memory,
|
|
63
|
+
NSNumber *temperature) {
|
|
64
|
+
typeof(self) strongSelf = weakSelf;
|
|
65
|
+
if (!strongSelf) return;
|
|
66
|
+
if (cpuEnabled) strongSelf.quotaView.systemCPUPercent = cpu;
|
|
67
|
+
if (memoryEnabled) strongSelf.quotaView.systemMemoryPercent = memory;
|
|
68
|
+
if (temperatureEnabled) strongSelf.quotaView.systemTemperatureCelsius = temperature;
|
|
69
|
+
strongSelf.quotaView.needsDisplay = YES;
|
|
70
|
+
}];
|
|
71
|
+
}
|
|
72
|
+
- (void)updateSystemMetricsTimer {
|
|
73
|
+
BOOL shouldRun = self.quotaPanel.isVisible && [self hasEnabledSystemMetric];
|
|
74
|
+
if (!shouldRun) {
|
|
75
|
+
[self.systemMetricsTimer invalidate];
|
|
76
|
+
self.systemMetricsTimer = nil;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (self.systemMetricsTimer.isValid) return;
|
|
80
|
+
[self refreshSystemMetrics:nil];
|
|
81
|
+
__weak typeof(self) weakSelf = self;
|
|
82
|
+
self.systemMetricsTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES
|
|
83
|
+
block:^(NSTimer *timer) { [weakSelf refreshSystemMetrics:timer]; }];
|
|
84
|
+
self.systemMetricsTimer.tolerance = 0.4;
|
|
85
|
+
}
|
|
86
|
+
// "数据刷新" 显示的是相对时间,没有新数据也得自己走字。面板隐藏时不需要这个定时器,
|
|
87
|
+
// 隐藏的窗口本来就不会重绘。
|
|
88
|
+
- (void)startQuotaClock {
|
|
89
|
+
if (self.quotaClockTimer.isValid) return;
|
|
90
|
+
self.quotaClockTimer = [NSTimer scheduledTimerWithTimeInterval:QuotaClockInterval
|
|
91
|
+
target:self selector:@selector(tickQuotaClock:) userInfo:nil repeats:YES];
|
|
92
|
+
self.quotaClockTimer.tolerance = QuotaClockInterval * 0.3;
|
|
93
|
+
}
|
|
94
|
+
- (void)tickQuotaClock:(id)sender {
|
|
95
|
+
if (!self.quotaPanel.isVisible) {
|
|
96
|
+
[self.quotaClockTimer invalidate];
|
|
97
|
+
self.quotaClockTimer = nil;
|
|
98
|
+
[self updateSystemMetricsTimer];
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
self.quotaView.needsDisplay = YES;
|
|
102
|
+
}
|
|
103
|
+
- (void)positionQuotaDashboard {
|
|
104
|
+
NSRect petFrame = self.panel.frame;
|
|
105
|
+
NSRect visible = (self.panel.screen ?: NSScreen.mainScreen).visibleFrame;
|
|
106
|
+
NSSize size = self.quotaPanel.frame.size;
|
|
107
|
+
const CGFloat margin = 12;
|
|
108
|
+
const CGFloat gap = 8;
|
|
109
|
+
CGFloat minX = NSMinX(visible) + margin;
|
|
110
|
+
CGFloat maxX = NSMaxX(visible) - size.width - margin;
|
|
111
|
+
CGFloat minY = NSMinY(visible) + margin;
|
|
112
|
+
CGFloat maxY = NSMaxY(visible) - size.height - margin;
|
|
113
|
+
CGFloat leftX = NSMinX(petFrame) - size.width - gap;
|
|
114
|
+
CGFloat rightX = NSMaxX(petFrame) + gap;
|
|
115
|
+
CGFloat x;
|
|
116
|
+
CGFloat y = fmax(minY, fmin(NSMinY(petFrame) + 112, maxY));
|
|
117
|
+
|
|
118
|
+
// 优先放在宠物左侧,其次右侧;两个可点击窗口之间始终留出间隔。
|
|
119
|
+
if (leftX >= minX) {
|
|
120
|
+
x = leftX;
|
|
121
|
+
} else if (rightX <= maxX) {
|
|
122
|
+
x = rightX;
|
|
123
|
+
} else {
|
|
124
|
+
// 横向空间不足时改放上/下方,避免屏幕边缘钳位后重新盖住宠物。
|
|
125
|
+
x = fmax(minX, fmin(NSMidX(petFrame) - size.width / 2.0, maxX));
|
|
126
|
+
CGFloat aboveY = NSMaxY(petFrame) + gap;
|
|
127
|
+
CGFloat belowY = NSMinY(petFrame) - size.height - gap;
|
|
128
|
+
if (aboveY <= maxY) y = aboveY;
|
|
129
|
+
else if (belowY >= minY) y = belowY;
|
|
130
|
+
else y = fmax(minY, fmin(aboveY, maxY));
|
|
131
|
+
}
|
|
132
|
+
[self.quotaPanel setFrameOrigin:NSMakePoint(x, y)];
|
|
133
|
+
}
|
|
134
|
+
- (void)scheduleQuotaDashboardHide {
|
|
135
|
+
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideQuotaDashboardIfNeeded) object:nil];
|
|
136
|
+
[self performSelector:@selector(hideQuotaDashboardIfNeeded) withObject:nil afterDelay:0.28];
|
|
137
|
+
}
|
|
138
|
+
- (void)hideQuotaDashboardIfNeeded {
|
|
139
|
+
if (!self.pocketHovering && !self.dashboardHovering && !self.petDragging) {
|
|
140
|
+
[self.quotaPanel orderOut:nil];
|
|
141
|
+
[self rescheduleUsageTimer];
|
|
142
|
+
[self.quotaClockTimer invalidate];
|
|
143
|
+
self.quotaClockTimer = nil;
|
|
144
|
+
[self updateSystemMetricsTimer];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// 在线判定合并两个信号:包装脚本写出的客户端 pid 文件,以及该 provider 最近是否还在
|
|
148
|
+
// 发事件。只看前者会把直接跑 claude / codex 的会话误判成离线;只看后者则在客户端退出后
|
|
149
|
+
// 还要挂满一整个静默窗口。额度数据本身不是在线信号——它一直缓存着,用它判定会恒亮。
|
|
150
|
+
- (void)updateQuotaLiveState {
|
|
151
|
+
NSMutableSet<NSString *> *online = [self.liveClientProviders mutableCopy] ?: [NSMutableSet set];
|
|
152
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
153
|
+
for (NSString *provider in self.providerActivityAt) {
|
|
154
|
+
NSTimeInterval last = [self.providerActivityAt[provider] doubleValue];
|
|
155
|
+
if (now - last < AgentStatusInactivityInterval) [online addObject:provider];
|
|
156
|
+
}
|
|
157
|
+
NSInteger count = MAX(self.liveClientCount, (NSInteger)online.count);
|
|
158
|
+
if (self.quotaView.activeAgentCount == count &&
|
|
159
|
+
[self.quotaView.liveProviders isEqualToSet:online] &&
|
|
160
|
+
self.quotaView.hasUnlabeledClient == self.hasUnlabeledClient) return;
|
|
161
|
+
self.quotaView.activeAgentCount = count;
|
|
162
|
+
self.quotaView.liveProviders = online;
|
|
163
|
+
self.quotaView.hasUnlabeledClient = self.hasUnlabeledClient;
|
|
164
|
+
self.quotaView.needsDisplay = YES;
|
|
165
|
+
}
|
|
166
|
+
// 面板高度取决于渲染几张额度卡。调整玻璃容器后,内部内容随 autoresizingMask 铺满。
|
|
167
|
+
- (void)resizeQuotaDashboard {
|
|
168
|
+
NSSize size = NSMakeSize(QuotaLogicalWidth * QuotaScale,
|
|
169
|
+
QuotaLogicalHeightForProviderCount([self.quotaView visibleProviders].count) * QuotaScale);
|
|
170
|
+
if (NSEqualSizes(self.quotaPanel.frame.size, size)) return;
|
|
171
|
+
// 只改 size 不动 origin:窗口 frame 的原点在左下,面板会朝上收缩,底边保持贴着宠物。
|
|
172
|
+
NSRect frame = self.quotaPanel.frame;
|
|
173
|
+
frame.size = size;
|
|
174
|
+
[self.quotaPanel setFrame:frame display:NO];
|
|
175
|
+
NSRect bounds = NSMakeRect(0, 0, size.width, size.height);
|
|
176
|
+
self.quotaPanel.contentView.frame = bounds;
|
|
177
|
+
for (NSView *subview in self.quotaPanel.contentView.subviews) subview.frame = bounds;
|
|
178
|
+
self.quotaView.needsDisplay = YES;
|
|
179
|
+
if (self.quotaPanel.isVisible) [self positionQuotaDashboard];
|
|
180
|
+
}
|
|
181
|
+
// "这台机器上有哪几家 CLI"。只增不减,并持久化:探测会瞬时失败(配置目录被改名、
|
|
182
|
+
// 外置盘没挂上),卡片当着用户的面消失比多留一张更像 bug。
|
|
183
|
+
- (void)refreshDetectedProviders {
|
|
184
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
185
|
+
NSMutableSet<NSString *> *detected = [NSMutableSet setWithArray:
|
|
186
|
+
[defaults arrayForKey:DetectedProvidersKey] ?: @[]];
|
|
187
|
+
if (CodexCLIDetected()) [detected addObject:@"Codex"];
|
|
188
|
+
if (ClaudeCLIDetected()) [detected addObject:@"Claude"];
|
|
189
|
+
// 正在跑的客户端和已经拿到的额度数据都是比目录探测更硬的证据:探测漏判也不能
|
|
190
|
+
// 把一个正在工作、或者明明有额度的 provider 藏起来。
|
|
191
|
+
if (self.liveClientProviders.count > 0) [detected unionSet:self.liveClientProviders];
|
|
192
|
+
if (UsageShowsProviderInUse(self.quotaView.codexUsage)) [detected addObject:@"Codex"];
|
|
193
|
+
if (UsageShowsProviderInUse(self.quotaView.claudeUsage)) [detected addObject:@"Claude"];
|
|
194
|
+
if (self.quotaView.detectedProviders &&
|
|
195
|
+
[detected isEqualToSet:self.quotaView.detectedProviders]) return;
|
|
196
|
+
self.quotaView.detectedProviders = detected;
|
|
197
|
+
[defaults setObject:detected.allObjects forKey:DetectedProvidersKey];
|
|
198
|
+
[self resizeQuotaDashboard];
|
|
199
|
+
self.quotaView.needsDisplay = YES;
|
|
200
|
+
}
|
|
201
|
+
- (void)applyCodexUsage:(NSDictionary *)codexUsage claudeUsage:(NSDictionary *)claudeUsage {
|
|
202
|
+
self.quotaView.codexUsage = codexUsage;
|
|
203
|
+
self.quotaView.claudeUsage = claudeUsage;
|
|
204
|
+
[self refreshDetectedProviders];
|
|
205
|
+
NSDictionary *history = RecordQuotaHistory(self.quotaView.codexUsage,
|
|
206
|
+
self.quotaView.claudeUsage);
|
|
207
|
+
self.quotaView.codexHistory = QuotaHistorySeries(history, @"codex");
|
|
208
|
+
self.quotaView.claudeHistory = QuotaHistorySeries(history, @"claude");
|
|
209
|
+
self.quotaView.lastUpdatedAt = NSDate.date.timeIntervalSince1970;
|
|
210
|
+
self.quotaView.needsDisplay = YES;
|
|
211
|
+
}
|
|
212
|
+
- (void)refreshUsage:(id)sender {
|
|
213
|
+
self.lastUsageRefreshAt = NSDate.date.timeIntervalSince1970;
|
|
214
|
+
[self.usageMonitor refreshNow];
|
|
215
|
+
}
|
|
216
|
+
@end
|