cc-pets 2.4.0 → 2.5.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 +46 -9
- package/CC_BRIDGE.zh-CN.md +29 -6
- package/CHANGELOG.md +55 -0
- package/CHANGELOG.zh-CN.md +56 -1
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/PROVIDER_PROTOCOL.md +5 -1
- package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
- package/README.md +69 -16
- package/README.zh-CN.md +54 -19
- package/Resources/zh-Hans.lproj/Localizable.strings +35 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +47 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +109 -14
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +8 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +13 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +6 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +5 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +4 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +4 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.h +10 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.m +4 -2
- package/Sources/CCPets/Bridge/CCPetsBridge.m +34 -12
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +100 -33
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +12 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +317 -89
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +210 -95
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +28 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +222 -35
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +1 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +6 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +54 -5
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +9 -1
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +10 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +528 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +95 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +4 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +16 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +5 -3
- package/package.json +1 -1
|
@@ -40,6 +40,10 @@ static NSString *RandomHex(NSUInteger bytes) {
|
|
|
40
40
|
return hex;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
static BOOL IsTerminalStatus(id status) {
|
|
44
|
+
return [@[@"delivered", @"expired", @"undeliverable"] containsObject:status ?: @""];
|
|
45
|
+
}
|
|
46
|
+
|
|
43
47
|
NSString *CCBridgeSHA1Hex(NSString *text) {
|
|
44
48
|
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
|
|
45
49
|
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
|
|
@@ -262,6 +266,12 @@ static NSString *WaitForLock(NSString *name) {
|
|
|
262
266
|
return nil;
|
|
263
267
|
}
|
|
264
268
|
|
|
269
|
+
id CCBridgeWithLock(NSString *name, id (^body)(void)) {
|
|
270
|
+
NSString *lock = WaitForLock(name);
|
|
271
|
+
@try { return body(); }
|
|
272
|
+
@finally { CCBridgeReleaseLock(lock); }
|
|
273
|
+
}
|
|
274
|
+
|
|
265
275
|
#pragma mark - 会话注册表
|
|
266
276
|
|
|
267
277
|
NSString *CCBridgeSessionRef(NSString *sessionId) {
|
|
@@ -346,6 +356,51 @@ static NSArray<NSString *> *NameAnimals(void) {
|
|
|
346
356
|
return animals;
|
|
347
357
|
}
|
|
348
358
|
|
|
359
|
+
NSString *CCBridgeSessionAnimalAlias(NSDictionary *session) {
|
|
360
|
+
if (![CCPetsCurrentLanguage() hasPrefix:@"zh"] ||
|
|
361
|
+
![StringValue(session[@"nameSource"]) isEqualToString:@"generated"]) return @"";
|
|
362
|
+
static NSDictionary<NSString *, NSString *> *aliases;
|
|
363
|
+
static dispatch_once_t once;
|
|
364
|
+
dispatch_once(&once, ^{
|
|
365
|
+
aliases = @{
|
|
366
|
+
@"otter": @"水獭", @"panda": @"熊猫", @"quokka": @"短尾矮袋鼠", @"corgi": @"柯基", @"koala": @"考拉",
|
|
367
|
+
@"capy": @"水豚", @"puffin": @"海鹦", @"lemur": @"狐猴", @"alpaca": @"羊驼", @"wombat": @"袋熊",
|
|
368
|
+
@"bunny": @"小兔", @"kitten": @"小猫", @"puppy": @"小狗", @"hamster": @"仓鼠", @"fox": @"狐狸",
|
|
369
|
+
@"owl": @"猫头鹰", @"seal": @"海豹", @"penguin": @"企鹅", @"duck": @"鸭子", @"llama": @"美洲驼",
|
|
370
|
+
@"sloth": @"树懒", @"fennec": @"耳廓狐", @"bear": @"熊", @"cub": @"幼崽", @"lamb": @"小羊",
|
|
371
|
+
@"fawn": @"小鹿", @"deer": @"鹿", @"dolphin": @"海豚", @"beluga": @"白鲸", @"orca": @"虎鲸",
|
|
372
|
+
@"narwhal": @"独角鲸", @"walrus": @"海象", @"manatee": @"海牛", @"turtle": @"乌龟", @"frog": @"青蛙",
|
|
373
|
+
@"gecko": @"壁虎", @"chick": @"小鸡", @"robin": @"知更鸟", @"finch": @"雀鸟", @"wren": @"鹪鹩",
|
|
374
|
+
@"sparrow": @"麻雀", @"toucan": @"巨嘴鸟", @"parrot": @"鹦鹉", @"macaw": @"金刚鹦鹉", @"dove": @"鸽子",
|
|
375
|
+
@"swan": @"天鹅", @"owlet": @"小猫头鹰", @"goose": @"鹅", @"koi": @"锦鲤", @"bee": @"蜜蜂",
|
|
376
|
+
@"ladybug": @"瓢虫", @"raccoon": @"浣熊", @"ferret": @"雪貂", @"badger": @"獾", @"beaver": @"河狸",
|
|
377
|
+
@"marmot": @"旱獭", @"meerkat": @"狐獴", @"mouse": @"小鼠", @"gerbil": @"沙鼠", @"pony": @"矮马",
|
|
378
|
+
@"foal": @"小马驹", @"piglet": @"小猪", @"calf": @"小牛", @"giraffe": @"长颈鹿", @"zebra": @"斑马",
|
|
379
|
+
@"hippo": @"河马", @"rhino": @"犀牛", @"tapir": @"貘", @"okapi": @"霍加狓", @"lynx": @"猞猁",
|
|
380
|
+
@"tiger": @"老虎", @"cheetah": @"猎豹", @"lion": @"狮子", @"husky": @"哈士奇", @"shiba": @"柴犬",
|
|
381
|
+
@"beagle": @"比格犬", @"pug": @"巴哥犬", @"poodle": @"贵宾犬", @"collie": @"柯利犬", @"dingo": @"澳洲野犬",
|
|
382
|
+
@"newt": @"蝾螈", @"tadpole": @"蝌蚪", @"octopus": @"章鱼", @"squid": @"鱿鱼", @"crab": @"螃蟹",
|
|
383
|
+
@"snail": @"蜗牛", @"peacock": @"孔雀", @"emu": @"鸸鹋", @"quail": @"鹌鹑", @"magpie": @"喜鹊",
|
|
384
|
+
@"kea": @"啄羊鹦鹉", @"heron": @"鹭鸟", @"stork": @"鹳", @"ibis": @"朱鹭", @"numbat": @"袋食蚁兽",
|
|
385
|
+
@"bilby": @"兔耳袋狸", @"possum": @"负鼠", @"dugong": @"儒艮", @"yak": @"牦牛", @"bison": @"野牛",
|
|
386
|
+
};
|
|
387
|
+
});
|
|
388
|
+
NSArray<NSString *> *parts = [StringValue(session[@"name"]) componentsSeparatedByString:@"."];
|
|
389
|
+
if (parts.count != 3) return @"";
|
|
390
|
+
NSString *animal = parts.lastObject;
|
|
391
|
+
NSCharacterSet *digits = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
|
|
392
|
+
NSUInteger end = animal.length;
|
|
393
|
+
while (end > 0 && [digits characterIsMember:[animal characterAtIndex:end - 1]]) end--;
|
|
394
|
+
NSString *alias = aliases[[animal substringToIndex:end]];
|
|
395
|
+
return alias ? [alias stringByAppendingString:[animal substringFromIndex:end]] : @"";
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
NSString *CCBridgeSessionDisplayName(NSDictionary *session) {
|
|
399
|
+
NSString *name = StringValue(session[@"name"]) ?: @"";
|
|
400
|
+
NSString *alias = CCBridgeSessionAnimalAlias(session);
|
|
401
|
+
return alias.length > 0 ? [name stringByAppendingFormat:@" · %@", alias] : name;
|
|
402
|
+
}
|
|
403
|
+
|
|
349
404
|
static NSDictionary *NameTakenBy(NSString *name, NSString *sessionId, NSArray<NSDictionary *> *liveSessions) {
|
|
350
405
|
for (NSDictionary *entry in liveSessions) {
|
|
351
406
|
if (![entry[@"session"] isEqual:sessionId] && [entry[@"name"] isEqual:name]) return entry;
|
|
@@ -381,6 +436,18 @@ static NSString *AllocateName(NSString *provider, NSString *profileName, NSStrin
|
|
|
381
436
|
}
|
|
382
437
|
}
|
|
383
438
|
|
|
439
|
+
// 状态卡这类窄地方用的短名:去掉默认名字开头的账号段(claude.my-app.otter → my-app.otter),
|
|
440
|
+
// 只处理明确标记为自动生成的名字;自定义名字和来源不明的旧记录原样返回。
|
|
441
|
+
NSString *CCBridgeSessionShortName(NSDictionary *session) {
|
|
442
|
+
NSString *name = StringValue(session[@"name"]);
|
|
443
|
+
if (name.length == 0) return @"";
|
|
444
|
+
if (![StringValue(session[@"nameSource"]) isEqualToString:@"generated"]) return name;
|
|
445
|
+
// 自动名字的首段就是分配时的账号名,不依赖之后可能变化的 profileName。
|
|
446
|
+
NSRange separator = [name rangeOfString:@"."];
|
|
447
|
+
return separator.location != NSNotFound && separator.location > 0 && NSMaxRange(separator) < name.length
|
|
448
|
+
? [name substringFromIndex:NSMaxRange(separator)] : name;
|
|
449
|
+
}
|
|
450
|
+
|
|
384
451
|
// 启动时指定的名字被占用就追加序号(frontend → frontend-2),不让会话因此注册失败。
|
|
385
452
|
static NSString *AllocateCustomName(NSString *name, NSString *sessionId, NSArray<NSDictionary *> *liveSessions) {
|
|
386
453
|
if (!NameTakenBy(name, sessionId, liveSessions)) return name;
|
|
@@ -426,6 +493,7 @@ static NSDictionary *UpsertSession(NSString *sessionId, NSString *provider, NSNu
|
|
|
426
493
|
record[@"name"] = custom ? AllocateCustomName(custom, safe, liveSessions ?: @[])
|
|
427
494
|
: AllocateName(StringValue(record[@"provider"]) ?: @"", StringValue(record[@"profileName"]),
|
|
428
495
|
StringValue(record[@"cwd"]), safe, liveSessions ?: @[]);
|
|
496
|
+
record[@"nameSource"] = custom ? @"custom" : @"generated";
|
|
429
497
|
}
|
|
430
498
|
if (status) record[@"status"] = status;
|
|
431
499
|
record[@"updatedAt"] = @(now);
|
|
@@ -447,6 +515,7 @@ static NSDictionary *RenameSession(NSString *sessionId, NSString *requestedName,
|
|
|
447
515
|
}
|
|
448
516
|
NSString *previous = StringValue(record[@"name"]) ?: @"";
|
|
449
517
|
record[@"name"] = name;
|
|
518
|
+
record[@"nameSource"] = @"custom";
|
|
450
519
|
record[@"updatedAt"] = @(CCPetsMilliseconds());
|
|
451
520
|
CCBridgeWriteJSON(SessionFile(record[@"session"]), record);
|
|
452
521
|
return @{@"session": record, @"previous": previous};
|
|
@@ -501,8 +570,11 @@ NSDictionary *CCBridgeReadReceipt(id messageId) {
|
|
|
501
570
|
return [receipt isKindOfClass:NSDictionary.class] ? receipt : nil;
|
|
502
571
|
}
|
|
503
572
|
|
|
573
|
+
// 回执只是元数据:正文只在信箱里,送达后随之删除,不跟着回执再留 48 小时。
|
|
504
574
|
void CCBridgeWriteReceipt(NSDictionary *message) {
|
|
505
|
-
|
|
575
|
+
NSMutableDictionary *receipt = [message mutableCopy];
|
|
576
|
+
[receipt removeObjectForKey:@"body"];
|
|
577
|
+
CCBridgeWriteJSON(ReceiptFile(message[@"id"]), receipt);
|
|
506
578
|
}
|
|
507
579
|
|
|
508
580
|
NSDictionary *CCBridgeUpdateReceipt(NSString *messageId, NSDictionary *patch) {
|
|
@@ -512,7 +584,7 @@ NSDictionary *CCBridgeUpdateReceipt(NSString *messageId, NSDictionary *patch) {
|
|
|
512
584
|
NSMutableDictionary *next = [CCBridgeReadReceipt(messageId) mutableCopy];
|
|
513
585
|
if (!next) return nil;
|
|
514
586
|
// 终态不能被迟到的 pending、失败回调等覆盖。
|
|
515
|
-
if (
|
|
587
|
+
if (IsTerminalStatus(next[@"status"])) return next;
|
|
516
588
|
[next addEntriesFromDictionary:patch];
|
|
517
589
|
next[@"updatedAt"] = @(CCPetsMilliseconds());
|
|
518
590
|
CCBridgeWriteReceipt(next);
|
|
@@ -555,11 +627,18 @@ NSString *CCBridgeCheckSendLimits(NSString *fromSession, NSString *toSession) {
|
|
|
555
627
|
return nil;
|
|
556
628
|
}
|
|
557
629
|
|
|
558
|
-
|
|
630
|
+
// 和清理共用信箱锁,并在锁内复查:发送可能卡在 codex queue 上几十秒,这期间 Bridge 可能已关闭、
|
|
631
|
+
// 目标可能已退出。锁外检查的话,清理做完之后这里才写入,正文就再也没人收拾了。
|
|
632
|
+
NSString *CCBridgeEnqueueInbox(NSDictionary *message) {
|
|
559
633
|
NSString *target = CCBridgeSafeSessionId(PartySession(message, @"to"));
|
|
560
634
|
if (!target) [NSException raise:@"CCBridgeInbox" format:@"%@", L(@"invalid target session id")];
|
|
561
|
-
|
|
562
|
-
|
|
635
|
+
return CCBridgeWithLock([@"inbox-" stringByAppendingString:target], ^id {
|
|
636
|
+
if (!CCBridgeEnabled()) return L(@"CC Bridge is off.");
|
|
637
|
+
if (!CCBridgeSessionLive(CCBridgeReadSession(target))) return L(@"the target session is offline");
|
|
638
|
+
CCBridgeWriteJSON([CCBridgeSubdirectory(@[@"inbox", target]) stringByAppendingPathComponent:
|
|
639
|
+
[message[@"id"] stringByAppendingString:@".json"]], message);
|
|
640
|
+
return nil;
|
|
641
|
+
});
|
|
563
642
|
}
|
|
564
643
|
|
|
565
644
|
static NSString *InboxLockFile(NSString *sessionId) {
|
|
@@ -588,8 +667,7 @@ NSArray<NSDictionary *> *CCBridgeClaimInbox(id sessionId) {
|
|
|
588
667
|
NSString *file = [claimed stringByAppendingPathComponent:name];
|
|
589
668
|
id message = CCBridgeReadJSON(file);
|
|
590
669
|
if (![message isKindOfClass:NSDictionary.class] || !IsMessageId(message[@"id"])) continue;
|
|
591
|
-
|
|
592
|
-
if ([@[@"delivered", @"expired", @"undeliverable"] containsObject:status ?: @""]) {
|
|
670
|
+
if (IsTerminalStatus(CCBridgeReadReceipt(message[@"id"])[@"status"])) {
|
|
593
671
|
unlink(file.fileSystemRepresentation);
|
|
594
672
|
continue;
|
|
595
673
|
}
|
|
@@ -630,26 +708,105 @@ NSUInteger CCBridgePendingCount(id sessionId) {
|
|
|
630
708
|
ListJSON(CCBridgeSubdirectory(@[@"claimed", safe])).count;
|
|
631
709
|
}
|
|
632
710
|
|
|
711
|
+
static NSString *SessionDirectory(NSString *kind, NSString *sessionId) {
|
|
712
|
+
return [[CCBridgeStateDirectory() stringByAppendingPathComponent:kind] stringByAppendingPathComponent:sessionId];
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// 调用方持有该信箱的锁。空目录一并删掉,否则已退出的会话会在状态目录里留下痕迹。
|
|
716
|
+
static void AbandonInboxLocked(NSString *sessionId, NSString *reason) {
|
|
717
|
+
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
718
|
+
NSString *directory = SessionDirectory(kind, sessionId);
|
|
719
|
+
for (NSString *name in ListJSON(directory)) {
|
|
720
|
+
NSString *file = [directory stringByAppendingPathComponent:name];
|
|
721
|
+
id message = CCBridgeReadJSON(file);
|
|
722
|
+
if ([message isKindOfClass:NSDictionary.class]) {
|
|
723
|
+
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"undeliverable", @"reason": reason});
|
|
724
|
+
}
|
|
725
|
+
unlink(file.fileSystemRepresentation);
|
|
726
|
+
}
|
|
727
|
+
rmdir(directory.fileSystemRepresentation);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
633
731
|
void CCBridgeAbandonInbox(id sessionId) {
|
|
634
732
|
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
635
733
|
if (!safe) return;
|
|
636
734
|
NSString *lock = WaitForLock([@"inbox-" stringByAppendingString:safe]);
|
|
735
|
+
@try { AbandonInboxLocked(safe, L(@"the target session has ended")); }
|
|
736
|
+
@finally { CCBridgeReleaseLock(lock); }
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
void CCBridgeAbandonAllInboxes(NSString *reason) {
|
|
740
|
+
NSMutableSet<NSString *> *sessions = [NSMutableSet set];
|
|
741
|
+
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
742
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:kind];
|
|
743
|
+
for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
744
|
+
if (CCBridgeSafeSessionId(name)) [sessions addObject:name];
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
// 已注册的会话也要过一遍锁:信箱目录可能正在被创建,只看现有目录会漏掉锁内还没写完的发送。
|
|
748
|
+
for (NSDictionary *session in CCBridgeListSessions()) {
|
|
749
|
+
if (CCBridgeSafeSessionId(session[@"session"])) [sessions addObject:session[@"session"]];
|
|
750
|
+
}
|
|
751
|
+
for (NSString *session in sessions) {
|
|
752
|
+
NSString *lock = WaitForLock([@"inbox-" stringByAppendingString:session]);
|
|
753
|
+
@try { AbandonInboxLocked(session, reason); }
|
|
754
|
+
@finally { CCBridgeReleaseLock(lock); }
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// 崩溃的会话不会触发 SessionEnd。这里只试一次锁:抢不到说明有人正在处理,留给下一次清理,
|
|
759
|
+
// 不能让查询在线列表的调用方等锁。锁内复查在线,防止同一 ID 刚被 resume。
|
|
760
|
+
void CCBridgeAbandonOfflineSession(id sessionId) {
|
|
761
|
+
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
762
|
+
if (!safe) return;
|
|
763
|
+
NSString *lock = CCBridgeAcquireLock([@"inbox-" stringByAppendingString:safe]);
|
|
764
|
+
if (!lock) return;
|
|
765
|
+
@try {
|
|
766
|
+
if (CCBridgeSessionLive(CCBridgeReadSession(safe))) return;
|
|
767
|
+
AbandonInboxLocked(safe, L(@"the target session has ended"));
|
|
768
|
+
CCBridgeClearIdleSubscriptions(safe);
|
|
769
|
+
CCBridgeDropWatcher(safe);
|
|
770
|
+
} @finally { CCBridgeReleaseLock(lock); }
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
static void ExpireInbox(NSString *sessionId) {
|
|
774
|
+
NSString *lock = CCBridgeAcquireLock([@"inbox-" stringByAppendingString:sessionId]);
|
|
775
|
+
if (!lock) return;
|
|
637
776
|
@try {
|
|
777
|
+
long long now = CCPetsMilliseconds();
|
|
638
778
|
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
639
|
-
NSString *directory =
|
|
779
|
+
NSString *directory = SessionDirectory(kind, sessionId);
|
|
640
780
|
for (NSString *name in ListJSON(directory)) {
|
|
641
781
|
NSString *file = [directory stringByAppendingPathComponent:name];
|
|
642
782
|
id message = CCBridgeReadJSON(file);
|
|
643
|
-
if ([message isKindOfClass:NSDictionary.class]) {
|
|
644
|
-
|
|
783
|
+
if (![message isKindOfClass:NSDictionary.class]) {
|
|
784
|
+
unlink(file.fileSystemRepresentation);
|
|
785
|
+
} else if (now - NumberValue(message[@"createdAt"]) > CCBridgeMessageTTL) {
|
|
786
|
+
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"expired"});
|
|
787
|
+
unlink(file.fileSystemRepresentation);
|
|
645
788
|
}
|
|
646
|
-
unlink(file.fileSystemRepresentation);
|
|
647
789
|
}
|
|
648
790
|
}
|
|
649
791
|
} @finally { CCBridgeReleaseLock(lock); }
|
|
650
792
|
}
|
|
651
793
|
|
|
652
|
-
|
|
794
|
+
void CCBridgeSweepMessages(void) {
|
|
795
|
+
NSMutableSet<NSString *> *sessions = [NSMutableSet set];
|
|
796
|
+
for (NSString *kind in @[@"inbox", @"claimed", @"subscriptions"]) {
|
|
797
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:kind];
|
|
798
|
+
for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
799
|
+
if (CCBridgeSafeSessionId(name)) [sessions addObject:name];
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
for (NSString *session in sessions) {
|
|
803
|
+
if (CCBridgeSessionLive(CCBridgeReadSession(session))) ExpireInbox(session);
|
|
804
|
+
else CCBridgeAbandonOfflineSession(session);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// 回执保留 TTL 两倍时长,够排查问题,又不会无限增长。超过 TTL 仍未到终态的(目标崩溃、投递中途退出)
|
|
809
|
+
// 不会再有人更新,在这里收尾,发送方才能看到结果。
|
|
653
810
|
void CCBridgePruneReceipts(void) {
|
|
654
811
|
NSString *directory = CCBridgeSubdirectory(@[@"sent"]);
|
|
655
812
|
long long now = CCPetsMilliseconds();
|
|
@@ -658,6 +815,8 @@ void CCBridgePruneReceipts(void) {
|
|
|
658
815
|
id receipt = CCBridgeReadJSON(file);
|
|
659
816
|
if (![receipt isKindOfClass:NSDictionary.class] || now - NumberValue(receipt[@"createdAt"]) > CCBridgeMessageTTL * 2) {
|
|
660
817
|
unlink(file.fileSystemRepresentation);
|
|
818
|
+
} else if (now - NumberValue(receipt[@"createdAt"]) > CCBridgeMessageTTL && !IsTerminalStatus(receipt[@"status"])) {
|
|
819
|
+
CCBridgeUpdateReceipt(receipt[@"id"], @{@"status": @"expired"});
|
|
661
820
|
}
|
|
662
821
|
}
|
|
663
822
|
}
|
|
@@ -696,6 +855,16 @@ NSArray<NSString *> *CCBridgeTakeIdleSubscriptions(NSString *targetSession) {
|
|
|
696
855
|
return subscribers;
|
|
697
856
|
}
|
|
698
857
|
|
|
858
|
+
void CCBridgeClearIdleSubscriptions(id targetSession) {
|
|
859
|
+
NSString *target = CCBridgeSafeSessionId(targetSession);
|
|
860
|
+
if (!target) return;
|
|
861
|
+
NSString *directory = SessionDirectory(@"subscriptions", target);
|
|
862
|
+
for (NSString *name in ListJSON(directory)) {
|
|
863
|
+
unlink([directory stringByAppendingPathComponent:name].fileSystemRepresentation);
|
|
864
|
+
}
|
|
865
|
+
rmdir(directory.fileSystemRepresentation);
|
|
866
|
+
}
|
|
867
|
+
|
|
699
868
|
#pragma mark - 锁
|
|
700
869
|
|
|
701
870
|
// 内核锁文件保持稳定 inode,PID 文件仅用于诊断;不能通过删除“陈旧锁”抢占。
|
|
@@ -737,6 +906,29 @@ void CCBridgeReleaseLock(NSString *lockFile) {
|
|
|
737
906
|
}
|
|
738
907
|
}
|
|
739
908
|
|
|
909
|
+
#pragma mark - watcher 接替
|
|
910
|
+
|
|
911
|
+
static NSString *WatcherOwnerFile(NSString *sessionId) {
|
|
912
|
+
return [CCBridgeSubdirectory(@[@"locks"]) stringByAppendingPathComponent:
|
|
913
|
+
[NSString stringWithFormat:@"watch-%@.owner", sessionId]];
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
NSString *CCBridgeClaimWatcher(NSString *sessionId) {
|
|
917
|
+
NSString *token = RandomHex(8);
|
|
918
|
+
CCBridgeWriteJSON(WatcherOwnerFile(sessionId), @{@"token": token});
|
|
919
|
+
return token;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
BOOL CCBridgeWatcherOwned(NSString *sessionId, NSString *token) {
|
|
923
|
+
id owner = CCBridgeReadJSON(WatcherOwnerFile(sessionId));
|
|
924
|
+
return [owner isKindOfClass:NSDictionary.class] && [owner[@"token"] isEqual:token];
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
void CCBridgeDropWatcher(id sessionId) {
|
|
928
|
+
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
929
|
+
if (safe) unlink(WatcherOwnerFile(safe).fileSystemRepresentation);
|
|
930
|
+
}
|
|
931
|
+
|
|
740
932
|
#pragma mark - 进程探测
|
|
741
933
|
|
|
742
934
|
BOOL CCBridgeProcessAlive(pid_t pid) {
|
|
@@ -754,8 +946,9 @@ static BOOL ProcessParent(pid_t pid, pid_t *parent, dev_t *tty) {
|
|
|
754
946
|
return YES;
|
|
755
947
|
}
|
|
756
948
|
|
|
757
|
-
//
|
|
758
|
-
|
|
949
|
+
// 保留 argv 的边界:路径可以含空格(如 Application Support),拼成字符串再切分就认不出可执行文件了。
|
|
950
|
+
// 读不到参数区(别的用户的进程、僵尸)时退回 ps,那里没有边界,只能按空白切分。
|
|
951
|
+
NSArray<NSString *> *CCBridgeProcessArguments(pid_t pid) {
|
|
759
952
|
if (pid <= 0) return nil;
|
|
760
953
|
int argumentMaximum = 0;
|
|
761
954
|
size_t size = sizeof argumentMaximum;
|
|
@@ -779,7 +972,7 @@ NSString *CCBridgeProcessCommand(pid_t pid) {
|
|
|
779
972
|
cursor += length + 1;
|
|
780
973
|
}
|
|
781
974
|
free(buffer);
|
|
782
|
-
if (arguments.count > 0) return
|
|
975
|
+
if (arguments.count > 0) return arguments;
|
|
783
976
|
} else {
|
|
784
977
|
free(buffer);
|
|
785
978
|
}
|
|
@@ -788,8 +981,11 @@ NSString *CCBridgeProcessCommand(pid_t pid) {
|
|
|
788
981
|
if (CCPetsRun(@"/bin/ps", @[@"-o", @"command=", @"-p", [NSString stringWithFormat:@"%d", pid]], nil, nil, 10, &output, NULL) != 0) {
|
|
789
982
|
return nil;
|
|
790
983
|
}
|
|
791
|
-
NSString *
|
|
792
|
-
|
|
984
|
+
NSMutableArray<NSString *> *tokens = [NSMutableArray array];
|
|
985
|
+
for (NSString *token in [output ?: @"" componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]) {
|
|
986
|
+
if (token.length > 0) [tokens addObject:token];
|
|
987
|
+
}
|
|
988
|
+
return tokens.count > 0 ? tokens : nil;
|
|
793
989
|
}
|
|
794
990
|
|
|
795
991
|
NSString *CCBridgeProcessTty(pid_t pid) {
|
|
@@ -801,26 +997,17 @@ NSString *CCBridgeProcessTty(pid_t pid) {
|
|
|
801
997
|
return [tty hasPrefix:@"tty"] ? tty : [@"tty" stringByAppendingString:tty];
|
|
802
998
|
}
|
|
803
999
|
|
|
804
|
-
// 只看可执行文件本身,不看参数:参数里出现 "claude" / "codex" 的进程太多了
|
|
805
|
-
// (比如 tmux new-session "codex …"、编辑器打开的某个路径)。
|
|
806
|
-
static NSArray<NSString *> *CommandTokens(NSString *command) {
|
|
807
|
-
NSMutableArray<NSString *> *tokens = [NSMutableArray array];
|
|
808
|
-
for (NSString *token in [command ?: @"" componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]) {
|
|
809
|
-
if (token.length > 0) [tokens addObject:token];
|
|
810
|
-
}
|
|
811
|
-
return tokens;
|
|
812
|
-
}
|
|
813
|
-
|
|
814
1000
|
static BOOL Matches(NSString *pattern, NSString *text) {
|
|
815
1001
|
if (text.length == 0) return NO;
|
|
816
1002
|
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
|
|
817
1003
|
return [expression firstMatchInString:text options:0 range:NSMakeRange(0, text.length)] != nil;
|
|
818
1004
|
}
|
|
819
1005
|
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
NSString *
|
|
1006
|
+
// 只看可执行文件本身和脚本路径,不看其余参数:参数里出现 "claude" / "codex" 的进程太多了
|
|
1007
|
+
// (比如 tmux new-session "codex …"、编辑器打开的某个路径)。
|
|
1008
|
+
BOOL CCBridgeArgumentsMatchProvider(NSString *provider, NSArray<NSString *> *arguments) {
|
|
1009
|
+
NSString *executable = arguments.firstObject ?: @"";
|
|
1010
|
+
NSString *second = arguments.count > 1 ? arguments[1] : @"";
|
|
824
1011
|
// Codex 的 npm 包是 node 包装器 + 原生二进制;hook 和 MCP 进程的直接祖先都是原生二进制,
|
|
825
1012
|
// 取离自己最近的那个,两边才能落到同一个 pid。
|
|
826
1013
|
if ([provider isEqualToString:@"Codex"]) {
|
|
@@ -850,9 +1037,9 @@ NSDictionary *CCBridgeFindAgentAncestor(pid_t startPid) {
|
|
|
850
1037
|
for (int depth = 0; depth < 12 && pid > 1; depth++) {
|
|
851
1038
|
pid_t parent = 0;
|
|
852
1039
|
if (!ProcessParent(pid, &parent, NULL)) return nil;
|
|
853
|
-
NSString *
|
|
1040
|
+
NSArray<NSString *> *arguments = CCBridgeProcessArguments(pid);
|
|
854
1041
|
for (NSString *provider in @[@"Claude", @"Codex"]) {
|
|
855
|
-
if (
|
|
1042
|
+
if (CCBridgeArgumentsMatchProvider(provider, arguments)) return @{@"provider": provider, @"pid": @(pid)};
|
|
856
1043
|
}
|
|
857
1044
|
pid = parent;
|
|
858
1045
|
}
|
|
@@ -864,8 +1051,8 @@ BOOL CCBridgeSessionLive(NSDictionary *session) {
|
|
|
864
1051
|
id pid = session[@"pid"];
|
|
865
1052
|
if ([pid isKindOfClass:NSNumber.class]) {
|
|
866
1053
|
if (!CCBridgeProcessAlive([pid intValue])) return NO;
|
|
867
|
-
NSString *
|
|
868
|
-
return
|
|
1054
|
+
NSArray<NSString *> *arguments = CCBridgeProcessArguments([pid intValue]);
|
|
1055
|
+
return arguments == nil || CCBridgeArgumentsMatchProvider(session[@"provider"], arguments);
|
|
869
1056
|
}
|
|
870
1057
|
return CCPetsMilliseconds() - NumberValue(session[@"updatedAt"]) < StaleWithoutPid;
|
|
871
1058
|
}
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
// Agent 事件的记录与状态归一化,同时是 --hook / --provider-event 两个子命令的实现。
|
|
4
4
|
NSString *SanitizedShortString(id value, NSUInteger maximumLength);
|
|
5
|
+
// 账号 ID 是标识符而非展示文字:保留冒号,并修复旧事件中 provider:hash 丢失冒号的格式。
|
|
6
|
+
NSString *NormalizedAgentProfileID(id value);
|
|
7
|
+
NSString *SanitizedWorkingDirectory(id value);
|
|
8
|
+
// 状态卡标题里的会话标签:优先 CC Bridge 短名,其次目录名;过长时保留末尾的动物后缀。
|
|
9
|
+
NSString *StatusSessionLabel(NSString *bridgeName, NSString *cwd);
|
|
5
10
|
NSString *ProviderFromEnvironment(void);
|
|
6
11
|
NSString *NormalizedStateForEvent(NSString *event, BOOL failed);
|
|
7
12
|
// 在线会话的 key:provider + 账号 + tty。同一个终端先后跑过两个账号时,只看 provider + tty
|
|
@@ -20,6 +20,47 @@ NSString *SanitizedShortString(id value, NSUInteger maximumLength) {
|
|
|
20
20
|
return [safe stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
NSString *NormalizedAgentProfileID(id value) {
|
|
24
|
+
if (![value isKindOfClass:NSString.class]) return @"";
|
|
25
|
+
NSString *identifier = [value stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
26
|
+
NSCharacterSet *allowed = [NSCharacterSet characterSetWithCharactersInString:
|
|
27
|
+
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-:"];
|
|
28
|
+
// 拒绝非法标识,不能删除字符后误认成另一个账号。
|
|
29
|
+
if (identifier.length == 0 || identifier.length > 128 ||
|
|
30
|
+
[identifier rangeOfCharacterFromSet:allowed.invertedSet].location != NSNotFound) return @"";
|
|
31
|
+
NSRegularExpression *legacy = [NSRegularExpression regularExpressionWithPattern:
|
|
32
|
+
@"\\A(codex|claude)([0-9a-f]{16})\\z" options:0 error:nil];
|
|
33
|
+
return [legacy stringByReplacingMatchesInString:identifier options:0
|
|
34
|
+
range:NSMakeRange(0, identifier.length) withTemplate:@"$1:$2"];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 工作目录只用于状态卡上的会话标签和悬停提示。路径里可以有中文和空格,所以不走
|
|
38
|
+
// SanitizedShortString 的白名单;只收绝对路径,挡掉控制字符和离谱的长度。
|
|
39
|
+
NSString *SanitizedWorkingDirectory(id value) {
|
|
40
|
+
if (![value isKindOfClass:NSString.class]) return @"";
|
|
41
|
+
NSString *path = [(NSString *)value stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
42
|
+
if (path.length == 0 || path.length > 1024 || ![path hasPrefix:@"/"]) return @"";
|
|
43
|
+
if ([path rangeOfCharacterFromSet:NSCharacterSet.controlCharacterSet].location != NSNotFound) return @"";
|
|
44
|
+
return path;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 状态卡标题里的会话标签:有 CC Bridge 短名(目录名.动物)就用它,否则退回目录名。
|
|
48
|
+
// 卡片很窄,超过 20 个字符时截掉中间,保留末尾的动物后缀——同目录的多个会话全靠它区分。
|
|
49
|
+
NSString *StatusSessionLabel(NSString *bridgeName, NSString *cwd) {
|
|
50
|
+
static const NSUInteger limit = 20;
|
|
51
|
+
NSString *label = bridgeName.length > 0 ? bridgeName : cwd.lastPathComponent;
|
|
52
|
+
if (label.length == 0 || [label isEqualToString:@"/"]) return @"";
|
|
53
|
+
if (label.length <= limit) return label;
|
|
54
|
+
NSRange dot = [label rangeOfString:@"." options:NSBackwardsSearch];
|
|
55
|
+
NSString *suffix = dot.location != NSNotFound && dot.location > 0 && label.length - dot.location <= 9
|
|
56
|
+
? [label substringFromIndex:dot.location] : @"";
|
|
57
|
+
NSUInteger keep = limit - 1 - suffix.length;
|
|
58
|
+
// 按完整字符截断,免得把中文目录名或 emoji 切成半个。
|
|
59
|
+
NSRange head = [label rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, keep)];
|
|
60
|
+
if (head.length > keep) head = [label rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, keep - 1)];
|
|
61
|
+
return [NSString stringWithFormat:@"%@…%@", [label substringWithRange:head], suffix];
|
|
62
|
+
}
|
|
63
|
+
|
|
23
64
|
NSString *ProviderFromEnvironment(void) {
|
|
24
65
|
NSDictionary<NSString *, NSString *> *environment = NSProcessInfo.processInfo.environment;
|
|
25
66
|
if ([environment[@"CC_PETS_CODEX_AGENT_HOOK"] boolValue]) return @"Codex";
|
|
@@ -91,7 +132,7 @@ NSString *NormalizedStateForEvent(NSString *event, BOOL failed) {
|
|
|
91
132
|
|
|
92
133
|
NSString *OnlineAgentSessionKey(id provider, id profileID, id tty) {
|
|
93
134
|
NSString *providerName = SanitizedShortString(provider, 32);
|
|
94
|
-
NSString *profile =
|
|
135
|
+
NSString *profile = NormalizedAgentProfileID(profileID);
|
|
95
136
|
// 先取末段再清洗:清洗会去掉 "/","/dev/ttys003" 就成了 "devttys003"。
|
|
96
137
|
NSString *ttyName = [tty isKindOfClass:NSString.class]
|
|
97
138
|
? SanitizedShortString([(NSString *)tty lastPathComponent], 64) : @"";
|
|
@@ -189,19 +230,18 @@ int RecordHookEvent(void) {
|
|
|
189
230
|
if (![payload isKindOfClass:NSDictionary.class]) return EXIT_SUCCESS;
|
|
190
231
|
NSString *event = payload[@"hook_event_name"];
|
|
191
232
|
if (![event isKindOfClass:NSString.class] || event.length == 0) return EXIT_SUCCESS;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
//
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
233
|
+
NSString *provider = ProviderFromEnvironment();
|
|
234
|
+
NSDictionary *terminal = TerminalFocusTargetForHook(payload, provider);
|
|
235
|
+
BOOL lifecycle = [event isEqualToString:@"SessionStart"] || [event isEqualToString:@"SessionEnd"];
|
|
236
|
+
// 桌面会话不经过 CLI 包装器,必须记录生命周期,但消费端不播报任务完成。
|
|
237
|
+
// Codex 的结束事件即使缺少回跳目标也要保留,以 session_id 清理记录。
|
|
238
|
+
if (lifecycle && !([provider isEqualToString:@"Codex"] &&
|
|
239
|
+
(IsCodexDesktopFocusTarget(terminal) || [event isEqualToString:@"SessionEnd"]))) return EXIT_SUCCESS;
|
|
199
240
|
NSString *tool = [payload[@"tool_name"] isKindOfClass:NSString.class] ? payload[@"tool_name"] : @"";
|
|
200
241
|
NSString *session = SanitizedShortString(payload[@"session_id"], 128);
|
|
201
242
|
id toolResult = payload[@"tool_response"] ?: payload[@"tool_result"];
|
|
202
243
|
BOOL failed = [event isEqualToString:@"PostToolUseFailure"] ||
|
|
203
244
|
[event isEqualToString:@"StopFailure"] || HookValueIndicatesFailure(toolResult);
|
|
204
|
-
NSString *provider = ProviderFromEnvironment();
|
|
205
245
|
BOOL autoReview = [provider isEqualToString:@"Codex"] &&
|
|
206
246
|
[event isEqualToString:@"PermissionRequest"] && CodexAutoReviewEnabled();
|
|
207
247
|
NSMutableDictionary *record = [@{
|
|
@@ -213,12 +253,24 @@ int RecordHookEvent(void) {
|
|
|
213
253
|
@"timestamp": @([NSDate.date timeIntervalSince1970])
|
|
214
254
|
} mutableCopy];
|
|
215
255
|
if (session.length > 0) record[@"session"] = session;
|
|
216
|
-
NSString *
|
|
256
|
+
NSString *cwd = SanitizedWorkingDirectory(payload[@"cwd"]);
|
|
257
|
+
if (cwd.length > 0) record[@"cwd"] = cwd;
|
|
258
|
+
NSString *profileID = NormalizedAgentProfileID(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_ID"]);
|
|
217
259
|
NSString *profileName = SanitizedShortString(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_NAME"], 64);
|
|
218
260
|
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
219
261
|
if (profileName.length > 0) record[@"profileName"] = profileName;
|
|
220
|
-
NSDictionary *terminal = TerminalFocusTargetForHook(payload, provider);
|
|
221
262
|
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
263
|
+
if ([provider isEqualToString:@"Codex"]) {
|
|
264
|
+
// 不记录 prompt;只保存会话是否有转录,区分桌面后台临时任务与真实聊天。
|
|
265
|
+
id transcript = payload[@"transcript_path"];
|
|
266
|
+
if (transcript == NSNull.null) record[@"transient"] = @YES;
|
|
267
|
+
else if ([transcript isKindOfClass:NSString.class] && [transcript length] > 0) {
|
|
268
|
+
record[@"transcriptPath"] = transcript;
|
|
269
|
+
}
|
|
270
|
+
NSString *home = NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_HOME"]
|
|
271
|
+
?: NSProcessInfo.processInfo.environment[@"CODEX_HOME"] ?: DefaultCodexHomeDirectory();
|
|
272
|
+
if (home.isAbsolutePath) record[@"codexHome"] = home;
|
|
273
|
+
}
|
|
222
274
|
AppendAgentEventRecord(record);
|
|
223
275
|
return EXIT_SUCCESS;
|
|
224
276
|
}
|
|
@@ -264,7 +316,11 @@ int RecordProviderEvent(void) {
|
|
|
264
316
|
@"state": state,
|
|
265
317
|
@"timestamp": @([NSDate.date timeIntervalSince1970])
|
|
266
318
|
} mutableCopy];
|
|
267
|
-
|
|
319
|
+
// 包装脚本的启动事件没有 Hook payload,从调用进程继承的目录补齐会话标签。
|
|
320
|
+
NSString *cwd = SanitizedWorkingDirectory(payload[@"cwd"]);
|
|
321
|
+
if (cwd.length == 0) cwd = SanitizedWorkingDirectory(NSFileManager.defaultManager.currentDirectoryPath);
|
|
322
|
+
if (cwd.length > 0) record[@"cwd"] = cwd;
|
|
323
|
+
NSString *profileID = NormalizedAgentProfileID(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_ID"]);
|
|
268
324
|
NSString *profileName = SanitizedShortString(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_NAME"], 64);
|
|
269
325
|
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
270
326
|
if (profileName.length > 0) record[@"profileName"] = profileName;
|
|
@@ -391,7 +391,7 @@ int InstallCodexHooksAtHome(NSString *binary, NSString *codexHome) {
|
|
|
391
391
|
CCPetsShellQuote(codexHome), CCPetsShellQuote(binary)];
|
|
392
392
|
ReplaceManagedHooks(config, @[@[CCPetsCodexHookMarker, @"cc-pets"], @[CCPetsLegacyCodexHookMarker, @"codex-pet"]], command, @[
|
|
393
393
|
@"SessionStart", @"UserPromptSubmit", @"PreToolUse", @"PermissionRequest", @"PostToolUse",
|
|
394
|
-
@"SubagentStart", @"SubagentStop", @"Stop"
|
|
394
|
+
@"SubagentStart", @"SubagentStop", @"Stop", @"SessionEnd"
|
|
395
395
|
]);
|
|
396
396
|
|
|
397
397
|
NSString *text = [CCPetsJSONStringify(config) stringByAppendingString:@"\n"];
|
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
// 由 Codex 共享服务(app-server --managed-daemon)拉起的 hook 返回空目标,见实现处注释。
|
|
6
6
|
NSDictionary *TerminalFocusTargetFromEnvironment(void);
|
|
7
7
|
BOOL TerminalFocusRunsUnderSharedAgentHost(void);
|
|
8
|
+
// 桌面宿主不使用终端 TTY;事件时间必须属于当前应用实例,避免重启后复活旧会话。
|
|
9
|
+
BOOL IsCodexDesktopFocusTarget(NSDictionary *target);
|
|
10
|
+
BOOL CodexDesktopFocusTargetIsLive(NSDictionary *target, NSTimeInterval eventTime);
|
|
11
|
+
BOOL CodexDesktopApplicationIsRunning(void);
|
|
12
|
+
// 只读本地聊天索引;YES 为已持久化聊天,NO 为未收录/已归档,nil 为无法确认。
|
|
13
|
+
NSNumber *CodexPersistentThreadStatus(NSString *thread, NSString *codexHome);
|
|
8
14
|
|
|
9
15
|
// hook 用:普通情况同 TerminalFocusTargetFromEnvironment;Codex 共享服务下按会话 ID 找
|
|
10
16
|
// codex-with-pet 登记的终端,找不到返回空目标。
|