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
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
// CC Bridge 的用户选项:保存在开关文件(~/.cc-pets/bridge-enabled)里,
|
|
2
|
-
// 由 `cc-pets bridge enable / configure` 写入,桌宠菜单也通过这两个命令修改。
|
|
3
|
-
//
|
|
4
|
-
// codexApprove Codex 中免审批的工具(写入 config.toml 的带标记块)
|
|
5
|
-
// claudeAllow Claude Code 中免确认的工具(写入 settings.json 的 permissions.allow)
|
|
6
|
-
// wake 自动唤醒空闲会话;关闭后消息只进信箱,等对方下次收到用户输入时带入(省 token)
|
|
7
|
-
// editGuard 编辑他人预留的文件时暂停一次(首次拦截、重试放行)
|
|
8
|
-
|
|
9
|
-
import { isBridgeEnabled, readBridgeOptions } from "./store.mjs";
|
|
10
|
-
import { t } from "../i18n.mjs";
|
|
11
|
-
|
|
12
|
-
// 桌宠菜单按用途分组,而不是暴露 7 个工具名。
|
|
13
|
-
export const TOOL_GROUPS = {
|
|
14
|
-
view: ["list_agents", "list_reservations", "check_inbox"],
|
|
15
|
-
send: ["send_message"],
|
|
16
|
-
reserve: ["reserve_files", "release_files"],
|
|
17
|
-
name: ["set_name"]
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const ALL_TOOLS = Object.values(TOOL_GROUPS).flat();
|
|
21
|
-
|
|
22
|
-
export const DEFAULT_OPTIONS = Object.freeze({
|
|
23
|
-
codexApprove: [],
|
|
24
|
-
claudeAllow: [],
|
|
25
|
-
wake: true,
|
|
26
|
-
editGuard: true
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const uniqueTools = (tools) => ALL_TOOLS.filter((tool) => tools.includes(tool));
|
|
30
|
-
|
|
31
|
-
export const normalizeOptions = (raw = {}) => ({
|
|
32
|
-
codexApprove: Array.isArray(raw.codexApprove) ? uniqueTools(raw.codexApprove) : [],
|
|
33
|
-
claudeAllow: Array.isArray(raw.claudeAllow) ? uniqueTools(raw.claudeAllow) : [],
|
|
34
|
-
wake: raw.wake !== false,
|
|
35
|
-
editGuard: raw.editGuard !== false
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// 已开启时取保存的选项,否则取默认值。hook / watcher 每次都读,文件很小。
|
|
39
|
-
export const currentOptions = () => (isBridgeEnabled() ? normalizeOptions(readBridgeOptions()) : { ...DEFAULT_OPTIONS });
|
|
40
|
-
|
|
41
|
-
// "view,send_message" → 展开分组、校验工具名。空字符串表示清空。
|
|
42
|
-
export const parseToolList = (text, flag) => {
|
|
43
|
-
const tools = [];
|
|
44
|
-
const unknown = [];
|
|
45
|
-
for (const item of String(text ?? "").split(",").map((entry) => entry.trim()).filter(Boolean)) {
|
|
46
|
-
if (TOOL_GROUPS[item]) {
|
|
47
|
-
tools.push(...TOOL_GROUPS[item]);
|
|
48
|
-
} else if (ALL_TOOLS.includes(item)) {
|
|
49
|
-
tools.push(item);
|
|
50
|
-
} else {
|
|
51
|
-
unknown.push(item);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (unknown.length > 0) {
|
|
55
|
-
throw new Error(t("{flag} only accepts groups {TOOL_GROUPS} or tools {ALL_TOOLS}, got: {unknown}", { flag, TOOL_GROUPS: Object.keys(TOOL_GROUPS).join(", "), ALL_TOOLS: ALL_TOOLS.join(", "), unknown: unknown.join(", ") }));
|
|
56
|
-
}
|
|
57
|
-
return uniqueTools(tools);
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const parseSwitch = (text, flag) => {
|
|
61
|
-
const value = String(text ?? "").trim().toLowerCase();
|
|
62
|
-
if (["on", "true", "1", "yes"].includes(value)) return true;
|
|
63
|
-
if (["off", "false", "0", "no"].includes(value)) return false;
|
|
64
|
-
throw new Error(t("{flag} only accepts on / off, got: {text}", { flag, text }));
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// 在 base 之上叠加命令行里给出的选项;没给的保持原值。
|
|
68
|
-
export const mergeOptionFlags = (base, flags) => {
|
|
69
|
-
const next = { ...normalizeOptions(base) };
|
|
70
|
-
if (flags.approve !== undefined) {
|
|
71
|
-
const tools = parseToolList(flags.approve, "--approve");
|
|
72
|
-
next.codexApprove = tools;
|
|
73
|
-
next.claudeAllow = tools;
|
|
74
|
-
}
|
|
75
|
-
if (flags.codexApprove !== undefined) next.codexApprove = parseToolList(flags.codexApprove, "--codex-approve");
|
|
76
|
-
if (flags.claudeAllow !== undefined) next.claudeAllow = parseToolList(flags.claudeAllow, "--claude-allow");
|
|
77
|
-
if (flags.wake !== undefined) next.wake = parseSwitch(flags.wake, "--wake");
|
|
78
|
-
if (flags.editGuard !== undefined) next.editGuard = parseSwitch(flags.editGuard, "--edit-guard");
|
|
79
|
-
return next;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const describeOptions = (options) => [
|
|
83
|
-
t("Codex auto-approve: {codexApprove}", { codexApprove: options.codexApprove.length > 0 ? options.codexApprove.join(", ") : t("none") }),
|
|
84
|
-
t("Claude allow without asking: {claudeAllow}", { claudeAllow: options.claudeAllow.length > 0 ? options.claudeAllow.join(", ") : t("none") }),
|
|
85
|
-
t("Wake idle sessions: {wake}", { wake: options.wake ? t("on") : t("off") }),
|
|
86
|
-
t("Pause edits on reserved files: {editGuard}", { editGuard: options.editGuard ? t("on") : t("off") })
|
|
87
|
-
];
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
// 进程探测:存活判断、沿进程树找到所属的 Claude / Codex 进程。
|
|
2
|
-
//
|
|
3
|
-
// 身份判定不信任调用方自报——Codex 的 MCP 进程拿不到 CODEX_THREAD_ID,只能靠
|
|
4
|
-
// "我的祖先里哪个是 codex 进程"反查注册表;hook 进程也用同一套逻辑补记 pid,
|
|
5
|
-
// 两边落到同一个 pid 上才对得上。
|
|
6
|
-
|
|
7
|
-
import { execFileSync } from "node:child_process";
|
|
8
|
-
import fs from "node:fs";
|
|
9
|
-
import os from "node:os";
|
|
10
|
-
import path from "node:path";
|
|
11
|
-
|
|
12
|
-
export const isProcessAlive = (pid) => {
|
|
13
|
-
if (!Number.isInteger(pid) || pid <= 1) return false;
|
|
14
|
-
try {
|
|
15
|
-
process.kill(pid, 0);
|
|
16
|
-
return true;
|
|
17
|
-
} catch (error) {
|
|
18
|
-
return error.code === "EPERM";
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const processInfo = (pid) => {
|
|
23
|
-
try {
|
|
24
|
-
const output = execFileSync("/bin/ps", ["-o", "ppid=,command=", "-p", String(pid)],
|
|
25
|
-
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
26
|
-
const match = output.match(/^(\d+)\s+(.*)$/s);
|
|
27
|
-
return match ? { ppid: Number(match[1]), command: match[2] } : null;
|
|
28
|
-
} catch {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const processCommand = (pid) => processInfo(pid)?.command ?? null;
|
|
34
|
-
|
|
35
|
-
// CLI 进程的控制终端,如 "ttys003"。hook 进程自己的 stdin 是管道,只能看宿主 CLI 进程的。
|
|
36
|
-
export const processTty = (pid) => {
|
|
37
|
-
if (!Number.isInteger(pid) || pid <= 1) return null;
|
|
38
|
-
try {
|
|
39
|
-
const tty = execFileSync("/bin/ps", ["-o", "tty=", "-p", String(pid)],
|
|
40
|
-
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
41
|
-
if (!tty || tty === "??" || tty === "?") return null;
|
|
42
|
-
return tty.startsWith("tty") ? tty : `tty${tty}`;
|
|
43
|
-
} catch {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// 只看可执行文件本身,不看参数:参数里出现 "claude" / "codex" 的进程太多了
|
|
49
|
-
// (比如 tmux new-session "codex …"、编辑器打开的某个路径)。
|
|
50
|
-
const executableOf = (command) => (command || "").split(/\s+/)[0] || "";
|
|
51
|
-
|
|
52
|
-
const PROVIDER_MATCHERS = {
|
|
53
|
-
// Codex 的 npm 包是 node 包装器 + 原生二进制;hook 和 MCP 进程的直接祖先都是原生二进制,
|
|
54
|
-
// 取离自己最近的那个,两边才能落到同一个 pid。
|
|
55
|
-
Codex: (command) => {
|
|
56
|
-
const executable = executableOf(command);
|
|
57
|
-
return path.basename(executable) === "codex" || /\/codex-[a-z0-9-]+\/vendor\//.test(executable);
|
|
58
|
-
},
|
|
59
|
-
Claude: (command) => {
|
|
60
|
-
const executable = executableOf(command);
|
|
61
|
-
return path.basename(executable) === "claude" ||
|
|
62
|
-
/@anthropic-ai\/claude-code\//.test(executable) ||
|
|
63
|
-
/@anthropic-ai\/claude-code\//.test((command || "").split(/\s+/)[1] || "");
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const commandMatchesProvider = (provider, command) =>
|
|
68
|
-
Boolean(PROVIDER_MATCHERS[provider]?.(command));
|
|
69
|
-
|
|
70
|
-
// 返回离 startPid 最近的 Claude / Codex 祖先进程。取"最近"而不是"某一种":
|
|
71
|
-
// 在 Claude 的 Bash 里再起一个 codex,里面的 MCP 进程属于那个 codex,而不是外层 Claude。
|
|
72
|
-
export const findAgentAncestor = (startPid = process.ppid, maximumDepth = 12) => {
|
|
73
|
-
// 测试 / 诊断覆盖:"Codex:1234"。测试本身常跑在某个 Claude 会话的 Bash 里,
|
|
74
|
-
// 不覆盖的话会沿进程树认到那个真实会话头上。
|
|
75
|
-
const forced = (process.env.CC_BRIDGE_AGENT || "").match(/^(Claude|Codex):(\d+)$/);
|
|
76
|
-
if (forced) return { provider: forced[1], pid: Number(forced[2]) };
|
|
77
|
-
let pid = startPid;
|
|
78
|
-
for (let depth = 0; depth < maximumDepth && pid > 1; depth += 1) {
|
|
79
|
-
const info = processInfo(pid);
|
|
80
|
-
if (!info) return null;
|
|
81
|
-
for (const [provider, matches] of Object.entries(PROVIDER_MATCHERS)) {
|
|
82
|
-
if (matches(info.command)) return { provider, pid };
|
|
83
|
-
}
|
|
84
|
-
pid = info.ppid;
|
|
85
|
-
}
|
|
86
|
-
return null;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
// 会话是否在线:有 pid 时以"进程还在且仍是同一种 CLI"为准(防 pid 复用);
|
|
90
|
-
// 没有 pid 的记录退回到 6 小时活动窗口,与桌宠"没有 pid 文件就看最近事件"的思路一致。
|
|
91
|
-
const STALE_WITHOUT_PID_MS = 6 * 60 * 60 * 1000;
|
|
92
|
-
|
|
93
|
-
export const isSessionLive = (session, now = Date.now()) => {
|
|
94
|
-
if (!session || session.status === "gone") return false;
|
|
95
|
-
if (Number.isInteger(session.pid)) {
|
|
96
|
-
if (!isProcessAlive(session.pid)) return false;
|
|
97
|
-
const command = processCommand(session.pid);
|
|
98
|
-
return command === null || commandMatchesProvider(session.provider, command);
|
|
99
|
-
}
|
|
100
|
-
return now - (session.updatedAt || 0) < STALE_WITHOUT_PID_MS;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// 找真实的 codex 可执行文件,跳过 cc-pets 自己的 shim(与 bin/codex-with-pet 同一规则)。
|
|
104
|
-
export const resolveRealExecutable = (name, realBinVariable) => {
|
|
105
|
-
const forced = process.env[realBinVariable];
|
|
106
|
-
if (forced) return forced;
|
|
107
|
-
const shim = path.resolve(process.env.CC_PETS_SHIM_DIR || path.join(os.homedir(), ".cc-pets/shims"));
|
|
108
|
-
for (const entry of (process.env.PATH || "").split(path.delimiter)) {
|
|
109
|
-
if (!entry) continue;
|
|
110
|
-
let resolvedEntry;
|
|
111
|
-
try {
|
|
112
|
-
resolvedEntry = fs.realpathSync(entry);
|
|
113
|
-
} catch {
|
|
114
|
-
resolvedEntry = path.resolve(entry);
|
|
115
|
-
}
|
|
116
|
-
if (resolvedEntry === shim) continue;
|
|
117
|
-
const candidate = path.join(entry, name);
|
|
118
|
-
try {
|
|
119
|
-
const stats = fs.statSync(candidate);
|
|
120
|
-
if (!stats.isFile() || (stats.mode & 0o111) === 0) continue;
|
|
121
|
-
const real = fs.realpathSync(candidate);
|
|
122
|
-
if (path.dirname(real) === shim || /-with-pet$/.test(path.basename(real))) continue;
|
|
123
|
-
return candidate;
|
|
124
|
-
} catch {
|
|
125
|
-
// 断链或没权限的候选跳过。
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return null;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
// `codex queue` 是较新的 Codex 才有的子命令。旧版本把不认识的子命令当成交互式启动参数,
|
|
132
|
-
// `--help` 打出的是总帮助而不是报错,所以按帮助里有没有 --thread / --message 判断。
|
|
133
|
-
// 结果只用于提前提示:投递时 queue 失败本来就会退回信箱(core.mjs 的 deliver),
|
|
134
|
-
// 探测失败(超时、找不到 codex)一律算 unknown,不能因此拒绝开启 CC Bridge。
|
|
135
|
-
export const probeCodexQueue = (executable = resolveRealExecutable("codex", "CODEX_REAL_BIN")) => {
|
|
136
|
-
if (!executable || !fs.existsSync(executable)) return { status: "missing", executable: executable ?? null, version: null };
|
|
137
|
-
const run = (args) => {
|
|
138
|
-
try {
|
|
139
|
-
return execFileSync(executable, args,
|
|
140
|
-
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 10_000 });
|
|
141
|
-
} catch (error) {
|
|
142
|
-
if (error.code === "ETIMEDOUT" || error.code === "ENOENT" || error.code === "EACCES") return null;
|
|
143
|
-
return `${error.stdout || ""}${error.stderr || ""}`;
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
const version = (run(["--version"]) || "").trim().split("\n")[0] || null;
|
|
147
|
-
const help = run(["queue", "--help"]);
|
|
148
|
-
if (help === null) return { status: "unknown", executable, version };
|
|
149
|
-
const supported = help.includes("--thread") && help.includes("--message");
|
|
150
|
-
return { status: supported ? "supported" : "unsupported", executable, version };
|
|
151
|
-
};
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
// 文件预留:多个 Agent 在同一个仓库里工作时,先声明"我要改哪些文件",避免互相覆盖。
|
|
2
|
-
//
|
|
3
|
-
// 预留是建议性的,不锁文件。其他会话第一次编辑被预留的文件时,PreToolUse hook 会把这次编辑
|
|
4
|
-
// 拦下一次并说明原因;重试同一操作即放行,之后这处预留不再拦它("首次拦截、重试放行")。
|
|
5
|
-
// 只注入提醒不够:实测 Codex 的 additionalContext 在工具调用已经发出之后才到达,拦不住这次
|
|
6
|
-
// 编辑,模型也常常忽略。只记录路径模式和原因,不记录文件内容。
|
|
7
|
-
//
|
|
8
|
-
// 作用域是仓库根目录(git rev-parse --show-toplevel):不同仓库、同一仓库的不同 worktree
|
|
9
|
-
// 各自独立——它们是不同的工作副本,物理上不会互相覆盖。
|
|
10
|
-
|
|
11
|
-
import { execFileSync } from "node:child_process";
|
|
12
|
-
import crypto from "node:crypto";
|
|
13
|
-
import fs from "node:fs";
|
|
14
|
-
import path from "node:path";
|
|
15
|
-
import { describeSession, liveSessions } from "./core.mjs";
|
|
16
|
-
import { bridgeSubdirectory, readJson, writeJsonAtomic } from "./store.mjs";
|
|
17
|
-
import { t } from "../i18n.mjs";
|
|
18
|
-
|
|
19
|
-
export const DEFAULT_TTL_MINUTES = 30;
|
|
20
|
-
export const MAX_TTL_MINUTES = 8 * 60;
|
|
21
|
-
const MAX_PATTERNS = 50;
|
|
22
|
-
const MAX_PATTERN_LENGTH = 300;
|
|
23
|
-
const MAX_REASON_LENGTH = 200;
|
|
24
|
-
|
|
25
|
-
const GLOB_CHARACTERS = /[*?[\]{}]/;
|
|
26
|
-
|
|
27
|
-
export const repoRoot = (directory) => {
|
|
28
|
-
if (!directory) return null;
|
|
29
|
-
try {
|
|
30
|
-
return execFileSync("git", ["rev-parse", "--show-toplevel"],
|
|
31
|
-
{ cwd: directory, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim() || directory;
|
|
32
|
-
} catch {
|
|
33
|
-
return directory;
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// 把调用方给的路径(绝对、相对、glob 均可)规范成"相对仓库根、以 / 分隔"的模式。
|
|
38
|
-
// 越出仓库根的路径拒绝,防止预留变成对整个磁盘的声明。
|
|
39
|
-
export const normalizePattern = (root, baseDirectory, raw) => {
|
|
40
|
-
const text = String(raw ?? "").trim();
|
|
41
|
-
if (!text || text.length > MAX_PATTERN_LENGTH) return null;
|
|
42
|
-
const absolute = path.isAbsolute(text) ? path.normalize(text) : path.normalize(path.join(baseDirectory, text));
|
|
43
|
-
const relative = path.relative(root, absolute);
|
|
44
|
-
if (relative === "" ) return "**";
|
|
45
|
-
if (relative.startsWith("..") || path.isAbsolute(relative)) return null;
|
|
46
|
-
return relative.split(path.sep).join("/").replace(/\/+$/, "");
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const escapeRegExp = (text) => text.replace(/[.+^${}()|\\]/g, "\\$&");
|
|
50
|
-
|
|
51
|
-
// 支持 **、*、?;不带通配符的模式视为"这个文件,或这个目录下的一切"。
|
|
52
|
-
export const patternToRegExp = (pattern) => {
|
|
53
|
-
if (!GLOB_CHARACTERS.test(pattern)) {
|
|
54
|
-
return new RegExp(`^${escapeRegExp(pattern)}(?:/.*)?$`);
|
|
55
|
-
}
|
|
56
|
-
let source = "";
|
|
57
|
-
for (let index = 0; index < pattern.length; index += 1) {
|
|
58
|
-
const character = pattern[index];
|
|
59
|
-
if (character === "*" && pattern[index + 1] === "*") {
|
|
60
|
-
// "**/" 可以匹配零层目录:src/**/a.ts 也匹配 src/a.ts。
|
|
61
|
-
if (pattern[index + 2] === "/") {
|
|
62
|
-
source += "(?:.*/)?";
|
|
63
|
-
index += 2;
|
|
64
|
-
} else {
|
|
65
|
-
source += ".*";
|
|
66
|
-
index += 1;
|
|
67
|
-
}
|
|
68
|
-
} else if (character === "*") {
|
|
69
|
-
source += "[^/]*";
|
|
70
|
-
} else if (character === "?") {
|
|
71
|
-
source += "[^/]";
|
|
72
|
-
} else if (character === "[" || character === "]" || character === "{" || character === "}") {
|
|
73
|
-
source += `\\${character}`;
|
|
74
|
-
} else {
|
|
75
|
-
source += escapeRegExp(character);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return new RegExp(`^${source}$`);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const staticPrefix = (pattern) => {
|
|
82
|
-
const index = pattern.search(GLOB_CHARACTERS);
|
|
83
|
-
if (index < 0) return pattern;
|
|
84
|
-
const head = pattern.slice(0, index);
|
|
85
|
-
return head.slice(0, head.lastIndexOf("/") + 1);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
// 两个模式是否可能指向同一个文件。有一方是具体路径时精确匹配;两个都是 glob 时,
|
|
89
|
-
// 只比较静态目录前缀——宁可多报(src/**/*.ts 与 src/api/* 视为重叠),不能漏报。
|
|
90
|
-
export const patternsOverlap = (left, right) => {
|
|
91
|
-
const leftGlob = GLOB_CHARACTERS.test(left);
|
|
92
|
-
const rightGlob = GLOB_CHARACTERS.test(right);
|
|
93
|
-
if (!leftGlob || !rightGlob) {
|
|
94
|
-
return patternToRegExp(left).test(right) || patternToRegExp(right).test(left);
|
|
95
|
-
}
|
|
96
|
-
const leftPrefix = staticPrefix(left);
|
|
97
|
-
const rightPrefix = staticPrefix(right);
|
|
98
|
-
return leftPrefix.startsWith(rightPrefix) || rightPrefix.startsWith(leftPrefix);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
// ---------------------------------------------------------------------------
|
|
102
|
-
// 存储:一条预留 = 一个 (会话, 仓库, 模式),id 由三者派生,重复预留即续期。
|
|
103
|
-
|
|
104
|
-
const reservationDirectory = () => bridgeSubdirectory("reservations");
|
|
105
|
-
|
|
106
|
-
const reservationId = (session, root, pattern) =>
|
|
107
|
-
`r${crypto.createHash("sha1").update(`${session}\0${root}\0${pattern}`).digest("hex").slice(0, 16)}`;
|
|
108
|
-
|
|
109
|
-
const listAll = () => {
|
|
110
|
-
const directory = reservationDirectory();
|
|
111
|
-
let names;
|
|
112
|
-
try {
|
|
113
|
-
names = fs.readdirSync(directory);
|
|
114
|
-
} catch {
|
|
115
|
-
return [];
|
|
116
|
-
}
|
|
117
|
-
return names.filter((name) => name.endsWith(".json"))
|
|
118
|
-
.map((name) => readJson(path.join(directory, name))).filter(Boolean);
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const removeReservation = (id) => fs.rmSync(path.join(reservationDirectory(), `${id}.json`), { force: true });
|
|
122
|
-
|
|
123
|
-
// 过期的、持有者已不在线的预留顺手清掉,返回仍然有效的。
|
|
124
|
-
export const activeReservations = ({ root, now = Date.now(), sessions } = {}) => {
|
|
125
|
-
// PreToolUse 每次编辑都会走到这里;没有任何预留时不做进程探测。
|
|
126
|
-
const all = listAll();
|
|
127
|
-
if (all.length === 0) return [];
|
|
128
|
-
const live = new Map((sessions ?? liveSessions()).map((session) => [session.session, session]));
|
|
129
|
-
const active = [];
|
|
130
|
-
for (const reservation of all) {
|
|
131
|
-
const holder = live.get(reservation.session);
|
|
132
|
-
if (!holder || reservation.expiresAt <= now) {
|
|
133
|
-
removeReservation(reservation.id);
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
if (root && reservation.root !== root) continue;
|
|
137
|
-
active.push({ ...reservation, holder });
|
|
138
|
-
}
|
|
139
|
-
return active.sort((left, right) => left.createdAt - right.createdAt);
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
export const releaseSessionReservations = (sessionId) => {
|
|
143
|
-
for (const reservation of listAll()) {
|
|
144
|
-
if (reservation.session === sessionId) removeReservation(reservation.id);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const minutesLeft = (reservation, now = Date.now()) => Math.max(1, Math.round((reservation.expiresAt - now) / 60000));
|
|
149
|
-
|
|
150
|
-
export const describeReservation = (reservation, now = Date.now()) =>
|
|
151
|
-
t("{pattern} · {holder} · {minutes} min left", { pattern: reservation.pattern, holder: describeSession(reservation.holder), minutes: minutesLeft(reservation, now) }) +
|
|
152
|
-
(reservation.reason ? ` · ${reservation.reason}` : "");
|
|
153
|
-
|
|
154
|
-
// ---------------------------------------------------------------------------
|
|
155
|
-
|
|
156
|
-
export const reserveFiles = ({ self, paths, reason = "", ttlMinutes = DEFAULT_TTL_MINUTES, now = Date.now() }) => {
|
|
157
|
-
const list = Array.isArray(paths) ? paths : [paths];
|
|
158
|
-
if (list.length === 0 || list.length > MAX_PATTERNS) return { error: t("Reserve between 1 and {MAX_PATTERNS} paths at a time.", { MAX_PATTERNS }) };
|
|
159
|
-
const ttl = Number(ttlMinutes);
|
|
160
|
-
if (!Number.isFinite(ttl) || ttl < 1 || ttl > MAX_TTL_MINUTES) {
|
|
161
|
-
return { error: t("ttl_minutes must be between 1 and {MAX_TTL_MINUTES}.", { MAX_TTL_MINUTES }) };
|
|
162
|
-
}
|
|
163
|
-
const root = repoRoot(self.cwd);
|
|
164
|
-
if (!root) return { error: t("Can't determine the repository directory of the current session.") };
|
|
165
|
-
const patterns = [];
|
|
166
|
-
for (const raw of list) {
|
|
167
|
-
const pattern = normalizePattern(root, self.cwd, raw);
|
|
168
|
-
if (!pattern) return { error: t("Path {raw} is invalid or not inside the current repository {root}.", { raw, root }) };
|
|
169
|
-
if (!patterns.includes(pattern)) patterns.push(pattern);
|
|
170
|
-
}
|
|
171
|
-
const note = String(reason ?? "").trim().slice(0, MAX_REASON_LENGTH);
|
|
172
|
-
const others = activeReservations({ root, now }).filter((entry) => entry.session !== self.session);
|
|
173
|
-
const conflicts = [];
|
|
174
|
-
for (const pattern of patterns) {
|
|
175
|
-
const id = reservationId(self.session, root, pattern);
|
|
176
|
-
const file = path.join(reservationDirectory(), `${id}.json`);
|
|
177
|
-
// 续期保留已放行名单,否则持有者每续期一次,别人就会被重新拦一次。
|
|
178
|
-
const previous = readJson(file);
|
|
179
|
-
writeJsonAtomic(file, {
|
|
180
|
-
id, session: self.session, root, pattern, reason: note, createdAt: previous?.createdAt ?? now,
|
|
181
|
-
expiresAt: now + ttl * 60000, acknowledgedBy: previous?.acknowledgedBy ?? []
|
|
182
|
-
});
|
|
183
|
-
for (const other of others) {
|
|
184
|
-
if (patternsOverlap(pattern, other.pattern)) conflicts.push({ pattern, other });
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return { root, patterns, ttl, conflicts };
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
// 记下"这个会话已经被这处预留拦过一次",之后放行。
|
|
191
|
-
export const acknowledgeReservation = (reservationId, sessionId) => {
|
|
192
|
-
const file = path.join(reservationDirectory(), `${reservationId}.json`);
|
|
193
|
-
const record = readJson(file);
|
|
194
|
-
if (!record) return;
|
|
195
|
-
const acknowledged = new Set(record.acknowledgedBy ?? []);
|
|
196
|
-
if (acknowledged.has(sessionId)) return;
|
|
197
|
-
acknowledged.add(sessionId);
|
|
198
|
-
writeJsonAtomic(file, { ...record, acknowledgedBy: [...acknowledged] });
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
export const isAcknowledged = (reservation, sessionId) =>
|
|
202
|
-
Array.isArray(reservation.acknowledgedBy) && reservation.acknowledgedBy.includes(sessionId);
|
|
203
|
-
|
|
204
|
-
export const releaseFiles = ({ self, paths }) => {
|
|
205
|
-
const root = repoRoot(self.cwd);
|
|
206
|
-
const own = listAll().filter((entry) => entry.session === self.session && entry.root === root);
|
|
207
|
-
let targets = own;
|
|
208
|
-
if (Array.isArray(paths) && paths.length > 0) {
|
|
209
|
-
const wanted = new Set(paths.map((raw) => normalizePattern(root, self.cwd, raw)).filter(Boolean));
|
|
210
|
-
targets = own.filter((entry) => wanted.has(entry.pattern));
|
|
211
|
-
}
|
|
212
|
-
for (const entry of targets) removeReservation(entry.id);
|
|
213
|
-
return { root, released: targets.map((entry) => entry.pattern) };
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// PreToolUse 用:这些具体文件是否被其他在线会话预留。
|
|
217
|
-
export const findForeignReservations = ({ sessionId, cwd, filePaths, now = Date.now() }) => {
|
|
218
|
-
const root = repoRoot(cwd);
|
|
219
|
-
const others = activeReservations({ root, now }).filter((entry) => entry.session !== sessionId);
|
|
220
|
-
if (others.length === 0) return [];
|
|
221
|
-
const hits = [];
|
|
222
|
-
for (const filePath of filePaths) {
|
|
223
|
-
const relative = normalizePattern(root, cwd, filePath);
|
|
224
|
-
if (!relative || GLOB_CHARACTERS.test(relative)) continue;
|
|
225
|
-
for (const other of others) {
|
|
226
|
-
if (patternToRegExp(other.pattern).test(relative)) hits.push({ file: relative, reservation: other });
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return hits;
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
// 从编辑类工具的输入里找出要改的文件。
|
|
233
|
-
// - Claude:Edit / Write / MultiEdit 的 file_path,NotebookEdit 的 notebook_path;
|
|
234
|
-
// - Codex:apply_patch 的补丁正文里的 "*** Update/Add/Delete File: <path>" 与 "*** Move to: <path>"。
|
|
235
|
-
// Codex hook 里补丁放在哪个字段没有文档保证,所以对整个 tool_input 做文本匹配。
|
|
236
|
-
export const editedFilesFromToolInput = (toolInput) => {
|
|
237
|
-
const files = new Set();
|
|
238
|
-
if (toolInput && typeof toolInput === "object") {
|
|
239
|
-
for (const key of ["file_path", "notebook_path"]) {
|
|
240
|
-
if (typeof toolInput[key] === "string") files.add(toolInput[key]);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
const text = typeof toolInput === "string" ? toolInput : JSON.stringify(toolInput ?? "");
|
|
244
|
-
const patch = text.replace(/\\n/g, "\n");
|
|
245
|
-
for (const match of patch.matchAll(/\*\*\* (?:Update|Add|Delete) File: ([^\n"]+)/g)) files.add(match[1].trim());
|
|
246
|
-
for (const match of patch.matchAll(/\*\*\* Move to: ([^\n"]+)/g)) files.add(match[1].trim());
|
|
247
|
-
return [...files];
|
|
248
|
-
};
|