cc-pets 2.4.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.
Files changed (53) hide show
  1. package/CC_BRIDGE.md +46 -9
  2. package/CC_BRIDGE.zh-CN.md +29 -6
  3. package/CHANGELOG.md +55 -0
  4. package/CHANGELOG.zh-CN.md +56 -1
  5. package/CONTRIBUTING.md +1 -1
  6. package/CONTRIBUTING.zh-CN.md +1 -1
  7. package/PROVIDER_PROTOCOL.md +5 -1
  8. package/PROVIDER_PROTOCOL.zh-CN.md +4 -1
  9. package/README.md +69 -16
  10. package/README.zh-CN.md +54 -19
  11. package/Resources/zh-Hans.lproj/Localizable.strings +35 -11
  12. package/Sources/CCPets/App/CCPetsAppDelegate+AgentEvents.m +47 -4
  13. package/Sources/CCPets/App/CCPetsAppDelegate+AgentStatus.m +109 -14
  14. package/Sources/CCPets/App/CCPetsAppDelegate+Bridge.m +8 -6
  15. package/Sources/CCPets/App/CCPetsAppDelegate+MenuBar.m +86 -0
  16. package/Sources/CCPets/App/CCPetsAppDelegate+Private.h +13 -0
  17. package/Sources/CCPets/App/CCPetsAppDelegate+Quota.m +6 -1
  18. package/Sources/CCPets/App/CCPetsAppDelegate+Speech.m +5 -3
  19. package/Sources/CCPets/App/CCPetsAppDelegate+Update.m +4 -1
  20. package/Sources/CCPets/App/CCPetsAppDelegate+Workbench.m +4 -0
  21. package/Sources/CCPets/App/CCPetsAppDelegate.h +10 -0
  22. package/Sources/CCPets/App/CCPetsAppDelegate.m +4 -2
  23. package/Sources/CCPets/Bridge/CCPetsBridge.m +34 -12
  24. package/Sources/CCPets/Bridge/CCPetsBridgeCLI.m +100 -33
  25. package/Sources/CCPets/Bridge/CCPetsBridgeCore.h +12 -3
  26. package/Sources/CCPets/Bridge/CCPetsBridgeCore.m +317 -89
  27. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.h +0 -2
  28. package/Sources/CCPets/Bridge/CCPetsBridgeSetup.m +210 -95
  29. package/Sources/CCPets/Bridge/CCPetsBridgeStore.h +28 -3
  30. package/Sources/CCPets/Bridge/CCPetsBridgeStore.m +222 -35
  31. package/Sources/CCPets/Integrations/CCPetsEvents.h +5 -0
  32. package/Sources/CCPets/Integrations/CCPetsEvents.m +68 -12
  33. package/Sources/CCPets/Integrations/CCPetsIntegrations.m +1 -1
  34. package/Sources/CCPets/Integrations/CCPetsTerminalFocus.h +6 -0
  35. package/Sources/CCPets/Integrations/CCPetsTerminalFocus.m +54 -5
  36. package/Sources/CCPets/Pet/CCPetsPhrasesEditor.m +7 -0
  37. package/Sources/CCPets/Pet/PetView+Menu.m +9 -1
  38. package/Sources/CCPets/Pet/PetView.h +7 -0
  39. package/Sources/CCPets/Pet/PetView.m +12 -1
  40. package/Sources/CCPets/Support/CCPetsPreferences.h +10 -0
  41. package/Sources/CCPets/Support/CCPetsPreferences.m +18 -0
  42. package/Sources/CCPets/UI/CCPetsMenuBarIcon.h +16 -0
  43. package/Sources/CCPets/UI/CCPetsMenuBarIcon.m +70 -0
  44. package/Sources/CCPets/UI/CCPetsMenuBarPanel.h +18 -0
  45. package/Sources/CCPets/UI/CCPetsMenuBarPanel.m +528 -0
  46. package/Sources/CCPets/UI/CCPetsMenuBarSummary.h +51 -0
  47. package/Sources/CCPets/UI/CCPetsMenuBarSummary.m +95 -0
  48. package/Sources/CCPets/UI/CCPetsSettingsPage.h +4 -1
  49. package/Sources/CCPets/UI/CCPetsSettingsPage.m +16 -0
  50. package/Sources/CCPets/UI/CCPetsStatusItem.h +28 -0
  51. package/Sources/CCPets/UI/CCPetsStatusItem.m +147 -0
  52. package/Sources/CCPets/UI/CCPetsWorkbenchPages.m +5 -3
  53. package/package.json +1 -1
package/README.zh-CN.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="docs/images/cover.png" alt="CC Pets v2.4.0:终端精准跳转、启动保护与用量改进">
2
+ <img src="docs/images/cover.png" alt="CC Pets v2.5.0:菜单栏模式、会话展示与 CC Bridge 可靠性改进">
3
3
  </p>
4
4
 
5
5
  # CC Pets
@@ -9,16 +9,16 @@
9
9
  一个不依赖 Codex/Claude 桌面端的 macOS 原生桌宠。直接启动桌面端,或启动 Codex CLI / Claude Code CLI 时自动出现,
10
10
  并通过 Codex App Server 官方接口与 Claude Code 官方 status line 数据读取 5 小时额度、周额度和周重置时间。
11
11
 
12
- ## v2.4.0 新功能
12
+ ## v2.5.0 新功能
13
13
 
14
- - **终端精准跳转**:支持 VS Code 及其分支、JetBrains、Ghostty 和 iTerm2,
15
- 在设置页集中管理权限和终端扩展。
16
- - **启动保护与 CC Bridge**:可选 Shadowrocket 出口检查,会话身份匹配更严格,
17
- 未确认的信箱消息可恢复。
18
- - **用量与宠物管理**:修复 Codex Token 计数、补充后台用量,按账号提醒低额度,
19
- 更新外观设置,并支持删除自定义宠物。
14
+ - **菜单栏模式**:保留桌宠、同时显示菜单栏或仅用菜单栏;查看会话与额度,
15
+ 有待审批会话时可直接跳到对应终端。
16
+ - **会话更清楚**:状态卡按会话区分,跟踪 Codex 桌面端的持久化聊天,
17
+ 会话结束时清理状态,不误播完成动画。
18
+ - **CC Bridge 可靠性**:逐次识别共享宿主上的调用者,改进并发文件预留和消息清理,
19
+ 保留配置中不属于 Bridge 的内容。
20
20
 
21
- 完整改动见 [更新记录](./CHANGELOG.zh-CN.md) 和 [Release Note](./docs/release-notes-v2.4.0.md)。
21
+ 完整改动见 [更新记录](./CHANGELOG.zh-CN.md) 和 [Release Note](./docs/release-notes-v2.5.0.md)。
22
22
 
23
23
  ## 界面预览
24
24
 
@@ -31,7 +31,7 @@
31
31
  - App 与 CLI 支持英语和简体中文,界面语言可即时切换
32
32
  - macOS 26 及以上可在经典与原生 Liquid Glass 两种面板主题之间切换
33
33
  - 随 Codex CLI 或 Claude Code CLI 自动启动,无需打开对应桌面版
34
- - 多个 Codex/Claude CLI 共用一个桌宠,最后一个 CLI 退出后桌宠自动关闭
34
+ - 多个 Codex/Claude CLI 共用一个桌宠,最后一个托管 CLI 退出且 Codex 桌面端未运行时,桌宠自动关闭
35
35
  - 鼠标悬停口袋时展开额度详情面板,用两张卡分别显示 Codex、Claude 的剩余 5 小时额度、周额度和精确到秒的周重置时间
36
36
  - reset 时间自动转换为 Mac 当前系统时区,例如 `Jul 22 3pm America/Los_Angeles` 在上海时区显示为 `7/23 06:00:00`
37
37
  - 支持待机、左右拖动、身体两侧感应、头部/脚部悬停和点击反馈动画
@@ -50,6 +50,7 @@
50
50
  - 可分别启用任务完成、失败、等待审批和响应超时的 macOS 系统通知
51
51
  - 内置一个原创素材,更多桌宠可从兼容 PetDex 的素材源按需下载到本机
52
52
  - 提供普通的主菜单:概览、会话与 CC Bridge 消息、按模型的 Token 用量、全部设置和内置诊断(见[主菜单](#主菜单))
53
+ - 可以常驻菜单栏:一键查看会话、各账号额度和今日用量,也可以完全隐藏桌宠(见[菜单栏](#菜单栏))
53
54
  - 使用原生 AppKit,运行时不依赖 Electron
54
55
 
55
56
  ## 主菜单
@@ -83,7 +84,7 @@ cc-pets open general # 还有 pet、accounts、alerts、diagnostics
83
84
  数据全部来自本机会话日志,不会发送到任何地方;按日归档让 Claude Code 清理旧转录之后,历史仍然留得住。
84
85
  - **多账号**:添加 Claude / Codex Home,查看账号的目录与集成状态,并通过“管理”菜单修复、重命名、重新定位或移除自定义 Home 的集成。
85
86
  - **宠物 / 提醒 / 设置**:桌宠的全部设置拆成三页(宠物:选宠物、确认删除未选中的自定义宠物、大小、碎碎念、点击互动、导入 Codex 素材;提醒:系统通知、桌面状态卡和系统状态;
86
- 设置:语言、面板外观和素材目录),另有更细的项(比如连击阈值、可以连续调节的玻璃压暗,菜单里仍是四档)。
87
+ 设置:语言、显示位置、面板外观和素材目录),另有更细的项(比如连击阈值、可以连续调节的玻璃压暗,菜单里仍是四档)。
87
88
  右键菜单只留最常用的几项:管理桌宠、宠物大小、显示消息气泡、用量展示模式、面板主题和 CC Bridge,其余都到这里改。改动会立刻作用到桌面上的宠物,在菜单里改的也会同步显示在这里。
88
89
  会改写 Claude Code / Codex 配置的选项(启用 CC Bridge、免审批)仍只在右键菜单里。
89
90
  - **诊断**:和 `cc-pets doctor` 同一套检查,要处理的问题置顶,每条带所属模块、修复命令和「复制命令」;
@@ -93,8 +94,30 @@ cc-pets open general # 还有 pet、accounts、alerts、diagnostics
93
94
  源码 checkout/worktree 显示「本地源码包」,npm 安装显示「本地 npm 包」,无法确认来源时显示「本地包」。
94
95
  这些标签不表示 npm 已发布该版本;线上版本由更新检查单独查询。
95
96
 
96
- 窗口在打开时才创建,关闭即释放,关着的时候没有任何后台任务。Dock 图标和菜单栏只在窗口打开期间出现,
97
- 窗口最小化后点 Dock 图标可以恢复。
97
+ 主菜单窗口在打开时才创建,关闭即释放,其页面关闭后不运行后台任务。Dock 图标和应用菜单只在窗口打开期间出现,
98
+ 窗口最小化后点 Dock 图标可以恢复。可选的菜单栏状态图标由「设置 → 显示位置」独立控制,不受主菜单窗口开关影响。
99
+
100
+ ### 菜单栏
101
+
102
+ 在 **主菜单 → 设置 → 显示位置** 选择 CC Pets 出现在哪里:**仅桌宠**(默认)、**桌宠 + 菜单栏**
103
+ 或 **仅菜单栏**。有菜单栏图标时:
104
+
105
+ - **左键**打开精简的玻璃面板,风格与悬停面板一致:宠物和它此刻在做什么;最多 5 个在线会话
106
+ (等待审批的置顶,点一行跳到对应终端);每个账号一行,显示 5 小时额度环、本周剩余和下次重置时间
107
+ (API 账号显示本月 Token 和调用次数;订阅账号排在前面);今日总用量;**显示桌宠**和**主菜单**。有新版本时这里也会
108
+ 出现「更新到 x.y.z…」。趋势、Token 明细和系统状态仍在悬停面板和主菜单里。点面板外或按 Esc 关闭。
109
+ - **有会话等待审批时**,左键改为直接跳到那个会话的终端(会话列表里排第一的那个,处理完再点就是下一个)。
110
+ 按住 ⌥ 点击仍然打开面板;取不到该会话的终端时也照常打开面板。
111
+ - **右键**(或按住 Control 点击)打开和右键桌宠相同的菜单,CC Bridge 开关和**退出**都在里面。
112
+ - 图标是一个带 `>_` 提示符的描边终端窗口,颜色与状态卡同步:待机为菜单栏原色,Agent 工作中为蓝色,
113
+ 任务完成为绿色,失败为红色。等待审批是唯一需要你动手的状态,显示为橙色 `•••`。
114
+ 状态卡回落待机(约 1 分钟没有新事件)时恢复菜单栏原色,因此隐藏桌宠时也能看到 Hook 动态。
115
+ - 图标旁用红色数字显示等待审批的会话数。打开 **菜单栏显示额度** 后会附上订阅账号中最低的
116
+ 5 小时剩余额度,20% 及以下为橙色,10% 及以下为红色;关掉开关后不显示数字,额度低时也一样。
117
+
118
+ **仅菜单栏** 会隐藏桌宠、状态卡、说话气泡、更新气泡和悬停面板。碎碎念直接跳过、不占每小时预算,
119
+ 因此低额度提醒只剩菜单栏数字变色。Agent 状态跟踪、额度采集和系统通知照常运行。
120
+ 最后一个托管 CLI 退出且 Codex 桌面端未运行时,菜单栏图标随桌宠一起消失;独立启动时会保持运行,直到手动退出。
98
121
 
99
122
  ## 互动方式
100
123
 
@@ -120,7 +143,7 @@ VS Code 1.80+ 及其分支(Cursor、Windsurf、VSCodium、Insiders 等)可
120
143
 
121
144
  | Agent 事件 | 桌宠反馈 |
122
145
  | --- | --- |
123
- | 新会话启动 | 挥手唤醒 |
146
+ | CLI 新会话启动 | 挥手唤醒 |
124
147
  | 提交用户提示、Agent 思考 | 思考动画 |
125
148
  | 执行 Shell 命令 | 跑动动画 |
126
149
  | 编辑或写入文件 | 工作动画 |
@@ -130,6 +153,7 @@ VS Code 1.80+ 及其分支(Cursor、Windsurf、VSCodium、Insiders 等)可
130
153
  | 工具执行成功 | 完成动画 |
131
154
  | 工具执行失败 | 失败动画 |
132
155
  | 当前回合或子 Agent 结束 | 告别后恢复待机 |
156
+ | 当前会话退出 | 切换到其他在线会话或恢复待机,不播放完成动画 |
133
157
 
134
158
  Agent 动画分别由 Codex Hooks 和 Claude Code Hooks 驱动。首次安装或 Codex Hook 内容更新后,启动 Codex 并执行
135
159
  `/hooks`,检查并信任 CC Pets Hook;未经信任的用户级 Hook 会被 Codex 安全机制跳过。
@@ -145,6 +169,10 @@ Claude Code Hook 会合并到 `~/.claude/settings.json`,现有的 `env`、`mod
145
169
  会回退到内核信息:从进程树解析控制终端和承载终端的应用,这些会话的状态卡同样可以点击回跳。
146
170
  这类会话没有 pid 文件,只要 Hook 还在持续发事件就会留在最近会话列表里,闲置 60 秒后移出列表。
147
171
 
172
+ Codex 桌面端的持久化聊天也会出现在会话列表并计入在线数量,按当前应用实例判断存活;后台临时任务不计入。
173
+ 重启桌宠可恢复近期桌面会话记录,不重播动画或通知,未完成的状态恢复为待机。收到会话结束事件后移除该聊天,
174
+ 并切换到其他在线会话或恢复待机;未收到结束事件时,聊天可能保留到桌面应用退出。桌面会话启动不播放 CLI 的启动动画。
175
+
148
176
  所有 Hook 状态卡都可以点击;右侧圆形状态图标会展开最近 8 个在线 Agent 终端会话。
149
177
  macOS Terminal 和 iTerm2 会按 TTY 精确选中对应标签页或 Session,Ghostty 1.4+ 还支持精确选择分屏。
150
178
  VS Code 和 JetBrains IDE 通过随包扩展精确选择终端;未安装扩展时,以及 Warp、WezTerm 和旧版 Ghostty,
@@ -177,7 +205,7 @@ Hook 状态卡和消息气泡在宠物小于 100% 时同比例缩小,文字、
177
205
  可在通透(0%)、轻度(25%)、标准(50%)、清晰(100%)之间切换,作用于额度面板、状态卡和
178
206
  说话气泡:档位越低越通透,档位越高在浅色背景上越容易看清。
179
207
 
180
- ### 启动前检查小火箭
208
+ ### 启动前检查 Shadowrocket
181
209
 
182
210
  在 **主菜单 → 设置 → 启动保护** 打开「Shadowrocket」开关(默认关闭)。
183
211
  启动保护按软件分别配置,当前提供 Shadowrocket,后续软件可在同一分组增加独立开关。
@@ -256,7 +284,9 @@ Agent 状态动画会优先播放。
256
284
 
257
285
  Agent 状态卡片会根据屏幕剩余空间自动显示在桌宠上方或下方。点击桌宠旁的圆形箭头可以
258
286
  折叠卡片;折叠后圆钮会显示当前活跃的 Codex/Claude CLI 会话数,点击后重新展开。卡片只展示
259
- Provider、脱敏状态和通用说明,不提供消息输入,也不会显示提示词、命令正文或模型输出。
287
+ 会话标签、脱敏状态和通用说明,不提供消息输入,也不会显示提示词、命令正文或模型输出。
288
+ 标题优先使用 CC Bridge 会话名:自动名称缩写为「目录名.动物」,自定义名称保持原样;
289
+ 没有 Bridge 名称时使用工作目录名,再回退到智能体名称。长标签保留末尾动物后缀,悬停可查看完整会话名和工作目录。
260
290
  连续 1 分钟没有收到新的 Agent 状态事件时,卡片和圆钮会一起自动隐藏;下一次收到事件时
261
291
  会重新显示并重新计时。
262
292
 
@@ -419,6 +449,11 @@ defaults delete com.universewang.cc-pets CCPetsSpeechCooldown # 交
419
449
  [CC Bridge](./CC_BRIDGE.zh-CN.md) 是默认关闭的可选功能,让本机 Claude Code / Codex 会话互发消息并预留文件。
420
450
  身份识别优先验证会话 ID;按 PID 回退时只接受唯一在线会话,共享宿主下身份不明确会拒绝操作。
421
451
  Codex 消息使用接收方的 Home 投递。
452
+ 多个 Codex 会话共用 app-server PID 时,Agent 在每次 MCP 调用中传入 Bridge hook 提供的当前 `session_id`。
453
+ 更新后需重新连接 MCP,以加载新的工具定义。
454
+
455
+ 中文显示会为自动生成的会话名附加动物别名,发送消息仍使用原始名称或 ref。
456
+ 没有持久化转录的 Codex 临时任务不注册为收件人,但仍执行文件预留检查。
422
457
 
423
458
  未确认的信箱消息可在异常退出后恢复。输出和确认之间崩溃时可能重复投递同一消息 ID;送达不代表已读或同意。
424
459
  关闭自动唤醒后,已运行的 watcher 也会停止主动取信;关闭 Bridge 后 CLI 拒绝发送、改名和收件。
@@ -656,7 +691,7 @@ API 用量模式只替换对应
656
691
  “调用次数”来自本机会话记录:Claude 按请求标识去重,Codex 按去重后的累计 Token 快照计数。
657
692
  某个窗口
658
693
  当前没有额度数据时该列标注"当前无活跃窗口"。卡片上的在线徽章表示该 Provider 是否还有
659
- 运行中的客户端(包装脚本写出的 pid 文件,或该 Provider 最近 60 秒内仍在发事件),与是否
694
+ 运行中的客户端(包装脚本写出的 pid 文件、当前 Codex 桌面应用实例中的持久化聊天,或该 Provider 最近 60 秒内仍在发事件),与是否
660
695
  已采集到额度数据无关;客户端退出后显示"离线",从未采集到数据时显示"等待数据"。经
661
696
  `codex-with-pet` / `claude-with-pet` 启动的会话退出后 3 秒内转为离线;直接运行 `codex` /
662
697
  `claude` 时没有可靠的退出信号,最多需要 60 秒静默才会转为离线。服务端的 Unix 时间戳会使用 Mac 当前系统时区
@@ -783,9 +818,9 @@ PetDex 官方 CLI(`npx petdex install <slug>`)会把素材放在 `~/.petdex/
783
818
  - Claude 额度只从 Claude Code 传给本机 status line 的 `rate_limits` 字段读取。
784
819
  - 为区分 API 账号和订阅账号,CC Pets 会读取各已登记 Home 中的认证配置(Claude 的 `settings.json`;Codex 的 `auth.json` 和 `config.toml`),并可能在本机运行该 Home 的 `claude auth status` / `codex login status`。只在内存中保留识别出的模式,不保存密钥、邮箱或命令输出。
785
820
  - 诊断页修复前,会把受影响的配置文件和脚本备份到 `~/Library/Application Support/CC Pets/diagnostic-backups/`(仅本人可读),备份只保存在本机。
786
- - Agent Hooks 只向当前用户的临时目录写入桌宠事件和额度缓存。
821
+ - Agent Hooks 向当前用户的临时目录写入脱敏状态事件和额度缓存;事件可包含工作目录、Home 和转录路径,用于会话标签与生命周期跟踪,不包含转录正文。
787
822
  - 额度历史默认关闭;启用后只在本机保留最近 7 天的剩余比例快照。
788
- - 状态气泡和通知只显示 Provider、状态及脱敏工具类别,不显示会话正文。
823
+ - 状态卡显示会话标签、状态及脱敏工具类别,悬停提示可显示完整会话名和工作目录;Agent 通知显示 Provider 和脱敏状态,不显示会话正文。
789
824
  - 项目不上传会话内容、额度、凭据或使用统计,也不包含遥测服务。
790
825
  - 桌宠启动时及 CLI 再次唤起桌宠时会向 npm Registry 查询最新版本号(同一次运行内最多每 10 分钟一次);仅在有新版本时再读取该版本 tag 中已提交的 Release Note,依次尝试 raw.githubusercontent.com、jsDelivr,最后回退到 GitHub Release API。请求不携带任何账号或使用数据。
791
826
  - 右键“退出桌宠”或执行 `cc-pets uninstall` 后,不会继续运行后台守护进程。
@@ -39,7 +39,7 @@
39
39
  "Disabled. The related hooks and MCP registrations were removed." = "已关闭,相关 hooks 与 MCP 注册已移除。";
40
40
  "Can't Enable Notifications" = "无法启用系统通知";
41
41
  "Allow notifications in System Settings → Notifications → CC Pets, then try again." = "请在“系统设置 → 通知 → CC Pets”中允许通知后重试。";
42
- "%1$@ → %2$@ and %3$lu more" = "%@ → %@ 等 %lu 条";
42
+ "%1$@ → %2$@ and %3$lu more" = "%@ → %@ 及另外 %lu 条";
43
43
  "New CC Bridge Message" = "CC Bridge 新消息";
44
44
  "CC Bridge Messages" = "CC Bridge 消息";
45
45
  "📬 %1$@ has %2$lu undelivered message (say anything in that terminal to deliver)" = "📬 %@ 有 %lu 条消息待投递(去那个终端说句话即可送达)";
@@ -393,7 +393,7 @@
393
393
  "When replying, put the from name from the message header into to." = "回复收到的消息时,把消息头里的 from 名字填到 to。";
394
394
  "Recipient session name (from list_agents); write \"name [ref]\" when names collide" = "收件会话名(来自 list_agents),重名时写成 \"名字 [ref]\"";
395
395
  "Message body. Say what it's about in the first sentence; don't rely on @file references, since attachments aren't delivered." = "消息正文。第一句写清楚这条消息是关于什么的;不要依赖 @文件 引用,对方收不到附件。";
396
- "When true, this session gets a one-time notice when the recipient next finishes a turn and goes idle." = "为 true 时,对方下一次完成回合进入空闲后,本会话会收到一条一次性空闲通知。";
396
+ "When true, this session gets a one-time notice when the recipient next finishes a turn and goes idle. That turn may be one already in progress, so the notice doesn't mean this message was handled." = "为 true 时,对方下一次完成回合进入空闲后,本会话会收到一条一次性空闲通知。这一回合可能是正在进行的回合,所以收到通知不代表这条消息已处理。";
397
397
  "Send cross-session message" = "发送跨会话消息";
398
398
  "Rename the current session in CC Bridge (the address other sessions use to message you), e.g. frontend or api-server. " = "修改当前会话在 CC Bridge 中的名字(即其他会话给你发消息时用的地址),例如 frontend、api-server。";
399
399
  "Only lowercase letters, digits and . _ - are allowed, up to 40 characters; fails if another online session uses the name. " = "只允许小写字母、数字和 . _ -,最长 40 个字符;名字已被其他在线会话占用时会失败。";
@@ -505,17 +505,11 @@
505
505
  "codex executable not found, so messages to Codex can only go to its inbox; set CODEX_REAL_BIN and run cc-pets bridge enable." = "未找到 codex 可执行文件,发给 Codex 的消息只能进信箱;可设置 CODEX_REAL_BIN 后执行 cc-pets bridge enable。";
506
506
  "Couldn't tell whether Codex supports codex queue; failed deliveries fall back to the inbox." = "无法确认 Codex 是否支持 codex queue;投递失败时会自动退回信箱。";
507
507
  "Claude Code hooks written to {path}" = "Claude Code hooks 已写入 {path}";
508
- "Claude Code allows these tools without asking: {claudeAllow}" = "Claude Code 中以下工具免确认:{claudeAllow}";
509
- "In Claude Code, cc-bridge tools follow your permission settings." = "Claude Code 中 cc-bridge 的工具按你的权限设置询问。";
510
508
  "Claude Code MCP registered: {MCP_NAME} (on first use, Claude asks according to your permission settings)" = "Claude Code MCP 已注册:{MCP_NAME}(首次调用时 Claude 会按你的权限设置询问是否允许)";
511
509
  "Claude Code MCP registration failed. Run manually: claude {shellQuote}" = "Claude Code MCP 注册失败,请手动执行:claude {shellQuote}";
512
- "Claude Code CLI not detected, skipped." = "未检测到 Claude Code CLI,跳过。";
513
510
  "Codex hooks written to {path}. Next time you start Codex, run /hooks and trust the cc-pets bridge hooks." = "Codex hooks 已写入 {path}——下次启动 Codex 后请执行 /hooks 并信任 cc-pets bridge 的 hooks。";
514
511
  "Codex MCP registered: {MCP_NAME}" = "Codex MCP 已注册:{MCP_NAME}";
515
512
  "Codex MCP registration failed. Run manually: codex {shellQuote}" = "Codex MCP 注册失败,请手动执行:codex {shellQuote}";
516
- "Codex auto-approves these tools: {codexApprove} (restart Codex sessions to apply)" = "Codex 中以下工具免审批:{codexApprove}(需重启 Codex 会话生效)";
517
- "cc-bridge tools keep Codex's default approvals (calls fail under approval_policy=never; allow them with --codex-approve)." = "Codex 中 cc-bridge 的工具保持默认审批(approval_policy=never 时调用会失败,可用 --codex-approve 放开)。";
518
- "Codex CLI not detected, skipped." = "未检测到 Codex CLI,跳过。";
519
513
  "Removed the cc-bridge hooks and tool permissions from Claude Code." = "已移除 Claude Code 中的 cc-bridge hooks 与工具放行规则。";
520
514
  "Removed the cc-bridge hooks from Codex." = "已移除 Codex 中的 cc-bridge hooks。";
521
515
  "{flag} only accepts groups {TOOL_GROUPS} or tools {ALL_TOOLS}, got: {unknown}" = "{flag} 只接受分组 {TOOL_GROUPS} 或工具 {ALL_TOOLS},收到:{unknown}";
@@ -899,15 +893,27 @@
899
893
 
900
894
  "Current session identity is unavailable or ambiguous (you can view sessions, but cannot act as one)." = "当前会话身份缺失或不明确(可以查看会话列表,但不能以任何会话身份操作)。";
901
895
 
896
+ "[cc-bridge identity] Your current session_id is {session_id}. Pass this exact value as session_id on every CC Bridge tool call. Do not use a recipient's ID or an ID from a cross-session message." = "[cc-bridge 身份] 你当前的 session_id 是 {session_id}。每次调用 CC Bridge 工具都将此值原样作为 session_id 传入。不要使用收件人的 ID 或跨会话消息中的 ID。";
897
+ "Your current session ID from the cc-bridge hook context or your own shell's CODEX_THREAD_ID / CLAUDE_CODE_SESSION_ID. Pass it on every call when available; required to identify one of several sessions sharing a host. Never use the recipient's ID or guess from the session list." = "来自 cc-bridge hook 上下文或当前会话 shell 的 CODEX_THREAD_ID / CLAUDE_CODE_SESSION_ID 的会话 ID。有值时每次调用都传入;多个会话共享宿主时必须提供才能识别身份。不要使用收件人的 ID,也不要根据会话列表猜测。";
898
+ " Pass your current session_id on every CC Bridge tool call, using the ID from the cc-bridge hook context. If it is missing, read CODEX_THREAD_ID (Codex) or CLAUDE_CODE_SESSION_ID (Claude) in your own session's shell; if neither is available, refresh this session's bridge hooks. Never guess an ID from names, directories, activity or received messages, and never save one session's ID in shared MCP configuration." = " 每次调用 CC Bridge 工具都传入当前 session_id,取值来自 cc-bridge hook 上下文。缺失时,在当前会话的 shell 中读取 CODEX_THREAD_ID(Codex)或 CLAUDE_CODE_SESSION_ID(Claude);仍不可用时,刷新当前会话的 bridge hooks。不要根据名字、目录、忙闲或收到的消息猜测 ID,也不要把某个会话的 ID 写入共享 MCP 配置。";
899
+ "Invalid session_id: it must identify a live registered session belonging to this MCP host. No action was taken." = "无效的 session_id:必须对应属于当前 MCP 宿主的已注册在线会话。未执行操作。";
900
+ "It isn't a registered online session." = "该 ID 不是已注册的在线会话。";
901
+ "This MCP server's host process can't be identified, so the session can't be verified as belonging to it." = "无法识别此 MCP 服务的宿主进程,因此无法确认该会话属于它。";
902
+ "It belongs to a different host process." = "该会话属于其他宿主进程。";
903
+ "paths must be an array of strings." = "paths 必须是字符串数组。";
904
+ "codex queue timed out, so the message may already be queued for {name}. It was also put in its inbox, so it may arrive twice with the same message id." = "codex queue 超时,消息可能已进入 {name} 的队列。消息同时放进了它的信箱,所以可能以相同的消息 id 到达两次。";
905
+ "{flag} needs a value." = "{flag} 需要一个值。";
906
+ "Unknown option: {option}" = "未知选项:{option}";
907
+
902
908
  /* 启动前代理检查 */
903
909
  "Launch Protection" = "启动保护";
904
910
  "Block Claude / Codex when Shadowrocket is disconnected or the exit is in an unsupported region" = "Shadowrocket 未连接,或出口为不支持的地区时,阻止启动 Claude / Codex";
905
911
  "Off by default. Checks each service's exit IP and region through CC Pets wrappers; does not validate your account or API key." = "默认关闭。通过 CC Pets 包装器分别检查各服务的出口 IP 和地区,不验证账号或 API 密钥。";
906
912
  "CC Pets blocked {provider}: {reason}" = "CC Pets 已阻止启动 {provider}:{reason}";
907
- "Connect Shadowrocket and select an exit outside mainland China / Hong Kong, then check routing and DNS settings. You can turn off this check in Main menu → Settings → Launch Protection." = "请连接小火箭,选择中国大陆和香港以外的出口,并检查分流及 DNS 设置。可在「主菜单 → 设置 → 启动保护」关闭此检查。";
913
+ "Connect Shadowrocket and select an exit outside mainland China / Hong Kong, then check routing and DNS settings. You can turn off this check in Main menu → Settings → Launch Protection." = "请连接 Shadowrocket,选择中国大陆和香港以外的出口,并检查分流及 DNS 设置。可在「主菜单 → 设置 → 启动保护」关闭此检查。";
908
914
  "Unable to read the system VPN status." = "无法读取系统 VPN 状态。";
909
- "Shadowrocket is not connected. Opening the app alone is not enough." = "小火箭尚未连接,仅打开应用不代表代理已开启。";
910
- "No Shadowrocket VPN configuration was found." = "未找到小火箭的 VPN 配置。";
915
+ "Shadowrocket is not connected. Opening the app alone is not enough." = "Shadowrocket 尚未连接,仅打开应用不代表代理已开启。";
916
+ "No Shadowrocket VPN configuration was found." = "未找到 Shadowrocket 的 VPN 配置。";
911
917
  "Launch check expects claude or codex." = "启动检查需要指定 claude 或 codex。";
912
918
  "CC Pets: checking Shadowrocket and service exit regions ..." = "CC Pets:正在检查 Shadowrocket 连接状态及服务出口地区 ...";
913
919
  "The proxy or service hostname could not be resolved." = "无法解析代理或服务域名。";
@@ -1014,3 +1020,21 @@
1014
1020
  "Install Extension…" = "安装扩展…";
1015
1021
  "Delete “%@”?" = "删除「%@」?";
1016
1022
  "This can't be undone. Continue?" = "删除后无法撤销,是否继续?";
1023
+
1024
+ /* 菜单栏图标 */
1025
+ "%@ awaiting approval" = "%@ 个等待审批";
1026
+ "Show Pet" = "显示桌宠";
1027
+ "This month · %@ calls" = "本月 · %@ 次调用";
1028
+ "Week %@" = "本周 %@";
1029
+ "Quota" = "额度";
1030
+ "%@ more sessions…" = "还有 %@ 个会话…";
1031
+ "Show In" = "显示位置";
1032
+ "Display" = "显示方式";
1033
+ "Desktop Pet" = "仅桌宠";
1034
+ "Pet + Menu Bar" = "桌宠 + 菜单栏";
1035
+ "Menu Bar Only" = "仅菜单栏";
1036
+ "Menu bar only hides the pet, its status card, speech and hover panel; notifications still work" = "仅菜单栏时隐藏桌宠、状态卡、气泡和悬停面板,系统通知照常";
1037
+ "Quota in Menu Bar" = "菜单栏显示额度";
1038
+ "Lowest 5-hour quota left; orange at 20% or below, red at 10%" = "显示最低的 5 小时剩余额度;20% 及以下为橙色,10% 及以下为红色";
1039
+ "The pet is hidden (menu bar only). Line: %@" = "桌宠已隐藏(仅菜单栏),这句是:%@";
1040
+ "Click to jump to the session awaiting approval (⌥-click for the panel)" = "点击跳到等待审批的会话(按住 ⌥ 点击打开面板)";
@@ -75,10 +75,39 @@ static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
75
75
  if (lineData.length == 0) continue;
76
76
  NSDictionary *record = [NSJSONSerialization JSONObjectWithData:lineData options:0 error:nil];
77
77
  if (![record isKindOfClass:NSDictionary.class]) continue;
78
- if (recentOnly && [record[@"timestamp"] doubleValue] < cutoff) continue;
78
+ NSMutableDictionary *normalized = [record mutableCopy];
79
+ NSString *profileID = NormalizedAgentProfileID(record[@"profileId"]);
80
+ if (profileID.length > 0) normalized[@"profileId"] = profileID;
81
+ else [normalized removeObjectForKey:@"profileId"];
82
+ record = normalized;
83
+ BOOL ended = [record[@"event"] isEqual:@"SessionEnd"];
84
+ BOOL desktop = [record[@"provider"] isEqual:@"Codex"] && IsCodexDesktopFocusTarget(record[@"terminal"]);
85
+ NSNumber *persistent = desktop && !ended ? [self desktopSessionPersistenceForRecord:record] : nil;
86
+ if (desktop && !ended && persistent && !persistent.boolValue) continue;
87
+ if (persistent) normalized[@"desktopPersistent"] = persistent;
88
+ BOOL replay = recentOnly && (desktop || [record[@"timestamp"] doubleValue] < cutoff);
89
+ // 只有确认的持久化聊天可以恢复在线列表。应用存活不证明某轮任务仍在运行。
90
+ if (replay && !ended && !(desktop && persistent.boolValue && CodexDesktopFocusTargetIsLive(record[@"terminal"],
91
+ [record[@"timestamp"] doubleValue]))) continue;
92
+ NSString *sessionKey = [self agentSessionKeyForRecord:record];
93
+ NSDictionary *previous = sessionKey ? self.agentSessionRecords[sessionKey] : nil;
94
+ // 在记录、审批、动画等所有消费者之前挡住完成后的尾随事件。
95
+ if (!ended && previous && [[self statusIdentityForRecord:previous] isEqual:[self statusIdentityForRecord:record]] &&
96
+ [self isTrailingRecord:record afterState:previous[@"state"]]) continue;
97
+ if (replay && desktop && !ended &&
98
+ ![@[@"completed", @"failed"] containsObject:record[@"state"] ?: @""]) {
99
+ normalized[@"state"] = @"idle";
100
+ normalized[@"event"] = @"Snapshot";
101
+ normalized[@"tool"] = @"";
102
+ }
79
103
  [self trackAgentSessionRecord:record];
80
104
  NSString *event = record[@"event"];
81
105
  NSString *state = [record[@"state"] isKindOfClass:NSString.class] ? record[@"state"] : @"";
106
+ if (ended) {
107
+ [self.pendingApprovalRecords removeObjectForKey:[self approvalKeyForRecord:record]];
108
+ if (!recentOnly) [self finishAgentSessionPresentation:record];
109
+ continue;
110
+ }
82
111
  if ([event isKindOfClass:NSString.class]) {
83
112
  if (!self.pendingApprovalRecords) {
84
113
  self.pendingApprovalRecords = [NSMutableDictionary dictionary];
@@ -92,11 +121,13 @@ static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
92
121
  [self.pendingApprovalRecords removeObjectForKey:approvalKey];
93
122
  }
94
123
  [self prunePendingApprovalRecords];
124
+ if (replay || (desktop && [event isEqual:@"SessionStart"])) continue;
95
125
  // 多会话下不能让一个未处理的审批全局挡住其他 Agent 的所有 Hook。每条事件
96
126
  // 都正常驱动气泡和动画;仍在等待的审批保留在多会话记录中供用户回跳。
97
127
  [self displayAgentRecord:record notify:YES];
98
128
  }
99
129
  }
130
+ [self updateQuotaLiveState];
100
131
  [self refreshApprovalBadge];
101
132
  }
102
133
  - (void)readNewAgentEvents {
@@ -180,8 +211,7 @@ static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
180
211
  stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
181
212
  NSString *tty = lines.count > 1 ? [lines[1]
182
213
  stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] : @"";
183
- NSString *profileID = lines.count > 2 ? [lines[2]
184
- stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] : @"";
214
+ NSString *profileID = lines.count > 2 ? NormalizedAgentProfileID(lines[2]) : @"";
185
215
  if (!ClientProcessAlive(pid, tty)) {
186
216
  [NSFileManager.defaultManager removeItemAtPath:path error:nil];
187
217
  continue;
@@ -205,6 +235,18 @@ static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
205
235
  self.liveClientProviders = providers;
206
236
  self.liveClientProfileIDs = profileIDs;
207
237
  self.liveUnscopedClientProviders = unscopedProviders;
238
+ if (!self.managedAgentSessionKeys) self.managedAgentSessionKeys = [NSMutableSet set];
239
+ // 先用本轮与上一轮的 pid 快照标记对应会话,再替换快照,保留精确退出信号。
240
+ NSMutableSet<NSString *> *observedKeys = [sessionKeys mutableCopy];
241
+ [observedKeys unionSet:self.liveAgentSessionKeys ?: [NSSet set]];
242
+ for (NSDictionary *record in self.agentSessionRecords.allValues) {
243
+ NSString *key = [self onlineAgentSessionKeyForRecord:record];
244
+ NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
245
+ NSString *legacyKey = OnlineAgentSessionKey(record[@"provider"], nil, terminal[@"tty"]);
246
+ if ((key && [observedKeys containsObject:key]) || (legacyKey && [observedKeys containsObject:legacyKey])) {
247
+ [self.managedAgentSessionKeys addObject:[self statusIdentityForRecord:record]];
248
+ }
249
+ }
208
250
  self.liveAgentSessionKeys = sessionKeys;
209
251
  self.hasUnlabeledClient = unlabeled;
210
252
  [self pruneOfflineAgentSessionRecords];
@@ -220,6 +262,7 @@ static BOOL ClientProcessAlive(pid_t pid, NSString *recordedTTY) {
220
262
  [self refreshApprovalBadge];
221
263
  // 启动模式在应用生命周期内保持不变。手动启动的桌宠即使后来检测到
222
264
  // Codex/Claude 客户端,也不应被转成 CLI 托管模式并随客户端退出。
223
- if (liveClients == 0 && self.managedByCLI) [NSApp terminate:nil];
265
+ // 桌面端不创建 CLI pid 文件;只要桌面应用仍运行,就不能随最后一个终端退出。
266
+ if (liveClients == 0 && self.managedByCLI && !CodexDesktopApplicationIsRunning()) [NSApp terminate:nil];
224
267
  }
225
268
  @end
@@ -1,5 +1,6 @@
1
1
  // Agent 状态卡、会话列表、审批与卡住提醒、终端回跳、系统通知。
2
2
  #import "CCPetsAppDelegate+Private.h"
3
+ #import "CCPetsBridgeStore.h"
3
4
 
4
5
  @implementation AppDelegate (AgentStatus)
5
6
  - (void)sendNotificationWithTitle:(NSString *)title body:(NSString *)body {
@@ -189,7 +190,7 @@
189
190
  - (void)updateStatusBubbleExpanded:(BOOL)expanded {
190
191
  self.statusBubbleExpanded = expanded;
191
192
  CCPetsPrefSetBool(StatusBubbleExpandedKey, expanded);
192
- if (self.statusBubbleExpanded && self.hasAgentStatus) {
193
+ if (self.statusBubbleExpanded && self.hasAgentStatus && [self petVisible]) {
193
194
  [self positionAgentStatus];
194
195
  [self.statusPanel orderFrontRegardless];
195
196
  } else {
@@ -216,7 +217,7 @@
216
217
  }
217
218
  - (NSString *)statusIdentityForRecord:(NSDictionary *)record {
218
219
  NSString *provider = SanitizedShortString(record[@"provider"], 32);
219
- NSString *profileID = SanitizedShortString(record[@"profileId"], 128);
220
+ NSString *profileID = NormalizedAgentProfileID(record[@"profileId"]);
220
221
  NSString *session = SanitizedShortString(record[@"session"], 128);
221
222
  NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
222
223
  NSString *terminalID = SanitizedShortString(terminal[@"ideTerminal"], 64);
@@ -226,15 +227,33 @@
226
227
  return [NSString stringWithFormat:@"%@|%@|%@", provider ?: @"", profileID ?: @"", identity ?: @""];
227
228
  }
228
229
  - (void)noteStatusIdentityForRecord:(NSDictionary *)record {
230
+ self.lastStatusRecord = record;
229
231
  self.lastStatusAgentProvider = SanitizedShortString(record[@"provider"], 32);
230
232
  self.lastStatusIdentity = [self statusIdentityForRecord:record];
233
+ // Bridge 注册表是定时刷新的:新会话的头一两条事件可能还对不上名字,先显示目录名,
234
+ // 下一条事件自然补成"目录名.动物"。
235
+ NSDictionary *bridge = [self bridgeSessionEntryForRecord:record];
236
+ NSString *cwd = SanitizedWorkingDirectory(record[@"cwd"]);
237
+ if (cwd.length == 0) cwd = SanitizedWorkingDirectory(bridge[@"cwd"]);
238
+ self.lastStatusSessionLabel = StatusSessionLabel(CCBridgeSessionShortName(bridge), cwd);
239
+ NSMutableArray<NSString *> *lines = [NSMutableArray array];
240
+ NSString *name = [bridge[@"name"] isKindOfClass:NSString.class] ? bridge[@"name"] : @"";
241
+ [lines addObject:name.length > 0 ? name : [self statusPresentationNameForRecord:record]];
242
+ if (cwd.length > 0) [lines addObject:cwd.stringByAbbreviatingWithTildeInPath];
243
+ [lines addObject:L(@"Go back to the Agent's terminal")];
244
+ self.lastStatusSessionTooltip = [lines componentsJoinedByString:@"\n"];
231
245
  }
232
246
  - (void)applyStatusPresentationForState:(NSString *)state provider:(NSString *)provider
233
247
  tool:(NSString *)tool {
234
248
  self.lastStatusState = state;
235
249
  self.lastStatusProvider = provider;
250
+ // 标题优先显示会话标签:多个会话同时在跑时,"Claude"分不出是哪一个,目录名.动物可以。
251
+ // 是哪家 Agent、完整路径放进悬停提示。
236
252
  self.statusTitleLabel.stringValue = [NSString stringWithFormat:@"%@ · %@",
237
- provider, [self statusTextForState:state tool:tool]];
253
+ self.lastStatusSessionLabel.length > 0 ? self.lastStatusSessionLabel : provider,
254
+ [self statusTextForState:state tool:tool]];
255
+ self.statusClickButton.toolTip = self.lastStatusSessionTooltip.length > 0
256
+ ? self.lastStatusSessionTooltip : L(@"Go back to the Agent's terminal");
238
257
  self.statusDetailLabel.stringValue = [self statusDetailForState:state tool:tool];
239
258
  // 上一句碎碎念借走的副行已经被新状态覆盖,取消那次归还,否则它会把旧文案写回来。
240
259
  [NSObject cancelPreviousPerformRequestsWithTarget:self
@@ -251,7 +270,7 @@
251
270
  [self layoutApprovalBadge];
252
271
  [self positionAgentStatus];
253
272
  [self notePetCardChanged];
254
- if (self.statusBubbleExpanded) {
273
+ if (self.statusBubbleExpanded && [self petVisible]) {
255
274
  [self.statusPanel orderFrontRegardless];
256
275
  // 卡片一出现就收掉独立气泡:两者都是宠物在说话,同时挂着就是重影。
257
276
  // 更新气泡也一样让开:agent 有新动静时状态卡优先,更新入口还有角标和菜单。
@@ -307,7 +326,7 @@
307
326
  self.hasAgentStatus = YES;
308
327
  [self noteStatusIdentityForRecord:record];
309
328
  [self applyStatusPresentationForState:state provider:presentationName tool:tool];
310
- [self.panel orderFrontRegardless];
329
+ if ([self petVisible]) [self.panel orderFrontRegardless];
311
330
  [self performSelector:@selector(hideAgentStatus)
312
331
  withObject:nil afterDelay:AgentStatusInactivityInterval];
313
332
  if ([state isEqualToString:@"starting"]) {
@@ -331,7 +350,8 @@
331
350
  NSTimeInterval cutoff = NSDate.date.timeIntervalSince1970 - PendingApprovalTTL;
332
351
  for (NSString *key in self.pendingApprovalRecords.allKeys) {
333
352
  NSDictionary *record = self.pendingApprovalRecords[key];
334
- if ([record[@"timestamp"] doubleValue] < cutoff) {
353
+ BOOL desktop = [record[@"provider"] isEqual:@"Codex"] && IsCodexDesktopFocusTarget(record[@"terminal"]);
354
+ if ([record[@"timestamp"] doubleValue] < cutoff || (desktop && ![self isAgentSessionRecordLive:record])) {
335
355
  [self.pendingApprovalRecords removeObjectForKey:key];
336
356
  }
337
357
  }
@@ -365,7 +385,7 @@
365
385
  }
366
386
  - (NSString *)agentSessionKeyForRecord:(NSDictionary *)record {
367
387
  NSString *provider = SanitizedShortString(record[@"provider"], 32);
368
- NSString *profileID = SanitizedShortString(record[@"profileId"], 128);
388
+ NSString *profileID = NormalizedAgentProfileID(record[@"profileId"]);
369
389
  NSString *identity = profileID.length > 0 ? [NSString stringWithFormat:@"%@|%@", provider, profileID] : provider;
370
390
  NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
371
391
  ? record[@"terminal"] : nil;
@@ -383,21 +403,72 @@
383
403
  ? record[@"terminal"] : nil;
384
404
  return OnlineAgentSessionKey(record[@"provider"], record[@"profileId"], terminal[@"tty"]);
385
405
  }
406
+ - (NSNumber *)desktopSessionPersistenceForRecord:(NSDictionary *)record {
407
+ if (![record[@"provider"] isEqual:@"Codex"] || !IsCodexDesktopFocusTarget(record[@"terminal"])) return nil;
408
+ if ([record[@"transient"] boolValue]) return @NO;
409
+ // 新会话的路径可能先于索引落盘;明确提供转录路径时不因索引延迟而丢掉首条事件。
410
+ if ([record[@"transcriptPath"] isKindOfClass:NSString.class] && [record[@"transcriptPath"] length] > 0) return @YES;
411
+ NSString *home = [record[@"codexHome"] isKindOfClass:NSString.class] ? record[@"codexHome"] : nil;
412
+ NSString *profile = NormalizedAgentProfileID(record[@"profileId"]);
413
+ if (home.length == 0 && profile.length > 0) home = CCPetsProfileWithID(profile)[@"home"];
414
+ if (home.length == 0 && profile.length == 0) home = DefaultCodexHomeDirectory();
415
+ return CodexPersistentThreadStatus(SanitizedShortString(record[@"session"], 128), home);
416
+ }
417
+
418
+ - (void)finishAgentSessionPresentation:(NSDictionary *)record {
419
+ NSString *session = SanitizedShortString(record[@"session"], 128);
420
+ if (!self.hasAgentStatus || session.length == 0 ||
421
+ ![self.lastStatusRecord[@"session"] isEqual:session] ||
422
+ ![self.lastStatusRecord[@"provider"] isEqual:record[@"provider"]]) return;
423
+ // 内部记录已删除后,屏幕也必须同步切换,不能把已结束会话的“思考”留到超时。
424
+ NSDictionary *replacement = nil;
425
+ for (NSDictionary *candidate in self.agentSessionRecords.allValues) {
426
+ if (![self isAgentSessionRecordLive:candidate]) continue;
427
+ if (!replacement || [candidate[@"timestamp"] doubleValue] > [replacement[@"timestamp"] doubleValue]) {
428
+ replacement = candidate;
429
+ }
430
+ }
431
+ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideAgentStatus) object:nil];
432
+ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(enterIdleStatus) object:nil];
433
+ if (replacement) {
434
+ if ([replacement[@"state"] isEqual:@"idle"]) {
435
+ [self.petView resetAgentToIdle];
436
+ } else {
437
+ [self.petView handleAgentEvent:replacement[@"event"] tool:replacement[@"tool"]
438
+ failed:[replacement[@"failed"] boolValue]];
439
+ }
440
+ [self showAgentStatusForRecord:replacement notify:NO];
441
+ } else {
442
+ [self.petView resetAgentToIdle];
443
+ [self hideAgentStatus];
444
+ self.lastStatusRecord = nil;
445
+ self.lastTerminalFocusTarget = nil;
446
+ }
447
+ }
386
448
  // 在线判定优先用包装脚本写出的 pid 文件:它给出精确的退出信号,会话一关列表就更新。
387
449
  // 但直接跑 claude / codex(或终端窗口早于 shim 安装就已打开)的会话根本没有 pid 文件,
388
450
  // 只按 pid 文件判定会把它们当场判死,列表里永远看不到——哪怕它们的 Hook 正在正常发事件。
389
- // 这类会话退回活跃度宽限:最近一次事件在静默窗口内就算在线。某个 provider 一旦出现过
390
- // pid 文件,说明包装脚本对它生效,继续按精确信号判定,不被宽限盖住。
451
+ // 桌面会话按应用实例判断;未经过包装器的终端退回活跃度宽限。
452
+ // 只能用精确的账号 + tty 判断某个包装器是否已退出,不能用整个 provider 排除其他会话。
391
453
  - (BOOL)isAgentSessionRecordLive:(NSDictionary *)record {
454
+ if ([record[@"event"] isEqual:@"SessionEnd"]) return NO;
455
+ NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
456
+ ? record[@"terminal"] : nil;
457
+ if ([record[@"provider"] isEqual:@"Codex"] && IsCodexDesktopFocusTarget(terminal)) {
458
+ if ([record[@"transient"] boolValue]) return NO;
459
+ if (![record[@"desktopPersistent"] boolValue]) {
460
+ // 无法证明是真实聊天的旧事件只给短暂宽限,不能靠宿主进程永久保活。
461
+ return NSDate.date.timeIntervalSince1970 - [record[@"timestamp"] doubleValue]
462
+ < AgentStatusInactivityInterval;
463
+ }
464
+ return CodexDesktopFocusTargetIsLive(terminal, [record[@"timestamp"] doubleValue]);
465
+ }
392
466
  NSString *onlineKey = [self onlineAgentSessionKeyForRecord:record];
393
467
  if (onlineKey.length > 0 && [self.liveAgentSessionKeys containsObject:onlineKey]) return YES;
394
468
  // 老版本包装脚本的 pid 文件不带账号,只能按 provider + tty 认领。
395
- NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class]
396
- ? record[@"terminal"] : nil;
397
469
  NSString *unscopedKey = OnlineAgentSessionKey(record[@"provider"], nil, terminal[@"tty"]);
398
470
  if (unscopedKey.length > 0 && [self.liveAgentSessionKeys containsObject:unscopedKey]) return YES;
399
- NSString *provider = SanitizedShortString(record[@"provider"], 32);
400
- if (provider.length > 0 && [self.liveClientProviders containsObject:provider]) return NO;
471
+ if ([self.managedAgentSessionKeys containsObject:[self statusIdentityForRecord:record]]) return NO;
401
472
  return NSDate.date.timeIntervalSince1970 - [record[@"timestamp"] doubleValue]
402
473
  < AgentStatusInactivityInterval;
403
474
  }
@@ -418,8 +489,32 @@
418
489
  SanitizedShortString(terminal[@"bundleID"], 128), tty];
419
490
  }
420
491
  - (void)trackAgentSessionRecord:(NSDictionary *)record {
492
+ if ([record[@"event"] isEqual:@"SessionEnd"]) {
493
+ // 结束事件可能拿不到终端,用稳定的会话身份清理;不能清掉同账号的其他聊天。
494
+ NSString *session = SanitizedShortString(record[@"session"], 128);
495
+ if (session.length == 0) return;
496
+ for (NSString *existingKey in self.agentSessionRecords.allKeys) {
497
+ NSDictionary *existing = self.agentSessionRecords[existingKey];
498
+ if ([existing[@"session"] isEqual:session] && [existing[@"provider"] isEqual:record[@"provider"]]) {
499
+ [self.agentSessionRecords removeObjectForKey:existingKey];
500
+ [self notePetCardChanged];
501
+ }
502
+ }
503
+ return;
504
+ }
421
505
  NSString *key = [self agentSessionKeyForRecord:record];
422
506
  if (key.length == 0) return;
507
+ NSDictionary *previous = self.agentSessionRecords[key];
508
+ if ([[self statusIdentityForRecord:previous] isEqual:[self statusIdentityForRecord:record]] &&
509
+ [self isTrailingRecord:record afterState:previous[@"state"]]) return;
510
+ NSString *onlineKey = [self onlineAgentSessionKeyForRecord:record];
511
+ NSDictionary *terminal = [record[@"terminal"] isKindOfClass:NSDictionary.class] ? record[@"terminal"] : nil;
512
+ NSString *legacyKey = OnlineAgentSessionKey(record[@"provider"], nil, terminal[@"tty"]);
513
+ if ((onlineKey && [self.liveAgentSessionKeys containsObject:onlineKey]) ||
514
+ (legacyKey && [self.liveAgentSessionKeys containsObject:legacyKey])) {
515
+ if (!self.managedAgentSessionKeys) self.managedAgentSessionKeys = [NSMutableSet set];
516
+ [self.managedAgentSessionKeys addObject:[self statusIdentityForRecord:record]];
517
+ }
423
518
  if (!self.agentSessionRecords) self.agentSessionRecords = [NSMutableDictionary dictionary];
424
519
  // 一个终端前台同时只跑一个客户端。同一终端里新起的会话(可能换了账号)说明之前那个
425
520
  // 已经退出;它退出时不会补发结束事件,不在这里清掉就会在列表里留一行"正在启动"。
@@ -527,7 +622,7 @@
527
622
  [self applyStatusPresentationForState:state
528
623
  provider:[self statusPresentationNameForRecord:record]
529
624
  tool:[record[@"tool"] isKindOfClass:NSString.class] ? record[@"tool"] : @""];
530
- [self.panel orderFrontRegardless];
625
+ if ([self petVisible]) [self.panel orderFrontRegardless];
531
626
  [self performSelector:@selector(hideAgentStatus) withObject:nil
532
627
  afterDelay:AgentStatusInactivityInterval];
533
628
  }
@@ -1,5 +1,6 @@
1
1
  // CC Bridge 开关、消息角标与菜单。
2
2
  #import "CCPetsAppDelegate+Private.h"
3
+ #import "CCPetsBridgeStore.h"
3
4
 
4
5
  @implementation AppDelegate (Bridge)
5
6
  // 菜单开关 → cc-pets bridge enable / disable / configure。逻辑全在 CLI 里,桌宠只负责调用,
@@ -206,9 +207,9 @@
206
207
  if (![NSUserDefaults.standardUserDefaults boolForKey:BridgeNotificationKey]) return;
207
208
  NSDictionary *latest = fresh.firstObject;
208
209
  NSString *body = fresh.count == 1
209
- ? [NSString stringWithFormat:@"%@ → %@", latest[@"from"], latest[@"to"]]
210
- : [NSString stringWithFormat:L(@"%1$@ → %2$@ and %3$lu more"), latest[@"from"], latest[@"to"],
211
- (unsigned long)fresh.count];
210
+ ? [NSString stringWithFormat:@"%@ → %@", CCBridgeSessionDisplayName(latest[@"fromIdentity"]), CCBridgeSessionDisplayName(latest[@"toIdentity"])]
211
+ : [NSString stringWithFormat:L(@"%1$@ → %2$@ and %3$lu more"), CCBridgeSessionDisplayName(latest[@"fromIdentity"]), CCBridgeSessionDisplayName(latest[@"toIdentity"]),
212
+ (unsigned long)fresh.count - 1];
212
213
  [self sendNotificationWithTitle:L(@"New CC Bridge Message") body:body];
213
214
  }
214
215
  // 蓝色 = 有新的跨会话消息送达;橙色 = 有消息卡在某个会话的信箱里(那个 Claude 会话长时间空闲、
@@ -283,7 +284,8 @@
283
284
  NSMenuItem *heading = [menu addItemWithTitle:L(@"CC Bridge Messages") action:nil keyEquivalent:@""];
284
285
  heading.enabled = NO;
285
286
  [pending enumerateKeysAndObjectsUsingBlock:^(NSString *session, NSNumber *count, BOOL *stop) {
286
- NSString *name = self.bridgeSessions[session][@"name"] ?: session;
287
+ NSString *name = CCBridgeSessionDisplayName(self.bridgeSessions[session]);
288
+ if (name.length == 0) name = session;
287
289
  NSString *title = [NSString stringWithFormat:count.unsignedLongValue != 1
288
290
  ? L(@"📬 %1$@ has %2$lu undelivered messages (say anything in that terminal to deliver)")
289
291
  : L(@"📬 %1$@ has %2$lu undelivered message (say anything in that terminal to deliver)"),
@@ -300,11 +302,11 @@
300
302
  dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
301
303
  BOOL unseen = [delivery[@"at"] doubleValue] > self.bridgeSeenAt;
302
304
  NSString *title = [NSString stringWithFormat:@"%@%@ → %@ · %@",
303
- unseen ? @"✉️ " : @"", delivery[@"from"], delivery[@"to"], time];
305
+ unseen ? @"✉️ " : @"", CCBridgeSessionDisplayName(delivery[@"fromIdentity"]), CCBridgeSessionDisplayName(delivery[@"toIdentity"]), time];
304
306
  NSMenuItem *item = [menu addItemWithTitle:title action:@selector(focusBridgeSession:) keyEquivalent:@""];
305
307
  item.target = self;
306
308
  item.representedObject = delivery[@"toSession"];
307
- item.toolTip = [NSString stringWithFormat:L(@"Jump to %@'s terminal"), delivery[@"to"]];
309
+ item.toolTip = [NSString stringWithFormat:L(@"Jump to %@'s terminal"), CCBridgeSessionDisplayName(delivery[@"toIdentity"])];
308
310
  }
309
311
  }
310
312
  @end