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.
Files changed (111) hide show
  1. package/CC_BRIDGE.md +93 -14
  2. package/CC_BRIDGE.zh-CN.md +71 -11
  3. package/CHANGELOG.md +55 -0
  4. package/CHANGELOG.zh-CN.md +56 -1
  5. package/CONTRIBUTING.md +1 -1
  6. package/CONTRIBUTING.zh-CN.md +1 -1
  7. package/PROVIDER_PROTOCOL.md +5 -1
  8. package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
  9. package/README.md +116 -34
  10. package/README.zh-CN.md +99 -35
  11. package/Resources/AgentIcons/antigravity.icns +0 -0
  12. package/Resources/zh-Hans.lproj/Localizable.strings +99 -26
  13. package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +51 -4
  14. package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +120 -17
  15. package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +24 -11
  16. package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
  17. package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +24 -1
  18. package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +21 -11
  19. package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +33 -17
  20. package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +43 -7
  21. package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +7 -4
  22. package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +7 -4
  23. package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +24 -22
  24. package/Sources/CCPets/App/CCPetsAppDelegate.h +10 -0
  25. package/Sources/CCPets/App/CCPetsAppDelegate.m +18 -8
  26. package/Sources/CCPets/App/CCPetsVersion.h +4 -0
  27. package/Sources/CCPets/App/CCPetsVersion.m +34 -0
  28. package/Sources/CCPets/App/main.m +11 -0
  29. package/Sources/CCPets/Bridge/CCPetsBridge.m +41 -17
  30. package/Sources/CCPets/Bridge/CCPetsBridgeAntigravityApp.h +4 -0
  31. package/Sources/CCPets/Bridge/CCPetsBridgeAntigravityApp.m +119 -0
  32. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +3 -1
  33. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +272 -50
  34. package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +12 -3
  35. package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +341 -95
  36. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
  37. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +227 -95
  38. package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +35 -5
  39. package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +310 -42
  40. package/Sources/CCPets/Bridge/CCPetsBridgeTerminalTitle.h +12 -0
  41. package/Sources/CCPets/Bridge/CCPetsBridgeTerminalTitle.m +105 -0
  42. package/Sources/CCPets/Integrations/CCPetsAntigravityConfig.h +17 -0
  43. package/Sources/CCPets/Integrations/CCPetsAntigravityConfig.m +462 -0
  44. package/Sources/CCPets/Integrations/CCPetsAntigravityEvents.h +5 -0
  45. package/Sources/CCPets/Integrations/CCPetsAntigravityEvents.m +180 -0
  46. package/Sources/CCPets/Integrations/CCPetsBridgeTitleRelay.h +3 -0
  47. package/Sources/CCPets/Integrations/CCPetsBridgeTitleRelay.m +463 -0
  48. package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
  49. package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
  50. package/Sources/CCPets/Integrations/CCPetsIntegrations.h +1 -1
  51. package/Sources/CCPets/Integrations/CCPetsIntegrations.m +38 -8
  52. package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +15 -5
  53. package/Sources/CCPets/Integrations/CCPetsProfiles.h +2 -1
  54. package/Sources/CCPets/Integrations/CCPetsProfiles.m +112 -81
  55. package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +11 -1
  56. package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +217 -45
  57. package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
  58. package/Sources/CCPets/Pet/CCPetsPreviews.m +16 -8
  59. package/Sources/CCPets/Pet/PetView+Menu.m +64 -5
  60. package/Sources/CCPets/Pet/PetView.h +7 -0
  61. package/Sources/CCPets/Pet/PetView.m +12 -1
  62. package/Sources/CCPets/Support/CCPetsDoctor.m +126 -37
  63. package/Sources/CCPets/Support/CCPetsPaths.h +5 -0
  64. package/Sources/CCPets/Support/CCPetsPaths.m +15 -0
  65. package/Sources/CCPets/Support/CCPetsPreferences.h +14 -1
  66. package/Sources/CCPets/Support/CCPetsPreferences.m +36 -0
  67. package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
  68. package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
  69. package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
  70. package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +533 -0
  71. package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
  72. package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +98 -0
  73. package/Sources/CCPets/UI/CCPetsSettingsPage.h +7 -1
  74. package/Sources/CCPets/UI/CCPetsSettingsPage.m +205 -8
  75. package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
  76. package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
  77. package/Sources/CCPets/UI/CCPetsUIStyle.m +3 -1
  78. package/Sources/CCPets/UI/CCPetsUsagePage.h +4 -3
  79. package/Sources/CCPets/UI/CCPetsUsagePage.m +69 -43
  80. package/Sources/CCPets/UI/CCPetsUsageStore.m +11 -1
  81. package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +6 -4
  82. package/Sources/CCPets/Usage/CCPetsAntigravityQuota.m +115 -0
  83. package/Sources/CCPets/Usage/CCPetsNormalizedUsage.h +5 -0
  84. package/Sources/CCPets/Usage/CCPetsNormalizedUsage.m +43 -0
  85. package/Sources/CCPets/Usage/CCPetsProviderRegistry+Testing.h +13 -0
  86. package/Sources/CCPets/Usage/CCPetsProviderRegistry.h +25 -0
  87. package/Sources/CCPets/Usage/CCPetsProviderRegistry.m +94 -0
  88. package/Sources/CCPets/Usage/CCPetsQuotaHistory.h +1 -1
  89. package/Sources/CCPets/Usage/CCPetsQuotaHistory.m +11 -8
  90. package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +2 -0
  91. package/Sources/CCPets/Usage/CCPetsUsage+Models.m +206 -14
  92. package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +4 -4
  93. package/Sources/CCPets/Usage/CCPetsUsage.h +7 -2
  94. package/Sources/CCPets/Usage/CCPetsUsageMode.h +2 -0
  95. package/Sources/CCPets/Usage/CCPetsUsageMode.m +29 -4
  96. package/Sources/CCPets/Usage/CCPetsUsageMonitor.h +4 -4
  97. package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +90 -7
  98. package/Sources/CCPets/Usage/QuotaDashboardView+CompactRow.m +81 -0
  99. package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +5 -8
  100. package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +26 -4
  101. package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +99 -99
  102. package/Sources/CCPets/Usage/QuotaDashboardView.h +9 -10
  103. package/Sources/CCPets/Usage/QuotaDashboardView.m +232 -79
  104. package/THIRD_PARTY_NOTICES.md +5 -4
  105. package/THIRD_PARTY_NOTICES.zh-CN.md +6 -5
  106. package/bin/agy-with-pet +82 -0
  107. package/bin/cc-pets +1 -1
  108. package/bin/claude-with-pet +15 -0
  109. package/bin/codex-with-pet +17 -2
  110. package/package.json +5 -3
  111. package/scripts/install-shell-integration.sh +5 -0
@@ -0,0 +1,105 @@
1
+ #import "CCPetsBridgeTerminalTitle.h"
2
+ #import "CCPetsBridge.h"
3
+ #import "CCPetsBridgeStore.h"
4
+ #import "CCPetsCLISupport.h"
5
+ #import <unistd.h>
6
+
7
+ static NSString *StringValue(id value) {
8
+ return [value isKindOfClass:NSString.class] ? value : nil;
9
+ }
10
+
11
+ static NSString *BindingPath(NSString *launchToken, BOOL createDirectory) {
12
+ NSString *rawToken = StringValue(launchToken);
13
+ NSUUID *token = rawToken.length == 36 ? [[NSUUID alloc] initWithUUIDString:rawToken] : nil;
14
+ if (!token) return nil;
15
+ NSString *directory = createDirectory ? CCBridgeSubdirectory(@[@"terminal-titles"])
16
+ : [CCBridgeStateDirectory() stringByAppendingPathComponent:@"terminal-titles"];
17
+ return [directory stringByAppendingPathComponent:[token.UUIDString.lowercaseString stringByAppendingString:@".json"]];
18
+ }
19
+
20
+ BOOL CCBridgeTerminalTitleEnabled(void) {
21
+ return CCBridgeEnabled() && [CCBridgeOptions()[@"tabTitle"] boolValue];
22
+ }
23
+
24
+ static void BindTerminalTitle(NSString *launchToken, NSString *provider, NSString *sessionID, BOOL replace) {
25
+ if (!provider.length || !CCBridgeSafeSessionId(sessionID)) return;
26
+ NSString *path = BindingPath(launchToken, YES);
27
+ if (!path) return;
28
+ CCBridgeWithLock(@"terminal-title-bindings", ^id {
29
+ NSDictionary *binding = CCBridgeReadJSON(path);
30
+ if (!binding || (replace && ![binding[@"session"] isEqual:sessionID])) {
31
+ CCBridgeWriteJSON(path, @{@"provider": provider, @"session": sessionID});
32
+ }
33
+ return nil;
34
+ });
35
+ }
36
+
37
+ void CCBridgeBindTerminalTitle(NSString *launchToken, NSString *provider, NSString *sessionID) {
38
+ BindTerminalTitle(launchToken, provider, sessionID, YES);
39
+ }
40
+
41
+ void CCBridgeBindTerminalTitleIfUnbound(NSString *launchToken, NSString *provider, NSString *sessionID) {
42
+ BindTerminalTitle(launchToken, provider, sessionID, NO);
43
+ }
44
+
45
+ void CCBridgeClearTerminalTitleBinding(NSString *launchToken) {
46
+ NSString *path = BindingPath(launchToken, NO);
47
+ if (path) unlink(path.fileSystemRepresentation);
48
+ }
49
+
50
+ NSString *CCBridgeTerminalTitleNameForSession(NSString *sessionID, NSString *provider) {
51
+ if (!CCBridgeTerminalTitleEnabled() || !CCBridgeSafeSessionId(sessionID)) return nil;
52
+ NSDictionary *record = CCBridgeReadSession(sessionID);
53
+ if (![record[@"provider"] isEqual:provider] || !CCBridgeSessionLive(record)) return nil;
54
+ NSString *alias = CCBridgeSessionAnimalAlias(record);
55
+ if (alias.length > 0) return alias;
56
+ NSString *name = CCBridgeNormalizeSessionName(record[@"name"]);
57
+ if ([record[@"nameSource"] isEqual:@"generated"]) {
58
+ NSArray<NSString *> *parts = [name componentsSeparatedByString:@"."];
59
+ if (parts.count == 3) return parts.lastObject;
60
+ }
61
+ return name;
62
+ }
63
+
64
+ NSString *CCBridgeTerminalTitleName(NSString *provider, NSString *launchToken, pid_t childPID) {
65
+ if (!CCBridgeProcessAlive(childPID)) return nil;
66
+ NSString *path = BindingPath(launchToken, NO);
67
+ id binding = path ? CCBridgeReadJSON(path) : nil;
68
+ if (![binding isKindOfClass:NSDictionary.class] || ![binding[@"provider"] isEqual:provider]) return nil;
69
+ return CCBridgeTerminalTitleNameForSession(StringValue(binding[@"session"]), provider);
70
+ }
71
+
72
+ static NSString *PlainTitle(NSString *value) {
73
+ if (!value.length) return @"";
74
+ NSString *line = [[value componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet] componentsJoinedByString:@" "];
75
+ NSArray<NSString *> *parts = [line componentsSeparatedByCharactersInSet:NSCharacterSet.controlCharacterSet];
76
+ return [[parts componentsJoinedByString:@""] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
77
+ }
78
+
79
+ NSString *CCBridgeComposeTerminalTitle(NSString *base, NSString *nickname) {
80
+ NSString *title = PlainTitle(base);
81
+ NSString *name = PlainTitle(nickname);
82
+ if (!name.length) return title;
83
+ return title.length > 0 ? [NSString stringWithFormat:@"%@ · %@", title, name] : name;
84
+ }
85
+
86
+ int CCBridgePrintAntigravityTitle(NSString *originalTitle) {
87
+ @try {
88
+ NSData *input = [NSFileHandle.fileHandleWithStandardInput readDataToEndOfFile];
89
+ id state = input.length > 0 ? [NSJSONSerialization JSONObjectWithData:input options:0 error:nil] : nil;
90
+ if (![state isKindOfClass:NSDictionary.class]) state = @{};
91
+ NSString *sessionID = StringValue(state[@"conversation_id"]) ?: StringValue(state[@"session_id"]);
92
+ NSString *nickname = CCBridgeTerminalTitleNameForSession(sessionID, CCBridgeAntigravityProvider);
93
+ NSString *base = originalTitle;
94
+ if (base == nil) {
95
+ NSString *title = PlainTitle(StringValue(state[@"conversation_title"]));
96
+ NSString *project = StringValue(state[@"cwd"]).lastPathComponent;
97
+ base = title.length > 0 && project.length > 0 ? [NSString stringWithFormat:@"%@ | %@", title, project]
98
+ : (title.length > 0 ? title : project);
99
+ }
100
+ CCPetsPrintLine(stdout, CCBridgeComposeTerminalTitle(base, nickname));
101
+ } @catch (NSException *exception) {
102
+ if (originalTitle) CCPetsPrintLine(stdout, PlainTitle(originalTitle));
103
+ }
104
+ return 0;
105
+ }
@@ -0,0 +1,17 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ @class CCPetsJSONObject;
4
+
5
+ // Missing or blank files have no configuration. Malformed nonblank files report an error.
6
+ CCPetsJSONObject *CCPetsReadAntigravityConfig(NSString *path, NSString **errorMessage);
7
+ NSArray<NSString *> *CCPetsInstallAntigravityPetHooks(NSDictionary *profile);
8
+ BOOL CCPetsRemoveAntigravityPetHooks(NSDictionary *profile);
9
+ BOOL CCPetsAntigravityPetHooksInstalled(NSDictionary *profile);
10
+ BOOL CCPetsAntigravityBridgeInstalled(NSDictionary *profile);
11
+ NSArray<NSString *> *CCPetsInstallAntigravityBridge(NSDictionary *profile, NSArray<NSString *> *allowed, BOOL registerMcp);
12
+ NSArray<NSString *> *CCPetsRemoveAntigravityBridge(NSDictionary *profile, BOOL unregisterMcp);
13
+ // Wrap or restore the terminal title command in this native CLI profile only.
14
+ void CCPetsConfigureAntigravityBridgeTitle(NSDictionary *profile, BOOL enabled);
15
+ // Desktop Sidecars are scoped to the default Gemini root. Custom CLI Homes are ignored.
16
+ NSArray<NSString *> *CCPetsConfigureAntigravityAppSidecar(NSDictionary *profile, BOOL enabled);
17
+ BOOL CCPetsAntigravityAppSidecarInstalled(NSDictionary *profile);
@@ -0,0 +1,462 @@
1
+ #import "CCPetsAntigravityConfig.h"
2
+ #import "CCPetsBridge.h"
3
+ #import "CCPetsBridgeSetup.h"
4
+ #import "CCPetsBridgeStore.h"
5
+ #import "CCPetsCLISupport.h"
6
+ #import "CCPetsJSON.h"
7
+ #import "CCPetsL10n.h"
8
+ #import "CCPetsProfiles.h"
9
+ #import <errno.h>
10
+ #import <sys/stat.h>
11
+ #import <unistd.h>
12
+
13
+ static NSString *const HookName = @"cc-pets-cli";
14
+ static NSString *const HookMarker = @"CC_PETS_AGY_HOOK=1 ";
15
+ static NSString *const MCPMarker = @"CC_PETS_AGY_MCP";
16
+ static NSString *const StatusLineMarker = @"CC_PETS_AGY_STATUS_LINE='";
17
+ static NSString *const BridgeTitleMarker = @"CC_PETS_AGY_BRIDGE_TITLE='";
18
+ static NSString *const AppSidecarName = @"cc-pets-bridge";
19
+ static NSString *const AppSidecarDescription = @"CC Pets Bridge delivery for Antigravity App";
20
+
21
+ static NSString *ProfileFile(NSDictionary *profile, NSString *relative) {
22
+ NSString *home = profile[@"home"];
23
+ if (![home isKindOfClass:NSString.class] || !home.isAbsolutePath) {
24
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"Invalid Antigravity profile home"];
25
+ }
26
+ return [home stringByAppendingPathComponent:relative];
27
+ }
28
+
29
+ static CCPetsJSONObject *Object(id value) {
30
+ return [value isKindOfClass:CCPetsJSONObject.class] ? value : nil;
31
+ }
32
+
33
+ CCPetsJSONObject *CCPetsReadAntigravityConfig(NSString *path, NSString **errorMessage) {
34
+ NSError *readError = nil;
35
+ NSData *data = [NSData dataWithContentsOfFile:path options:0 error:&readError];
36
+ if (!data) {
37
+ if (errorMessage && !([readError.domain isEqual:NSCocoaErrorDomain] &&
38
+ (readError.code == NSFileReadNoSuchFileError || readError.code == NSFileNoSuchFileError))) {
39
+ *errorMessage = readError.localizedDescription;
40
+ }
41
+ return nil;
42
+ }
43
+ const unsigned char *bytes = data.bytes;
44
+ NSUInteger index = 0;
45
+ while (index < data.length && (bytes[index] == ' ' || bytes[index] == '\t' ||
46
+ bytes[index] == '\r' || bytes[index] == '\n')) index++;
47
+ if (index == data.length) return nil;
48
+ id document = CCPetsJSONParse(data, errorMessage);
49
+ if (document && !Object(document)) {
50
+ if (errorMessage) *errorMessage = L(@"Expected a JSON object.");
51
+ return nil;
52
+ }
53
+ return document;
54
+ }
55
+
56
+ static CCPetsJSONObject *ReadConfig(NSString *path) {
57
+ NSString *error = nil;
58
+ CCPetsJSONObject *document = CCPetsReadAntigravityConfig(path, &error);
59
+ if (error) [NSException raise:@"CCPetsAntigravityConfig" format:@"%@: %@", path, error];
60
+ return document;
61
+ }
62
+
63
+ static BOOL UpdateConfig(NSString *path, BOOL (^update)(CCPetsJSONObject *)) {
64
+ NSString *lock = [@"agy-config-" stringByAppendingString:CCBridgeSHA1Hex(path)];
65
+ return [CCBridgeWithLock(lock, ^id {
66
+ CCPetsJSONObject *document = ReadConfig(path) ?: [CCPetsJSONObject new];
67
+ if (!update(document)) return @NO;
68
+ NSError *directoryError = nil;
69
+ if (![NSFileManager.defaultManager createDirectoryAtPath:path.stringByDeletingLastPathComponent
70
+ withIntermediateDirectories:YES attributes:@{NSFilePosixPermissions: @0700} error:&directoryError]) {
71
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@", directoryError.localizedDescription];
72
+ }
73
+ struct stat info;
74
+ mode_t mode = stat(path.fileSystemRepresentation, &info) == 0 ? info.st_mode & 0777 : 0600;
75
+ NSString *error = nil;
76
+ if (!CCPetsWriteFileAtomically(path, [CCPetsJSONStringify(document) stringByAppendingString:@"\n"], mode, &error)) {
77
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@", error];
78
+ }
79
+ return @YES;
80
+ }) boolValue];
81
+ }
82
+
83
+ static NSArray<NSString *> *Events(void) {
84
+ return @[@"SessionStart", @"PreInvocation", @"PreToolUse", @"PostToolUse", @"Stop"];
85
+ }
86
+
87
+ static NSString *ProfileEnvironment(NSDictionary *profile) {
88
+ return [NSString stringWithFormat:@"CC_PETS_PROFILE_ID=%@ CC_PETS_PROFILE_NAME=%@ CC_PETS_PROFILE_HOME=%@",
89
+ CCPetsShellQuote(profile[@"id"] ?: @""), CCPetsShellQuote(profile[@"displayName"] ?: @"Antigravity"),
90
+ CCPetsShellQuote(profile[@"home"])];
91
+ }
92
+
93
+ // The managed command carries its previous settings so repair/uninstall does not require a second file.
94
+ static CCPetsJSONObject *WrappedCommandBackup(id command, NSString *marker) {
95
+ if (![command isKindOfClass:NSString.class] || ![command hasPrefix:marker]) return nil;
96
+ NSString *suffix = [command substringFromIndex:marker.length];
97
+ NSRange end = [suffix rangeOfString:@"';"];
98
+ if (end.location == NSNotFound) return nil;
99
+ NSData *data = [[NSData alloc] initWithBase64EncodedString:[suffix substringToIndex:end.location] options:0];
100
+ return data ? Object(CCPetsJSONParse(data, NULL)) : nil;
101
+ }
102
+
103
+ static void ChangeStatusLine(NSDictionary *profile, BOOL install) {
104
+ UpdateConfig(ProfileFile(profile, @"antigravity-cli/settings.json"), ^BOOL(CCPetsJSONObject *document) {
105
+ CCPetsJSONObject *line = Object(document[@"statusLine"]);
106
+ // Let the CLI keep its existing behavior for an older or unrecognized form.
107
+ if (document[@"statusLine"] && !line) return NO;
108
+ id command = line[@"command"];
109
+ if (command && ![command isKindOfClass:NSString.class]) return NO;
110
+ CCPetsJSONObject *previous = WrappedCommandBackup(command, StatusLineMarker);
111
+ if (!install) {
112
+ if (!previous) return NO;
113
+ NSString *previousCommand = previous[@"command"];
114
+ if (previousCommand) line[@"command"] = previousCommand;
115
+ else [line removeObjectForKey:@"command"];
116
+ if (previousCommand.length == 0 && [line[@"stack_with_default"] isEqual:@YES]) {
117
+ if (previous[@"stack_with_default"]) line[@"stack_with_default"] = previous[@"stack_with_default"];
118
+ else [line removeObjectForKey:@"stack_with_default"];
119
+ }
120
+ if (line.keys.count == 0) [document removeObjectForKey:@"statusLine"];
121
+ return YES;
122
+ }
123
+ if ([command hasPrefix:StatusLineMarker] && !previous) return NO;
124
+ previous = previous ?: line ?: [CCPetsJSONObject new];
125
+ if (!line) line = [CCPetsJSONObject new];
126
+ NSString *oldCommand = previous[@"command"];
127
+ NSString *snapshot = [[CCPetsJSONStringify(previous) dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
128
+ NSString *collect = [NSString stringWithFormat:@"%@%@'; agy_status_input=$(/bin/cat); /usr/bin/printf '%%s' \"$agy_status_input\" | %@ %@ --agy-status-line >/dev/null 2>&1 || :",
129
+ StatusLineMarker, snapshot, ProfileEnvironment(profile), CCPetsShellQuote(CCPetsExecutablePath())];
130
+ if (oldCommand.length > 0) {
131
+ collect = [collect stringByAppendingFormat:@"; /usr/bin/printf '%%s' \"$agy_status_input\" | /bin/sh -c %@", CCPetsShellQuote(oldCommand)];
132
+ } else {
133
+ line[@"stack_with_default"] = @YES;
134
+ }
135
+ if ([line[@"command"] isEqual:collect]) return NO;
136
+ line[@"command"] = collect;
137
+ document[@"statusLine"] = line;
138
+ return YES;
139
+ });
140
+ }
141
+
142
+ static BOOL IsJSONBoolean(id value) {
143
+ return [value isKindOfClass:NSNumber.class] && CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID();
144
+ }
145
+
146
+ void CCPetsConfigureAntigravityBridgeTitle(NSDictionary *profile, BOOL enabled) {
147
+ UpdateConfig(ProfileFile(profile, @"antigravity-cli/settings.json"), ^BOOL(CCPetsJSONObject *document) {
148
+ id configured = document[@"title"];
149
+ CCPetsJSONObject *title = Object(configured);
150
+ if (configured && !title && !IsJSONBoolean(configured)) return NO;
151
+ id currentCommand = title[@"command"];
152
+ if (currentCommand && ![currentCommand isKindOfClass:NSString.class]) return NO;
153
+ CCPetsJSONObject *backup = WrappedCommandBackup(currentCommand, BridgeTitleMarker);
154
+ if (!enabled) {
155
+ if (!backup) return NO;
156
+ id original = backup[@"title"];
157
+ NSString *originalCommand = Object(original)[@"command"];
158
+ if (originalCommand) title[@"command"] = originalCommand;
159
+ else [title removeObjectForKey:@"command"];
160
+
161
+ if (IsJSONBoolean(original) && (title.keys.count == 0 ||
162
+ (title.keys.count == 1 && IsJSONBoolean(title[@"enabled"])))) {
163
+ // Keep a user's later /title toggle as well as the legacy boolean shape.
164
+ document[@"title"] = title[@"enabled"] ?: @YES;
165
+ } else if (!original && title.keys.count == 0) {
166
+ [document removeObjectForKey:@"title"];
167
+ }
168
+ return YES;
169
+ }
170
+
171
+ if ([currentCommand hasPrefix:BridgeTitleMarker] && !backup) return NO;
172
+ if (!backup) {
173
+ backup = [CCPetsJSONObject new];
174
+ if (configured) backup[@"title"] = configured;
175
+ }
176
+ if (!title) {
177
+ title = [CCPetsJSONObject new];
178
+ if (IsJSONBoolean(configured)) title[@"enabled"] = configured;
179
+ }
180
+ NSString *originalCommand = Object(backup[@"title"])[@"command"];
181
+ NSString *snapshot = [[CCPetsJSONStringify(backup) dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
182
+ // A sentinel keeps trailing newlines in the JSON while command substitution stores it.
183
+ NSMutableString *managed = [NSMutableString stringWithFormat:
184
+ @"%@%@'; agy_title_input=$(/bin/cat && /usr/bin/printf x) || exit $?; agy_title_input=${agy_title_input%%x}; ",
185
+ BridgeTitleMarker, snapshot];
186
+ NSString *render = [NSString stringWithFormat:
187
+ @"/usr/bin/printf '%%s' \"$agy_title_input\" | %@ %@ --agy-bridge-title",
188
+ ProfileEnvironment(profile), CCPetsShellQuote(CCPetsExecutablePath())];
189
+ if (originalCommand.length > 0) {
190
+ [managed appendFormat:@"agy_title_original=$(/usr/bin/printf '%%s' \"$agy_title_input\" | /bin/sh -c %@) || exit $?; ",
191
+ CCPetsShellQuote(originalCommand)];
192
+ [managed appendFormat:@"%@ \"$agy_title_original\" || /usr/bin/printf '%%s\\n' \"$agy_title_original\"", render];
193
+ } else {
194
+ [managed appendString:render];
195
+ }
196
+ if ([currentCommand isEqual:managed]) return NO;
197
+ title[@"command"] = managed;
198
+ document[@"title"] = title;
199
+ return YES;
200
+ });
201
+ }
202
+
203
+ static BOOL IsToolEvent(NSString *event) {
204
+ return [event isEqual:@"PreToolUse"] || [event isEqual:@"PostToolUse"];
205
+ }
206
+
207
+ static BOOL HasHook(id candidate, NSString *event) {
208
+ id entries = Object(candidate)[event];
209
+ if (![entries isKindOfClass:NSArray.class]) return NO;
210
+ for (id entry in entries) {
211
+ NSArray *handlers = IsToolEvent(event) ? Object(entry)[@"hooks"] : @[entry];
212
+ if (![handlers isKindOfClass:NSArray.class]) continue;
213
+ for (id handler in handlers) {
214
+ id command = Object(handler)[@"command"];
215
+ if ([command isKindOfClass:NSString.class] && [command hasPrefix:HookMarker]) return YES;
216
+ }
217
+ }
218
+ return NO;
219
+ }
220
+
221
+ static BOOL OwnsHook(id candidate) {
222
+ for (NSString *event in Events()) if (HasHook(candidate, event)) return YES;
223
+ return NO;
224
+ }
225
+
226
+ BOOL CCPetsAntigravityPetHooksInstalled(NSDictionary *profile) {
227
+ @try {
228
+ CCPetsJSONObject *hook = Object(ReadConfig(ProfileFile(profile, @"config/hooks.json"))[HookName]);
229
+ if ([hook[@"enabled"] isEqual:@NO]) return NO;
230
+ for (NSString *event in Events()) if (!HasHook(hook, event)) return NO;
231
+ return YES;
232
+ } @catch (NSException *exception) { return NO; }
233
+ }
234
+
235
+ NSArray<NSString *> *CCPetsInstallAntigravityPetHooks(NSDictionary *profile) {
236
+ NSString *path = ProfileFile(profile, @"config/hooks.json");
237
+ UpdateConfig(path, ^BOOL(CCPetsJSONObject *document) {
238
+ if (document[HookName] && !OwnsHook(document[HookName])) {
239
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@: %@", path, L(@"The Antigravity hook name is already in use.")];
240
+ }
241
+ NSMutableDictionary *hook = [NSMutableDictionary dictionary];
242
+ for (NSString *event in Events()) {
243
+ NSString *command = [NSString stringWithFormat:@"%@%@ %@ --agy-hook %@",
244
+ HookMarker, ProfileEnvironment(profile), CCPetsShellQuote(CCPetsExecutablePath()), event];
245
+ NSArray *handlers = @[@{@"type": @"command", @"command": command, @"timeout": @10}];
246
+ hook[event] = IsToolEvent(event) ? @[@{@"matcher": @"*", @"hooks": handlers}] : handlers;
247
+ }
248
+ document[HookName] = hook;
249
+ return YES;
250
+ });
251
+ ChangeStatusLine(profile, YES);
252
+ return @[CCPetsInterpolate(L(@"Antigravity CLI hooks configured in {path}."), @{@"path": path})];
253
+ }
254
+
255
+ BOOL CCPetsRemoveAntigravityPetHooks(NSDictionary *profile) {
256
+ UpdateConfig(ProfileFile(profile, @"config/hooks.json"), ^BOOL(CCPetsJSONObject *document) {
257
+ if (!OwnsHook(document[HookName])) return NO;
258
+ [document removeObjectForKey:HookName];
259
+ return YES;
260
+ });
261
+ ChangeStatusLine(profile, NO);
262
+ return YES;
263
+ }
264
+
265
+ static BOOL OwnsMCP(id candidate) {
266
+ CCPetsJSONObject *server = Object(candidate);
267
+ return [Object(server[@"env"])[MCPMarker] isEqual:@"1"] && [server[@"args"] isEqual:@[@"bridge", @"mcp"]];
268
+ }
269
+
270
+ BOOL CCPetsAntigravityBridgeInstalled(NSDictionary *profile) {
271
+ @try {
272
+ CCPetsJSONObject *mcp = ReadConfig(ProfileFile(profile, @"config/mcp_config.json"));
273
+ CCPetsJSONObject *server = Object(Object(mcp[@"mcpServers"])[CCBridgeMCPName]);
274
+ NSString *command = server[@"command"];
275
+ return CCPetsAntigravityPetHooksInstalled(profile) && OwnsMCP(server) &&
276
+ [command isKindOfClass:NSString.class] && command.isAbsolutePath &&
277
+ [NSFileManager.defaultManager isExecutableFileAtPath:command];
278
+ } @catch (NSException *exception) { return NO; }
279
+ }
280
+
281
+ static BOOL ChangeMCP(NSDictionary *profile, BOOL enabled) {
282
+ NSString *path = ProfileFile(profile, @"config/mcp_config.json");
283
+ return UpdateConfig(path, ^BOOL(CCPetsJSONObject *document) {
284
+ CCPetsJSONObject *servers = Object(document[@"mcpServers"]);
285
+ id current = servers[CCBridgeMCPName];
286
+ if (!enabled) {
287
+ if (!OwnsMCP(current)) return NO;
288
+ [servers removeObjectForKey:CCBridgeMCPName];
289
+ if (servers.keys.count == 0) [document removeObjectForKey:@"mcpServers"];
290
+ return YES;
291
+ }
292
+ if ((document[@"mcpServers"] && !servers) || (current && !OwnsMCP(current))) {
293
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@: %@", path, L(@"The Antigravity MCP server name is already in use.")];
294
+ }
295
+ if (!servers) servers = [CCPetsJSONObject new];
296
+ CCPetsJSONObject *server = Object(current) ?: [CCPetsJSONObject new];
297
+ CCPetsJSONObject *environment = Object(server[@"env"]) ?: [CCPetsJSONObject new];
298
+ NSString *command = CCPetsExecutablePath();
299
+ BOOL changed = ![server[@"command"] isEqual:command] || !OwnsMCP(server);
300
+ for (NSString *key in @[@"CC_PETS_HOME", @"CC_PETS_STATE_DIR"]) {
301
+ NSString *value = CCPetsEnvironmentValue(key);
302
+ id old = environment[key];
303
+ if (!(old == value || [old isEqual:value])) changed = YES;
304
+ if (value) environment[key] = value;
305
+ else [environment removeObjectForKey:key];
306
+ }
307
+ if (!changed) return NO;
308
+ environment[MCPMarker] = @"1";
309
+ server[@"command"] = command;
310
+ server[@"args"] = @[@"bridge", @"mcp"];
311
+ server[@"env"] = environment;
312
+ servers[CCBridgeMCPName] = server;
313
+ document[@"mcpServers"] = servers;
314
+ return YES;
315
+ });
316
+ }
317
+
318
+ static NSString *AllowRule(NSString *tool) {
319
+ return [NSString stringWithFormat:@"mcp(%@/%@)", CCBridgeMCPName, tool];
320
+ }
321
+
322
+ static void SetAllows(NSDictionary *profile, NSArray<NSString *> *allowed) {
323
+ UpdateConfig(ProfileFile(profile, @"antigravity-cli/settings.json"), ^BOOL(CCPetsJSONObject *document) {
324
+ CCPetsJSONObject *permissions = Object(document[@"permissions"]);
325
+ NSArray *current = permissions[@"allow"];
326
+ if ((document[@"permissions"] && !permissions) || (current && ![current isKindOfClass:NSArray.class])) {
327
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@", L(@"Expected an Antigravity permissions object with an allow array.")];
328
+ }
329
+ NSMutableSet *managed = [NSMutableSet set];
330
+ for (NSString *group in CCBridgeToolGroupNames()) {
331
+ for (NSString *tool in CCBridgeToolGroup(group)) [managed addObject:AllowRule(tool)];
332
+ }
333
+ NSMutableArray *updated = [NSMutableArray array];
334
+ for (id rule in current) if (![managed containsObject:rule]) [updated addObject:rule];
335
+ for (NSString *tool in allowed) [updated addObject:AllowRule(tool)];
336
+ if ([updated isEqualToArray:current ?: @[]]) return NO;
337
+ if (!permissions) permissions = [CCPetsJSONObject new];
338
+ if (updated.count > 0) permissions[@"allow"] = updated;
339
+ else [permissions removeObjectForKey:@"allow"];
340
+ if (permissions.keys.count > 0) document[@"permissions"] = permissions;
341
+ else [document removeObjectForKey:@"permissions"];
342
+ return YES;
343
+ });
344
+ }
345
+
346
+ NSArray<NSString *> *CCPetsInstallAntigravityBridge(NSDictionary *profile, NSArray<NSString *> *allowed, BOOL registerMcp) {
347
+ NSMutableArray *report = [CCPetsInstallAntigravityPetHooks(profile) mutableCopy];
348
+ if (registerMcp && ChangeMCP(profile, YES)) [report addObject:L(@"Registered CC Bridge MCP for Antigravity CLI.")];
349
+ SetAllows(profile, allowed);
350
+ return report;
351
+ }
352
+
353
+ NSArray<NSString *> *CCPetsRemoveAntigravityBridge(NSDictionary *profile, BOOL unregisterMcp) {
354
+ SetAllows(profile, @[]);
355
+ return unregisterMcp && ChangeMCP(profile, NO) ? @[L(@"Removed CC Bridge MCP from Antigravity CLI.")] : @[];
356
+ }
357
+
358
+ static BOOL IsAntigravityAppProfile(NSDictionary *profile) {
359
+ return [profile[@"provider"] isEqual:CCPetsProfileProviderAntigravity] && CCPetsProfileIsOfficialDefault(profile);
360
+ }
361
+
362
+ static NSString *AppSidecarManifestPath(NSDictionary *profile) {
363
+ return ProfileFile(profile, [NSString stringWithFormat:@"config/sidecars/%@/sidecar.json", AppSidecarName]);
364
+ }
365
+
366
+ static NSArray<NSString *> *AppSidecarArguments(void) {
367
+ return @[@"bridge", @"agy-app-sidecar"];
368
+ }
369
+
370
+ static BOOL OwnsAppSidecar(CCPetsJSONObject *manifest) {
371
+ return [manifest[@"description"] isEqual:AppSidecarDescription] &&
372
+ [manifest[@"args"] isEqual:AppSidecarArguments()];
373
+ }
374
+
375
+ BOOL CCPetsAntigravityAppSidecarInstalled(NSDictionary *profile) {
376
+ if (!IsAntigravityAppProfile(profile)) return NO;
377
+ @try {
378
+ CCPetsJSONObject *manifest = ReadConfig(AppSidecarManifestPath(profile));
379
+ NSString *command = manifest[@"command"];
380
+ CCPetsJSONObject *mcp = ReadConfig(ProfileFile(profile, @"config/mcp_config.json"));
381
+ CCPetsJSONObject *server = Object(Object(mcp[@"mcpServers"])[CCBridgeMCPName]);
382
+ CCPetsJSONObject *config = ReadConfig(ProfileFile(profile, @"config/config.json"));
383
+ CCPetsJSONObject *state = Object(Object(config[@"sidecars"])[AppSidecarName]);
384
+ return OwnsAppSidecar(manifest) && [command isKindOfClass:NSString.class] && command.isAbsolutePath &&
385
+ [NSFileManager.defaultManager isExecutableFileAtPath:command] &&
386
+ OwnsMCP(server) && [server[@"command"] isEqual:command] &&
387
+ [manifest[@"restart_policy"] isEqual:@"on-failure"] && [state[@"enabled"] isEqual:@YES];
388
+ } @catch (NSException *exception) { return NO; }
389
+ }
390
+
391
+ static void FailSidecarCollision(NSString *path) {
392
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@: %@", path, L(@"The Antigravity sidecar name is already in use.")];
393
+ }
394
+
395
+ NSArray<NSString *> *CCPetsConfigureAntigravityAppSidecar(NSDictionary *profile, BOOL enabled) {
396
+ if (!IsAntigravityAppProfile(profile)) return @[];
397
+ NSString *manifestPath = AppSidecarManifestPath(profile);
398
+ NSString *configPath = ProfileFile(profile, @"config/config.json");
399
+ NSString *lock = [@"agy-app-sidecar-" stringByAppendingString:CCBridgeSHA1Hex(manifestPath)];
400
+ return CCBridgeWithLock(lock, ^id {
401
+ NSString *error = nil;
402
+ CCPetsJSONObject *manifest = CCPetsReadAntigravityConfig(manifestPath, &error);
403
+ BOOL owned = OwnsAppSidecar(manifest);
404
+ if (!enabled && !owned) return @[];
405
+ if (error) [NSException raise:@"CCPetsAntigravityConfig" format:@"%@: %@", manifestPath, error];
406
+
407
+ CCPetsJSONObject *config = ReadConfig(configPath);
408
+ CCPetsJSONObject *sidecars = Object(config[@"sidecars"]);
409
+ id state = sidecars[AppSidecarName];
410
+ if (enabled) {
411
+ struct stat info;
412
+ if (!owned && lstat(manifestPath.fileSystemRepresentation, &info) == 0) FailSidecarCollision(manifestPath);
413
+ if ((state && (!owned || !Object(state))) || (config[@"sidecars"] && !sidecars)) FailSidecarCollision(configPath);
414
+
415
+ UpdateConfig(manifestPath, ^BOOL(CCPetsJSONObject *document) {
416
+ if (document.keys.count > 0 && !OwnsAppSidecar(document)) FailSidecarCollision(manifestPath);
417
+ NSMutableDictionary *environment = [NSMutableDictionary dictionary];
418
+ for (NSString *key in @[@"CC_PETS_HOME", @"CC_PETS_STATE_DIR"]) {
419
+ NSString *value = CCPetsEnvironmentValue(key);
420
+ if (value) environment[key] = value;
421
+ }
422
+ document[@"display_name"] = @"CC Pets Bridge";
423
+ document[@"description"] = AppSidecarDescription;
424
+ document[@"command"] = CCPetsExecutablePath();
425
+ document[@"args"] = AppSidecarArguments();
426
+ document[@"restart_policy"] = @"on-failure";
427
+ if (environment.count > 0) document[@"env"] = environment;
428
+ else [document removeObjectForKey:@"env"];
429
+ return YES;
430
+ });
431
+ }
432
+
433
+ UpdateConfig(configPath, ^BOOL(CCPetsJSONObject *document) {
434
+ CCPetsJSONObject *entries = Object(document[@"sidecars"]);
435
+ if (!enabled) {
436
+ if (!entries[AppSidecarName]) return NO;
437
+ [entries removeObjectForKey:AppSidecarName];
438
+ if (entries.keys.count == 0) [document removeObjectForKey:@"sidecars"];
439
+ return YES;
440
+ }
441
+ id existing = entries[AppSidecarName];
442
+ CCPetsJSONObject *entry = Object(existing);
443
+ if ((document[@"sidecars"] && !entries) || (existing && !entry)) FailSidecarCollision(configPath);
444
+ if ([entry[@"enabled"] isEqual:@YES]) return NO;
445
+ entries = entries ?: [CCPetsJSONObject new];
446
+ entry = entry ?: [CCPetsJSONObject new];
447
+ entry[@"enabled"] = @YES;
448
+ entries[AppSidecarName] = entry;
449
+ document[@"sidecars"] = entries;
450
+ return YES;
451
+ });
452
+
453
+ if (!enabled) {
454
+ if (unlink(manifestPath.fileSystemRepresentation) != 0 && errno != ENOENT) {
455
+ NSError *removeError = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
456
+ [NSException raise:@"CCPetsAntigravityConfig" format:@"%@: %@", manifestPath, removeError.localizedDescription];
457
+ }
458
+ rmdir(manifestPath.stringByDeletingLastPathComponent.fileSystemRepresentation);
459
+ }
460
+ return @[enabled ? L(@"Antigravity App Sidecar enabled.") : L(@"Antigravity App Sidecar disabled.")];
461
+ });
462
+ }
@@ -0,0 +1,5 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ int CCPetsRecordAntigravityHook(NSString *hookEvent);
4
+ int CCPetsRecordAntigravityStatusLine(void);
5
+ NSString *CCPetsRegisterAntigravityTerminalClient(pid_t pid);