cc-pets 2.0.1 → 2.1.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 +239 -0
- package/CC_BRIDGE.zh-CN.md +210 -0
- package/CHANGELOG.md +39 -0
- package/CHANGELOG.zh-CN.md +39 -0
- package/PROVIDER_PROTOCOL.md +6 -0
- package/PROVIDER_PROTOCOL.zh-CN.md +5 -0
- package/README.md +26 -0
- package/README.zh-CN.md +25 -2
- package/Resources/Info.plist +2 -0
- package/Sources/CCPets/CCPetsAppDelegate.h +25 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +751 -29
- package/Sources/CCPets/CCPetsBridge.h +43 -0
- package/Sources/CCPets/CCPetsBridge.m +198 -0
- package/Sources/CCPets/CCPetsEvents.m +7 -2
- package/Sources/CCPets/CCPetsPaths.h +4 -0
- package/Sources/CCPets/CCPetsPaths.m +6 -0
- package/Sources/CCPets/CCPetsQuotaHistory.m +9 -2
- package/Sources/CCPets/CCPetsTerminalFocus.h +11 -0
- package/Sources/CCPets/CCPetsTerminalFocus.m +150 -0
- package/Sources/CCPets/PetView.m +86 -1
- package/Sources/CCPets/QuotaDashboardView.m +17 -4
- package/Sources/CCPets/main.m +5 -0
- package/bin/cc-pets +5 -0
- package/bin/claude-with-pet +8 -2
- package/bin/codex-with-pet +8 -3
- package/package.json +3 -1
- package/scripts/bridge/cli.mjs +229 -0
- package/scripts/bridge/core.mjs +227 -0
- package/scripts/bridge/hook.mjs +149 -0
- package/scripts/bridge/install.mjs +288 -0
- package/scripts/bridge/mcp-server.mjs +300 -0
- package/scripts/bridge/options.mjs +86 -0
- package/scripts/bridge/process.mjs +129 -0
- package/scripts/bridge/reservations.mjs +247 -0
- package/scripts/bridge/store.mjs +432 -0
- package/scripts/bridge/watch.mjs +69 -0
- package/scripts/install-shell-integration.sh +3 -0
- package/scripts/uninstall-integrations.mjs +10 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// CC Bridge(scripts/bridge/)状态的只读视图,供桌宠显示角标、在会话列表里标出
|
|
4
|
+
// 会话名、以及点击跳回收件会话的终端。
|
|
5
|
+
//
|
|
6
|
+
// 只读元数据:会话名 / tty、谁发给谁、何时送达、信箱积压条数。回执文件里虽然带着
|
|
7
|
+
// 消息正文,这里解析后立即丢弃,不返回、不缓存——桌宠界面不显示正文。
|
|
8
|
+
|
|
9
|
+
// 桌宠侧的偏好:消息角标(默认开)、新消息系统通知(默认关)。
|
|
10
|
+
extern NSString *const BridgeBadgeEnabledKey;
|
|
11
|
+
extern NSString *const BridgeNotificationKey;
|
|
12
|
+
|
|
13
|
+
// <PetStateDirectory>/cc-bridge-<uid>,与 scripts/bridge/store.mjs 的 bridgeDirectory 一致。
|
|
14
|
+
NSString *CCBridgeStateDirectory(void);
|
|
15
|
+
|
|
16
|
+
// ~/.cc-pets/bridge-enabled(可用 CC_PETS_HOME 覆盖)存在即视为开启。
|
|
17
|
+
BOOL CCBridgeEnabled(void);
|
|
18
|
+
|
|
19
|
+
// 开关文件里保存的选项(与 scripts/bridge/options.mjs 一致),未开启或缺项时取默认值:
|
|
20
|
+
// @{ @"codexApprove": NSArray, @"claudeAllow": NSArray, @"wake": @YES, @"editGuard": @YES }
|
|
21
|
+
NSDictionary *CCBridgeOptions(void);
|
|
22
|
+
|
|
23
|
+
// 免审批分组:view / send / reserve / name → 工具名。必须与 options.mjs 的 TOOL_GROUPS 一致。
|
|
24
|
+
NSArray<NSString *> *CCBridgeToolGroupNames(void);
|
|
25
|
+
NSArray<NSString *> *CCBridgeToolGroup(NSString *group);
|
|
26
|
+
|
|
27
|
+
// CLI 定位(~/.cc-pets/bridge-cli.json,由 cc-pets bridge refresh / enable / configure 写入):
|
|
28
|
+
// @{ @"node", @"cli" },两者都必须是存在的绝对路径,否则返回 nil。
|
|
29
|
+
NSDictionary<NSString *, NSString *> *CCBridgeCLILocator(void);
|
|
30
|
+
|
|
31
|
+
// 仍有效(未过期、持有者在给定会话集合内)的文件预留条数。
|
|
32
|
+
NSUInteger CCBridgeActiveReservationCount(NSSet<NSString *> *sessionIds);
|
|
33
|
+
|
|
34
|
+
// 在线会话:session id → @{ @"name", @"ref", @"provider", @"tty" }(tty 可能缺省)。
|
|
35
|
+
// 记录了 pid 而进程已不在的会话不返回。
|
|
36
|
+
NSDictionary<NSString *, NSDictionary *> *CCBridgeSessions(void);
|
|
37
|
+
|
|
38
|
+
// 最近送达的跨会话消息,不含 cc-bridge 自己发的系统通知(空闲通知等),按送达时间倒序。
|
|
39
|
+
// 每项:@{ @"id", @"from", @"to", @"toSession", @"at" },at 为秒级时间戳。
|
|
40
|
+
NSArray<NSDictionary *> *CCBridgeRecentDeliveries(NSTimeInterval since, NSUInteger limit);
|
|
41
|
+
|
|
42
|
+
// session id → 信箱里仍未投递的条数(只含 > 0 的会话)。
|
|
43
|
+
NSDictionary<NSString *, NSNumber *> *CCBridgePendingCounts(void);
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
#import "CCPetsBridge.h"
|
|
2
|
+
#import "CCPetsPaths.h"
|
|
3
|
+
#import <errno.h>
|
|
4
|
+
#import <signal.h>
|
|
5
|
+
#import <unistd.h>
|
|
6
|
+
|
|
7
|
+
NSString *const BridgeBadgeEnabledKey = @"CCPetsBridgeBadgeEnabled";
|
|
8
|
+
NSString *const BridgeNotificationKey = @"CCPetsBridgeNotification";
|
|
9
|
+
|
|
10
|
+
// 目录里文件数的上限。回执保留 48 小时,正常使用远到不了这个量;
|
|
11
|
+
// 真到了说明有东西在刷消息,宁可少显示几条也不让桌宠主线程卡在读盘上。
|
|
12
|
+
static const NSUInteger BridgeFileScanLimit = 500;
|
|
13
|
+
|
|
14
|
+
NSString *CCBridgeStateDirectory(void) {
|
|
15
|
+
NSString *name = [NSString stringWithFormat:@"cc-bridge-%u", getuid()];
|
|
16
|
+
return [PetStateDirectory() stringByAppendingPathComponent:name];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static NSString *BridgeHomeDirectory(void) {
|
|
20
|
+
NSString *home = NSProcessInfo.processInfo.environment[@"CC_PETS_HOME"];
|
|
21
|
+
if (home.length == 0) home = [NSHomeDirectory() stringByAppendingPathComponent:@".cc-pets"];
|
|
22
|
+
return home.stringByStandardizingPath;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
BOOL CCBridgeEnabled(void) {
|
|
26
|
+
return [NSFileManager.defaultManager fileExistsAtPath:
|
|
27
|
+
[BridgeHomeDirectory() stringByAppendingPathComponent:@"bridge-enabled"]];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
NSArray<NSString *> *CCBridgeToolGroupNames(void) {
|
|
31
|
+
return @[@"view", @"send", @"reserve", @"name"];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
NSArray<NSString *> *CCBridgeToolGroup(NSString *group) {
|
|
35
|
+
static NSDictionary<NSString *, NSArray<NSString *> *> *groups;
|
|
36
|
+
static dispatch_once_t once;
|
|
37
|
+
dispatch_once(&once, ^{
|
|
38
|
+
groups = @{
|
|
39
|
+
@"view": @[@"list_agents", @"list_reservations", @"check_inbox"],
|
|
40
|
+
@"send": @[@"send_message"],
|
|
41
|
+
@"reserve": @[@"reserve_files", @"release_files"],
|
|
42
|
+
@"name": @[@"set_name"]
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
return groups[group] ?: @[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static NSArray<NSString *> *KnownTools(id value) {
|
|
49
|
+
if (![value isKindOfClass:NSArray.class]) return @[];
|
|
50
|
+
NSMutableArray<NSString *> *tools = [NSMutableArray array];
|
|
51
|
+
for (NSString *group in CCBridgeToolGroupNames()) {
|
|
52
|
+
for (NSString *tool in CCBridgeToolGroup(group)) {
|
|
53
|
+
if ([value containsObject:tool]) [tools addObject:tool];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return tools;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 名字、ref、tty 都会进菜单标题。bridge 端已经约束过格式,这里再按同一字符集收一遍,
|
|
60
|
+
// 防止被手改的状态文件往菜单里塞奇怪的东西。
|
|
61
|
+
static NSString *BridgeSafeString(id value, NSUInteger maximumLength) {
|
|
62
|
+
if (![value isKindOfClass:NSString.class]) return @"";
|
|
63
|
+
NSCharacterSet *allowed = [NSCharacterSet characterSetWithCharactersInString:
|
|
64
|
+
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"];
|
|
65
|
+
NSMutableString *safe = [NSMutableString string];
|
|
66
|
+
NSString *text = value;
|
|
67
|
+
for (NSUInteger index = 0; index < text.length && safe.length < maximumLength; index++) {
|
|
68
|
+
unichar character = [text characterAtIndex:index];
|
|
69
|
+
if ([allowed characterIsMember:character]) [safe appendFormat:@"%C", character];
|
|
70
|
+
}
|
|
71
|
+
return safe;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static NSArray<NSString *> *JSONFilesInDirectory(NSString *directory) {
|
|
75
|
+
NSArray<NSString *> *names = [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil];
|
|
76
|
+
NSMutableArray<NSString *> *files = [NSMutableArray array];
|
|
77
|
+
for (NSString *name in names) {
|
|
78
|
+
if (![name hasSuffix:@".json"]) continue;
|
|
79
|
+
[files addObject:[directory stringByAppendingPathComponent:name]];
|
|
80
|
+
if (files.count >= BridgeFileScanLimit) break;
|
|
81
|
+
}
|
|
82
|
+
return files;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static NSDictionary *ReadJSONObject(NSString *path) {
|
|
86
|
+
NSData *data = [NSData dataWithContentsOfFile:path];
|
|
87
|
+
if (data.length == 0) return nil;
|
|
88
|
+
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
|
|
89
|
+
return [object isKindOfClass:NSDictionary.class] ? object : nil;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
NSDictionary *CCBridgeOptions(void) {
|
|
93
|
+
NSDictionary *raw = nil;
|
|
94
|
+
if (CCBridgeEnabled()) {
|
|
95
|
+
raw = ReadJSONObject([BridgeHomeDirectory() stringByAppendingPathComponent:@"bridge-enabled"]);
|
|
96
|
+
}
|
|
97
|
+
return @{
|
|
98
|
+
@"codexApprove": KnownTools(raw[@"codexApprove"]),
|
|
99
|
+
@"claudeAllow": KnownTools(raw[@"claudeAllow"]),
|
|
100
|
+
@"wake": @(![raw[@"wake"] isEqual:@NO]),
|
|
101
|
+
@"editGuard": @(![raw[@"editGuard"] isEqual:@NO])
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
NSDictionary<NSString *, NSString *> *CCBridgeCLILocator(void) {
|
|
106
|
+
NSDictionary *raw = ReadJSONObject([BridgeHomeDirectory() stringByAppendingPathComponent:@"bridge-cli.json"]);
|
|
107
|
+
NSString *node = [raw[@"node"] isKindOfClass:NSString.class] ? [raw[@"node"] stringByStandardizingPath] : nil;
|
|
108
|
+
NSString *cli = [raw[@"cli"] isKindOfClass:NSString.class] ? [raw[@"cli"] stringByStandardizingPath] : nil;
|
|
109
|
+
NSFileManager *files = NSFileManager.defaultManager;
|
|
110
|
+
if (!node.isAbsolutePath || !cli.isAbsolutePath) return nil;
|
|
111
|
+
if (![files isExecutableFileAtPath:node] || ![files isReadableFileAtPath:cli]) return nil;
|
|
112
|
+
return @{@"node": node, @"cli": cli};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
NSUInteger CCBridgeActiveReservationCount(NSSet<NSString *> *sessionIds) {
|
|
116
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:@"reservations"];
|
|
117
|
+
double now = NSDate.date.timeIntervalSince1970 * 1000.0;
|
|
118
|
+
NSUInteger count = 0;
|
|
119
|
+
for (NSString *path in JSONFilesInDirectory(directory)) {
|
|
120
|
+
NSDictionary *reservation = ReadJSONObject(path);
|
|
121
|
+
if ([reservation[@"expiresAt"] doubleValue] <= now) continue;
|
|
122
|
+
NSString *session = [reservation[@"session"] isKindOfClass:NSString.class] ? reservation[@"session"] : nil;
|
|
123
|
+
if (session && [sessionIds containsObject:session]) count++;
|
|
124
|
+
}
|
|
125
|
+
return count;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static BOOL ProcessAlive(id pidValue) {
|
|
129
|
+
if (![pidValue isKindOfClass:NSNumber.class]) return YES; // 没记 pid 的会话交给 bridge 端判定
|
|
130
|
+
pid_t pid = [pidValue intValue];
|
|
131
|
+
if (pid <= 1) return YES;
|
|
132
|
+
return kill(pid, 0) == 0 || errno == EPERM;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
NSDictionary<NSString *, NSDictionary *> *CCBridgeSessions(void) {
|
|
136
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:@"sessions"];
|
|
137
|
+
NSMutableDictionary<NSString *, NSDictionary *> *sessions = [NSMutableDictionary dictionary];
|
|
138
|
+
for (NSString *path in JSONFilesInDirectory(directory)) {
|
|
139
|
+
NSDictionary *record = ReadJSONObject(path);
|
|
140
|
+
NSString *session = BridgeSafeString(record[@"session"], 128);
|
|
141
|
+
NSString *name = BridgeSafeString(record[@"name"], 40);
|
|
142
|
+
if (session.length == 0 || name.length == 0 || !ProcessAlive(record[@"pid"])) continue;
|
|
143
|
+
NSMutableDictionary *entry = [@{
|
|
144
|
+
@"name": name,
|
|
145
|
+
@"ref": BridgeSafeString(record[@"ref"], 6),
|
|
146
|
+
@"provider": BridgeSafeString(record[@"provider"], 16)
|
|
147
|
+
} mutableCopy];
|
|
148
|
+
NSString *tty = BridgeSafeString(record[@"tty"], 32);
|
|
149
|
+
if (tty.length > 0) entry[@"tty"] = tty;
|
|
150
|
+
sessions[session] = entry;
|
|
151
|
+
}
|
|
152
|
+
return sessions;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
NSArray<NSDictionary *> *CCBridgeRecentDeliveries(NSTimeInterval since, NSUInteger limit) {
|
|
156
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:@"sent"];
|
|
157
|
+
NSMutableArray<NSDictionary *> *deliveries = [NSMutableArray array];
|
|
158
|
+
for (NSString *path in JSONFilesInDirectory(directory)) {
|
|
159
|
+
NSDictionary *receipt = ReadJSONObject(path);
|
|
160
|
+
if (![receipt[@"status"] isEqual:@"delivered"]) continue;
|
|
161
|
+
NSDictionary *from = [receipt[@"from"] isKindOfClass:NSDictionary.class] ? receipt[@"from"] : nil;
|
|
162
|
+
NSDictionary *to = [receipt[@"to"] isKindOfClass:NSDictionary.class] ? receipt[@"to"] : nil;
|
|
163
|
+
if (!from || !to || [from[@"system"] boolValue]) continue;
|
|
164
|
+
id stamp = receipt[@"updatedAt"] ?: receipt[@"createdAt"];
|
|
165
|
+
if (![stamp isKindOfClass:NSNumber.class]) continue;
|
|
166
|
+
NSTimeInterval at = [stamp doubleValue] / 1000.0;
|
|
167
|
+
if (at < since) continue;
|
|
168
|
+
NSString *fromName = BridgeSafeString(from[@"name"], 40);
|
|
169
|
+
NSString *toName = BridgeSafeString(to[@"name"], 40);
|
|
170
|
+
NSString *toSession = BridgeSafeString(to[@"session"], 128);
|
|
171
|
+
if (fromName.length == 0 || toName.length == 0 || toSession.length == 0) continue;
|
|
172
|
+
// 只取元数据:receipt[@"body"] 不进入返回值。
|
|
173
|
+
[deliveries addObject:@{
|
|
174
|
+
@"id": BridgeSafeString(receipt[@"id"], 64),
|
|
175
|
+
@"from": fromName,
|
|
176
|
+
@"to": toName,
|
|
177
|
+
@"toSession": toSession,
|
|
178
|
+
@"at": @(at)
|
|
179
|
+
}];
|
|
180
|
+
}
|
|
181
|
+
[deliveries sortUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
|
|
182
|
+
return [right[@"at"] compare:left[@"at"]];
|
|
183
|
+
}];
|
|
184
|
+
if (deliveries.count > limit) return [deliveries subarrayWithRange:NSMakeRange(0, limit)];
|
|
185
|
+
return deliveries;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
NSDictionary<NSString *, NSNumber *> *CCBridgePendingCounts(void) {
|
|
189
|
+
NSString *inbox = [CCBridgeStateDirectory() stringByAppendingPathComponent:@"inbox"];
|
|
190
|
+
NSMutableDictionary<NSString *, NSNumber *> *counts = [NSMutableDictionary dictionary];
|
|
191
|
+
for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:inbox error:nil]) {
|
|
192
|
+
NSString *session = BridgeSafeString(name, 128);
|
|
193
|
+
if (session.length == 0 || ![session isEqualToString:name]) continue;
|
|
194
|
+
NSUInteger count = JSONFilesInDirectory([inbox stringByAppendingPathComponent:name]).count;
|
|
195
|
+
if (count > 0) counts[session] = @(count);
|
|
196
|
+
}
|
|
197
|
+
return counts;
|
|
198
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#import "CCPetsEvents.h"
|
|
2
2
|
#import "CCPetsPaths.h"
|
|
3
|
+
#import "CCPetsTerminalFocus.h"
|
|
3
4
|
#import <sys/stat.h>
|
|
4
5
|
#import <fcntl.h>
|
|
5
6
|
#import <unistd.h>
|
|
@@ -196,6 +197,8 @@ int RecordHookEvent(void) {
|
|
|
196
197
|
@"timestamp": @([NSDate.date timeIntervalSince1970])
|
|
197
198
|
} mutableCopy];
|
|
198
199
|
if (session.length > 0) record[@"session"] = session;
|
|
200
|
+
NSDictionary *terminal = TerminalFocusTargetFromEnvironment();
|
|
201
|
+
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
199
202
|
AppendAgentEventRecord(record);
|
|
200
203
|
return EXIT_SUCCESS;
|
|
201
204
|
}
|
|
@@ -233,14 +236,16 @@ int RecordProviderEvent(void) {
|
|
|
233
236
|
@"failed": @"StopFailure",
|
|
234
237
|
@"notification": @"Notification"
|
|
235
238
|
};
|
|
236
|
-
|
|
239
|
+
NSMutableDictionary *record = [@{
|
|
237
240
|
@"event": events[state],
|
|
238
241
|
@"tool": tool,
|
|
239
242
|
@"failed": @([state isEqualToString:@"failed"] || [state isEqualToString:@"tool_failed"]),
|
|
240
243
|
@"provider": provider,
|
|
241
244
|
@"state": state,
|
|
242
245
|
@"timestamp": @([NSDate.date timeIntervalSince1970])
|
|
243
|
-
};
|
|
246
|
+
} mutableCopy];
|
|
247
|
+
NSDictionary *terminal = TerminalFocusTargetFromEnvironment();
|
|
248
|
+
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
244
249
|
if (!AppendAgentEventRecord(record)) {
|
|
245
250
|
fprintf(stderr, "无法写入 Provider 事件。\n");
|
|
246
251
|
return EXIT_FAILURE;
|
|
@@ -39,6 +39,8 @@ extern NSString *const HistoryEnabledKey;
|
|
|
39
39
|
extern NSString *const NotificationCompletionKey;
|
|
40
40
|
extern NSString *const NotificationFailureKey;
|
|
41
41
|
extern NSString *const NotificationApprovalKey;
|
|
42
|
+
// Agent 卡在审批或思考态超过阈值时的提醒开关。
|
|
43
|
+
extern NSString *const NotificationStallKey;
|
|
42
44
|
extern NSString *const StatusBubbleExpandedKey;
|
|
43
45
|
extern NSString *const StatusBubblePreferenceV2Key;
|
|
44
46
|
extern NSString *const ImportCodexPetsKey;
|
|
@@ -55,6 +57,8 @@ extern NSString *const DetectedProvidersKey;
|
|
|
55
57
|
extern const NSTimeInterval AgentStatusInactivityInterval;
|
|
56
58
|
extern const NSTimeInterval AgentStatusOrphanInterval;
|
|
57
59
|
extern const NSTimeInterval AgentStartingGraceInterval;
|
|
60
|
+
extern const NSTimeInterval AgentApprovalStallInterval;
|
|
61
|
+
extern const NSTimeInterval AgentThinkingStallInterval;
|
|
58
62
|
extern const NSTimeInterval UpdateRetryDelay;
|
|
59
63
|
extern const NSTimeInterval UsageRefreshIntervalVisible;
|
|
60
64
|
extern const NSTimeInterval UsageRefreshIntervalHidden;
|
|
@@ -213,6 +213,7 @@ NSString *const HistoryEnabledKey = @"CCPetsQuotaHistoryEnabled";
|
|
|
213
213
|
NSString *const NotificationCompletionKey = @"CCPetsNotifyCompletion";
|
|
214
214
|
NSString *const NotificationFailureKey = @"CCPetsNotifyFailure";
|
|
215
215
|
NSString *const NotificationApprovalKey = @"CCPetsNotifyApproval";
|
|
216
|
+
NSString *const NotificationStallKey = @"CCPetsNotifyStall";
|
|
216
217
|
NSString *const StatusBubbleExpandedKey = @"CCPetsStatusBubbleExpanded";
|
|
217
218
|
NSString *const StatusBubblePreferenceV2Key = @"CCPetsStatusBubblePreferenceV2";
|
|
218
219
|
NSString *const ImportCodexPetsKey = @"CCPetsImportCodexPets";
|
|
@@ -230,6 +231,11 @@ const NSTimeInterval AgentStatusOrphanInterval = 10.0;
|
|
|
230
231
|
// "正在启动"只在会话拉起的一瞬间成立。超过这段还没有任何后续事件,说明会话已经就绪、
|
|
231
232
|
// 正在等用户输入,气泡应当落到"待机中",否则启动态和空闲态看起来完全一样。
|
|
232
233
|
const NSTimeInterval AgentStartingGraceInterval = 8.0;
|
|
234
|
+
// 审批和思考是唯二会长时间停住不动的状态:窗口切走之后没人知道 Agent 早就停在
|
|
235
|
+
// 那儿了。超过这两个阈值就主动提醒一次,会话有新事件后重新武装。审批是纯粹在等
|
|
236
|
+
// 人,给得短;思考本来就可能跑很久,给得长,免得正常的长任务被当成卡死。
|
|
237
|
+
const NSTimeInterval AgentApprovalStallInterval = 120.0;
|
|
238
|
+
const NSTimeInterval AgentThinkingStallInterval = 300.0;
|
|
233
239
|
// 刚发布的版本存在 registry 传播竞态,更新失败后等这么久再自动重试一次。
|
|
234
240
|
const NSTimeInterval UpdateRetryDelay = 5.0;
|
|
235
241
|
// 额度变化由 FSEvents/VNODE 主动推送;定时器只保留为监听不可用时的 120 秒兜底。
|
|
@@ -24,8 +24,15 @@ static NSDictionary *HistoryProviderSample(NSDictionary *usage, NSString *usedKe
|
|
|
24
24
|
NSNumber *fiveHour = RemainingValue(usage, @"fiveHour", usedKey);
|
|
25
25
|
NSNumber *week = RemainingValue(usage, @"week", usedKey);
|
|
26
26
|
// usage_limit_exceeded 没有窗口归属,既不能证明两个窗口都归零,也不能证明上次快照
|
|
27
|
-
//
|
|
28
|
-
|
|
27
|
+
// 仍代表当前额度。但"受限"和"没有可信百分比"是两件事:受限期间官方 rate_limits
|
|
28
|
+
// 照样会返回窗口百分比(见 CCPetsCodexRateLimits.m 的 LiveUsageFromRateLimitBucket,
|
|
29
|
+
// 受限分支之前就已经填好 fiveHour / week),那是服务端给的真实数值。一律丢掉的话,
|
|
30
|
+
// 长期受限的 provider 在 7 天里攒不下一个历史点,趋势曲线就只剩"当前"这一个点,
|
|
31
|
+
// 画不出线。所以只在快照比受限时刻更旧时才丢——判据与 CCPetsUsage.m 的
|
|
32
|
+
// ExhaustionStillHolds 一致:exhaustedAt < sampledAt 说明快照更新、仍然可信。
|
|
33
|
+
NSNumber *exhaustedAt = [usage[@"exhaustedAt"] isKindOfClass:NSNumber.class]
|
|
34
|
+
? usage[@"exhaustedAt"] : nil;
|
|
35
|
+
if (exhaustedAt && exhaustedAt.doubleValue >= [usage[@"sampledAt"] doubleValue]) {
|
|
29
36
|
fiveHour = nil;
|
|
30
37
|
week = nil;
|
|
31
38
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
// 包装脚本在 Agent 启动时固定下来的终端身份。Hook 子进程继承这些环境变量,
|
|
4
|
+
// 因此任务结束时即使用户已经切到别的应用,也不会把目标错记成当前前台窗口。
|
|
5
|
+
NSDictionary *TerminalFocusTargetFromEnvironment(void);
|
|
6
|
+
|
|
7
|
+
// Terminal / iTerm2 按 TTY 精确选中 tab/session;其他终端在无扩展模式下激活应用。
|
|
8
|
+
BOOL ActivateTerminalFocusTarget(NSDictionary *target);
|
|
9
|
+
|
|
10
|
+
// 供包装脚本在 exec Agent 之前捕获承载终端的应用。
|
|
11
|
+
NSString *FrontmostApplicationBundleIdentifier(void);
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
#import "CCPetsTerminalFocus.h"
|
|
2
|
+
#import "CCPetsEvents.h"
|
|
3
|
+
#import <sys/sysctl.h>
|
|
4
|
+
#import <sys/stat.h>
|
|
5
|
+
#import <stdlib.h>
|
|
6
|
+
#import <unistd.h>
|
|
7
|
+
|
|
8
|
+
static BOOL ProcessInfoForPID(pid_t pid, struct kinfo_proc *info) {
|
|
9
|
+
if (pid <= 0) return NO;
|
|
10
|
+
size_t length = sizeof(*info);
|
|
11
|
+
int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
|
|
12
|
+
return sysctl(name, 4, info, &length, NULL, 0) == 0 && length > 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 包装脚本没被走到时(用户直接跑 codex / claude,或终端窗口早于 shim 安装就已打开)
|
|
16
|
+
// 环境里没有 CC_PETS_TERMINAL_*。记录端自己是 Agent 的子进程,控制终端就是 Agent
|
|
17
|
+
// 所在的那个 tty,直接问内核即可——stdin 是 JSON 管道,isatty 这类办法在这里没用。
|
|
18
|
+
static NSString *ControllingTerminalName(void) {
|
|
19
|
+
struct kinfo_proc info;
|
|
20
|
+
if (!ProcessInfoForPID(getpid(), &info)) return @"";
|
|
21
|
+
dev_t device = info.kp_eproc.e_tdev;
|
|
22
|
+
if (device == NODEV) return @"";
|
|
23
|
+
const char *tty = devname(device, S_IFCHR);
|
|
24
|
+
return tty ? @(tty).lastPathComponent : @"";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 承载终端的应用:沿父进程链往上走,第一个能被 NSRunningApplication 认领的就是
|
|
28
|
+
// GUI 应用本体(Ghostty / Terminal / iTerm2 …)。不能用"当前前台应用"兜底——
|
|
29
|
+
// Hook 触发时用户往往已经切到别的窗口,那样会把跳转目标记错。
|
|
30
|
+
static NSString *HostApplicationBundleIdentifier(void) {
|
|
31
|
+
pid_t pid = getppid();
|
|
32
|
+
for (NSUInteger depth = 0; depth < 16 && pid > 1; depth++) {
|
|
33
|
+
NSString *bundleID = [NSRunningApplication
|
|
34
|
+
runningApplicationWithProcessIdentifier:pid].bundleIdentifier;
|
|
35
|
+
if (bundleID.length > 0) return bundleID;
|
|
36
|
+
struct kinfo_proc info;
|
|
37
|
+
if (!ProcessInfoForPID(pid, &info)) break;
|
|
38
|
+
pid = info.kp_eproc.e_ppid;
|
|
39
|
+
}
|
|
40
|
+
return @"";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static NSString *TerminalTargetValue(NSDictionary<NSString *, NSString *> *environment,
|
|
44
|
+
NSString *key, NSUInteger maximumLength) {
|
|
45
|
+
return SanitizedShortString(environment[key], maximumLength);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
NSDictionary *TerminalFocusTargetFromEnvironment(void) {
|
|
49
|
+
NSDictionary<NSString *, NSString *> *environment = NSProcessInfo.processInfo.environment;
|
|
50
|
+
NSString *tty = environment[@"CC_PETS_TERMINAL_TTY"];
|
|
51
|
+
if ([tty isKindOfClass:NSString.class]) tty = tty.lastPathComponent;
|
|
52
|
+
tty = SanitizedShortString(tty, 64);
|
|
53
|
+
NSString *program = TerminalTargetValue(environment, @"CC_PETS_TERMINAL_PROGRAM", 64);
|
|
54
|
+
NSString *session = TerminalTargetValue(environment, @"CC_PETS_TERMINAL_SESSION", 128);
|
|
55
|
+
NSString *bundleID = TerminalTargetValue(environment, @"CC_PETS_TERMINAL_BUNDLE_ID", 128);
|
|
56
|
+
if (tty.length == 0) tty = SanitizedShortString(ControllingTerminalName(), 64);
|
|
57
|
+
if (bundleID.length == 0) {
|
|
58
|
+
bundleID = SanitizedShortString(HostApplicationBundleIdentifier(), 128);
|
|
59
|
+
}
|
|
60
|
+
if (tty.length == 0 && program.length == 0 && session.length == 0 && bundleID.length == 0) {
|
|
61
|
+
return @{};
|
|
62
|
+
}
|
|
63
|
+
NSMutableDictionary *target = [NSMutableDictionary dictionary];
|
|
64
|
+
if (tty.length > 0) target[@"tty"] = tty;
|
|
65
|
+
if (program.length > 0) target[@"program"] = program;
|
|
66
|
+
if (session.length > 0) target[@"session"] = session;
|
|
67
|
+
if (bundleID.length > 0) target[@"bundleID"] = bundleID;
|
|
68
|
+
return target;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
NSString *FrontmostApplicationBundleIdentifier(void) {
|
|
72
|
+
return NSWorkspace.sharedWorkspace.frontmostApplication.bundleIdentifier ?: @"";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static BOOL RunTerminalSelectionScript(NSString *bundleID, NSString *ttyName) {
|
|
76
|
+
if (ttyName.length == 0) return NO;
|
|
77
|
+
NSString *tty = [@"/dev/" stringByAppendingString:ttyName.lastPathComponent];
|
|
78
|
+
NSString *source = nil;
|
|
79
|
+
if ([bundleID isEqualToString:@"com.apple.Terminal"]) {
|
|
80
|
+
source = [NSString stringWithFormat:
|
|
81
|
+
@"tell application \"Terminal\"\n"
|
|
82
|
+
"repeat with terminalWindow in windows\n"
|
|
83
|
+
"repeat with terminalTab in tabs of terminalWindow\n"
|
|
84
|
+
"if (tty of terminalTab) is \"%@\" then\n"
|
|
85
|
+
"set selected tab of terminalWindow to terminalTab\n"
|
|
86
|
+
"set index of terminalWindow to 1\n"
|
|
87
|
+
"activate\n"
|
|
88
|
+
"return true\n"
|
|
89
|
+
"end if\n"
|
|
90
|
+
"end repeat\n"
|
|
91
|
+
"end repeat\n"
|
|
92
|
+
"end tell\n"
|
|
93
|
+
"return false", tty];
|
|
94
|
+
} else if ([bundleID isEqualToString:@"com.googlecode.iterm2"]) {
|
|
95
|
+
source = [NSString stringWithFormat:
|
|
96
|
+
@"tell application \"iTerm2\"\n"
|
|
97
|
+
"repeat with terminalWindow in windows\n"
|
|
98
|
+
"repeat with terminalTab in tabs of terminalWindow\n"
|
|
99
|
+
"repeat with terminalSession in sessions of terminalTab\n"
|
|
100
|
+
"if (tty of terminalSession) is \"%@\" then\n"
|
|
101
|
+
"select terminalSession\n"
|
|
102
|
+
"select terminalWindow\n"
|
|
103
|
+
"activate\n"
|
|
104
|
+
"return true\n"
|
|
105
|
+
"end if\n"
|
|
106
|
+
"end repeat\n"
|
|
107
|
+
"end repeat\n"
|
|
108
|
+
"end repeat\n"
|
|
109
|
+
"end tell\n"
|
|
110
|
+
"return false", tty];
|
|
111
|
+
}
|
|
112
|
+
if (!source) return NO;
|
|
113
|
+
NSDictionary *error = nil;
|
|
114
|
+
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];
|
|
115
|
+
NSAppleEventDescriptor *result = [script executeAndReturnError:&error];
|
|
116
|
+
return result.booleanValue && error == nil;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static NSString *BundleIDForTerminalProgram(NSString *program) {
|
|
120
|
+
NSString *lower = program.lowercaseString;
|
|
121
|
+
if ([lower isEqualToString:@"apple_terminal"]) return @"com.apple.Terminal";
|
|
122
|
+
if ([lower containsString:@"iterm"]) return @"com.googlecode.iterm2";
|
|
123
|
+
if ([lower isEqualToString:@"vscode"]) return @"com.microsoft.VSCode";
|
|
124
|
+
if ([lower containsString:@"warp"]) return @"dev.warp.Warp-Stable";
|
|
125
|
+
if ([lower containsString:@"wezterm"]) return @"com.github.wez.wezterm";
|
|
126
|
+
if ([lower containsString:@"ghostty"]) return @"com.mitchellh.ghostty";
|
|
127
|
+
return @"";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
BOOL ActivateTerminalFocusTarget(NSDictionary *target) {
|
|
131
|
+
if (![target isKindOfClass:NSDictionary.class]) return NO;
|
|
132
|
+
NSString *tty = SanitizedShortString(target[@"tty"], 64);
|
|
133
|
+
// 已知 TERM_PROGRAM 比“启动瞬间的前台应用”更可靠:VS Code Task 可能在窗口不位于
|
|
134
|
+
// 前台时启动 shell。JetBrains 等没有稳定统一 bundle ID 的宿主才使用捕获值兜底。
|
|
135
|
+
NSString *bundleID = BundleIDForTerminalProgram(
|
|
136
|
+
SanitizedShortString(target[@"program"], 64));
|
|
137
|
+
if (bundleID.length == 0) bundleID = SanitizedShortString(target[@"bundleID"], 128);
|
|
138
|
+
if (bundleID.length == 0) return NO;
|
|
139
|
+
if (RunTerminalSelectionScript(bundleID, tty)) return YES;
|
|
140
|
+
|
|
141
|
+
NSArray<NSRunningApplication *> *applications =
|
|
142
|
+
[NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID];
|
|
143
|
+
NSRunningApplication *application = applications.firstObject;
|
|
144
|
+
if (!application) return NO;
|
|
145
|
+
#pragma clang diagnostic push
|
|
146
|
+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
147
|
+
return [application activateWithOptions:
|
|
148
|
+
NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps];
|
|
149
|
+
#pragma clang diagnostic pop
|
|
150
|
+
}
|
package/Sources/CCPets/PetView.m
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#import "MenuToggleSwitch.h"
|
|
4
4
|
#import "CCPetsPaths.h"
|
|
5
5
|
#import "CCPetsImageLoader.h"
|
|
6
|
+
#import "CCPetsBridge.h"
|
|
6
7
|
|
|
7
8
|
// 碎碎念频率档位的 defaults 键。定义在 CCPetsAppDelegate.m,这里只读不写;
|
|
8
9
|
// 单独 extern 而不 import 那个头文件,是因为它反过来 import 了 PetView.h。
|
|
@@ -1466,6 +1467,88 @@ typedef NS_ENUM(NSInteger, PetMicroBehaviorKind) {
|
|
|
1466
1467
|
[menu addItem:item];
|
|
1467
1468
|
return item;
|
|
1468
1469
|
}
|
|
1470
|
+
// CC Bridge 开关组。状态每次打开菜单时现读(开关文件 / 选项),不在视图里缓存。
|
|
1471
|
+
// 除"消息角标 / 新消息通知"这两项只影响桌宠外,其余开关都通过 cc-pets bridge enable / configure
|
|
1472
|
+
// 落到 Claude Code / Codex 的配置里;bridge 未开启时它们置灰。
|
|
1473
|
+
- (void)addBridgeSwitchToMenu:(NSMenu *)menu title:(NSString *)title checked:(BOOL)checked
|
|
1474
|
+
action:(SEL)action tag:(NSInteger)tag enabled:(BOOL)enabled toolTip:(NSString *)toolTip {
|
|
1475
|
+
// 与其他子菜单同宽。菜单宽度取最宽的一行:标题都控制在 5 个字以内,说明性文字放进悬停提示,
|
|
1476
|
+
// 否则长文字行会把菜单撑宽,而自绘开关行是固定宽度,开关就不再贴右边。
|
|
1477
|
+
NSMenuItem *item = [self addPersistentSwitchToMenu:menu title:title checked:checked
|
|
1478
|
+
action:action width:PetSubmenuRowWidth tag:tag];
|
|
1479
|
+
item.view.toolTip = toolTip;
|
|
1480
|
+
for (NSView *subview in item.view.subviews) {
|
|
1481
|
+
if ([subview isKindOfClass:MenuToggleSwitch.class]) ((MenuToggleSwitch *)subview).enabled = enabled;
|
|
1482
|
+
if ([subview isKindOfClass:NSTextField.class] && !enabled) {
|
|
1483
|
+
((NSTextField *)subview).textColor = NSColor.disabledControlTextColor;
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
- (void)addBridgeMenuToMenu:(NSMenu *)menu {
|
|
1488
|
+
NSMenuItem *bridgeItem = [menu addItemWithTitle:@"CC Bridge" action:nil keyEquivalent:@""];
|
|
1489
|
+
NSMenu *bridgeMenu = [NSMenu new];
|
|
1490
|
+
BOOL enabled = CCBridgeEnabled();
|
|
1491
|
+
NSDictionary *options = CCBridgeOptions();
|
|
1492
|
+
[self addBridgeSwitchToMenu:bridgeMenu title:@"启用" checked:enabled
|
|
1493
|
+
action:@selector(toggleBridgeEnabled:) tag:0 enabled:YES
|
|
1494
|
+
toolTip:@"让本机 Claude Code / Codex 会话互相发消息、唤醒对方、预留文件"];
|
|
1495
|
+
|
|
1496
|
+
[bridgeMenu addItem:NSMenuItem.separatorItem];
|
|
1497
|
+
NSMenuItem *approvalHeader = [bridgeMenu addItemWithTitle:@"免审批" action:nil keyEquivalent:@""];
|
|
1498
|
+
approvalHeader.enabled = NO;
|
|
1499
|
+
approvalHeader.toolTip = @"每组同时作用于 Codex 免审批与 Claude 免确认;Codex 需重启会话生效";
|
|
1500
|
+
NSArray<NSDictionary *> *groups = @[
|
|
1501
|
+
@{@"title": @"查看类", @"group": @"view", @"tag": @1,
|
|
1502
|
+
@"tip": @"列出会话、查看预留、读取信箱免审批;只读,放开没有风险"},
|
|
1503
|
+
@{@"title": @"发消息", @"group": @"send", @"tag": @2,
|
|
1504
|
+
@"tip": @"开启后,Agent 可以不经确认给其他会话发消息"},
|
|
1505
|
+
@{@"title": @"文件预留", @"group": @"reserve", @"tag": @3,
|
|
1506
|
+
@"tip": @"预留 / 释放文件免审批"},
|
|
1507
|
+
@{@"title": @"改会话名", @"group": @"name", @"tag": @4,
|
|
1508
|
+
@"tip": @"修改当前会话在 CC Bridge 中的名字免审批"}
|
|
1509
|
+
];
|
|
1510
|
+
for (NSDictionary *group in groups) {
|
|
1511
|
+
NSArray<NSString *> *tools = CCBridgeToolGroup(group[@"group"]);
|
|
1512
|
+
NSSet *codex = [NSSet setWithArray:options[@"codexApprove"]];
|
|
1513
|
+
NSSet *claude = [NSSet setWithArray:options[@"claudeAllow"]];
|
|
1514
|
+
BOOL checked = tools.count > 0 && [[NSSet setWithArray:tools] isSubsetOfSet:codex] &&
|
|
1515
|
+
[[NSSet setWithArray:tools] isSubsetOfSet:claude];
|
|
1516
|
+
[self addBridgeSwitchToMenu:bridgeMenu title:group[@"title"] checked:checked
|
|
1517
|
+
action:@selector(toggleBridgeApproval:) tag:[group[@"tag"] integerValue] enabled:enabled
|
|
1518
|
+
toolTip:group[@"tip"]];
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
[bridgeMenu addItem:NSMenuItem.separatorItem];
|
|
1522
|
+
NSMenuItem *behaviorHeader = [bridgeMenu addItemWithTitle:@"行为" action:nil keyEquivalent:@""];
|
|
1523
|
+
behaviorHeader.enabled = NO;
|
|
1524
|
+
[self addBridgeSwitchToMenu:bridgeMenu title:@"自动唤醒" checked:[options[@"wake"] boolValue]
|
|
1525
|
+
action:@selector(toggleBridgeWake:) tag:0 enabled:enabled
|
|
1526
|
+
toolTip:@"自动唤醒空闲会话。关闭后消息只进信箱,等对方下次收到你的输入时带入,可省 token;Codex 需重启会话生效"];
|
|
1527
|
+
[self addBridgeSwitchToMenu:bridgeMenu title:@"编辑拦截" checked:[options[@"editGuard"] boolValue]
|
|
1528
|
+
action:@selector(toggleBridgeEditGuard:) tag:0 enabled:enabled
|
|
1529
|
+
toolTip:@"编辑他人预留的文件时先暂停一次并说明原因,重试即放行;Codex 需重启会话生效"];
|
|
1530
|
+
[self addBridgeSwitchToMenu:bridgeMenu title:@"消息角标"
|
|
1531
|
+
checked:[NSUserDefaults.standardUserDefaults boolForKey:BridgeBadgeEnabledKey]
|
|
1532
|
+
action:@selector(toggleBridgeBadge:) tag:0 enabled:YES
|
|
1533
|
+
toolTip:@"状态图标右下角:蓝色为新送达,橙色为信箱积压"];
|
|
1534
|
+
[self addBridgeSwitchToMenu:bridgeMenu title:@"新消息通知"
|
|
1535
|
+
checked:[NSUserDefaults.standardUserDefaults boolForKey:BridgeNotificationKey]
|
|
1536
|
+
action:@selector(toggleBridgeNotification:) tag:0 enabled:YES
|
|
1537
|
+
toolTip:@"会话之间有消息送达时发一条系统通知(只含谁发给谁,不含正文)"];
|
|
1538
|
+
|
|
1539
|
+
[bridgeMenu addItem:NSMenuItem.separatorItem];
|
|
1540
|
+
NSString *status = @"未开启";
|
|
1541
|
+
if (enabled) {
|
|
1542
|
+
NSDictionary *sessions = CCBridgeSessions();
|
|
1543
|
+
NSUInteger reservations = CCBridgeActiveReservationCount([NSSet setWithArray:sessions.allKeys]);
|
|
1544
|
+
status = [NSString stringWithFormat:@"%lu 会话 · %lu 预留",
|
|
1545
|
+
(unsigned long)sessions.count, (unsigned long)reservations];
|
|
1546
|
+
}
|
|
1547
|
+
NSMenuItem *statusItem = [bridgeMenu addItemWithTitle:status action:nil keyEquivalent:@""];
|
|
1548
|
+
statusItem.enabled = NO;
|
|
1549
|
+
statusItem.toolTip = @"在线会话数 · 有效的文件预留数";
|
|
1550
|
+
bridgeItem.submenu = bridgeMenu;
|
|
1551
|
+
}
|
|
1469
1552
|
- (void)rightMouseDown:(NSEvent *)event {
|
|
1470
1553
|
NSArray<NSDictionary *> *availablePets = self.petOptionsRequested ? self.petOptionsRequested() : @[];
|
|
1471
1554
|
NSMenu *menu = [NSMenu new];
|
|
@@ -1505,7 +1588,8 @@ typedef NS_ENUM(NSInteger, PetMicroBehaviorKind) {
|
|
|
1505
1588
|
NSArray<NSDictionary *> *notificationOptions = @[
|
|
1506
1589
|
@{@"title": @"任务完成", @"key": NotificationCompletionKey, @"tag": @1},
|
|
1507
1590
|
@{@"title": @"任务失败", @"key": NotificationFailureKey, @"tag": @2},
|
|
1508
|
-
@{@"title": @"等待审批", @"key": NotificationApprovalKey, @"tag": @3}
|
|
1591
|
+
@{@"title": @"等待审批", @"key": NotificationApprovalKey, @"tag": @3},
|
|
1592
|
+
@{@"title": @"响应超时", @"key": NotificationStallKey, @"tag": @4}
|
|
1509
1593
|
];
|
|
1510
1594
|
for (NSDictionary *option in notificationOptions) {
|
|
1511
1595
|
[self addPersistentSwitchToMenu:notificationMenu
|
|
@@ -1516,6 +1600,7 @@ typedef NS_ENUM(NSInteger, PetMicroBehaviorKind) {
|
|
|
1516
1600
|
tag:[option[@"tag"] integerValue]];
|
|
1517
1601
|
}
|
|
1518
1602
|
notificationItem.submenu = notificationMenu;
|
|
1603
|
+
[self addBridgeMenuToMenu:menu];
|
|
1519
1604
|
NSMenuItem *interactionItem = [menu addItemWithTitle:@"连击互动" action:nil keyEquivalent:@""];
|
|
1520
1605
|
NSMenu *interactionMenu = [NSMenu new];
|
|
1521
1606
|
[self addPersistentSwitchToMenu:interactionMenu
|
|
@@ -758,11 +758,24 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
758
758
|
? self.codexHistory : self.claudeHistory;
|
|
759
759
|
NSNumber *currentUsed = weekUsed;
|
|
760
760
|
NSArray<NSDictionary *> *points = [self curvePointsFromHistory:history currentUsed:currentUsed];
|
|
761
|
-
|
|
762
|
-
|
|
761
|
+
// 趋势这一列算的是 7 天窗口(currentUsed 取 weekUsed)。而 exhaustedAt 来自
|
|
762
|
+
// rateLimitReachedType,没有窗口归属,实际撞墙的通常是 5 小时窗口——拿它一票否决
|
|
763
|
+
// 整列,会出现左边 7 天窗口好好显示着 45% 剩余、右边却说"等待官方额度刷新"的自相
|
|
764
|
+
// 矛盾。所以只有 7 天窗口自己确实没有百分比时才退回"待刷新"。
|
|
765
|
+
BOOL weekUnknown = exhausted && currentUsed == nil;
|
|
766
|
+
NSDictionary *pace = weekUnknown
|
|
767
|
+
? @{ @"label": @"待刷新", @"tip": @"等待官方额度刷新",
|
|
763
768
|
@"color": NSColor.systemOrangeColor }
|
|
764
769
|
: [self paceStatusForQuota:week currentUsed:currentUsed color:color];
|
|
765
770
|
NSColor *paceColor = pace[@"color"];
|
|
771
|
+
// 档位讲的是 7 天趋势,脚注讲的是此刻为什么发不出请求:受限但 7 天窗口尚有余量时
|
|
772
|
+
// 两者都要说清,所以脚注单独取色,不跟着档位走。
|
|
773
|
+
NSString *paceTip = pace[@"tip"];
|
|
774
|
+
NSColor *tipColor = paceColor;
|
|
775
|
+
if (exhausted && !weekUnknown) {
|
|
776
|
+
paceTip = @"官方额度受限,等待重置";
|
|
777
|
+
tipColor = NSColor.systemOrangeColor;
|
|
778
|
+
}
|
|
766
779
|
NSDictionary *pillAttributes = [self textAttributesWithSize:11 color:paceColor
|
|
767
780
|
weight:NSFontWeightSemibold];
|
|
768
781
|
// 档位标签跟在标题后面。宽度仍按文字算:四个档位不一样长,最宽的"数据不足"到
|
|
@@ -786,8 +799,8 @@ NSImage *OfficialAppIcon(NSString *bundleIdentifier, NSString *resourceName) {
|
|
|
786
799
|
withAttributes:[self rightAlignedTextAttributesWithSize:10 color:secondary
|
|
787
800
|
weight:NSFontWeightRegular]];
|
|
788
801
|
// 与左边两列的"已用 Token"同一条基线(+22),也和 API 卡的脚注行对齐。
|
|
789
|
-
[
|
|
790
|
-
withAttributes:[self textAttributesWithSize:11 color:
|
|
802
|
+
[paceTip drawInRect:NSMakeRect(trendX, NSMinY(card) + 22, 145, 18)
|
|
803
|
+
withAttributes:[self textAttributesWithSize:11 color:tipColor weight:NSFontWeightMedium]];
|
|
791
804
|
}
|
|
792
805
|
- (void)drawRect:(NSRect)dirtyRect {
|
|
793
806
|
[NSGraphicsContext saveGraphicsState];
|