cc-pets 2.2.1 → 2.4.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 +24 -2
- package/CC_BRIDGE.zh-CN.md +19 -2
- package/CHANGELOG.md +111 -0
- package/CHANGELOG.zh-CN.md +111 -0
- package/CONTRIBUTING.md +35 -10
- package/CONTRIBUTING.zh-CN.md +28 -10
- package/README.md +212 -32
- package/README.zh-CN.md +167 -36
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +699 -20
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +90 -25
- package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
- package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +17 -3
- package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +71 -23
- package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
- package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
- package/Sources/CCPets/{CCPetsAppDelegate+Settings.m → App/CCPetsAppDelegate+Settings.m} +87 -102
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +121 -51
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +17 -2
- package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +107 -11
- package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
- package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
- package/Sources/CCPets/{main.m → App/main.m} +74 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +43 -23
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +832 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +59 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +576 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +507 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +106 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +897 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
- package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
- package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +2 -1
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +148 -36
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
- package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
- package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +11 -2
- package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +19 -2
- package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +4 -1
- package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
- package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
- package/Sources/CCPets/Pet/PetView+Private.h +81 -0
- package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
- package/Sources/CCPets/Pet/PetView.m +635 -0
- package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
- package/Sources/CCPets/Support/CCPetsDoctor.m +646 -0
- package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
- package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
- package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
- package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
- package/Sources/CCPets/Support/CCPetsPreferences.h +33 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +117 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +44 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +279 -0
- package/Sources/CCPets/UI/CCPetsGlassView.h +57 -0
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +60 -11
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +64 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +1412 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +135 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +865 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +288 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +712 -0
- package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
- package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
- package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
- package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
- package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
- package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
- package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
- package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +97 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +323 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +58 -0
- package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
- package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
- package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +174 -19
- package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +598 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +713 -0
- package/THIRD_PARTY_NOTICES.md +8 -4
- package/THIRD_PARTY_NOTICES.zh-CN.md +6 -2
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +12 -2
- package/bin/codex-with-pet +15 -5
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +31 -5
- package/scripts/compile-cached.zsh +133 -0
- package/scripts/i18n.zsh +9 -52
- package/scripts/install-app.sh +4 -0
- package/scripts/install-shell-integration.sh +17 -10
- package/scripts/uninstall-shell-integration.sh +6 -1
- package/Sources/CCPets/CCPetsEvents.h +0 -10
- package/Sources/CCPets/CCPetsGlassView.h +0 -31
- package/Sources/CCPets/CCPetsUsage.h +0 -36
- package/Sources/CCPets/CCPetsUsage.m +0 -2069
- package/Sources/CCPets/PetView.m +0 -1839
- package/Sources/CCPets/QuotaDashboardView.m +0 -1145
- package/scripts/bridge/cli.mjs +0 -237
- package/scripts/bridge/core.mjs +0 -228
- package/scripts/bridge/hook.mjs +0 -150
- package/scripts/bridge/install.mjs +0 -311
- package/scripts/bridge/mcp-server.mjs +0 -308
- package/scripts/bridge/options.mjs +0 -87
- package/scripts/bridge/process.mjs +0 -151
- package/scripts/bridge/reservations.mjs +0 -248
- package/scripts/bridge/store.mjs +0 -433
- package/scripts/bridge/watch.mjs +0 -69
- package/scripts/check-l10n.mjs +0 -168
- package/scripts/configure-updater.mjs +0 -43
- package/scripts/detect-cli.mjs +0 -68
- package/scripts/doctor.mjs +0 -470
- package/scripts/i18n.mjs +0 -122
- package/scripts/install-claude-hooks.mjs +0 -253
- package/scripts/install-codex-hooks.mjs +0 -78
- package/scripts/integration-markers.mjs +0 -51
- package/scripts/locales/zh-Hans.json +0 -366
- package/scripts/pet-store.mjs +0 -415
- package/scripts/uninstall-integrations.mjs +0 -205
- /package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
- /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.h → Pet/CCPetsImageLoader.h} +0 -0
- /package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.h → Pet/CCPetsPetSize.h} +0 -0
- /package/Sources/CCPets/{CCPetsPetSize.m → Pet/CCPetsPetSize.m} +0 -0
- /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
- /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
- /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
- /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
- /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
- /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
- /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
- /package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +0 -0
- /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
- /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.h → UI/MenuPetSizeView.h} +0 -0
- /package/Sources/CCPets/{MenuPetSizeView.m → UI/MenuPetSizeView.m} +0 -0
- /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
#import "CCPetsIDEIntegration.h"
|
|
2
|
+
#import "CCPetsEvents.h"
|
|
3
|
+
#import "CCPetsL10n.h"
|
|
4
|
+
#import "CCPetsVSCodeIntegration.h"
|
|
5
|
+
#import <sys/stat.h>
|
|
6
|
+
#import <sys/time.h>
|
|
7
|
+
#import <unistd.h>
|
|
8
|
+
|
|
9
|
+
static NSString *const IDEPluginID = @"com.ccpets.terminal";
|
|
10
|
+
|
|
11
|
+
// Any IntelliJ-platform IDE (IDEA, PyCharm, WebStorm, GoLand, Rider, Android Studio…)
|
|
12
|
+
// can host the plugin. The registration is still bound to the running PID and app path.
|
|
13
|
+
static BOOL IDEBundleIsJetBrains(NSString *bundleID) {
|
|
14
|
+
if (![bundleID isKindOfClass:NSString.class]) return NO;
|
|
15
|
+
return [bundleID hasPrefix:@"com.jetbrains."] || [bundleID isEqualToString:@"com.google.android.studio"];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static NSString *IDERegistryDirectory(void) {
|
|
19
|
+
return [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/CC Pets/ide-instances"];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A registration never chooses an arbitrary URL. Only authenticated IPv4 loopback
|
|
23
|
+
// is used, with redirects and system proxies disabled.
|
|
24
|
+
@interface CCPetsIDELocalSession : NSObject <NSURLSessionTaskDelegate>
|
|
25
|
+
@end
|
|
26
|
+
@implementation CCPetsIDELocalSession
|
|
27
|
+
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
|
|
28
|
+
willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request
|
|
29
|
+
completionHandler:(void (^)(NSURLRequest *))completionHandler {
|
|
30
|
+
completionHandler(nil);
|
|
31
|
+
}
|
|
32
|
+
@end
|
|
33
|
+
|
|
34
|
+
NSDictionary *CCPetsIDEBridgeRequest(NSDictionary *instance, NSString *route, NSDictionary *body) {
|
|
35
|
+
NSInteger port = [instance[@"port"] integerValue];
|
|
36
|
+
NSString *token = instance[@"token"];
|
|
37
|
+
if (port < 1 || port > 65535 || ![token isKindOfClass:NSString.class] || token.length < 32) return nil;
|
|
38
|
+
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:%ld/%@", (long)port, route]];
|
|
39
|
+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
|
40
|
+
request.HTTPMethod = @"POST";
|
|
41
|
+
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:body ?: @{} options:0 error:nil];
|
|
42
|
+
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
|
43
|
+
[request setValue:[@"Bearer " stringByAppendingString:token] forHTTPHeaderField:@"Authorization"];
|
|
44
|
+
NSTimeInterval timeout = [route isEqualToString:@"focus"] ? 4 : 1;
|
|
45
|
+
NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration;
|
|
46
|
+
configuration.timeoutIntervalForRequest = timeout;
|
|
47
|
+
configuration.timeoutIntervalForResource = timeout;
|
|
48
|
+
configuration.connectionProxyDictionary = @{};
|
|
49
|
+
configuration.URLCache = nil;
|
|
50
|
+
configuration.HTTPCookieStorage = nil;
|
|
51
|
+
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
|
|
52
|
+
delegate:[CCPetsIDELocalSession new] delegateQueue:nil];
|
|
53
|
+
dispatch_semaphore_t done = dispatch_semaphore_create(0);
|
|
54
|
+
__block NSDictionary *result = nil;
|
|
55
|
+
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
|
|
56
|
+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
57
|
+
NSInteger status = [(NSHTTPURLResponse *)response statusCode];
|
|
58
|
+
if (!error && data.length < 65536 && status >= 200 && status < 500) {
|
|
59
|
+
id value = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
60
|
+
if ([value isKindOfClass:NSDictionary.class]) result = value;
|
|
61
|
+
}
|
|
62
|
+
dispatch_semaphore_signal(done);
|
|
63
|
+
}];
|
|
64
|
+
[task resume];
|
|
65
|
+
if (dispatch_semaphore_wait(done, dispatch_time(DISPATCH_TIME_NOW, (int64_t)((timeout + 0.2) * NSEC_PER_SEC)))) {
|
|
66
|
+
[task cancel];
|
|
67
|
+
[session invalidateAndCancel];
|
|
68
|
+
return nil;
|
|
69
|
+
}
|
|
70
|
+
[session finishTasksAndInvalidate];
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
NSArray<NSDictionary *> *CCPetsIDEBridgeInstances(BOOL (^accept)(NSDictionary *record, NSRunningApplication *app)) {
|
|
75
|
+
NSMutableArray *instances = [NSMutableArray array];
|
|
76
|
+
NSString *directory = IDERegistryDirectory();
|
|
77
|
+
for (NSString *file in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
78
|
+
if (![file.pathExtension isEqualToString:@"json"]) continue;
|
|
79
|
+
NSString *path = [directory stringByAppendingPathComponent:file];
|
|
80
|
+
struct stat info;
|
|
81
|
+
if (lstat(path.fileSystemRepresentation, &info) || !S_ISREG(info.st_mode) ||
|
|
82
|
+
info.st_uid != getuid() || (info.st_mode & 0077) || info.st_size > 65536) continue;
|
|
83
|
+
NSData *data = [NSData dataWithContentsOfFile:path];
|
|
84
|
+
id record = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
85
|
+
if (![record isKindOfClass:NSDictionary.class] || [record[@"schemaVersion"] integerValue] != 1 ||
|
|
86
|
+
![record[@"instanceID"] isKindOfClass:NSString.class] ||
|
|
87
|
+
![[file stringByDeletingPathExtension] isEqualToString:record[@"instanceID"]]) continue;
|
|
88
|
+
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:[record[@"pid"] intValue]];
|
|
89
|
+
if (!app || app.terminated || ![app.bundleURL.path isEqualToString:record[@"appPath"]] ||
|
|
90
|
+
!accept(record, app)) continue;
|
|
91
|
+
[instances addObject:record];
|
|
92
|
+
if (instances.count >= 16) break;
|
|
93
|
+
}
|
|
94
|
+
return instances;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// JetBrains registrations carry no "kind"; other bridges (VS Code) tag themselves.
|
|
98
|
+
static NSArray<NSDictionary *> *IDEInstances(void) {
|
|
99
|
+
return CCPetsIDEBridgeInstances(^BOOL(NSDictionary *record, NSRunningApplication *app) {
|
|
100
|
+
return record[@"kind"] == nil && IDEBundleIsJetBrains(app.bundleIdentifier);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static NSString *IDEPluginArchive(void) {
|
|
105
|
+
NSString *relative = @"IDEPlugins/cc-pets-terminal.zip";
|
|
106
|
+
NSString *bundled = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:relative];
|
|
107
|
+
if ([NSFileManager.defaultManager fileExistsAtPath:bundled]) return bundled;
|
|
108
|
+
NSString *besideBinary = [[NSProcessInfo.processInfo.arguments.firstObject stringByDeletingLastPathComponent]
|
|
109
|
+
stringByAppendingPathComponent:relative];
|
|
110
|
+
return [NSFileManager.defaultManager fileExistsAtPath:besideBinary] ? besideBinary : nil;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void CCPetsShowIDEPluginInstaller(void) {
|
|
114
|
+
NSString *archive = IDEPluginArchive();
|
|
115
|
+
NSAlert *alert = [NSAlert new];
|
|
116
|
+
alert.messageText = L(@"Install the JetBrains plugin");
|
|
117
|
+
if (!archive) {
|
|
118
|
+
alert.informativeText = L(@"The JetBrains plugin ZIP is missing from this CC Pets installation. Reinstall CC Pets to restore it.");
|
|
119
|
+
[alert addButtonWithTitle:L(@"OK")];
|
|
120
|
+
[alert runModal];
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
[NSWorkspace.sharedWorkspace activateFileViewerSelectingURLs:@[[NSURL fileURLWithPath:archive]]];
|
|
124
|
+
alert.informativeText = L(@"In your JetBrains IDE, open Settings → Plugins → ⚙ → Install Plugin from Disk, then choose cc-pets-terminal.zip. Restart the IDE if prompted. Open a project and use the Reworked terminal; CC Pets connects automatically.");
|
|
125
|
+
[alert addButtonWithTitle:L(@"Copy ZIP Path")];
|
|
126
|
+
[alert addButtonWithTitle:L(@"Done")];
|
|
127
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) {
|
|
128
|
+
[NSPasteboard.generalPasteboard clearContents];
|
|
129
|
+
[NSPasteboard.generalPasteboard setString:archive forType:NSPasteboardTypeString];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#pragma mark - Plugin versions
|
|
134
|
+
|
|
135
|
+
static NSString *const IDEPluginFolder = @"cc-pets-terminal";
|
|
136
|
+
static NSString *const IDEPluginJar = @"cc-pets-terminal/lib/cc-pets-terminal.jar";
|
|
137
|
+
|
|
138
|
+
static NSData *IDEUnzipEntry(NSString *archive, NSString *entry) {
|
|
139
|
+
NSTask *task = [NSTask new];
|
|
140
|
+
task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/unzip"];
|
|
141
|
+
task.arguments = @[@"-p", archive, entry];
|
|
142
|
+
NSPipe *output = [NSPipe pipe];
|
|
143
|
+
task.standardOutput = output;
|
|
144
|
+
task.standardError = NSFileHandle.fileHandleWithNullDevice;
|
|
145
|
+
if (![task launchAndReturnError:nil]) return nil;
|
|
146
|
+
NSData *data = [output.fileHandleForReading readDataToEndOfFile];
|
|
147
|
+
[task waitUntilExit];
|
|
148
|
+
return task.terminationStatus == 0 && data.length < 4 * 1024 * 1024 ? data : nil;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static NSString *IDEVersionFromPluginXML(NSData *xml) {
|
|
152
|
+
NSString *text = xml ? [[NSString alloc] initWithData:xml encoding:NSUTF8StringEncoding] : nil;
|
|
153
|
+
if (!text) return nil;
|
|
154
|
+
NSRegularExpression *pattern = [NSRegularExpression regularExpressionWithPattern:
|
|
155
|
+
@"<version>\\s*([0-9A-Za-z.\\-]{1,32})\\s*</version>" options:0 error:nil];
|
|
156
|
+
NSTextCheckingResult *match = [pattern firstMatchInString:text options:0 range:NSMakeRange(0, text.length)];
|
|
157
|
+
return match ? [text substringWithRange:[match rangeAtIndex:1]] : nil;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Reading a version spawns unzip, so results are cached by path, size and mtime.
|
|
161
|
+
static NSString *IDECachedVersion(NSString *path, NSString *(^read)(void)) {
|
|
162
|
+
static NSMutableDictionary<NSString *, NSString *> *cache;
|
|
163
|
+
static dispatch_once_t once;
|
|
164
|
+
dispatch_once(&once, ^{ cache = [NSMutableDictionary dictionary]; });
|
|
165
|
+
struct stat info;
|
|
166
|
+
if (stat(path.fileSystemRepresentation, &info)) return nil;
|
|
167
|
+
NSString *key = [NSString stringWithFormat:@"%@|%lld|%ld.%09ld", path, (long long)info.st_size,
|
|
168
|
+
(long)info.st_mtimespec.tv_sec, info.st_mtimespec.tv_nsec];
|
|
169
|
+
@synchronized(cache) {
|
|
170
|
+
if (cache[key]) return cache[key].length ? cache[key] : nil;
|
|
171
|
+
}
|
|
172
|
+
NSString *version = read() ?: @"";
|
|
173
|
+
@synchronized(cache) { cache[key] = version; }
|
|
174
|
+
return version.length ? version : nil;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static NSString *IDEInstalledPluginVersion(NSString *jar) {
|
|
178
|
+
return IDECachedVersion(jar, ^NSString *{
|
|
179
|
+
return IDEVersionFromPluginXML(IDEUnzipEntry(jar, @"META-INF/plugin.xml"));
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static NSString *IDEBundledPluginVersion(void) {
|
|
184
|
+
NSString *archive = IDEPluginArchive();
|
|
185
|
+
if (!archive) return nil;
|
|
186
|
+
return IDECachedVersion(archive, ^NSString *{
|
|
187
|
+
NSData *jar = IDEUnzipEntry(archive, IDEPluginJar);
|
|
188
|
+
if (!jar) return nil;
|
|
189
|
+
NSString *temporary = [NSTemporaryDirectory() stringByAppendingPathComponent:
|
|
190
|
+
[NSString stringWithFormat:@"cc-pets-plugin-%@.jar", NSUUID.UUID.UUIDString]];
|
|
191
|
+
if (![jar writeToFile:temporary options:NSDataWritingAtomic error:nil]) return nil;
|
|
192
|
+
NSString *version = IDEVersionFromPluginXML(IDEUnzipEntry(temporary, @"META-INF/plugin.xml"));
|
|
193
|
+
[NSFileManager.defaultManager removeItemAtPath:temporary error:nil];
|
|
194
|
+
return version;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#pragma mark - Paths
|
|
199
|
+
|
|
200
|
+
static BOOL IDEOwnedDirectory(NSString *path) {
|
|
201
|
+
struct stat info;
|
|
202
|
+
return path.length && !lstat(path.fileSystemRepresentation, &info) && S_ISDIR(info.st_mode) &&
|
|
203
|
+
info.st_uid == getuid();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// idea.config.path / idea.plugins.path overrides make the inferred folder unreliable.
|
|
207
|
+
static BOOL IDEPropertiesOverridePaths(NSString *file) {
|
|
208
|
+
NSString *text = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
|
|
209
|
+
for (NSString *raw in [text componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) {
|
|
210
|
+
NSString *line = [raw stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
|
|
211
|
+
if ([line hasPrefix:@"idea.config.path"] || [line hasPrefix:@"idea.plugins.path"]) return YES;
|
|
212
|
+
}
|
|
213
|
+
return NO;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
#pragma mark - Snapshot
|
|
217
|
+
|
|
218
|
+
NSArray<NSDictionary *> *CCPetsIDEIntegrationSnapshot(void) {
|
|
219
|
+
NSMutableOrderedSet<NSString *> *paths = [NSMutableOrderedSet orderedSet];
|
|
220
|
+
NSMutableDictionary<NSString *, NSString *> *bundleIDs = [NSMutableDictionary dictionary];
|
|
221
|
+
for (NSString *directory in @[@"/Applications", [NSHomeDirectory() stringByAppendingPathComponent:@"Applications"]]) {
|
|
222
|
+
for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:directory error:nil]) {
|
|
223
|
+
if (![name.pathExtension isEqualToString:@"app"]) continue;
|
|
224
|
+
NSString *path = [directory stringByAppendingPathComponent:name];
|
|
225
|
+
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:
|
|
226
|
+
[path stringByAppendingPathComponent:@"Contents/Info.plist"]];
|
|
227
|
+
if (!IDEBundleIsJetBrains(plist[@"CFBundleIdentifier"])) continue;
|
|
228
|
+
[paths addObject:path];
|
|
229
|
+
bundleIDs[path] = plist[@"CFBundleIdentifier"];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
NSMutableDictionary<NSString *, NSDate *> *launched = [NSMutableDictionary dictionary];
|
|
233
|
+
for (NSRunningApplication *app in NSWorkspace.sharedWorkspace.runningApplications) {
|
|
234
|
+
NSString *path = app.bundleURL.path;
|
|
235
|
+
if (!IDEBundleIsJetBrains(app.bundleIdentifier) || !path) continue;
|
|
236
|
+
[paths addObject:path];
|
|
237
|
+
bundleIDs[path] = app.bundleIdentifier;
|
|
238
|
+
launched[path] = app.launchDate ?: NSDate.distantPast;
|
|
239
|
+
}
|
|
240
|
+
NSArray *instances = IDEInstances();
|
|
241
|
+
NSMutableDictionary *connected = [NSMutableDictionary dictionary];
|
|
242
|
+
for (NSDictionary *instance in instances) {
|
|
243
|
+
NSDictionary *status = CCPetsIDEBridgeRequest(instance, @"status", nil);
|
|
244
|
+
if ([status[@"instanceID"] isEqual:instance[@"instanceID"]] && [status[@"schemaVersion"] integerValue] == 1)
|
|
245
|
+
connected[instance[@"appPath"]] = status;
|
|
246
|
+
}
|
|
247
|
+
NSString *bundledVersion = IDEBundledPluginVersion();
|
|
248
|
+
NSMutableArray *rows = [NSMutableArray array];
|
|
249
|
+
for (NSString *path in paths) {
|
|
250
|
+
NSData *data = [NSData dataWithContentsOfFile:[path stringByAppendingPathComponent:@"Contents/Resources/product-info.json"]];
|
|
251
|
+
id info = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
|
252
|
+
if (![info isKindOfClass:NSDictionary.class]) continue;
|
|
253
|
+
NSString *version = [info[@"version"] isKindOfClass:NSString.class] ? info[@"version"] : @"";
|
|
254
|
+
NSString *build = [info[@"buildNumber"] isKindOfClass:NSString.class] ? info[@"buildNumber"] : @"";
|
|
255
|
+
// Some products prefix the build with their product code (e.g. "AI-251.…").
|
|
256
|
+
NSString *bareBuild = [build componentsSeparatedByString:@"-"].lastObject;
|
|
257
|
+
NSInteger baseline = [bareBuild componentsSeparatedByString:@"."].firstObject.integerValue;
|
|
258
|
+
BOOL compatible = baseline == 262 || baseline == 263;
|
|
259
|
+
// product-info.json names the per-product config folder (e.g. PyCharm2026.2,
|
|
260
|
+
// Google/AndroidStudio2025.1); fall back to the IDEA layout for older metadata.
|
|
261
|
+
NSString *dataDirectory = [info[@"dataDirectoryName"] isKindOfClass:NSString.class] ? info[@"dataDirectoryName"] : nil;
|
|
262
|
+
if (dataDirectory.length == 0) {
|
|
263
|
+
NSArray *parts = [version componentsSeparatedByString:@"."];
|
|
264
|
+
NSString *suffix = parts.count >= 2 ? [NSString stringWithFormat:@"%@.%@", parts[0], parts[1]] : version;
|
|
265
|
+
dataDirectory = [@"IntelliJIdea" stringByAppendingString:suffix];
|
|
266
|
+
}
|
|
267
|
+
BOOL google = [dataDirectory hasPrefix:@"AndroidStudio"];
|
|
268
|
+
NSString *config = [[NSHomeDirectory() stringByAppendingPathComponent:
|
|
269
|
+
google ? @"Library/Application Support/Google" : @"Library/Application Support/JetBrains"]
|
|
270
|
+
stringByAppendingPathComponent:dataDirectory];
|
|
271
|
+
NSString *plugins = [config stringByAppendingPathComponent:@"plugins"];
|
|
272
|
+
// A live registration supplies the IDE's real paths (Toolbox, idea.config.path, …).
|
|
273
|
+
BOOL registered = NO;
|
|
274
|
+
for (NSDictionary *instance in instances) {
|
|
275
|
+
if (![instance[@"appPath"] isEqual:path]) continue;
|
|
276
|
+
if ([instance[@"configPath"] isKindOfClass:NSString.class]) config = instance[@"configPath"];
|
|
277
|
+
if ([instance[@"pluginsPath"] isKindOfClass:NSString.class]) plugins = instance[@"pluginsPath"];
|
|
278
|
+
registered = YES;
|
|
279
|
+
}
|
|
280
|
+
BOOL customPaths = !registered &&
|
|
281
|
+
(IDEPropertiesOverridePaths([path stringByAppendingPathComponent:@"Contents/bin/idea.properties"]) ||
|
|
282
|
+
IDEPropertiesOverridePaths([config stringByAppendingPathComponent:@"idea.properties"]));
|
|
283
|
+
// The IDE must have run once so its config folder exists; never create it ourselves.
|
|
284
|
+
BOOL autoInstall = compatible && !customPaths && IDEOwnedDirectory(config);
|
|
285
|
+
|
|
286
|
+
NSString *pluginDirectory = [plugins stringByAppendingPathComponent:IDEPluginFolder];
|
|
287
|
+
NSString *jar = [plugins stringByAppendingPathComponent:IDEPluginJar];
|
|
288
|
+
BOOL installed = [NSFileManager.defaultManager fileExistsAtPath:jar];
|
|
289
|
+
NSString *installedVersion = installed ? IDEInstalledPluginVersion(jar) : nil;
|
|
290
|
+
NSDictionary *status = connected[path];
|
|
291
|
+
NSString *runningVersion = [status[@"pluginVersion"] isKindOfClass:NSString.class] ? status[@"pluginVersion"] : nil;
|
|
292
|
+
BOOL outdated = installed && bundledVersion.length && installedVersion.length &&
|
|
293
|
+
[installedVersion compare:bundledVersion options:NSNumericSearch] == NSOrderedAscending;
|
|
294
|
+
// Installed or updated after the IDE started: files are in place but not loaded yet.
|
|
295
|
+
struct stat folder;
|
|
296
|
+
BOOL changedSinceLaunch = launched[path] && installed &&
|
|
297
|
+
!stat(pluginDirectory.fileSystemRepresentation, &folder) &&
|
|
298
|
+
folder.st_mtimespec.tv_sec > (time_t)launched[path].timeIntervalSince1970;
|
|
299
|
+
BOOL pendingRestart = launched[path] && installed &&
|
|
300
|
+
(status ? (runningVersion.length && ![runningVersion isEqualToString:@"unknown"] && installedVersion.length &&
|
|
301
|
+
![runningVersion isEqualToString:installedVersion])
|
|
302
|
+
: changedSinceLaunch);
|
|
303
|
+
|
|
304
|
+
NSString *disabled = [NSString stringWithContentsOfFile:[config stringByAppendingPathComponent:@"disabled_plugins.txt"]
|
|
305
|
+
encoding:NSUTF8StringEncoding error:nil];
|
|
306
|
+
BOOL isDisabled = [[disabled componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet] containsObject:IDEPluginID];
|
|
307
|
+
NSString *state;
|
|
308
|
+
if (!compatible && !status) state = @"unsupported";
|
|
309
|
+
else if (isDisabled && !status) state = @"disabled";
|
|
310
|
+
else if (!installed && !status) state = @"missing";
|
|
311
|
+
else if (outdated) state = @"outdated";
|
|
312
|
+
else if (pendingRestart) state = @"pendingRestart";
|
|
313
|
+
else if (status) state = @"connected";
|
|
314
|
+
else state = @"installed";
|
|
315
|
+
NSMutableDictionary *row = [@{@"name": [path.lastPathComponent stringByDeletingPathExtension],
|
|
316
|
+
@"version": version, @"state": state, @"path": path, @"compatible": @(compatible),
|
|
317
|
+
@"autoInstall": @(autoInstall), @"running": @(launched[path] != nil),
|
|
318
|
+
@"configPath": config, @"pluginsPath": plugins} mutableCopy];
|
|
319
|
+
if (bundleIDs[path]) row[@"bundleID"] = bundleIDs[path];
|
|
320
|
+
// 同一产品的多个版本(含 EAP)共用 product-info 里的 name,设置页据此合并成一行。
|
|
321
|
+
if ([info[@"name"] isKindOfClass:NSString.class]) row[@"productName"] = info[@"name"];
|
|
322
|
+
if (installedVersion) row[@"pluginVersion"] = installedVersion;
|
|
323
|
+
if (bundledVersion) row[@"bundledVersion"] = bundledVersion;
|
|
324
|
+
[rows addObject:row];
|
|
325
|
+
}
|
|
326
|
+
return rows;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
#pragma mark - Install
|
|
330
|
+
|
|
331
|
+
static NSError *IDEInstallError(NSString *message) {
|
|
332
|
+
return [NSError errorWithDomain:@"CCPetsIDEPlugin" code:1 userInfo:@{NSLocalizedDescriptionKey: message}];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Returns nil on success, otherwise a message for the user.
|
|
336
|
+
static NSString *IDESwapInPlugin(NSString *archive, NSString *staging, NSString *target) {
|
|
337
|
+
struct stat info;
|
|
338
|
+
NSTask *task = [NSTask new];
|
|
339
|
+
task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/ditto"];
|
|
340
|
+
task.arguments = @[@"-x", @"-k", archive, staging];
|
|
341
|
+
task.standardOutput = NSFileHandle.fileHandleWithNullDevice;
|
|
342
|
+
task.standardError = NSFileHandle.fileHandleWithNullDevice;
|
|
343
|
+
if (![task launchAndReturnError:nil]) return L(@"Installation failed.");
|
|
344
|
+
[task waitUntilExit];
|
|
345
|
+
NSString *fresh = [staging stringByAppendingPathComponent:IDEPluginFolder];
|
|
346
|
+
NSString *freshJar = [staging stringByAppendingPathComponent:IDEPluginJar];
|
|
347
|
+
if (task.terminationStatus != 0 || lstat(freshJar.fileSystemRepresentation, &info) || !S_ISREG(info.st_mode)) {
|
|
348
|
+
return L(@"The bundled plugin ZIP is damaged. Reinstall CC Pets to restore it.");
|
|
349
|
+
}
|
|
350
|
+
NSString *previous = [staging stringByAppendingPathComponent:@"previous"];
|
|
351
|
+
BOOL hadPrevious = lstat(target.fileSystemRepresentation, &info) == 0;
|
|
352
|
+
if (hadPrevious && rename(target.fileSystemRepresentation, previous.fileSystemRepresentation) != 0) {
|
|
353
|
+
return [NSString stringWithFormat:@"%@ (%s)", target, strerror(errno)];
|
|
354
|
+
}
|
|
355
|
+
if (rename(fresh.fileSystemRepresentation, target.fileSystemRepresentation) != 0) {
|
|
356
|
+
NSString *message = [NSString stringWithFormat:@"%@ (%s)", target, strerror(errno)];
|
|
357
|
+
if (hadPrevious) rename(previous.fileSystemRepresentation, target.fileSystemRepresentation);
|
|
358
|
+
return message;
|
|
359
|
+
}
|
|
360
|
+
// The ZIP carries fixed timestamps; mark the folder with the install time so a
|
|
361
|
+
// running IDE can be reported as needing a restart.
|
|
362
|
+
utimes(target.fileSystemRepresentation, NULL);
|
|
363
|
+
return nil;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Extracts the bundled ZIP next to the target and swaps it in with renames, so a failed
|
|
367
|
+
// extraction never leaves a half-written plugin behind.
|
|
368
|
+
BOOL CCPetsInstallIDEPlugin(NSDictionary *ide, NSError **error) {
|
|
369
|
+
NSString *archive = IDEPluginArchive();
|
|
370
|
+
NSString *config = ide[@"configPath"];
|
|
371
|
+
NSString *plugins = ide[@"pluginsPath"];
|
|
372
|
+
NSFileManager *files = NSFileManager.defaultManager;
|
|
373
|
+
if (!archive) {
|
|
374
|
+
if (error) *error = IDEInstallError(L(@"The JetBrains plugin ZIP is missing from this CC Pets installation. Reinstall CC Pets to restore it."));
|
|
375
|
+
return NO;
|
|
376
|
+
}
|
|
377
|
+
if (![ide[@"autoInstall"] boolValue] || ![config isKindOfClass:NSString.class] ||
|
|
378
|
+
![plugins isKindOfClass:NSString.class] || !IDEOwnedDirectory(config)) {
|
|
379
|
+
if (error) *error = IDEInstallError(L(@"This IDE's plugin folder could not be determined safely."));
|
|
380
|
+
return NO;
|
|
381
|
+
}
|
|
382
|
+
struct stat info;
|
|
383
|
+
if (lstat(plugins.fileSystemRepresentation, &info) == 0) {
|
|
384
|
+
if (!IDEOwnedDirectory(plugins)) {
|
|
385
|
+
if (error) *error = IDEInstallError(L(@"This IDE's plugin folder could not be determined safely."));
|
|
386
|
+
return NO;
|
|
387
|
+
}
|
|
388
|
+
} else if (mkdir(plugins.fileSystemRepresentation, 0755) != 0) {
|
|
389
|
+
if (error) *error = IDEInstallError([NSString stringWithFormat:@"%@ (%s)", plugins, strerror(errno)]);
|
|
390
|
+
return NO;
|
|
391
|
+
}
|
|
392
|
+
NSString *target = [plugins stringByAppendingPathComponent:IDEPluginFolder];
|
|
393
|
+
if (lstat(target.fileSystemRepresentation, &info) == 0 && (!S_ISDIR(info.st_mode) || info.st_uid != getuid())) {
|
|
394
|
+
if (error) *error = IDEInstallError(L(@"This IDE's plugin folder could not be determined safely."));
|
|
395
|
+
return NO;
|
|
396
|
+
}
|
|
397
|
+
char pattern[PATH_MAX];
|
|
398
|
+
snprintf(pattern, sizeof pattern, "%s/.cc-pets-terminal-install-XXXXXX", plugins.fileSystemRepresentation);
|
|
399
|
+
if (!mkdtemp(pattern)) {
|
|
400
|
+
if (error) *error = IDEInstallError([NSString stringWithFormat:@"%@ (%s)", plugins, strerror(errno)]);
|
|
401
|
+
return NO;
|
|
402
|
+
}
|
|
403
|
+
NSString *staging = [files stringWithFileSystemRepresentation:pattern length:strlen(pattern)];
|
|
404
|
+
NSString *failure = IDESwapInPlugin(archive, staging, target);
|
|
405
|
+
[files removeItemAtPath:staging error:nil];
|
|
406
|
+
if (failure && error) *error = IDEInstallError(failure);
|
|
407
|
+
return failure == nil;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
void CCPetsPromptInstallIDEPlugin(NSDictionary *ide, void (^completion)(void)) {
|
|
411
|
+
if (![ide[@"autoInstall"] boolValue]) {
|
|
412
|
+
CCPetsShowIDEPluginInstaller();
|
|
413
|
+
if (completion) completion();
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
BOOL update = [ide[@"state"] isEqualToString:@"outdated"];
|
|
417
|
+
NSString *name = ide[@"name"];
|
|
418
|
+
NSAlert *confirm = [NSAlert new];
|
|
419
|
+
confirm.messageText = [NSString stringWithFormat:
|
|
420
|
+
update ? L(@"Update CC Pets Terminal in %@?") : L(@"Install CC Pets Terminal in %@?"), name];
|
|
421
|
+
NSString *where = [NSString stringWithFormat:L(@"The plugin will be written to %@."),
|
|
422
|
+
[ide[@"pluginsPath"] stringByAbbreviatingWithTildeInPath]];
|
|
423
|
+
confirm.informativeText = [ide[@"running"] boolValue]
|
|
424
|
+
? [NSString stringWithFormat:@"%@ %@", where, [NSString stringWithFormat:L(@"Restart %@ afterwards to load it."), name]]
|
|
425
|
+
: [NSString stringWithFormat:@"%@ %@", where, L(@"It loads the next time the IDE starts.")];
|
|
426
|
+
[confirm addButtonWithTitle:update ? L(@"Update") : L(@"Install")];
|
|
427
|
+
[confirm addButtonWithTitle:L(@"Cancel")];
|
|
428
|
+
if ([confirm runModal] != NSAlertFirstButtonReturn) {
|
|
429
|
+
if (completion) completion();
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
433
|
+
NSError *error = nil;
|
|
434
|
+
BOOL ok = CCPetsInstallIDEPlugin(ide, &error);
|
|
435
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
436
|
+
if (!ok) {
|
|
437
|
+
NSAlert *alert = [NSAlert new];
|
|
438
|
+
alert.alertStyle = NSAlertStyleWarning;
|
|
439
|
+
alert.messageText = L(@"Could not install the plugin automatically");
|
|
440
|
+
alert.informativeText = error.localizedDescription ?: L(@"Installation failed.");
|
|
441
|
+
[alert addButtonWithTitle:L(@"Install Manually…")];
|
|
442
|
+
[alert addButtonWithTitle:L(@"OK")];
|
|
443
|
+
if ([alert runModal] == NSAlertFirstButtonReturn) CCPetsShowIDEPluginInstaller();
|
|
444
|
+
}
|
|
445
|
+
if (completion) completion();
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
static void IDEFocusNotice(BOOL pluginMissing, NSString *bundleID) {
|
|
451
|
+
NSAlert *alert = [NSAlert new];
|
|
452
|
+
alert.messageText = L(@"JetBrains terminal jump");
|
|
453
|
+
alert.informativeText = pluginMissing
|
|
454
|
+
? L(@"Install and enable CC Pets Terminal in your JetBrains IDE, then open a project. The plugin connects automatically and selects the terminal for this card.")
|
|
455
|
+
: L(@"The matching JetBrains terminal is unavailable. Use a local Reworked terminal and trigger a new agent event. If the IDE is showing a dialog, close it and try again.");
|
|
456
|
+
if (pluginMissing) [alert addButtonWithTitle:L(@"Install Plugin…")];
|
|
457
|
+
[alert addButtonWithTitle:L(@"OK")];
|
|
458
|
+
if ([alert runModal] != NSAlertFirstButtonReturn || !pluginMissing) return;
|
|
459
|
+
// Install straight into the IDE that hosts this card when it is unambiguous.
|
|
460
|
+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
461
|
+
NSMutableArray *matches = [NSMutableArray array];
|
|
462
|
+
for (NSDictionary *ide in CCPetsIDEIntegrationSnapshot()) {
|
|
463
|
+
if ([ide[@"bundleID"] isEqualToString:bundleID] && [ide[@"running"] boolValue]) [matches addObject:ide];
|
|
464
|
+
}
|
|
465
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
466
|
+
if (matches.count == 1) CCPetsPromptInstallIDEPlugin(matches.firstObject, nil);
|
|
467
|
+
else CCPetsShowIDEPluginInstaller();
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
BOOL CCPetsRequestIDETerminalFocus(NSDictionary *target) {
|
|
473
|
+
NSString *bundle = [target[@"bundleID"] isKindOfClass:NSString.class] ? target[@"bundleID"] : @"";
|
|
474
|
+
NSString *instanceID = SanitizedShortString(target[@"ideInstance"], 64);
|
|
475
|
+
NSString *terminalID = SanitizedShortString(target[@"ideTerminal"], 64);
|
|
476
|
+
NSString *tty = SanitizedShortString([target[@"tty"] isKindOfClass:NSString.class]
|
|
477
|
+
? [target[@"tty"] lastPathComponent] : nil, 64);
|
|
478
|
+
// A VS Code window started from a JetBrains terminal inherits its CC_PETS_IDE_* values;
|
|
479
|
+
// those never name a VS Code terminal.
|
|
480
|
+
if (CCPetsBundleIsVSCodeFamily(bundle)) return NO;
|
|
481
|
+
if (!IDEBundleIsJetBrains(bundle) && instanceID.length == 0) return NO;
|
|
482
|
+
// AppKit stays responsive while the IDE selects its content on its own UI thread.
|
|
483
|
+
static BOOL focusing = NO;
|
|
484
|
+
if (focusing) return YES;
|
|
485
|
+
focusing = YES;
|
|
486
|
+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
|
|
487
|
+
NSArray *instances = IDEInstances();
|
|
488
|
+
BOOL found = NO, focused = NO;
|
|
489
|
+
for (NSDictionary *instance in instances) {
|
|
490
|
+
if (instanceID.length && ![instance[@"instanceID"] isEqualToString:instanceID]) continue;
|
|
491
|
+
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:[instance[@"pid"] intValue]];
|
|
492
|
+
if (!instanceID.length && ![app.bundleIdentifier isEqualToString:bundle]) continue;
|
|
493
|
+
found = YES;
|
|
494
|
+
NSDictionary *response = CCPetsIDEBridgeRequest(instance, @"focus", @{@"tty": tty ?: @"", @"terminalID": terminalID ?: @""});
|
|
495
|
+
if ([response[@"focused"] boolValue]) { focused = YES; break; }
|
|
496
|
+
}
|
|
497
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
498
|
+
focusing = NO;
|
|
499
|
+
if (!focused) IDEFocusNotice(!found, bundle);
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
return YES;
|
|
503
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// CC Pets 在 Claude Code / Codex 配置和 shell 里留下的集成:Agent Hooks、Claude status line
|
|
4
|
+
// 额度采集、.zshrc 里的 shim 段落与 shim 软链。原先是 scripts/install-claude-hooks.mjs、
|
|
5
|
+
// install-codex-hooks.mjs 和 uninstall-integrations.mjs,改成原生实现后安装和卸载都不再依赖 Node。
|
|
6
|
+
// CC Bridge 的集成不在这里,仍由 scripts/bridge 负责。
|
|
7
|
+
|
|
8
|
+
// 写进 Claude Code / Codex 配置里的条目怎么认。安装、卸载、cc-pets doctor 共用这一份,
|
|
9
|
+
// 以免哪天改了写法,doctor 还按旧规则说"一切正常"。Legacy 是改名前(codex-pet)的写法。
|
|
10
|
+
extern NSString *const CCPetsClaudeHookMarker;
|
|
11
|
+
extern NSString *const CCPetsLegacyClaudeHookMarker;
|
|
12
|
+
extern NSString *const CCPetsCodexHookMarker;
|
|
13
|
+
extern NSString *const CCPetsLegacyCodexHookMarker;
|
|
14
|
+
extern NSString *const CCPetsStatusLineMarker;
|
|
15
|
+
extern NSString *const CCPetsLegacyStatusLineMarker;
|
|
16
|
+
extern NSString *const CCPetsStatusLineStartMarker;
|
|
17
|
+
extern NSString *const CCPetsStatusLineEndMarker;
|
|
18
|
+
extern NSString *const CCPetsCreatedStatusLineMarker;
|
|
19
|
+
extern NSString *const CCPetsShimStartMarker;
|
|
20
|
+
extern NSString *const CCPetsShimEndMarker;
|
|
21
|
+
|
|
22
|
+
// marker + 指向 <包目录>/.build/release/<executable> 的 --hook 命令才算自家条目。
|
|
23
|
+
BOOL CCPetsIsManagedHookCommand(id command, NSString *marker, NSString *executable);
|
|
24
|
+
// 包装器形态的 status line:marker + bin/claude-statusline-with-pet + base64 编码的原命令。
|
|
25
|
+
BOOL CCPetsIsManagedStatusLine(NSString *command, NSString *marker);
|
|
26
|
+
// 能就地注入 prelude 的 status line 命令解析出的脚本:@{script, target, interpreter, flags},不能注入时为 nil。
|
|
27
|
+
NSDictionary<NSString *, id> *CCPetsParseStatusLineCommand(id command);
|
|
28
|
+
// 安装 Claude Hooks 可能修改的文件(配置与可注入的 status line 脚本),供诊断修复前备份。
|
|
29
|
+
NSArray<NSString *> *CCPetsClaudeIntegrationFilesAtHome(NSString *claudeHome);
|
|
30
|
+
|
|
31
|
+
// 写入 Hooks。binary 是写进 hook 命令的 cc-pets 路径,必须是 <包目录>/.build/release/cc-pets:
|
|
32
|
+
// 卸载和 cc-pets doctor 按这个后缀认领条目。
|
|
33
|
+
// Claude 的 status line 包装器取同一包目录下的 bin/claude-statusline-with-pet。
|
|
34
|
+
//
|
|
35
|
+
// 返回进程退出码:0 成功或没装对应 CLI(跳过不算失败,install-shell-integration.sh 是 set -eu,
|
|
36
|
+
// 非零会把后续步骤全部带停),1 配置文件无法解析或写入,2 缺参数。
|
|
37
|
+
int InstallClaudeHooks(NSString *binary);
|
|
38
|
+
int InstallCodexHooks(NSString *binary);
|
|
39
|
+
int InstallClaudeHooksAtHome(NSString *binary, NSString *claudeHome);
|
|
40
|
+
int InstallCodexHooksAtHome(NSString *binary, NSString *codexHome);
|
|
41
|
+
int UninstallClaudeHooksAtHome(NSString *claudeHome);
|
|
42
|
+
int UninstallCodexHooksAtHome(NSString *codexHome);
|
|
43
|
+
|
|
44
|
+
// 移除上面写入的一切,并还原 status line。shellRC 为 nil 时不碰 shell 配置。
|
|
45
|
+
// preparingInstall:安装前的清理,只迁移改名前(codex-pet 时代)的旧条目和 alias 段落,
|
|
46
|
+
// 当前版本的 Hooks、status line 注入和 shim 都留给紧接着的安装去覆盖。
|
|
47
|
+
// 返回 0;有配置文件解析失败时照常处理其余部分,最后返回 1。
|
|
48
|
+
int UninstallIntegrations(BOOL preparingInstall, NSString *shellRC);
|
|
49
|
+
|
|
50
|
+
// 记下 npm 安装时用的 node 与 npm-cli,供应用内一键更新使用(<Application Support>/CC Pets/updater.json)。
|
|
51
|
+
int ConfigureUpdater(void);
|