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
|
@@ -13,8 +13,6 @@ extern NSString *const CCBridgeHookMarker;
|
|
|
13
13
|
extern NSString *const CCBridgeMCPName;
|
|
14
14
|
|
|
15
15
|
// 返回给用户看的报告行。registerMcp=NO 用于只改选项(桌宠开关、cc-pets bridge configure)。
|
|
16
|
-
NSArray<NSString *> *CCBridgeInstall(NSDictionary *options, BOOL registerMcp);
|
|
17
|
-
NSArray<NSString *> *CCBridgeUninstall(BOOL unregisterMcp);
|
|
18
16
|
NSArray<NSString *> *CCBridgeInstallForProfile(NSDictionary *profile, NSDictionary *options, BOOL registerMcp);
|
|
19
17
|
NSArray<NSString *> *CCBridgeUninstallForProfile(NSDictionary *profile, BOOL unregisterMcp);
|
|
20
18
|
// CC_BRIDGE_SKIP_MCP=1 时不碰 claude / codex 的 MCP 注册(测试用)。
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
#import "CCPetsBridgeSetup.h"
|
|
3
3
|
#import "CCPetsBridge.h"
|
|
4
4
|
#import "CCPetsBridgeStore.h"
|
|
5
|
+
#import "CCPetsAntigravityConfig.h"
|
|
5
6
|
#import "CCPetsCLISupport.h"
|
|
6
7
|
#import "CCPetsJSON.h"
|
|
8
|
+
#import "CCPetsProfiles.h"
|
|
7
9
|
#import <sys/stat.h>
|
|
8
10
|
|
|
9
11
|
#define LF(localized, values) CCPetsInterpolate(localized, values)
|
|
@@ -202,15 +204,73 @@ static NSString *CodexApprovalBlock(NSArray<NSString *> *tools) {
|
|
|
202
204
|
return [lines componentsJoinedByString:@"\n"];
|
|
203
205
|
}
|
|
204
206
|
|
|
205
|
-
|
|
207
|
+
typedef NS_ENUM(NSInteger, TomlLexState) {
|
|
208
|
+
TomlLexNormal,
|
|
209
|
+
TomlLexMultilineBasic,
|
|
210
|
+
TomlLexMultilineLiteral
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// 扫过一行,返回行尾的词法状态。只关心多行字符串:字符串里的内容长得再像管理块也是用户的数据。
|
|
214
|
+
static TomlLexState AdvanceTomlState(NSString *line, TomlLexState state) {
|
|
215
|
+
NSUInteger length = line.length;
|
|
216
|
+
BOOL (^startsWith)(NSUInteger, NSString *) = ^BOOL(NSUInteger index, NSString *text) {
|
|
217
|
+
return index + text.length <= length &&
|
|
218
|
+
[[line substringWithRange:NSMakeRange(index, text.length)] isEqualToString:text];
|
|
219
|
+
};
|
|
220
|
+
for (NSUInteger index = 0; index < length; index++) {
|
|
221
|
+
unichar character = [line characterAtIndex:index];
|
|
222
|
+
if (state == TomlLexMultilineBasic) {
|
|
223
|
+
if (character == '\\') index++;
|
|
224
|
+
else if (startsWith(index, @"\"\"\"")) { state = TomlLexNormal; index += 2; }
|
|
225
|
+
} else if (state == TomlLexMultilineLiteral) {
|
|
226
|
+
if (startsWith(index, @"'''")) { state = TomlLexNormal; index += 2; }
|
|
227
|
+
} else if (character == '#') {
|
|
228
|
+
return state;
|
|
229
|
+
} else if (startsWith(index, @"\"\"\"")) {
|
|
230
|
+
state = TomlLexMultilineBasic;
|
|
231
|
+
index += 2;
|
|
232
|
+
} else if (startsWith(index, @"'''")) {
|
|
233
|
+
state = TomlLexMultilineLiteral;
|
|
234
|
+
index += 2;
|
|
235
|
+
} else if (character == '"' || character == '\'') {
|
|
236
|
+
for (index++; index < length && [line characterAtIndex:index] != character; index++) {
|
|
237
|
+
if (character == '"' && [line characterAtIndex:index] == '\\') index++;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return state;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// lines[start] 是管理块的起始标记时返回结束标记所在行,否则 NSNotFound。没有结束标记的不算管理块。
|
|
245
|
+
static NSUInteger TomlBlockEnd(NSArray<NSString *> *lines, NSUInteger start) {
|
|
246
|
+
NSCharacterSet *whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet;
|
|
247
|
+
NSString *first = [lines[start] stringByTrimmingCharactersInSet:whitespace];
|
|
206
248
|
for (NSArray<NSString *> *block in TomlBlocks()) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
249
|
+
if (![first isEqualToString:block[0]]) continue;
|
|
250
|
+
for (NSUInteger index = start + 1; index < lines.count; index++) {
|
|
251
|
+
if ([[lines[index] stringByTrimmingCharactersInSet:whitespace] isEqualToString:block[1]]) return index;
|
|
252
|
+
}
|
|
211
253
|
}
|
|
212
|
-
|
|
213
|
-
|
|
254
|
+
return NSNotFound;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// 只移除自己的管理块和安装时加在它前面的那一个空行,其余内容逐字保留:
|
|
258
|
+
// config.toml 是用户的文件,多行字符串里的空行也是值的一部分。
|
|
259
|
+
static NSString *StripTomlBlocks(NSString *text) {
|
|
260
|
+
NSArray<NSString *> *lines = [text componentsSeparatedByString:@"\n"];
|
|
261
|
+
NSMutableArray<NSString *> *kept = [NSMutableArray array];
|
|
262
|
+
TomlLexState state = TomlLexNormal;
|
|
263
|
+
for (NSUInteger index = 0; index < lines.count; index++) {
|
|
264
|
+
NSUInteger end = state == TomlLexNormal ? TomlBlockEnd(lines, index) : NSNotFound;
|
|
265
|
+
if (end != NSNotFound) {
|
|
266
|
+
if ([kept.lastObject isEqualToString:@""]) [kept removeLastObject];
|
|
267
|
+
index = end;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
[kept addObject:lines[index]];
|
|
271
|
+
state = AdvanceTomlState(lines[index], state);
|
|
272
|
+
}
|
|
273
|
+
return [kept componentsJoinedByString:@"\n"];
|
|
214
274
|
}
|
|
215
275
|
|
|
216
276
|
static void SetCodexApprovalAtHome(NSArray<NSString *> *tools, NSString *codexHome) {
|
|
@@ -219,13 +279,11 @@ static void SetCodexApprovalAtHome(NSArray<NSString *> *tools, NSString *codexHo
|
|
|
219
279
|
BOOL exists = stat(file.fileSystemRepresentation, &info) == 0;
|
|
220
280
|
NSString *original = exists ? [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] : @"";
|
|
221
281
|
if (!original) [NSException raise:@"CCBridgeConfig" format:@"Cannot read %@", file];
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
NSString *updated = [[stripped substringToIndex:end] stringByAppendingString:@"\n"];
|
|
228
|
-
if (tools.count > 0) updated = [updated stringByAppendingFormat:@"\n%@\n", CodexApprovalBlock(tools)];
|
|
282
|
+
NSString *updated = StripTomlBlocks(original);
|
|
283
|
+
if (tools.count > 0) {
|
|
284
|
+
if (updated.length > 0 && ![updated hasSuffix:@"\n"]) updated = [updated stringByAppendingString:@"\n"];
|
|
285
|
+
updated = [updated stringByAppendingFormat:updated.length > 0 ? @"\n%@\n" : @"%@\n", CodexApprovalBlock(tools)];
|
|
286
|
+
}
|
|
229
287
|
if ([updated isEqualToString:original]) return;
|
|
230
288
|
NSString *error = nil;
|
|
231
289
|
if (!CCPetsWriteFileAtomically(file, updated, exists ? (info.st_mode & 0777) : 0600, &error)) {
|
|
@@ -233,16 +291,8 @@ static void SetCodexApprovalAtHome(NSArray<NSString *> *tools, NSString *codexHo
|
|
|
233
291
|
}
|
|
234
292
|
}
|
|
235
293
|
|
|
236
|
-
static void SetCodexApproval(NSArray<NSString *> *tools) {
|
|
237
|
-
SetCodexApprovalAtHome(tools, CCPetsCodexHome());
|
|
238
|
-
}
|
|
239
|
-
|
|
240
294
|
#pragma mark - MCP
|
|
241
295
|
|
|
242
|
-
static BOOL RunQuietly(NSString *executable, NSArray<NSString *> *arguments) {
|
|
243
|
-
return CCPetsRun(executable, arguments, nil, nil, 60, NULL, NULL) == 0;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
296
|
static BOOL RunQuietlyForHome(NSString *executable, NSArray<NSString *> *arguments,
|
|
247
297
|
NSString *provider, NSString *home) {
|
|
248
298
|
NSMutableDictionary<NSString *, NSString *> *environment =
|
|
@@ -313,6 +363,16 @@ static BOOL RemoveJSONHooks(NSString *file, BOOL claudeAllow);
|
|
|
313
363
|
NSArray<NSString *> *CCBridgeInstallForProfile(NSDictionary *profile, NSDictionary *rawOptions, BOOL registerMcp) {
|
|
314
364
|
NSString *provider = [profile[@"provider"] isKindOfClass:NSString.class] ? profile[@"provider"] : @"";
|
|
315
365
|
NSString *home = [profile[@"home"] isKindOfClass:NSString.class] ? profile[@"home"] : @"";
|
|
366
|
+
if ([provider isEqual:@"antigravity"]) {
|
|
367
|
+
NSDictionary *options = CCBridgeNormalizeOptions(rawOptions);
|
|
368
|
+
BOOL appEnabled = [options[@"antigravityApp"] boolValue];
|
|
369
|
+
// The App's MCP and Sidecar must run the same Bridge build, including when upgrading
|
|
370
|
+
// an existing CLI-only MCP registration from a different installation.
|
|
371
|
+
BOOL ensureMcp = appEnabled && CCBridgeShouldTouchMCP() && CCPetsProfileIsOfficialDefault(profile);
|
|
372
|
+
NSArray *report = CCPetsInstallAntigravityBridge(profile, options[@"antigravityAllow"], registerMcp || ensureMcp);
|
|
373
|
+
CCPetsConfigureAntigravityBridgeTitle(profile, [options[@"tabTitle"] boolValue]);
|
|
374
|
+
return [report arrayByAddingObjectsFromArray:CCPetsConfigureAntigravityAppSidecar(profile, appEnabled)];
|
|
375
|
+
}
|
|
316
376
|
if (home.length == 0 || (![provider isEqualToString:@"claude"] && ![provider isEqualToString:@"codex"])) {
|
|
317
377
|
[NSException raise:@"CCBridgeConfig" format:@"Invalid Agent profile."];
|
|
318
378
|
}
|
|
@@ -365,6 +425,145 @@ NSArray<NSString *> *CCBridgeInstallForProfile(NSDictionary *profile, NSDictiona
|
|
|
365
425
|
return report;
|
|
366
426
|
}
|
|
367
427
|
|
|
428
|
+
static NSUInteger TomlSkipSpaces(NSString *text, NSUInteger index) {
|
|
429
|
+
while (index < text.length && ([text characterAtIndex:index] == ' ' || [text characterAtIndex:index] == '\t')) index++;
|
|
430
|
+
return index;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// index 指向开引号,返回闭引号的位置;没有闭引号时返回 text.length。
|
|
434
|
+
static NSUInteger TomlStringEnd(NSString *text, NSUInteger index) {
|
|
435
|
+
unichar quote = [text characterAtIndex:index];
|
|
436
|
+
for (index++; index < text.length && [text characterAtIndex:index] != quote; index++) {
|
|
437
|
+
if (quote == '"' && [text characterAtIndex:index] == '\\') index++;
|
|
438
|
+
}
|
|
439
|
+
return MIN(index, text.length);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// 双引号键的转义要先解码才能比较:`"cc-bridge"` 就是 cc-bridge。不合法的转义按原样保留。
|
|
443
|
+
static NSString *TomlDecodeBasicString(NSString *text) {
|
|
444
|
+
if (![text containsString:@"\\"]) return text;
|
|
445
|
+
NSDictionary<NSString *, NSString *> *simple = @{@"b": @"\b", @"t": @"\t", @"n": @"\n", @"f": @"\f", @"r": @"\r",
|
|
446
|
+
@"e": @"\033", @"\"": @"\"", @"\\": @"\\"};
|
|
447
|
+
NSDictionary<NSString *, NSNumber *> *numeric = @{@"x": @2, @"u": @4, @"U": @8};
|
|
448
|
+
NSCharacterSet *notHex = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"].invertedSet;
|
|
449
|
+
NSMutableString *decoded = [NSMutableString string];
|
|
450
|
+
for (NSUInteger index = 0; index < text.length; index++) {
|
|
451
|
+
unichar character = [text characterAtIndex:index];
|
|
452
|
+
if (character != '\\' || index + 1 >= text.length) {
|
|
453
|
+
[decoded appendFormat:@"%C", character];
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
NSString *escape = [text substringWithRange:NSMakeRange(++index, 1)];
|
|
457
|
+
if (simple[escape]) {
|
|
458
|
+
[decoded appendString:simple[escape]];
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
NSUInteger digits = numeric[escape].unsignedIntegerValue;
|
|
462
|
+
NSString *hex = digits > 0 && index + digits < text.length
|
|
463
|
+
? [text substringWithRange:NSMakeRange(index + 1, digits)] : nil;
|
|
464
|
+
uint32_t scalar = hex ? (uint32_t)strtoul(hex.UTF8String, NULL, 16) : 0;
|
|
465
|
+
BOOL valid = hex && [hex rangeOfCharacterFromSet:notHex].location == NSNotFound &&
|
|
466
|
+
scalar <= 0x10FFFF && (scalar < 0xD800 || scalar > 0xDFFF);
|
|
467
|
+
if (!valid) {
|
|
468
|
+
[decoded appendFormat:@"\\%@", escape];
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
[decoded appendString:[[NSString alloc] initWithBytes:&scalar length:sizeof scalar encoding:NSUTF32LittleEndianStringEncoding]];
|
|
472
|
+
index += digits;
|
|
473
|
+
}
|
|
474
|
+
return decoded;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// 从 cursor 读一个键路径(每一级可以是裸键或带引号),停在路径后第一个非空白字符上。不是键时返回 nil。
|
|
478
|
+
static NSArray<NSString *> *TomlParseKey(NSString *text, NSUInteger *cursor) {
|
|
479
|
+
NSMutableCharacterSet *bare = [NSMutableCharacterSet alphanumericCharacterSet];
|
|
480
|
+
[bare addCharactersInString:@"_-"];
|
|
481
|
+
NSMutableArray<NSString *> *path = [NSMutableArray array];
|
|
482
|
+
NSUInteger index = *cursor;
|
|
483
|
+
while (YES) {
|
|
484
|
+
index = TomlSkipSpaces(text, index);
|
|
485
|
+
if (index >= text.length) return nil;
|
|
486
|
+
unichar character = [text characterAtIndex:index];
|
|
487
|
+
if (character == '"' || character == '\'') {
|
|
488
|
+
NSUInteger end = TomlStringEnd(text, index);
|
|
489
|
+
if (end >= text.length) return nil;
|
|
490
|
+
NSString *key = [text substringWithRange:NSMakeRange(index + 1, end - index - 1)];
|
|
491
|
+
[path addObject:character == '"' ? TomlDecodeBasicString(key) : key];
|
|
492
|
+
index = end + 1;
|
|
493
|
+
} else {
|
|
494
|
+
NSUInteger start = index;
|
|
495
|
+
while (index < text.length && [bare characterIsMember:[text characterAtIndex:index]]) index++;
|
|
496
|
+
if (index == start) return nil;
|
|
497
|
+
[path addObject:[text substringWithRange:NSMakeRange(start, index - start)]];
|
|
498
|
+
}
|
|
499
|
+
index = TomlSkipSpaces(text, index);
|
|
500
|
+
if (index >= text.length || [text characterAtIndex:index] != '.') break;
|
|
501
|
+
index++;
|
|
502
|
+
}
|
|
503
|
+
*cursor = index;
|
|
504
|
+
return path;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// index 指向 { 之后:这张 inline table 的第一层是否有名为 name 的键。
|
|
508
|
+
static BOOL TomlInlineTableHasKey(NSString *text, NSUInteger index, NSString *name) {
|
|
509
|
+
while (YES) {
|
|
510
|
+
NSArray<NSString *> *key = TomlParseKey(text, &index);
|
|
511
|
+
if (!key || index >= text.length || [text characterAtIndex:index] != '=') return NO;
|
|
512
|
+
if ([key.firstObject isEqualToString:name]) return YES;
|
|
513
|
+
NSInteger depth = 0;
|
|
514
|
+
for (index++; index < text.length; index++) {
|
|
515
|
+
unichar character = [text characterAtIndex:index];
|
|
516
|
+
if (character == '"' || character == '\'') {
|
|
517
|
+
index = TomlStringEnd(text, index);
|
|
518
|
+
} else if (character == '{' || character == '[') {
|
|
519
|
+
depth++;
|
|
520
|
+
} else if (character == '}' || character == ']') {
|
|
521
|
+
if (depth == 0) return NO;
|
|
522
|
+
depth--;
|
|
523
|
+
} else if (character == ',' && depth == 0) {
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (index >= text.length) return NO;
|
|
528
|
+
index++;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// 只认 mcp_servers 下这个服务器的声明:表头、点号键、[mcp_servers] 表里的键,以及 inline table,
|
|
533
|
+
// 每一级都可以带引号。项目路径、其他服务器的参数里出现同样的文字都不算,否则卸载会误报"移除失败";
|
|
534
|
+
// 真正的声明也不能漏,否则会跳过 mcp remove,注册还在却报告成功。
|
|
535
|
+
static BOOL TomlDeclaresMCPServer(NSString *text, NSString *name) {
|
|
536
|
+
BOOL (^isServer)(NSArray<NSString *> *) = ^BOOL(NSArray<NSString *> *path) {
|
|
537
|
+
return path.count >= 2 && [path[0] isEqualToString:@"mcp_servers"] && [path[1] isEqualToString:name];
|
|
538
|
+
};
|
|
539
|
+
TomlLexState state = TomlLexNormal;
|
|
540
|
+
NSArray<NSString *> *table = @[];
|
|
541
|
+
for (NSString *line in [text componentsSeparatedByString:@"\n"]) {
|
|
542
|
+
if (state == TomlLexNormal) {
|
|
543
|
+
NSUInteger index = TomlSkipSpaces(line, 0);
|
|
544
|
+
if (index < line.length && [line characterAtIndex:index] == '[') {
|
|
545
|
+
index += index + 1 < line.length && [line characterAtIndex:index + 1] == '[' ? 2 : 1;
|
|
546
|
+
NSArray<NSString *> *header = TomlParseKey(line, &index);
|
|
547
|
+
if (header && index < line.length && [line characterAtIndex:index] == ']') {
|
|
548
|
+
if (isServer(header)) return YES;
|
|
549
|
+
table = header;
|
|
550
|
+
}
|
|
551
|
+
} else {
|
|
552
|
+
NSArray<NSString *> *key = TomlParseKey(line, &index);
|
|
553
|
+
if (key && index < line.length && [line characterAtIndex:index] == '=') {
|
|
554
|
+
NSArray<NSString *> *path = [table arrayByAddingObjectsFromArray:key];
|
|
555
|
+
if (isServer(path)) return YES;
|
|
556
|
+
NSUInteger value = TomlSkipSpaces(line, index + 1);
|
|
557
|
+
if ([path isEqualToArray:@[@"mcp_servers"]] && value < line.length && [line characterAtIndex:value] == '{' &&
|
|
558
|
+
TomlInlineTableHasKey(line, value + 1, name)) return YES;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
state = AdvanceTomlState(line, state);
|
|
563
|
+
}
|
|
564
|
+
return NO;
|
|
565
|
+
}
|
|
566
|
+
|
|
368
567
|
// remove 对“本来就不存在”的注册也可能返回非零;只有确认不存在才按成功处理。
|
|
369
568
|
static BOOL HasMCPRegistration(NSString *provider, NSString *home, NSString *name) {
|
|
370
569
|
if ([provider isEqualToString:@"claude"]) {
|
|
@@ -380,12 +579,7 @@ static BOOL HasMCPRegistration(NSString *provider, NSString *home, NSString *nam
|
|
|
380
579
|
if (![NSFileManager.defaultManager fileExistsAtPath:file]) return NO;
|
|
381
580
|
NSString *text = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
|
|
382
581
|
if (!text) [NSException raise:@"CCBridgeConfig" format:@"Cannot read %@", file];
|
|
383
|
-
|
|
384
|
-
for (NSString *line in [text componentsSeparatedByString:@"\n"]) {
|
|
385
|
-
NSString *trimmed = [line stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
|
|
386
|
-
if (![trimmed hasPrefix:@"#"] && [trimmed containsString:name]) return YES;
|
|
387
|
-
}
|
|
388
|
-
return NO;
|
|
582
|
+
return TomlDeclaresMCPServer(text, name);
|
|
389
583
|
}
|
|
390
584
|
|
|
391
585
|
static void RemoveMCPRegistration(NSString *executable, NSString *provider, NSString *home,
|
|
@@ -404,6 +598,11 @@ static void RemoveMCPRegistration(NSString *executable, NSString *provider, NSSt
|
|
|
404
598
|
|
|
405
599
|
NSArray<NSString *> *CCBridgeUninstallForProfile(NSDictionary *profile, BOOL unregisterMcp) {
|
|
406
600
|
NSString *provider = [profile[@"provider"] isKindOfClass:NSString.class] ? profile[@"provider"] : @"";
|
|
601
|
+
if ([provider isEqual:@"antigravity"]) {
|
|
602
|
+
CCPetsConfigureAntigravityBridgeTitle(profile, NO);
|
|
603
|
+
NSArray *sidecar = CCPetsConfigureAntigravityAppSidecar(profile, NO);
|
|
604
|
+
return [sidecar arrayByAddingObjectsFromArray:CCPetsRemoveAntigravityBridge(profile, unregisterMcp)];
|
|
605
|
+
}
|
|
407
606
|
NSString *home = [profile[@"home"] isKindOfClass:NSString.class] ? profile[@"home"] : @"";
|
|
408
607
|
NSMutableArray<NSString *> *report = [NSMutableArray array];
|
|
409
608
|
if ([provider isEqualToString:@"claude"]) {
|
|
@@ -428,66 +627,6 @@ NSArray<NSString *> *CCBridgeUninstallForProfile(NSDictionary *profile, BOOL unr
|
|
|
428
627
|
return report;
|
|
429
628
|
}
|
|
430
629
|
|
|
431
|
-
NSArray<NSString *> *CCBridgeInstall(NSDictionary *rawOptions, BOOL registerMcp) {
|
|
432
|
-
NSDictionary *options = CCBridgeNormalizeOptions(rawOptions);
|
|
433
|
-
NSArray<NSString *> *claudeAllow = options[@"claudeAllow"];
|
|
434
|
-
NSArray<NSString *> *codexApprove = options[@"codexApprove"];
|
|
435
|
-
NSMutableArray<NSString *> *report = [NSMutableArray array];
|
|
436
|
-
if (CCPetsClaudeCLIInstalled()) {
|
|
437
|
-
[NSFileManager.defaultManager createDirectoryAtPath:CCPetsClaudeHome() withIntermediateDirectories:YES attributes:nil error:nil];
|
|
438
|
-
NSString *settingsPath = [CCPetsClaudeHome() stringByAppendingPathComponent:@"settings.json"];
|
|
439
|
-
CCPetsJSONObject *config = ReadJSONConfig(settingsPath);
|
|
440
|
-
StripBridgeHooks(config);
|
|
441
|
-
AddClaudeHooks(config, options, nil);
|
|
442
|
-
ApplyClaudeAllow(config, claudeAllow);
|
|
443
|
-
WriteJSONConfig(settingsPath, config);
|
|
444
|
-
[report addObject:LF(L(@"Claude Code hooks written to {path}"), @{@"path": settingsPath})];
|
|
445
|
-
[report addObject:claudeAllow.count > 0
|
|
446
|
-
? LF(L(@"Claude Code allows these tools without asking: {claudeAllow}"), @{@"claudeAllow": [claudeAllow componentsJoinedByString:@", "]})
|
|
447
|
-
: L(@"In Claude Code, cc-bridge tools follow your permission settings.")];
|
|
448
|
-
if (registerMcp) {
|
|
449
|
-
NSString *claude = CCPetsFindRealExecutable(@"claude", @"CLAUDE_REAL_BIN");
|
|
450
|
-
for (NSString *name in MCPNames()) RunQuietly(claude ?: @"claude", @[@"mcp", @"remove", @"--scope", @"user", name]);
|
|
451
|
-
BOOL ok = claude && RunQuietly(claude, ClaudeMCPAddArguments());
|
|
452
|
-
[report addObject:ok
|
|
453
|
-
? LF(L(@"Claude Code MCP registered: {MCP_NAME} (on first use, Claude asks according to your permission settings)"), @{@"MCP_NAME": CCBridgeMCPName})
|
|
454
|
-
: LF(L(@"Claude Code MCP registration failed. Run manually: claude {shellQuote}"), @{@"shellQuote": QuotedArguments(ClaudeMCPAddArguments())})];
|
|
455
|
-
}
|
|
456
|
-
} else {
|
|
457
|
-
[report addObject:L(@"Claude Code CLI not detected, skipped.")];
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (CCPetsCodexCLIInstalled()) {
|
|
461
|
-
[NSFileManager.defaultManager createDirectoryAtPath:CCPetsCodexHome() withIntermediateDirectories:YES attributes:nil error:nil];
|
|
462
|
-
NSString *hooksPath = [CCPetsCodexHome() stringByAppendingPathComponent:@"hooks.json"];
|
|
463
|
-
CCPetsJSONObject *config = ReadJSONConfig(hooksPath);
|
|
464
|
-
StripBridgeHooks(config);
|
|
465
|
-
AddCodexHooks(config, options, nil);
|
|
466
|
-
WriteJSONConfig(hooksPath, config);
|
|
467
|
-
[report addObject:LF(L(@"Codex hooks written to {path}. Next time you start Codex, run /hooks and trust the cc-pets bridge hooks."), @{@"path": hooksPath})];
|
|
468
|
-
if (registerMcp) {
|
|
469
|
-
NSString *codex = CCPetsFindRealExecutable(@"codex", @"CODEX_REAL_BIN");
|
|
470
|
-
for (NSString *name in MCPNames()) RunQuietly(codex ?: @"codex", @[@"mcp", @"remove", name]);
|
|
471
|
-
BOOL ok = codex && RunQuietly(codex, CodexMCPAddArguments());
|
|
472
|
-
[report addObject:ok
|
|
473
|
-
? LF(L(@"Codex MCP registered: {MCP_NAME}"), @{@"MCP_NAME": CCBridgeMCPName})
|
|
474
|
-
: LF(L(@"Codex MCP registration failed. Run manually: codex {shellQuote}"), @{@"shellQuote": QuotedArguments(CodexMCPAddArguments())})];
|
|
475
|
-
// 只在 enable / refresh 时探测:configure 由桌宠菜单开关触发,不值得每次多拉起两次 codex。
|
|
476
|
-
if ([options[@"wake"] boolValue]) {
|
|
477
|
-
NSString *notice = CCBridgeDescribeCodexQueue(CCBridgeProbeCodexQueue(codex));
|
|
478
|
-
if (notice) [report addObject:notice];
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
SetCodexApproval(codexApprove);
|
|
482
|
-
[report addObject:codexApprove.count > 0
|
|
483
|
-
? LF(L(@"Codex auto-approves these tools: {codexApprove} (restart Codex sessions to apply)"), @{@"codexApprove": [codexApprove componentsJoinedByString:@", "]})
|
|
484
|
-
: L(@"cc-bridge tools keep Codex's default approvals (calls fail under approval_policy=never; allow them with --codex-approve).")];
|
|
485
|
-
} else {
|
|
486
|
-
[report addObject:L(@"Codex CLI not detected, skipped.")];
|
|
487
|
-
}
|
|
488
|
-
return report;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
630
|
// 文件不存在、或里面本来就没有我们的东西时不改写——用户的文件不该因为一次卸载被重排格式。
|
|
492
631
|
static BOOL RemoveJSONHooks(NSString *file, BOOL claudeAllow) {
|
|
493
632
|
if (![NSFileManager.defaultManager fileExistsAtPath:file]) return NO;
|
|
@@ -498,10 +637,3 @@ static BOOL RemoveJSONHooks(NSString *file, BOOL claudeAllow) {
|
|
|
498
637
|
WriteJSONConfig(file, config);
|
|
499
638
|
return YES;
|
|
500
639
|
}
|
|
501
|
-
|
|
502
|
-
NSArray<NSString *> *CCBridgeUninstall(BOOL unregisterMcp) {
|
|
503
|
-
NSMutableArray *report = [NSMutableArray array];
|
|
504
|
-
[report addObjectsFromArray:CCBridgeUninstallForProfile(@{@"provider": @"claude", @"home": CCPetsClaudeHome()}, unregisterMcp)];
|
|
505
|
-
[report addObjectsFromArray:CCBridgeUninstallForProfile(@{@"provider": @"codex", @"home": CCPetsCodexHome()}, unregisterMcp)];
|
|
506
|
-
return report;
|
|
507
|
-
}
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
extern const NSUInteger CCBridgeMessageBodyLimit;
|
|
15
15
|
extern const long long CCBridgeMessageTTL;
|
|
16
16
|
extern const NSUInteger CCBridgePairLimitCount;
|
|
17
|
+
extern NSString *const CCBridgeAntigravityProvider;
|
|
18
|
+
extern NSString *const CCBridgeAntigravityAppProvider;
|
|
17
19
|
|
|
18
20
|
#pragma mark - 开关与选项
|
|
19
21
|
|
|
@@ -48,9 +50,16 @@ NSString *CCBridgeSHA1Hex(NSString *text);
|
|
|
48
50
|
NSString *CCBridgeSessionRef(NSString *sessionId);
|
|
49
51
|
NSDictionary *CCBridgeReadSession(id sessionId);
|
|
50
52
|
NSArray<NSDictionary *> *CCBridgeListSessions(void);
|
|
53
|
+
NSNumber *CCBridgeSessionHostPID(NSDictionary *session);
|
|
51
54
|
void CCBridgeRemoveSession(id sessionId);
|
|
52
55
|
void CCBridgeRemoveSessionIfOffline(id sessionId);
|
|
53
56
|
NSString *CCBridgeNormalizeSessionName(id value);
|
|
57
|
+
// 仅对标记为自动生成的名字去掉账号段;自定义名字及旧记录保持原名。
|
|
58
|
+
NSString *CCBridgeSessionShortName(NSDictionary *session);
|
|
59
|
+
// 中文界面下自动名称的动物别名;其他语言、自定义名称和旧记录返回空串。
|
|
60
|
+
NSString *CCBridgeSessionAnimalAlias(NSDictionary *session);
|
|
61
|
+
// 仅供展示,不可作为消息地址;原 name / ref 始终保持不变。
|
|
62
|
+
NSString *CCBridgeSessionDisplayName(NSDictionary *session);
|
|
54
63
|
// liveSessions 参数保留兼容;名字分配/改名在注册表锁内重新读取在线会话,避免使用过期快照。
|
|
55
64
|
NSDictionary *CCBridgeUpsertSession(NSString *sessionId, NSString *provider, NSNumber *pid, NSString *cwd,
|
|
56
65
|
NSString *tty, NSString *status, NSString *preferredName, NSString *profileID,
|
|
@@ -67,14 +76,23 @@ NSDictionary *CCBridgeUpdateReceipt(NSString *messageId, NSDictionary *patch);
|
|
|
67
76
|
NSArray<NSDictionary *> *CCBridgeListReceipts(void);
|
|
68
77
|
// 发送前的配额检查。nil 表示放行,否则返回拒绝原因。
|
|
69
78
|
NSString *CCBridgeCheckSendLimits(NSString *fromSession, NSString *toSession);
|
|
70
|
-
|
|
79
|
+
// 写入目标信箱。返回 nil 表示成功;Bridge 已关闭或目标已离线时不写入,返回拒绝原因。
|
|
80
|
+
NSString *CCBridgeEnqueueInbox(NSDictionary *message);
|
|
71
81
|
// 认领并持有信箱锁:恢复上次未确认的 claimed;调用方必须在输出后 FinishInbox。
|
|
72
82
|
NSArray<NSDictionary *> *CCBridgeClaimInbox(id sessionId);
|
|
73
83
|
// transport 非 nil 表示输出成功;nil 保留消息供重试。两种情况都会释放锁。
|
|
74
84
|
void CCBridgeFinishInbox(id sessionId, NSArray<NSDictionary *> *messages, NSString *transport);
|
|
75
85
|
NSUInteger CCBridgePendingCount(id sessionId);
|
|
86
|
+
// 无锁查看信箱中最早的消息文件名,不读取或认领正文。
|
|
87
|
+
NSString *CCBridgeOldestPendingMessageName(id sessionId);
|
|
76
88
|
// 会话结束:信箱里还没投出去的消息标记为 undeliverable。
|
|
77
89
|
void CCBridgeAbandonInbox(id sessionId);
|
|
90
|
+
// 同上,但用于没有 SessionEnd 的离线会话(崩溃、被杀):连同它的空闲订阅一起清掉;锁繁忙或会话又上线时不动。
|
|
91
|
+
void CCBridgeAbandonOfflineSession(id sessionId);
|
|
92
|
+
// 关闭 Bridge:之后不会再有 hook 来清理,所有未投递的消息现在就标记 undeliverable 并删除。
|
|
93
|
+
void CCBridgeAbandonAllInboxes(NSString *reason);
|
|
94
|
+
// 扫描状态目录:离线会话按上面处理,在线会话里超过 TTL 的消息标记为 expired。锁繁忙的留给下一次。
|
|
95
|
+
void CCBridgeSweepMessages(void);
|
|
78
96
|
void CCBridgePruneReceipts(void);
|
|
79
97
|
|
|
80
98
|
#pragma mark - 空闲订阅(一次性)
|
|
@@ -82,23 +100,35 @@ void CCBridgePruneReceipts(void);
|
|
|
82
100
|
void CCBridgeAddIdleSubscription(NSString *targetSession, NSString *subscriberSession);
|
|
83
101
|
BOOL CCBridgeHasIdleSubscriptions(NSString *targetSession);
|
|
84
102
|
NSArray<NSString *> *CCBridgeTakeIdleSubscriptions(NSString *targetSession);
|
|
103
|
+
void CCBridgeClearIdleSubscriptions(id targetSession);
|
|
104
|
+
|
|
105
|
+
#pragma mark - watcher 接替
|
|
106
|
+
|
|
107
|
+
// 新 watcher 登记后,旧的在下次轮询时退出,新的拿到锁并重新计时:每轮结束都能续满等待时间。
|
|
108
|
+
NSString *CCBridgeClaimWatcher(NSString *sessionId);
|
|
109
|
+
BOOL CCBridgeWatcherOwned(NSString *sessionId, NSString *token);
|
|
110
|
+
// 会话结束:清掉登记,还在等待的 watcher 随之退出。
|
|
111
|
+
void CCBridgeDropWatcher(id sessionId);
|
|
85
112
|
|
|
86
113
|
#pragma mark - 锁
|
|
87
114
|
|
|
88
115
|
// 稳定 inode 上的 flock;PID 文件只作诊断。进程退出自动释放,成功返回诊断文件路径。
|
|
89
116
|
NSString *CCBridgeAcquireLock(NSString *name);
|
|
90
117
|
void CCBridgeReleaseLock(NSString *lockFile);
|
|
118
|
+
// 等锁(有上限,超时抛异常)后执行 body。锁不可重入,body 里不能再取同名锁。
|
|
119
|
+
id CCBridgeWithLock(NSString *name, id (^body)(void));
|
|
91
120
|
|
|
92
121
|
#pragma mark - 进程探测
|
|
93
122
|
|
|
94
123
|
BOOL CCBridgeProcessAlive(pid_t pid);
|
|
95
|
-
|
|
124
|
+
// 进程的 argv,元素边界与启动时一致;读不到时返回 nil。
|
|
125
|
+
NSArray<NSString *> *CCBridgeProcessArguments(pid_t pid);
|
|
96
126
|
// CLI 进程的控制终端,如 "ttys003";没有时返回 nil。
|
|
97
127
|
NSString *CCBridgeProcessTty(pid_t pid);
|
|
98
|
-
BOOL
|
|
99
|
-
// 离 startPid 最近的
|
|
128
|
+
BOOL CCBridgeArgumentsMatchProvider(NSString *provider, NSArray<NSString *> *arguments);
|
|
129
|
+
// 离 startPid 最近的 Agent 祖先进程:@{provider, pid}。CC_BRIDGE_AGENT=Codex:1234 可强制指定。
|
|
100
130
|
NSDictionary *CCBridgeFindAgentAncestor(pid_t startPid);
|
|
101
|
-
// 有 pid
|
|
131
|
+
// 有 pid 时检查进程与宿主类型。Claude/Codex 的旧记录没有 pid 时使用活动窗口。
|
|
102
132
|
BOOL CCBridgeSessionLive(NSDictionary *session);
|
|
103
133
|
// `codex queue` 能力探测:@{status: supported|unsupported|missing|unknown, executable, version}。
|
|
104
134
|
NSDictionary *CCBridgeProbeCodexQueue(NSString *executable);
|