cc-pets 2.5.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 +47 -5
- package/CC_BRIDGE.zh-CN.md +42 -5
- package/CONTRIBUTING.md +1 -1
- package/CONTRIBUTING.zh-CN.md +1 -1
- package/README.md +48 -19
- package/README.zh-CN.md +46 -17
- package/Resources/AgentIcons/antigravity.icns +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +64 -15
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +7 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +11 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +16 -5
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +11 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +21 -11
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +27 -16
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +43 -7
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +2 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +3 -3
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +20 -22
- package/Sources/CCPets/App/CCPetsAppDelegate.m +14 -6
- 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 +7 -5
- 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 +172 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +25 -7
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +17 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +7 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +92 -11
- 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/CCPetsIntegrations.h +1 -1
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +37 -7
- 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 +5 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +163 -40
- package/Sources/CCPets/Pet/CCPetsPreviews.m +16 -8
- package/Sources/CCPets/Pet/PetView+Menu.m +56 -5
- 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 +4 -1
- package/Sources/CCPets/Support/CCPetsPreferences.m +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +10 -5
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +9 -6
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +3 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +189 -8
- 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 +1 -1
- 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
|
@@ -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,180 @@
|
|
|
1
|
+
#import "CCPetsAntigravityEvents.h"
|
|
2
|
+
#import "CCPetsBridgeCLI.h"
|
|
3
|
+
#import "CCPetsBridgeStore.h"
|
|
4
|
+
#import "CCPetsCLISupport.h"
|
|
5
|
+
#import "CCPetsEvents.h"
|
|
6
|
+
#import "CCPetsPaths.h"
|
|
7
|
+
#import "CCPetsProfiles.h"
|
|
8
|
+
#import "CCPetsTerminalFocus.h"
|
|
9
|
+
#import <sys/stat.h>
|
|
10
|
+
#import <unistd.h>
|
|
11
|
+
|
|
12
|
+
static NSString *StringValue(id value) {
|
|
13
|
+
return [value isKindOfClass:NSString.class] ? value : nil;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static NSString *ToolCategory(id value) {
|
|
17
|
+
NSString *tool = SanitizedShortString(value, 64);
|
|
18
|
+
if ([tool hasPrefix:@"browser_"] || [tool isEqual:@"search_web"] || [tool isEqual:@"read_url_content"]) return @"web";
|
|
19
|
+
if ([tool hasPrefix:@"mcp_"] || [tool isEqual:@"call_mcp_tool"]) return @"mcp";
|
|
20
|
+
return @{
|
|
21
|
+
@"run_command": @"shell", @"send_command_input": @"shell",
|
|
22
|
+
@"write_to_file": @"write", @"replace_file_content": @"edit", @"multi_replace_file_content": @"edit", @"sed_file": @"edit",
|
|
23
|
+
@"view_file": @"read", @"list_dir": @"read", @"view_file_outline": @"read",
|
|
24
|
+
@"find_by_name": @"search", @"grep_search": @"search",
|
|
25
|
+
@"invoke_subagent": @"agent", @"manage_subagents": @"agent"
|
|
26
|
+
}[tool] ?: tool;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
NSString *CCPetsRegisterAntigravityTerminalClient(pid_t pid) {
|
|
30
|
+
NSString *tty = StringValue(TerminalFocusTargetFromEnvironment()[@"tty"]);
|
|
31
|
+
if (tty.length == 0) return nil;
|
|
32
|
+
NSString *directory = [PetStateDirectory() stringByAppendingPathComponent:
|
|
33
|
+
[NSString stringWithFormat:@"cc-pets-%u-clients", getuid()]];
|
|
34
|
+
[NSFileManager.defaultManager createDirectoryAtPath:directory withIntermediateDirectories:YES
|
|
35
|
+
attributes:@{NSFilePosixPermissions: @0700} error:nil];
|
|
36
|
+
struct stat info;
|
|
37
|
+
if (lstat(directory.fileSystemRepresentation, &info) != 0 || !S_ISDIR(info.st_mode) || info.st_uid != getuid()) return nil;
|
|
38
|
+
NSString *path = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", pid]];
|
|
39
|
+
NSDictionary *profile = CCPetsProfileForHome(CCPetsProfileProviderAntigravity, DefaultAntigravityConfigDirectory());
|
|
40
|
+
NSString *contents = [NSString stringWithFormat:@"%@\n%@\n%@\n", CCBridgeAntigravityProvider, tty,
|
|
41
|
+
NormalizedAgentProfileID(profile[@"id"])];
|
|
42
|
+
return CCPetsWriteFileAtomically(path, contents, 0600, NULL) ? path : nil;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// App 与 CLI 共用 Gemini 根目录里的 Hooks,靠祖先进程区分来源。App 会话没有终端,
|
|
46
|
+
// 回跳目标是 App 本身;标题用 Provider 名,避免和同一账号的 CLI 会话混在一起。
|
|
47
|
+
static BOOL AppendAntigravityEvent(NSString *session, NSString *cwd, NSString *event, NSString *tool, BOOL failed,
|
|
48
|
+
NSString *state, BOOL app) {
|
|
49
|
+
NSString *identifier = CCBridgeSafeSessionId(session);
|
|
50
|
+
if (!identifier) return NO;
|
|
51
|
+
NSMutableDictionary *record = [@{
|
|
52
|
+
@"event": event,
|
|
53
|
+
@"tool": ToolCategory(tool),
|
|
54
|
+
@"failed": @(failed),
|
|
55
|
+
@"provider": app ? CCBridgeAntigravityAppProvider : CCBridgeAntigravityProvider,
|
|
56
|
+
@"state": state ?: NormalizedStateForEvent(event, failed),
|
|
57
|
+
@"timestamp": @(NSDate.date.timeIntervalSince1970),
|
|
58
|
+
@"session": identifier
|
|
59
|
+
} mutableCopy];
|
|
60
|
+
NSString *directory = SanitizedWorkingDirectory(cwd);
|
|
61
|
+
if (directory.length > 0) record[@"cwd"] = directory;
|
|
62
|
+
if (app) {
|
|
63
|
+
NSDictionary *profile = CCPetsProfileForHome(CCPetsProfileProviderAntigravity, DefaultAntigravityAppConfigDirectory());
|
|
64
|
+
NSString *profileID = NormalizedAgentProfileID(profile[@"id"]);
|
|
65
|
+
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
66
|
+
record[@"terminal"] = @{@"bundleID": CCPetsAntigravityAppBundleIdentifier};
|
|
67
|
+
return AppendAgentEventRecord(record);
|
|
68
|
+
}
|
|
69
|
+
NSString *profileID = NormalizedAgentProfileID(CCPetsEnvironmentValue(@"CC_PETS_PROFILE_ID"));
|
|
70
|
+
NSString *profileName = SanitizedShortString(CCPetsEnvironmentValue(@"CC_PETS_PROFILE_NAME"), 64);
|
|
71
|
+
if (![profileID hasPrefix:@"antigravity:"]) {
|
|
72
|
+
NSDictionary *profile = CCPetsProfileForHome(CCPetsProfileProviderAntigravity, DefaultAntigravityConfigDirectory());
|
|
73
|
+
profileID = NormalizedAgentProfileID(profile[@"id"]);
|
|
74
|
+
profileName = SanitizedShortString(profile[@"displayName"], 64);
|
|
75
|
+
}
|
|
76
|
+
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
77
|
+
if (profileName.length > 0) record[@"profileName"] = profileName;
|
|
78
|
+
NSDictionary *terminal = TerminalFocusTargetFromEnvironment();
|
|
79
|
+
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
80
|
+
return AppendAgentEventRecord(record);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static void AppendHookEvent(NSString *hookEvent, NSDictionary *payload, BOOL app) {
|
|
84
|
+
NSString *event = @{
|
|
85
|
+
@"SessionStart": @"SessionStart", @"PreInvocation": @"UserPromptSubmit", @"PreToolUse": @"PreToolUse",
|
|
86
|
+
@"PostToolUse": @"PostToolUse", @"Stop": @"Stop"
|
|
87
|
+
}[hookEvent];
|
|
88
|
+
if (!event) return;
|
|
89
|
+
BOOL failed = StringValue(payload[@"error"]).length > 0;
|
|
90
|
+
if ([hookEvent isEqual:@"Stop"]) {
|
|
91
|
+
if ([payload[@"fullyIdle"] isEqual:@NO]) return;
|
|
92
|
+
NSString *reason = StringValue(payload[@"terminationReason"]);
|
|
93
|
+
failed = failed || [reason isEqual:@"error"] || [reason isEqual:@"max_steps_exceeded"];
|
|
94
|
+
if (failed) event = @"StopFailure";
|
|
95
|
+
}
|
|
96
|
+
if ([hookEvent isEqual:@"PostToolUse"] && failed) event = @"PostToolUseFailure";
|
|
97
|
+
NSArray *workspaces = [payload[@"workspacePaths"] isKindOfClass:NSArray.class] ? payload[@"workspacePaths"] : nil;
|
|
98
|
+
NSString *cwd = StringValue(payload[@"cwd"]) ?: StringValue(workspaces.firstObject);
|
|
99
|
+
NSDictionary *tool = [payload[@"toolCall"] isKindOfClass:NSDictionary.class] ? payload[@"toolCall"] : nil;
|
|
100
|
+
AppendAntigravityEvent(StringValue(payload[@"conversationId"]), cwd, event, StringValue(tool[@"name"]), failed, nil, app);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static NSDictionary *ReadPayload(void) {
|
|
104
|
+
NSData *input = [NSFileHandle.fileHandleWithStandardInput readDataToEndOfFile];
|
|
105
|
+
id payload = input.length > 0 ? [NSJSONSerialization JSONObjectWithData:input options:0 error:nil] : nil;
|
|
106
|
+
return [payload isKindOfClass:NSDictionary.class] ? payload : nil;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
int CCPetsRecordAntigravityHook(NSString *hookEvent) {
|
|
110
|
+
NSDictionary *payload = ReadPayload();
|
|
111
|
+
NSDictionary *response = [hookEvent isEqual:@"PreToolUse"] ? @{@"decision": @"allow"}
|
|
112
|
+
: ([hookEvent isEqual:@"Stop"] ? @{@"decision": @"stop"} : @{});
|
|
113
|
+
BOOL written = NO;
|
|
114
|
+
// CLI 与 App 共用这一个 Hook:两边都上报会话状态,Provider 按祖先进程区分。
|
|
115
|
+
@try {
|
|
116
|
+
@try {
|
|
117
|
+
NSDictionary *ancestor = CCBridgeFindAgentAncestor(getppid());
|
|
118
|
+
BOOL cli = [ancestor[@"provider"] isEqual:CCBridgeAntigravityProvider];
|
|
119
|
+
BOOL app = [ancestor[@"provider"] isEqual:CCBridgeAntigravityAppProvider];
|
|
120
|
+
if (payload && (cli || app)) {
|
|
121
|
+
AppendHookEvent(hookEvent, payload, app);
|
|
122
|
+
response = CCBridgeHandleAntigravityHook(hookEvent, payload) ?: response;
|
|
123
|
+
}
|
|
124
|
+
} @catch (NSException *exception) {
|
|
125
|
+
// A monitoring failure must not block the user's Antigravity turn.
|
|
126
|
+
}
|
|
127
|
+
NSData *json = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil];
|
|
128
|
+
if (json) {
|
|
129
|
+
written = fwrite(json.bytes, 1, json.length, stdout) == json.length && fputc('\n', stdout) != EOF;
|
|
130
|
+
written = fflush(stdout) == 0 && written && !ferror(stdout);
|
|
131
|
+
}
|
|
132
|
+
} @finally {
|
|
133
|
+
CCBridgeFinishAntigravityHook(written);
|
|
134
|
+
}
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
int CCPetsRecordAntigravityStatusLine(void) {
|
|
139
|
+
@try {
|
|
140
|
+
NSDictionary *payload = ReadPayload();
|
|
141
|
+
NSString *session = CCBridgeSafeSessionId(payload[@"conversation_id"]) ?: CCBridgeSafeSessionId(payload[@"session_id"]);
|
|
142
|
+
NSString *state = StringValue(payload[@"agent_state"]);
|
|
143
|
+
id confirmation = payload[@"tool_confirmation_pending"];
|
|
144
|
+
if (!session || (!state.length && ![confirmation isKindOfClass:NSNumber.class])) return 0;
|
|
145
|
+
BOOL pending = [confirmation isEqual:@YES] || [state isEqual:@"reviewing"];
|
|
146
|
+
NSString *file = [CCBridgeSubdirectory(@[]) stringByAppendingPathComponent:@"agy-approvals.json"];
|
|
147
|
+
if (!pending && ![NSFileManager.defaultManager fileExistsAtPath:file]) return 0;
|
|
148
|
+
|
|
149
|
+
NSDictionary *ancestor = CCBridgeFindAgentAncestor(getppid());
|
|
150
|
+
if (![ancestor[@"provider"] isEqual:CCBridgeAntigravityProvider]) return 0;
|
|
151
|
+
NSNumber *owner = ancestor[@"pid"];
|
|
152
|
+
NSString *lock = CCBridgeAcquireLock(@"agy-approvals");
|
|
153
|
+
if (!lock) return 0;
|
|
154
|
+
@try {
|
|
155
|
+
id snapshot = CCBridgeReadJSON(file);
|
|
156
|
+
NSMutableDictionary *approvals = [snapshot isKindOfClass:NSDictionary.class] ? [snapshot mutableCopy]
|
|
157
|
+
: [NSMutableDictionary dictionary];
|
|
158
|
+
for (NSString *identifier in approvals.allKeys) {
|
|
159
|
+
id pid = approvals[identifier];
|
|
160
|
+
if (![pid isKindOfClass:NSNumber.class] || !CCBridgeProcessAlive([pid intValue])) {
|
|
161
|
+
[approvals removeObjectForKey:identifier];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
BOOL wasPending = [approvals[session] isEqual:owner];
|
|
165
|
+
if (pending != wasPending) {
|
|
166
|
+
NSString *event = pending ? @"PermissionRequest" : @"ApprovalResolved";
|
|
167
|
+
NSString *eventState = pending ? @"approval" : ([state isEqual:@"idle"] ? @"idle" : @"thinking");
|
|
168
|
+
if (AppendAntigravityEvent(session, StringValue(payload[@"cwd"]), event, nil, NO, eventState, NO)) {
|
|
169
|
+
if (pending) approvals[session] = owner;
|
|
170
|
+
else [approvals removeObjectForKey:session];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (approvals.count == 0) unlink(file.fileSystemRepresentation);
|
|
174
|
+
else if (![approvals isEqual:snapshot]) CCBridgeWriteJSON(file, approvals);
|
|
175
|
+
} @finally {
|
|
176
|
+
CCBridgeReleaseLock(lock);
|
|
177
|
+
}
|
|
178
|
+
} @catch (NSException *exception) {}
|
|
179
|
+
return 0;
|
|
180
|
+
}
|