cc-pets 2.4.0 → 2.6.0-beta.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 +93 -14
- package/CC_BRIDGE.zh-CN.md +71 -11
- 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 +116 -34
- package/README.zh-CN.md +99 -35
- package/Resources/AgentIcons/antigravity.icns +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +99 -26
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +51 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +120 -17
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +24 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +24 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +21 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +33 -17
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +43 -7
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +7 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +7 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +24 -22
- package/Sources/CCPets/App/CCPetsAppDelegate.h +10 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.m +18 -8
- package/Sources/CCPets/App/CCPetsVersion.h +4 -0
- package/Sources/CCPets/App/CCPetsVersion.m +34 -0
- package/Sources/CCPets/App/main.m +11 -0
- package/Sources/CCPets/Bridge/CCPetsBridge.m +41 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeAntigravityApp.h +4 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeAntigravityApp.m +119 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +3 -1
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +272 -50
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +12 -3
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +341 -95
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +227 -95
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +35 -5
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +310 -42
- package/Sources/CCPets/Bridge/CCPetsBridgeTerminalTitle.h +12 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeTerminalTitle.m +105 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityConfig.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityConfig.m +462 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsAntigravityEvents.m +180 -0
- package/Sources/CCPets/Integrations/CCPetsBridgeTitleRelay.h +3 -0
- package/Sources/CCPets/Integrations/CCPetsBridgeTitleRelay.m +463 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +1 -1
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +38 -8
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +15 -5
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +2 -1
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +112 -81
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +11 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +217 -45
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/CCPetsPreviews.m +16 -8
- package/Sources/CCPets/Pet/PetView+Menu.m +64 -5
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsDoctor.m +126 -37
- package/Sources/CCPets/Support/CCPetsPaths.h +5 -0
- package/Sources/CCPets/Support/CCPetsPaths.m +15 -0
- package/Sources/CCPets/Support/CCPetsPreferences.h +14 -1
- package/Sources/CCPets/Support/CCPetsPreferences.m +36 -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 +533 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +98 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +7 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +205 -8
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +3 -1
- package/Sources/CCPets/UI/CCPetsUsagePage.h +4 -3
- package/Sources/CCPets/UI/CCPetsUsagePage.m +69 -43
- package/Sources/CCPets/UI/CCPetsUsageStore.m +11 -1
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +6 -4
- package/Sources/CCPets/Usage/CCPetsAntigravityQuota.m +115 -0
- package/Sources/CCPets/Usage/CCPetsNormalizedUsage.h +5 -0
- package/Sources/CCPets/Usage/CCPetsNormalizedUsage.m +43 -0
- package/Sources/CCPets/Usage/CCPetsProviderRegistry+Testing.h +13 -0
- package/Sources/CCPets/Usage/CCPetsProviderRegistry.h +25 -0
- package/Sources/CCPets/Usage/CCPetsProviderRegistry.m +94 -0
- package/Sources/CCPets/Usage/CCPetsQuotaHistory.h +1 -1
- package/Sources/CCPets/Usage/CCPetsQuotaHistory.m +11 -8
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +2 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +206 -14
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +4 -4
- package/Sources/CCPets/Usage/CCPetsUsage.h +7 -2
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +2 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +29 -4
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.h +4 -4
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +90 -7
- package/Sources/CCPets/Usage/QuotaDashboardView+CompactRow.m +81 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +5 -8
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +26 -4
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +99 -99
- package/Sources/CCPets/Usage/QuotaDashboardView.h +9 -10
- package/Sources/CCPets/Usage/QuotaDashboardView.m +232 -79
- package/THIRD_PARTY_NOTICES.md +5 -4
- package/THIRD_PARTY_NOTICES.zh-CN.md +6 -5
- package/bin/agy-with-pet +82 -0
- package/bin/cc-pets +1 -1
- package/bin/claude-with-pet +15 -0
- package/bin/codex-with-pet +17 -2
- package/package.json +5 -3
- package/scripts/install-shell-integration.sh +5 -0
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
#import "CCPetsCLISupport.h"
|
|
5
5
|
#import "CCPetsJSON.h"
|
|
6
6
|
#import <CommonCrypto/CommonDigest.h>
|
|
7
|
+
#import <libproc.h>
|
|
7
8
|
#import <sys/sysctl.h>
|
|
8
9
|
#import <sys/stat.h>
|
|
9
10
|
#import <sys/file.h>
|
|
@@ -16,6 +17,8 @@
|
|
|
16
17
|
|
|
17
18
|
const NSUInteger CCBridgeMessageBodyLimit = 16 * 1024;
|
|
18
19
|
const long long CCBridgeMessageTTL = 24LL * 60 * 60 * 1000;
|
|
20
|
+
NSString *const CCBridgeAntigravityProvider = @"Antigravity";
|
|
21
|
+
NSString *const CCBridgeAntigravityAppProvider = @"Antigravity App";
|
|
19
22
|
// 同一对会话 10 分钟内来回超过这个数,基本可以断定是两个 Agent 在互相自动回复。
|
|
20
23
|
static const long long PairLimitWindow = 10LL * 60 * 1000;
|
|
21
24
|
const NSUInteger CCBridgePairLimitCount = 20;
|
|
@@ -40,6 +43,10 @@ static NSString *RandomHex(NSUInteger bytes) {
|
|
|
40
43
|
return hex;
|
|
41
44
|
}
|
|
42
45
|
|
|
46
|
+
static BOOL IsTerminalStatus(id status) {
|
|
47
|
+
return [@[@"delivered", @"expired", @"undeliverable"] containsObject:status ?: @""];
|
|
48
|
+
}
|
|
49
|
+
|
|
43
50
|
NSString *CCBridgeSHA1Hex(NSString *text) {
|
|
44
51
|
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
|
|
45
52
|
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
|
|
@@ -83,7 +90,7 @@ void CCBridgeSetEnabled(BOOL enabled, NSDictionary *options) {
|
|
|
83
90
|
EnsureHomeDirectory();
|
|
84
91
|
NSDictionary *normalized = CCBridgeNormalizeOptions(options);
|
|
85
92
|
CCPetsJSONObject *document = [CCPetsJSONObject new];
|
|
86
|
-
for (NSString *key in @[@"codexApprove", @"claudeAllow", @"wake", @"editGuard"]) document[key] = normalized[key];
|
|
93
|
+
for (NSString *key in @[@"codexApprove", @"claudeAllow", @"antigravityAllow", @"antigravityApp", @"tabTitle", @"wake", @"editGuard"]) document[key] = normalized[key];
|
|
87
94
|
NSISO8601DateFormatter *formatter = [NSISO8601DateFormatter new];
|
|
88
95
|
formatter.formatOptions = NSISO8601DateFormatWithInternetDateTime | NSISO8601DateFormatWithFractionalSeconds;
|
|
89
96
|
document[@"enabledAt"] = [formatter stringFromDate:NSDate.date];
|
|
@@ -160,6 +167,21 @@ NSDictionary *CCBridgeMergeOptionFlags(NSDictionary *base, NSDictionary<NSString
|
|
|
160
167
|
if (!tools) return nil;
|
|
161
168
|
next[@"claudeAllow"] = tools;
|
|
162
169
|
}
|
|
170
|
+
if (flags[@"antigravityAllow"]) {
|
|
171
|
+
NSArray *tools = CCBridgeParseToolList(flags[@"antigravityAllow"], @"--agy-allow", error);
|
|
172
|
+
if (!tools) return nil;
|
|
173
|
+
next[@"antigravityAllow"] = tools;
|
|
174
|
+
}
|
|
175
|
+
if (flags[@"antigravityApp"]) {
|
|
176
|
+
NSNumber *value = CCBridgeParseSwitch(flags[@"antigravityApp"], @"--agy-app", error);
|
|
177
|
+
if (!value) return nil;
|
|
178
|
+
next[@"antigravityApp"] = value;
|
|
179
|
+
}
|
|
180
|
+
if (flags[@"tabTitle"]) {
|
|
181
|
+
NSNumber *value = CCBridgeParseSwitch(flags[@"tabTitle"], @"--tab-title", error);
|
|
182
|
+
if (!value) return nil;
|
|
183
|
+
next[@"tabTitle"] = value;
|
|
184
|
+
}
|
|
163
185
|
if (flags[@"wake"]) {
|
|
164
186
|
NSNumber *value = CCBridgeParseSwitch(flags[@"wake"], @"--wake", error);
|
|
165
187
|
if (!value) return nil;
|
|
@@ -176,9 +198,13 @@ NSDictionary *CCBridgeMergeOptionFlags(NSDictionary *base, NSDictionary<NSString
|
|
|
176
198
|
NSArray<NSString *> *CCBridgeDescribeOptions(NSDictionary *options) {
|
|
177
199
|
NSArray *codex = options[@"codexApprove"];
|
|
178
200
|
NSArray *claude = options[@"claudeAllow"];
|
|
201
|
+
NSArray *agy = options[@"antigravityAllow"];
|
|
179
202
|
return @[
|
|
180
203
|
LF(L(@"Codex auto-approve: {codexApprove}"), (@{@"codexApprove": codex.count > 0 ? [codex componentsJoinedByString:@", "] : L(@"none")})),
|
|
181
204
|
LF(L(@"Claude allow without asking: {claudeAllow}"), (@{@"claudeAllow": claude.count > 0 ? [claude componentsJoinedByString:@", "] : L(@"none")})),
|
|
205
|
+
LF(L(@"Antigravity allow without asking: {agyAllow}"), (@{@"agyAllow": agy.count > 0 ? [agy componentsJoinedByString:@", "] : L(@"none")})),
|
|
206
|
+
LF(L(@"Antigravity App integration: {agyApp}"), (@{@"agyApp": [options[@"antigravityApp"] isEqual:@YES] ? L(@"on") : L(@"off")})),
|
|
207
|
+
LF(L(@"Bridge nicknames in tab titles: {tabTitle}"), (@{@"tabTitle": [options[@"tabTitle"] boolValue] ? L(@"on") : L(@"off")})),
|
|
182
208
|
LF(L(@"Wake idle sessions: {wake}"), (@{@"wake": [options[@"wake"] boolValue] ? L(@"on") : L(@"off")})),
|
|
183
209
|
LF(L(@"Pause edits on reserved files: {editGuard}"), (@{@"editGuard": [options[@"editGuard"] boolValue] ? L(@"on") : L(@"off")}))
|
|
184
210
|
];
|
|
@@ -262,6 +288,12 @@ static NSString *WaitForLock(NSString *name) {
|
|
|
262
288
|
return nil;
|
|
263
289
|
}
|
|
264
290
|
|
|
291
|
+
id CCBridgeWithLock(NSString *name, id (^body)(void)) {
|
|
292
|
+
NSString *lock = WaitForLock(name);
|
|
293
|
+
@try { return body(); }
|
|
294
|
+
@finally { CCBridgeReleaseLock(lock); }
|
|
295
|
+
}
|
|
296
|
+
|
|
265
297
|
#pragma mark - 会话注册表
|
|
266
298
|
|
|
267
299
|
NSString *CCBridgeSessionRef(NSString *sessionId) {
|
|
@@ -289,6 +321,17 @@ NSArray<NSDictionary *> *CCBridgeListSessions(void) {
|
|
|
289
321
|
return sessions;
|
|
290
322
|
}
|
|
291
323
|
|
|
324
|
+
static BOOL UsesBridgeHostPID(id provider) {
|
|
325
|
+
return [provider isEqual:CCBridgeAntigravityProvider] || [provider isEqual:CCBridgeAntigravityAppProvider];
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
NSNumber *CCBridgeSessionHostPID(NSDictionary *session) {
|
|
329
|
+
id hostPID = session[@"hostPid"];
|
|
330
|
+
if (UsesBridgeHostPID(session[@"provider"]) && [hostPID isKindOfClass:NSNumber.class]) return hostPID;
|
|
331
|
+
id pid = session[@"pid"];
|
|
332
|
+
return [pid isKindOfClass:NSNumber.class] ? pid : nil;
|
|
333
|
+
}
|
|
334
|
+
|
|
292
335
|
void CCBridgeRemoveSession(id sessionId) {
|
|
293
336
|
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
294
337
|
if (!safe) return;
|
|
@@ -346,6 +389,51 @@ static NSArray<NSString *> *NameAnimals(void) {
|
|
|
346
389
|
return animals;
|
|
347
390
|
}
|
|
348
391
|
|
|
392
|
+
NSString *CCBridgeSessionAnimalAlias(NSDictionary *session) {
|
|
393
|
+
if (![CCPetsCurrentLanguage() hasPrefix:@"zh"] ||
|
|
394
|
+
![StringValue(session[@"nameSource"]) isEqualToString:@"generated"]) return @"";
|
|
395
|
+
static NSDictionary<NSString *, NSString *> *aliases;
|
|
396
|
+
static dispatch_once_t once;
|
|
397
|
+
dispatch_once(&once, ^{
|
|
398
|
+
aliases = @{
|
|
399
|
+
@"otter": @"水獭", @"panda": @"熊猫", @"quokka": @"短尾矮袋鼠", @"corgi": @"柯基", @"koala": @"考拉",
|
|
400
|
+
@"capy": @"水豚", @"puffin": @"海鹦", @"lemur": @"狐猴", @"alpaca": @"羊驼", @"wombat": @"袋熊",
|
|
401
|
+
@"bunny": @"小兔", @"kitten": @"小猫", @"puppy": @"小狗", @"hamster": @"仓鼠", @"fox": @"狐狸",
|
|
402
|
+
@"owl": @"猫头鹰", @"seal": @"海豹", @"penguin": @"企鹅", @"duck": @"鸭子", @"llama": @"美洲驼",
|
|
403
|
+
@"sloth": @"树懒", @"fennec": @"耳廓狐", @"bear": @"熊", @"cub": @"幼崽", @"lamb": @"小羊",
|
|
404
|
+
@"fawn": @"小鹿", @"deer": @"鹿", @"dolphin": @"海豚", @"beluga": @"白鲸", @"orca": @"虎鲸",
|
|
405
|
+
@"narwhal": @"独角鲸", @"walrus": @"海象", @"manatee": @"海牛", @"turtle": @"乌龟", @"frog": @"青蛙",
|
|
406
|
+
@"gecko": @"壁虎", @"chick": @"小鸡", @"robin": @"知更鸟", @"finch": @"雀鸟", @"wren": @"鹪鹩",
|
|
407
|
+
@"sparrow": @"麻雀", @"toucan": @"巨嘴鸟", @"parrot": @"鹦鹉", @"macaw": @"金刚鹦鹉", @"dove": @"鸽子",
|
|
408
|
+
@"swan": @"天鹅", @"owlet": @"小猫头鹰", @"goose": @"鹅", @"koi": @"锦鲤", @"bee": @"蜜蜂",
|
|
409
|
+
@"ladybug": @"瓢虫", @"raccoon": @"浣熊", @"ferret": @"雪貂", @"badger": @"獾", @"beaver": @"河狸",
|
|
410
|
+
@"marmot": @"旱獭", @"meerkat": @"狐獴", @"mouse": @"小鼠", @"gerbil": @"沙鼠", @"pony": @"矮马",
|
|
411
|
+
@"foal": @"小马驹", @"piglet": @"小猪", @"calf": @"小牛", @"giraffe": @"长颈鹿", @"zebra": @"斑马",
|
|
412
|
+
@"hippo": @"河马", @"rhino": @"犀牛", @"tapir": @"貘", @"okapi": @"霍加狓", @"lynx": @"猞猁",
|
|
413
|
+
@"tiger": @"老虎", @"cheetah": @"猎豹", @"lion": @"狮子", @"husky": @"哈士奇", @"shiba": @"柴犬",
|
|
414
|
+
@"beagle": @"比格犬", @"pug": @"巴哥犬", @"poodle": @"贵宾犬", @"collie": @"柯利犬", @"dingo": @"澳洲野犬",
|
|
415
|
+
@"newt": @"蝾螈", @"tadpole": @"蝌蚪", @"octopus": @"章鱼", @"squid": @"鱿鱼", @"crab": @"螃蟹",
|
|
416
|
+
@"snail": @"蜗牛", @"peacock": @"孔雀", @"emu": @"鸸鹋", @"quail": @"鹌鹑", @"magpie": @"喜鹊",
|
|
417
|
+
@"kea": @"啄羊鹦鹉", @"heron": @"鹭鸟", @"stork": @"鹳", @"ibis": @"朱鹭", @"numbat": @"袋食蚁兽",
|
|
418
|
+
@"bilby": @"兔耳袋狸", @"possum": @"负鼠", @"dugong": @"儒艮", @"yak": @"牦牛", @"bison": @"野牛",
|
|
419
|
+
};
|
|
420
|
+
});
|
|
421
|
+
NSArray<NSString *> *parts = [StringValue(session[@"name"]) componentsSeparatedByString:@"."];
|
|
422
|
+
if (parts.count != 3) return @"";
|
|
423
|
+
NSString *animal = parts.lastObject;
|
|
424
|
+
NSCharacterSet *digits = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
|
|
425
|
+
NSUInteger end = animal.length;
|
|
426
|
+
while (end > 0 && [digits characterIsMember:[animal characterAtIndex:end - 1]]) end--;
|
|
427
|
+
NSString *alias = aliases[[animal substringToIndex:end]];
|
|
428
|
+
return alias ? [alias stringByAppendingString:[animal substringFromIndex:end]] : @"";
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
NSString *CCBridgeSessionDisplayName(NSDictionary *session) {
|
|
432
|
+
NSString *name = StringValue(session[@"name"]) ?: @"";
|
|
433
|
+
NSString *alias = CCBridgeSessionAnimalAlias(session);
|
|
434
|
+
return alias.length > 0 ? [name stringByAppendingFormat:@" · %@", alias] : name;
|
|
435
|
+
}
|
|
436
|
+
|
|
349
437
|
static NSDictionary *NameTakenBy(NSString *name, NSString *sessionId, NSArray<NSDictionary *> *liveSessions) {
|
|
350
438
|
for (NSDictionary *entry in liveSessions) {
|
|
351
439
|
if (![entry[@"session"] isEqual:sessionId] && [entry[@"name"] isEqual:name]) return entry;
|
|
@@ -381,6 +469,18 @@ static NSString *AllocateName(NSString *provider, NSString *profileName, NSStrin
|
|
|
381
469
|
}
|
|
382
470
|
}
|
|
383
471
|
|
|
472
|
+
// 状态卡这类窄地方用的短名:去掉默认名字开头的账号段(claude.my-app.otter → my-app.otter),
|
|
473
|
+
// 只处理明确标记为自动生成的名字;自定义名字和来源不明的旧记录原样返回。
|
|
474
|
+
NSString *CCBridgeSessionShortName(NSDictionary *session) {
|
|
475
|
+
NSString *name = StringValue(session[@"name"]);
|
|
476
|
+
if (name.length == 0) return @"";
|
|
477
|
+
if (![StringValue(session[@"nameSource"]) isEqualToString:@"generated"]) return name;
|
|
478
|
+
// 自动名字的首段就是分配时的账号名,不依赖之后可能变化的 profileName。
|
|
479
|
+
NSRange separator = [name rangeOfString:@"."];
|
|
480
|
+
return separator.location != NSNotFound && separator.location > 0 && NSMaxRange(separator) < name.length
|
|
481
|
+
? [name substringFromIndex:NSMaxRange(separator)] : name;
|
|
482
|
+
}
|
|
483
|
+
|
|
384
484
|
// 启动时指定的名字被占用就追加序号(frontend → frontend-2),不让会话因此注册失败。
|
|
385
485
|
static NSString *AllocateCustomName(NSString *name, NSString *sessionId, NSArray<NSDictionary *> *liveSessions) {
|
|
386
486
|
if (!NameTakenBy(name, sessionId, liveSessions)) return name;
|
|
@@ -413,7 +513,17 @@ static NSDictionary *UpsertSession(NSString *sessionId, NSString *provider, NSNu
|
|
|
413
513
|
if (provider) record[@"provider"] = provider;
|
|
414
514
|
}
|
|
415
515
|
if (provider) record[@"provider"] = provider;
|
|
416
|
-
if (pid
|
|
516
|
+
if (pid.intValue > 1) {
|
|
517
|
+
if (UsesBridgeHostPID(record[@"provider"])) {
|
|
518
|
+
// Older Bridge clients treat an unknown provider with a pid as offline and may
|
|
519
|
+
// remove the record. They can still address it via the legacy activity timestamp.
|
|
520
|
+
record[@"hostPid"] = pid;
|
|
521
|
+
[record removeObjectForKey:@"pid"];
|
|
522
|
+
} else {
|
|
523
|
+
record[@"pid"] = pid;
|
|
524
|
+
[record removeObjectForKey:@"hostPid"];
|
|
525
|
+
}
|
|
526
|
+
}
|
|
417
527
|
if (cwd.length > 0) record[@"cwd"] = cwd;
|
|
418
528
|
if (tty.length > 0) record[@"tty"] = tty;
|
|
419
529
|
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
@@ -426,6 +536,7 @@ static NSDictionary *UpsertSession(NSString *sessionId, NSString *provider, NSNu
|
|
|
426
536
|
record[@"name"] = custom ? AllocateCustomName(custom, safe, liveSessions ?: @[])
|
|
427
537
|
: AllocateName(StringValue(record[@"provider"]) ?: @"", StringValue(record[@"profileName"]),
|
|
428
538
|
StringValue(record[@"cwd"]), safe, liveSessions ?: @[]);
|
|
539
|
+
record[@"nameSource"] = custom ? @"custom" : @"generated";
|
|
429
540
|
}
|
|
430
541
|
if (status) record[@"status"] = status;
|
|
431
542
|
record[@"updatedAt"] = @(now);
|
|
@@ -447,6 +558,7 @@ static NSDictionary *RenameSession(NSString *sessionId, NSString *requestedName,
|
|
|
447
558
|
}
|
|
448
559
|
NSString *previous = StringValue(record[@"name"]) ?: @"";
|
|
449
560
|
record[@"name"] = name;
|
|
561
|
+
record[@"nameSource"] = @"custom";
|
|
450
562
|
record[@"updatedAt"] = @(CCPetsMilliseconds());
|
|
451
563
|
CCBridgeWriteJSON(SessionFile(record[@"session"]), record);
|
|
452
564
|
return @{@"session": record, @"previous": previous};
|
|
@@ -501,8 +613,11 @@ NSDictionary *CCBridgeReadReceipt(id messageId) {
|
|
|
501
613
|
return [receipt isKindOfClass:NSDictionary.class] ? receipt : nil;
|
|
502
614
|
}
|
|
503
615
|
|
|
616
|
+
// 回执只是元数据:正文只在信箱里,送达后随之删除,不跟着回执再留 48 小时。
|
|
504
617
|
void CCBridgeWriteReceipt(NSDictionary *message) {
|
|
505
|
-
|
|
618
|
+
NSMutableDictionary *receipt = [message mutableCopy];
|
|
619
|
+
[receipt removeObjectForKey:@"body"];
|
|
620
|
+
CCBridgeWriteJSON(ReceiptFile(message[@"id"]), receipt);
|
|
506
621
|
}
|
|
507
622
|
|
|
508
623
|
NSDictionary *CCBridgeUpdateReceipt(NSString *messageId, NSDictionary *patch) {
|
|
@@ -512,7 +627,7 @@ NSDictionary *CCBridgeUpdateReceipt(NSString *messageId, NSDictionary *patch) {
|
|
|
512
627
|
NSMutableDictionary *next = [CCBridgeReadReceipt(messageId) mutableCopy];
|
|
513
628
|
if (!next) return nil;
|
|
514
629
|
// 终态不能被迟到的 pending、失败回调等覆盖。
|
|
515
|
-
if (
|
|
630
|
+
if (IsTerminalStatus(next[@"status"])) return next;
|
|
516
631
|
[next addEntriesFromDictionary:patch];
|
|
517
632
|
next[@"updatedAt"] = @(CCPetsMilliseconds());
|
|
518
633
|
CCBridgeWriteReceipt(next);
|
|
@@ -555,11 +670,18 @@ NSString *CCBridgeCheckSendLimits(NSString *fromSession, NSString *toSession) {
|
|
|
555
670
|
return nil;
|
|
556
671
|
}
|
|
557
672
|
|
|
558
|
-
|
|
673
|
+
// 和清理共用信箱锁,并在锁内复查:发送可能卡在 codex queue 上几十秒,这期间 Bridge 可能已关闭、
|
|
674
|
+
// 目标可能已退出。锁外检查的话,清理做完之后这里才写入,正文就再也没人收拾了。
|
|
675
|
+
NSString *CCBridgeEnqueueInbox(NSDictionary *message) {
|
|
559
676
|
NSString *target = CCBridgeSafeSessionId(PartySession(message, @"to"));
|
|
560
677
|
if (!target) [NSException raise:@"CCBridgeInbox" format:@"%@", L(@"invalid target session id")];
|
|
561
|
-
|
|
562
|
-
|
|
678
|
+
return CCBridgeWithLock([@"inbox-" stringByAppendingString:target], ^id {
|
|
679
|
+
if (!CCBridgeEnabled()) return L(@"CC Bridge is off.");
|
|
680
|
+
if (!CCBridgeSessionLive(CCBridgeReadSession(target))) return L(@"the target session is offline");
|
|
681
|
+
CCBridgeWriteJSON([CCBridgeSubdirectory(@[@"inbox", target]) stringByAppendingPathComponent:
|
|
682
|
+
[message[@"id"] stringByAppendingString:@".json"]], message);
|
|
683
|
+
return nil;
|
|
684
|
+
});
|
|
563
685
|
}
|
|
564
686
|
|
|
565
687
|
static NSString *InboxLockFile(NSString *sessionId) {
|
|
@@ -588,8 +710,7 @@ NSArray<NSDictionary *> *CCBridgeClaimInbox(id sessionId) {
|
|
|
588
710
|
NSString *file = [claimed stringByAppendingPathComponent:name];
|
|
589
711
|
id message = CCBridgeReadJSON(file);
|
|
590
712
|
if (![message isKindOfClass:NSDictionary.class] || !IsMessageId(message[@"id"])) continue;
|
|
591
|
-
|
|
592
|
-
if ([@[@"delivered", @"expired", @"undeliverable"] containsObject:status ?: @""]) {
|
|
713
|
+
if (IsTerminalStatus(CCBridgeReadReceipt(message[@"id"])[@"status"])) {
|
|
593
714
|
unlink(file.fileSystemRepresentation);
|
|
594
715
|
continue;
|
|
595
716
|
}
|
|
@@ -630,26 +751,119 @@ NSUInteger CCBridgePendingCount(id sessionId) {
|
|
|
630
751
|
ListJSON(CCBridgeSubdirectory(@[@"claimed", safe])).count;
|
|
631
752
|
}
|
|
632
753
|
|
|
754
|
+
static NSString *SessionDirectory(NSString *kind, NSString *sessionId) {
|
|
755
|
+
return [[CCBridgeStateDirectory() stringByAppendingPathComponent:kind] stringByAppendingPathComponent:sessionId];
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
NSString *CCBridgeOldestPendingMessageName(id sessionId) {
|
|
759
|
+
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
760
|
+
if (!safe) return nil;
|
|
761
|
+
NSString *oldest = nil;
|
|
762
|
+
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
763
|
+
for (NSString *name in ListJSON(SessionDirectory(kind, safe))) {
|
|
764
|
+
if (!IsMessageId(name.stringByDeletingPathExtension)) continue;
|
|
765
|
+
if (!oldest || [name compare:oldest] == NSOrderedAscending) oldest = name;
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return oldest;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// 调用方持有该信箱的锁。空目录一并删掉,否则已退出的会话会在状态目录里留下痕迹。
|
|
773
|
+
static void AbandonInboxLocked(NSString *sessionId, NSString *reason) {
|
|
774
|
+
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
775
|
+
NSString *directory = SessionDirectory(kind, sessionId);
|
|
776
|
+
for (NSString *name in ListJSON(directory)) {
|
|
777
|
+
NSString *file = [directory stringByAppendingPathComponent:name];
|
|
778
|
+
id message = CCBridgeReadJSON(file);
|
|
779
|
+
if ([message isKindOfClass:NSDictionary.class]) {
|
|
780
|
+
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"undeliverable", @"reason": reason});
|
|
781
|
+
}
|
|
782
|
+
unlink(file.fileSystemRepresentation);
|
|
783
|
+
}
|
|
784
|
+
rmdir(directory.fileSystemRepresentation);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
633
788
|
void CCBridgeAbandonInbox(id sessionId) {
|
|
634
789
|
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
635
790
|
if (!safe) return;
|
|
636
791
|
NSString *lock = WaitForLock([@"inbox-" stringByAppendingString:safe]);
|
|
792
|
+
@try { AbandonInboxLocked(safe, L(@"the target session has ended")); }
|
|
793
|
+
@finally { CCBridgeReleaseLock(lock); }
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
void CCBridgeAbandonAllInboxes(NSString *reason) {
|
|
797
|
+
NSMutableSet<NSString *> *sessions = [NSMutableSet set];
|
|
798
|
+
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
799
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:kind];
|
|
800
|
+
for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
801
|
+
if (CCBridgeSafeSessionId(name)) [sessions addObject:name];
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
// 已注册的会话也要过一遍锁:信箱目录可能正在被创建,只看现有目录会漏掉锁内还没写完的发送。
|
|
805
|
+
for (NSDictionary *session in CCBridgeListSessions()) {
|
|
806
|
+
if (CCBridgeSafeSessionId(session[@"session"])) [sessions addObject:session[@"session"]];
|
|
807
|
+
}
|
|
808
|
+
for (NSString *session in sessions) {
|
|
809
|
+
NSString *lock = WaitForLock([@"inbox-" stringByAppendingString:session]);
|
|
810
|
+
@try { AbandonInboxLocked(session, reason); }
|
|
811
|
+
@finally { CCBridgeReleaseLock(lock); }
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// 崩溃的会话不会触发 SessionEnd。这里只试一次锁:抢不到说明有人正在处理,留给下一次清理,
|
|
816
|
+
// 不能让查询在线列表的调用方等锁。锁内复查在线,防止同一 ID 刚被 resume。
|
|
817
|
+
void CCBridgeAbandonOfflineSession(id sessionId) {
|
|
818
|
+
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
819
|
+
if (!safe) return;
|
|
820
|
+
NSString *lock = CCBridgeAcquireLock([@"inbox-" stringByAppendingString:safe]);
|
|
821
|
+
if (!lock) return;
|
|
637
822
|
@try {
|
|
823
|
+
if (CCBridgeSessionLive(CCBridgeReadSession(safe))) return;
|
|
824
|
+
AbandonInboxLocked(safe, L(@"the target session has ended"));
|
|
825
|
+
CCBridgeClearIdleSubscriptions(safe);
|
|
826
|
+
CCBridgeDropWatcher(safe);
|
|
827
|
+
} @finally { CCBridgeReleaseLock(lock); }
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
static void ExpireInbox(NSString *sessionId) {
|
|
831
|
+
NSString *lock = CCBridgeAcquireLock([@"inbox-" stringByAppendingString:sessionId]);
|
|
832
|
+
if (!lock) return;
|
|
833
|
+
@try {
|
|
834
|
+
long long now = CCPetsMilliseconds();
|
|
638
835
|
for (NSString *kind in @[@"inbox", @"claimed"]) {
|
|
639
|
-
NSString *directory =
|
|
836
|
+
NSString *directory = SessionDirectory(kind, sessionId);
|
|
640
837
|
for (NSString *name in ListJSON(directory)) {
|
|
641
838
|
NSString *file = [directory stringByAppendingPathComponent:name];
|
|
642
839
|
id message = CCBridgeReadJSON(file);
|
|
643
|
-
if ([message isKindOfClass:NSDictionary.class]) {
|
|
644
|
-
|
|
840
|
+
if (![message isKindOfClass:NSDictionary.class]) {
|
|
841
|
+
unlink(file.fileSystemRepresentation);
|
|
842
|
+
} else if (now - NumberValue(message[@"createdAt"]) > CCBridgeMessageTTL) {
|
|
843
|
+
CCBridgeUpdateReceipt(message[@"id"], @{@"status": @"expired"});
|
|
844
|
+
unlink(file.fileSystemRepresentation);
|
|
645
845
|
}
|
|
646
|
-
unlink(file.fileSystemRepresentation);
|
|
647
846
|
}
|
|
648
847
|
}
|
|
649
848
|
} @finally { CCBridgeReleaseLock(lock); }
|
|
650
849
|
}
|
|
651
850
|
|
|
652
|
-
|
|
851
|
+
void CCBridgeSweepMessages(void) {
|
|
852
|
+
NSMutableSet<NSString *> *sessions = [NSMutableSet set];
|
|
853
|
+
for (NSString *kind in @[@"inbox", @"claimed", @"subscriptions"]) {
|
|
854
|
+
NSString *directory = [CCBridgeStateDirectory() stringByAppendingPathComponent:kind];
|
|
855
|
+
for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
856
|
+
if (CCBridgeSafeSessionId(name)) [sessions addObject:name];
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
for (NSString *session in sessions) {
|
|
860
|
+
if (CCBridgeSessionLive(CCBridgeReadSession(session))) ExpireInbox(session);
|
|
861
|
+
else CCBridgeAbandonOfflineSession(session);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// 回执保留 TTL 两倍时长,够排查问题,又不会无限增长。超过 TTL 仍未到终态的(目标崩溃、投递中途退出)
|
|
866
|
+
// 不会再有人更新,在这里收尾,发送方才能看到结果。
|
|
653
867
|
void CCBridgePruneReceipts(void) {
|
|
654
868
|
NSString *directory = CCBridgeSubdirectory(@[@"sent"]);
|
|
655
869
|
long long now = CCPetsMilliseconds();
|
|
@@ -658,6 +872,8 @@ void CCBridgePruneReceipts(void) {
|
|
|
658
872
|
id receipt = CCBridgeReadJSON(file);
|
|
659
873
|
if (![receipt isKindOfClass:NSDictionary.class] || now - NumberValue(receipt[@"createdAt"]) > CCBridgeMessageTTL * 2) {
|
|
660
874
|
unlink(file.fileSystemRepresentation);
|
|
875
|
+
} else if (now - NumberValue(receipt[@"createdAt"]) > CCBridgeMessageTTL && !IsTerminalStatus(receipt[@"status"])) {
|
|
876
|
+
CCBridgeUpdateReceipt(receipt[@"id"], @{@"status": @"expired"});
|
|
661
877
|
}
|
|
662
878
|
}
|
|
663
879
|
}
|
|
@@ -696,6 +912,16 @@ NSArray<NSString *> *CCBridgeTakeIdleSubscriptions(NSString *targetSession) {
|
|
|
696
912
|
return subscribers;
|
|
697
913
|
}
|
|
698
914
|
|
|
915
|
+
void CCBridgeClearIdleSubscriptions(id targetSession) {
|
|
916
|
+
NSString *target = CCBridgeSafeSessionId(targetSession);
|
|
917
|
+
if (!target) return;
|
|
918
|
+
NSString *directory = SessionDirectory(@"subscriptions", target);
|
|
919
|
+
for (NSString *name in ListJSON(directory)) {
|
|
920
|
+
unlink([directory stringByAppendingPathComponent:name].fileSystemRepresentation);
|
|
921
|
+
}
|
|
922
|
+
rmdir(directory.fileSystemRepresentation);
|
|
923
|
+
}
|
|
924
|
+
|
|
699
925
|
#pragma mark - 锁
|
|
700
926
|
|
|
701
927
|
// 内核锁文件保持稳定 inode,PID 文件仅用于诊断;不能通过删除“陈旧锁”抢占。
|
|
@@ -737,6 +963,29 @@ void CCBridgeReleaseLock(NSString *lockFile) {
|
|
|
737
963
|
}
|
|
738
964
|
}
|
|
739
965
|
|
|
966
|
+
#pragma mark - watcher 接替
|
|
967
|
+
|
|
968
|
+
static NSString *WatcherOwnerFile(NSString *sessionId) {
|
|
969
|
+
return [CCBridgeSubdirectory(@[@"locks"]) stringByAppendingPathComponent:
|
|
970
|
+
[NSString stringWithFormat:@"watch-%@.owner", sessionId]];
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
NSString *CCBridgeClaimWatcher(NSString *sessionId) {
|
|
974
|
+
NSString *token = RandomHex(8);
|
|
975
|
+
CCBridgeWriteJSON(WatcherOwnerFile(sessionId), @{@"token": token});
|
|
976
|
+
return token;
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
BOOL CCBridgeWatcherOwned(NSString *sessionId, NSString *token) {
|
|
980
|
+
id owner = CCBridgeReadJSON(WatcherOwnerFile(sessionId));
|
|
981
|
+
return [owner isKindOfClass:NSDictionary.class] && [owner[@"token"] isEqual:token];
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
void CCBridgeDropWatcher(id sessionId) {
|
|
985
|
+
NSString *safe = CCBridgeSafeSessionId(sessionId);
|
|
986
|
+
if (safe) unlink(WatcherOwnerFile(safe).fileSystemRepresentation);
|
|
987
|
+
}
|
|
988
|
+
|
|
740
989
|
#pragma mark - 进程探测
|
|
741
990
|
|
|
742
991
|
BOOL CCBridgeProcessAlive(pid_t pid) {
|
|
@@ -754,8 +1003,9 @@ static BOOL ProcessParent(pid_t pid, pid_t *parent, dev_t *tty) {
|
|
|
754
1003
|
return YES;
|
|
755
1004
|
}
|
|
756
1005
|
|
|
757
|
-
//
|
|
758
|
-
|
|
1006
|
+
// 保留 argv 的边界:路径可以含空格(如 Application Support),拼成字符串再切分就认不出可执行文件了。
|
|
1007
|
+
// 读不到参数区(别的用户的进程、僵尸)时退回 ps,那里没有边界,只能按空白切分。
|
|
1008
|
+
NSArray<NSString *> *CCBridgeProcessArguments(pid_t pid) {
|
|
759
1009
|
if (pid <= 0) return nil;
|
|
760
1010
|
int argumentMaximum = 0;
|
|
761
1011
|
size_t size = sizeof argumentMaximum;
|
|
@@ -779,7 +1029,7 @@ NSString *CCBridgeProcessCommand(pid_t pid) {
|
|
|
779
1029
|
cursor += length + 1;
|
|
780
1030
|
}
|
|
781
1031
|
free(buffer);
|
|
782
|
-
if (arguments.count > 0) return
|
|
1032
|
+
if (arguments.count > 0) return arguments;
|
|
783
1033
|
} else {
|
|
784
1034
|
free(buffer);
|
|
785
1035
|
}
|
|
@@ -788,8 +1038,11 @@ NSString *CCBridgeProcessCommand(pid_t pid) {
|
|
|
788
1038
|
if (CCPetsRun(@"/bin/ps", @[@"-o", @"command=", @"-p", [NSString stringWithFormat:@"%d", pid]], nil, nil, 10, &output, NULL) != 0) {
|
|
789
1039
|
return nil;
|
|
790
1040
|
}
|
|
791
|
-
NSString *
|
|
792
|
-
|
|
1041
|
+
NSMutableArray<NSString *> *tokens = [NSMutableArray array];
|
|
1042
|
+
for (NSString *token in [output ?: @"" componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]) {
|
|
1043
|
+
if (token.length > 0) [tokens addObject:token];
|
|
1044
|
+
}
|
|
1045
|
+
return tokens.count > 0 ? tokens : nil;
|
|
793
1046
|
}
|
|
794
1047
|
|
|
795
1048
|
NSString *CCBridgeProcessTty(pid_t pid) {
|
|
@@ -801,26 +1054,31 @@ NSString *CCBridgeProcessTty(pid_t pid) {
|
|
|
801
1054
|
return [tty hasPrefix:@"tty"] ? tty : [@"tty" stringByAppendingString:tty];
|
|
802
1055
|
}
|
|
803
1056
|
|
|
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
1057
|
static BOOL Matches(NSString *pattern, NSString *text) {
|
|
815
1058
|
if (text.length == 0) return NO;
|
|
816
1059
|
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
|
|
817
1060
|
return [expression firstMatchInString:text options:0 range:NSMakeRange(0, text.length)] != nil;
|
|
818
1061
|
}
|
|
819
1062
|
|
|
820
|
-
BOOL
|
|
821
|
-
|
|
822
|
-
NSString *
|
|
823
|
-
|
|
1063
|
+
static BOOL NativeAntigravityArguments(NSArray<NSString *> *arguments) {
|
|
1064
|
+
NSString *name = arguments.firstObject.lastPathComponent;
|
|
1065
|
+
NSString *configured = [CCPetsEnvironmentValue(@"AGY_REAL_BIN") lastPathComponent];
|
|
1066
|
+
return [name isEqualToString:@"agy"] || (configured.length > 0 && [name isEqualToString:configured]);
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
static BOOL AntigravityAppExecutable(NSString *path) {
|
|
1070
|
+
return [path hasPrefix:@"/"] && [path hasSuffix:@"/Antigravity.app/Contents/Resources/bin/language_server"];
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
static BOOL AntigravityAppHost(pid_t pid) {
|
|
1074
|
+
char path[PROC_PIDPATHINFO_MAXSIZE];
|
|
1075
|
+
// Hooks and MCP share the App backend. Use the kernel's executable path, not its argv.
|
|
1076
|
+
return proc_pidpath(pid, path, sizeof path) > 0 && AntigravityAppExecutable(@(path));
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
BOOL CCBridgeArgumentsMatchProvider(NSString *provider, NSArray<NSString *> *arguments) {
|
|
1080
|
+
NSString *executable = arguments.firstObject ?: @"";
|
|
1081
|
+
NSString *second = arguments.count > 1 ? arguments[1] : @"";
|
|
824
1082
|
// Codex 的 npm 包是 node 包装器 + 原生二进制;hook 和 MCP 进程的直接祖先都是原生二进制,
|
|
825
1083
|
// 取离自己最近的那个,两边才能落到同一个 pid。
|
|
826
1084
|
if ([provider isEqualToString:@"Codex"]) {
|
|
@@ -830,6 +1088,10 @@ BOOL CCBridgeCommandMatchesProvider(NSString *provider, NSString *command) {
|
|
|
830
1088
|
return [executable.lastPathComponent isEqualToString:@"claude"] ||
|
|
831
1089
|
Matches(@"@anthropic-ai/claude-code/", executable) || Matches(@"@anthropic-ai/claude-code/", second);
|
|
832
1090
|
}
|
|
1091
|
+
if ([provider isEqualToString:CCBridgeAntigravityProvider]) {
|
|
1092
|
+
return NativeAntigravityArguments(arguments);
|
|
1093
|
+
}
|
|
1094
|
+
if ([provider isEqualToString:CCBridgeAntigravityAppProvider]) return AntigravityAppExecutable(executable);
|
|
833
1095
|
return NO;
|
|
834
1096
|
}
|
|
835
1097
|
|
|
@@ -850,9 +1112,10 @@ NSDictionary *CCBridgeFindAgentAncestor(pid_t startPid) {
|
|
|
850
1112
|
for (int depth = 0; depth < 12 && pid > 1; depth++) {
|
|
851
1113
|
pid_t parent = 0;
|
|
852
1114
|
if (!ProcessParent(pid, &parent, NULL)) return nil;
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
1115
|
+
if (AntigravityAppHost(pid)) return @{@"provider": CCBridgeAntigravityAppProvider, @"pid": @(pid)};
|
|
1116
|
+
NSArray<NSString *> *arguments = CCBridgeProcessArguments(pid);
|
|
1117
|
+
for (NSString *provider in @[@"Claude", @"Codex", CCBridgeAntigravityProvider]) {
|
|
1118
|
+
if (CCBridgeArgumentsMatchProvider(provider, arguments)) return @{@"provider": provider, @"pid": @(pid)};
|
|
856
1119
|
}
|
|
857
1120
|
pid = parent;
|
|
858
1121
|
}
|
|
@@ -861,13 +1124,18 @@ NSDictionary *CCBridgeFindAgentAncestor(pid_t startPid) {
|
|
|
861
1124
|
|
|
862
1125
|
BOOL CCBridgeSessionLive(NSDictionary *session) {
|
|
863
1126
|
if (![session isKindOfClass:NSDictionary.class] || [session[@"status"] isEqual:@"gone"]) return NO;
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
1127
|
+
NSString *provider = session[@"provider"];
|
|
1128
|
+
NSNumber *pid = CCBridgeSessionHostPID(session);
|
|
1129
|
+
if ([provider isEqual:CCBridgeAntigravityAppProvider]) {
|
|
1130
|
+
return pid.intValue > 1 && [CCBridgeOptions()[@"antigravityApp"] isEqual:@YES] && AntigravityAppHost(pid.intValue);
|
|
1131
|
+
}
|
|
1132
|
+
if (pid) {
|
|
1133
|
+
if (!CCBridgeProcessAlive(pid.intValue)) return NO;
|
|
1134
|
+
NSArray<NSString *> *arguments = CCBridgeProcessArguments(pid.intValue);
|
|
1135
|
+
return arguments == nil || CCBridgeArgumentsMatchProvider(provider, arguments);
|
|
869
1136
|
}
|
|
870
|
-
return
|
|
1137
|
+
return !UsesBridgeHostPID(provider) &&
|
|
1138
|
+
CCPetsMilliseconds() - NumberValue(session[@"updatedAt"]) < StaleWithoutPid;
|
|
871
1139
|
}
|
|
872
1140
|
|
|
873
1141
|
// `codex queue` 是较新的 Codex 才有的子命令。旧版本把不认识的子命令当成交互式启动参数,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <sys/types.h>
|
|
3
|
+
|
|
4
|
+
BOOL CCBridgeTerminalTitleEnabled(void);
|
|
5
|
+
// The launch token binds a local terminal to the exact conversation registered by its hook.
|
|
6
|
+
void CCBridgeBindTerminalTitle(NSString *launchToken, NSString *provider, NSString *sessionID);
|
|
7
|
+
void CCBridgeBindTerminalTitleIfUnbound(NSString *launchToken, NSString *provider, NSString *sessionID);
|
|
8
|
+
void CCBridgeClearTerminalTitleBinding(NSString *launchToken);
|
|
9
|
+
NSString *CCBridgeTerminalTitleName(NSString *provider, NSString *launchToken, pid_t childPID);
|
|
10
|
+
NSString *CCBridgeTerminalTitleNameForSession(NSString *sessionID, NSString *provider);
|
|
11
|
+
NSString *CCBridgeComposeTerminalTitle(NSString *base, NSString *nickname);
|
|
12
|
+
int CCBridgePrintAntigravityTitle(NSString *originalTitle);
|