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,215 @@
|
|
|
1
|
+
// agent 事件流读取与客户端存活检测。
|
|
2
|
+
#import "CCPetsAppDelegate+Private.h"
|
|
3
|
+
|
|
4
|
+
// 客户端是否真的还在跑。只问 kill(pid, 0) 不够:关掉终端窗口只是销毁 pty,claude /
|
|
5
|
+
// codex(Node 进程)不一定跟着 SIGHUP 退出,会变成脱离控制终端的孤儿继续活着。那样
|
|
6
|
+
// pid 一直存在,会话就永久挂在"在线"上,最近会话列表里那条也永远不消失。
|
|
7
|
+
// 所以再看两件事:进程是否还有控制终端;以及它是不是仍然是 pid 文件里记的那个 TTY
|
|
8
|
+
// (pty 会被新开的窗口复用,光看"有 tty"挡不住换了主人的情况)。
|
|
9
|
+
// recordedTTY 为空是 1.0.2 及更早的老客户端,只能退回"有控制终端"这一条。
|
|
10
|
+
static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
|
|
11
|
+
if (pid <= 1) return NO;
|
|
12
|
+
struct kinfo_proc info;
|
|
13
|
+
size_t length = sizeof(info);
|
|
14
|
+
int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
|
|
15
|
+
if (sysctl(name, 4, &info, &length, NULL, 0) != 0 || length == 0) return NO;
|
|
16
|
+
if (info.kp_proc.p_stat == SZOMB) return NO;
|
|
17
|
+
dev_t device = info.kp_eproc.e_tdev;
|
|
18
|
+
if (device == NODEV) return NO;
|
|
19
|
+
if (recordedTTY.length == 0) return YES;
|
|
20
|
+
const char *current = devname(device, S_IFCHR);
|
|
21
|
+
if (!current) return NO;
|
|
22
|
+
return [recordedTTY isEqualToString:@(current).lastPathComponent];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@implementation AppDelegate (AgentEvents)
|
|
26
|
+
- (void)prepareAgentEventReader {
|
|
27
|
+
NSString *path = AgentEventPath();
|
|
28
|
+
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
|
|
29
|
+
unsigned long long size = [attributes[NSFileSize] unsignedLongLongValue];
|
|
30
|
+
if (self.agentEventReaderInitialized) {
|
|
31
|
+
if (size < self.agentEventOffset) {
|
|
32
|
+
self.agentEventOffset = 0;
|
|
33
|
+
[self.agentEventPartialLine setLength:0];
|
|
34
|
+
}
|
|
35
|
+
[self readNewAgentEvents];
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
self.agentEventReaderInitialized = YES;
|
|
39
|
+
self.agentEventOffset = size;
|
|
40
|
+
if (size > 0) {
|
|
41
|
+
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
|
|
42
|
+
unsigned long long start = size > 65536 ? size - 65536 : 0;
|
|
43
|
+
[handle seekToFileOffset:start];
|
|
44
|
+
NSData *recent = [handle readDataToEndOfFile];
|
|
45
|
+
[handle closeFile];
|
|
46
|
+
[self processAgentEventData:recent ignoreFirstPartial:start > 0 recentOnly:YES];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
- (void)consumeAgentEventData:(NSData *)data {
|
|
50
|
+
if (data.length == 0) return;
|
|
51
|
+
[self.agentEventPartialLine appendData:data];
|
|
52
|
+
const uint8_t *bytes = self.agentEventPartialLine.bytes;
|
|
53
|
+
NSUInteger lineStart = 0;
|
|
54
|
+
for (NSUInteger index = 0; index < self.agentEventPartialLine.length; index++) {
|
|
55
|
+
if (bytes[index] != '\n') continue;
|
|
56
|
+
NSData *lineData = [self.agentEventPartialLine subdataWithRange:NSMakeRange(lineStart, index - lineStart)];
|
|
57
|
+
[self processAgentEventData:lineData ignoreFirstPartial:NO recentOnly:NO];
|
|
58
|
+
lineStart = index + 1;
|
|
59
|
+
}
|
|
60
|
+
if (lineStart > 0) {
|
|
61
|
+
NSData *remainder = [self.agentEventPartialLine subdataWithRange:
|
|
62
|
+
NSMakeRange(lineStart, self.agentEventPartialLine.length - lineStart)];
|
|
63
|
+
self.agentEventPartialLine = [remainder mutableCopy];
|
|
64
|
+
}
|
|
65
|
+
if (self.agentEventPartialLine.length > 1024 * 1024) [self.agentEventPartialLine setLength:0];
|
|
66
|
+
}
|
|
67
|
+
- (void)processAgentEventData:(NSData *)data ignoreFirstPartial:(BOOL)ignoreFirstPartial recentOnly:(BOOL)recentOnly {
|
|
68
|
+
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
69
|
+
if (!text) return;
|
|
70
|
+
NSArray<NSString *> *lines = [text componentsSeparatedByString:@"\n"];
|
|
71
|
+
NSTimeInterval cutoff = [NSDate.date timeIntervalSince1970] - 5;
|
|
72
|
+
for (NSUInteger index = 0; index < lines.count; index++) {
|
|
73
|
+
if (ignoreFirstPartial && index == 0) continue;
|
|
74
|
+
NSData *lineData = [lines[index] dataUsingEncoding:NSUTF8StringEncoding];
|
|
75
|
+
if (lineData.length == 0) continue;
|
|
76
|
+
NSDictionary *record = [NSJSONSerialization JSONObjectWithData:lineData options:0 error:nil];
|
|
77
|
+
if (![record isKindOfClass:NSDictionary.class]) continue;
|
|
78
|
+
if (recentOnly && [record[@"timestamp"] doubleValue] < cutoff) continue;
|
|
79
|
+
[self trackAgentSessionRecord:record];
|
|
80
|
+
NSString *event = record[@"event"];
|
|
81
|
+
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
82
|
+
if ([event isKindOfClass:NSString.class]) {
|
|
83
|
+
if (!self.pendingApprovalRecords) {
|
|
84
|
+
self.pendingApprovalRecords = [NSMutableDictionary dictionary];
|
|
85
|
+
}
|
|
86
|
+
[self prunePendingApprovalRecords];
|
|
87
|
+
NSString *approvalKey = [self approvalKeyForRecord:record];
|
|
88
|
+
BOOL manualApproval = [state isEqualToString:@"approval"];
|
|
89
|
+
if (manualApproval) {
|
|
90
|
+
self.pendingApprovalRecords[approvalKey] = record;
|
|
91
|
+
} else {
|
|
92
|
+
[self.pendingApprovalRecords removeObjectForKey:approvalKey];
|
|
93
|
+
}
|
|
94
|
+
[self prunePendingApprovalRecords];
|
|
95
|
+
// 多会话下不能让一个未处理的审批全局挡住其他 Agent 的所有 Hook。每条事件
|
|
96
|
+
// 都正常驱动气泡和动画;仍在等待的审批保留在多会话记录中供用户回跳。
|
|
97
|
+
[self displayAgentRecord:record notify:YES];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
[self refreshApprovalBadge];
|
|
101
|
+
}
|
|
102
|
+
- (void)readNewAgentEvents {
|
|
103
|
+
NSString *path = AgentEventPath();
|
|
104
|
+
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
|
|
105
|
+
unsigned long long size = [attributes[NSFileSize] unsignedLongLongValue];
|
|
106
|
+
if (size < self.agentEventOffset) self.agentEventOffset = 0;
|
|
107
|
+
if (size == self.agentEventOffset) return;
|
|
108
|
+
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
|
|
109
|
+
if (!handle) return;
|
|
110
|
+
unsigned long long start = self.agentEventOffset;
|
|
111
|
+
[handle seekToFileOffset:start];
|
|
112
|
+
NSData *data = [handle readDataToEndOfFile];
|
|
113
|
+
[handle closeFile];
|
|
114
|
+
self.agentEventOffset = start + data.length;
|
|
115
|
+
[self consumeAgentEventData:data];
|
|
116
|
+
}
|
|
117
|
+
- (void)startAgentEventReader {
|
|
118
|
+
if (self.agentEventSource) return;
|
|
119
|
+
NSString *path = AgentEventPath();
|
|
120
|
+
[NSFileManager.defaultManager createDirectoryAtPath:path.stringByDeletingLastPathComponent
|
|
121
|
+
withIntermediateDirectories:YES attributes:nil error:nil];
|
|
122
|
+
int creator = open(path.fileSystemRepresentation, O_CREAT | O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
|
123
|
+
if (creator >= 0) close(creator);
|
|
124
|
+
int descriptor = open(path.fileSystemRepresentation, O_EVTONLY | O_CLOEXEC);
|
|
125
|
+
if (descriptor < 0) return;
|
|
126
|
+
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, descriptor,
|
|
127
|
+
DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_DELETE |
|
|
128
|
+
DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
|
|
129
|
+
dispatch_get_main_queue());
|
|
130
|
+
if (!source) {
|
|
131
|
+
close(descriptor);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
self.agentEventSource = source;
|
|
135
|
+
__weak typeof(self) weakSelf = self;
|
|
136
|
+
dispatch_source_set_event_handler(source, ^{
|
|
137
|
+
typeof(self) strongSelf = weakSelf;
|
|
138
|
+
if (!strongSelf) return;
|
|
139
|
+
dispatch_source_t currentSource = strongSelf.agentEventSource;
|
|
140
|
+
if (!currentSource) return;
|
|
141
|
+
unsigned long flags = dispatch_source_get_data(currentSource);
|
|
142
|
+
[strongSelf readNewAgentEvents];
|
|
143
|
+
if (flags & (DISPATCH_VNODE_DELETE | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE)) {
|
|
144
|
+
strongSelf.agentEventSource = nil;
|
|
145
|
+
dispatch_source_cancel(currentSource);
|
|
146
|
+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100 * NSEC_PER_MSEC),
|
|
147
|
+
dispatch_get_main_queue(), ^{ [weakSelf startAgentEventReader]; });
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
dispatch_source_set_cancel_handler(source, ^{ close(descriptor); });
|
|
151
|
+
dispatch_resume(source);
|
|
152
|
+
[self prepareAgentEventReader];
|
|
153
|
+
}
|
|
154
|
+
- (void)ensureAgentEventReader:(id)sender {
|
|
155
|
+
if (!self.agentEventSource) [self startAgentEventReader];
|
|
156
|
+
}
|
|
157
|
+
- (void)refreshClientLifecycle:(id)sender {
|
|
158
|
+
[self considerIdleSpeech];
|
|
159
|
+
[self prunePendingApprovalRecords];
|
|
160
|
+
NSString *clientName = [NSString stringWithFormat:@"cc-pets-%u-clients", getuid()];
|
|
161
|
+
NSString *clientDirectory = [PetStateDirectory() stringByAppendingPathComponent:clientName];
|
|
162
|
+
NSArray<NSString *> *entries = [NSFileManager.defaultManager contentsOfDirectoryAtPath:clientDirectory error:nil] ?: @[];
|
|
163
|
+
NSInteger liveClients = 0;
|
|
164
|
+
NSMutableSet<NSString *> *providers = [NSMutableSet set];
|
|
165
|
+
NSMutableSet<NSString *> *sessionKeys = [NSMutableSet set];
|
|
166
|
+
BOOL unlabeled = NO;
|
|
167
|
+
for (NSString *entry in entries) {
|
|
168
|
+
pid_t pid = (pid_t)entry.intValue;
|
|
169
|
+
NSString *path = [clientDirectory stringByAppendingPathComponent:entry];
|
|
170
|
+
// 包装脚本会把 provider 名写进 pid 文件第一行、TTY 写进第二行。1.0.2 及更早
|
|
171
|
+
// 的版本只 touch 出空文件,升级后仍在运行的老客户端读出来是空的:这类当作
|
|
172
|
+
// “身份不明”,只要还有一个就不清场,避免把仍然活着的会话误判成已退出。
|
|
173
|
+
NSString *contents = [NSString stringWithContentsOfFile:path
|
|
174
|
+
encoding:NSUTF8StringEncoding error:nil] ?: @"";
|
|
175
|
+
NSArray<NSString *> *lines = [contents componentsSeparatedByCharactersInSet:
|
|
176
|
+
NSCharacterSet.newlineCharacterSet];
|
|
177
|
+
NSString *label = [lines.firstObject
|
|
178
|
+
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
179
|
+
NSString *tty = lines.count > 1 ? [lines[1]
|
|
180
|
+
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] : @"";
|
|
181
|
+
if (!ClientProcessAlive(pid, tty)) {
|
|
182
|
+
[NSFileManager.defaultManager removeItemAtPath:path error:nil];
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
liveClients += 1;
|
|
186
|
+
if (label.length > 0 && label.length <= 32) [providers addObject:label];
|
|
187
|
+
else unlabeled = YES;
|
|
188
|
+
NSString *sessionKey = [self onlineAgentSessionKeyForProvider:label tty:tty];
|
|
189
|
+
if (sessionKey.length > 0) [sessionKeys addObject:sessionKey];
|
|
190
|
+
}
|
|
191
|
+
// 包装脚本启动的客户端有精确的退出信号:pid 文件被回收。这一段不该被为"直接跑
|
|
192
|
+
// claude / codex"准备的 60 秒活跃度宽限盖住,否则退出后还要挂满一分钟才转离线。
|
|
193
|
+
// 因此某一家的 pid 文件一旦从有变无,立刻丢掉它的活跃度记录;从来没有过 pid 文件
|
|
194
|
+
// 的(直接启动)不受影响,继续走宽限。
|
|
195
|
+
NSSet<NSString *> *previousProviders = self.liveClientProviders;
|
|
196
|
+
self.liveClientCount = liveClients;
|
|
197
|
+
self.liveClientProviders = providers;
|
|
198
|
+
self.liveAgentSessionKeys = sessionKeys;
|
|
199
|
+
self.hasUnlabeledClient = unlabeled;
|
|
200
|
+
[self pruneOfflineAgentSessionRecords];
|
|
201
|
+
for (NSString *provider in previousProviders) {
|
|
202
|
+
if (![providers containsObject:provider]) {
|
|
203
|
+
[self.providerActivityAt removeObjectForKey:provider];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
[self updateQuotaLiveState];
|
|
207
|
+
[self refreshDetectedProviders];
|
|
208
|
+
[self hideAgentStatusIfClientGone];
|
|
209
|
+
[self checkStalledAgentSessions];
|
|
210
|
+
[self refreshApprovalBadge];
|
|
211
|
+
// 启动模式在应用生命周期内保持不变。手动启动的桌宠即使后来检测到
|
|
212
|
+
// Codex/Claude 客户端,也不应被转成 CLI 托管模式并随客户端退出。
|
|
213
|
+
if (liveClients == 0 && self.managedByCLI) [NSApp terminate:nil];
|
|
214
|
+
}
|
|
215
|
+
@end
|