cc-pets 2.3.0 → 2.5.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 +63 -7
- package/CC_BRIDGE.zh-CN.md +43 -5
- package/CHANGELOG.md +106 -0
- package/CHANGELOG.zh-CN.md +106 -0
- package/CONTRIBUTING.md +23 -1
- package/CONTRIBUTING.zh-CN.md +18 -1
- package/PROVIDER_PROTOCOL.md +5 -1
- package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
- package/README.md +177 -33
- package/README.zh-CN.md +137 -32
- package/Resources/AgentIcons/claude.png +0 -0
- package/Resources/AgentIcons/codex.icns +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.vsix +0 -0
- package/Resources/IDEPlugins/cc-pets-terminal.zip +0 -0
- package/Resources/zh-Hans.lproj/Localizable.strings +187 -35
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +47 -4
- package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +121 -20
- package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +8 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
- package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +14 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +6 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Settings.m +3 -6
- package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +118 -50
- package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +4 -1
- package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +4 -0
- package/Sources/CCPets/App/CCPetsAppDelegate.h +12 -2
- package/Sources/CCPets/App/CCPetsAppDelegate.m +4 -2
- package/Sources/CCPets/App/main.m +6 -0
- package/Sources/CCPets/Bridge/CCPetsBridge.m +45 -18
- package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +175 -68
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +14 -6
- package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +356 -110
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
- package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +247 -126
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +37 -9
- package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +385 -80
- package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.h +21 -0
- package/Sources/CCPets/Integrations/CCPetsIDEIntegration.m +503 -0
- package/Sources/CCPets/Integrations/CCPetsIntegrations.m +1 -1
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.h +5 -0
- package/Sources/CCPets/Integrations/CCPetsLaunchPreflight.m +367 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +8 -1
- package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +156 -25
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.h +17 -0
- package/Sources/CCPets/Integrations/CCPetsTerminalIntegration.m +64 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.h +23 -0
- package/Sources/CCPets/Integrations/CCPetsVSCodeIntegration.m +475 -0
- package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
- package/Sources/CCPets/Pet/PetView+Menu.m +11 -3
- package/Sources/CCPets/Pet/PetView.h +7 -0
- package/Sources/CCPets/Pet/PetView.m +12 -1
- package/Sources/CCPets/Support/CCPetsDoctor.m +26 -12
- package/Sources/CCPets/Support/CCPetsPreferences.h +12 -0
- package/Sources/CCPets/Support/CCPetsPreferences.m +52 -2
- package/Sources/CCPets/UI/CCPetsFormBuilder.h +2 -0
- package/Sources/CCPets/UI/CCPetsFormBuilder.m +18 -7
- package/Sources/CCPets/UI/CCPetsGlassView.h +26 -4
- package/Sources/CCPets/UI/CCPetsGlassView.m +40 -9
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
- package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
- package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +528 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
- package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +95 -0
- package/Sources/CCPets/UI/CCPetsSettingsPage.h +6 -1
- package/Sources/CCPets/UI/CCPetsSettingsPage.m +577 -66
- package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
- package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
- package/Sources/CCPets/UI/CCPetsUIStyle.h +5 -2
- package/Sources/CCPets/UI/CCPetsUIStyle.m +57 -8
- package/Sources/CCPets/UI/CCPetsUsageChartView.m +4 -0
- package/Sources/CCPets/UI/CCPetsUsagePage.m +23 -28
- package/Sources/CCPets/UI/CCPetsUsageStore.m +2 -2
- package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +5 -3
- package/Sources/CCPets/Usage/CCPetsUsage+CodexBackground.m +193 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Internal.h +1 -0
- package/Sources/CCPets/Usage/CCPetsUsage+Models.m +49 -11
- package/Sources/CCPets/Usage/CCPetsUsage.h +3 -2
- package/Sources/CCPets/Usage/CCPetsUsageMonitor.m +15 -17
- package/Sources/CCPets/Usage/QuotaDashboardView+Rendering.m +11 -29
- package/Sources/CCPets/Usage/QuotaDashboardView.m +128 -2
- package/THIRD_PARTY_NOTICES.md +7 -3
- package/THIRD_PARTY_NOTICES.zh-CN.md +5 -1
- package/bin/claude-with-pet +2 -0
- package/bin/codex-with-pet +2 -0
- package/package.json +5 -1
- package/plugins/jetbrains/README.md +62 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalBridge.java +379 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalIdentity.java +24 -0
- package/plugins/jetbrains/src/main/java/com/ccpets/terminal/TerminalStartup.java +12 -0
- package/plugins/jetbrains/src/main/resources/META-INF/plugin.xml +23 -0
- package/plugins/jetbrains/src/main/resources/META-INF/pluginIcon.svg +380 -0
- package/plugins/vscode/README.md +63 -0
- package/plugins/vscode/extension.js +210 -0
- package/plugins/vscode/package.json +18 -0
- package/scripts/build-idea-plugin.sh +101 -0
- package/scripts/build-vscode-extension.sh +112 -0
- package/scripts/build.sh +24 -2
- package/scripts/compile-cached.zsh +133 -0
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -8,9 +8,13 @@ is unrelated to any existing anime, game, or commercial mascot and is distribute
|
|
|
8
8
|
with the project code under the MIT License.
|
|
9
9
|
|
|
10
10
|
At runtime, the app may read locally installed Codex/ChatGPT and Claude application
|
|
11
|
-
icons for interface identification.
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
icons for interface identification. If unavailable, it uses bundled fallback assets
|
|
12
|
+
in `Resources/AgentIcons`: `claude.png` from https://claude.ai/apple-touch-icon.png
|
|
13
|
+
and `codex.icns` from the locally installed ChatGPT.app resource `icon-chatgpt.icns`.
|
|
14
|
+
These assets retain their respective owners' copyrights and trademarks; they are
|
|
15
|
+
not original CC Pets artwork and are not covered by this project's MIT License.
|
|
16
|
+
Attribution does not grant redistribution permission or imply endorsement by
|
|
17
|
+
Anthropic or OpenAI. Distribution must comply with the rights holders' terms.
|
|
14
18
|
|
|
15
19
|
Assets installed by users from PetDex or other sources are managed by those users
|
|
16
20
|
and are not part of this project distribution. Follow the applicable author,
|
|
@@ -6,7 +6,11 @@ CC Pets 发布包不捆绑 PetDex 下载的角色素材。内置的 `ByteMochi.w
|
|
|
6
6
|
原创默认素材,与任何现有动漫、游戏或商业吉祥物无关,并与项目代码一同按 MIT License 分发。
|
|
7
7
|
|
|
8
8
|
程序会在运行时尝试读取用户本机已安装的 Codex/ChatGPT 和 Claude 应用图标,仅用于界面识别;
|
|
9
|
-
|
|
9
|
+
读取失败时使用 `Resources/AgentIcons` 中随包分发的备用素材:`claude.png` 来自
|
|
10
|
+
https://claude.ai/apple-touch-icon.png,`codex.icns` 来自本机 ChatGPT.app 的
|
|
11
|
+
`icon-chatgpt.icns` 资源。这些素材的商标和版权归各自权利人所有,不是 CC Pets 原创素材,
|
|
12
|
+
也不适用本项目的 MIT License。标注来源不代表获得再分发授权,也不代表 Anthropic 或
|
|
13
|
+
OpenAI 为本项目背书;分发时仍须遵守权利人的使用条款。
|
|
10
14
|
|
|
11
15
|
用户通过 PetDex 或其他来源安装的素材由用户自行管理,不属于本项目发布内容。使用和再分发
|
|
12
16
|
此类素材时,请遵守对应作者、平台和权利人的许可证及使用条款。
|
package/bin/claude-with-pet
CHANGED
package/bin/codex-with-pet
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-pets",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "A native macOS desktop pet for Codex CLI and Claude Code",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Sunnyshiny",
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"scripts",
|
|
35
35
|
"Resources/Info.plist",
|
|
36
36
|
"Resources/AppIcon.icns",
|
|
37
|
+
"Resources/AgentIcons",
|
|
38
|
+
"Resources/IDEPlugins",
|
|
39
|
+
"plugins/jetbrains",
|
|
40
|
+
"plugins/vscode",
|
|
37
41
|
"Resources/phrases.default.*.txt",
|
|
38
42
|
"Resources/*.lproj/*",
|
|
39
43
|
"Sources/CCPets",
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# CC Pets Terminal for JetBrains IDEs
|
|
2
|
+
|
|
3
|
+
本地 macOS 插件,支持基于 IntelliJ 平台的 JetBrains IDE 2026.2 / 2026.3 的 **Reworked** 终端
|
|
4
|
+
(IntelliJ IDEA、PyCharm、WebStorm、GoLand、CLion、PhpStorm、RubyMine、Rider 等)。
|
|
5
|
+
Android Studio 的平台版本通常落后,需其基线达到 262 后才能安装。
|
|
6
|
+
Classic 终端、远程开发和分屏内子终端暂不作为支持范围。
|
|
7
|
+
|
|
8
|
+
## 安装
|
|
9
|
+
|
|
10
|
+
1. 在 Pet 的「设置 → 通用 → 终端跳转」找到对应 IDE,点「安装」并确认。
|
|
11
|
+
Pet 把随包 ZIP 解压到该 IDE 的插件目录 `<配置目录>/plugins/cc-pets-terminal`。
|
|
12
|
+
2. IDE 正在运行时该行显示「需要重启」,重启后加载。
|
|
13
|
+
3. 打开一个项目。Pet 会自动检测连接;无需填写端口或手动配对。
|
|
14
|
+
4. 在 Reworked 终端中启动 Agent 并触发新的 Hook 卡片,点击卡片跳转。
|
|
15
|
+
|
|
16
|
+
只在用户确认后写入,且只写 `cc-pets-terminal` 一个目录:先解压到同目录下的临时文件夹,
|
|
17
|
+
校验 jar 后再改名替换,失败时恢复旧版本。目标目录必须属于当前用户且不是符号链接;
|
|
18
|
+
IDE 从未启动过(配置目录不存在)或 `idea.properties` 自定义了 `idea.config.path` /
|
|
19
|
+
`idea.plugins.path` 时不自动安装,改为手动:Settings → Plugins → ⚙ → Install Plugin from Disk。
|
|
20
|
+
已连接过的 IDE 使用插件上报的真实插件目录。用户在 IDE 中禁用的插件保持禁用。
|
|
21
|
+
|
|
22
|
+
随 CC Pets 更新的 ZIP 版本更高时,该行显示「更新」,流程同安装。卸载或禁用通过 IDE 的
|
|
23
|
+
Plugins 设置完成。ZIP 尚未发布到 JetBrains Marketplace。
|
|
24
|
+
每个 IDE、稳定版和 EAP 的插件配置独立,需要分别安装。
|
|
25
|
+
|
|
26
|
+
## 工作方式
|
|
27
|
+
|
|
28
|
+
每个 IDE 进程注册独立的本机端点。新终端通过 `LocalTerminalCustomizer`
|
|
29
|
+
得到 `CC_PETS_IDE_INSTANCE_ID` 和 `CC_PETS_IDE_TERMINAL_ID`;Pet 的 Hook 记录保留
|
|
30
|
+
这两个值。插件按标识查找终端,范围包括终端工具窗口的标签页和「作为编辑器标签页打开」后
|
|
31
|
+
位于编辑器区(含拆出的独立窗口)的终端,再前置项目窗口、选择标签页并聚焦输入组件。
|
|
32
|
+
已打开的终端没有这些标识时,使用其启动 PID 的控制 TTY 精确匹配;不按标题或工作目录猜测。
|
|
33
|
+
若 PID 不可用、终端已关闭、匹配不唯一或 IDE 正显示模态对话框,返回失败并提示用户。
|
|
34
|
+
|
|
35
|
+
Codex 使用共享后台时,会话与终端的配对由 Pet 完成。多个客户端在同目录启动时,
|
|
36
|
+
Pet 参考本机 `logs_2.sqlite` 中 TUI 的就绪时刻与进程身份,避免网络初始化延迟导致串台。
|
|
37
|
+
它只查询启动元数据;该日志格式是 Codex 内部实现,缺失、变更或同时就绪造成歧义时
|
|
38
|
+
不会按“最近启动”猜测。显式 `codex resume <会话 ID>` 可直接按参数匹配。
|
|
39
|
+
|
|
40
|
+
注册文件位于 `~/Library/Application Support/CC Pets/ide-instances/`。
|
|
41
|
+
目录权限为 `0700`,文件为 `0600`,每个进程使用独立随机令牌。
|
|
42
|
+
通信仅监听 `127.0.0.1`,要求带 Bearer 令牌的 POST,并拒绝浏览器 Origin 请求。
|
|
43
|
+
仅提供状态查询和聚焦操作,不读取终端输出,不向终端输入命令。
|
|
44
|
+
Pet 不跟随重定向、不使用代理;过期注册通过进程路径和认证查询排除。
|
|
45
|
+
正常卸载/退出会清除注册,异常退出留下的文件不会被视为有效连接。
|
|
46
|
+
|
|
47
|
+
## 构建
|
|
48
|
+
|
|
49
|
+
使用 zsh、macOS 自带的 XML/ZIP 工具,以及已安装 IDEA 自带的 Java 25 编译模块,
|
|
50
|
+
无需 Python、Gradle 或下载依赖:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
./scripts/build-idea-plugin.sh --ide '/Applications/IntelliJ IDEA.app'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
输出为 `Resources/IDEPlugins/cc-pets-terminal.zip`,随 npm 包及 Pet 应用一起分发。
|
|
57
|
+
普通用户安装 Pet 时不需要 JDK;修改插件源码后,开发者需要重新生成此 ZIP。
|
|
58
|
+
可通过 `--output` 指定临时输出,例如检查另一个 IDEA SDK 的编译兼容性。
|
|
59
|
+
发布插件时同时更新 `plugin.xml` 和桥接状态中的版本号。
|
|
60
|
+
|
|
61
|
+
已在 IntelliJ IDEA 2026.2.3 和 2026.3 EAP 中完成 SDK 编译、静态检查,以及插件安装和
|
|
62
|
+
卡片跳转的人工验收。该项目默认不执行自动化测试。
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
package com.ccpets.terminal;
|
|
2
|
+
|
|
3
|
+
import com.google.gson.Gson;
|
|
4
|
+
import com.google.gson.JsonObject;
|
|
5
|
+
import com.google.gson.JsonParser;
|
|
6
|
+
import com.intellij.ide.plugins.IdeaPluginDescriptor;
|
|
7
|
+
import com.intellij.ide.plugins.PluginManagerCore;
|
|
8
|
+
import com.intellij.openapi.Disposable;
|
|
9
|
+
import com.intellij.openapi.application.ApplicationInfo;
|
|
10
|
+
import com.intellij.openapi.application.ApplicationManager;
|
|
11
|
+
import com.intellij.openapi.application.ModalityState;
|
|
12
|
+
import com.intellij.openapi.application.PathManager;
|
|
13
|
+
import com.intellij.openapi.diagnostic.Logger;
|
|
14
|
+
import com.intellij.openapi.fileEditor.FileEditorManager;
|
|
15
|
+
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
|
|
16
|
+
import com.intellij.openapi.extensions.PluginId;
|
|
17
|
+
import com.intellij.openapi.project.Project;
|
|
18
|
+
import com.intellij.openapi.project.ProjectManager;
|
|
19
|
+
import com.intellij.openapi.vfs.VirtualFile;
|
|
20
|
+
import com.intellij.openapi.wm.IdeFocusManager;
|
|
21
|
+
import com.intellij.openapi.wm.ToolWindow;
|
|
22
|
+
import com.intellij.openapi.wm.ToolWindowManager;
|
|
23
|
+
import com.intellij.openapi.wm.WindowManager;
|
|
24
|
+
import com.intellij.platform.eel.provider.EelProviderUtil;
|
|
25
|
+
import com.intellij.platform.eel.provider.LocalEelDescriptor;
|
|
26
|
+
import com.intellij.terminal.frontend.editor.TerminalViewVirtualFile;
|
|
27
|
+
import com.intellij.terminal.frontend.toolwindow.TerminalToolWindowTab;
|
|
28
|
+
import com.intellij.terminal.frontend.toolwindow.TerminalToolWindowTabsManager;
|
|
29
|
+
import com.intellij.terminal.frontend.toolwindow.TerminalToolWindowTabsManagerKt;
|
|
30
|
+
import com.intellij.terminal.frontend.view.TerminalView;
|
|
31
|
+
import com.intellij.ui.AppIcon;
|
|
32
|
+
import com.intellij.ui.content.Content;
|
|
33
|
+
import com.sun.net.httpserver.HttpExchange;
|
|
34
|
+
import com.sun.net.httpserver.HttpServer;
|
|
35
|
+
import org.jetbrains.plugins.terminal.session.TerminalStartupOptions;
|
|
36
|
+
|
|
37
|
+
import java.io.IOException;
|
|
38
|
+
import java.net.InetAddress;
|
|
39
|
+
import java.net.InetSocketAddress;
|
|
40
|
+
import java.nio.charset.StandardCharsets;
|
|
41
|
+
import java.nio.file.Files;
|
|
42
|
+
import java.nio.file.Path;
|
|
43
|
+
import java.nio.file.StandardCopyOption;
|
|
44
|
+
import java.nio.file.attribute.PosixFilePermissions;
|
|
45
|
+
import java.security.MessageDigest;
|
|
46
|
+
import java.util.ArrayList;
|
|
47
|
+
import java.util.HashSet;
|
|
48
|
+
import java.util.List;
|
|
49
|
+
import java.util.Map;
|
|
50
|
+
import java.util.Set;
|
|
51
|
+
import java.util.UUID;
|
|
52
|
+
import java.util.concurrent.ArrayBlockingQueue;
|
|
53
|
+
import java.util.concurrent.CompletableFuture;
|
|
54
|
+
import java.util.concurrent.ThreadPoolExecutor;
|
|
55
|
+
import java.util.concurrent.TimeUnit;
|
|
56
|
+
|
|
57
|
+
/** One authenticated loopback endpoint per IDE process; disposed on plugin unload. */
|
|
58
|
+
public final class TerminalBridge implements Disposable {
|
|
59
|
+
private static final Logger LOG = Logger.getInstance(TerminalBridge.class);
|
|
60
|
+
private static final Gson JSON = new Gson();
|
|
61
|
+
public final String instanceID = UUID.randomUUID().toString();
|
|
62
|
+
private final String token = UUID.randomUUID() + "-" + UUID.randomUUID();
|
|
63
|
+
private HttpServer server;
|
|
64
|
+
private ThreadPoolExecutor workers;
|
|
65
|
+
private Path registration;
|
|
66
|
+
private volatile boolean disposed;
|
|
67
|
+
|
|
68
|
+
public static TerminalBridge getInstance() {
|
|
69
|
+
return ApplicationManager.getApplication().getService(TerminalBridge.class);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public synchronized boolean start() {
|
|
73
|
+
if (disposed || !System.getProperty("os.name", "").startsWith("Mac")) return false;
|
|
74
|
+
if (server != null) return true;
|
|
75
|
+
try {
|
|
76
|
+
Path directory = Path.of(System.getProperty("user.home"), "Library", "Application Support",
|
|
77
|
+
"CC Pets", "ide-instances");
|
|
78
|
+
Files.createDirectories(directory);
|
|
79
|
+
if (Files.isSymbolicLink(directory)) throw new IOException("Invalid bridge directory");
|
|
80
|
+
Files.setPosixFilePermissions(directory, PosixFilePermissions.fromString("rwx------"));
|
|
81
|
+
server = HttpServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), 8);
|
|
82
|
+
workers = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(16), task -> {
|
|
83
|
+
Thread thread = new Thread(task, "CC Pets Terminal Bridge");
|
|
84
|
+
thread.setDaemon(true);
|
|
85
|
+
return thread;
|
|
86
|
+
});
|
|
87
|
+
server.setExecutor(workers);
|
|
88
|
+
server.createContext("/", this::handle);
|
|
89
|
+
server.start();
|
|
90
|
+
JsonObject record = status();
|
|
91
|
+
record.addProperty("port", server.getAddress().getPort());
|
|
92
|
+
record.addProperty("token", token);
|
|
93
|
+
record.addProperty("appPath", Path.of(PathManager.getHomePath()).getParent().toString());
|
|
94
|
+
record.addProperty("configPath", PathManager.getConfigPath());
|
|
95
|
+
record.addProperty("pluginsPath", PathManager.getPluginsPath());
|
|
96
|
+
registration = directory.resolve(instanceID + ".json");
|
|
97
|
+
Path temporary = Files.createTempFile(directory, ".registration-", ".tmp",
|
|
98
|
+
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
|
|
99
|
+
try {
|
|
100
|
+
Files.writeString(temporary, JSON.toJson(record));
|
|
101
|
+
Files.move(temporary, registration, StandardCopyOption.ATOMIC_MOVE);
|
|
102
|
+
} finally {
|
|
103
|
+
Files.deleteIfExists(temporary);
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
} catch (Exception error) {
|
|
107
|
+
LOG.warn("Could not start CC Pets terminal bridge", error);
|
|
108
|
+
stop();
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** CC Pets compares this with the version on disk to detect a pending restart. */
|
|
114
|
+
private static String pluginVersion() {
|
|
115
|
+
IdeaPluginDescriptor descriptor = PluginManagerCore.getPlugin(PluginId.getId("com.ccpets.terminal"));
|
|
116
|
+
return descriptor != null && descriptor.getVersion() != null ? descriptor.getVersion() : "unknown";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private JsonObject status() {
|
|
120
|
+
JsonObject record = new JsonObject();
|
|
121
|
+
record.addProperty("schemaVersion", 1);
|
|
122
|
+
record.addProperty("pluginVersion", pluginVersion());
|
|
123
|
+
record.addProperty("instanceID", instanceID);
|
|
124
|
+
record.addProperty("pid", ProcessHandle.current().pid());
|
|
125
|
+
record.addProperty("ideVersion", ApplicationInfo.getInstance().getFullVersion());
|
|
126
|
+
return record;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private void handle(HttpExchange exchange) throws IOException {
|
|
130
|
+
try (exchange) {
|
|
131
|
+
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
|
|
132
|
+
if (!"POST".equals(exchange.getRequestMethod()) ||
|
|
133
|
+
exchange.getRequestHeaders().containsKey("Origin") || authorization == null ||
|
|
134
|
+
!MessageDigest.isEqual(("Bearer " + token).getBytes(StandardCharsets.UTF_8),
|
|
135
|
+
authorization.getBytes(StandardCharsets.UTF_8))) {
|
|
136
|
+
reply(exchange, 403, Map.of("error", "unauthorized"));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
String path = exchange.getRequestURI().getPath();
|
|
140
|
+
if ("/status".equals(path)) {
|
|
141
|
+
reply(exchange, 200, status());
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (!"/focus".equals(path)) {
|
|
145
|
+
reply(exchange, 404, Map.of("error", "unknown_route"));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
byte[] body = exchange.getRequestBody().readNBytes(4097);
|
|
149
|
+
if (body.length > 4096) {
|
|
150
|
+
reply(exchange, 413, Map.of("error", "request_too_large"));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
JsonObject request = JsonParser.parseString(new String(body, StandardCharsets.UTF_8)).getAsJsonObject();
|
|
155
|
+
String terminalID = string(request, "terminalID");
|
|
156
|
+
String tty = string(request, "tty").replaceFirst("^/dev/", "");
|
|
157
|
+
if (!(terminalID.matches("[a-fA-F0-9-]{36}") || tty.matches("ttys[0-9]{1,8}"))) {
|
|
158
|
+
reply(exchange, 400, Map.of("error", "invalid_target"));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(3);
|
|
162
|
+
List<Target> targets = snapshot();
|
|
163
|
+
List<Target> matches = new ArrayList<>();
|
|
164
|
+
for (Target target : targets) {
|
|
165
|
+
if (System.nanoTime() > deadline) throw new java.util.concurrent.TimeoutException();
|
|
166
|
+
if (!terminalID.isEmpty()) {
|
|
167
|
+
if (terminalID.equals(target.terminalID)) matches.add(target);
|
|
168
|
+
} else if (tty.equals(ttyForProcess(target.pid))) {
|
|
169
|
+
matches.add(target);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (matches.size() != 1) {
|
|
173
|
+
reply(exchange, matches.isEmpty() ? 404 : 409,
|
|
174
|
+
Map.of("error", matches.isEmpty() ? "terminal_not_found" : "ambiguous_target"));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
long remaining = deadline - System.nanoTime();
|
|
178
|
+
if (remaining <= 0) throw new java.util.concurrent.TimeoutException();
|
|
179
|
+
boolean focused = focus(matches.getFirst(), deadline).get(remaining, TimeUnit.NANOSECONDS);
|
|
180
|
+
reply(exchange, focused ? 200 : 409, Map.of("focused", focused));
|
|
181
|
+
} catch (java.util.concurrent.TimeoutException error) {
|
|
182
|
+
reply(exchange, 503, Map.of("error", "ide_busy"));
|
|
183
|
+
} catch (IllegalArgumentException | IllegalStateException error) {
|
|
184
|
+
reply(exchange, 400, Map.of("error", "invalid_request"));
|
|
185
|
+
} catch (Exception error) {
|
|
186
|
+
LOG.warn("CC Pets terminal focus failed", error);
|
|
187
|
+
reply(exchange, 500, Map.of("error", "focus_failed"));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private static String string(JsonObject object, String key) {
|
|
193
|
+
return object.has(key) && object.get(key).isJsonPrimitive() ? object.get(key).getAsString() : "";
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Exactly one of tab (Terminal tool window) or file (moved to an editor tab) is set. */
|
|
197
|
+
private record Target(Project project, TerminalToolWindowTab tab, VirtualFile file, TerminalView view,
|
|
198
|
+
Long pid, String terminalID) {}
|
|
199
|
+
|
|
200
|
+
private static void addTarget(List<Target> targets, Project project, TerminalToolWindowTab tab,
|
|
201
|
+
VirtualFile file, TerminalView view) {
|
|
202
|
+
var options = view.getStartupOptionsDeferred();
|
|
203
|
+
if (!options.isCompleted() || options.isCancelled()) return;
|
|
204
|
+
TerminalStartupOptions startup = options.getCompleted();
|
|
205
|
+
targets.add(new Target(project, tab, file, view, startup.getPid(),
|
|
206
|
+
startup.getEnvVariables().getOrDefault("CC_PETS_IDE_TERMINAL_ID", "")));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Terminals shown in the editor area. Two mechanisms exist:
|
|
211
|
+
* - "Move to Editor" (2026.2+) opens a TerminalViewVirtualFile.
|
|
212
|
+
* - "Open as Editor Tab" (2026.3+) moves the tool window Content itself into a platform
|
|
213
|
+
* editor tab. The Content is read through editorContent() and mapped back with
|
|
214
|
+
* getTerminalTab(Content).
|
|
215
|
+
*/
|
|
216
|
+
private static void addEditorTargets(List<Target> targets, Project project) {
|
|
217
|
+
FileEditorManager editors = FileEditorManager.getInstance(project);
|
|
218
|
+
for (VirtualFile file : editors.getOpenFiles()) {
|
|
219
|
+
try {
|
|
220
|
+
if (file instanceof TerminalViewVirtualFile terminal) {
|
|
221
|
+
addTarget(targets, project, null, file, terminal.getTab().getView());
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
Content content = editorContent(project, file);
|
|
225
|
+
TerminalToolWindowTab tab = content == null ? null : TerminalToolWindowTabsManagerKt.getTerminalTab(content);
|
|
226
|
+
if (tab != null) addTarget(targets, project, null, file, tab.getView());
|
|
227
|
+
} catch (LinkageError | RuntimeException error) {
|
|
228
|
+
// Internal editor-tab API changed: tool window terminals keep working.
|
|
229
|
+
LOG.debug("Terminal editor tab skipped", error);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private static final String EDITOR_TAB_FILE = "com.intellij.openapi.wm.impl.tabInEditor.ToolWindowEditorTabFile";
|
|
235
|
+
private static final String EDITOR_TAB_MANAGER = "com.intellij.openapi.wm.impl.tabInEditor.ToolWindowEditorTabManager";
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The tool window Content shown by a 2026.3 "Open as Editor Tab" file:
|
|
239
|
+
* ToolWindowEditorTabManager.getInstance(project).getSession(file).getContent().
|
|
240
|
+
* All three are public, but the classes only exist from 2026.3, hence reflection.
|
|
241
|
+
*/
|
|
242
|
+
private static Content editorContent(Project project, VirtualFile file) {
|
|
243
|
+
if (!EDITOR_TAB_FILE.equals(file.getClass().getName())) return null;
|
|
244
|
+
try {
|
|
245
|
+
Class<?> managerClass = Class.forName(EDITOR_TAB_MANAGER, false, file.getClass().getClassLoader());
|
|
246
|
+
Object manager = managerClass.getMethod("getInstance", Project.class).invoke(null, project);
|
|
247
|
+
Object session = managerClass.getMethod("getSession", file.getClass()).invoke(manager, file);
|
|
248
|
+
if (session == null) return null;
|
|
249
|
+
Object content = session.getClass().getMethod("getContent").invoke(session);
|
|
250
|
+
return content instanceof Content value ? value : null;
|
|
251
|
+
} catch (ReflectiveOperationException | LinkageError error) {
|
|
252
|
+
LOG.debug("Terminal editor tab content unavailable", error);
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private List<Target> snapshot() throws Exception {
|
|
258
|
+
CompletableFuture<List<Target>> result = new CompletableFuture<>();
|
|
259
|
+
ApplicationManager.getApplication().invokeLater(() -> {
|
|
260
|
+
if (disposed || result.isDone()) return;
|
|
261
|
+
try {
|
|
262
|
+
List<Target> targets = new ArrayList<>();
|
|
263
|
+
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
|
|
264
|
+
if (project.isDisposed() ||
|
|
265
|
+
!(EelProviderUtil.getEelDescriptor(project) instanceof LocalEelDescriptor)) continue;
|
|
266
|
+
// Editor-area terminals first; a moved tab must not also match as a tool window tab.
|
|
267
|
+
int firstEditorTarget = targets.size();
|
|
268
|
+
addEditorTargets(targets, project);
|
|
269
|
+
Set<TerminalView> inEditor = new HashSet<>();
|
|
270
|
+
for (Target target : targets.subList(firstEditorTarget, targets.size())) inEditor.add(target.view);
|
|
271
|
+
for (TerminalToolWindowTab tab : TerminalToolWindowTabsManager.getInstance(project).getTabs()) {
|
|
272
|
+
if (!inEditor.contains(tab.getView())) addTarget(targets, project, tab, null, tab.getView());
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
result.complete(targets);
|
|
276
|
+
} catch (Throwable error) {
|
|
277
|
+
result.completeExceptionally(error);
|
|
278
|
+
}
|
|
279
|
+
}, ModalityState.nonModal());
|
|
280
|
+
try {
|
|
281
|
+
return result.get(800, TimeUnit.MILLISECONDS);
|
|
282
|
+
} finally {
|
|
283
|
+
result.cancel(false);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private static String ttyForProcess(Long pid) throws Exception {
|
|
288
|
+
if (pid == null || pid <= 0) return "";
|
|
289
|
+
Process process = new ProcessBuilder("/bin/ps", "-p", Long.toString(pid), "-o", "tty=").start();
|
|
290
|
+
try {
|
|
291
|
+
if (!process.waitFor(150, TimeUnit.MILLISECONDS)) return "";
|
|
292
|
+
return new String(process.getInputStream().readNBytes(128), StandardCharsets.UTF_8).trim();
|
|
293
|
+
} finally {
|
|
294
|
+
if (process.isAlive()) process.destroyForcibly();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private CompletableFuture<Boolean> focus(Target target, long deadline) {
|
|
299
|
+
CompletableFuture<Boolean> result = new CompletableFuture<>();
|
|
300
|
+
ApplicationManager.getApplication().invokeLater(() -> {
|
|
301
|
+
try {
|
|
302
|
+
if (disposed || target.project.isDisposed() || System.nanoTime() > deadline) {
|
|
303
|
+
result.complete(false);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (target.file != null) {
|
|
307
|
+
result.complete(focusEditor(target));
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
Content content = target.tab.getContent();
|
|
311
|
+
ToolWindow window = ToolWindowManager.getInstance(target.project).getToolWindow("Terminal");
|
|
312
|
+
if (content.getManager() == null || window == null ||
|
|
313
|
+
!TerminalToolWindowTabsManager.getInstance(target.project).getTabs().contains(target.tab)) {
|
|
314
|
+
result.complete(false);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
var frame = WindowManager.getInstance().getFrame(target.project);
|
|
318
|
+
if (frame != null) AppIcon.getInstance().requestFocus(frame);
|
|
319
|
+
window.activate(() -> {
|
|
320
|
+
try {
|
|
321
|
+
if (disposed || target.project.isDisposed() || content.getManager() == null ||
|
|
322
|
+
System.nanoTime() > deadline) {
|
|
323
|
+
result.complete(false);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
content.getManager().setSelectedContent(content, true);
|
|
327
|
+
IdeFocusManager.getInstance(target.project)
|
|
328
|
+
.requestFocus(target.tab.getView().getPreferredFocusableComponent(), true);
|
|
329
|
+
result.complete(true);
|
|
330
|
+
} catch (RuntimeException error) {
|
|
331
|
+
result.completeExceptionally(error);
|
|
332
|
+
}
|
|
333
|
+
}, true, true);
|
|
334
|
+
} catch (RuntimeException error) {
|
|
335
|
+
result.completeExceptionally(error);
|
|
336
|
+
}
|
|
337
|
+
}, ModalityState.nonModal());
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Runs on the EDT. Selects the editor tab (in whichever window holds it) and focuses its input. */
|
|
342
|
+
private static boolean focusEditor(Target target) {
|
|
343
|
+
FileEditorManager editors = FileEditorManager.getInstance(target.project);
|
|
344
|
+
if (!editors.isFileOpen(target.file)) return false;
|
|
345
|
+
var frame = WindowManager.getInstance().getFrame(target.project);
|
|
346
|
+
if (frame != null) AppIcon.getInstance().requestFocus(frame);
|
|
347
|
+
// Reuses the tab wherever it is open, including a detached editor window.
|
|
348
|
+
new OpenFileDescriptor(target.project, target.file).navigate(true);
|
|
349
|
+
var component = target.view.getPreferredFocusableComponent();
|
|
350
|
+
var window = javax.swing.SwingUtilities.getWindowAncestor(component);
|
|
351
|
+
if (window != null) window.toFront();
|
|
352
|
+
IdeFocusManager.getInstance(target.project).requestFocus(component, true);
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private static void reply(HttpExchange exchange, int code, Object body) throws IOException {
|
|
357
|
+
byte[] bytes = JSON.toJson(body).getBytes(StandardCharsets.UTF_8);
|
|
358
|
+
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
|
|
359
|
+
exchange.getResponseHeaders().set("Cache-Control", "no-store");
|
|
360
|
+
exchange.sendResponseHeaders(code, bytes.length);
|
|
361
|
+
exchange.getResponseBody().write(bytes);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private synchronized void stop() {
|
|
365
|
+
if (server != null) server.stop(0);
|
|
366
|
+
server = null;
|
|
367
|
+
if (workers != null) workers.shutdownNow();
|
|
368
|
+
workers = null;
|
|
369
|
+
if (registration != null) {
|
|
370
|
+
try { Files.deleteIfExists(registration); }
|
|
371
|
+
catch (IOException error) { LOG.warn("Could not remove CC Pets registration", error); }
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
@Override public void dispose() {
|
|
376
|
+
disposed = true;
|
|
377
|
+
stop();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package com.ccpets.terminal;
|
|
2
|
+
|
|
3
|
+
import com.intellij.openapi.project.Project;
|
|
4
|
+
import com.intellij.platform.eel.EelDescriptor;
|
|
5
|
+
import com.intellij.platform.eel.provider.LocalEelDescriptor;
|
|
6
|
+
import org.jetbrains.plugins.terminal.LocalTerminalCustomizer;
|
|
7
|
+
import java.util.List;
|
|
8
|
+
import java.util.Map;
|
|
9
|
+
import java.util.UUID;
|
|
10
|
+
|
|
11
|
+
/** Identity belongs to a terminal, not its title, working directory, or active window. */
|
|
12
|
+
@SuppressWarnings("deprecation")
|
|
13
|
+
public final class TerminalIdentity extends LocalTerminalCustomizer {
|
|
14
|
+
@Override public List<String> customizeCommandAndEnvironment(Project project, String directory,
|
|
15
|
+
List<String> command, Map<String, String> environment, EelDescriptor descriptor) {
|
|
16
|
+
if (!(descriptor instanceof LocalEelDescriptor)) return command;
|
|
17
|
+
TerminalBridge bridge = TerminalBridge.getInstance();
|
|
18
|
+
if (bridge.start()) {
|
|
19
|
+
environment.put("CC_PETS_IDE_INSTANCE_ID", bridge.instanceID);
|
|
20
|
+
environment.put("CC_PETS_IDE_TERMINAL_ID", UUID.randomUUID().toString());
|
|
21
|
+
}
|
|
22
|
+
return command;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
package com.ccpets.terminal;
|
|
2
|
+
|
|
3
|
+
import com.intellij.openapi.project.Project;
|
|
4
|
+
import com.intellij.openapi.startup.StartupActivity;
|
|
5
|
+
|
|
6
|
+
/** Also starts the bridge for terminals opened before this plugin was installed. */
|
|
7
|
+
@SuppressWarnings("deprecation")
|
|
8
|
+
public final class TerminalStartup implements StartupActivity.DumbAware {
|
|
9
|
+
@Override public void runActivity(Project project) {
|
|
10
|
+
TerminalBridge.getInstance().start();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<idea-plugin>
|
|
2
|
+
<id>com.ccpets.terminal</id>
|
|
3
|
+
<name>CC Pets Terminal</name>
|
|
4
|
+
<version>0.1.0</version>
|
|
5
|
+
<vendor url="https://github.com/Sunnyshinnny776/cc-pets">CC Pets</vendor>
|
|
6
|
+
<description><![CDATA[
|
|
7
|
+
Jump from a CC Pets agent card to its JetBrains IDE terminal tab.
|
|
8
|
+
Supports local Reworked terminals on macOS in IntelliJ-based IDEs 2026.2 and 2026.3
|
|
9
|
+
(IntelliJ IDEA, PyCharm, WebStorm, GoLand, CLion, PhpStorm, RubyMine, Rider, etc.).
|
|
10
|
+
Connects automatically to CC Pets over authenticated localhost communication.
|
|
11
|
+
Does not read terminal output or execute shell commands in your sessions.
|
|
12
|
+
]]></description>
|
|
13
|
+
<idea-version since-build="262" until-build="263.*"/>
|
|
14
|
+
<depends>com.intellij.modules.platform</depends>
|
|
15
|
+
<depends>org.jetbrains.plugins.terminal</depends>
|
|
16
|
+
<extensions defaultExtensionNs="com.intellij">
|
|
17
|
+
<applicationService serviceImplementation="com.ccpets.terminal.TerminalBridge"/>
|
|
18
|
+
<postStartupActivity implementation="com.ccpets.terminal.TerminalStartup"/>
|
|
19
|
+
</extensions>
|
|
20
|
+
<extensions defaultExtensionNs="org.jetbrains.plugins.terminal">
|
|
21
|
+
<localTerminalCustomizer implementation="com.ccpets.terminal.TerminalIdentity"/>
|
|
22
|
+
</extensions>
|
|
23
|
+
</idea-plugin>
|