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.
- package/CC_BRIDGE.md +5 -2
- package/CC_BRIDGE.zh-CN.md +4 -2
- package/CHANGELOG.md +60 -0
- package/CHANGELOG.zh-CN.md +60 -0
- package/CONTRIBUTING.md +13 -10
- package/CONTRIBUTING.zh-CN.md +11 -10
- package/README.md +113 -24
- package/README.zh-CN.md +89 -28
- package/Resources/zh-Hans.lproj/Localizable.strings +568 -17
- package/Sources/CCPets/{CCPetsAppDelegate+AgentEvents.m → App/CCPetsAppDelegate+AgentEvents.m} +15 -5
- package/Sources/CCPets/{CCPetsAppDelegate+AgentStatus.m → App/CCPetsAppDelegate+AgentStatus.m} +82 -23
- 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} +70 -22
- 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} +86 -98
- package/Sources/CCPets/{CCPetsAppDelegate+Speech.m → App/CCPetsAppDelegate+Speech.m} +8 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +139 -0
- package/Sources/CCPets/{CCPetsAppDelegate.h → App/CCPetsAppDelegate.h} +15 -0
- 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} +68 -1
- package/Sources/CCPets/{CCPetsBridge.h → Bridge/CCPetsBridge.h} +17 -4
- package/Sources/CCPets/{CCPetsBridge.m → Bridge/CCPetsBridge.m} +32 -17
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.h +10 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +792 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +60 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +558 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +24 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +501 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +103 -0
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +779 -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/CCPetsIntegrations.h +51 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +662 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.h +38 -0
- package/Sources/CCPets/Integrations/CCPetsProfiles.m +516 -0
- package/Sources/CCPets/{CCPetsTerminalFocus.m → Integrations/CCPetsTerminalFocus.m} +42 -12
- 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 +632 -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 +31 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +85 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.h +25 -0
- package/Sources/CCPets/UI/CCPetsDiagnosticsPage.m +440 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +42 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +268 -0
- package/Sources/CCPets/{CCPetsGlassView.h → UI/CCPetsGlassView.h} +5 -1
- package/Sources/CCPets/{CCPetsGlassView.m → UI/CCPetsGlassView.m} +25 -7
- package/Sources/CCPets/UI/CCPetsMainWindow.h +55 -0
- package/Sources/CCPets/UI/CCPetsMainWindow.m +730 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +62 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +917 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +132 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.m +816 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.h +15 -0
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +284 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.h +32 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +717 -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+CodexReader.m +392 -0
- package/Sources/CCPets/Usage/CCPetsUsage+CodexSessions.m +484 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +96 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +285 -0
- package/Sources/CCPets/Usage/CCPetsUsage+TokenTotals.m +655 -0
- package/Sources/CCPets/Usage/CCPetsUsage.h +57 -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} +163 -6
- 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 +616 -0
- package/Sources/CCPets/{QuotaDashboardView.h → Usage/QuotaDashboardView.h} +17 -0
- package/Sources/CCPets/Usage/QuotaDashboardView.m +587 -0
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/THIRD_PARTY_NOTICES.zh-CN.md +1 -1
- package/bin/cc-pets +8 -6
- package/bin/claude-with-pet +10 -2
- package/bin/codex-with-pet +13 -5
- package/package.json +1 -1
- package/scripts/build.sh +7 -3
- 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/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/{CCPetsTerminalFocus.h → Integrations/CCPetsTerminalFocus.h} +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,75 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// 命令行子命令(安装 / 卸载集成、CC Bridge、doctor、pet)共用的小工具。
|
|
4
|
+
// 这些入口原先是 Node 脚本,行为上尽量对齐 Node:$HOME 可被覆盖、路径按 path.resolve 规则折叠、
|
|
5
|
+
// 文案参数用 {name} 占位。
|
|
6
|
+
|
|
7
|
+
void CCPetsPrintLine(FILE *stream, NSString *text);
|
|
8
|
+
|
|
9
|
+
// 非空的环境变量值,空字符串视同未设置(与 Node 里 `process.env.X || 默认值` 一致)。
|
|
10
|
+
NSString *CCPetsEnvironmentValue(NSString *name);
|
|
11
|
+
|
|
12
|
+
// 用 $HOME 而不是 NSHomeDirectory():后者只看账户数据库,测试靠覆盖 HOME 做隔离。
|
|
13
|
+
NSString *CCPetsHomeDirectory(void);
|
|
14
|
+
|
|
15
|
+
// 对应 Node 的 path.resolve:补成绝对路径并折叠 . 和 ..,但不碰文件系统。
|
|
16
|
+
// 不用 stringByStandardizingPath:它会展开 ~,还会把 /private/tmp 改写成 /tmp。
|
|
17
|
+
NSString *CCPetsResolvePath(NSString *path);
|
|
18
|
+
NSString *CCPetsRealPath(NSString *path);
|
|
19
|
+
NSString *CCPetsShellQuote(NSString *value);
|
|
20
|
+
BOOL CCPetsDirectoryExists(NSString *path);
|
|
21
|
+
// home 开头的路径显示成 ~/…,输出常被贴进 issue。
|
|
22
|
+
NSString *CCPetsTildePath(NSString *path);
|
|
23
|
+
|
|
24
|
+
// 当前进程可执行文件的绝对路径(不解析软链)。
|
|
25
|
+
NSString *CCPetsExecutablePath(void);
|
|
26
|
+
long long CCPetsMilliseconds(void);
|
|
27
|
+
|
|
28
|
+
// 把译文里的 {name} 换成 values[name],values 里没有的占位符原样保留。
|
|
29
|
+
// 写法:CCPetsInterpolate(L(@"Removed {name}."), @{@"name": name}),L() 里是表的 key。
|
|
30
|
+
NSString *CCPetsInterpolate(NSString *text, NSDictionary<NSString *, id> *values);
|
|
31
|
+
|
|
32
|
+
#pragma mark - 外部 CLI
|
|
33
|
+
|
|
34
|
+
// 生效的配置目录:CLAUDE_CONFIG_DIR / CODEX_HOME 指向哪里就是哪里(会话级,安装与 Bridge 都按它)。
|
|
35
|
+
NSString *CCPetsClaudeHome(void);
|
|
36
|
+
NSString *CCPetsCodexHome(void);
|
|
37
|
+
NSString *CCPetsShimDirectory(void);
|
|
38
|
+
|
|
39
|
+
// PATH 里真正的 codex / claude,跳过 cc-pets 自己的 shim 目录与 *-with-pet 包装器——安装完成后
|
|
40
|
+
// shim 排在 PATH 最前面,直接 which 只会命中自己。realBinVariable(CODEX_REAL_BIN 等)设置时直接用它。
|
|
41
|
+
NSString *CCPetsFindRealExecutable(NSString *name, NSString *realBinVariable);
|
|
42
|
+
// 同上,但在给定的 PATH 字符串里找(主窗口用登录 shell 的 PATH 代替自己的)。
|
|
43
|
+
NSString *CCPetsFindRealExecutableInPath(NSString *name, NSString *realBinVariable, NSString *searchPath);
|
|
44
|
+
|
|
45
|
+
// 用户登录 shell(-l -i,会加载 nvm 等 rc 配置)里的 PATH。Finder 拉起的 App 只有 launchd 的最小 PATH,
|
|
46
|
+
// 看不到终端里装的 CLI。会起一个子进程(最多等 5 秒),不要在主线程调;拿不到时返回 nil。
|
|
47
|
+
NSString *CCPetsLoginShellPath(void);
|
|
48
|
+
|
|
49
|
+
// 这台机器上有没有这家 CLI:配置目录已存在,或 PATH 里有真二进制。CC_PETS_ASSUME_CLI=1 或设置了
|
|
50
|
+
// CODEX_REAL_BIN / CLAUDE_REAL_BIN 时视为已安装(探测漏判时的逃生舱)。
|
|
51
|
+
BOOL CCPetsClaudeCLIInstalled(void);
|
|
52
|
+
BOOL CCPetsCodexCLIInstalled(void);
|
|
53
|
+
|
|
54
|
+
#pragma mark - 文件
|
|
55
|
+
|
|
56
|
+
// 先写同目录临时文件再 rename,中途失败不会留下半份文件;临时文件一创建就是目标权限。
|
|
57
|
+
BOOL CCPetsWriteFileAtomically(NSString *path, NSString *text, mode_t mode, NSString **errorMessage);
|
|
58
|
+
|
|
59
|
+
// 解析已有的 JSON 配置(保序,见 CCPetsJSON.h)。文件不存在返回 nil 且不设 errorMessage;
|
|
60
|
+
// 解析失败返回 nil 并给出原因;顶层不是对象时返回 NSNull。
|
|
61
|
+
id CCPetsReadConfig(NSString *path, NSString **errorMessage);
|
|
62
|
+
|
|
63
|
+
#pragma mark - 子进程
|
|
64
|
+
|
|
65
|
+
// 跑一个外部命令并收集输出。executable 不含 / 时按 PATH 查找。返回退出码;
|
|
66
|
+
// 启动失败(找不到、没权限)返回 CCPetsRunLaunchFailed,超时被杀返回 CCPetsRunTimedOut。
|
|
67
|
+
// environment 为 nil 时继承当前环境;directory 为 nil 时继承当前目录。
|
|
68
|
+
extern const int CCPetsRunLaunchFailed;
|
|
69
|
+
extern const int CCPetsRunTimedOut;
|
|
70
|
+
int CCPetsRun(NSString *executable, NSArray<NSString *> *arguments, NSString *directory,
|
|
71
|
+
NSDictionary<NSString *, NSString *> *environment, NSTimeInterval timeout,
|
|
72
|
+
NSString **output, NSString **errorOutput);
|
|
73
|
+
|
|
74
|
+
// 脱离当前会话在后台启动,不等待、不收集输出。
|
|
75
|
+
BOOL CCPetsSpawnDetached(NSString *executable, NSArray<NSString *> *arguments);
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
#import "CCPetsCLISupport.h"
|
|
2
|
+
#import "CCPetsJSON.h"
|
|
3
|
+
#import <mach-o/dyld.h>
|
|
4
|
+
#import <sys/stat.h>
|
|
5
|
+
#import <sys/wait.h>
|
|
6
|
+
#import <fcntl.h>
|
|
7
|
+
#import <poll.h>
|
|
8
|
+
#import <signal.h>
|
|
9
|
+
#import <spawn.h>
|
|
10
|
+
#import <unistd.h>
|
|
11
|
+
#import <pwd.h>
|
|
12
|
+
#import <errno.h>
|
|
13
|
+
|
|
14
|
+
extern char **environ;
|
|
15
|
+
|
|
16
|
+
const int CCPetsRunLaunchFailed = -1;
|
|
17
|
+
const int CCPetsRunTimedOut = -2;
|
|
18
|
+
|
|
19
|
+
void CCPetsPrintLine(FILE *stream, NSString *text) {
|
|
20
|
+
fprintf(stream, "%s\n", text.UTF8String);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
NSString *CCPetsEnvironmentValue(NSString *name) {
|
|
24
|
+
NSString *value = NSProcessInfo.processInfo.environment[name];
|
|
25
|
+
return value.length > 0 ? value : nil;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
NSString *CCPetsHomeDirectory(void) {
|
|
29
|
+
return CCPetsEnvironmentValue(@"HOME") ?: NSHomeDirectory();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
NSString *CCPetsResolvePath(NSString *path) {
|
|
33
|
+
if (![path hasPrefix:@"/"]) {
|
|
34
|
+
path = [NSFileManager.defaultManager.currentDirectoryPath stringByAppendingFormat:@"/%@", path];
|
|
35
|
+
}
|
|
36
|
+
NSMutableArray<NSString *> *components = [NSMutableArray array];
|
|
37
|
+
for (NSString *component in [path componentsSeparatedByString:@"/"]) {
|
|
38
|
+
if (component.length == 0 || [component isEqualToString:@"."]) continue;
|
|
39
|
+
if ([component isEqualToString:@".."]) {
|
|
40
|
+
if (components.count > 0) [components removeLastObject];
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
[components addObject:component];
|
|
44
|
+
}
|
|
45
|
+
return [@"/" stringByAppendingString:[components componentsJoinedByString:@"/"]];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
NSString *CCPetsRealPath(NSString *path) {
|
|
49
|
+
char resolved[PATH_MAX];
|
|
50
|
+
if (!realpath(path.fileSystemRepresentation, resolved)) return nil;
|
|
51
|
+
return [NSString stringWithUTF8String:resolved];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
NSString *CCPetsShellQuote(NSString *value) {
|
|
55
|
+
return [NSString stringWithFormat:@"'%@'", [value stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
BOOL CCPetsDirectoryExists(NSString *path) {
|
|
59
|
+
BOOL isDirectory = NO;
|
|
60
|
+
return path.length > 0 && [NSFileManager.defaultManager fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
NSString *CCPetsTildePath(NSString *path) {
|
|
64
|
+
NSString *home = CCPetsHomeDirectory();
|
|
65
|
+
if (![path isKindOfClass:NSString.class]) return path;
|
|
66
|
+
if ([path isEqualToString:home]) return @"~";
|
|
67
|
+
if ([path hasPrefix:[home stringByAppendingString:@"/"]]) {
|
|
68
|
+
return [@"~" stringByAppendingString:[path substringFromIndex:home.length]];
|
|
69
|
+
}
|
|
70
|
+
return path;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
NSString *CCPetsExecutablePath(void) {
|
|
74
|
+
uint32_t size = PATH_MAX;
|
|
75
|
+
char buffer[PATH_MAX];
|
|
76
|
+
if (_NSGetExecutablePath(buffer, &size) != 0) return NSProcessInfo.processInfo.arguments.firstObject;
|
|
77
|
+
return CCPetsResolvePath([NSString stringWithUTF8String:buffer]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
long long CCPetsMilliseconds(void) {
|
|
81
|
+
return (long long)(NSDate.date.timeIntervalSince1970 * 1000.0);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
NSString *CCPetsInterpolate(NSString *text, NSDictionary<NSString *, id> *values) {
|
|
85
|
+
if (values.count == 0) return text;
|
|
86
|
+
static NSRegularExpression *placeholder;
|
|
87
|
+
static dispatch_once_t once;
|
|
88
|
+
dispatch_once(&once, ^{
|
|
89
|
+
placeholder = [NSRegularExpression regularExpressionWithPattern:@"\\{([A-Za-z][A-Za-z0-9_]*)\\}" options:0 error:nil];
|
|
90
|
+
});
|
|
91
|
+
NSMutableString *result = [NSMutableString string];
|
|
92
|
+
__block NSUInteger cursor = 0;
|
|
93
|
+
[placeholder enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length)
|
|
94
|
+
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
|
|
95
|
+
id value = values[[text substringWithRange:[match rangeAtIndex:1]]];
|
|
96
|
+
if (!value) return;
|
|
97
|
+
[result appendString:[text substringWithRange:NSMakeRange(cursor, match.range.location - cursor)]];
|
|
98
|
+
[result appendString:[value description]];
|
|
99
|
+
cursor = NSMaxRange(match.range);
|
|
100
|
+
}];
|
|
101
|
+
[result appendString:[text substringFromIndex:cursor]];
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
#pragma mark - 外部 CLI
|
|
106
|
+
|
|
107
|
+
NSString *CCPetsClaudeHome(void) {
|
|
108
|
+
return CCPetsEnvironmentValue(@"CLAUDE_CONFIG_DIR") ?: [CCPetsHomeDirectory() stringByAppendingPathComponent:@".claude"];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
NSString *CCPetsCodexHome(void) {
|
|
112
|
+
return CCPetsEnvironmentValue(@"CODEX_HOME") ?: [CCPetsHomeDirectory() stringByAppendingPathComponent:@".codex"];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
NSString *CCPetsShimDirectory(void) {
|
|
116
|
+
return CCPetsResolvePath(CCPetsEnvironmentValue(@"CC_PETS_SHIM_DIR") ?:
|
|
117
|
+
[CCPetsHomeDirectory() stringByAppendingPathComponent:@".cc-pets/shims"]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
NSString *CCPetsFindRealExecutable(NSString *name, NSString *realBinVariable) {
|
|
121
|
+
return CCPetsFindRealExecutableInPath(name, realBinVariable, CCPetsEnvironmentValue(@"PATH"));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
NSString *CCPetsFindRealExecutableInPath(NSString *name, NSString *realBinVariable, NSString *searchPath) {
|
|
125
|
+
NSString *forced = realBinVariable ? CCPetsEnvironmentValue(realBinVariable) : nil;
|
|
126
|
+
if (forced) return forced;
|
|
127
|
+
NSString *shim = CCPetsShimDirectory();
|
|
128
|
+
for (NSString *entry in [searchPath ?: @"" componentsSeparatedByString:@":"]) {
|
|
129
|
+
if (entry.length == 0) continue;
|
|
130
|
+
NSString *resolvedEntry = CCPetsRealPath(entry) ?: CCPetsResolvePath(entry);
|
|
131
|
+
if ([resolvedEntry isEqualToString:shim]) continue;
|
|
132
|
+
NSString *candidate = [entry stringByAppendingPathComponent:name];
|
|
133
|
+
struct stat info;
|
|
134
|
+
if (stat(candidate.fileSystemRepresentation, &info) != 0) continue;
|
|
135
|
+
if (!S_ISREG(info.st_mode) || (info.st_mode & 0111) == 0) continue;
|
|
136
|
+
// 复制而非软链安装时 shim 可能不在 CC_PETS_SHIM_DIR 里,按包装器的名字再兜一层。
|
|
137
|
+
NSString *real = CCPetsRealPath(candidate);
|
|
138
|
+
if (!real) continue;
|
|
139
|
+
if ([real.stringByDeletingLastPathComponent isEqualToString:shim]) continue;
|
|
140
|
+
if ([real.lastPathComponent hasSuffix:@"-with-pet"]) continue;
|
|
141
|
+
return candidate;
|
|
142
|
+
}
|
|
143
|
+
return nil;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
NSString *CCPetsLoginShellPath(void) {
|
|
147
|
+
NSString *shell = CCPetsEnvironmentValue(@"SHELL");
|
|
148
|
+
if (!shell) {
|
|
149
|
+
struct passwd *entry = getpwuid(getuid());
|
|
150
|
+
shell = entry && entry->pw_shell[0] ? @(entry->pw_shell) : @"/bin/zsh";
|
|
151
|
+
}
|
|
152
|
+
// rc 文件可能往 stdout 打欢迎语,用标记把 PATH 从噪声里切出来。
|
|
153
|
+
NSString *marker = @"__CC_PETS_PATH__";
|
|
154
|
+
NSString *script = [NSString stringWithFormat:@"printf '%%s%%s' '%@' \"$PATH\"", marker];
|
|
155
|
+
NSString *output = nil;
|
|
156
|
+
int status = CCPetsRun(shell, @[@"-l", @"-i", @"-c", script], nil, nil, 5, &output, NULL);
|
|
157
|
+
if (status == CCPetsRunLaunchFailed || status == CCPetsRunTimedOut) return nil;
|
|
158
|
+
NSRange range = [output rangeOfString:marker options:NSBackwardsSearch];
|
|
159
|
+
if (range.location == NSNotFound) return nil;
|
|
160
|
+
NSString *path = [[output substringFromIndex:NSMaxRange(range)]
|
|
161
|
+
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
162
|
+
return path.length > 0 ? path : nil;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static BOOL CLIForced(NSString *realBinVariable) {
|
|
166
|
+
return [CCPetsEnvironmentValue(@"CC_PETS_ASSUME_CLI") isEqualToString:@"1"] ||
|
|
167
|
+
CCPetsEnvironmentValue(realBinVariable) != nil;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
BOOL CCPetsClaudeCLIInstalled(void) {
|
|
171
|
+
return CLIForced(@"CLAUDE_REAL_BIN") || CCPetsDirectoryExists(CCPetsClaudeHome()) ||
|
|
172
|
+
CCPetsFindRealExecutable(@"claude", nil) != nil;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
BOOL CCPetsCodexCLIInstalled(void) {
|
|
176
|
+
return CLIForced(@"CODEX_REAL_BIN") || CCPetsDirectoryExists(CCPetsCodexHome()) ||
|
|
177
|
+
CCPetsFindRealExecutable(@"codex", nil) != nil;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#pragma mark - 文件
|
|
181
|
+
|
|
182
|
+
BOOL CCPetsWriteFileAtomically(NSString *path, NSString *text, mode_t mode, NSString **errorMessage) {
|
|
183
|
+
NSString *temporaryPath = [NSString stringWithFormat:@"%@.%d.cc-pets.tmp", path, getpid()];
|
|
184
|
+
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
|
|
185
|
+
int descriptor = open(temporaryPath.fileSystemRepresentation, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
|
186
|
+
BOOL written = descriptor >= 0;
|
|
187
|
+
const uint8_t *bytes = data.bytes;
|
|
188
|
+
NSUInteger offset = 0;
|
|
189
|
+
while (written && offset < data.length) {
|
|
190
|
+
ssize_t count = write(descriptor, bytes + offset, data.length - offset);
|
|
191
|
+
if (count < 0 && errno == EINTR) continue;
|
|
192
|
+
if (count <= 0) written = NO;
|
|
193
|
+
else offset += (NSUInteger)count;
|
|
194
|
+
}
|
|
195
|
+
int failure = written ? 0 : errno;
|
|
196
|
+
if (descriptor >= 0) close(descriptor);
|
|
197
|
+
if (written && rename(temporaryPath.fileSystemRepresentation, path.fileSystemRepresentation) != 0) {
|
|
198
|
+
failure = errno;
|
|
199
|
+
written = NO;
|
|
200
|
+
}
|
|
201
|
+
if (!written) {
|
|
202
|
+
unlink(temporaryPath.fileSystemRepresentation);
|
|
203
|
+
if (errorMessage) *errorMessage = [NSString stringWithFormat:@"%@: %s", path, strerror(failure)];
|
|
204
|
+
return NO;
|
|
205
|
+
}
|
|
206
|
+
chmod(path.fileSystemRepresentation, mode);
|
|
207
|
+
return YES;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
id CCPetsReadConfig(NSString *path, NSString **errorMessage) {
|
|
211
|
+
if (![NSFileManager.defaultManager fileExistsAtPath:path]) return nil;
|
|
212
|
+
NSError *readError = nil;
|
|
213
|
+
NSData *data = [NSData dataWithContentsOfFile:path options:0 error:&readError];
|
|
214
|
+
if (!data) {
|
|
215
|
+
if (errorMessage) *errorMessage = readError.localizedDescription;
|
|
216
|
+
return nil;
|
|
217
|
+
}
|
|
218
|
+
id parsed = CCPetsJSONParse(data, errorMessage);
|
|
219
|
+
if (!parsed) return nil;
|
|
220
|
+
return [parsed isKindOfClass:CCPetsJSONObject.class] ? parsed : NSNull.null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
#pragma mark - 子进程
|
|
224
|
+
|
|
225
|
+
static char **CStringArray(NSArray<NSString *> *strings) {
|
|
226
|
+
char **array = calloc(strings.count + 1, sizeof(char *));
|
|
227
|
+
for (NSUInteger index = 0; index < strings.count; index++) array[index] = strdup(strings[index].UTF8String);
|
|
228
|
+
return array;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static void FreeCStringArray(char **array) {
|
|
232
|
+
if (!array) return;
|
|
233
|
+
for (char **cursor = array; *cursor; cursor++) free(*cursor);
|
|
234
|
+
free(array);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
int CCPetsRun(NSString *executable, NSArray<NSString *> *arguments, NSString *directory,
|
|
238
|
+
NSDictionary<NSString *, NSString *> *environment, NSTimeInterval timeout,
|
|
239
|
+
NSString **output, NSString **errorOutput) {
|
|
240
|
+
int outPipe[2], errPipe[2];
|
|
241
|
+
if (pipe(outPipe) != 0) return CCPetsRunLaunchFailed;
|
|
242
|
+
if (pipe(errPipe) != 0) {
|
|
243
|
+
close(outPipe[0]);
|
|
244
|
+
close(outPipe[1]);
|
|
245
|
+
return CCPetsRunLaunchFailed;
|
|
246
|
+
}
|
|
247
|
+
posix_spawn_file_actions_t actions;
|
|
248
|
+
posix_spawn_file_actions_init(&actions);
|
|
249
|
+
posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0);
|
|
250
|
+
posix_spawn_file_actions_adddup2(&actions, outPipe[1], STDOUT_FILENO);
|
|
251
|
+
posix_spawn_file_actions_adddup2(&actions, errPipe[1], STDERR_FILENO);
|
|
252
|
+
posix_spawn_file_actions_addclose(&actions, outPipe[0]);
|
|
253
|
+
posix_spawn_file_actions_addclose(&actions, errPipe[0]);
|
|
254
|
+
if (directory) posix_spawn_file_actions_addchdir_np(&actions, directory.fileSystemRepresentation);
|
|
255
|
+
|
|
256
|
+
char **argv = CStringArray([@[executable] arrayByAddingObjectsFromArray:arguments]);
|
|
257
|
+
char **envp = NULL;
|
|
258
|
+
if (environment) {
|
|
259
|
+
NSMutableArray<NSString *> *pairs = [NSMutableArray array];
|
|
260
|
+
[environment enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
|
|
261
|
+
[pairs addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
|
|
262
|
+
}];
|
|
263
|
+
envp = CStringArray(pairs);
|
|
264
|
+
}
|
|
265
|
+
pid_t pid = 0;
|
|
266
|
+
int spawned = [executable containsString:@"/"]
|
|
267
|
+
? posix_spawn(&pid, executable.fileSystemRepresentation, &actions, NULL, argv, envp ?: environ)
|
|
268
|
+
: posix_spawnp(&pid, executable.UTF8String, &actions, NULL, argv, envp ?: environ);
|
|
269
|
+
posix_spawn_file_actions_destroy(&actions);
|
|
270
|
+
FreeCStringArray(argv);
|
|
271
|
+
FreeCStringArray(envp);
|
|
272
|
+
close(outPipe[1]);
|
|
273
|
+
close(errPipe[1]);
|
|
274
|
+
if (spawned != 0) {
|
|
275
|
+
close(outPipe[0]);
|
|
276
|
+
close(errPipe[0]);
|
|
277
|
+
return CCPetsRunLaunchFailed;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
NSMutableData *outData = [NSMutableData data];
|
|
281
|
+
NSMutableData *errData = [NSMutableData data];
|
|
282
|
+
struct pollfd descriptors[2] = {{outPipe[0], POLLIN, 0}, {errPipe[0], POLLIN, 0}};
|
|
283
|
+
NSDate *deadline = timeout > 0 ? [NSDate dateWithTimeIntervalSinceNow:timeout] : nil;
|
|
284
|
+
BOOL timedOut = NO;
|
|
285
|
+
int remaining = 2;
|
|
286
|
+
while (remaining > 0) {
|
|
287
|
+
int wait = -1;
|
|
288
|
+
if (deadline) {
|
|
289
|
+
NSTimeInterval left = deadline.timeIntervalSinceNow;
|
|
290
|
+
if (left <= 0) {
|
|
291
|
+
timedOut = YES;
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
wait = (int)(left * 1000) + 1;
|
|
295
|
+
}
|
|
296
|
+
int ready = poll(descriptors, 2, wait);
|
|
297
|
+
if (ready < 0 && errno == EINTR) continue;
|
|
298
|
+
if (ready < 0) break;
|
|
299
|
+
for (int index = 0; index < 2; index++) {
|
|
300
|
+
if (descriptors[index].fd < 0 || descriptors[index].revents == 0) continue;
|
|
301
|
+
char buffer[16384];
|
|
302
|
+
ssize_t count = read(descriptors[index].fd, buffer, sizeof buffer);
|
|
303
|
+
if (count > 0) {
|
|
304
|
+
[(index == 0 ? outData : errData) appendBytes:buffer length:(NSUInteger)count];
|
|
305
|
+
} else if (count == 0 || errno != EINTR) {
|
|
306
|
+
close(descriptors[index].fd);
|
|
307
|
+
descriptors[index].fd = -1;
|
|
308
|
+
remaining--;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (timedOut) kill(pid, SIGKILL);
|
|
313
|
+
for (int index = 0; index < 2; index++) {
|
|
314
|
+
if (descriptors[index].fd >= 0) close(descriptors[index].fd);
|
|
315
|
+
}
|
|
316
|
+
int status = 0;
|
|
317
|
+
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
|
|
318
|
+
if (output) *output = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding] ?: @"";
|
|
319
|
+
if (errorOutput) *errorOutput = [[NSString alloc] initWithData:errData encoding:NSUTF8StringEncoding] ?: @"";
|
|
320
|
+
if (timedOut) return CCPetsRunTimedOut;
|
|
321
|
+
if (WIFEXITED(status)) return WEXITSTATUS(status);
|
|
322
|
+
return 128 + (WIFSIGNALED(status) ? WTERMSIG(status) : 0);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
BOOL CCPetsSpawnDetached(NSString *executable, NSArray<NSString *> *arguments) {
|
|
326
|
+
posix_spawn_file_actions_t actions;
|
|
327
|
+
posix_spawn_file_actions_init(&actions);
|
|
328
|
+
posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0);
|
|
329
|
+
posix_spawn_file_actions_addopen(&actions, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
|
|
330
|
+
posix_spawn_file_actions_addopen(&actions, STDERR_FILENO, "/dev/null", O_WRONLY, 0);
|
|
331
|
+
posix_spawnattr_t attributes;
|
|
332
|
+
posix_spawnattr_init(&attributes);
|
|
333
|
+
posix_spawnattr_setflags(&attributes, POSIX_SPAWN_SETSID);
|
|
334
|
+
char **argv = CStringArray([@[executable] arrayByAddingObjectsFromArray:arguments]);
|
|
335
|
+
pid_t pid = 0;
|
|
336
|
+
int spawned = posix_spawn(&pid, executable.fileSystemRepresentation, &actions, &attributes, argv, environ);
|
|
337
|
+
posix_spawn_file_actions_destroy(&actions);
|
|
338
|
+
posix_spawnattr_destroy(&attributes);
|
|
339
|
+
FreeCStringArray(argv);
|
|
340
|
+
// 不等待:子进程脱离会话后由 launchd 收养,父进程退出时不会留下僵尸。
|
|
341
|
+
return spawned == 0;
|
|
342
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// Agent 事件的记录与状态归一化,同时是 --hook / --provider-event 两个子命令的实现。
|
|
4
|
+
NSString *SanitizedShortString(id value, NSUInteger maximumLength);
|
|
5
|
+
NSString *ProviderFromEnvironment(void);
|
|
6
|
+
NSString *NormalizedStateForEvent(NSString *event, BOOL failed);
|
|
7
|
+
// 在线会话的 key:provider + 账号 + tty。同一个终端先后跑过两个账号时,只看 provider + tty
|
|
8
|
+
// 会让已退出账号的记录借着另一个账号的 pid 文件一直"在线"。老版本 pid 文件没有账号,传空。
|
|
9
|
+
NSString *OnlineAgentSessionKey(id provider, id profileID, id tty);
|
|
10
|
+
// 会话列表展示用的状态:SessionStart 之后超过宽限仍无后续事件,按待机展示。
|
|
11
|
+
NSString *SessionDisplayState(NSString *state, NSTimeInterval timestamp, NSTimeInterval now);
|
|
12
|
+
BOOL AppendAgentEventRecord(NSDictionary *record);
|
|
13
|
+
BOOL HookValueIndicatesFailure(id value);
|
|
14
|
+
int RecordHookEvent(void);
|
|
15
|
+
int RecordProviderEvent(void);
|
|
@@ -89,6 +89,21 @@ NSString *NormalizedStateForEvent(NSString *event, BOOL failed) {
|
|
|
89
89
|
return @"thinking";
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
NSString *OnlineAgentSessionKey(id provider, id profileID, id tty) {
|
|
93
|
+
NSString *providerName = SanitizedShortString(provider, 32);
|
|
94
|
+
NSString *profile = SanitizedShortString(profileID, 128);
|
|
95
|
+
// 先取末段再清洗:清洗会去掉 "/","/dev/ttys003" 就成了 "devttys003"。
|
|
96
|
+
NSString *ttyName = [tty isKindOfClass:NSString.class]
|
|
97
|
+
? SanitizedShortString([(NSString *)tty lastPathComponent], 64) : @"";
|
|
98
|
+
if (providerName.length == 0 || ttyName.length == 0) return nil;
|
|
99
|
+
return [NSString stringWithFormat:@"%@|%@|%@", providerName, profile, ttyName];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
NSString *SessionDisplayState(NSString *state, NSTimeInterval timestamp, NSTimeInterval now) {
|
|
103
|
+
if ([state isEqualToString:@"starting"] && now - timestamp >= AgentStartingGraceInterval) return @"idle";
|
|
104
|
+
return state;
|
|
105
|
+
}
|
|
106
|
+
|
|
92
107
|
static const off_t AgentEventLogSizeLimit = 4 * 1024 * 1024;
|
|
93
108
|
|
|
94
109
|
BOOL AppendAgentEventRecord(NSDictionary *record) {
|
|
@@ -198,6 +213,10 @@ int RecordHookEvent(void) {
|
|
|
198
213
|
@"timestamp": @([NSDate.date timeIntervalSince1970])
|
|
199
214
|
} mutableCopy];
|
|
200
215
|
if (session.length > 0) record[@"session"] = session;
|
|
216
|
+
NSString *profileID = SanitizedShortString(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_ID"], 128);
|
|
217
|
+
NSString *profileName = SanitizedShortString(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_NAME"], 64);
|
|
218
|
+
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
219
|
+
if (profileName.length > 0) record[@"profileName"] = profileName;
|
|
201
220
|
NSDictionary *terminal = TerminalFocusTargetForHook(payload, provider);
|
|
202
221
|
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
203
222
|
AppendAgentEventRecord(record);
|
|
@@ -245,6 +264,10 @@ int RecordProviderEvent(void) {
|
|
|
245
264
|
@"state": state,
|
|
246
265
|
@"timestamp": @([NSDate.date timeIntervalSince1970])
|
|
247
266
|
} mutableCopy];
|
|
267
|
+
NSString *profileID = SanitizedShortString(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_ID"], 128);
|
|
268
|
+
NSString *profileName = SanitizedShortString(NSProcessInfo.processInfo.environment[@"CC_PETS_PROFILE_NAME"], 64);
|
|
269
|
+
if (profileID.length > 0) record[@"profileId"] = profileID;
|
|
270
|
+
if (profileName.length > 0) record[@"profileName"] = profileName;
|
|
248
271
|
NSDictionary *terminal = TerminalFocusTargetFromEnvironment();
|
|
249
272
|
if (terminal.count > 0) record[@"terminal"] = terminal;
|
|
250
273
|
if (!AppendAgentEventRecord(record)) {
|
|
@@ -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);
|