cc-pets 2.1.0 → 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 +11 -6
- package/CC_BRIDGE.zh-CN.md +8 -4
- package/CHANGELOG.md +59 -1
- package/CHANGELOG.zh-CN.md +59 -1
- package/README.md +60 -12
- package/README.zh-CN.md +58 -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 +17 -16
- package/Sources/CCPets/CCPetsAppDelegate.m +74 -2406
- package/Sources/CCPets/CCPetsCleanup.h +4 -0
- package/Sources/CCPets/CCPetsCleanup.m +35 -2
- package/Sources/CCPets/CCPetsCodexRateLimits.h +1 -0
- package/Sources/CCPets/CCPetsCodexRateLimits.m +7 -0
- package/Sources/CCPets/CCPetsEvents.m +1 -1
- package/Sources/CCPets/CCPetsGlassMenu.h +10 -0
- package/Sources/CCPets/CCPetsGlassMenu.m +196 -0
- package/Sources/CCPets/CCPetsGlassView.h +31 -0
- package/Sources/CCPets/CCPetsGlassView.m +246 -0
- package/Sources/CCPets/CCPetsPaths.h +7 -1
- package/Sources/CCPets/CCPetsPaths.m +14 -1
- package/Sources/CCPets/CCPetsTerminalFocus.h +15 -0
- package/Sources/CCPets/CCPetsTerminalFocus.m +306 -15
- package/Sources/CCPets/CCPetsUsage.h +2 -1
- package/Sources/CCPets/CCPetsUsage.m +69 -21
- package/Sources/CCPets/CCPetsUsageMonitor.m +43 -8
- package/Sources/CCPets/CCPetsVersion.h +7 -0
- package/Sources/CCPets/CCPetsVersion.m +82 -0
- package/Sources/CCPets/MenuChoiceRow.h +19 -0
- package/Sources/CCPets/MenuChoiceRow.m +96 -0
- package/Sources/CCPets/MenuHintView.h +15 -0
- package/Sources/CCPets/MenuHintView.m +128 -0
- package/Sources/CCPets/PetView.h +2 -0
- package/Sources/CCPets/PetView.m +121 -53
- package/Sources/CCPets/QuotaDashboardView.h +3 -4
- package/Sources/CCPets/QuotaDashboardView.m +222 -146
- package/Sources/CCPets/main.m +29 -0
- package/bin/cc-pets +39 -1
- package/bin/codex-with-pet +2 -0
- 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,297 @@
|
|
|
1
|
+
// CC Bridge 开关、消息角标与菜单。
|
|
2
|
+
#import "CCPetsAppDelegate+Private.h"
|
|
3
|
+
|
|
4
|
+
@implementation AppDelegate (Bridge)
|
|
5
|
+
// 菜单开关 → cc-pets bridge enable / disable / configure。逻辑全在 CLI 里,桌宠只负责调用,
|
|
6
|
+
// 命令行与菜单两条路的行为才一致。enable 要跑 claude / codex mcp add,要几秒,所以异步执行;
|
|
7
|
+
// 执行期间忽略新的切换,结束后按实际状态刷新(菜单下次打开时现读)。
|
|
8
|
+
- (void)runBridgeCommand:(NSArray<NSString *> *)arguments sender:(NSButton *)sender
|
|
9
|
+
successMessage:(NSString *)successMessage {
|
|
10
|
+
NSDictionary<NSString *, NSString *> *locator = CCBridgeCLILocator();
|
|
11
|
+
if (!locator) {
|
|
12
|
+
[self showAlertWithTitle:@"无法修改 CC Bridge 设置"
|
|
13
|
+
message:@"没有找到 cc-pets 的命令行程序。请在终端执行一次 cc-pets install 后重试。"];
|
|
14
|
+
[self syncBridgeSwitch:sender];
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (self.bridgeCommandRunning) {
|
|
18
|
+
[self syncBridgeSwitch:sender];
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
self.bridgeCommandRunning = YES;
|
|
22
|
+
NSTask *task = [NSTask new];
|
|
23
|
+
task.executableURL = [NSURL fileURLWithPath:locator[@"node"]];
|
|
24
|
+
task.arguments = [@[locator[@"cli"]] arrayByAddingObjectsFromArray:arguments];
|
|
25
|
+
NSMutableDictionary<NSString *, NSString *> *environment =
|
|
26
|
+
[NSProcessInfo.processInfo.environment mutableCopy];
|
|
27
|
+
NSString *existingPath = environment[@"PATH"].length > 0 ? environment[@"PATH"] : @"/usr/bin:/bin:/usr/sbin:/sbin";
|
|
28
|
+
// claude / codex 常装在 node 同目录(nvm),桌宠的 PATH 里未必有。
|
|
29
|
+
environment[@"PATH"] = [NSString stringWithFormat:@"%@:%@",
|
|
30
|
+
[locator[@"node"] stringByDeletingLastPathComponent], existingPath];
|
|
31
|
+
task.environment = environment;
|
|
32
|
+
NSPipe *output = [NSPipe pipe];
|
|
33
|
+
task.standardOutput = output;
|
|
34
|
+
task.standardError = output;
|
|
35
|
+
__weak typeof(self) weakSelf = self;
|
|
36
|
+
task.terminationHandler = ^(NSTask *finished) {
|
|
37
|
+
NSData *data = [output.fileHandleForReading readDataToEndOfFile];
|
|
38
|
+
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"";
|
|
39
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
40
|
+
typeof(self) strongSelf = weakSelf;
|
|
41
|
+
if (!strongSelf) return;
|
|
42
|
+
strongSelf.bridgeCommandRunning = NO;
|
|
43
|
+
[strongSelf refreshBridgeState:nil];
|
|
44
|
+
if (finished.terminationStatus == EXIT_SUCCESS) {
|
|
45
|
+
if (successMessage.length > 0) {
|
|
46
|
+
[strongSelf sendNotificationWithTitle:@"CC Bridge" body:successMessage];
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
[strongSelf syncBridgeSwitch:sender];
|
|
51
|
+
NSString *detail = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
52
|
+
[strongSelf showAlertWithTitle:@"CC Bridge 设置失败"
|
|
53
|
+
message:detail.length > 0 ? detail : @"命令执行失败。"];
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
NSError *error = nil;
|
|
57
|
+
if (![task launchAndReturnError:&error]) {
|
|
58
|
+
self.bridgeCommandRunning = NO;
|
|
59
|
+
[self syncBridgeSwitch:sender];
|
|
60
|
+
[self showAlertWithTitle:@"CC Bridge 设置失败" message:error.localizedDescription];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// 与其他菜单开关一致:目标状态按"实际存储的状态取反"算,不信任开关视图自己翻转后的 state——
|
|
64
|
+
// 菜单停留期间状态可能已被命令行改过。命令没执行成功时再把开关翻回实际状态。
|
|
65
|
+
- (BOOL)currentBridgeStateForSwitch:(NSButton *)sender {
|
|
66
|
+
NSDictionary *options = CCBridgeOptions();
|
|
67
|
+
if (sender.action == @selector(toggleBridgeEnabled:)) return CCBridgeEnabled();
|
|
68
|
+
if (sender.action == @selector(toggleBridgeWake:)) return [options[@"wake"] boolValue];
|
|
69
|
+
if (sender.action == @selector(toggleBridgeEditGuard:)) return [options[@"editGuard"] boolValue];
|
|
70
|
+
if (sender.action == @selector(toggleBridgeApproval:)) {
|
|
71
|
+
NSSet *tools = [NSSet setWithArray:CCBridgeToolGroup([self bridgeApprovalGroupForTag:sender.tag])];
|
|
72
|
+
return tools.count > 0 && [tools isSubsetOfSet:[NSSet setWithArray:options[@"codexApprove"]]] &&
|
|
73
|
+
[tools isSubsetOfSet:[NSSet setWithArray:options[@"claudeAllow"]]];
|
|
74
|
+
}
|
|
75
|
+
return NO;
|
|
76
|
+
}
|
|
77
|
+
- (void)syncBridgeSwitch:(NSButton *)sender {
|
|
78
|
+
if (!sender) return;
|
|
79
|
+
sender.state = [self currentBridgeStateForSwitch:sender] ? NSControlStateValueOn : NSControlStateValueOff;
|
|
80
|
+
}
|
|
81
|
+
// 返回目标状态,并先把开关视图摆到目标状态上(命令失败时 syncBridgeSwitch 再翻回来)。
|
|
82
|
+
- (BOOL)targetBridgeStateForSwitch:(NSButton *)sender {
|
|
83
|
+
BOOL target = ![self currentBridgeStateForSwitch:sender];
|
|
84
|
+
sender.state = target ? NSControlStateValueOn : NSControlStateValueOff;
|
|
85
|
+
return target;
|
|
86
|
+
}
|
|
87
|
+
- (NSString *)bridgeApprovalGroupForTag:(NSInteger)tag {
|
|
88
|
+
NSArray<NSString *> *groups = CCBridgeToolGroupNames();
|
|
89
|
+
return tag >= 1 && tag <= (NSInteger)groups.count ? groups[tag - 1] : @"";
|
|
90
|
+
}
|
|
91
|
+
- (void)toggleBridgeEnabled:(NSButton *)sender {
|
|
92
|
+
BOOL enable = [self targetBridgeStateForSwitch:sender];
|
|
93
|
+
[self runBridgeCommand:@[enable ? @"enable" : @"disable"] sender:sender
|
|
94
|
+
successMessage:enable
|
|
95
|
+
? @"已开启。已在运行的 Codex 会话需重启并在 /hooks 中信任;Claude 会话重启后才有 cc-bridge 工具。"
|
|
96
|
+
: @"已关闭,相关 hooks 与 MCP 注册已移除。"];
|
|
97
|
+
}
|
|
98
|
+
// 一个分组同时作用于 Codex 免审批与 Claude 免确认:菜单上只有一组开关,两边保持一致。
|
|
99
|
+
- (void)toggleBridgeApproval:(NSButton *)sender {
|
|
100
|
+
NSArray<NSString *> *tools = CCBridgeToolGroup([self bridgeApprovalGroupForTag:sender.tag]);
|
|
101
|
+
if (tools.count == 0) return;
|
|
102
|
+
BOOL allow = [self targetBridgeStateForSwitch:sender];
|
|
103
|
+
NSDictionary *options = CCBridgeOptions();
|
|
104
|
+
NSString *(^apply)(NSArray<NSString *> *) = ^NSString *(NSArray<NSString *> *current) {
|
|
105
|
+
NSMutableOrderedSet<NSString *> *next = [NSMutableOrderedSet orderedSetWithArray:current];
|
|
106
|
+
if (allow) [next addObjectsFromArray:tools]; else [next removeObjectsInArray:tools];
|
|
107
|
+
return [next.array componentsJoinedByString:@","];
|
|
108
|
+
};
|
|
109
|
+
[self runBridgeCommand:@[@"configure",
|
|
110
|
+
[@"--codex-approve=" stringByAppendingString:apply(options[@"codexApprove"])],
|
|
111
|
+
[@"--claude-allow=" stringByAppendingString:apply(options[@"claudeAllow"])]]
|
|
112
|
+
sender:sender successMessage:nil];
|
|
113
|
+
}
|
|
114
|
+
- (void)toggleBridgeWake:(NSButton *)sender {
|
|
115
|
+
[self runBridgeCommand:@[@"configure",
|
|
116
|
+
[self targetBridgeStateForSwitch:sender] ? @"--wake=on" : @"--wake=off"] sender:sender successMessage:nil];
|
|
117
|
+
}
|
|
118
|
+
- (void)toggleBridgeEditGuard:(NSButton *)sender {
|
|
119
|
+
[self runBridgeCommand:@[@"configure",
|
|
120
|
+
[self targetBridgeStateForSwitch:sender] ? @"--edit-guard=on" : @"--edit-guard=off"]
|
|
121
|
+
sender:sender successMessage:nil];
|
|
122
|
+
}
|
|
123
|
+
- (void)toggleBridgeBadge:(NSButton *)sender {
|
|
124
|
+
BOOL enabled = ![NSUserDefaults.standardUserDefaults boolForKey:BridgeBadgeEnabledKey];
|
|
125
|
+
[NSUserDefaults.standardUserDefaults setBool:enabled forKey:BridgeBadgeEnabledKey];
|
|
126
|
+
sender.state = enabled ? NSControlStateValueOn : NSControlStateValueOff;
|
|
127
|
+
[self refreshBridgeBadge];
|
|
128
|
+
}
|
|
129
|
+
// 与"系统通知"那组开关同一套授权流程:第一次打开时向系统申请通知权限。
|
|
130
|
+
- (void)toggleBridgeNotification:(NSButton *)sender {
|
|
131
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
132
|
+
if ([defaults boolForKey:BridgeNotificationKey]) {
|
|
133
|
+
[defaults setBool:NO forKey:BridgeNotificationKey];
|
|
134
|
+
sender.state = NSControlStateValueOff;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
[UNUserNotificationCenter.currentNotificationCenter
|
|
138
|
+
requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound
|
|
139
|
+
completionHandler:^(BOOL granted, NSError *error) {
|
|
140
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
141
|
+
[defaults setBool:granted forKey:BridgeNotificationKey];
|
|
142
|
+
sender.state = granted ? NSControlStateValueOn : NSControlStateValueOff;
|
|
143
|
+
if (!granted) {
|
|
144
|
+
[self showAlertWithTitle:@"无法启用系统通知" message:error.localizedDescription ?:
|
|
145
|
+
@"请在“系统设置 → 通知 → CC Pets”中允许通知后重试。"];
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}];
|
|
149
|
+
}
|
|
150
|
+
// 读 bridge 状态(会话名、最近送达、信箱积压),刷新角标。bridge 未开启时清空并隐藏。
|
|
151
|
+
// 回执目录没变就不重读文件,只按时间窗重新过滤缓存——回执状态一变,写入端的
|
|
152
|
+
// "临时文件 + rename" 必然改动目录的修改时间。
|
|
153
|
+
- (void)refreshBridgeState:(NSTimer *)timer {
|
|
154
|
+
if (!CCBridgeEnabled()) {
|
|
155
|
+
self.bridgeSessions = nil;
|
|
156
|
+
self.bridgeRecentDeliveries = nil;
|
|
157
|
+
self.bridgePendingCounts = nil;
|
|
158
|
+
self.bridgeSentStamp = nil;
|
|
159
|
+
[self refreshBridgeBadge];
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
163
|
+
NSString *directory = CCBridgeStateDirectory();
|
|
164
|
+
self.bridgeSessions = CCBridgeSessions();
|
|
165
|
+
NSDate *stamp = [NSFileManager.defaultManager attributesOfItemAtPath:
|
|
166
|
+
[directory stringByAppendingPathComponent:@"sent"] error:nil].fileModificationDate;
|
|
167
|
+
if (!stamp || ![stamp isEqualToDate:self.bridgeSentStamp] || !self.bridgeDeliveryCache) {
|
|
168
|
+
self.bridgeDeliveryCache = CCBridgeRecentDeliveries(now - BridgeRecentWindow, 50);
|
|
169
|
+
self.bridgeSentStamp = stamp;
|
|
170
|
+
}
|
|
171
|
+
NSMutableArray<NSDictionary *> *recent = [NSMutableArray array];
|
|
172
|
+
for (NSDictionary *delivery in self.bridgeDeliveryCache) {
|
|
173
|
+
if ([delivery[@"at"] doubleValue] >= now - BridgeRecentWindow) [recent addObject:delivery];
|
|
174
|
+
}
|
|
175
|
+
self.bridgeRecentDeliveries = recent;
|
|
176
|
+
// 只算仍在线的会话:已退出会话的积压已经投不出去,提醒也无从处理。
|
|
177
|
+
NSMutableDictionary<NSString *, NSNumber *> *pending = [NSMutableDictionary dictionary];
|
|
178
|
+
[CCBridgePendingCounts() enumerateKeysAndObjectsUsingBlock:^(NSString *session, NSNumber *count, BOOL *stop) {
|
|
179
|
+
if (self.bridgeSessions[session]) pending[session] = count;
|
|
180
|
+
}];
|
|
181
|
+
self.bridgePendingCounts = pending;
|
|
182
|
+
[self notifyNewBridgeDeliveries];
|
|
183
|
+
[self refreshBridgeBadge];
|
|
184
|
+
}
|
|
185
|
+
// 新送达的跨会话消息发系统通知:只含谁发给谁,不含正文。一次刷新里来了多条时合并成一条。
|
|
186
|
+
- (void)notifyNewBridgeDeliveries {
|
|
187
|
+
NSMutableArray<NSDictionary *> *fresh = [NSMutableArray array];
|
|
188
|
+
for (NSDictionary *delivery in self.bridgeRecentDeliveries) {
|
|
189
|
+
if ([delivery[@"at"] doubleValue] > self.bridgeNotifiedAt) [fresh addObject:delivery];
|
|
190
|
+
}
|
|
191
|
+
if (fresh.count == 0) return;
|
|
192
|
+
for (NSDictionary *delivery in fresh) {
|
|
193
|
+
self.bridgeNotifiedAt = MAX(self.bridgeNotifiedAt, [delivery[@"at"] doubleValue]);
|
|
194
|
+
}
|
|
195
|
+
if (![NSUserDefaults.standardUserDefaults boolForKey:BridgeNotificationKey]) return;
|
|
196
|
+
NSDictionary *latest = fresh.firstObject;
|
|
197
|
+
NSString *body = fresh.count == 1
|
|
198
|
+
? [NSString stringWithFormat:@"%@ → %@", latest[@"from"], latest[@"to"]]
|
|
199
|
+
: [NSString stringWithFormat:@"%@ → %@ 等 %lu 条", latest[@"from"], latest[@"to"],
|
|
200
|
+
(unsigned long)fresh.count];
|
|
201
|
+
[self sendNotificationWithTitle:@"CC Bridge 新消息" body:body];
|
|
202
|
+
}
|
|
203
|
+
// 蓝色 = 有新的跨会话消息送达;橙色 = 有消息卡在某个会话的信箱里(那个 Claude 会话长时间空闲、
|
|
204
|
+
// 唤醒 watcher 已退出,要等用户在那个终端开口才会送进去),需要用户过去看一眼。
|
|
205
|
+
- (void)refreshBridgeBadge {
|
|
206
|
+
CCPetsApprovalBadgeView *badge = (CCPetsApprovalBadgeView *)self.bridgeBadgeView;
|
|
207
|
+
if (!badge) return;
|
|
208
|
+
if (![NSUserDefaults.standardUserDefaults boolForKey:BridgeBadgeEnabledKey]) {
|
|
209
|
+
badge.count = 0;
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
NSUInteger unseen = 0;
|
|
213
|
+
for (NSDictionary *delivery in self.bridgeRecentDeliveries) {
|
|
214
|
+
if ([delivery[@"at"] doubleValue] > self.bridgeSeenAt) unseen++;
|
|
215
|
+
}
|
|
216
|
+
NSUInteger waiting = 0;
|
|
217
|
+
for (NSNumber *count in self.bridgePendingCounts.allValues) waiting += count.unsignedIntegerValue;
|
|
218
|
+
badge.fillColor = waiting > 0
|
|
219
|
+
? [NSColor colorWithRed:0.92 green:0.58 blue:0.12 alpha:1]
|
|
220
|
+
: [NSColor colorWithRed:0.20 green:0.47 blue:0.90 alpha:1];
|
|
221
|
+
badge.count = unseen + waiting;
|
|
222
|
+
[self layoutApprovalBadge];
|
|
223
|
+
}
|
|
224
|
+
- (NSString *)bridgeSessionIdForRecord:(NSDictionary *)record {
|
|
225
|
+
NSString *session = SanitizedShortString(record[@"session"], 128);
|
|
226
|
+
if (session.length > 0 && self.bridgeSessions[session]) return session;
|
|
227
|
+
// 事件里没有 session id 的记录,退回按 provider + tty 对上 bridge 注册表。
|
|
228
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
|
|
229
|
+
NSString *tty = SanitizedShortString([terminal[@"tty"] lastPathComponent], 64);
|
|
230
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
231
|
+
if (tty.length == 0) return nil;
|
|
232
|
+
__block NSString *match = nil;
|
|
233
|
+
[self.bridgeSessions enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *entry, BOOL *stop) {
|
|
234
|
+
if ([entry[@"tty"] isEqualToString:tty] && [entry[@"provider"] isEqualToString:provider]) {
|
|
235
|
+
match = key;
|
|
236
|
+
*stop = YES;
|
|
237
|
+
}
|
|
238
|
+
}];
|
|
239
|
+
return match;
|
|
240
|
+
}
|
|
241
|
+
- (NSDictionary *)bridgeSessionEntryForRecord:(NSDictionary *)record {
|
|
242
|
+
NSString *session = [self bridgeSessionIdForRecord:record];
|
|
243
|
+
return session ? self.bridgeSessions[session] : nil;
|
|
244
|
+
}
|
|
245
|
+
// 收件会话的终端:优先用桌宠自己记录的该会话终端信息(带宿主应用),其次按 tty 对上任一
|
|
246
|
+
// 已知终端,最后借用最近一次的宿主应用配上 bridge 记录的 tty。
|
|
247
|
+
- (NSDictionary *)terminalTargetForBridgeSession:(NSString *)sessionId {
|
|
248
|
+
NSString *tty = self.bridgeSessions[sessionId][@"tty"];
|
|
249
|
+
NSDictionary *byTty = nil;
|
|
250
|
+
for (NSDictionary *record in self.agentSessionRecords.allValues) {
|
|
251
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
|
|
252
|
+
if (terminal.count == 0) continue;
|
|
253
|
+
if ([SanitizedShortString(record[@"session"], 128) isEqualToString:sessionId]) return terminal;
|
|
254
|
+
if (tty.length > 0 && [[terminal[@"tty"] lastPathComponent] isEqualToString:tty]) byTty = terminal;
|
|
255
|
+
}
|
|
256
|
+
if (byTty) return byTty;
|
|
257
|
+
if (tty.length > 0 && self.lastTerminalFocusTarget.count > 0) {
|
|
258
|
+
NSMutableDictionary *target = [self.lastTerminalFocusTarget mutableCopy];
|
|
259
|
+
target[@"tty"] = tty;
|
|
260
|
+
[target removeObjectForKey:@"session"];
|
|
261
|
+
return target;
|
|
262
|
+
}
|
|
263
|
+
return nil;
|
|
264
|
+
}
|
|
265
|
+
- (void)focusBridgeSession:(NSMenuItem *)sender {
|
|
266
|
+
NSString *sessionId = [sender.representedObject isKindOfClass:NSString.class] ? sender.representedObject : nil;
|
|
267
|
+
NSDictionary *target = sessionId ? [self terminalTargetForBridgeSession:sessionId] : nil;
|
|
268
|
+
if (target.count > 0) ActivateTerminalFocusTarget(target);
|
|
269
|
+
}
|
|
270
|
+
- (void)addBridgeItemsToMenu:(NSMenu *)menu deliveries:(NSArray<NSDictionary *> *)deliveries
|
|
271
|
+
pending:(NSDictionary<NSString *, NSNumber *> *)pending {
|
|
272
|
+
NSMenuItem *heading = [menu addItemWithTitle:@"CC Bridge 消息" action:nil keyEquivalent:@""];
|
|
273
|
+
heading.enabled = NO;
|
|
274
|
+
[pending enumerateKeysAndObjectsUsingBlock:^(NSString *session, NSNumber *count, BOOL *stop) {
|
|
275
|
+
NSString *name = self.bridgeSessions[session][@"name"] ?: session;
|
|
276
|
+
NSString *title = [NSString stringWithFormat:@"📬 %@ 有 %lu 条消息待投递(去那个终端说句话即可送达)",
|
|
277
|
+
name, count.unsignedLongValue];
|
|
278
|
+
NSMenuItem *item = [menu addItemWithTitle:title action:@selector(focusBridgeSession:) keyEquivalent:@""];
|
|
279
|
+
item.target = self;
|
|
280
|
+
item.representedObject = session;
|
|
281
|
+
}];
|
|
282
|
+
NSUInteger shown = 0;
|
|
283
|
+
for (NSDictionary *delivery in deliveries) {
|
|
284
|
+
if (shown++ >= BridgeMenuDeliveryLimit) break;
|
|
285
|
+
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[delivery[@"at"] doubleValue]];
|
|
286
|
+
NSString *time = [NSDateFormatter localizedStringFromDate:date
|
|
287
|
+
dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
|
|
288
|
+
BOOL unseen = [delivery[@"at"] doubleValue] > self.bridgeSeenAt;
|
|
289
|
+
NSString *title = [NSString stringWithFormat:@"%@%@ → %@ · %@",
|
|
290
|
+
unseen ? @"✉️ " : @"", delivery[@"from"], delivery[@"to"], time];
|
|
291
|
+
NSMenuItem *item = [menu addItemWithTitle:title action:@selector(focusBridgeSession:) keyEquivalent:@""];
|
|
292
|
+
item.target = self;
|
|
293
|
+
item.representedObject = delivery[@"toSession"];
|
|
294
|
+
item.toolTip = [NSString stringWithFormat:@"跳到 %@ 所在的终端", delivery[@"to"]];
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
@end
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// 宠物素材发现、切换与删除。
|
|
2
|
+
#import "CCPetsAppDelegate+Private.h"
|
|
3
|
+
|
|
4
|
+
@implementation AppDelegate (Pets)
|
|
5
|
+
- (NSDictionary *)petManifestInDirectory:(NSString *)directory {
|
|
6
|
+
NSString *jsonPath = [directory stringByAppendingPathComponent:@"pet.json"];
|
|
7
|
+
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
|
|
8
|
+
if (!jsonData) return @{};
|
|
9
|
+
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
|
10
|
+
return [json isKindOfClass:NSDictionary.class] ? json : @{};
|
|
11
|
+
}
|
|
12
|
+
- (NSInteger)spriteVersionInDirectory:(NSString *)directory fallbackDirectory:(NSString *)fallbackDirectory {
|
|
13
|
+
NSArray<NSString *> *directories = fallbackDirectory.length > 0
|
|
14
|
+
? @[directory, fallbackDirectory]
|
|
15
|
+
: @[directory];
|
|
16
|
+
for (NSString *candidate in directories) {
|
|
17
|
+
id value = [self petManifestInDirectory:candidate][@"spriteVersionNumber"];
|
|
18
|
+
if (![value isKindOfClass:NSNumber.class]) continue;
|
|
19
|
+
NSInteger version = [value integerValue];
|
|
20
|
+
if (version == 1 || version == 2) return version;
|
|
21
|
+
}
|
|
22
|
+
return 1;
|
|
23
|
+
}
|
|
24
|
+
- (NSInteger)spriteRowCountForVersion:(NSInteger)version {
|
|
25
|
+
return version == 2 ? 11 : 9;
|
|
26
|
+
}
|
|
27
|
+
- (NSString *)spritePathInDirectory:(NSString *)directory {
|
|
28
|
+
NSString *configuredName = nil;
|
|
29
|
+
id value = [self petManifestInDirectory:directory][@"spritesheetPath"];
|
|
30
|
+
if ([value isKindOfClass:NSString.class]) configuredName = [value lastPathComponent];
|
|
31
|
+
NSMutableArray<NSString *> *names = [NSMutableArray array];
|
|
32
|
+
if (configuredName.length > 0) [names addObject:configuredName];
|
|
33
|
+
[names addObjectsFromArray:@[@"spritesheet.webp", @"spritesheet.png"]];
|
|
34
|
+
for (NSString *name in names) {
|
|
35
|
+
NSString *path = [directory stringByAppendingPathComponent:name];
|
|
36
|
+
BOOL isDirectory = NO;
|
|
37
|
+
if ([NSFileManager.defaultManager fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory) return path;
|
|
38
|
+
}
|
|
39
|
+
return nil;
|
|
40
|
+
}
|
|
41
|
+
- (NSArray<NSString *> *)petDirectoryNamesAtPath:(NSString *)root {
|
|
42
|
+
NSArray<NSString *> *entries = [NSFileManager.defaultManager contentsOfDirectoryAtPath:root error:nil] ?: @[];
|
|
43
|
+
NSMutableArray<NSString *> *directories = [NSMutableArray array];
|
|
44
|
+
for (NSString *entry in entries) {
|
|
45
|
+
if ([entry hasPrefix:@"."]) continue;
|
|
46
|
+
BOOL isDirectory = NO;
|
|
47
|
+
NSString *path = [root stringByAppendingPathComponent:entry];
|
|
48
|
+
if ([NSFileManager.defaultManager fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory) {
|
|
49
|
+
[directories addObject:entry];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return [directories sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
|
|
53
|
+
}
|
|
54
|
+
// 桌宠只扫 OwnPetsDirectory() 加包内素材,无条件、不看那里有没有东西。~/.petdex/pets 和
|
|
55
|
+
// ~/.codex/pets 由别的工具写入,直接扫就永远分不清哪只是自己装的;要用 Codex 的素材就打开
|
|
56
|
+
// "导入 Codex 素材"开关,由 ImportCodexPets 复制进来,之后它们和自己装的没有区别。
|
|
57
|
+
- (NSArray<NSDictionary *> *)discoverPetOptions {
|
|
58
|
+
NSString *ownRoot = OwnPetsDirectory();
|
|
59
|
+
NSMutableArray<NSDictionary *> *result = [NSMutableArray array];
|
|
60
|
+
|
|
61
|
+
for (NSString *name in [self petDirectoryNamesAtPath:ownRoot]) {
|
|
62
|
+
NSString *directory = [ownRoot stringByAppendingPathComponent:name];
|
|
63
|
+
NSString *path = [self spritePathInDirectory:directory];
|
|
64
|
+
if (!path) continue;
|
|
65
|
+
NSInteger version = [self spriteVersionInDirectory:directory fallbackDirectory:nil];
|
|
66
|
+
[result addObject:@{
|
|
67
|
+
@"id": [@"external:" stringByAppendingString:name],
|
|
68
|
+
@"name": name,
|
|
69
|
+
@"path": path,
|
|
70
|
+
@"spriteVersionNumber": @(version),
|
|
71
|
+
@"spriteRowCount": @([self spriteRowCountForVersion:version])
|
|
72
|
+
}];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
NSArray<NSString *> *bundled = [NSFileManager.defaultManager
|
|
76
|
+
contentsOfDirectoryAtPath:self.binaryDirectory error:nil] ?: @[];
|
|
77
|
+
for (NSString *fileName in [bundled sortedArrayUsingSelector:@selector(localizedStandardCompare:)]) {
|
|
78
|
+
NSString *extension = fileName.pathExtension.lowercaseString;
|
|
79
|
+
if (![extension isEqualToString:@"webp"] && ![extension isEqualToString:@"png"]) continue;
|
|
80
|
+
[result addObject:@{
|
|
81
|
+
@"id": [@"builtin:" stringByAppendingString:fileName],
|
|
82
|
+
@"name": fileName.stringByDeletingPathExtension,
|
|
83
|
+
@"path": [self.binaryDirectory stringByAppendingPathComponent:fileName],
|
|
84
|
+
@"spriteVersionNumber": @1,
|
|
85
|
+
@"spriteRowCount": @9
|
|
86
|
+
}];
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
// 素材目录的 mtime。目录里增删条目会改动它,而 `cc-pets pet add` / `remove` 正是在增删
|
|
91
|
+
// 条目——所以这一个时间戳就足以判断列表是否需要重扫,不必每次打开菜单都遍历目录。
|
|
92
|
+
- (NSDate *)petOptionsStamp {
|
|
93
|
+
NSDictionary *attributes = [NSFileManager.defaultManager
|
|
94
|
+
attributesOfItemAtPath:OwnPetsDirectory() error:nil];
|
|
95
|
+
return attributes[NSFileModificationDate];
|
|
96
|
+
}
|
|
97
|
+
- (void)invalidatePetOptionsCache {
|
|
98
|
+
self.cachedPetOptions = nil;
|
|
99
|
+
self.cachedPetOptionsStamp = nil;
|
|
100
|
+
}
|
|
101
|
+
- (NSArray<NSDictionary *> *)petOptions {
|
|
102
|
+
NSDate *stamp = [self petOptionsStamp];
|
|
103
|
+
BOOL stale = !self.cachedPetOptions ||
|
|
104
|
+
(stamp == nil) != (self.cachedPetOptionsStamp == nil) ||
|
|
105
|
+
(stamp && ![stamp isEqualToDate:self.cachedPetOptionsStamp]);
|
|
106
|
+
if (stale) {
|
|
107
|
+
self.cachedPetOptions = [self discoverPetOptions];
|
|
108
|
+
self.cachedPetOptionsStamp = stamp;
|
|
109
|
+
}
|
|
110
|
+
return self.cachedPetOptions;
|
|
111
|
+
}
|
|
112
|
+
- (NSDictionary *)petOptionWithID:(NSString *)petID inOptions:(NSArray<NSDictionary *> *)options {
|
|
113
|
+
for (NSDictionary *option in options) if ([option[@"id"] isEqualToString:petID]) return option;
|
|
114
|
+
return nil;
|
|
115
|
+
}
|
|
116
|
+
- (NSArray<NSDictionary *> *)waitForPetOptions {
|
|
117
|
+
while (YES) {
|
|
118
|
+
// 这个循环就是"重新扫描"按钮的实现,必须绕开缓存。
|
|
119
|
+
[self invalidatePetOptionsCache];
|
|
120
|
+
NSArray<NSDictionary *> *options = [self petOptions];
|
|
121
|
+
if (options.count > 0) return options;
|
|
122
|
+
|
|
123
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
124
|
+
NSAlert *alert = [NSAlert new];
|
|
125
|
+
alert.messageText = @"没有找到桌宠素材";
|
|
126
|
+
alert.informativeText = @"用 `cc-pets pet add <名称>` 下载素材,或把 PNG / WebP 精灵图放入 "
|
|
127
|
+
@"~/.cc-pets/pets/<名称>/。桌宠不会去读 ~/.petdex/pets/ 和 ~/.codex/pets/;"
|
|
128
|
+
@"要用 Codex 的素材,请在右键菜单打开“导入 Codex 素材”。完成后点击“重新扫描”。";
|
|
129
|
+
[alert addButtonWithTitle:@"重新扫描"];
|
|
130
|
+
[alert addButtonWithTitle:@"打开素材目录"];
|
|
131
|
+
[alert addButtonWithTitle:@"退出"];
|
|
132
|
+
NSModalResponse response = [alert runModal];
|
|
133
|
+
if (response == NSAlertSecondButtonReturn) {
|
|
134
|
+
NSString *directory = OwnPetsDirectory();
|
|
135
|
+
[NSFileManager.defaultManager createDirectoryAtPath:directory
|
|
136
|
+
withIntermediateDirectories:YES attributes:nil error:nil];
|
|
137
|
+
[NSWorkspace.sharedWorkspace openURL:[NSURL fileURLWithPath:directory]];
|
|
138
|
+
} else if (response == NSAlertThirdButtonReturn) {
|
|
139
|
+
return @[];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
- (void)switchPetToID:(NSString *)petID {
|
|
144
|
+
NSArray<NSDictionary *> *options = [self petOptions];
|
|
145
|
+
NSDictionary *option = [self petOptionWithID:petID inOptions:options];
|
|
146
|
+
if (!option) return;
|
|
147
|
+
NSInteger spriteRowCount = [option[@"spriteRowCount"] integerValue] ?: 9;
|
|
148
|
+
NSImage *image = LoadPetSpriteImage(option[@"path"], NSMakeSize(140, 150), spriteRowCount);
|
|
149
|
+
if (!image) return;
|
|
150
|
+
[self.petView applySheet:image petID:petID rowCount:spriteRowCount];
|
|
151
|
+
PetPhrasesSetCurrentPetID(petID);
|
|
152
|
+
[NSUserDefaults.standardUserDefaults setObject:petID forKey:@"CCPetsSelectedSprite"];
|
|
153
|
+
}
|
|
154
|
+
- (BOOL)deletePetWithID:(NSString *)petID {
|
|
155
|
+
if (![petID hasPrefix:@"external:"]) return NO;
|
|
156
|
+
NSArray<NSDictionary *> *options = [self petOptions];
|
|
157
|
+
NSDictionary *option = [self petOptionWithID:petID inOptions:options];
|
|
158
|
+
if (!option) return NO;
|
|
159
|
+
|
|
160
|
+
NSString *name = [petID substringFromIndex:@"external:".length];
|
|
161
|
+
if (name.length == 0 || [name containsString:@"/"] || [name isEqualToString:@"."] ||
|
|
162
|
+
[name isEqualToString:@".."]) return NO;
|
|
163
|
+
NSString *root = OwnPetsDirectory().stringByStandardizingPath;
|
|
164
|
+
NSString *directory = [root stringByAppendingPathComponent:name].stringByStandardizingPath;
|
|
165
|
+
NSString *optionDirectory = [[option[@"path"] stringByDeletingLastPathComponent] stringByStandardizingPath];
|
|
166
|
+
if (![directory.stringByDeletingLastPathComponent isEqualToString:root] ||
|
|
167
|
+
![optionDirectory isEqualToString:directory]) return NO;
|
|
168
|
+
|
|
169
|
+
NSError *error = nil;
|
|
170
|
+
if (![NSFileManager.defaultManager removeItemAtPath:directory error:&error]) {
|
|
171
|
+
[self showAlertWithTitle:@"无法删除素材"
|
|
172
|
+
message:error.localizedDescription ?: @"素材目录删除失败。"];
|
|
173
|
+
return NO;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
[self invalidatePetOptionsCache];
|
|
177
|
+
if ([self.petView.currentPetID isEqualToString:petID]) {
|
|
178
|
+
NSDictionary *fallback = [self petOptions].firstObject;
|
|
179
|
+
if (fallback) [self switchPetToID:fallback[@"id"]];
|
|
180
|
+
else {
|
|
181
|
+
PetPhrasesSetCurrentPetID(nil);
|
|
182
|
+
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"CCPetsSelectedSprite"];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return YES;
|
|
186
|
+
}
|
|
187
|
+
@end
|