cc-pets 2.0.0 → 2.0.3
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/CHANGELOG.md +73 -30
- package/CHANGELOG.zh-CN.md +90 -0
- package/CONTRIBUTING.md +18 -15
- package/CONTRIBUTING.zh-CN.md +22 -0
- package/LICENSE +1 -1
- package/PROVIDER_PROTOCOL.md +50 -41
- package/PROVIDER_PROTOCOL.zh-CN.md +58 -0
- package/README.md +240 -502
- package/README.zh-CN.md +617 -0
- package/Resources/Info.plist +2 -0
- package/Resources/phrases.default.txt +48 -49
- package/SECURITY.md +13 -10
- package/SECURITY.zh-CN.md +18 -0
- package/Sources/CCPets/CCPetsAppDelegate.h +12 -0
- package/Sources/CCPets/CCPetsAppDelegate.m +400 -37
- package/Sources/CCPets/CCPetsCodexRateLimits.h +13 -0
- package/Sources/CCPets/CCPetsCodexRateLimits.m +383 -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/CCPetsPhrases.h +1 -24
- package/Sources/CCPets/CCPetsPhrases.m +1 -82
- package/Sources/CCPets/CCPetsPhrasesEditor.m +0 -28
- package/Sources/CCPets/CCPetsQuotaHistory.m +12 -5
- package/Sources/CCPets/CCPetsTerminalFocus.h +11 -0
- package/Sources/CCPets/CCPetsTerminalFocus.m +150 -0
- package/Sources/CCPets/CCPetsUsage.m +135 -14
- package/Sources/CCPets/CCPetsUsageMonitor.m +41 -3
- package/Sources/CCPets/PetView.m +19 -11
- package/Sources/CCPets/QuotaDashboardView.m +38 -16
- package/Sources/CCPets/main.m +5 -0
- package/THIRD_PARTY_NOTICES.md +13 -7
- package/THIRD_PARTY_NOTICES.zh-CN.md +12 -0
- package/bin/claude-with-pet +8 -2
- package/bin/codex-with-pet +8 -3
- package/package.json +14 -5
- package/scripts/pet-store.mjs +1 -1
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
#import "CCPetsPhrases.h"
|
|
8
8
|
#import "CCPetsPhrasesEditor.h"
|
|
9
9
|
#import "CCPetsUsage.h"
|
|
10
|
+
#import "CCPetsTerminalFocus.h"
|
|
10
11
|
#import "MenuToggleSwitch.h"
|
|
11
12
|
#import <UserNotifications/UserNotifications.h>
|
|
12
13
|
#import <signal.h>
|
|
@@ -14,13 +15,68 @@
|
|
|
14
15
|
#import <fcntl.h>
|
|
15
16
|
#import <unistd.h>
|
|
16
17
|
#import <errno.h>
|
|
18
|
+
#import <sys/sysctl.h>
|
|
19
|
+
#import <stdlib.h>
|
|
17
20
|
|
|
18
21
|
static const NSTimeInterval PendingApprovalTTL = 24 * 60 * 60;
|
|
19
22
|
static const NSUInteger PendingApprovalLimit = 100;
|
|
23
|
+
static const NSUInteger AgentSessionRecordLimit = 20;
|
|
24
|
+
static const NSUInteger AgentSessionMenuLimit = 8;
|
|
25
|
+
static const CGFloat PetApprovalBadgeSize = 17.0;
|
|
20
26
|
static const unsigned long long UpdateLogSizeLimit = 1024 * 1024;
|
|
21
27
|
static NSString *const PetInteractionPhrasesV1MigratedKey =
|
|
22
28
|
@"CCPetsInteractionPhrasesV1Migrated";
|
|
23
29
|
|
|
30
|
+
@interface CCPetsStatusClickButton : NSButton
|
|
31
|
+
@end
|
|
32
|
+
|
|
33
|
+
@implementation CCPetsStatusClickButton
|
|
34
|
+
- (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; }
|
|
35
|
+
- (void)resetCursorRects {
|
|
36
|
+
[super resetCursorRects];
|
|
37
|
+
[self addCursorRect:self.bounds cursor:NSCursor.pointingHandCursor];
|
|
38
|
+
}
|
|
39
|
+
@end
|
|
40
|
+
|
|
41
|
+
// 待审批角标。等审批的会话按定义是停住不动的,它的时间戳只会越来越旧——状态卡
|
|
42
|
+
// 正文永远显示最新事件,会话列表又按时间倒序,两边都会把最该处理的那条推到看
|
|
43
|
+
// 不见的地方。角标把这个数字单独拎出来常驻。
|
|
44
|
+
@interface CCPetsApprovalBadgeView : NSView
|
|
45
|
+
@property(nonatomic) NSUInteger count;
|
|
46
|
+
@end
|
|
47
|
+
|
|
48
|
+
@implementation CCPetsApprovalBadgeView
|
|
49
|
+
- (void)setCount:(NSUInteger)count {
|
|
50
|
+
if (_count == count) return;
|
|
51
|
+
_count = count;
|
|
52
|
+
self.hidden = count == 0;
|
|
53
|
+
self.needsDisplay = YES;
|
|
54
|
+
}
|
|
55
|
+
// 角标压在圆形状态图标的右上角,但不能把那颗按钮的点击吃掉。
|
|
56
|
+
- (NSView *)hitTest:(NSPoint)point { return nil; }
|
|
57
|
+
- (void)drawRect:(NSRect)dirtyRect {
|
|
58
|
+
if (self.count == 0) return;
|
|
59
|
+
// 描边是给玻璃卡片准备的:审批色和卡片背景都偏亮,没有这圈白边角标会糊在一起。
|
|
60
|
+
NSRect circle = NSInsetRect(self.bounds, 1.0, 1.0);
|
|
61
|
+
NSBezierPath *fill = [NSBezierPath bezierPathWithOvalInRect:circle];
|
|
62
|
+
[[NSColor colorWithRed:0.86 green:0.24 blue:0.24 alpha:1] setFill];
|
|
63
|
+
[fill fill];
|
|
64
|
+
fill.lineWidth = 1.5;
|
|
65
|
+
[[NSColor colorWithWhite:1 alpha:0.92] setStroke];
|
|
66
|
+
[fill stroke];
|
|
67
|
+
BOOL overflow = self.count > 9;
|
|
68
|
+
NSString *text = overflow ? @"9+" : @(self.count).stringValue;
|
|
69
|
+
NSDictionary *attributes = @{
|
|
70
|
+
NSFontAttributeName: [NSFont systemFontOfSize:overflow ? 8.5 : 10
|
|
71
|
+
weight:NSFontWeightBold],
|
|
72
|
+
NSForegroundColorAttributeName: NSColor.whiteColor
|
|
73
|
+
};
|
|
74
|
+
NSSize size = [text sizeWithAttributes:attributes];
|
|
75
|
+
[text drawAtPoint:NSMakePoint(NSMidX(self.bounds) - size.width / 2.0,
|
|
76
|
+
NSMidY(self.bounds) - size.height / 2.0) withAttributes:attributes];
|
|
77
|
+
}
|
|
78
|
+
@end
|
|
79
|
+
|
|
24
80
|
static void TrimUpdateLog(NSString *path) {
|
|
25
81
|
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
|
|
26
82
|
if (!handle) return;
|
|
@@ -347,6 +403,7 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
347
403
|
if (tag == 1) return NotificationCompletionKey;
|
|
348
404
|
if (tag == 2) return NotificationFailureKey;
|
|
349
405
|
if (tag == 3) return NotificationApprovalKey;
|
|
406
|
+
if (tag == 4) return NotificationStallKey;
|
|
350
407
|
return nil;
|
|
351
408
|
}
|
|
352
409
|
- (void)toggleSpeech:(NSButton *)sender {
|
|
@@ -573,10 +630,39 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
573
630
|
[self hideAgentStatus];
|
|
574
631
|
}
|
|
575
632
|
- (void)hideAgentStatus {
|
|
633
|
+
// 卡在审批的会话不该跟着沉默一起消失。60 秒没有新事件时,气泡不清场,而是回落
|
|
634
|
+
// 到最近那条审批:这样"有人在等你确认"始终有个落点,点卡片就能跳回那个终端,
|
|
635
|
+
// 角标也不会随面板一起被收走。
|
|
636
|
+
//
|
|
637
|
+
// 这不是把旧的"审批全局优先"逻辑搬回来——那一版在事件流里拦截,一个未处理的
|
|
638
|
+
// 审批会把其他 Agent 的所有 Hook 全挡住。这里只在完全没有新事件时才接管,任何
|
|
639
|
+
// 一条新事件都照常刷新正文。
|
|
640
|
+
NSArray<NSDictionary *> *pending = [self pendingApprovalSessionRecords];
|
|
641
|
+
if (self.hasAgentStatus && pending.count > 0) {
|
|
642
|
+
[self performSelector:@selector(hideAgentStatus) withObject:nil
|
|
643
|
+
afterDelay:AgentStatusInactivityInterval];
|
|
644
|
+
[self presentPendingApprovalRecord:pending.firstObject];
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
576
647
|
[NSObject cancelPreviousPerformRequestsWithTarget:self
|
|
577
648
|
selector:@selector(enterIdleStatus) object:nil];
|
|
578
649
|
[self.statusPanel orderOut:nil];
|
|
579
650
|
self.hasAgentStatus = NO;
|
|
651
|
+
[self refreshApprovalBadge];
|
|
652
|
+
}
|
|
653
|
+
// 只改卡片内容和回跳目标,不走 displayAgentRecord:那条路会连带播动画、说一句话、
|
|
654
|
+
// 发一次通知,而这里是"气泡闲下来后回落",重复表演反而吵。
|
|
655
|
+
- (void)presentPendingApprovalRecord:(NSDictionary *)record {
|
|
656
|
+
if (record.count == 0) return;
|
|
657
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
|
|
658
|
+
? record[@"terminal"] : nil;
|
|
659
|
+
if (terminal.count > 0) self.lastTerminalFocusTarget = terminal;
|
|
660
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
661
|
+
NSString *tool = [record[@"tool"] isKindOfClass:NSString.class] ? record[@"tool"] : @"";
|
|
662
|
+
if ([self.lastStatusState isEqualToString:@"approval"] &&
|
|
663
|
+
[self.lastStatusProvider isEqualToString:provider]) return;
|
|
664
|
+
[self applyStatusPresentationForState:@"approval"
|
|
665
|
+
provider:provider.length > 0 ? provider : @"Agent" tool:tool];
|
|
580
666
|
}
|
|
581
667
|
// 勾选 = 显示气泡。没有 agent 状态时也允许改,这样用户可以提前设好偏好,
|
|
582
668
|
// 而不必等下一次会话开始。
|
|
@@ -621,6 +707,7 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
621
707
|
self.statusIconButton.contentTintColor = [stateColor blendedColorWithFraction:0.18
|
|
622
708
|
ofColor:NSColor.blackColor] ?: stateColor;
|
|
623
709
|
[self resizeStatusCardToFitText];
|
|
710
|
+
[self layoutApprovalBadge];
|
|
624
711
|
[self positionAgentStatus];
|
|
625
712
|
if (self.statusBubbleExpanded) {
|
|
626
713
|
[self.statusPanel orderFrontRegardless];
|
|
@@ -651,6 +738,10 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
651
738
|
if (self.hasAgentStatus && [provider isEqualToString:self.lastStatusProvider] &&
|
|
652
739
|
[self isTrailingRecord:record afterState:self.lastStatusState]) return;
|
|
653
740
|
|
|
741
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
|
|
742
|
+
? record[@"terminal"] : nil;
|
|
743
|
+
self.lastTerminalFocusTarget = terminal.count > 0 ? terminal : nil;
|
|
744
|
+
|
|
654
745
|
[NSObject cancelPreviousPerformRequestsWithTarget:self
|
|
655
746
|
selector:@selector(hideAgentStatus) object:nil];
|
|
656
747
|
[NSObject cancelPreviousPerformRequestsWithTarget:self
|
|
@@ -700,15 +791,6 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
700
791
|
[self.pendingApprovalRecords removeObjectForKey:key];
|
|
701
792
|
}
|
|
702
793
|
}
|
|
703
|
-
- (NSDictionary *)latestPendingApprovalRecord {
|
|
704
|
-
NSDictionary *latest = nil;
|
|
705
|
-
for (NSDictionary *record in self.pendingApprovalRecords.allValues) {
|
|
706
|
-
if (!latest || [record[@"timestamp"] doubleValue] > [latest[@"timestamp"] doubleValue]) {
|
|
707
|
-
latest = record;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return latest;
|
|
711
|
-
}
|
|
712
794
|
- (void)displayAgentRecord:(NSDictionary *)record notify:(BOOL)shouldNotify {
|
|
713
795
|
NSString *event = [record[@"event"] isKindOfClass:NSString.class] ? record[@"event"] : @"";
|
|
714
796
|
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
@@ -722,6 +804,246 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
722
804
|
// 的 recentOnly + 5 秒 cutoff 挡住,再加上预算制和冷却,最坏也只多说一句。
|
|
723
805
|
[self considerSpeechForRecord:record];
|
|
724
806
|
}
|
|
807
|
+
- (NSString *)agentSessionKeyForRecord:(NSDictionary *)record {
|
|
808
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
809
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
|
|
810
|
+
? record[@"terminal"] : nil;
|
|
811
|
+
NSString *tty = SanitizedShortString(terminal[@"tty"], 64);
|
|
812
|
+
NSString *bundleID = SanitizedShortString(terminal[@"bundleID"], 128);
|
|
813
|
+
if (tty.length > 0) {
|
|
814
|
+
return [NSString stringWithFormat:@"%@|%@|%@", provider, bundleID, tty];
|
|
815
|
+
}
|
|
816
|
+
NSString *session = SanitizedShortString(record[@"session"], 128);
|
|
817
|
+
if (session.length > 0) return [NSString stringWithFormat:@"%@|%@", provider, session];
|
|
818
|
+
return terminal.count > 0 ? [NSString stringWithFormat:@"%@|%@", provider, bundleID] : nil;
|
|
819
|
+
}
|
|
820
|
+
- (NSString *)onlineAgentSessionKeyForProvider:(NSString *)provider tty:(NSString *)tty {
|
|
821
|
+
provider = SanitizedShortString(provider, 32);
|
|
822
|
+
tty = SanitizedShortString(tty.lastPathComponent, 64);
|
|
823
|
+
if (provider.length == 0 || tty.length == 0) return nil;
|
|
824
|
+
return [NSString stringWithFormat:@"%@|%@", provider, tty];
|
|
825
|
+
}
|
|
826
|
+
- (NSString *)onlineAgentSessionKeyForRecord:(NSDictionary *)record {
|
|
827
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
|
|
828
|
+
? record[@"terminal"] : nil;
|
|
829
|
+
return [self onlineAgentSessionKeyForProvider:record[@"provider"] tty:terminal[@"tty"]];
|
|
830
|
+
}
|
|
831
|
+
// 在线判定优先用包装脚本写出的 pid 文件:它给出精确的退出信号,会话一关列表就更新。
|
|
832
|
+
// 但直接跑 claude / codex(或终端窗口早于 shim 安装就已打开)的会话根本没有 pid 文件,
|
|
833
|
+
// 只按 pid 文件判定会把它们当场判死,列表里永远看不到——哪怕它们的 Hook 正在正常发事件。
|
|
834
|
+
// 这类会话退回活跃度宽限:最近一次事件在静默窗口内就算在线。某个 provider 一旦出现过
|
|
835
|
+
// pid 文件,说明包装脚本对它生效,继续按精确信号判定,不被宽限盖住。
|
|
836
|
+
- (BOOL)isAgentSessionRecordLive:(NSDictionary *)record {
|
|
837
|
+
NSString *onlineKey = [self onlineAgentSessionKeyForRecord:record];
|
|
838
|
+
if (onlineKey.length > 0 && [self.liveAgentSessionKeys containsObject:onlineKey]) return YES;
|
|
839
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
840
|
+
if (provider.length > 0 && [self.liveClientProviders containsObject:provider]) return NO;
|
|
841
|
+
return NSDate.date.timeIntervalSince1970 - [record[@"timestamp"] doubleValue]
|
|
842
|
+
< AgentStatusInactivityInterval;
|
|
843
|
+
}
|
|
844
|
+
- (void)pruneOfflineAgentSessionRecords {
|
|
845
|
+
for (NSString *key in self.agentSessionRecords.allKeys) {
|
|
846
|
+
if (![self isAgentSessionRecordLive:self.agentSessionRecords[key]]) {
|
|
847
|
+
[self.agentSessionRecords removeObjectForKey:key];
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
- (void)trackAgentSessionRecord:(NSDictionary *)record {
|
|
852
|
+
NSString *key = [self agentSessionKeyForRecord:record];
|
|
853
|
+
if (key.length == 0) return;
|
|
854
|
+
if (!self.agentSessionRecords) self.agentSessionRecords = [NSMutableDictionary dictionary];
|
|
855
|
+
self.agentSessionRecords[key] = record;
|
|
856
|
+
if (self.agentSessionRecords.count <= AgentSessionRecordLimit) return;
|
|
857
|
+
NSArray<NSDictionary *> *oldestFirst = [self.agentSessionRecords.allValues
|
|
858
|
+
sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
|
|
859
|
+
return [left[@"timestamp"] compare:right[@"timestamp"]];
|
|
860
|
+
}];
|
|
861
|
+
NSUInteger removeCount = oldestFirst.count - AgentSessionRecordLimit;
|
|
862
|
+
for (NSUInteger index = 0; index < removeCount; index++) {
|
|
863
|
+
NSString *oldKey = [self agentSessionKeyForRecord:oldestFirst[index]];
|
|
864
|
+
if (oldKey) [self.agentSessionRecords removeObjectForKey:oldKey];
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
- (BOOL)isApprovalSessionRecord:(NSDictionary *)record {
|
|
868
|
+
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
869
|
+
return [state isEqualToString:@"approval"];
|
|
870
|
+
}
|
|
871
|
+
// 卡在审批的在线会话,最近的排前面。角标计数和会话列表置顶共用这一份,两处数字
|
|
872
|
+
// 才不会打架。判据是"该会话的最后一条事件是审批"——审批一旦被处理,后续事件会
|
|
873
|
+
// 把这条记录顶掉,会话自然退出这个集合,不需要额外的解除信号。
|
|
874
|
+
- (NSArray<NSDictionary *> *)pendingApprovalSessionRecords {
|
|
875
|
+
[self pruneOfflineAgentSessionRecords];
|
|
876
|
+
NSMutableArray<NSDictionary *> *pending = [NSMutableArray array];
|
|
877
|
+
for (NSDictionary *record in self.agentSessionRecords.allValues) {
|
|
878
|
+
if ([self isApprovalSessionRecord:record]) [pending addObject:record];
|
|
879
|
+
}
|
|
880
|
+
[pending sortUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
|
|
881
|
+
return [right[@"timestamp"] compare:left[@"timestamp"]];
|
|
882
|
+
}];
|
|
883
|
+
return pending;
|
|
884
|
+
}
|
|
885
|
+
- (NSTimeInterval)stallIntervalForState:(NSString *)state {
|
|
886
|
+
if ([state isEqualToString:@"approval"]) return AgentApprovalStallInterval;
|
|
887
|
+
if ([state isEqualToString:@"thinking"]) return AgentThinkingStallInterval;
|
|
888
|
+
return 0;
|
|
889
|
+
}
|
|
890
|
+
// 会话停在同一个状态太久就提醒一次。去重键带上该会话当时的时间戳:会话一有新
|
|
891
|
+
// 事件,时间戳变了,旧键自然失效,于是下一次卡住还会再提醒。每轮用当前有效键
|
|
892
|
+
// 求交集,退出的会话不会在集合里留垃圾。
|
|
893
|
+
- (void)checkStalledAgentSessions {
|
|
894
|
+
if (self.agentSessionRecords.count == 0) {
|
|
895
|
+
[self.stallNotifiedSessionKeys removeAllObjects];
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
[self pruneOfflineAgentSessionRecords];
|
|
899
|
+
if (!self.stallNotifiedSessionKeys) self.stallNotifiedSessionKeys = [NSMutableSet set];
|
|
900
|
+
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
|
|
901
|
+
BOOL shouldNotify = [NSUserDefaults.standardUserDefaults boolForKey:NotificationStallKey];
|
|
902
|
+
NSMutableSet<NSString *> *valid = [NSMutableSet set];
|
|
903
|
+
NSDictionary *stalled = nil;
|
|
904
|
+
for (NSString *key in self.agentSessionRecords.allKeys) {
|
|
905
|
+
NSDictionary *record = self.agentSessionRecords[key];
|
|
906
|
+
NSTimeInterval timestamp = [record[@"timestamp"] doubleValue];
|
|
907
|
+
NSString *stamp = [NSString stringWithFormat:@"%@|%.0f", key, timestamp];
|
|
908
|
+
[valid addObject:stamp];
|
|
909
|
+
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
910
|
+
NSTimeInterval limit = [self stallIntervalForState:state];
|
|
911
|
+
if (limit <= 0 || now - timestamp < limit) continue;
|
|
912
|
+
if ([self.stallNotifiedSessionKeys containsObject:stamp]) continue;
|
|
913
|
+
[self.stallNotifiedSessionKeys addObject:stamp];
|
|
914
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
915
|
+
if (provider.length == 0) provider = @"Agent";
|
|
916
|
+
if (shouldNotify) {
|
|
917
|
+
NSInteger minutes = (NSInteger)((now - timestamp) / 60.0);
|
|
918
|
+
[self sendNotificationWithTitle:
|
|
919
|
+
[state isEqualToString:@"approval"] ? @"审批仍在等待" : @"Agent 长时间无响应"
|
|
920
|
+
body:[NSString stringWithFormat:@"%@ 已%@ %ld 分钟。",
|
|
921
|
+
provider, [self statusTextForState:state tool:record[@"tool"]], (long)minutes]];
|
|
922
|
+
}
|
|
923
|
+
// 多个会话同时卡住时只把最旧的那条顶到气泡上:它等得最久。
|
|
924
|
+
if (!stalled || [record[@"timestamp"] doubleValue] < [stalled[@"timestamp"] doubleValue]) {
|
|
925
|
+
stalled = record;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
[self.stallNotifiedSessionKeys intersectSet:valid];
|
|
929
|
+
if (!stalled) return;
|
|
930
|
+
NSString *event = [stalled[@"event"] isKindOfClass:NSString.class] ? stalled[@"event"] : @"";
|
|
931
|
+
NSString *tool = [stalled[@"tool"] isKindOfClass:NSString.class] ? stalled[@"tool"] : @"";
|
|
932
|
+
[self.petView handleAgentEvent:event tool:tool failed:NO];
|
|
933
|
+
[self presentStalledRecord:stalled];
|
|
934
|
+
}
|
|
935
|
+
// 卡住提醒要把气泡重新推到眼前,所以这里不走 presentPendingApprovalRecord 那条
|
|
936
|
+
// 带早退的回落路径——面板此刻很可能已经因为长时间沉默被收走了,必须强制重现。
|
|
937
|
+
- (void)presentStalledRecord:(NSDictionary *)record {
|
|
938
|
+
NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
|
|
939
|
+
? record[@"terminal"] : nil;
|
|
940
|
+
if (terminal.count > 0) self.lastTerminalFocusTarget = terminal;
|
|
941
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
942
|
+
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
943
|
+
self.hasAgentStatus = YES;
|
|
944
|
+
[NSObject cancelPreviousPerformRequestsWithTarget:self
|
|
945
|
+
selector:@selector(hideAgentStatus) object:nil];
|
|
946
|
+
[self applyStatusPresentationForState:state
|
|
947
|
+
provider:provider.length > 0 ? provider : @"Agent"
|
|
948
|
+
tool:[record[@"tool"] isKindOfClass:NSString.class] ? record[@"tool"] : @""];
|
|
949
|
+
[self.panel orderFrontRegardless];
|
|
950
|
+
[self performSelector:@selector(hideAgentStatus) withObject:nil
|
|
951
|
+
afterDelay:AgentStatusInactivityInterval];
|
|
952
|
+
}
|
|
953
|
+
- (void)layoutApprovalBadge {
|
|
954
|
+
if (!self.approvalBadgeView) return;
|
|
955
|
+
// statusIconButton 在玻璃卡片内,角标在卡片外层,差一个 6 点的卡片边距。
|
|
956
|
+
// 再各让出 4 点压到图标身上,看起来才是"贴在图标角上"而不是浮在旁边。
|
|
957
|
+
NSRect icon = self.statusIconButton.frame;
|
|
958
|
+
self.approvalBadgeView.frame = NSMakeRect(
|
|
959
|
+
6 + NSMaxX(icon) - PetApprovalBadgeSize + 4,
|
|
960
|
+
6 + NSMaxY(icon) - PetApprovalBadgeSize + 4,
|
|
961
|
+
PetApprovalBadgeSize, PetApprovalBadgeSize);
|
|
962
|
+
}
|
|
963
|
+
- (void)refreshApprovalBadge {
|
|
964
|
+
CCPetsApprovalBadgeView *badge = (CCPetsApprovalBadgeView *)self.approvalBadgeView;
|
|
965
|
+
if (!badge) return;
|
|
966
|
+
badge.count = [self pendingApprovalSessionRecords].count;
|
|
967
|
+
[self layoutApprovalBadge];
|
|
968
|
+
}
|
|
969
|
+
// 等审批的排最前面。纯按时间倒序会把它们推到最底下——它们的时间戳按定义只会
|
|
970
|
+
// 越来越旧,而它们恰恰是唯一需要用户动手的那几条。
|
|
971
|
+
- (NSArray<NSDictionary *> *)recentAgentSessionRecords {
|
|
972
|
+
[self pruneOfflineAgentSessionRecords];
|
|
973
|
+
NSArray<NSDictionary *> *records = [self.agentSessionRecords.allValues
|
|
974
|
+
sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
|
|
975
|
+
BOOL leftApproval = [self isApprovalSessionRecord:left];
|
|
976
|
+
BOOL rightApproval = [self isApprovalSessionRecord:right];
|
|
977
|
+
if (leftApproval != rightApproval) return leftApproval ? NSOrderedAscending : NSOrderedDescending;
|
|
978
|
+
return [right[@"timestamp"] compare:left[@"timestamp"]];
|
|
979
|
+
}];
|
|
980
|
+
if (records.count <= AgentSessionMenuLimit) return records;
|
|
981
|
+
return [records subarrayWithRange:NSMakeRange(0, AgentSessionMenuLimit)];
|
|
982
|
+
}
|
|
983
|
+
- (NSString *)terminalNameForTarget:(NSDictionary *)target {
|
|
984
|
+
NSString *program = SanitizedShortString(target[@"program"], 64);
|
|
985
|
+
NSString *lower = program.lowercaseString;
|
|
986
|
+
if ([lower isEqualToString:@"apple_terminal"]) return @"Terminal";
|
|
987
|
+
if ([lower containsString:@"iterm"]) return @"iTerm2";
|
|
988
|
+
if ([lower isEqualToString:@"vscode"]) return @"VS Code";
|
|
989
|
+
if ([lower containsString:@"jetbrains"]) return @"JetBrains";
|
|
990
|
+
if ([lower containsString:@"ghostty"]) return @"Ghostty";
|
|
991
|
+
if ([lower containsString:@"warp"]) return @"Warp";
|
|
992
|
+
if ([lower containsString:@"wezterm"]) return @"WezTerm";
|
|
993
|
+
if (program.length > 0) return program;
|
|
994
|
+
NSString *bundleID = SanitizedShortString(target[@"bundleID"], 128);
|
|
995
|
+
return bundleID.length > 0 ? bundleID : @"Terminal";
|
|
996
|
+
}
|
|
997
|
+
- (void)focusAgentSessionRecord:(NSMenuItem *)sender {
|
|
998
|
+
NSDictionary *record = [sender.representedObject isKindOfClass:NSDictionary.class]
|
|
999
|
+
? sender.representedObject : nil;
|
|
1000
|
+
NSDictionary *target = [record[@"terminal"] isKindOfClass:NSDictionary.class]
|
|
1001
|
+
? record[@"terminal"] : nil;
|
|
1002
|
+
if (target.count > 0) ActivateTerminalFocusTarget(target);
|
|
1003
|
+
}
|
|
1004
|
+
- (void)showAgentSessionsMenu:(NSButton *)sender {
|
|
1005
|
+
NSArray<NSDictionary *> *records = [self recentAgentSessionRecords];
|
|
1006
|
+
if (records.count == 0) return;
|
|
1007
|
+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"最近 Agent 会话"];
|
|
1008
|
+
NSMenuItem *heading = [menu addItemWithTitle:@"最近 Agent 会话" action:nil keyEquivalent:@""];
|
|
1009
|
+
heading.enabled = NO;
|
|
1010
|
+
[menu addItem:NSMenuItem.separatorItem];
|
|
1011
|
+
BOOL separatedApprovals = NO;
|
|
1012
|
+
for (NSDictionary *record in records) {
|
|
1013
|
+
NSString *provider = SanitizedShortString(record[@"provider"], 32);
|
|
1014
|
+
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
1015
|
+
NSString *tool = [record[@"tool"] isKindOfClass:NSString.class] ? record[@"tool"] : @"";
|
|
1016
|
+
NSDictionary *target = record[@"terminal"];
|
|
1017
|
+
BOOL approval = [self isApprovalSessionRecord:record];
|
|
1018
|
+
// 置顶的审批组和其余会话之间划一道线,免得两段看起来像同一个时间序列。
|
|
1019
|
+
if (!approval && !separatedApprovals && record != records.firstObject) {
|
|
1020
|
+
[menu addItem:NSMenuItem.separatorItem];
|
|
1021
|
+
separatedApprovals = YES;
|
|
1022
|
+
}
|
|
1023
|
+
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[record[@"timestamp"] doubleValue]];
|
|
1024
|
+
NSString *time = [NSDateFormatter localizedStringFromDate:date
|
|
1025
|
+
dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
|
|
1026
|
+
NSString *title = [NSString stringWithFormat:@"%@%@ · %@ · %@ · %@",
|
|
1027
|
+
approval ? @"⚠️ " : @"",
|
|
1028
|
+
provider.length > 0 ? provider : @"Agent",
|
|
1029
|
+
[self statusTextForState:state tool:tool], [self terminalNameForTarget:target], time];
|
|
1030
|
+
NSMenuItem *item = [menu addItemWithTitle:title
|
|
1031
|
+
action:@selector(focusAgentSessionRecord:) keyEquivalent:@""];
|
|
1032
|
+
item.target = self;
|
|
1033
|
+
item.representedObject = record;
|
|
1034
|
+
if ([target isEqual:self.lastTerminalFocusTarget]) item.state = NSControlStateValueOn;
|
|
1035
|
+
}
|
|
1036
|
+
[menu popUpMenuPositioningItem:nil
|
|
1037
|
+
atLocation:NSMakePoint(0, NSHeight(sender.bounds) + 4) inView:sender];
|
|
1038
|
+
}
|
|
1039
|
+
- (BOOL)focusLatestAgentTerminal {
|
|
1040
|
+
// 只有 Hook 状态气泡上的透明按钮会调用这里;桌宠本体继续负责原有互动。
|
|
1041
|
+
if (!self.hasAgentStatus || self.lastTerminalFocusTarget.count == 0) return NO;
|
|
1042
|
+
return ActivateTerminalFocusTarget(self.lastTerminalFocusTarget);
|
|
1043
|
+
}
|
|
1044
|
+
- (void)focusLatestAgentTerminal:(id)sender {
|
|
1045
|
+
[self focusLatestAgentTerminal];
|
|
1046
|
+
}
|
|
725
1047
|
- (NSDictionary *)petManifestInDirectory:(NSString *)directory {
|
|
726
1048
|
NSString *jsonPath = [directory stringByAppendingPathComponent:@"pet.json"];
|
|
727
1049
|
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
|
|
@@ -947,15 +1269,6 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
947
1269
|
}
|
|
948
1270
|
self.statusBubbleExpanded = [defaults boolForKey:StatusBubbleExpandedKey];
|
|
949
1271
|
NSString *selected = [defaults stringForKey:@"CCPetsSelectedSprite"];
|
|
950
|
-
if (selected.length == 0) {
|
|
951
|
-
NSDictionary *legacyDefaults = [defaults persistentDomainForName:@"com.universewang.codex-pet"];
|
|
952
|
-
NSString *legacySelected = [legacyDefaults[@"CodexPetSelectedSprite"] isKindOfClass:NSString.class]
|
|
953
|
-
? legacyDefaults[@"CodexPetSelectedSprite"] : nil;
|
|
954
|
-
if (legacySelected.length > 0) {
|
|
955
|
-
selected = legacySelected;
|
|
956
|
-
[defaults setObject:selected forKey:@"CCPetsSelectedSprite"];
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
1272
|
if (selected.length > 0 && ![selected containsString:@":"]) selected = [@"builtin:" stringByAppendingString:selected];
|
|
960
1273
|
NSDictionary *selectedOption = [self petOptionWithID:selected inOptions:petOptions] ?: petOptions.firstObject;
|
|
961
1274
|
selected = selectedOption[@"id"];
|
|
@@ -1031,7 +1344,7 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
1031
1344
|
self.statusPanel.opaque = NO;
|
|
1032
1345
|
self.statusPanel.backgroundColor = NSColor.clearColor;
|
|
1033
1346
|
self.statusPanel.hasShadow = NO;
|
|
1034
|
-
self.statusPanel.ignoresMouseEvents =
|
|
1347
|
+
self.statusPanel.ignoresMouseEvents = NO;
|
|
1035
1348
|
self.statusPanel.level = NSFloatingWindowLevel;
|
|
1036
1349
|
self.statusPanel.collectionBehavior =
|
|
1037
1350
|
NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorFullScreenAuxiliary;
|
|
@@ -1085,15 +1398,37 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
1085
1398
|
self.statusDetailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
1086
1399
|
[self.statusGlass addSubview:self.statusDetailLabel];
|
|
1087
1400
|
|
|
1088
|
-
self.statusIconButton = [[
|
|
1401
|
+
self.statusIconButton = [[CCPetsStatusClickButton alloc] initWithFrame:NSMakeRect(
|
|
1089
1402
|
statusGlassSize.width - 48, 12, 34, 34)];
|
|
1090
1403
|
self.statusIconButton.bordered = NO;
|
|
1091
1404
|
self.statusIconButton.imagePosition = NSImageOnly;
|
|
1092
1405
|
self.statusIconButton.wantsLayer = YES;
|
|
1093
1406
|
self.statusIconButton.layer.cornerRadius = 17;
|
|
1094
1407
|
self.statusIconButton.layer.masksToBounds = YES;
|
|
1408
|
+
self.statusIconButton.toolTip = @"查看最近 Agent 会话";
|
|
1409
|
+
self.statusIconButton.target = self;
|
|
1410
|
+
self.statusIconButton.action = @selector(showAgentSessionsMenu:);
|
|
1095
1411
|
[self.statusGlass addSubview:self.statusIconButton];
|
|
1096
1412
|
[statusRoot addSubview:self.statusGlass];
|
|
1413
|
+
CCPetsStatusClickButton *statusClick = [[CCPetsStatusClickButton alloc]
|
|
1414
|
+
initWithFrame:NSMakeRect(6, 6, statusGlassSize.width - 56, statusGlassSize.height)];
|
|
1415
|
+
statusClick.bordered = NO;
|
|
1416
|
+
statusClick.transparent = YES;
|
|
1417
|
+
statusClick.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
1418
|
+
statusClick.title = @"";
|
|
1419
|
+
statusClick.toolTip = @"返回触发此状态的 Agent 终端";
|
|
1420
|
+
statusClick.target = self;
|
|
1421
|
+
statusClick.action = @selector(focusLatestAgentTerminal:);
|
|
1422
|
+
self.statusClickButton = statusClick;
|
|
1423
|
+
[statusRoot addSubview:statusClick];
|
|
1424
|
+
// 角标挂在 statusRoot 而不是 statusGlass 里:玻璃卡片是 masksToBounds 的胶囊,
|
|
1425
|
+
// 右上角正好落在圆角外面,放进去会被裁掉一半。
|
|
1426
|
+
CCPetsApprovalBadgeView *badge = [[CCPetsApprovalBadgeView alloc]
|
|
1427
|
+
initWithFrame:NSMakeRect(0, 0, PetApprovalBadgeSize, PetApprovalBadgeSize)];
|
|
1428
|
+
badge.hidden = YES;
|
|
1429
|
+
self.approvalBadgeView = badge;
|
|
1430
|
+
[statusRoot addSubview:badge];
|
|
1431
|
+
[self layoutApprovalBadge];
|
|
1097
1432
|
self.statusPanel.contentView = statusRoot;
|
|
1098
1433
|
|
|
1099
1434
|
NSSize quotaSize = NSMakeSize(QuotaLogicalWidth * QuotaScale, QuotaLogicalHeight * QuotaScale);
|
|
@@ -1144,6 +1479,7 @@ static NSString *const PetSpeechFrequencyChatty = @"chatty";
|
|
|
1144
1479
|
self.systemMonitor = [CCPetsSystemMonitor new];
|
|
1145
1480
|
self.agentEventPartialLine = [NSMutableData data];
|
|
1146
1481
|
self.pendingApprovalRecords = [NSMutableDictionary dictionary];
|
|
1482
|
+
self.stallNotifiedSessionKeys = [NSMutableSet set];
|
|
1147
1483
|
self.usageMonitor.changeHandler = ^(NSDictionary *codexUsage, NSDictionary *claudeUsage) {
|
|
1148
1484
|
[weakSelf applyCodexUsage:codexUsage claudeUsage:claudeUsage];
|
|
1149
1485
|
[weakSelf considerQuotaSpeech];
|
|
@@ -1708,6 +2044,7 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1708
2044
|
}
|
|
1709
2045
|
self.statusIconButton.frame = NSMakeRect(glassWidth - trailing - iconWidth,
|
|
1710
2046
|
(height - iconWidth) / 2.0, iconWidth, iconWidth);
|
|
2047
|
+
self.statusClickButton.frame = NSMakeRect(6, 6, glassWidth - 56, height);
|
|
1711
2048
|
}
|
|
1712
2049
|
|
|
1713
2050
|
// 独立气泡:左右各 16 内边距,没有图标。
|
|
@@ -1851,6 +2188,7 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1851
2188
|
NSDictionary *record = [NSJSONSerialization JSONObjectWithData:lineData options:0 error:nil];
|
|
1852
2189
|
if (![record isKindOfClass:NSDictionary.class]) continue;
|
|
1853
2190
|
if (recentOnly && [record[@"timestamp"] doubleValue] < cutoff) continue;
|
|
2191
|
+
[self trackAgentSessionRecord:record];
|
|
1854
2192
|
NSString *event = record[@"event"];
|
|
1855
2193
|
NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
|
|
1856
2194
|
if ([event isKindOfClass:NSString.class]) {
|
|
@@ -1859,7 +2197,6 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1859
2197
|
}
|
|
1860
2198
|
[self prunePendingApprovalRecords];
|
|
1861
2199
|
NSString *approvalKey = [self approvalKeyForRecord:record];
|
|
1862
|
-
NSDictionary *previousPriority = [self latestPendingApprovalRecord];
|
|
1863
2200
|
BOOL manualApproval = [state isEqualToString:@"approval"];
|
|
1864
2201
|
if (manualApproval) {
|
|
1865
2202
|
self.pendingApprovalRecords[approvalKey] = record;
|
|
@@ -1867,18 +2204,12 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1867
2204
|
[self.pendingApprovalRecords removeObjectForKey:approvalKey];
|
|
1868
2205
|
}
|
|
1869
2206
|
[self prunePendingApprovalRecords];
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
if (manualApproval) {
|
|
1873
|
-
[self displayAgentRecord:currentPriority notify:YES];
|
|
1874
|
-
} else if (currentPriority != previousPriority) {
|
|
1875
|
-
[self displayAgentRecord:currentPriority notify:NO];
|
|
1876
|
-
}
|
|
1877
|
-
continue;
|
|
1878
|
-
}
|
|
2207
|
+
// 多会话下不能让一个未处理的审批全局挡住其他 Agent 的所有 Hook。每条事件
|
|
2208
|
+
// 都正常驱动气泡和动画;仍在等待的审批保留在多会话记录中供用户回跳。
|
|
1879
2209
|
[self displayAgentRecord:record notify:YES];
|
|
1880
2210
|
}
|
|
1881
2211
|
}
|
|
2212
|
+
[self refreshApprovalBadge];
|
|
1882
2213
|
}
|
|
1883
2214
|
- (void)readNewAgentEvents {
|
|
1884
2215
|
NSString *path = AgentEventPath();
|
|
@@ -1954,6 +2285,26 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1954
2285
|
self.quotaView.hasUnlabeledClient = self.hasUnlabeledClient;
|
|
1955
2286
|
self.quotaView.needsDisplay = YES;
|
|
1956
2287
|
}
|
|
2288
|
+
// 客户端是否真的还在跑。只问 kill(pid, 0) 不够:关掉终端窗口只是销毁 pty,claude /
|
|
2289
|
+
// codex(Node 进程)不一定跟着 SIGHUP 退出,会变成脱离控制终端的孤儿继续活着。那样
|
|
2290
|
+
// pid 一直存在,会话就永久挂在"在线"上,最近会话列表里那条也永远不消失。
|
|
2291
|
+
// 所以再看两件事:进程是否还有控制终端;以及它是不是仍然是 pid 文件里记的那个 TTY
|
|
2292
|
+
// (pty 会被新开的窗口复用,光看"有 tty"挡不住换了主人的情况)。
|
|
2293
|
+
// recordedTTY 为空是 1.0.2 及更早的老客户端,只能退回"有控制终端"这一条。
|
|
2294
|
+
static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
|
|
2295
|
+
if (pid <= 1) return NO;
|
|
2296
|
+
struct kinfo_proc info;
|
|
2297
|
+
size_t length = sizeof(info);
|
|
2298
|
+
int name[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
|
|
2299
|
+
if (sysctl(name, 4, &info, &length, NULL, 0) != 0 || length == 0) return NO;
|
|
2300
|
+
if (info.kp_proc.p_stat == SZOMB) return NO;
|
|
2301
|
+
dev_t device = info.kp_eproc.e_tdev;
|
|
2302
|
+
if (device == NODEV) return NO;
|
|
2303
|
+
if (recordedTTY.length == 0) return YES;
|
|
2304
|
+
const char *current = devname(device, S_IFCHR);
|
|
2305
|
+
if (!current) return NO;
|
|
2306
|
+
return [recordedTTY isEqualToString:@(current).lastPathComponent];
|
|
2307
|
+
}
|
|
1957
2308
|
- (void)refreshClientLifecycle:(id)sender {
|
|
1958
2309
|
[self considerIdleSpeech];
|
|
1959
2310
|
[self prunePendingApprovalRecords];
|
|
@@ -1962,23 +2313,31 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1962
2313
|
NSArray<NSString *> *entries = [NSFileManager.defaultManager contentsOfDirectoryAtPath:clientDirectory error:nil] ?: @[];
|
|
1963
2314
|
NSInteger liveClients = 0;
|
|
1964
2315
|
NSMutableSet<NSString *> *providers = [NSMutableSet set];
|
|
2316
|
+
NSMutableSet<NSString *> *sessionKeys = [NSMutableSet set];
|
|
1965
2317
|
BOOL unlabeled = NO;
|
|
1966
2318
|
for (NSString *entry in entries) {
|
|
1967
2319
|
pid_t pid = (pid_t)entry.intValue;
|
|
1968
|
-
BOOL alive = pid > 1 && (kill(pid, 0) == 0 || errno == EPERM);
|
|
1969
2320
|
NSString *path = [clientDirectory stringByAppendingPathComponent:entry];
|
|
1970
|
-
|
|
2321
|
+
// 包装脚本会把 provider 名写进 pid 文件第一行、TTY 写进第二行。1.0.2 及更早
|
|
2322
|
+
// 的版本只 touch 出空文件,升级后仍在运行的老客户端读出来是空的:这类当作
|
|
2323
|
+
// “身份不明”,只要还有一个就不清场,避免把仍然活着的会话误判成已退出。
|
|
2324
|
+
NSString *contents = [NSString stringWithContentsOfFile:path
|
|
2325
|
+
encoding:NSUTF8StringEncoding error:nil] ?: @"";
|
|
2326
|
+
NSArray<NSString *> *lines = [contents componentsSeparatedByCharactersInSet:
|
|
2327
|
+
NSCharacterSet.newlineCharacterSet];
|
|
2328
|
+
NSString *label = [lines.firstObject
|
|
2329
|
+
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
2330
|
+
NSString *tty = lines.count > 1 ? [lines[1]
|
|
2331
|
+
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] : @"";
|
|
2332
|
+
if (!ClientProcessAlive(pid, tty)) {
|
|
1971
2333
|
[NSFileManager.defaultManager removeItemAtPath:path error:nil];
|
|
1972
2334
|
continue;
|
|
1973
2335
|
}
|
|
1974
2336
|
liveClients += 1;
|
|
1975
|
-
// 包装脚本会把 provider 名写进 pid 文件。1.0.2 及更早的版本只 touch 出
|
|
1976
|
-
// 空文件,升级后仍在运行的老客户端读出来是空的:这类当作“身份不明”,
|
|
1977
|
-
// 只要还有一个就不清场,避免把仍然活着的会话误判成已退出。
|
|
1978
|
-
NSString *label = [[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]
|
|
1979
|
-
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
1980
2337
|
if (label.length > 0 && label.length <= 32) [providers addObject:label];
|
|
1981
2338
|
else unlabeled = YES;
|
|
2339
|
+
NSString *sessionKey = [self onlineAgentSessionKeyForProvider:label tty:tty];
|
|
2340
|
+
if (sessionKey.length > 0) [sessionKeys addObject:sessionKey];
|
|
1982
2341
|
}
|
|
1983
2342
|
// 包装脚本启动的客户端有精确的退出信号:pid 文件被回收。这一段不该被为"直接跑
|
|
1984
2343
|
// claude / codex"准备的 60 秒活跃度宽限盖住,否则退出后还要挂满一分钟才转离线。
|
|
@@ -1987,7 +2346,9 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1987
2346
|
NSSet<NSString *> *previousProviders = self.liveClientProviders;
|
|
1988
2347
|
self.liveClientCount = liveClients;
|
|
1989
2348
|
self.liveClientProviders = providers;
|
|
2349
|
+
self.liveAgentSessionKeys = sessionKeys;
|
|
1990
2350
|
self.hasUnlabeledClient = unlabeled;
|
|
2351
|
+
[self pruneOfflineAgentSessionRecords];
|
|
1991
2352
|
for (NSString *provider in previousProviders) {
|
|
1992
2353
|
if (![providers containsObject:provider]) {
|
|
1993
2354
|
[self.providerActivityAt removeObjectForKey:provider];
|
|
@@ -1996,6 +2357,8 @@ static CGFloat PetMeasuredLabelWidth(NSTextField *label) {
|
|
|
1996
2357
|
[self updateQuotaLiveState];
|
|
1997
2358
|
[self refreshDetectedProviders];
|
|
1998
2359
|
[self hideAgentStatusIfClientGone];
|
|
2360
|
+
[self checkStalledAgentSessions];
|
|
2361
|
+
[self refreshApprovalBadge];
|
|
1999
2362
|
// 启动模式在应用生命周期内保持不变。手动启动的桌宠即使后来检测到
|
|
2000
2363
|
// Codex/Claude 客户端,也不应被转成 CLI 托管模式并随客户端退出。
|
|
2001
2364
|
if (liveClients == 0 && self.managedByCLI) [NSApp terminate:nil];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 通过 `codex app-server` 的 account/rateLimits/read 读取服务端当前额度。会话转录里的
|
|
4
|
+
// rate_limits 只是一份历史快照,不能作为当前窗口的唯一事实来源。
|
|
5
|
+
@interface CCPetsCodexRateLimitsReader : NSObject
|
|
6
|
+
@property(copy) void (^liveUsageHandler)(NSDictionary *liveUsage);
|
|
7
|
+
- (void)refresh;
|
|
8
|
+
- (void)stop;
|
|
9
|
+
@end
|
|
10
|
+
|
|
11
|
+
// 把实时额度覆盖到会话读取器产出的用量上;本机 Token 聚合等非官方字段会被保留。
|
|
12
|
+
NSDictionary *CodexUsageByApplyingLiveUsage(NSDictionary *sessionUsage,
|
|
13
|
+
NSDictionary *liveUsage);
|