cc-pets 2.2.1 → 2.3.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 (171) hide show
  1. package/CC_BRIDGE.md +5 -2
  2. package/CC_BRIDGE.zh-CN.md +4 -2
  3. package/CHANGELOG.md +60 -0
  4. package/CHANGELOG.zh-CN.md +60 -0
  5. package/CONTRIBUTING.md +13 -10
  6. package/CONTRIBUTING.zh-CN.md +11 -10
  7. package/README.md +113 -24
  8. package/README.zh-CN.md +89 -28
  9. package/Resources/zh-Hans.lproj/Localizable.strings +568 -17
  10. package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
  11. package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +82 -23
  12. package/Sources/CCPets/{CCPetsAppDelegate+Bridge.m → App/CCPetsAppDelegate+Bridge.m} +35 -24
  13. package/Sources/CCPets/{CCPetsAppDelegate+Pets.m → App/CCPetsAppDelegate+Pets.m} +17 -3
  14. package/Sources/CCPets/{CCPetsAppDelegate+Private.h → App/CCPetsAppDelegate+Private.h} +70 -22
  15. package/Sources/CCPets/App/CCPetsAppDelegate+Profiles.m +238 -0
  16. package/Sources/CCPets/{CCPetsAppDelegate+Quota.m → App/CCPetsAppDelegate+Quota.m} +47 -32
  17. package/Sources/CCPets/{CCPetsAppDelegate+Settings.m → App/CCPetsAppDelegate+Settings.m} +86 -98
  18. package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +8 -4
  19. package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
  20. package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +15 -0
  21. package/Sources/CCPets/{CCPetsAppDelegate.m → App/CCPetsAppDelegate.m} +107 -11
  22. package/Sources/CCPets/App/CCPetsOpenCommand.h +13 -0
  23. package/Sources/CCPets/App/CCPetsOpenCommand.m +99 -0
  24. package/Sources/CCPets/{main.m → App/main.m} +68 -1
  25. package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
  26. package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
  27. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
  28. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
  29. package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
  30. package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
  31. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
  32. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
  33. package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
  34. package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -0
  35. package/Sources/CCPets/Integrations/CCPetsCLISupport.h +75 -0
  36. package/Sources/CCPets/Integrations/CCPetsCLISupport.m +342 -0
  37. package/Sources/CCPets/Integrations/CCPetsEvents.h +15 -0
  38. package/Sources/CCPets/{CCPetsEvents.m → Integrations/CCPetsEvents.m} +23 -0
  39. package/Sources/CCPets/Integrations/CCPetsIntegrations.h +51 -0
  40. package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
  41. package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
  42. package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
  43. package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
  44. package/Sources/CCPets/Pet/CCPetsPetStore.h +12 -0
  45. package/Sources/CCPets/Pet/CCPetsPetStore.m +476 -0
  46. package/Sources/CCPets/{CCPetsPhrases.h → Pet/CCPetsPhrases.h} +11 -2
  47. package/Sources/CCPets/{CCPetsPhrases.m → Pet/CCPetsPhrases.m} +19 -2
  48. package/Sources/CCPets/{CCPetsPhrasesEditor.m → Pet/CCPetsPhrasesEditor.m} +4 -1
  49. package/Sources/CCPets/Pet/PetView+Interaction.m +207 -0
  50. package/Sources/CCPets/Pet/PetView+Menu.m +598 -0
  51. package/Sources/CCPets/Pet/PetView+MicroBehavior.m +234 -0
  52. package/Sources/CCPets/Pet/PetView+Private.h +81 -0
  53. package/Sources/CCPets/{PetView.h → Pet/PetView.h} +3 -5
  54. package/Sources/CCPets/Pet/PetView.m +635 -0
  55. package/Sources/CCPets/Support/CCPetsDoctor.h +25 -0
  56. package/Sources/CCPets/Support/CCPetsDoctor.m +632 -0
  57. package/Sources/CCPets/Support/CCPetsJSON.h +27 -0
  58. package/Sources/CCPets/Support/CCPetsJSON.m +397 -0
  59. package/Sources/CCPets/{CCPetsL10n.h → Support/CCPetsL10n.h} +3 -1
  60. package/Sources/CCPets/{CCPetsL10n.m → Support/CCPetsL10n.m} +30 -1
  61. package/Sources/CCPets/Support/CCPetsPreferences.h +31 -0
  62. package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
  63. package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
  64. package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
  65. package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
  66. package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
  67. package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
  68. package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
  69. package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
  70. package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
  71. package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
  72. package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
  73. package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
  74. package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
  75. package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
  76. package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
  77. package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
  78. package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -0
  79. package/Sources/CCPets/UI/CCPetsUsageShared.h +53 -0
  80. package/Sources/CCPets/UI/CCPetsUsageStore.m +120 -0
  81. package/Sources/CCPets/UI/CCPetsUsageWidgets.h +30 -0
  82. package/Sources/CCPets/UI/CCPetsUsageWidgets.m +201 -0
  83. package/Sources/CCPets/UI/CCPetsWorkbench.h +40 -0
  84. package/Sources/CCPets/UI/CCPetsWorkbenchPages.h +13 -0
  85. package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +783 -0
  86. package/Sources/CCPets/{MenuToggleSwitch.m → UI/MenuToggleSwitch.m} +1 -0
  87. package/Sources/CCPets/{CCPetsCodexRateLimits.h → Usage/CCPetsCodexRateLimits.h} +1 -0
  88. package/Sources/CCPets/{CCPetsCodexRateLimits.m → Usage/CCPetsCodexRateLimits.m} +10 -3
  89. package/Sources/CCPets/{CCPetsQuotaHistory.h → Usage/CCPetsQuotaHistory.h} +3 -0
  90. package/Sources/CCPets/{CCPetsQuotaHistory.m → Usage/CCPetsQuotaHistory.m} +58 -0
  91. package/Sources/CCPets/Usage/CCPetsUsage+ClaudeReader.m +436 -0
  92. package/Sources/CCPets/Usage/CCPetsUsage+CodexReader.m +392 -0
  93. package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
  94. package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
  95. package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
  96. package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
  97. package/Sources/CCPets/Usage/CCPetsUsage.h +57 -0
  98. package/Sources/CCPets/Usage/CCPetsUsage.m +225 -0
  99. package/Sources/CCPets/Usage/CCPetsUsageMode.h +10 -0
  100. package/Sources/CCPets/Usage/CCPetsUsageMode.m +163 -0
  101. package/Sources/CCPets/{CCPetsUsageMonitor.h → Usage/CCPetsUsageMonitor.h} +3 -0
  102. package/Sources/CCPets/{CCPetsUsageMonitor.m → Usage/CCPetsUsageMonitor.m} +163 -6
  103. package/Sources/CCPets/Usage/QuotaDashboardView+Primitives.m +328 -0
  104. package/Sources/CCPets/Usage/QuotaDashboardView+Private.h +103 -0
  105. package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +616 -0
  106. package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
  107. package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
  108. package/THIRD_PARTY_NOTICES.md +1 -1
  109. package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
  110. package/bin/cc-pets +8 -6
  111. package/bin/claude-with-pet +10 -2
  112. package/bin/codex-with-pet +13 -5
  113. package/package.json +1 -1
  114. package/scripts/build.sh +7 -3
  115. package/scripts/i18n.zsh +9 -52
  116. package/scripts/install-app.sh +4 -0
  117. package/scripts/install-shell-integration.sh +17 -10
  118. package/scripts/uninstall-shell-integration.sh +6 -1
  119. package/Sources/CCPets/CCPetsEvents.h +0 -10
  120. package/Sources/CCPets/CCPetsUsage.h +0 -36
  121. package/Sources/CCPets/CCPetsUsage.m +0 -2069
  122. package/Sources/CCPets/PetView.m +0 -1839
  123. package/Sources/CCPets/QuotaDashboardView.m +0 -1145
  124. package/scripts/bridge/cli.mjs +0 -237
  125. package/scripts/bridge/core.mjs +0 -228
  126. package/scripts/bridge/hook.mjs +0 -150
  127. package/scripts/bridge/install.mjs +0 -311
  128. package/scripts/bridge/mcp-server.mjs +0 -308
  129. package/scripts/bridge/options.mjs +0 -87
  130. package/scripts/bridge/process.mjs +0 -151
  131. package/scripts/bridge/reservations.mjs +0 -248
  132. package/scripts/bridge/store.mjs +0 -433
  133. package/scripts/bridge/watch.mjs +0 -69
  134. package/scripts/check-l10n.mjs +0 -168
  135. package/scripts/configure-updater.mjs +0 -43
  136. package/scripts/detect-cli.mjs +0 -68
  137. package/scripts/doctor.mjs +0 -470
  138. package/scripts/i18n.mjs +0 -122
  139. package/scripts/install-claude-hooks.mjs +0 -253
  140. package/scripts/install-codex-hooks.mjs +0 -78
  141. package/scripts/integration-markers.mjs +0 -51
  142. package/scripts/locales/zh-Hans.json +0 -366
  143. package/scripts/pet-store.mjs +0 -415
  144. package/scripts/uninstall-integrations.mjs +0 -205
  145. /package/Sources/CCPets/{CCPetsAppDelegate+Update.m → App/CCPetsAppDelegate+Update.m} +0 -0
  146. /package/Sources/CCPets/{CCPetsVersion.h → App/CCPetsVersion.h} +0 -0
  147. /package/Sources/CCPets/{CCPetsVersion.m → App/CCPetsVersion.m} +0 -0
  148. /package/Sources/CCPets/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +0 -0
  149. /package/Sources/CCPets/{CCPetsImageLoader.h → Pet/CCPetsImageLoader.h} +0 -0
  150. /package/Sources/CCPets/{CCPetsImageLoader.m → Pet/CCPetsImageLoader.m} +0 -0
  151. /package/Sources/CCPets/{CCPetsPetSize.h → Pet/CCPetsPetSize.h} +0 -0
  152. /package/Sources/CCPets/{CCPetsPetSize.m → Pet/CCPetsPetSize.m} +0 -0
  153. /package/Sources/CCPets/{CCPetsPhrasesEditor.h → Pet/CCPetsPhrasesEditor.h} +0 -0
  154. /package/Sources/CCPets/{CCPetsPreviews.h → Pet/CCPetsPreviews.h} +0 -0
  155. /package/Sources/CCPets/{CCPetsPreviews.m → Pet/CCPetsPreviews.m} +0 -0
  156. /package/Sources/CCPets/Resources/{/351/273/230/350/256/244.webp" → ByteMochi.webp} +0 -0
  157. /package/Sources/CCPets/{CCPetsCleanup.h → Support/CCPetsCleanup.h} +0 -0
  158. /package/Sources/CCPets/{CCPetsCleanup.m → Support/CCPetsCleanup.m} +0 -0
  159. /package/Sources/CCPets/{CCPetsPaths.h → Support/CCPetsPaths.h} +0 -0
  160. /package/Sources/CCPets/{CCPetsPaths.m → Support/CCPetsPaths.m} +0 -0
  161. /package/Sources/CCPets/{CCPetsSystemMonitor.h → Support/CCPetsSystemMonitor.h} +0 -0
  162. /package/Sources/CCPets/{CCPetsSystemMonitor.m → Support/CCPetsSystemMonitor.m} +0 -0
  163. /package/Sources/CCPets/{CCPetsGlassMenu.h → UI/CCPetsGlassMenu.h} +0 -0
  164. /package/Sources/CCPets/{CCPetsGlassMenu.m → UI/CCPetsGlassMenu.m} +0 -0
  165. /package/Sources/CCPets/{MenuChoiceRow.h → UI/MenuChoiceRow.h} +0 -0
  166. /package/Sources/CCPets/{MenuChoiceRow.m → UI/MenuChoiceRow.m} +0 -0
  167. /package/Sources/CCPets/{MenuHintView.h → UI/MenuHintView.h} +0 -0
  168. /package/Sources/CCPets/{MenuHintView.m → UI/MenuHintView.m} +0 -0
  169. /package/Sources/CCPets/{MenuPetSizeView.h → UI/MenuPetSizeView.h} +0 -0
  170. /package/Sources/CCPets/{MenuPetSizeView.m → UI/MenuPetSizeView.m} +0 -0
  171. /package/Sources/CCPets/{MenuToggleSwitch.h → UI/MenuToggleSwitch.h} +0 -0
@@ -0,0 +1,38 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ extern NSString *const CCPetsProfileProviderClaude;
4
+ extern NSString *const CCPetsProfileProviderCodex;
5
+ extern NSNotificationName const CCPetsProfilesDidChangeNotification;
6
+
7
+ NSString *CCPetsProfilesPath(void);
8
+ NSArray<NSDictionary *> *CCPetsProfiles(void);
9
+ NSArray<NSDictionary *> *CCPetsProfilesForProvider(NSString *provider);
10
+ NSDictionary *CCPetsProfileWithID(NSString *profileID);
11
+ NSDictionary *CCPetsProfileForHome(NSString *provider, NSString *home);
12
+ // 集成状态:state 取下面三个值之一;界面显示的文字统一走 CCPetsProfileStateTitle。
13
+ extern NSString *const CCPetsProfileStateIntegrated;
14
+ extern NSString *const CCPetsProfileStateRepair;
15
+ extern NSString *const CCPetsProfileStateUnavailable;
16
+ NSDictionary *CCPetsProfileStatus(NSDictionary *profile);
17
+ NSString *CCPetsProfileStateTitle(NSString *state);
18
+ NSString *CCPetsProfileID(NSString *provider, NSString *home);
19
+ NSString *CCPetsProfileSlug(NSString *home);
20
+ NSString *CCPetsProfileDefaultDisplayName(NSString *provider, NSString *home);
21
+ BOOL CCPetsProfileIsOfficialDefault(NSDictionary *profile);
22
+ // 文件夹像不像这家的 Agent Home:有本家特征文件,且没有另一家独有的文件。添加和重新定位前用它挡住选错的目录。
23
+ BOOL CCPetsProfileHomeMatchesProvider(NSString *provider, NSString *home);
24
+
25
+ // 添加或更新登记。Home 必须是现有目录;保存前会解析真实路径。
26
+ // existingID 非空用于“重新定位”,保留原身份与历史。
27
+ NSDictionary *CCPetsRegisterProfile(NSString *provider, NSString *home,
28
+ NSString *displayName, NSString *existingID, NSString **errorMessage);
29
+ BOOL CCPetsForgetProfile(NSString *profileID, NSString **errorMessage);
30
+ BOOL CCPetsSetProfileNeedsRepair(NSString *profileID, BOOL needsRepair, NSString **errorMessage);
31
+
32
+ // 只迁移已经存在 CC Pets 管理标记的默认配置,不扫描任何其他目录。
33
+ BOOL CCPetsMigrateManagedDefaultProfiles(NSString **errorMessage);
34
+ // 自动发现官方 ~/.claude / ~/.codex(目录已存在或 CLI 已安装);不发现任何自定义 Home。
35
+ BOOL CCPetsEnsureOfficialDefaultProfiles(NSString **errorMessage);
36
+
37
+ // profile CLI:list/ensure-defaults/add/repair/remove。add/repair 会执行实际集成适配。
38
+ int CCPetsProfileCommandMain(NSArray<NSString *> *arguments);
@@ -0,0 +1,516 @@
1
+ #import "CCPetsProfiles.h"
2
+ #import "CCPetsBridge.h"
3
+ #import "CCPetsBridgeSetup.h"
4
+ #import "CCPetsCLISupport.h"
5
+ #import "CCPetsIntegrations.h"
6
+ #import "CCPetsJSON.h"
7
+ #import "CCPetsL10n.h"
8
+ #import <sys/stat.h>
9
+
10
+ NSString *const CCPetsProfileProviderClaude = @"claude";
11
+ NSString *const CCPetsProfileProviderCodex = @"codex";
12
+ NSNotificationName const CCPetsProfilesDidChangeNotification = @"CCPetsProfilesDidChange";
13
+ NSString *const CCPetsProfileStateIntegrated = @"integrated";
14
+ NSString *const CCPetsProfileStateRepair = @"repair";
15
+ NSString *const CCPetsProfileStateUnavailable = @"unavailable";
16
+
17
+ static NSString *ProfilesDirectory(void) {
18
+ return [CCPetsHomeDirectory() stringByAppendingPathComponent:@".cc-pets"];
19
+ }
20
+
21
+ NSString *CCPetsProfilesPath(void) {
22
+ return [ProfilesDirectory() stringByAppendingPathComponent:@"agent-profiles.json"];
23
+ }
24
+
25
+ static BOOL ValidProvider(NSString *provider) {
26
+ return [provider isEqualToString:CCPetsProfileProviderClaude] ||
27
+ [provider isEqualToString:CCPetsProfileProviderCodex];
28
+ }
29
+
30
+ static NSString *CanonicalHome(NSString *home) {
31
+ if (![home isKindOfClass:NSString.class] || home.length == 0) return nil;
32
+ NSString *expanded = [home stringByExpandingTildeInPath];
33
+ NSString *resolved = CCPetsRealPath(expanded);
34
+ return resolved ?: CCPetsResolvePath(expanded);
35
+ }
36
+
37
+ static uint64_t StablePathHash(NSString *text) {
38
+ uint64_t hash = 1469598103934665603ULL;
39
+ const unsigned char *bytes = (const unsigned char *)text.UTF8String;
40
+ for (const unsigned char *cursor = bytes; cursor && *cursor; cursor++) {
41
+ hash ^= (uint64_t)*cursor;
42
+ hash *= 1099511628211ULL;
43
+ }
44
+ return hash;
45
+ }
46
+
47
+ NSString *CCPetsProfileID(NSString *provider, NSString *home) {
48
+ NSString *canonical = CanonicalHome(home);
49
+ if (!ValidProvider(provider) || canonical.length == 0) return nil;
50
+ NSString *source = [NSString stringWithFormat:@"%@:%@", provider, canonical];
51
+ return [NSString stringWithFormat:@"%@:%016llx", provider,
52
+ (unsigned long long)StablePathHash(source)];
53
+ }
54
+
55
+ NSString *CCPetsProfileSlug(NSString *home) {
56
+ NSString *name = CanonicalHome(home).lastPathComponent ?: @"";
57
+ while ([name hasPrefix:@"."]) name = [name substringFromIndex:1];
58
+ return name.length > 0 ? name : @"profile";
59
+ }
60
+
61
+ static NSString *LegacyFriendlyDisplayName(NSString *provider, NSString *home) {
62
+ NSString *slug = CCPetsProfileSlug(home);
63
+ NSMutableArray<NSString *> *words = [NSMutableArray array];
64
+ for (NSString *part in [slug componentsSeparatedByCharactersInSet:
65
+ [NSCharacterSet characterSetWithCharactersInString:@"-_ "]]) {
66
+ if (part.length == 0) continue;
67
+ if ([part.lowercaseString isEqualToString:@"claude"]) [words addObject:@"Claude"];
68
+ else if ([part.lowercaseString isEqualToString:@"codex"]) [words addObject:@"Codex"];
69
+ else [words addObject:part.uppercaseString];
70
+ }
71
+ if (words.count == 0) {
72
+ return [provider isEqualToString:CCPetsProfileProviderClaude] ? @"Claude" : @"Codex";
73
+ }
74
+ return [words componentsJoinedByString:@" "];
75
+ }
76
+
77
+ NSString *CCPetsProfileDefaultDisplayName(NSString *provider, NSString *home) {
78
+ (void)provider;
79
+ return CCPetsProfileSlug(home);
80
+ }
81
+
82
+ BOOL CCPetsProfileIsOfficialDefault(NSDictionary *profile) {
83
+ NSString *provider = profile[@"provider"];
84
+ NSString *leaf = [provider isEqualToString:CCPetsProfileProviderClaude] ? @".claude"
85
+ : ([provider isEqualToString:CCPetsProfileProviderCodex] ? @".codex" : nil);
86
+ if (!leaf) return NO;
87
+ NSString *official = CanonicalHome([CCPetsHomeDirectory() stringByAppendingPathComponent:leaf]);
88
+ return official.length > 0 && [CanonicalHome(profile[@"home"]) isEqualToString:official];
89
+ }
90
+
91
+ // sessions/ 不能当 Codex 的特征:Claude Code 也会建 <Home>/sessions/<pid>.json,靠它判定时
92
+ // 选错的 Claude Home 会被当成 Codex 登记,集成步骤再把 Codex 的 config.toml / hooks.json 写进去。
93
+ static NSArray<NSString *> *ProfileHomeMarkers(NSString *provider) {
94
+ return [provider isEqualToString:CCPetsProfileProviderClaude]
95
+ ? @[@"settings.json", @".claude.json", @"projects"]
96
+ : @[@"config.toml", @"hooks.json", @"auth.json", @"version.json"];
97
+ }
98
+
99
+ // 另一家独有、只会由 Agent 自己写出的文件。settings.json / config.toml / hooks.json 不算:
100
+ // CC Pets 集成时会写它们,装错过一次的 Home 会因此被永久挡在外面。
101
+ static NSArray<NSString *> *ProfileHomeExclusiveMarkers(NSString *provider) {
102
+ return [provider isEqualToString:CCPetsProfileProviderClaude]
103
+ ? @[@".claude.json", @"projects"]
104
+ : @[@"auth.json", @"version.json"];
105
+ }
106
+
107
+ static BOOL HomeContainsAny(NSString *home, NSArray<NSString *> *names) {
108
+ for (NSString *name in names) {
109
+ if ([NSFileManager.defaultManager fileExistsAtPath:[home stringByAppendingPathComponent:name]]) return YES;
110
+ }
111
+ return NO;
112
+ }
113
+
114
+ BOOL CCPetsProfileHomeMatchesProvider(NSString *provider, NSString *home) {
115
+ if (!ValidProvider(provider) || home.length == 0) return NO;
116
+ NSString *other = [provider isEqualToString:CCPetsProfileProviderClaude]
117
+ ? CCPetsProfileProviderCodex : CCPetsProfileProviderClaude;
118
+ return HomeContainsAny(home, ProfileHomeMarkers(provider)) &&
119
+ !HomeContainsAny(home, ProfileHomeExclusiveMarkers(other));
120
+ }
121
+
122
+ static NSDictionary *EmptyDocument(void) {
123
+ return @{@"schemaVersion": @1, @"profiles": @[]};
124
+ }
125
+
126
+ static NSDictionary *ReadDocument(void) {
127
+ NSData *data = [NSData dataWithContentsOfFile:CCPetsProfilesPath()];
128
+ NSDictionary *document = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
129
+ if (![document isKindOfClass:NSDictionary.class] || [document[@"schemaVersion"] integerValue] != 1 ||
130
+ ![document[@"profiles"] isKindOfClass:NSArray.class]) return EmptyDocument();
131
+ return document;
132
+ }
133
+
134
+ static BOOL WriteProfiles(NSArray<NSDictionary *> *profiles, NSString **errorMessage) {
135
+ [NSFileManager.defaultManager createDirectoryAtPath:ProfilesDirectory()
136
+ withIntermediateDirectories:YES attributes:@{NSFilePosixPermissions: @0700} error:nil];
137
+ NSDictionary *document = @{@"schemaVersion": @1, @"profiles": profiles ?: @[]};
138
+ NSData *data = [NSJSONSerialization dataWithJSONObject:document
139
+ options:NSJSONWritingPrettyPrinted | NSJSONWritingSortedKeys | NSJSONWritingWithoutEscapingSlashes error:nil];
140
+ if (!data) {
141
+ if (errorMessage) *errorMessage = @"Unable to encode agent profile registry.";
142
+ return NO;
143
+ }
144
+ NSString *text = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
145
+ stringByAppendingString:@"\n"];
146
+ return CCPetsWriteFileAtomically(CCPetsProfilesPath(), text, 0600, errorMessage);
147
+ }
148
+
149
+ NSArray<NSDictionary *> *CCPetsProfiles(void) {
150
+ NSMutableArray<NSDictionary *> *valid = [NSMutableArray array];
151
+ for (id entry in ReadDocument()[@"profiles"]) {
152
+ if (![entry isKindOfClass:NSDictionary.class]) continue;
153
+ NSString *profileID = entry[@"id"], *provider = entry[@"provider"], *home = entry[@"home"];
154
+ if (![profileID isKindOfClass:NSString.class] || !ValidProvider(provider) ||
155
+ ![home isKindOfClass:NSString.class] || home.length == 0) continue;
156
+ NSMutableDictionary *profile = [entry mutableCopy];
157
+ NSString *rawName = CCPetsProfileDefaultDisplayName(provider, home);
158
+ NSString *storedName = [entry[@"displayName"] isKindOfClass:NSString.class]
159
+ ? entry[@"displayName"] : nil;
160
+ NSNumber *customName = [entry[@"customName"] isKindOfClass:NSNumber.class]
161
+ ? entry[@"customName"] : nil;
162
+ // 早期手动添加流程把自动生成的文件夹名也误记成了“自定义”。两者完全相同
163
+ // 时恢复为自动名称,重新定位 Home 后才能自然跟随新的文件夹名。
164
+ if ([customName boolValue] && [storedName isEqualToString:rawName]) customName = @NO;
165
+ if (!customName) {
166
+ NSString *legacyName = LegacyFriendlyDisplayName(provider, home);
167
+ BOOL wasCustomized = storedName.length > 0 &&
168
+ ![storedName isEqualToString:legacyName] && ![storedName isEqualToString:rawName];
169
+ customName = @(wasCustomized);
170
+ }
171
+ profile[@"customName"] = customName;
172
+ if (![customName boolValue]) profile[@"displayName"] = rawName;
173
+ else if (storedName.length == 0) profile[@"displayName"] = rawName;
174
+ profile[@"slug"] = CCPetsProfileSlug(home);
175
+ [valid addObject:profile];
176
+ }
177
+ return valid;
178
+ }
179
+
180
+ NSArray<NSDictionary *> *CCPetsProfilesForProvider(NSString *provider) {
181
+ NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *profile, NSDictionary *bindings) {
182
+ return [profile[@"provider"] isEqualToString:provider];
183
+ }];
184
+ return [CCPetsProfiles() filteredArrayUsingPredicate:predicate];
185
+ }
186
+
187
+ NSDictionary *CCPetsProfileWithID(NSString *profileID) {
188
+ for (NSDictionary *profile in CCPetsProfiles()) {
189
+ if ([profile[@"id"] isEqualToString:profileID]) return profile;
190
+ }
191
+ return nil;
192
+ }
193
+
194
+ NSDictionary *CCPetsProfileForHome(NSString *provider, NSString *home) {
195
+ NSString *canonical = CanonicalHome(home);
196
+ for (NSDictionary *profile in CCPetsProfiles()) {
197
+ if ([profile[@"provider"] isEqualToString:provider] &&
198
+ [profile[@"home"] isEqualToString:canonical]) return profile;
199
+ }
200
+ return nil;
201
+ }
202
+
203
+ NSDictionary *CCPetsRegisterProfile(NSString *provider, NSString *home,
204
+ NSString *displayName, NSString *existingID, NSString **errorMessage) {
205
+ if (!ValidProvider(provider)) {
206
+ if (errorMessage) *errorMessage = @"Provider must be claude or codex.";
207
+ return nil;
208
+ }
209
+ NSString *canonical = CanonicalHome(home);
210
+ BOOL isDirectory = NO;
211
+ if (canonical.length == 0 || ![NSFileManager.defaultManager fileExistsAtPath:canonical isDirectory:&isDirectory] || !isDirectory) {
212
+ if (errorMessage) *errorMessage = [NSString stringWithFormat:@"Profile Home is not a directory: %@", home ?: @""];
213
+ return nil;
214
+ }
215
+ NSString *profileID = existingID.length > 0 ? existingID : CCPetsProfileID(provider, canonical);
216
+ NSString *slug = CCPetsProfileSlug(canonical);
217
+ NSString *name = [displayName stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
218
+ BOOL customName = name.length > 0;
219
+ if (!customName) name = CCPetsProfileDefaultDisplayName(provider, canonical);
220
+ NSMutableArray<NSDictionary *> *profiles = [CCPetsProfiles() mutableCopy];
221
+ NSUInteger replacement = NSNotFound;
222
+ for (NSUInteger index = 0; index < profiles.count; index++) {
223
+ NSDictionary *candidate = profiles[index];
224
+ if (existingID.length > 0 && ![candidate[@"id"] isEqualToString:existingID] &&
225
+ [candidate[@"provider"] isEqualToString:provider] &&
226
+ [candidate[@"home"] isEqualToString:canonical]) {
227
+ if (errorMessage) *errorMessage = @"That Agent Home is already registered.";
228
+ return nil;
229
+ }
230
+ if ([candidate[@"id"] isEqualToString:profileID] ||
231
+ ([candidate[@"provider"] isEqualToString:provider] && [candidate[@"home"] isEqualToString:canonical])) {
232
+ replacement = index;
233
+ break;
234
+ }
235
+ }
236
+ NSNumber *addedAt = replacement == NSNotFound ? @(NSDate.date.timeIntervalSince1970) : profiles[replacement][@"addedAt"];
237
+ NSMutableDictionary *profile = [@{
238
+ @"id": profileID,
239
+ @"provider": provider,
240
+ @"home": canonical,
241
+ @"slug": slug,
242
+ @"displayName": name,
243
+ @"customName": @(customName),
244
+ @"bridgePolicy": @"followGlobal",
245
+ @"addedAt": addedAt ?: @(NSDate.date.timeIntervalSince1970)
246
+ } mutableCopy];
247
+ if (replacement != NSNotFound && [profiles[replacement][@"needsRepair"] boolValue]) {
248
+ profile[@"needsRepair"] = @YES;
249
+ }
250
+ if (replacement == NSNotFound) [profiles addObject:profile]; else profiles[replacement] = profile;
251
+ if (!WriteProfiles(profiles, errorMessage)) return nil;
252
+ return profile;
253
+ }
254
+
255
+ BOOL CCPetsForgetProfile(NSString *profileID, NSString **errorMessage) {
256
+ NSMutableArray<NSDictionary *> *profiles = [CCPetsProfiles() mutableCopy];
257
+ NSIndexSet *matches = [profiles indexesOfObjectsPassingTest:^BOOL(NSDictionary *profile, NSUInteger index, BOOL *stop) {
258
+ return [profile[@"id"] isEqualToString:profileID];
259
+ }];
260
+ if (matches.count == 0) return YES;
261
+ [profiles removeObjectsAtIndexes:matches];
262
+ return WriteProfiles(profiles, errorMessage);
263
+ }
264
+
265
+ BOOL CCPetsSetProfileNeedsRepair(NSString *profileID, BOOL needsRepair, NSString **errorMessage) {
266
+ NSMutableArray<NSDictionary *> *profiles = [CCPetsProfiles() mutableCopy];
267
+ BOOL found = NO;
268
+ for (NSUInteger index = 0; index < profiles.count; index++) {
269
+ if (![profiles[index][@"id"] isEqualToString:profileID]) continue;
270
+ NSMutableDictionary *updated = [profiles[index] mutableCopy];
271
+ if (needsRepair) updated[@"needsRepair"] = @YES;
272
+ else [updated removeObjectForKey:@"needsRepair"];
273
+ profiles[index] = updated;
274
+ found = YES;
275
+ break;
276
+ }
277
+ return !found || WriteProfiles(profiles, errorMessage);
278
+ }
279
+
280
+ static BOOL FileContainsMarker(NSString *path, NSString *marker) {
281
+ NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
282
+ return text.length > 0 && [text containsString:marker];
283
+ }
284
+
285
+ NSDictionary *CCPetsProfileStatus(NSDictionary *profile) {
286
+ NSString *provider = profile[@"provider"], *home = profile[@"home"];
287
+ BOOL available = CCPetsDirectoryExists(home);
288
+ if (!available) return @{@"state": CCPetsProfileStateUnavailable, @"available": @NO,
289
+ @"petHooks": @NO, @"bridge": @NO};
290
+ NSString *config = [home stringByAppendingPathComponent:
291
+ [provider isEqualToString:CCPetsProfileProviderClaude] ? @"settings.json" : @"hooks.json"];
292
+ NSString *petMarker = [provider isEqualToString:CCPetsProfileProviderClaude]
293
+ ? CCPetsClaudeHookMarker : CCPetsCodexHookMarker;
294
+ NSString *configText = [NSString stringWithContentsOfFile:config
295
+ encoding:NSUTF8StringEncoding error:nil] ?: @"";
296
+ NSString *profileID = [profile[@"id"] isKindOfClass:NSString.class] ? profile[@"id"] : @"";
297
+ BOOL petHooks = [configText containsString:petMarker] &&
298
+ (profileID.length == 0 || [configText containsString:profileID]);
299
+ BOOL bridge = [configText containsString:CCBridgeHookMarker] &&
300
+ (profileID.length == 0 || [configText containsString:profileID]);
301
+ BOOL bridgeExpected = CCBridgeEnabled();
302
+ BOOL healthy = petHooks && (!bridgeExpected || bridge) && ![profile[@"needsRepair"] boolValue];
303
+ return @{@"state": healthy ? CCPetsProfileStateIntegrated : CCPetsProfileStateRepair,
304
+ @"available": @YES, @"petHooks": @(petHooks), @"bridge": @(bridge)};
305
+ }
306
+
307
+ NSString *CCPetsProfileStateTitle(NSString *state) {
308
+ if ([state isEqualToString:CCPetsProfileStateIntegrated]) return L(@"Integrated");
309
+ if ([state isEqualToString:CCPetsProfileStateUnavailable]) return L(@"Home unavailable");
310
+ return L(@"Needs repair");
311
+ }
312
+
313
+ BOOL CCPetsMigrateManagedDefaultProfiles(NSString **errorMessage) {
314
+ NSArray<NSDictionary *> *candidates = @[
315
+ @{@"provider": CCPetsProfileProviderClaude,
316
+ @"home": [CCPetsHomeDirectory() stringByAppendingPathComponent:@".claude"],
317
+ @"file": @"settings.json", @"marker": CCPetsClaudeHookMarker},
318
+ @{@"provider": CCPetsProfileProviderCodex,
319
+ @"home": [CCPetsHomeDirectory() stringByAppendingPathComponent:@".codex"],
320
+ @"file": @"hooks.json", @"marker": CCPetsCodexHookMarker}
321
+ ];
322
+ for (NSDictionary *candidate in candidates) {
323
+ NSString *home = candidate[@"home"];
324
+ NSString *file = [home stringByAppendingPathComponent:candidate[@"file"]];
325
+ if (!FileContainsMarker(file, candidate[@"marker"])) continue;
326
+ if (CCPetsProfileForHome(candidate[@"provider"], home)) continue;
327
+ if (!CCPetsRegisterProfile(candidate[@"provider"], home, nil, nil, errorMessage)) return NO;
328
+ }
329
+ return YES;
330
+ }
331
+
332
+ BOOL CCPetsEnsureOfficialDefaultProfiles(NSString **errorMessage) {
333
+ NSArray<NSDictionary *> *candidates = @[
334
+ @{ @"provider": CCPetsProfileProviderClaude,
335
+ @"home": [CCPetsHomeDirectory() stringByAppendingPathComponent:@".claude"] },
336
+ @{ @"provider": CCPetsProfileProviderCodex,
337
+ @"home": [CCPetsHomeDirectory() stringByAppendingPathComponent:@".codex"] }
338
+ ];
339
+ for (NSDictionary *candidate in candidates) {
340
+ NSString *provider = candidate[@"provider"];
341
+ NSString *home = candidate[@"home"];
342
+ if (CCPetsProfileForHome(provider, home)) continue;
343
+ BOOL isDirectory = NO;
344
+ BOOL homeExists = [NSFileManager.defaultManager fileExistsAtPath:home isDirectory:&isDirectory] && isDirectory;
345
+ BOOL cliInstalled = [provider isEqualToString:CCPetsProfileProviderClaude]
346
+ ? CCPetsClaudeCLIInstalled() : CCPetsCodexCLIInstalled();
347
+ // 官方 Home 是唯一允许自动发现的目录:已有目录直接登记;CLI 已安装但首次尚未
348
+ // 生成目录时创建它。其他任何相似目录都只能由用户从菜单手动添加。
349
+ if (!homeExists && !cliInstalled) continue;
350
+ NSError *directoryError = nil;
351
+ if (!homeExists && ![NSFileManager.defaultManager createDirectoryAtPath:home
352
+ withIntermediateDirectories:YES attributes:nil error:&directoryError]) {
353
+ if (errorMessage) *errorMessage = directoryError.localizedDescription;
354
+ return NO;
355
+ }
356
+ if (!CCPetsRegisterProfile(provider, home, nil, nil, errorMessage)) return NO;
357
+ }
358
+ return YES;
359
+ }
360
+
361
+ static NSString *ValueAfter(NSArray<NSString *> *arguments, NSString *flag) {
362
+ NSUInteger index = [arguments indexOfObject:flag];
363
+ return index != NSNotFound && index + 1 < arguments.count ? arguments[index + 1] : nil;
364
+ }
365
+
366
+ static int AdaptProfile(NSDictionary *profile) {
367
+ NSString *provider = profile[@"provider"], *home = profile[@"home"];
368
+ CCPetsPrintLine(stdout, @"stage=hooks");
369
+ int status = [provider isEqualToString:CCPetsProfileProviderClaude]
370
+ ? InstallClaudeHooksAtHome(CCPetsExecutablePath(), home)
371
+ : InstallCodexHooksAtHome(CCPetsExecutablePath(), home);
372
+ if (status != 0) {
373
+ CCPetsSetProfileNeedsRepair(profile[@"id"], YES, NULL);
374
+ return status;
375
+ }
376
+ if (CCBridgeEnabled()) {
377
+ CCPetsPrintLine(stdout, @"stage=bridge");
378
+ NSArray<NSString *> *report = nil;
379
+ @try {
380
+ report = CCBridgeInstallForProfile(profile, CCBridgeOptions(), YES);
381
+ } @catch (NSException *exception) {
382
+ CCPetsSetProfileNeedsRepair(profile[@"id"], YES, NULL);
383
+ @throw;
384
+ }
385
+ BOOL bridgeFailed = NO;
386
+ for (NSString *line in report) {
387
+ CCPetsPrintLine([line hasPrefix:@"ERROR: "] ? stderr : stdout, line);
388
+ if ([line hasPrefix:@"ERROR: "]) bridgeFailed = YES;
389
+ }
390
+ if (bridgeFailed) {
391
+ CCPetsSetProfileNeedsRepair(profile[@"id"], YES, NULL);
392
+ return 1;
393
+ }
394
+ }
395
+ CCPetsPrintLine(stdout, @"stage=verify");
396
+ CCPetsSetProfileNeedsRepair(profile[@"id"], NO, NULL);
397
+ return 0;
398
+ }
399
+
400
+ int CCPetsProfileCommandMain(NSArray<NSString *> *arguments) {
401
+ @try {
402
+ NSString *command = arguments.firstObject;
403
+ if ([command isEqualToString:@"list"]) {
404
+ NSData *json = [NSJSONSerialization dataWithJSONObject:CCPetsProfiles()
405
+ options:NSJSONWritingPrettyPrinted | NSJSONWritingWithoutEscapingSlashes error:nil];
406
+ fwrite(json.bytes, 1, json.length, stdout); fputc('\n', stdout);
407
+ return 0;
408
+ }
409
+ if ([command isEqualToString:@"resolve"]) {
410
+ NSDictionary *profile = CCPetsProfileForHome(ValueAfter(arguments, @"--provider"),
411
+ ValueAfter(arguments, @"--home"));
412
+ if (!profile) return 1;
413
+ NSString *field = ValueAfter(arguments, @"--field");
414
+ CCPetsPrintLine(stdout, [field isEqualToString:@"name"]
415
+ ? (profile[@"displayName"] ?: profile[@"slug"] ?: @"") : profile[@"id"]);
416
+ return 0;
417
+ }
418
+ if ([command isEqualToString:@"migrate"]) {
419
+ NSString *error = nil;
420
+ if (!CCPetsMigrateManagedDefaultProfiles(&error)) {
421
+ CCPetsPrintLine(stderr, error); return 1;
422
+ }
423
+ return 0;
424
+ }
425
+ if ([command isEqualToString:@"ensure-defaults"]) {
426
+ NSString *error = nil;
427
+ if (!CCPetsEnsureOfficialDefaultProfiles(&error)) {
428
+ CCPetsPrintLine(stderr, error); return 1;
429
+ }
430
+ return 0;
431
+ }
432
+ if ([command isEqualToString:@"add"]) {
433
+ NSString *provider = ValueAfter(arguments, @"--provider");
434
+ NSString *home = ValueAfter(arguments, @"--home");
435
+ NSString *name = ValueAfter(arguments, @"--name");
436
+ NSString *error = nil;
437
+ NSDictionary *profile = CCPetsRegisterProfile(provider, home, name, nil, &error);
438
+ if (!profile) { CCPetsPrintLine(stderr, error); return 1; }
439
+ CCPetsPrintLine(stdout, @"stage=registered");
440
+ return AdaptProfile(profile);
441
+ }
442
+ if ([command isEqualToString:@"repair"]) {
443
+ NSDictionary *profile = CCPetsProfileWithID(ValueAfter(arguments, @"--id"));
444
+ if (!profile) { CCPetsPrintLine(stderr, @"Profile not found."); return 1; }
445
+ return AdaptProfile(profile);
446
+ }
447
+ if ([command isEqualToString:@"repair-all"]) {
448
+ BOOL failed = NO;
449
+ for (NSDictionary *profile in CCPetsProfiles()) {
450
+ CCPetsPrintLine(stdout, [NSString stringWithFormat:@"profile=%@", profile[@"displayName"]]);
451
+ if (AdaptProfile(profile) != 0) failed = YES;
452
+ }
453
+ return failed ? 1 : 0;
454
+ }
455
+ if ([command isEqualToString:@"update"]) {
456
+ NSString *profileID = ValueAfter(arguments, @"--id");
457
+ NSDictionary *oldProfile = CCPetsProfileWithID(profileID);
458
+ if (!oldProfile) { CCPetsPrintLine(stderr, @"Profile not found."); return 1; }
459
+ NSString *home = ValueAfter(arguments, @"--home") ?: oldProfile[@"home"];
460
+ NSString *requestedName = ValueAfter(arguments, @"--name");
461
+ NSString *name = requestedName ?: ([oldProfile[@"customName"] boolValue]
462
+ ? oldProfile[@"displayName"] : nil);
463
+ NSString *error = nil;
464
+ NSDictionary *profile = CCPetsRegisterProfile(oldProfile[@"provider"], home,
465
+ name, profileID, &error);
466
+ if (!profile) { CCPetsPrintLine(stderr, error); return 1; }
467
+ CCPetsPrintLine(stdout, @"stage=registered");
468
+ int status = AdaptProfile(profile);
469
+ if (status != 0) return status;
470
+ if (![oldProfile[@"home"] isEqualToString:profile[@"home"]]) {
471
+ for (NSString *line in CCBridgeUninstallForProfile(oldProfile, CCBridgeShouldTouchMCP())) {
472
+ CCPetsPrintLine(stdout, line);
473
+ }
474
+ status = [oldProfile[@"provider"] isEqualToString:CCPetsProfileProviderClaude]
475
+ ? UninstallClaudeHooksAtHome(oldProfile[@"home"])
476
+ : UninstallCodexHooksAtHome(oldProfile[@"home"]);
477
+ }
478
+ return status;
479
+ }
480
+ if ([command isEqualToString:@"remove"]) {
481
+ NSString *profileID = ValueAfter(arguments, @"--id");
482
+ NSDictionary *profile = CCPetsProfileWithID(profileID);
483
+ if (!profile) { CCPetsPrintLine(stderr, @"Profile not found."); return 1; }
484
+ BOOL bridgeFailed = NO;
485
+ for (NSString *line in CCBridgeUninstallForProfile(profile, CCBridgeShouldTouchMCP())) {
486
+ CCPetsPrintLine([line hasPrefix:@"ERROR: "] ? stderr : stdout, line);
487
+ if ([line hasPrefix:@"ERROR: "]) bridgeFailed = YES;
488
+ }
489
+ int status = [profile[@"provider"] isEqualToString:CCPetsProfileProviderClaude]
490
+ ? UninstallClaudeHooksAtHome(profile[@"home"])
491
+ : UninstallCodexHooksAtHome(profile[@"home"]);
492
+ if (status != 0) return status;
493
+ if (bridgeFailed) {
494
+ CCPetsSetProfileNeedsRepair(profileID, YES, NULL);
495
+ return 1;
496
+ }
497
+ NSString *error = nil;
498
+ if (!CCPetsForgetProfile(profileID, &error)) {
499
+ CCPetsPrintLine(stderr, error); return 1;
500
+ }
501
+ return 0;
502
+ }
503
+ if ([command isEqualToString:@"forget"]) {
504
+ NSString *error = nil;
505
+ if (!CCPetsForgetProfile(ValueAfter(arguments, @"--id"), &error)) {
506
+ CCPetsPrintLine(stderr, error); return 1;
507
+ }
508
+ return 0;
509
+ }
510
+ CCPetsPrintLine(stderr, @"Usage: cc-pets profile list|resolve|migrate|ensure-defaults|add|update|repair|repair-all|remove");
511
+ return 2;
512
+ } @catch (NSException *exception) {
513
+ CCPetsPrintLine(stderr, exception.reason ?: exception.name);
514
+ return 1;
515
+ }
516
+ }
@@ -197,6 +197,27 @@ NSArray<NSString *> *TerminalFocusBundleCandidates(NSDictionary *target) {
197
197
  return candidates;
198
198
  }
199
199
 
200
+ // 没有脚本接口的宿主(JetBrains、VS Code 等)只能激活到应用这一级。activateWithOptions:
201
+ // 在 macOS 14 起走协作式激活:状态卡是 NonactivatingPanel,桌宠自己不在前台,请求可能
202
+ // 被系统拒绝,窗口在别的桌面时更是不会切过去。改走和点 Dock 图标同一条路,让系统负责
203
+ // 切到应用窗口所在的桌面;拿不到 bundleURL 时才退回旧接口。
204
+ static BOOL ActivateApplicationAcrossSpaces(NSRunningApplication *application) {
205
+ NSURL *bundleURL = application.bundleURL;
206
+ if (bundleURL) {
207
+ NSWorkspaceOpenConfiguration *configuration = [NSWorkspaceOpenConfiguration configuration];
208
+ configuration.activates = YES;
209
+ configuration.addsToRecentItems = NO;
210
+ [NSWorkspace.sharedWorkspace openApplicationAtURL:bundleURL configuration:configuration
211
+ completionHandler:nil];
212
+ return YES;
213
+ }
214
+ #pragma clang diagnostic push
215
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
216
+ return [application activateWithOptions:
217
+ NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps];
218
+ #pragma clang diagnostic pop
219
+ }
220
+
200
221
  BOOL ActivateTerminalFocusTarget(NSDictionary *target) {
201
222
  NSString *tty = SanitizedShortString(target[@"tty"], 64);
202
223
  // 候选按优先级往下试,没在运行的直接跳过:映射值和捕获值总有一个指向真正承载
@@ -206,11 +227,7 @@ BOOL ActivateTerminalFocusTarget(NSDictionary *target) {
206
227
  NSRunningApplication *application =
207
228
  [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID].firstObject;
208
229
  if (!application) continue;
209
- #pragma clang diagnostic push
210
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
211
- return [application activateWithOptions:
212
- NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps];
213
- #pragma clang diagnostic pop
230
+ return ActivateApplicationAcrossSpaces(application);
214
231
  }
215
232
  return NO;
216
233
  }
@@ -311,8 +328,9 @@ int RegisterCodexLaunch(void) {
311
328
 
312
329
  // 会话记录按 sessions/YYYY/MM/DD/rollout-<时间>-<会话 ID>.jsonl 存放,目录名字典序即时间序。
313
330
  // 刚开始收事件的会话一定在最近几天的目录里;resume 的旧会话找不到也没关系,按工作目录兜底。
314
- static NSString *CodexRolloutPathForThread(NSString *thread) {
315
- NSString *sessions = [DefaultCodexHomeDirectory() stringByAppendingPathComponent:@"sessions"];
331
+ static NSString *CodexRolloutPathForThread(NSString *thread, NSString *codexHome) {
332
+ NSString *sessions = [(codexHome.length > 0 ? codexHome : DefaultCodexHomeDirectory())
333
+ stringByAppendingPathComponent:@"sessions"];
316
334
  NSFileManager *files = NSFileManager.defaultManager;
317
335
  NSString *suffix = [NSString stringWithFormat:@"-%@.jsonl", thread];
318
336
  NSUInteger examinedDays = 0;
@@ -337,10 +355,12 @@ static NSString *CodexRolloutPathForThread(NSString *thread) {
337
355
  }
338
356
 
339
357
  // 第一行是 session_meta,带 cwd 和创建时刻;它里面有完整的系统提示词,可能很长,读到换行为止。
340
- static NSDictionary *CodexThreadMeta(NSString *thread, NSDictionary *payload) {
358
+ static NSDictionary *CodexThreadMeta(NSString *thread, NSDictionary *payload, NSString *codexHome) {
341
359
  NSString *path = [payload[@"transcript_path"] isKindOfClass:NSString.class]
342
360
  ? payload[@"transcript_path"] : nil;
343
- if (![NSFileManager.defaultManager fileExistsAtPath:path]) path = CodexRolloutPathForThread(thread);
361
+ if (![NSFileManager.defaultManager fileExistsAtPath:path]) {
362
+ path = CodexRolloutPathForThread(thread, codexHome);
363
+ }
344
364
  NSFileHandle *handle = path ? [NSFileHandle fileHandleForReadingAtPath:path] : nil;
345
365
  NSMutableData *line = [NSMutableData data];
346
366
  while (handle && line.length < 8 * 1024 * 1024) {
@@ -404,7 +424,8 @@ static NSString *ChooseCodexLaunch(NSDictionary<NSString *, NSDictionary *> *lau
404
424
  return byLaunch.lastObject;
405
425
  }
406
426
 
407
- NSDictionary *CodexLaunchTerminalForThread(NSString *thread, NSDictionary *payload) {
427
+ static NSDictionary *CodexLaunchTerminalForThreadAtHome(NSString *thread, NSDictionary *payload,
428
+ NSString *codexHome) {
408
429
  if (thread.length == 0) return @{};
409
430
  int lock = LockCodexLaunchRegistry();
410
431
  if (lock < 0) return @{};
@@ -417,7 +438,7 @@ NSDictionary *CodexLaunchTerminalForThread(NSString *thread, NSDictionary *paylo
417
438
  }
418
439
  }
419
440
  if (!terminal) {
420
- NSDictionary *meta = CodexThreadMeta(thread, payload);
441
+ NSDictionary *meta = CodexThreadMeta(thread, payload, codexHome);
421
442
  NSString *key = meta[@"cwd"] ? ChooseCodexLaunch(launches, meta[@"cwd"], meta[@"createdAt"]) : nil;
422
443
  if (key) {
423
444
  NSMutableDictionary *entry = [launches[key] mutableCopy];
@@ -434,8 +455,17 @@ NSDictionary *CodexLaunchTerminalForThread(NSString *thread, NSDictionary *paylo
434
455
  return [terminal isKindOfClass:NSDictionary.class] ? terminal : @{};
435
456
  }
436
457
 
458
+ NSDictionary *CodexLaunchTerminalForThread(NSString *thread, NSDictionary *payload) {
459
+ return CodexLaunchTerminalForThreadAtHome(thread, payload, DefaultCodexHomeDirectory());
460
+ }
461
+
437
462
  NSDictionary *TerminalFocusTargetForHook(NSDictionary *payload, NSString *provider) {
438
463
  if (!TerminalFocusRunsUnderSharedAgentHost()) return TerminalFocusTargetFromEnvironment();
439
464
  if (![provider isEqualToString:@"Codex"]) return @{};
440
- return CodexLaunchTerminalForThread(SanitizedShortString(payload[@"session_id"], 128), payload);
465
+ // 是路径,不能走 SanitizedShortString:它会删掉 `/`,拼出一个不存在的目录。
466
+ NSString *profileHome = NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_HOME"];
467
+ profileHome = [profileHome isKindOfClass:NSString.class] && profileHome.isAbsolutePath
468
+ ? profileHome.stringByStandardizingPath : nil;
469
+ return CodexLaunchTerminalForThreadAtHome(
470
+ SanitizedShortString(payload[@"session_id"], 128), payload, profileHome);
441
471
  }
@@ -0,0 +1,12 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ // `cc-pets pet <子命令>`:cc-pets 自己的素材仓库。
4
+ //
5
+ // 素材可以来自多个"源"(source),但一律落到 ~/.cc-pets/pets(CC_PETS_PETS_DIR 可覆盖),
6
+ // 不碰 ~/.petdex/pets 和 ~/.codex/pets——那两个目录是别的工具在写,混在一起就没法"只看自己装的宠物"。
7
+ //
8
+ // 目录名保持素材本名(boba 就是 boba),来源记在宠物目录内的 .source.json 里。这样桌宠端不需要知道
9
+ // "源"这回事,右键菜单里也不会出现 boba@petdex 这种名字;同名素材的冲突交给 --as 显式改名解决。
10
+ //
11
+ // arguments 不含 "pet" 本身。返回进程退出码。
12
+ int CCPetsPetStoreMain(NSArray<NSString *> *arguments);