commit 0c84dde85b0a5b89bf8fab402002ca1ac5f3f3a2 Author: gongzhiyong Date: Thu Apr 30 00:33:20 2026 +0800 docs: Heicode 愿景范式、瀑布/敏捷子 Agnet 角色与规模 - 新增 docs/vision-heicode-full-stack-agentic-dev.md(完整范式与 W1-W9 / A1-A8 角色) - 新增根 README.md 与 .gitignore(排除 node_modules、target 等) Made-with: Cursor diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d5dd4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# Dependencies +node_modules/ + +# Build / cache +**/target/ +dist/ +build/ +.turbo/ +.next/ +*.tsbuildinfo + +# Environment +.env +.env.* +!.env.example + +# OS / IDE +.DS_Store +.idea/ +.vscode/ +*.swp +*.swo + +# Logs +*.log +logs/ + +# Go +*.exe +*.test +*.out diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ffee0a --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# Heicode + +Heicode 是一个**面向团队的智能体驱动软件交付**工作区:把 **改造后的大模型网关(New API)**、**终端与桌面上的智能体编程客户端(cc-haha)**,以及 **Agnet 平台上的子智能体团队** 串成一条从想法到上线、再到运维迭代的闭环。 + +## 这个仓库里有什么 + +| 目录 | 作用 | +|------|------| +| `new-api/` | 基于 New API 生态的 **网关与运营能力**(模型渠道、计费、鉴权、多协议中继等),经深度改造后作为用户登录后看到的 **后台与 API 能力层**。 | +| `cc-haha/` | **CLI / 本地服务 / Desktop(Tauri)** 相关代码,提供类 **Claude Code** 的编程与协作体验,并与 New API 对齐登录与接口。 | +| `_all-in-one-upload/` | 历史或打包上传相关目录(按需维护)。 | + +## 产品在解决什么问题 + +- **自动化开发范式**:不止「写代码」,而是覆盖需求、实现、测试、**多云部署**、运维与迭代的 **标准化团队模式**(瀑布 / 敏捷模板 + 子智能体规模可配置)。 +- **平台整合**:用户从官网进入 → 使用改造后的 **New API** 控制台 → 下载 **CLI** 与 **Desktop** → 与 **Agnet** 上已部署的 **子智能体团队** 协同;子智能体可具备 Git 与云资源(GCP / AWS / Azure)的**策略化**自动化能力。 +- **可审计交付**:多智能体协作记录、产品文档、开发日志等与版本和发布关联。 + +## 详细愿景与范式说明 + +完整的产品叙事、三问回答、架构分层、用户旅程、**瀑布(5~9)与敏捷(3~8)子 Agnet 角色清单**、风险与路线图见: + +**[docs/vision-heicode-full-stack-agentic-dev.md](./docs/vision-heicode-full-stack-agentic-dev.md)** + +## 相关外部参考(概念) + +- [oh-my-claudecode](https://ohmyclaudecode.com/) — 高效使用 Claude Code 类工具的实践与脚手架方向。 +- Agnet 平台文档(部署与编排由平台侧提供)— 集成时以平台当前 API 与权限模型为准。 + +## 许可证 + +各子项目可能沿用不同开源许可证(例如 `new-api` 侧常见为 AGPLv3),请以各子目录内 `LICENSE` 为准。 diff --git a/_all-in-one-upload/cc-haha/adapters/README.md b/_all-in-one-upload/cc-haha/adapters/README.md new file mode 100644 index 0000000..fbc41f7 --- /dev/null +++ b/_all-in-one-upload/cc-haha/adapters/README.md @@ -0,0 +1,87 @@ +# Claude Code IM Adapters + +当前目录只放 IM Adapter 运行时代码。 + +用户文档已经迁移到 `docs/`,并且以 Desktop Webapp 配置流程为准: + +- `docs/im/index.md` +- `docs/im/telegram.md` +- `docs/im/feishu.md` + +## 当前方案摘要 + +当前真实链路是: + +```text +Desktop Webapp Settings + -> /api/adapters + -> ~/.claude/adapters.json + -> adapters//index.ts + -> /api/sessions + /ws/:sessionId + -> Claude Code session +``` + +注意两点: + +- IM 配置和配对都在 Desktop Webapp 的 `Settings -> IM 接入` +- Webapp 不会自动启动 Adapter 进程,仍需手动运行 `bun run telegram` 或 `bun run feishu` + +## 快速启动 + +```bash +cd adapters +bun install +bun run telegram +# 或 +bun run feishu +``` + +## 开发 + +### 运行测试 + +```bash +cd adapters +bun test +bun test common/ +bun test telegram/ +bun test feishu/ +``` + +### 目录结构 + +```text +adapters/ +├── common/ +│ └── attachment/ # 跨平台附件工具(types / limits / store / image-watcher) +├── telegram/ +│ └── media.ts # TelegramMediaService(grammy Bot API 封装) +├── feishu/ +│ ├── media.ts # FeishuMediaService(@larksuiteoapi/node-sdk 封装) +│ └── extract-payload.ts # 入站 im.message.receive_v1 事件解析 +├── package.json +├── tsconfig.json +└── README.md +``` + +## 附件收发 + +两个 Adapter 都支持双向图片/文件,和 Desktop 端走同一套 `AttachmentRef` 协议透传给主进程。 + +**入站(用户 → Claude):** + +- 飞书: 图片(jpg/png/gif/webp/heic)、文档(doc/xls/ppt/pdf 等)、post 富文本里的 img/file 元素 +- Telegram: photo、document、video、audio、voice + +下载落地到 `~/.claude/im-downloads/{platform}/{sessionId}/`,24 小时后自动 GC(`.part` 孤文件 10 分钟超时)。大小限制:单张图 ≤10 MB、单个文件 ≤30 MB,超限直接拒收并在 IM 里提示。 + +**出站(Claude → 用户):** + +Agent 流式文本里的 markdown 图片引用 `![alt](path|url|data:)` 会被 `ImageBlockWatcher` 识别、上传到 IM 平台,作为独立图片消息发出: + +- 飞书: `im.message.create(msg_type='image')` 单发(card 内嵌是后续优化) +- Telegram: `bot.api.sendPhoto(InputFile)` 单发 + +非图片类出站(Agent 产的 pdf/zip 等)暂不支持。 + +设计细节: `docs/superpowers/specs/2026-04-11-im-attachment-support-design.md`。 diff --git a/_all-in-one-upload/cc-haha/desktop/src-tauri/gen/schemas/acl-manifests.json b/_all-in-one-upload/cc-haha/desktop/src-tauri/gen/schemas/acl-manifests.json new file mode 100644 index 0000000..ff2a40c --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src-tauri/gen/schemas/acl-manifests.json @@ -0,0 +1 @@ +{"core":{"default_permission":{"identifier":"default","description":"Default core plugins set.","permissions":["core:path:default","core:event:default","core:window:default","core:webview:default","core:app:default","core:image:default","core:resources:default","core:menu:default","core:tray:default"]},"permissions":{},"permission_sets":{},"global_scope_schema":null},"core:app":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin.","permissions":["allow-version","allow-name","allow-tauri-version","allow-identifier","allow-bundle-type","allow-register-listener","allow-remove-listener"]},"permissions":{"allow-app-hide":{"identifier":"allow-app-hide","description":"Enables the app_hide command without any pre-configured scope.","commands":{"allow":["app_hide"],"deny":[]}},"allow-app-show":{"identifier":"allow-app-show","description":"Enables the app_show command without any pre-configured scope.","commands":{"allow":["app_show"],"deny":[]}},"allow-bundle-type":{"identifier":"allow-bundle-type","description":"Enables the bundle_type command without any pre-configured scope.","commands":{"allow":["bundle_type"],"deny":[]}},"allow-default-window-icon":{"identifier":"allow-default-window-icon","description":"Enables the default_window_icon command without any pre-configured scope.","commands":{"allow":["default_window_icon"],"deny":[]}},"allow-fetch-data-store-identifiers":{"identifier":"allow-fetch-data-store-identifiers","description":"Enables the fetch_data_store_identifiers command without any pre-configured scope.","commands":{"allow":["fetch_data_store_identifiers"],"deny":[]}},"allow-identifier":{"identifier":"allow-identifier","description":"Enables the identifier command without any pre-configured scope.","commands":{"allow":["identifier"],"deny":[]}},"allow-name":{"identifier":"allow-name","description":"Enables the name command without any pre-configured scope.","commands":{"allow":["name"],"deny":[]}},"allow-register-listener":{"identifier":"allow-register-listener","description":"Enables the register_listener command without any pre-configured scope.","commands":{"allow":["register_listener"],"deny":[]}},"allow-remove-data-store":{"identifier":"allow-remove-data-store","description":"Enables the remove_data_store command without any pre-configured scope.","commands":{"allow":["remove_data_store"],"deny":[]}},"allow-remove-listener":{"identifier":"allow-remove-listener","description":"Enables the remove_listener command without any pre-configured scope.","commands":{"allow":["remove_listener"],"deny":[]}},"allow-set-app-theme":{"identifier":"allow-set-app-theme","description":"Enables the set_app_theme command without any pre-configured scope.","commands":{"allow":["set_app_theme"],"deny":[]}},"allow-set-dock-visibility":{"identifier":"allow-set-dock-visibility","description":"Enables the set_dock_visibility command without any pre-configured scope.","commands":{"allow":["set_dock_visibility"],"deny":[]}},"allow-tauri-version":{"identifier":"allow-tauri-version","description":"Enables the tauri_version command without any pre-configured scope.","commands":{"allow":["tauri_version"],"deny":[]}},"allow-version":{"identifier":"allow-version","description":"Enables the version command without any pre-configured scope.","commands":{"allow":["version"],"deny":[]}},"deny-app-hide":{"identifier":"deny-app-hide","description":"Denies the app_hide command without any pre-configured scope.","commands":{"allow":[],"deny":["app_hide"]}},"deny-app-show":{"identifier":"deny-app-show","description":"Denies the app_show command without any pre-configured scope.","commands":{"allow":[],"deny":["app_show"]}},"deny-bundle-type":{"identifier":"deny-bundle-type","description":"Denies the bundle_type command without any pre-configured scope.","commands":{"allow":[],"deny":["bundle_type"]}},"deny-default-window-icon":{"identifier":"deny-default-window-icon","description":"Denies the default_window_icon command without any pre-configured scope.","commands":{"allow":[],"deny":["default_window_icon"]}},"deny-fetch-data-store-identifiers":{"identifier":"deny-fetch-data-store-identifiers","description":"Denies the fetch_data_store_identifiers command without any pre-configured scope.","commands":{"allow":[],"deny":["fetch_data_store_identifiers"]}},"deny-identifier":{"identifier":"deny-identifier","description":"Denies the identifier command without any pre-configured scope.","commands":{"allow":[],"deny":["identifier"]}},"deny-name":{"identifier":"deny-name","description":"Denies the name command without any pre-configured scope.","commands":{"allow":[],"deny":["name"]}},"deny-register-listener":{"identifier":"deny-register-listener","description":"Denies the register_listener command without any pre-configured scope.","commands":{"allow":[],"deny":["register_listener"]}},"deny-remove-data-store":{"identifier":"deny-remove-data-store","description":"Denies the remove_data_store command without any pre-configured scope.","commands":{"allow":[],"deny":["remove_data_store"]}},"deny-remove-listener":{"identifier":"deny-remove-listener","description":"Denies the remove_listener command without any pre-configured scope.","commands":{"allow":[],"deny":["remove_listener"]}},"deny-set-app-theme":{"identifier":"deny-set-app-theme","description":"Denies the set_app_theme command without any pre-configured scope.","commands":{"allow":[],"deny":["set_app_theme"]}},"deny-set-dock-visibility":{"identifier":"deny-set-dock-visibility","description":"Denies the set_dock_visibility command without any pre-configured scope.","commands":{"allow":[],"deny":["set_dock_visibility"]}},"deny-tauri-version":{"identifier":"deny-tauri-version","description":"Denies the tauri_version command without any pre-configured scope.","commands":{"allow":[],"deny":["tauri_version"]}},"deny-version":{"identifier":"deny-version","description":"Denies the version command without any pre-configured scope.","commands":{"allow":[],"deny":["version"]}}},"permission_sets":{},"global_scope_schema":null},"core:event":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin, which enables all commands.","permissions":["allow-listen","allow-unlisten","allow-emit","allow-emit-to"]},"permissions":{"allow-emit":{"identifier":"allow-emit","description":"Enables the emit command without any pre-configured scope.","commands":{"allow":["emit"],"deny":[]}},"allow-emit-to":{"identifier":"allow-emit-to","description":"Enables the emit_to command without any pre-configured scope.","commands":{"allow":["emit_to"],"deny":[]}},"allow-listen":{"identifier":"allow-listen","description":"Enables the listen command without any pre-configured scope.","commands":{"allow":["listen"],"deny":[]}},"allow-unlisten":{"identifier":"allow-unlisten","description":"Enables the unlisten command without any pre-configured scope.","commands":{"allow":["unlisten"],"deny":[]}},"deny-emit":{"identifier":"deny-emit","description":"Denies the emit command without any pre-configured scope.","commands":{"allow":[],"deny":["emit"]}},"deny-emit-to":{"identifier":"deny-emit-to","description":"Denies the emit_to command without any pre-configured scope.","commands":{"allow":[],"deny":["emit_to"]}},"deny-listen":{"identifier":"deny-listen","description":"Denies the listen command without any pre-configured scope.","commands":{"allow":[],"deny":["listen"]}},"deny-unlisten":{"identifier":"deny-unlisten","description":"Denies the unlisten command without any pre-configured scope.","commands":{"allow":[],"deny":["unlisten"]}}},"permission_sets":{},"global_scope_schema":null},"core:image":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin, which enables all commands.","permissions":["allow-new","allow-from-bytes","allow-from-path","allow-rgba","allow-size"]},"permissions":{"allow-from-bytes":{"identifier":"allow-from-bytes","description":"Enables the from_bytes command without any pre-configured scope.","commands":{"allow":["from_bytes"],"deny":[]}},"allow-from-path":{"identifier":"allow-from-path","description":"Enables the from_path command without any pre-configured scope.","commands":{"allow":["from_path"],"deny":[]}},"allow-new":{"identifier":"allow-new","description":"Enables the new command without any pre-configured scope.","commands":{"allow":["new"],"deny":[]}},"allow-rgba":{"identifier":"allow-rgba","description":"Enables the rgba command without any pre-configured scope.","commands":{"allow":["rgba"],"deny":[]}},"allow-size":{"identifier":"allow-size","description":"Enables the size command without any pre-configured scope.","commands":{"allow":["size"],"deny":[]}},"deny-from-bytes":{"identifier":"deny-from-bytes","description":"Denies the from_bytes command without any pre-configured scope.","commands":{"allow":[],"deny":["from_bytes"]}},"deny-from-path":{"identifier":"deny-from-path","description":"Denies the from_path command without any pre-configured scope.","commands":{"allow":[],"deny":["from_path"]}},"deny-new":{"identifier":"deny-new","description":"Denies the new command without any pre-configured scope.","commands":{"allow":[],"deny":["new"]}},"deny-rgba":{"identifier":"deny-rgba","description":"Denies the rgba command without any pre-configured scope.","commands":{"allow":[],"deny":["rgba"]}},"deny-size":{"identifier":"deny-size","description":"Denies the size command without any pre-configured scope.","commands":{"allow":[],"deny":["size"]}}},"permission_sets":{},"global_scope_schema":null},"core:menu":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin, which enables all commands.","permissions":["allow-new","allow-append","allow-prepend","allow-insert","allow-remove","allow-remove-at","allow-items","allow-get","allow-popup","allow-create-default","allow-set-as-app-menu","allow-set-as-window-menu","allow-text","allow-set-text","allow-is-enabled","allow-set-enabled","allow-set-accelerator","allow-set-as-windows-menu-for-nsapp","allow-set-as-help-menu-for-nsapp","allow-is-checked","allow-set-checked","allow-set-icon"]},"permissions":{"allow-append":{"identifier":"allow-append","description":"Enables the append command without any pre-configured scope.","commands":{"allow":["append"],"deny":[]}},"allow-create-default":{"identifier":"allow-create-default","description":"Enables the create_default command without any pre-configured scope.","commands":{"allow":["create_default"],"deny":[]}},"allow-get":{"identifier":"allow-get","description":"Enables the get command without any pre-configured scope.","commands":{"allow":["get"],"deny":[]}},"allow-insert":{"identifier":"allow-insert","description":"Enables the insert command without any pre-configured scope.","commands":{"allow":["insert"],"deny":[]}},"allow-is-checked":{"identifier":"allow-is-checked","description":"Enables the is_checked command without any pre-configured scope.","commands":{"allow":["is_checked"],"deny":[]}},"allow-is-enabled":{"identifier":"allow-is-enabled","description":"Enables the is_enabled command without any pre-configured scope.","commands":{"allow":["is_enabled"],"deny":[]}},"allow-items":{"identifier":"allow-items","description":"Enables the items command without any pre-configured scope.","commands":{"allow":["items"],"deny":[]}},"allow-new":{"identifier":"allow-new","description":"Enables the new command without any pre-configured scope.","commands":{"allow":["new"],"deny":[]}},"allow-popup":{"identifier":"allow-popup","description":"Enables the popup command without any pre-configured scope.","commands":{"allow":["popup"],"deny":[]}},"allow-prepend":{"identifier":"allow-prepend","description":"Enables the prepend command without any pre-configured scope.","commands":{"allow":["prepend"],"deny":[]}},"allow-remove":{"identifier":"allow-remove","description":"Enables the remove command without any pre-configured scope.","commands":{"allow":["remove"],"deny":[]}},"allow-remove-at":{"identifier":"allow-remove-at","description":"Enables the remove_at command without any pre-configured scope.","commands":{"allow":["remove_at"],"deny":[]}},"allow-set-accelerator":{"identifier":"allow-set-accelerator","description":"Enables the set_accelerator command without any pre-configured scope.","commands":{"allow":["set_accelerator"],"deny":[]}},"allow-set-as-app-menu":{"identifier":"allow-set-as-app-menu","description":"Enables the set_as_app_menu command without any pre-configured scope.","commands":{"allow":["set_as_app_menu"],"deny":[]}},"allow-set-as-help-menu-for-nsapp":{"identifier":"allow-set-as-help-menu-for-nsapp","description":"Enables the set_as_help_menu_for_nsapp command without any pre-configured scope.","commands":{"allow":["set_as_help_menu_for_nsapp"],"deny":[]}},"allow-set-as-window-menu":{"identifier":"allow-set-as-window-menu","description":"Enables the set_as_window_menu command without any pre-configured scope.","commands":{"allow":["set_as_window_menu"],"deny":[]}},"allow-set-as-windows-menu-for-nsapp":{"identifier":"allow-set-as-windows-menu-for-nsapp","description":"Enables the set_as_windows_menu_for_nsapp command without any pre-configured scope.","commands":{"allow":["set_as_windows_menu_for_nsapp"],"deny":[]}},"allow-set-checked":{"identifier":"allow-set-checked","description":"Enables the set_checked command without any pre-configured scope.","commands":{"allow":["set_checked"],"deny":[]}},"allow-set-enabled":{"identifier":"allow-set-enabled","description":"Enables the set_enabled command without any pre-configured scope.","commands":{"allow":["set_enabled"],"deny":[]}},"allow-set-icon":{"identifier":"allow-set-icon","description":"Enables the set_icon command without any pre-configured scope.","commands":{"allow":["set_icon"],"deny":[]}},"allow-set-text":{"identifier":"allow-set-text","description":"Enables the set_text command without any pre-configured scope.","commands":{"allow":["set_text"],"deny":[]}},"allow-text":{"identifier":"allow-text","description":"Enables the text command without any pre-configured scope.","commands":{"allow":["text"],"deny":[]}},"deny-append":{"identifier":"deny-append","description":"Denies the append command without any pre-configured scope.","commands":{"allow":[],"deny":["append"]}},"deny-create-default":{"identifier":"deny-create-default","description":"Denies the create_default command without any pre-configured scope.","commands":{"allow":[],"deny":["create_default"]}},"deny-get":{"identifier":"deny-get","description":"Denies the get command without any pre-configured scope.","commands":{"allow":[],"deny":["get"]}},"deny-insert":{"identifier":"deny-insert","description":"Denies the insert command without any pre-configured scope.","commands":{"allow":[],"deny":["insert"]}},"deny-is-checked":{"identifier":"deny-is-checked","description":"Denies the is_checked command without any pre-configured scope.","commands":{"allow":[],"deny":["is_checked"]}},"deny-is-enabled":{"identifier":"deny-is-enabled","description":"Denies the is_enabled command without any pre-configured scope.","commands":{"allow":[],"deny":["is_enabled"]}},"deny-items":{"identifier":"deny-items","description":"Denies the items command without any pre-configured scope.","commands":{"allow":[],"deny":["items"]}},"deny-new":{"identifier":"deny-new","description":"Denies the new command without any pre-configured scope.","commands":{"allow":[],"deny":["new"]}},"deny-popup":{"identifier":"deny-popup","description":"Denies the popup command without any pre-configured scope.","commands":{"allow":[],"deny":["popup"]}},"deny-prepend":{"identifier":"deny-prepend","description":"Denies the prepend command without any pre-configured scope.","commands":{"allow":[],"deny":["prepend"]}},"deny-remove":{"identifier":"deny-remove","description":"Denies the remove command without any pre-configured scope.","commands":{"allow":[],"deny":["remove"]}},"deny-remove-at":{"identifier":"deny-remove-at","description":"Denies the remove_at command without any pre-configured scope.","commands":{"allow":[],"deny":["remove_at"]}},"deny-set-accelerator":{"identifier":"deny-set-accelerator","description":"Denies the set_accelerator command without any pre-configured scope.","commands":{"allow":[],"deny":["set_accelerator"]}},"deny-set-as-app-menu":{"identifier":"deny-set-as-app-menu","description":"Denies the set_as_app_menu command without any pre-configured scope.","commands":{"allow":[],"deny":["set_as_app_menu"]}},"deny-set-as-help-menu-for-nsapp":{"identifier":"deny-set-as-help-menu-for-nsapp","description":"Denies the set_as_help_menu_for_nsapp command without any pre-configured scope.","commands":{"allow":[],"deny":["set_as_help_menu_for_nsapp"]}},"deny-set-as-window-menu":{"identifier":"deny-set-as-window-menu","description":"Denies the set_as_window_menu command without any pre-configured scope.","commands":{"allow":[],"deny":["set_as_window_menu"]}},"deny-set-as-windows-menu-for-nsapp":{"identifier":"deny-set-as-windows-menu-for-nsapp","description":"Denies the set_as_windows_menu_for_nsapp command without any pre-configured scope.","commands":{"allow":[],"deny":["set_as_windows_menu_for_nsapp"]}},"deny-set-checked":{"identifier":"deny-set-checked","description":"Denies the set_checked command without any pre-configured scope.","commands":{"allow":[],"deny":["set_checked"]}},"deny-set-enabled":{"identifier":"deny-set-enabled","description":"Denies the set_enabled command without any pre-configured scope.","commands":{"allow":[],"deny":["set_enabled"]}},"deny-set-icon":{"identifier":"deny-set-icon","description":"Denies the set_icon command without any pre-configured scope.","commands":{"allow":[],"deny":["set_icon"]}},"deny-set-text":{"identifier":"deny-set-text","description":"Denies the set_text command without any pre-configured scope.","commands":{"allow":[],"deny":["set_text"]}},"deny-text":{"identifier":"deny-text","description":"Denies the text command without any pre-configured scope.","commands":{"allow":[],"deny":["text"]}}},"permission_sets":{},"global_scope_schema":null},"core:path":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin, which enables all commands.","permissions":["allow-resolve-directory","allow-resolve","allow-normalize","allow-join","allow-dirname","allow-extname","allow-basename","allow-is-absolute"]},"permissions":{"allow-basename":{"identifier":"allow-basename","description":"Enables the basename command without any pre-configured scope.","commands":{"allow":["basename"],"deny":[]}},"allow-dirname":{"identifier":"allow-dirname","description":"Enables the dirname command without any pre-configured scope.","commands":{"allow":["dirname"],"deny":[]}},"allow-extname":{"identifier":"allow-extname","description":"Enables the extname command without any pre-configured scope.","commands":{"allow":["extname"],"deny":[]}},"allow-is-absolute":{"identifier":"allow-is-absolute","description":"Enables the is_absolute command without any pre-configured scope.","commands":{"allow":["is_absolute"],"deny":[]}},"allow-join":{"identifier":"allow-join","description":"Enables the join command without any pre-configured scope.","commands":{"allow":["join"],"deny":[]}},"allow-normalize":{"identifier":"allow-normalize","description":"Enables the normalize command without any pre-configured scope.","commands":{"allow":["normalize"],"deny":[]}},"allow-resolve":{"identifier":"allow-resolve","description":"Enables the resolve command without any pre-configured scope.","commands":{"allow":["resolve"],"deny":[]}},"allow-resolve-directory":{"identifier":"allow-resolve-directory","description":"Enables the resolve_directory command without any pre-configured scope.","commands":{"allow":["resolve_directory"],"deny":[]}},"deny-basename":{"identifier":"deny-basename","description":"Denies the basename command without any pre-configured scope.","commands":{"allow":[],"deny":["basename"]}},"deny-dirname":{"identifier":"deny-dirname","description":"Denies the dirname command without any pre-configured scope.","commands":{"allow":[],"deny":["dirname"]}},"deny-extname":{"identifier":"deny-extname","description":"Denies the extname command without any pre-configured scope.","commands":{"allow":[],"deny":["extname"]}},"deny-is-absolute":{"identifier":"deny-is-absolute","description":"Denies the is_absolute command without any pre-configured scope.","commands":{"allow":[],"deny":["is_absolute"]}},"deny-join":{"identifier":"deny-join","description":"Denies the join command without any pre-configured scope.","commands":{"allow":[],"deny":["join"]}},"deny-normalize":{"identifier":"deny-normalize","description":"Denies the normalize command without any pre-configured scope.","commands":{"allow":[],"deny":["normalize"]}},"deny-resolve":{"identifier":"deny-resolve","description":"Denies the resolve command without any pre-configured scope.","commands":{"allow":[],"deny":["resolve"]}},"deny-resolve-directory":{"identifier":"deny-resolve-directory","description":"Denies the resolve_directory command without any pre-configured scope.","commands":{"allow":[],"deny":["resolve_directory"]}}},"permission_sets":{},"global_scope_schema":null},"core:resources":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin, which enables all commands.","permissions":["allow-close"]},"permissions":{"allow-close":{"identifier":"allow-close","description":"Enables the close command without any pre-configured scope.","commands":{"allow":["close"],"deny":[]}},"deny-close":{"identifier":"deny-close","description":"Denies the close command without any pre-configured scope.","commands":{"allow":[],"deny":["close"]}}},"permission_sets":{},"global_scope_schema":null},"core:tray":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin, which enables all commands.","permissions":["allow-new","allow-get-by-id","allow-remove-by-id","allow-set-icon","allow-set-menu","allow-set-tooltip","allow-set-title","allow-set-visible","allow-set-temp-dir-path","allow-set-icon-as-template","allow-set-show-menu-on-left-click"]},"permissions":{"allow-get-by-id":{"identifier":"allow-get-by-id","description":"Enables the get_by_id command without any pre-configured scope.","commands":{"allow":["get_by_id"],"deny":[]}},"allow-new":{"identifier":"allow-new","description":"Enables the new command without any pre-configured scope.","commands":{"allow":["new"],"deny":[]}},"allow-remove-by-id":{"identifier":"allow-remove-by-id","description":"Enables the remove_by_id command without any pre-configured scope.","commands":{"allow":["remove_by_id"],"deny":[]}},"allow-set-icon":{"identifier":"allow-set-icon","description":"Enables the set_icon command without any pre-configured scope.","commands":{"allow":["set_icon"],"deny":[]}},"allow-set-icon-as-template":{"identifier":"allow-set-icon-as-template","description":"Enables the set_icon_as_template command without any pre-configured scope.","commands":{"allow":["set_icon_as_template"],"deny":[]}},"allow-set-menu":{"identifier":"allow-set-menu","description":"Enables the set_menu command without any pre-configured scope.","commands":{"allow":["set_menu"],"deny":[]}},"allow-set-show-menu-on-left-click":{"identifier":"allow-set-show-menu-on-left-click","description":"Enables the set_show_menu_on_left_click command without any pre-configured scope.","commands":{"allow":["set_show_menu_on_left_click"],"deny":[]}},"allow-set-temp-dir-path":{"identifier":"allow-set-temp-dir-path","description":"Enables the set_temp_dir_path command without any pre-configured scope.","commands":{"allow":["set_temp_dir_path"],"deny":[]}},"allow-set-title":{"identifier":"allow-set-title","description":"Enables the set_title command without any pre-configured scope.","commands":{"allow":["set_title"],"deny":[]}},"allow-set-tooltip":{"identifier":"allow-set-tooltip","description":"Enables the set_tooltip command without any pre-configured scope.","commands":{"allow":["set_tooltip"],"deny":[]}},"allow-set-visible":{"identifier":"allow-set-visible","description":"Enables the set_visible command without any pre-configured scope.","commands":{"allow":["set_visible"],"deny":[]}},"deny-get-by-id":{"identifier":"deny-get-by-id","description":"Denies the get_by_id command without any pre-configured scope.","commands":{"allow":[],"deny":["get_by_id"]}},"deny-new":{"identifier":"deny-new","description":"Denies the new command without any pre-configured scope.","commands":{"allow":[],"deny":["new"]}},"deny-remove-by-id":{"identifier":"deny-remove-by-id","description":"Denies the remove_by_id command without any pre-configured scope.","commands":{"allow":[],"deny":["remove_by_id"]}},"deny-set-icon":{"identifier":"deny-set-icon","description":"Denies the set_icon command without any pre-configured scope.","commands":{"allow":[],"deny":["set_icon"]}},"deny-set-icon-as-template":{"identifier":"deny-set-icon-as-template","description":"Denies the set_icon_as_template command without any pre-configured scope.","commands":{"allow":[],"deny":["set_icon_as_template"]}},"deny-set-menu":{"identifier":"deny-set-menu","description":"Denies the set_menu command without any pre-configured scope.","commands":{"allow":[],"deny":["set_menu"]}},"deny-set-show-menu-on-left-click":{"identifier":"deny-set-show-menu-on-left-click","description":"Denies the set_show_menu_on_left_click command without any pre-configured scope.","commands":{"allow":[],"deny":["set_show_menu_on_left_click"]}},"deny-set-temp-dir-path":{"identifier":"deny-set-temp-dir-path","description":"Denies the set_temp_dir_path command without any pre-configured scope.","commands":{"allow":[],"deny":["set_temp_dir_path"]}},"deny-set-title":{"identifier":"deny-set-title","description":"Denies the set_title command without any pre-configured scope.","commands":{"allow":[],"deny":["set_title"]}},"deny-set-tooltip":{"identifier":"deny-set-tooltip","description":"Denies the set_tooltip command without any pre-configured scope.","commands":{"allow":[],"deny":["set_tooltip"]}},"deny-set-visible":{"identifier":"deny-set-visible","description":"Denies the set_visible command without any pre-configured scope.","commands":{"allow":[],"deny":["set_visible"]}}},"permission_sets":{},"global_scope_schema":null},"core:webview":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin.","permissions":["allow-get-all-webviews","allow-webview-position","allow-webview-size","allow-internal-toggle-devtools"]},"permissions":{"allow-clear-all-browsing-data":{"identifier":"allow-clear-all-browsing-data","description":"Enables the clear_all_browsing_data command without any pre-configured scope.","commands":{"allow":["clear_all_browsing_data"],"deny":[]}},"allow-create-webview":{"identifier":"allow-create-webview","description":"Enables the create_webview command without any pre-configured scope.","commands":{"allow":["create_webview"],"deny":[]}},"allow-create-webview-window":{"identifier":"allow-create-webview-window","description":"Enables the create_webview_window command without any pre-configured scope.","commands":{"allow":["create_webview_window"],"deny":[]}},"allow-get-all-webviews":{"identifier":"allow-get-all-webviews","description":"Enables the get_all_webviews command without any pre-configured scope.","commands":{"allow":["get_all_webviews"],"deny":[]}},"allow-internal-toggle-devtools":{"identifier":"allow-internal-toggle-devtools","description":"Enables the internal_toggle_devtools command without any pre-configured scope.","commands":{"allow":["internal_toggle_devtools"],"deny":[]}},"allow-print":{"identifier":"allow-print","description":"Enables the print command without any pre-configured scope.","commands":{"allow":["print"],"deny":[]}},"allow-reparent":{"identifier":"allow-reparent","description":"Enables the reparent command without any pre-configured scope.","commands":{"allow":["reparent"],"deny":[]}},"allow-set-webview-auto-resize":{"identifier":"allow-set-webview-auto-resize","description":"Enables the set_webview_auto_resize command without any pre-configured scope.","commands":{"allow":["set_webview_auto_resize"],"deny":[]}},"allow-set-webview-background-color":{"identifier":"allow-set-webview-background-color","description":"Enables the set_webview_background_color command without any pre-configured scope.","commands":{"allow":["set_webview_background_color"],"deny":[]}},"allow-set-webview-focus":{"identifier":"allow-set-webview-focus","description":"Enables the set_webview_focus command without any pre-configured scope.","commands":{"allow":["set_webview_focus"],"deny":[]}},"allow-set-webview-position":{"identifier":"allow-set-webview-position","description":"Enables the set_webview_position command without any pre-configured scope.","commands":{"allow":["set_webview_position"],"deny":[]}},"allow-set-webview-size":{"identifier":"allow-set-webview-size","description":"Enables the set_webview_size command without any pre-configured scope.","commands":{"allow":["set_webview_size"],"deny":[]}},"allow-set-webview-zoom":{"identifier":"allow-set-webview-zoom","description":"Enables the set_webview_zoom command without any pre-configured scope.","commands":{"allow":["set_webview_zoom"],"deny":[]}},"allow-webview-close":{"identifier":"allow-webview-close","description":"Enables the webview_close command without any pre-configured scope.","commands":{"allow":["webview_close"],"deny":[]}},"allow-webview-hide":{"identifier":"allow-webview-hide","description":"Enables the webview_hide command without any pre-configured scope.","commands":{"allow":["webview_hide"],"deny":[]}},"allow-webview-position":{"identifier":"allow-webview-position","description":"Enables the webview_position command without any pre-configured scope.","commands":{"allow":["webview_position"],"deny":[]}},"allow-webview-show":{"identifier":"allow-webview-show","description":"Enables the webview_show command without any pre-configured scope.","commands":{"allow":["webview_show"],"deny":[]}},"allow-webview-size":{"identifier":"allow-webview-size","description":"Enables the webview_size command without any pre-configured scope.","commands":{"allow":["webview_size"],"deny":[]}},"deny-clear-all-browsing-data":{"identifier":"deny-clear-all-browsing-data","description":"Denies the clear_all_browsing_data command without any pre-configured scope.","commands":{"allow":[],"deny":["clear_all_browsing_data"]}},"deny-create-webview":{"identifier":"deny-create-webview","description":"Denies the create_webview command without any pre-configured scope.","commands":{"allow":[],"deny":["create_webview"]}},"deny-create-webview-window":{"identifier":"deny-create-webview-window","description":"Denies the create_webview_window command without any pre-configured scope.","commands":{"allow":[],"deny":["create_webview_window"]}},"deny-get-all-webviews":{"identifier":"deny-get-all-webviews","description":"Denies the get_all_webviews command without any pre-configured scope.","commands":{"allow":[],"deny":["get_all_webviews"]}},"deny-internal-toggle-devtools":{"identifier":"deny-internal-toggle-devtools","description":"Denies the internal_toggle_devtools command without any pre-configured scope.","commands":{"allow":[],"deny":["internal_toggle_devtools"]}},"deny-print":{"identifier":"deny-print","description":"Denies the print command without any pre-configured scope.","commands":{"allow":[],"deny":["print"]}},"deny-reparent":{"identifier":"deny-reparent","description":"Denies the reparent command without any pre-configured scope.","commands":{"allow":[],"deny":["reparent"]}},"deny-set-webview-auto-resize":{"identifier":"deny-set-webview-auto-resize","description":"Denies the set_webview_auto_resize command without any pre-configured scope.","commands":{"allow":[],"deny":["set_webview_auto_resize"]}},"deny-set-webview-background-color":{"identifier":"deny-set-webview-background-color","description":"Denies the set_webview_background_color command without any pre-configured scope.","commands":{"allow":[],"deny":["set_webview_background_color"]}},"deny-set-webview-focus":{"identifier":"deny-set-webview-focus","description":"Denies the set_webview_focus command without any pre-configured scope.","commands":{"allow":[],"deny":["set_webview_focus"]}},"deny-set-webview-position":{"identifier":"deny-set-webview-position","description":"Denies the set_webview_position command without any pre-configured scope.","commands":{"allow":[],"deny":["set_webview_position"]}},"deny-set-webview-size":{"identifier":"deny-set-webview-size","description":"Denies the set_webview_size command without any pre-configured scope.","commands":{"allow":[],"deny":["set_webview_size"]}},"deny-set-webview-zoom":{"identifier":"deny-set-webview-zoom","description":"Denies the set_webview_zoom command without any pre-configured scope.","commands":{"allow":[],"deny":["set_webview_zoom"]}},"deny-webview-close":{"identifier":"deny-webview-close","description":"Denies the webview_close command without any pre-configured scope.","commands":{"allow":[],"deny":["webview_close"]}},"deny-webview-hide":{"identifier":"deny-webview-hide","description":"Denies the webview_hide command without any pre-configured scope.","commands":{"allow":[],"deny":["webview_hide"]}},"deny-webview-position":{"identifier":"deny-webview-position","description":"Denies the webview_position command without any pre-configured scope.","commands":{"allow":[],"deny":["webview_position"]}},"deny-webview-show":{"identifier":"deny-webview-show","description":"Denies the webview_show command without any pre-configured scope.","commands":{"allow":[],"deny":["webview_show"]}},"deny-webview-size":{"identifier":"deny-webview-size","description":"Denies the webview_size command without any pre-configured scope.","commands":{"allow":[],"deny":["webview_size"]}}},"permission_sets":{},"global_scope_schema":null},"core:window":{"default_permission":{"identifier":"default","description":"Default permissions for the plugin.","permissions":["allow-get-all-windows","allow-scale-factor","allow-inner-position","allow-outer-position","allow-inner-size","allow-outer-size","allow-is-fullscreen","allow-is-minimized","allow-is-maximized","allow-is-focused","allow-is-decorated","allow-is-resizable","allow-is-maximizable","allow-is-minimizable","allow-is-closable","allow-is-visible","allow-is-enabled","allow-title","allow-current-monitor","allow-primary-monitor","allow-monitor-from-point","allow-available-monitors","allow-cursor-position","allow-theme","allow-is-always-on-top","allow-internal-toggle-maximize"]},"permissions":{"allow-available-monitors":{"identifier":"allow-available-monitors","description":"Enables the available_monitors command without any pre-configured scope.","commands":{"allow":["available_monitors"],"deny":[]}},"allow-center":{"identifier":"allow-center","description":"Enables the center command without any pre-configured scope.","commands":{"allow":["center"],"deny":[]}},"allow-close":{"identifier":"allow-close","description":"Enables the close command without any pre-configured scope.","commands":{"allow":["close"],"deny":[]}},"allow-create":{"identifier":"allow-create","description":"Enables the create command without any pre-configured scope.","commands":{"allow":["create"],"deny":[]}},"allow-current-monitor":{"identifier":"allow-current-monitor","description":"Enables the current_monitor command without any pre-configured scope.","commands":{"allow":["current_monitor"],"deny":[]}},"allow-cursor-position":{"identifier":"allow-cursor-position","description":"Enables the cursor_position command without any pre-configured scope.","commands":{"allow":["cursor_position"],"deny":[]}},"allow-destroy":{"identifier":"allow-destroy","description":"Enables the destroy command without any pre-configured scope.","commands":{"allow":["destroy"],"deny":[]}},"allow-get-all-windows":{"identifier":"allow-get-all-windows","description":"Enables the get_all_windows command without any pre-configured scope.","commands":{"allow":["get_all_windows"],"deny":[]}},"allow-hide":{"identifier":"allow-hide","description":"Enables the hide command without any pre-configured scope.","commands":{"allow":["hide"],"deny":[]}},"allow-inner-position":{"identifier":"allow-inner-position","description":"Enables the inner_position command without any pre-configured scope.","commands":{"allow":["inner_position"],"deny":[]}},"allow-inner-size":{"identifier":"allow-inner-size","description":"Enables the inner_size command without any pre-configured scope.","commands":{"allow":["inner_size"],"deny":[]}},"allow-internal-toggle-maximize":{"identifier":"allow-internal-toggle-maximize","description":"Enables the internal_toggle_maximize command without any pre-configured scope.","commands":{"allow":["internal_toggle_maximize"],"deny":[]}},"allow-is-always-on-top":{"identifier":"allow-is-always-on-top","description":"Enables the is_always_on_top command without any pre-configured scope.","commands":{"allow":["is_always_on_top"],"deny":[]}},"allow-is-closable":{"identifier":"allow-is-closable","description":"Enables the is_closable command without any pre-configured scope.","commands":{"allow":["is_closable"],"deny":[]}},"allow-is-decorated":{"identifier":"allow-is-decorated","description":"Enables the is_decorated command without any pre-configured scope.","commands":{"allow":["is_decorated"],"deny":[]}},"allow-is-enabled":{"identifier":"allow-is-enabled","description":"Enables the is_enabled command without any pre-configured scope.","commands":{"allow":["is_enabled"],"deny":[]}},"allow-is-focused":{"identifier":"allow-is-focused","description":"Enables the is_focused command without any pre-configured scope.","commands":{"allow":["is_focused"],"deny":[]}},"allow-is-fullscreen":{"identifier":"allow-is-fullscreen","description":"Enables the is_fullscreen command without any pre-configured scope.","commands":{"allow":["is_fullscreen"],"deny":[]}},"allow-is-maximizable":{"identifier":"allow-is-maximizable","description":"Enables the is_maximizable command without any pre-configured scope.","commands":{"allow":["is_maximizable"],"deny":[]}},"allow-is-maximized":{"identifier":"allow-is-maximized","description":"Enables the is_maximized command without any pre-configured scope.","commands":{"allow":["is_maximized"],"deny":[]}},"allow-is-minimizable":{"identifier":"allow-is-minimizable","description":"Enables the is_minimizable command without any pre-configured scope.","commands":{"allow":["is_minimizable"],"deny":[]}},"allow-is-minimized":{"identifier":"allow-is-minimized","description":"Enables the is_minimized command without any pre-configured scope.","commands":{"allow":["is_minimized"],"deny":[]}},"allow-is-resizable":{"identifier":"allow-is-resizable","description":"Enables the is_resizable command without any pre-configured scope.","commands":{"allow":["is_resizable"],"deny":[]}},"allow-is-visible":{"identifier":"allow-is-visible","description":"Enables the is_visible command without any pre-configured scope.","commands":{"allow":["is_visible"],"deny":[]}},"allow-maximize":{"identifier":"allow-maximize","description":"Enables the maximize command without any pre-configured scope.","commands":{"allow":["maximize"],"deny":[]}},"allow-minimize":{"identifier":"allow-minimize","description":"Enables the minimize command without any pre-configured scope.","commands":{"allow":["minimize"],"deny":[]}},"allow-monitor-from-point":{"identifier":"allow-monitor-from-point","description":"Enables the monitor_from_point command without any pre-configured scope.","commands":{"allow":["monitor_from_point"],"deny":[]}},"allow-outer-position":{"identifier":"allow-outer-position","description":"Enables the outer_position command without any pre-configured scope.","commands":{"allow":["outer_position"],"deny":[]}},"allow-outer-size":{"identifier":"allow-outer-size","description":"Enables the outer_size command without any pre-configured scope.","commands":{"allow":["outer_size"],"deny":[]}},"allow-primary-monitor":{"identifier":"allow-primary-monitor","description":"Enables the primary_monitor command without any pre-configured scope.","commands":{"allow":["primary_monitor"],"deny":[]}},"allow-request-user-attention":{"identifier":"allow-request-user-attention","description":"Enables the request_user_attention command without any pre-configured scope.","commands":{"allow":["request_user_attention"],"deny":[]}},"allow-scale-factor":{"identifier":"allow-scale-factor","description":"Enables the scale_factor command without any pre-configured scope.","commands":{"allow":["scale_factor"],"deny":[]}},"allow-set-always-on-bottom":{"identifier":"allow-set-always-on-bottom","description":"Enables the set_always_on_bottom command without any pre-configured scope.","commands":{"allow":["set_always_on_bottom"],"deny":[]}},"allow-set-always-on-top":{"identifier":"allow-set-always-on-top","description":"Enables the set_always_on_top command without any pre-configured scope.","commands":{"allow":["set_always_on_top"],"deny":[]}},"allow-set-background-color":{"identifier":"allow-set-background-color","description":"Enables the set_background_color command without any pre-configured scope.","commands":{"allow":["set_background_color"],"deny":[]}},"allow-set-badge-count":{"identifier":"allow-set-badge-count","description":"Enables the set_badge_count command without any pre-configured scope.","commands":{"allow":["set_badge_count"],"deny":[]}},"allow-set-badge-label":{"identifier":"allow-set-badge-label","description":"Enables the set_badge_label command without any pre-configured scope.","commands":{"allow":["set_badge_label"],"deny":[]}},"allow-set-closable":{"identifier":"allow-set-closable","description":"Enables the set_closable command without any pre-configured scope.","commands":{"allow":["set_closable"],"deny":[]}},"allow-set-content-protected":{"identifier":"allow-set-content-protected","description":"Enables the set_content_protected command without any pre-configured scope.","commands":{"allow":["set_content_protected"],"deny":[]}},"allow-set-cursor-grab":{"identifier":"allow-set-cursor-grab","description":"Enables the set_cursor_grab command without any pre-configured scope.","commands":{"allow":["set_cursor_grab"],"deny":[]}},"allow-set-cursor-icon":{"identifier":"allow-set-cursor-icon","description":"Enables the set_cursor_icon command without any pre-configured scope.","commands":{"allow":["set_cursor_icon"],"deny":[]}},"allow-set-cursor-position":{"identifier":"allow-set-cursor-position","description":"Enables the set_cursor_position command without any pre-configured scope.","commands":{"allow":["set_cursor_position"],"deny":[]}},"allow-set-cursor-visible":{"identifier":"allow-set-cursor-visible","description":"Enables the set_cursor_visible command without any pre-configured scope.","commands":{"allow":["set_cursor_visible"],"deny":[]}},"allow-set-decorations":{"identifier":"allow-set-decorations","description":"Enables the set_decorations command without any pre-configured scope.","commands":{"allow":["set_decorations"],"deny":[]}},"allow-set-effects":{"identifier":"allow-set-effects","description":"Enables the set_effects command without any pre-configured scope.","commands":{"allow":["set_effects"],"deny":[]}},"allow-set-enabled":{"identifier":"allow-set-enabled","description":"Enables the set_enabled command without any pre-configured scope.","commands":{"allow":["set_enabled"],"deny":[]}},"allow-set-focus":{"identifier":"allow-set-focus","description":"Enables the set_focus command without any pre-configured scope.","commands":{"allow":["set_focus"],"deny":[]}},"allow-set-focusable":{"identifier":"allow-set-focusable","description":"Enables the set_focusable command without any pre-configured scope.","commands":{"allow":["set_focusable"],"deny":[]}},"allow-set-fullscreen":{"identifier":"allow-set-fullscreen","description":"Enables the set_fullscreen command without any pre-configured scope.","commands":{"allow":["set_fullscreen"],"deny":[]}},"allow-set-icon":{"identifier":"allow-set-icon","description":"Enables the set_icon command without any pre-configured scope.","commands":{"allow":["set_icon"],"deny":[]}},"allow-set-ignore-cursor-events":{"identifier":"allow-set-ignore-cursor-events","description":"Enables the set_ignore_cursor_events command without any pre-configured scope.","commands":{"allow":["set_ignore_cursor_events"],"deny":[]}},"allow-set-max-size":{"identifier":"allow-set-max-size","description":"Enables the set_max_size command without any pre-configured scope.","commands":{"allow":["set_max_size"],"deny":[]}},"allow-set-maximizable":{"identifier":"allow-set-maximizable","description":"Enables the set_maximizable command without any pre-configured scope.","commands":{"allow":["set_maximizable"],"deny":[]}},"allow-set-min-size":{"identifier":"allow-set-min-size","description":"Enables the set_min_size command without any pre-configured scope.","commands":{"allow":["set_min_size"],"deny":[]}},"allow-set-minimizable":{"identifier":"allow-set-minimizable","description":"Enables the set_minimizable command without any pre-configured scope.","commands":{"allow":["set_minimizable"],"deny":[]}},"allow-set-overlay-icon":{"identifier":"allow-set-overlay-icon","description":"Enables the set_overlay_icon command without any pre-configured scope.","commands":{"allow":["set_overlay_icon"],"deny":[]}},"allow-set-position":{"identifier":"allow-set-position","description":"Enables the set_position command without any pre-configured scope.","commands":{"allow":["set_position"],"deny":[]}},"allow-set-progress-bar":{"identifier":"allow-set-progress-bar","description":"Enables the set_progress_bar command without any pre-configured scope.","commands":{"allow":["set_progress_bar"],"deny":[]}},"allow-set-resizable":{"identifier":"allow-set-resizable","description":"Enables the set_resizable command without any pre-configured scope.","commands":{"allow":["set_resizable"],"deny":[]}},"allow-set-shadow":{"identifier":"allow-set-shadow","description":"Enables the set_shadow command without any pre-configured scope.","commands":{"allow":["set_shadow"],"deny":[]}},"allow-set-simple-fullscreen":{"identifier":"allow-set-simple-fullscreen","description":"Enables the set_simple_fullscreen command without any pre-configured scope.","commands":{"allow":["set_simple_fullscreen"],"deny":[]}},"allow-set-size":{"identifier":"allow-set-size","description":"Enables the set_size command without any pre-configured scope.","commands":{"allow":["set_size"],"deny":[]}},"allow-set-size-constraints":{"identifier":"allow-set-size-constraints","description":"Enables the set_size_constraints command without any pre-configured scope.","commands":{"allow":["set_size_constraints"],"deny":[]}},"allow-set-skip-taskbar":{"identifier":"allow-set-skip-taskbar","description":"Enables the set_skip_taskbar command without any pre-configured scope.","commands":{"allow":["set_skip_taskbar"],"deny":[]}},"allow-set-theme":{"identifier":"allow-set-theme","description":"Enables the set_theme command without any pre-configured scope.","commands":{"allow":["set_theme"],"deny":[]}},"allow-set-title":{"identifier":"allow-set-title","description":"Enables the set_title command without any pre-configured scope.","commands":{"allow":["set_title"],"deny":[]}},"allow-set-title-bar-style":{"identifier":"allow-set-title-bar-style","description":"Enables the set_title_bar_style command without any pre-configured scope.","commands":{"allow":["set_title_bar_style"],"deny":[]}},"allow-set-visible-on-all-workspaces":{"identifier":"allow-set-visible-on-all-workspaces","description":"Enables the set_visible_on_all_workspaces command without any pre-configured scope.","commands":{"allow":["set_visible_on_all_workspaces"],"deny":[]}},"allow-show":{"identifier":"allow-show","description":"Enables the show command without any pre-configured scope.","commands":{"allow":["show"],"deny":[]}},"allow-start-dragging":{"identifier":"allow-start-dragging","description":"Enables the start_dragging command without any pre-configured scope.","commands":{"allow":["start_dragging"],"deny":[]}},"allow-start-resize-dragging":{"identifier":"allow-start-resize-dragging","description":"Enables the start_resize_dragging command without any pre-configured scope.","commands":{"allow":["start_resize_dragging"],"deny":[]}},"allow-theme":{"identifier":"allow-theme","description":"Enables the theme command without any pre-configured scope.","commands":{"allow":["theme"],"deny":[]}},"allow-title":{"identifier":"allow-title","description":"Enables the title command without any pre-configured scope.","commands":{"allow":["title"],"deny":[]}},"allow-toggle-maximize":{"identifier":"allow-toggle-maximize","description":"Enables the toggle_maximize command without any pre-configured scope.","commands":{"allow":["toggle_maximize"],"deny":[]}},"allow-unmaximize":{"identifier":"allow-unmaximize","description":"Enables the unmaximize command without any pre-configured scope.","commands":{"allow":["unmaximize"],"deny":[]}},"allow-unminimize":{"identifier":"allow-unminimize","description":"Enables the unminimize command without any pre-configured scope.","commands":{"allow":["unminimize"],"deny":[]}},"deny-available-monitors":{"identifier":"deny-available-monitors","description":"Denies the available_monitors command without any pre-configured scope.","commands":{"allow":[],"deny":["available_monitors"]}},"deny-center":{"identifier":"deny-center","description":"Denies the center command without any pre-configured scope.","commands":{"allow":[],"deny":["center"]}},"deny-close":{"identifier":"deny-close","description":"Denies the close command without any pre-configured scope.","commands":{"allow":[],"deny":["close"]}},"deny-create":{"identifier":"deny-create","description":"Denies the create command without any pre-configured scope.","commands":{"allow":[],"deny":["create"]}},"deny-current-monitor":{"identifier":"deny-current-monitor","description":"Denies the current_monitor command without any pre-configured scope.","commands":{"allow":[],"deny":["current_monitor"]}},"deny-cursor-position":{"identifier":"deny-cursor-position","description":"Denies the cursor_position command without any pre-configured scope.","commands":{"allow":[],"deny":["cursor_position"]}},"deny-destroy":{"identifier":"deny-destroy","description":"Denies the destroy command without any pre-configured scope.","commands":{"allow":[],"deny":["destroy"]}},"deny-get-all-windows":{"identifier":"deny-get-all-windows","description":"Denies the get_all_windows command without any pre-configured scope.","commands":{"allow":[],"deny":["get_all_windows"]}},"deny-hide":{"identifier":"deny-hide","description":"Denies the hide command without any pre-configured scope.","commands":{"allow":[],"deny":["hide"]}},"deny-inner-position":{"identifier":"deny-inner-position","description":"Denies the inner_position command without any pre-configured scope.","commands":{"allow":[],"deny":["inner_position"]}},"deny-inner-size":{"identifier":"deny-inner-size","description":"Denies the inner_size command without any pre-configured scope.","commands":{"allow":[],"deny":["inner_size"]}},"deny-internal-toggle-maximize":{"identifier":"deny-internal-toggle-maximize","description":"Denies the internal_toggle_maximize command without any pre-configured scope.","commands":{"allow":[],"deny":["internal_toggle_maximize"]}},"deny-is-always-on-top":{"identifier":"deny-is-always-on-top","description":"Denies the is_always_on_top command without any pre-configured scope.","commands":{"allow":[],"deny":["is_always_on_top"]}},"deny-is-closable":{"identifier":"deny-is-closable","description":"Denies the is_closable command without any pre-configured scope.","commands":{"allow":[],"deny":["is_closable"]}},"deny-is-decorated":{"identifier":"deny-is-decorated","description":"Denies the is_decorated command without any pre-configured scope.","commands":{"allow":[],"deny":["is_decorated"]}},"deny-is-enabled":{"identifier":"deny-is-enabled","description":"Denies the is_enabled command without any pre-configured scope.","commands":{"allow":[],"deny":["is_enabled"]}},"deny-is-focused":{"identifier":"deny-is-focused","description":"Denies the is_focused command without any pre-configured scope.","commands":{"allow":[],"deny":["is_focused"]}},"deny-is-fullscreen":{"identifier":"deny-is-fullscreen","description":"Denies the is_fullscreen command without any pre-configured scope.","commands":{"allow":[],"deny":["is_fullscreen"]}},"deny-is-maximizable":{"identifier":"deny-is-maximizable","description":"Denies the is_maximizable command without any pre-configured scope.","commands":{"allow":[],"deny":["is_maximizable"]}},"deny-is-maximized":{"identifier":"deny-is-maximized","description":"Denies the is_maximized command without any pre-configured scope.","commands":{"allow":[],"deny":["is_maximized"]}},"deny-is-minimizable":{"identifier":"deny-is-minimizable","description":"Denies the is_minimizable command without any pre-configured scope.","commands":{"allow":[],"deny":["is_minimizable"]}},"deny-is-minimized":{"identifier":"deny-is-minimized","description":"Denies the is_minimized command without any pre-configured scope.","commands":{"allow":[],"deny":["is_minimized"]}},"deny-is-resizable":{"identifier":"deny-is-resizable","description":"Denies the is_resizable command without any pre-configured scope.","commands":{"allow":[],"deny":["is_resizable"]}},"deny-is-visible":{"identifier":"deny-is-visible","description":"Denies the is_visible command without any pre-configured scope.","commands":{"allow":[],"deny":["is_visible"]}},"deny-maximize":{"identifier":"deny-maximize","description":"Denies the maximize command without any pre-configured scope.","commands":{"allow":[],"deny":["maximize"]}},"deny-minimize":{"identifier":"deny-minimize","description":"Denies the minimize command without any pre-configured scope.","commands":{"allow":[],"deny":["minimize"]}},"deny-monitor-from-point":{"identifier":"deny-monitor-from-point","description":"Denies the monitor_from_point command without any pre-configured scope.","commands":{"allow":[],"deny":["monitor_from_point"]}},"deny-outer-position":{"identifier":"deny-outer-position","description":"Denies the outer_position command without any pre-configured scope.","commands":{"allow":[],"deny":["outer_position"]}},"deny-outer-size":{"identifier":"deny-outer-size","description":"Denies the outer_size command without any pre-configured scope.","commands":{"allow":[],"deny":["outer_size"]}},"deny-primary-monitor":{"identifier":"deny-primary-monitor","description":"Denies the primary_monitor command without any pre-configured scope.","commands":{"allow":[],"deny":["primary_monitor"]}},"deny-request-user-attention":{"identifier":"deny-request-user-attention","description":"Denies the request_user_attention command without any pre-configured scope.","commands":{"allow":[],"deny":["request_user_attention"]}},"deny-scale-factor":{"identifier":"deny-scale-factor","description":"Denies the scale_factor command without any pre-configured scope.","commands":{"allow":[],"deny":["scale_factor"]}},"deny-set-always-on-bottom":{"identifier":"deny-set-always-on-bottom","description":"Denies the set_always_on_bottom command without any pre-configured scope.","commands":{"allow":[],"deny":["set_always_on_bottom"]}},"deny-set-always-on-top":{"identifier":"deny-set-always-on-top","description":"Denies the set_always_on_top command without any pre-configured scope.","commands":{"allow":[],"deny":["set_always_on_top"]}},"deny-set-background-color":{"identifier":"deny-set-background-color","description":"Denies the set_background_color command without any pre-configured scope.","commands":{"allow":[],"deny":["set_background_color"]}},"deny-set-badge-count":{"identifier":"deny-set-badge-count","description":"Denies the set_badge_count command without any pre-configured scope.","commands":{"allow":[],"deny":["set_badge_count"]}},"deny-set-badge-label":{"identifier":"deny-set-badge-label","description":"Denies the set_badge_label command without any pre-configured scope.","commands":{"allow":[],"deny":["set_badge_label"]}},"deny-set-closable":{"identifier":"deny-set-closable","description":"Denies the set_closable command without any pre-configured scope.","commands":{"allow":[],"deny":["set_closable"]}},"deny-set-content-protected":{"identifier":"deny-set-content-protected","description":"Denies the set_content_protected command without any pre-configured scope.","commands":{"allow":[],"deny":["set_content_protected"]}},"deny-set-cursor-grab":{"identifier":"deny-set-cursor-grab","description":"Denies the set_cursor_grab command without any pre-configured scope.","commands":{"allow":[],"deny":["set_cursor_grab"]}},"deny-set-cursor-icon":{"identifier":"deny-set-cursor-icon","description":"Denies the set_cursor_icon command without any pre-configured scope.","commands":{"allow":[],"deny":["set_cursor_icon"]}},"deny-set-cursor-position":{"identifier":"deny-set-cursor-position","description":"Denies the set_cursor_position command without any pre-configured scope.","commands":{"allow":[],"deny":["set_cursor_position"]}},"deny-set-cursor-visible":{"identifier":"deny-set-cursor-visible","description":"Denies the set_cursor_visible command without any pre-configured scope.","commands":{"allow":[],"deny":["set_cursor_visible"]}},"deny-set-decorations":{"identifier":"deny-set-decorations","description":"Denies the set_decorations command without any pre-configured scope.","commands":{"allow":[],"deny":["set_decorations"]}},"deny-set-effects":{"identifier":"deny-set-effects","description":"Denies the set_effects command without any pre-configured scope.","commands":{"allow":[],"deny":["set_effects"]}},"deny-set-enabled":{"identifier":"deny-set-enabled","description":"Denies the set_enabled command without any pre-configured scope.","commands":{"allow":[],"deny":["set_enabled"]}},"deny-set-focus":{"identifier":"deny-set-focus","description":"Denies the set_focus command without any pre-configured scope.","commands":{"allow":[],"deny":["set_focus"]}},"deny-set-focusable":{"identifier":"deny-set-focusable","description":"Denies the set_focusable command without any pre-configured scope.","commands":{"allow":[],"deny":["set_focusable"]}},"deny-set-fullscreen":{"identifier":"deny-set-fullscreen","description":"Denies the set_fullscreen command without any pre-configured scope.","commands":{"allow":[],"deny":["set_fullscreen"]}},"deny-set-icon":{"identifier":"deny-set-icon","description":"Denies the set_icon command without any pre-configured scope.","commands":{"allow":[],"deny":["set_icon"]}},"deny-set-ignore-cursor-events":{"identifier":"deny-set-ignore-cursor-events","description":"Denies the set_ignore_cursor_events command without any pre-configured scope.","commands":{"allow":[],"deny":["set_ignore_cursor_events"]}},"deny-set-max-size":{"identifier":"deny-set-max-size","description":"Denies the set_max_size command without any pre-configured scope.","commands":{"allow":[],"deny":["set_max_size"]}},"deny-set-maximizable":{"identifier":"deny-set-maximizable","description":"Denies the set_maximizable command without any pre-configured scope.","commands":{"allow":[],"deny":["set_maximizable"]}},"deny-set-min-size":{"identifier":"deny-set-min-size","description":"Denies the set_min_size command without any pre-configured scope.","commands":{"allow":[],"deny":["set_min_size"]}},"deny-set-minimizable":{"identifier":"deny-set-minimizable","description":"Denies the set_minimizable command without any pre-configured scope.","commands":{"allow":[],"deny":["set_minimizable"]}},"deny-set-overlay-icon":{"identifier":"deny-set-overlay-icon","description":"Denies the set_overlay_icon command without any pre-configured scope.","commands":{"allow":[],"deny":["set_overlay_icon"]}},"deny-set-position":{"identifier":"deny-set-position","description":"Denies the set_position command without any pre-configured scope.","commands":{"allow":[],"deny":["set_position"]}},"deny-set-progress-bar":{"identifier":"deny-set-progress-bar","description":"Denies the set_progress_bar command without any pre-configured scope.","commands":{"allow":[],"deny":["set_progress_bar"]}},"deny-set-resizable":{"identifier":"deny-set-resizable","description":"Denies the set_resizable command without any pre-configured scope.","commands":{"allow":[],"deny":["set_resizable"]}},"deny-set-shadow":{"identifier":"deny-set-shadow","description":"Denies the set_shadow command without any pre-configured scope.","commands":{"allow":[],"deny":["set_shadow"]}},"deny-set-simple-fullscreen":{"identifier":"deny-set-simple-fullscreen","description":"Denies the set_simple_fullscreen command without any pre-configured scope.","commands":{"allow":[],"deny":["set_simple_fullscreen"]}},"deny-set-size":{"identifier":"deny-set-size","description":"Denies the set_size command without any pre-configured scope.","commands":{"allow":[],"deny":["set_size"]}},"deny-set-size-constraints":{"identifier":"deny-set-size-constraints","description":"Denies the set_size_constraints command without any pre-configured scope.","commands":{"allow":[],"deny":["set_size_constraints"]}},"deny-set-skip-taskbar":{"identifier":"deny-set-skip-taskbar","description":"Denies the set_skip_taskbar command without any pre-configured scope.","commands":{"allow":[],"deny":["set_skip_taskbar"]}},"deny-set-theme":{"identifier":"deny-set-theme","description":"Denies the set_theme command without any pre-configured scope.","commands":{"allow":[],"deny":["set_theme"]}},"deny-set-title":{"identifier":"deny-set-title","description":"Denies the set_title command without any pre-configured scope.","commands":{"allow":[],"deny":["set_title"]}},"deny-set-title-bar-style":{"identifier":"deny-set-title-bar-style","description":"Denies the set_title_bar_style command without any pre-configured scope.","commands":{"allow":[],"deny":["set_title_bar_style"]}},"deny-set-visible-on-all-workspaces":{"identifier":"deny-set-visible-on-all-workspaces","description":"Denies the set_visible_on_all_workspaces command without any pre-configured scope.","commands":{"allow":[],"deny":["set_visible_on_all_workspaces"]}},"deny-show":{"identifier":"deny-show","description":"Denies the show command without any pre-configured scope.","commands":{"allow":[],"deny":["show"]}},"deny-start-dragging":{"identifier":"deny-start-dragging","description":"Denies the start_dragging command without any pre-configured scope.","commands":{"allow":[],"deny":["start_dragging"]}},"deny-start-resize-dragging":{"identifier":"deny-start-resize-dragging","description":"Denies the start_resize_dragging command without any pre-configured scope.","commands":{"allow":[],"deny":["start_resize_dragging"]}},"deny-theme":{"identifier":"deny-theme","description":"Denies the theme command without any pre-configured scope.","commands":{"allow":[],"deny":["theme"]}},"deny-title":{"identifier":"deny-title","description":"Denies the title command without any pre-configured scope.","commands":{"allow":[],"deny":["title"]}},"deny-toggle-maximize":{"identifier":"deny-toggle-maximize","description":"Denies the toggle_maximize command without any pre-configured scope.","commands":{"allow":[],"deny":["toggle_maximize"]}},"deny-unmaximize":{"identifier":"deny-unmaximize","description":"Denies the unmaximize command without any pre-configured scope.","commands":{"allow":[],"deny":["unmaximize"]}},"deny-unminimize":{"identifier":"deny-unminimize","description":"Denies the unminimize command without any pre-configured scope.","commands":{"allow":[],"deny":["unminimize"]}}},"permission_sets":{},"global_scope_schema":null},"dialog":{"default_permission":{"identifier":"default","description":"This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n","permissions":["allow-message","allow-save","allow-open"]},"permissions":{"allow-ask":{"identifier":"allow-ask","description":"Enables the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)","commands":{"allow":["message"],"deny":[]}},"allow-confirm":{"identifier":"allow-confirm","description":"Enables the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)","commands":{"allow":["message"],"deny":[]}},"allow-message":{"identifier":"allow-message","description":"Enables the message command without any pre-configured scope.","commands":{"allow":["message"],"deny":[]}},"allow-open":{"identifier":"allow-open","description":"Enables the open command without any pre-configured scope.","commands":{"allow":["open"],"deny":[]}},"allow-save":{"identifier":"allow-save","description":"Enables the save command without any pre-configured scope.","commands":{"allow":["save"],"deny":[]}},"deny-ask":{"identifier":"deny-ask","description":"Denies the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)","commands":{"allow":[],"deny":["message"]}},"deny-confirm":{"identifier":"deny-confirm","description":"Denies the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)","commands":{"allow":[],"deny":["message"]}},"deny-message":{"identifier":"deny-message","description":"Denies the message command without any pre-configured scope.","commands":{"allow":[],"deny":["message"]}},"deny-open":{"identifier":"deny-open","description":"Denies the open command without any pre-configured scope.","commands":{"allow":[],"deny":["open"]}},"deny-save":{"identifier":"deny-save","description":"Denies the save command without any pre-configured scope.","commands":{"allow":[],"deny":["save"]}}},"permission_sets":{},"global_scope_schema":null},"process":{"default_permission":{"identifier":"default","description":"This permission set configures which\nprocess features are by default exposed.\n\n#### Granted Permissions\n\nThis enables to quit via `allow-exit` and restart via `allow-restart`\nthe application.\n","permissions":["allow-exit","allow-restart"]},"permissions":{"allow-exit":{"identifier":"allow-exit","description":"Enables the exit command without any pre-configured scope.","commands":{"allow":["exit"],"deny":[]}},"allow-restart":{"identifier":"allow-restart","description":"Enables the restart command without any pre-configured scope.","commands":{"allow":["restart"],"deny":[]}},"deny-exit":{"identifier":"deny-exit","description":"Denies the exit command without any pre-configured scope.","commands":{"allow":[],"deny":["exit"]}},"deny-restart":{"identifier":"deny-restart","description":"Denies the restart command without any pre-configured scope.","commands":{"allow":[],"deny":["restart"]}}},"permission_sets":{},"global_scope_schema":null},"shell":{"default_permission":{"identifier":"default","description":"This permission set configures which\nshell functionality is exposed by default.\n\n#### Granted Permissions\n\nIt allows to use the `open` functionality with a reasonable\nscope pre-configured. It will allow opening `http(s)://`,\n`tel:` and `mailto:` links.\n","permissions":["allow-open"]},"permissions":{"allow-execute":{"identifier":"allow-execute","description":"Enables the execute command without any pre-configured scope.","commands":{"allow":["execute"],"deny":[]}},"allow-kill":{"identifier":"allow-kill","description":"Enables the kill command without any pre-configured scope.","commands":{"allow":["kill"],"deny":[]}},"allow-open":{"identifier":"allow-open","description":"Enables the open command without any pre-configured scope.","commands":{"allow":["open"],"deny":[]}},"allow-spawn":{"identifier":"allow-spawn","description":"Enables the spawn command without any pre-configured scope.","commands":{"allow":["spawn"],"deny":[]}},"allow-stdin-write":{"identifier":"allow-stdin-write","description":"Enables the stdin_write command without any pre-configured scope.","commands":{"allow":["stdin_write"],"deny":[]}},"deny-execute":{"identifier":"deny-execute","description":"Denies the execute command without any pre-configured scope.","commands":{"allow":[],"deny":["execute"]}},"deny-kill":{"identifier":"deny-kill","description":"Denies the kill command without any pre-configured scope.","commands":{"allow":[],"deny":["kill"]}},"deny-open":{"identifier":"deny-open","description":"Denies the open command without any pre-configured scope.","commands":{"allow":[],"deny":["open"]}},"deny-spawn":{"identifier":"deny-spawn","description":"Denies the spawn command without any pre-configured scope.","commands":{"allow":[],"deny":["spawn"]}},"deny-stdin-write":{"identifier":"deny-stdin-write","description":"Denies the stdin_write command without any pre-configured scope.","commands":{"allow":[],"deny":["stdin_write"]}}},"permission_sets":{},"global_scope_schema":{"$schema":"http://json-schema.org/draft-07/schema#","anyOf":[{"additionalProperties":false,"properties":{"args":{"allOf":[{"$ref":"#/definitions/ShellScopeEntryAllowedArgs"}],"description":"The allowed arguments for the command execution."},"cmd":{"description":"The command name. It can start with a variable that resolves to a system base directory. The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`, `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`, `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$LOG`, `$TEMP`, `$APPCONFIG`, `$APPDATA`, `$APPLOCALDATA`, `$APPCACHE`, `$APPLOG`.","type":"string"},"name":{"description":"The name for this allowed shell command configuration.\n\nThis name will be used inside of the webview API to call this command along with any specified arguments.","type":"string"}},"required":["cmd","name"],"type":"object"},{"additionalProperties":false,"properties":{"args":{"allOf":[{"$ref":"#/definitions/ShellScopeEntryAllowedArgs"}],"description":"The allowed arguments for the command execution."},"name":{"description":"The name for this allowed shell command configuration.\n\nThis name will be used inside of the webview API to call this command along with any specified arguments.","type":"string"},"sidecar":{"description":"If this command is a sidecar command.","type":"boolean"}},"required":["name","sidecar"],"type":"object"}],"definitions":{"ShellScopeEntryAllowedArg":{"anyOf":[{"description":"A non-configurable argument that is passed to the command in the order it was specified.","type":"string"},{"additionalProperties":false,"description":"A variable that is set while calling the command from the webview API.","properties":{"raw":{"default":false,"description":"Marks the validator as a raw regex, meaning the plugin should not make any modification at runtime.\n\nThis means the regex will not match on the entire string by default, which might be exploited if your regex allow unexpected input to be considered valid. When using this option, make sure your regex is correct.","type":"boolean"},"validator":{"description":"[regex] validator to require passed values to conform to an expected input.\n\nThis will require the argument value passed to this variable to match the `validator` regex before it will be executed.\n\nThe regex string is by default surrounded by `^...$` to match the full string. For example the `https?://\\w+` regex would be registered as `^https?://\\w+$`.\n\n[regex]: ","type":"string"}},"required":["validator"],"type":"object"}],"description":"A command argument allowed to be executed by the webview API."},"ShellScopeEntryAllowedArgs":{"anyOf":[{"description":"Use a simple boolean to allow all or disable all arguments to this command configuration.","type":"boolean"},{"description":"A specific set of [`ShellScopeEntryAllowedArg`] that are valid to call for the command configuration.","items":{"$ref":"#/definitions/ShellScopeEntryAllowedArg"},"type":"array"}],"description":"A set of command arguments allowed to be executed by the webview API.\n\nA value of `true` will allow any arguments to be passed to the command. `false` will disable all arguments. A list of [`ShellScopeEntryAllowedArg`] will set those arguments as the only valid arguments to be passed to the attached command configuration."}},"description":"Shell scope entry.","title":"ShellScopeEntry"}},"updater":{"default_permission":{"identifier":"default","description":"This permission set configures which kind of\nupdater functions are exposed to the frontend.\n\n#### Granted Permissions\n\nThe full workflow from checking for updates to installing them\nis enabled.\n\n","permissions":["allow-check","allow-download","allow-install","allow-download-and-install"]},"permissions":{"allow-check":{"identifier":"allow-check","description":"Enables the check command without any pre-configured scope.","commands":{"allow":["check"],"deny":[]}},"allow-download":{"identifier":"allow-download","description":"Enables the download command without any pre-configured scope.","commands":{"allow":["download"],"deny":[]}},"allow-download-and-install":{"identifier":"allow-download-and-install","description":"Enables the download_and_install command without any pre-configured scope.","commands":{"allow":["download_and_install"],"deny":[]}},"allow-install":{"identifier":"allow-install","description":"Enables the install command without any pre-configured scope.","commands":{"allow":["install"],"deny":[]}},"deny-check":{"identifier":"deny-check","description":"Denies the check command without any pre-configured scope.","commands":{"allow":[],"deny":["check"]}},"deny-download":{"identifier":"deny-download","description":"Denies the download command without any pre-configured scope.","commands":{"allow":[],"deny":["download"]}},"deny-download-and-install":{"identifier":"deny-download-and-install","description":"Denies the download_and_install command without any pre-configured scope.","commands":{"allow":[],"deny":["download_and_install"]}},"deny-install":{"identifier":"deny-install","description":"Denies the install command without any pre-configured scope.","commands":{"allow":[],"deny":["install"]}}},"permission_sets":{},"global_scope_schema":null}} \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/desktop/src-tauri/icons/Square30x30Logo.png b/_all-in-one-upload/cc-haha/desktop/src-tauri/icons/Square30x30Logo.png new file mode 100644 index 0000000..330ee21 Binary files /dev/null and b/_all-in-one-upload/cc-haha/desktop/src-tauri/icons/Square30x30Logo.png differ diff --git a/_all-in-one-upload/cc-haha/desktop/src-tauri/icons/android/mipmap-xxxhdpi/ic_launcher_foreground.png b/_all-in-one-upload/cc-haha/desktop/src-tauri/icons/android/mipmap-xxxhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..cc36933 Binary files /dev/null and b/_all-in-one-upload/cc-haha/desktop/src-tauri/icons/android/mipmap-xxxhdpi/ic_launcher_foreground.png differ diff --git a/_all-in-one-upload/cc-haha/desktop/src-tauri/src/lib.rs b/_all-in-one-upload/cc-haha/desktop/src-tauri/src/lib.rs new file mode 100644 index 0000000..c6a33d0 --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src-tauri/src/lib.rs @@ -0,0 +1,1106 @@ +use std::{ + collections::{HashMap, VecDeque}, + io::{Error as IoError, ErrorKind, Read, Write}, + net::{SocketAddr, TcpListener, TcpStream}, + path::PathBuf, + process::{Command as StdCommand, Stdio}, + str, + sync::{ + atomic::{AtomicU32, Ordering}, + Arc, Mutex, + }, + thread, + time::{Duration, Instant}, +}; + +use portable_pty::{native_pty_system, ChildKiller, CommandBuilder, MasterPty, PtySize}; +use serde::Serialize; +use tauri::menu::{MenuBuilder, MenuItemBuilder, SubmenuBuilder}; +use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; +use tauri::Emitter; +use tauri::{AppHandle, Manager, RunEvent, State, WindowEvent}; +use tauri_plugin_shell::{ + process::{CommandChild, CommandEvent}, + ShellExt, +}; + +const SERVER_STARTUP_LOG_LIMIT: usize = 80; +const MAIN_WINDOW_LABEL: &str = "main"; +const TRAY_SHOW_ID: &str = "tray_show"; +const TRAY_QUIT_ID: &str = "tray_quit"; + +#[derive(Default)] +struct ServerState(Mutex); + +struct ServerRuntime { + url: String, + child: CommandChild, +} + +#[derive(Default)] +struct ServerStatus { + runtime: Option, + startup_error: Option, +} + +#[derive(Default)] +struct AppExitState { + is_quitting: Mutex, +} + +/// 与 ServerState 平级的 adapter 子进程状态。 +/// +/// adapter sidecar(claude-sidecar adapters --feishu --telegram)的生命周期 +/// 跟 server 不同:它没有 HTTP 端口可探活,没配凭据时会自己干净退出, +/// 而且需要支持运行时热重启 —— 用户在设置页保存飞书 / Telegram 凭据后, +/// 前端会通过 invoke('restart_adapters_sidecar') 来重启它,让新凭据生效。 +#[derive(Default)] +struct AdapterState(Mutex>); + +#[derive(Default)] +struct TerminalState { + next_id: AtomicU32, + sessions: Mutex>, +} + +struct TerminalSession { + master: Box, + writer: Mutex>, + killer: Mutex>, +} + +#[derive(Serialize, Clone)] +struct TerminalSpawnResult { + session_id: u32, + shell: String, + cwd: String, +} + +#[derive(Serialize, Clone)] +struct TerminalOutputPayload { + session_id: u32, + data: String, +} + +#[derive(Serialize, Clone)] +struct TerminalExitPayload { + session_id: u32, + code: u32, + signal: Option, +} + +#[tauri::command] +fn get_server_url(state: State<'_, ServerState>) -> Result { + let guard = state + .0 + .lock() + .map_err(|_| "desktop server state is unavailable".to_string())?; + + if let Some(runtime) = guard.runtime.as_ref() { + return Ok(runtime.url.clone()); + } + + Err(guard + .startup_error + .clone() + .unwrap_or_else(|| "desktop server did not start".to_string())) +} + +/// 前端在设置页保存飞书 / Telegram 凭据后调用,触发 adapter sidecar 热重启。 +/// +/// 流程: +/// 1. kill 当前 adapter 子进程(如果在跑) +/// 2. spawn 新的 adapter 子进程 +/// 3. 新 sidecar 内部的 loadConfig() 会读到最新的 ~/.claude/adapters.json +/// 并重新建立 WebSocket 连接到飞书 / Telegram +/// +/// 凭据缺失时 sidecar 自己会 warn + skip + 退出,所以这里不需要前置检查。 +#[tauri::command] +fn restart_adapters_sidecar(app: AppHandle) -> Result<(), String> { + stop_adapters_sidecar(&app); + spawn_and_track_adapters_sidecar(&app); + Ok(()) +} + +#[tauri::command] +fn prepare_for_update_install(app: AppHandle) -> Result<(), String> { + mark_app_quitting(&app); + stop_server_sidecar(&app); + stop_adapters_sidecar(&app); + + #[cfg(target_os = "windows")] + { + kill_windows_sidecars(); + } + + // Give Windows a short moment to release executable file handles before the + // updater starts replacing bundled sidecars in the install directory. + std::thread::sleep(Duration::from_millis(750)); + Ok(()) +} + +fn mark_app_quitting(app: &AppHandle) { + if let Some(state) = app.try_state::() { + if let Ok(mut is_quitting) = state.is_quitting.lock() { + *is_quitting = true; + } + } +} + +fn should_hide_to_tray(app: &AppHandle, label: &str) -> bool { + if label != MAIN_WINDOW_LABEL { + return false; + } + + app.try_state::() + .and_then(|state| state.is_quitting.lock().ok().map(|value| !*value)) + .unwrap_or(true) +} + +fn show_main_window(app: &AppHandle) { + if let Some(window) = app.get_webview_window(MAIN_WINDOW_LABEL) { + let _ = window.unminimize(); + let _ = window.show(); + let _ = window.set_focus(); + } +} + +fn setup_system_tray(app: &mut tauri::App) -> tauri::Result<()> { + let menu = MenuBuilder::new(app) + .text(TRAY_SHOW_ID, "Show Claude Code Haha") + .separator() + .text(TRAY_QUIT_ID, "Quit Claude Code Haha") + .build()?; + + let mut tray = TrayIconBuilder::with_id("main-tray") + .tooltip("Claude Code Haha") + .menu(&menu) + .show_menu_on_left_click(false) + .on_menu_event(|app, event| match event.id().as_ref() { + TRAY_SHOW_ID => show_main_window(app), + TRAY_QUIT_ID => { + mark_app_quitting(app); + app.exit(0); + } + _ => {} + }) + .on_tray_icon_event(|tray, event| { + if let TrayIconEvent::Click { + button: MouseButton::Left, + button_state: MouseButtonState::Up, + .. + } = event + { + show_main_window(tray.app_handle()); + } + }); + + if let Some(icon) = app.default_window_icon() { + tray = tray.icon(icon.clone()); + } + + tray.build(app)?; + + Ok(()) +} + +#[tauri::command] +fn terminal_spawn( + app: AppHandle, + state: State<'_, TerminalState>, + cols: u16, + rows: u16, + cwd: Option, +) -> Result { + let cwd_path = resolve_terminal_cwd(cwd)?; + let shell = default_shell(); + let pty_system = native_pty_system(); + let pair = pty_system + .openpty(PtySize { + rows: rows.max(8), + cols: cols.max(20), + pixel_width: 0, + pixel_height: 0, + }) + .map_err(|err| format!("open terminal pty: {err}"))?; + + let mut cmd = CommandBuilder::new(&shell); + cmd.cwd(cwd_path.as_os_str()); + for (key, value) in terminal_environment(&shell) { + cmd.env(key, value); + } + cmd.env("TERM", "xterm-256color"); + cmd.env("COLORTERM", "truecolor"); + + let mut child = pair + .slave + .spawn_command(cmd) + .map_err(|err| format!("spawn terminal shell: {err}"))?; + drop(pair.slave); + + let mut reader = pair + .master + .try_clone_reader() + .map_err(|err| format!("clone terminal reader: {err}"))?; + let writer = pair + .master + .take_writer() + .map_err(|err| format!("open terminal writer: {err}"))?; + let killer = child.clone_killer(); + let session_id = state.next_id.fetch_add(1, Ordering::Relaxed) + 1; + + { + let mut sessions = state + .sessions + .lock() + .map_err(|_| "terminal state is unavailable".to_string())?; + sessions.insert( + session_id, + TerminalSession { + master: pair.master, + writer: Mutex::new(writer), + killer: Mutex::new(killer), + }, + ); + } + + let output_app = app.clone(); + thread::spawn(move || { + let mut buffer = [0_u8; 8192]; + let mut pending_utf8 = Vec::new(); + loop { + match reader.read(&mut buffer) { + Ok(0) => break, + Ok(n) => { + let data = decode_terminal_output(&mut pending_utf8, &buffer[..n]); + if !data.is_empty() { + let _ = output_app.emit( + "terminal-output", + TerminalOutputPayload { session_id, data }, + ); + } + } + Err(err) => { + let _ = output_app.emit( + "terminal-output", + TerminalOutputPayload { + session_id, + data: format!("\r\n[terminal read error: {err}]\r\n"), + }, + ); + break; + } + } + } + if !pending_utf8.is_empty() { + let data = String::from_utf8_lossy(&pending_utf8).to_string(); + let _ = output_app.emit( + "terminal-output", + TerminalOutputPayload { session_id, data }, + ); + } + }); + + let exit_app = app.clone(); + thread::spawn(move || { + let status = child.wait(); + if let Some(state) = exit_app.try_state::() { + if let Ok(mut sessions) = state.sessions.lock() { + sessions.remove(&session_id); + } + } + match status { + Ok(status) => { + let _ = exit_app.emit( + "terminal-exit", + TerminalExitPayload { + session_id, + code: status.exit_code(), + signal: status.signal().map(ToString::to_string), + }, + ); + } + Err(err) => { + let _ = exit_app.emit( + "terminal-output", + TerminalOutputPayload { + session_id, + data: format!("\r\n[terminal wait error: {err}]\r\n"), + }, + ); + } + } + }); + + Ok(TerminalSpawnResult { + session_id, + shell, + cwd: cwd_path.to_string_lossy().to_string(), + }) +} + +#[tauri::command] +fn terminal_write( + state: State<'_, TerminalState>, + session_id: u32, + data: String, +) -> Result<(), String> { + let sessions = state + .sessions + .lock() + .map_err(|_| "terminal state is unavailable".to_string())?; + let session = sessions + .get(&session_id) + .ok_or_else(|| "terminal session is not running".to_string())?; + let mut writer = session + .writer + .lock() + .map_err(|_| "terminal writer is unavailable".to_string())?; + writer + .write_all(data.as_bytes()) + .map_err(|err| format!("write terminal input: {err}"))?; + writer + .flush() + .map_err(|err| format!("flush terminal input: {err}"))?; + Ok(()) +} + +#[tauri::command] +fn terminal_resize( + state: State<'_, TerminalState>, + session_id: u32, + cols: u16, + rows: u16, +) -> Result<(), String> { + let sessions = state + .sessions + .lock() + .map_err(|_| "terminal state is unavailable".to_string())?; + let session = sessions + .get(&session_id) + .ok_or_else(|| "terminal session is not running".to_string())?; + session + .master + .resize(PtySize { + rows: rows.max(8), + cols: cols.max(20), + pixel_width: 0, + pixel_height: 0, + }) + .map_err(|err| format!("resize terminal: {err}"))?; + Ok(()) +} + +#[tauri::command] +fn terminal_kill(state: State<'_, TerminalState>, session_id: u32) -> Result<(), String> { + let session = { + let mut sessions = state + .sessions + .lock() + .map_err(|_| "terminal state is unavailable".to_string())?; + sessions.remove(&session_id) + }; + + if let Some(session) = session { + let mut killer = session + .killer + .lock() + .map_err(|_| "terminal killer is unavailable".to_string())?; + killer + .kill() + .map_err(|err| format!("kill terminal shell: {err}"))?; + } + Ok(()) +} + +fn decode_terminal_output(pending: &mut Vec, chunk: &[u8]) -> String { + pending.extend_from_slice(chunk); + let mut output = String::new(); + + loop { + match str::from_utf8(pending) { + Ok(text) => { + output.push_str(text); + pending.clear(); + break; + } + Err(err) => { + let valid_up_to = err.valid_up_to(); + if valid_up_to > 0 { + let text = str::from_utf8(&pending[..valid_up_to]) + .expect("valid_up_to marks a valid UTF-8 prefix"); + output.push_str(text); + pending.drain(..valid_up_to); + continue; + } + + match err.error_len() { + Some(error_len) => { + output.push('\u{fffd}'); + pending.drain(..error_len); + } + None => break, + } + } + } + } + + output +} + +fn terminal_environment(shell: &str) -> HashMap { + let mut env: HashMap = std::env::vars().collect(); + env.extend(login_shell_environment(shell)); + ensure_utf8_locale(&mut env); + env +} + +fn ensure_utf8_locale(env: &mut HashMap) { + let fallback = default_utf8_locale(); + for key in ["LANG", "LC_CTYPE", "LC_ALL"] { + let needs_fallback = env + .get(key) + .map(|value| !is_utf8_locale(value)) + .unwrap_or(true); + if needs_fallback { + env.insert(key.to_string(), fallback.to_string()); + } + } +} + +fn is_utf8_locale(value: &str) -> bool { + let normalized = value.trim().to_ascii_lowercase().replace('-', ""); + normalized.contains("utf8") +} + +fn default_utf8_locale() -> &'static str { + #[cfg(target_os = "macos")] + { + "en_US.UTF-8" + } + #[cfg(all(unix, not(target_os = "macos")))] + { + "C.UTF-8" + } + #[cfg(not(unix))] + { + "C.UTF-8" + } +} + +#[cfg(not(target_os = "windows"))] +fn login_shell_environment(shell: &str) -> HashMap { + let Ok(mut child) = StdCommand::new(shell) + .args(["-l", "-c", "env -0"]) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + else { + return HashMap::new(); + }; + + let deadline = Instant::now() + Duration::from_secs(2); + loop { + match child.try_wait() { + Ok(Some(status)) => { + if !status.success() { + return HashMap::new(); + } + let mut stdout = Vec::new(); + if let Some(mut pipe) = child.stdout.take() { + let _ = pipe.read_to_end(&mut stdout); + } + return parse_env_block(&stdout); + } + Ok(None) if Instant::now() < deadline => { + thread::sleep(Duration::from_millis(25)); + } + Ok(None) => { + let _ = child.kill(); + let _ = child.wait(); + return HashMap::new(); + } + Err(_) => return HashMap::new(), + } + } +} + +#[cfg(target_os = "windows")] +fn login_shell_environment(_shell: &str) -> HashMap { + HashMap::new() +} + +fn parse_env_block(bytes: &[u8]) -> HashMap { + bytes + .split(|byte| *byte == 0) + .filter_map(|entry| { + if entry.is_empty() { + return None; + } + let equals = entry.iter().position(|byte| *byte == b'=')?; + if equals == 0 { + return None; + } + let key = String::from_utf8_lossy(&entry[..equals]).to_string(); + let value = String::from_utf8_lossy(&entry[equals + 1..]).to_string(); + Some((key, value)) + }) + .collect() +} + +fn resolve_terminal_cwd(cwd: Option) -> Result { + let path = match cwd.and_then(|value| { + let trimmed = value.trim(); + if trimmed.is_empty() { + None + } else { + Some(PathBuf::from(trimmed)) + } + }) { + Some(path) => path, + None => home_dir().unwrap_or( + std::env::current_dir().map_err(|err| format!("resolve current directory: {err}"))?, + ), + }; + + if path.is_dir() { + Ok(path) + } else { + Err(format!("terminal cwd does not exist: {}", path.display())) + } +} + +fn home_dir() -> Option { + std::env::var_os("HOME") + .or_else(|| std::env::var_os("USERPROFILE")) + .map(PathBuf::from) +} + +fn default_shell() -> String { + #[cfg(target_os = "windows")] + { + std::env::var("COMSPEC").unwrap_or_else(|_| "powershell.exe".to_string()) + } + #[cfg(not(target_os = "windows"))] + { + std::env::var("SHELL").unwrap_or_else(|_| { + if PathBuf::from("/bin/zsh").exists() { + "/bin/zsh".to_string() + } else { + "/bin/bash".to_string() + } + }) + } +} + +fn reserve_local_port() -> Result { + let listener = + TcpListener::bind("127.0.0.1:0").map_err(|err| format!("bind local port: {err}"))?; + let port = listener + .local_addr() + .map_err(|err| format!("read local port: {err}"))? + .port(); + drop(listener); + Ok(port) +} + +fn wait_for_server(url_host: &str, port: u16) -> Result<(), String> { + let addr: SocketAddr = format!("{url_host}:{port}") + .parse() + .map_err(|err| format!("parse server address: {err}"))?; + let deadline = Instant::now() + Duration::from_secs(10); + + while Instant::now() < deadline { + if TcpStream::connect_timeout(&addr, Duration::from_millis(200)).is_ok() { + return Ok(()); + } + std::thread::sleep(Duration::from_millis(150)); + } + + Err(format!( + "desktop server did not start listening on {url_host}:{port} within 10 seconds" + )) +} + +fn push_server_startup_log(logs: &Arc>>, line: String) { + let line = line.trim_end().to_string(); + if line.is_empty() { + return; + } + + let Ok(mut guard) = logs.lock() else { + return; + }; + if guard.len() >= SERVER_STARTUP_LOG_LIMIT { + guard.pop_front(); + } + guard.push_back(line); +} + +fn format_server_startup_error(message: &str, logs: &Arc>>) -> String { + let log_text = logs + .lock() + .ok() + .map(|guard| guard.iter().cloned().collect::>().join("\n")) + .filter(|text| !text.trim().is_empty()) + .unwrap_or_else(|| "No server stdout/stderr was captured before the timeout.".to_string()); + + format!("{message}\n\nRecent server logs:\n{log_text}") +} + +fn resolve_app_root(_app: &AppHandle) -> Result { + // 历史用途:此前 sidecar launcher 用 dynamic file:// import 加载磁盘上 + // 的 src/server/index.ts 和 preload.ts,所以 Tauri 必须把整个 src/ + + // node_modules/ 当 Resource 一起 ship 到 .app/Contents/Resources/app/。 + // + // 现在 launcher 改成静态 import + bun build --compile 整棵静态打进二进制, + // sidecar 不再读磁盘上的 src/ 或 node_modules/。CLAUDE_APP_ROOT 现在 + // 只剩一个名义上的"app 安装根目录"作用,给 conversationService 在 + // spawn CLI 子进程时通过 --app-root 透传。 + // + // 我们直接用当前可执行文件所在目录作为 app_root: + // Dev: desktop/src-tauri/target// (rust 跑出来的 binary 那一层) + // Prod: .app/Contents/MacOS/ (sidecar 二进制的同级目录) + let exe = std::env::current_exe().map_err(|err| format!("resolve current exe path: {err}"))?; + let dir = exe + .parent() + .ok_or_else(|| "current exe has no parent dir".to_string())? + .to_path_buf(); + Ok(dir) +} + +fn start_server_sidecar(app: &AppHandle) -> Result { + let host = "127.0.0.1"; + let port = reserve_local_port()?; + let url = format!("http://{host}:{port}"); + let app_root = resolve_app_root(app)?; + let app_root_arg = app_root.to_string_lossy().to_string(); + + // 单一合并 sidecar:第一个参数选 server / cli / adapters 模式。 + let mut sidecar = app + .shell() + .sidecar("claude-sidecar") + .map_err(|err| format!("resolve sidecar: {err}"))?; + for (key, value) in terminal_environment(&default_shell()) { + sidecar = sidecar.env(key, value); + } + let sidecar = sidecar.args([ + "server", + "--app-root", + &app_root_arg, + "--host", + host, + "--port", + &port.to_string(), + ]); + + let startup_logs = Arc::new(Mutex::new(VecDeque::new())); + let logs_for_task = Arc::clone(&startup_logs); + + let (mut rx, child) = sidecar + .spawn() + .map_err(|err| format!("spawn server sidecar: {err}"))?; + + tauri::async_runtime::spawn(async move { + while let Some(event) = rx.recv().await { + match event { + CommandEvent::Stdout(line) => { + let line = String::from_utf8_lossy(&line); + let line = line.trim_end(); + println!("[claude-server] {line}"); + push_server_startup_log(&logs_for_task, format!("[stdout] {line}")); + } + CommandEvent::Stderr(line) => { + let line = String::from_utf8_lossy(&line); + let line = line.trim_end(); + eprintln!("[claude-server] {line}"); + push_server_startup_log(&logs_for_task, format!("[stderr] {line}")); + } + CommandEvent::Terminated(payload) => { + let line = format!( + "sidecar exited (code={:?}, signal={:?})", + payload.code, payload.signal + ); + eprintln!("[claude-server] {line}"); + push_server_startup_log(&logs_for_task, format!("[exit] {line}")); + } + _ => {} + } + } + }); + + if let Err(err) = wait_for_server(host, port) { + let _ = child.kill(); + return Err(format_server_startup_error(&err, &startup_logs)); + } + + Ok(ServerRuntime { url, child }) +} + +fn stop_server_sidecar(app: &AppHandle) { + let Some(state) = app.try_state::() else { + return; + }; + + let Ok(mut guard) = state.0.lock() else { + return; + }; + + if let Some(runtime) = guard.runtime.take() { + let _ = runtime.child.kill(); + } +} + +/// 启动 adapter sidecar。返回 Result 主要为了把"无法 spawn"和"spawn 后立刻 +/// 退出(凭据缺失)"区分开 —— 后者不算错误,是正常 default 状态。 +fn start_adapters_sidecar(app: &AppHandle) -> Result { + let app_root = resolve_app_root(app)?; + let app_root_arg = app_root.to_string_lossy().to_string(); + + // adapter 内部的 WsBridge 默认连 ws://127.0.0.1:3456,但桌面端的 server + // 用的是 reserve_local_port() 拿到的动态端口。这里把实际端口通过 + // ADAPTER_SERVER_URL env var 传过去 —— adapters/common/config.ts 的 + // loadConfig() 会读它。 + // + // 如果 server 还没起来 / 没拿到 URL,回退到 3456 作为最后兜底(adapter + // 自己有重连逻辑,等 server 上线就能连上)。 + let server_http_url = app + .try_state::() + .and_then(|state| { + state + .0 + .lock() + .ok() + .and_then(|guard| guard.runtime.as_ref().map(|r| r.url.clone())) + }) + .unwrap_or_else(|| "http://127.0.0.1:3456".to_string()); + // WsBridge 直接 `new WebSocket('${serverUrl}/ws/...')`,必须传 ws://; + // 不会自动从 http 转。 + let server_ws_url = if let Some(rest) = server_http_url.strip_prefix("http://") { + format!("ws://{rest}") + } else if let Some(rest) = server_http_url.strip_prefix("https://") { + format!("wss://{rest}") + } else { + server_http_url.clone() + }; + + let mut sidecar = app + .shell() + .sidecar("claude-sidecar") + .map_err(|err| format!("resolve sidecar: {err}"))?; + for (key, value) in terminal_environment(&default_shell()) { + sidecar = sidecar.env(key, value); + } + let sidecar = sidecar.env("ADAPTER_SERVER_URL", &server_ws_url).args([ + "adapters", + "--app-root", + &app_root_arg, + "--feishu", + "--telegram", + ]); + + let (mut rx, child) = sidecar + .spawn() + .map_err(|err| format!("spawn adapter sidecar: {err}"))?; + + // 用一个 async task 把 sidecar 的 stdout/stderr 转发出来。它退出时 + // 整个 task 也会自然结束。 + tauri::async_runtime::spawn(async move { + while let Some(event) = rx.recv().await { + match event { + CommandEvent::Stdout(line) => { + let line = String::from_utf8_lossy(&line); + println!("[claude-adapters] {}", line.trim_end()); + } + CommandEvent::Stderr(line) => { + let line = String::from_utf8_lossy(&line); + eprintln!("[claude-adapters] {}", line.trim_end()); + } + CommandEvent::Terminated(payload) => { + // exit code != 0 是常态:用户没配凭据时 sidecar 内部会 + // warn + skip + process.exit(1)。这里只 info 一行, + // 不要当错误冒泡。 + println!( + "[claude-adapters] sidecar exited (code={:?}, signal={:?})", + payload.code, payload.signal + ); + } + _ => {} + } + } + }); + + Ok(child) +} + +/// spawn adapter sidecar 并把 child handle 存进 AdapterState。 +/// 在启动 + 重启路径里复用,集中处理"无法 spawn"的日志。 +fn spawn_and_track_adapters_sidecar(app: &AppHandle) { + match start_adapters_sidecar(app) { + Ok(child) => { + if let Some(state) = app.try_state::() { + if let Ok(mut guard) = state.0.lock() { + *guard = Some(child); + } + } + } + Err(err) => { + eprintln!("[desktop] failed to start adapter sidecar: {err}"); + } + } +} + +fn stop_adapters_sidecar(app: &AppHandle) { + let Some(state) = app.try_state::() else { + return; + }; + let Ok(mut guard) = state.0.lock() else { + return; + }; + if let Some(child) = guard.take() { + let _ = child.kill(); + } +} + +#[cfg(target_os = "windows")] +fn kill_windows_sidecars() { + for image_name in [ + "claude-sidecar-x86_64-pc-windows-msvc.exe", + "claude-sidecar-aarch64-pc-windows-msvc.exe", + "claude-sidecar.exe", + ] { + let _ = StdCommand::new("taskkill") + .args(["/F", "/T", "/IM", image_name]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status(); + } +} + +#[cfg(test)] +mod tests { + use super::{decode_terminal_output, default_utf8_locale, ensure_utf8_locale, parse_env_block}; + use std::collections::HashMap; + + #[test] + fn terminal_output_decoder_preserves_split_chinese_characters() { + let mut pending = Vec::new(); + let bytes = "安装 Skills 成功\n".as_bytes(); + + assert_eq!(decode_terminal_output(&mut pending, &bytes[..2]), ""); + assert_eq!(decode_terminal_output(&mut pending, &bytes[2..4]), "安"); + assert_eq!( + decode_terminal_output(&mut pending, &bytes[4..]), + "装 Skills 成功\n" + ); + assert!(pending.is_empty()); + } + + #[test] + fn terminal_output_decoder_keeps_incomplete_suffix_pending() { + let mut pending = Vec::new(); + let bytes = "中文".as_bytes(); + + assert_eq!(decode_terminal_output(&mut pending, &bytes[..4]), "中"); + assert_eq!(pending, bytes[3..4]); + assert_eq!(decode_terminal_output(&mut pending, &bytes[4..]), "文"); + assert!(pending.is_empty()); + } + + #[test] + fn parse_env_block_reads_nul_delimited_values() { + let env = + parse_env_block(b"PATH=/opt/homebrew/bin:/usr/bin\0NODE_PATH=/tmp/node\0EMPTY=\0"); + + assert_eq!( + env.get("PATH").map(String::as_str), + Some("/opt/homebrew/bin:/usr/bin") + ); + assert_eq!(env.get("NODE_PATH").map(String::as_str), Some("/tmp/node")); + assert_eq!(env.get("EMPTY").map(String::as_str), Some("")); + } + + #[test] + fn terminal_environment_forces_utf8_locale_when_shell_uses_c_locale() { + let mut env = HashMap::from([ + ("LANG".to_string(), "C".to_string()), + ("LC_CTYPE".to_string(), "POSIX".to_string()), + ("LC_ALL".to_string(), "C".to_string()), + ]); + + ensure_utf8_locale(&mut env); + + assert_eq!( + env.get("LANG").map(String::as_str), + Some(default_utf8_locale()) + ); + assert_eq!( + env.get("LC_CTYPE").map(String::as_str), + Some(default_utf8_locale()) + ); + assert_eq!( + env.get("LC_ALL").map(String::as_str), + Some(default_utf8_locale()) + ); + } + + #[test] + fn terminal_environment_keeps_existing_utf8_locale() { + let mut env = HashMap::from([ + ("LANG".to_string(), "zh_CN.UTF-8".to_string()), + ("LC_CTYPE".to_string(), "en_US.UTF8".to_string()), + ("LC_ALL".to_string(), "C.UTF-8".to_string()), + ]); + + ensure_utf8_locale(&mut env); + + assert_eq!(env.get("LANG").map(String::as_str), Some("zh_CN.UTF-8")); + assert_eq!(env.get("LC_CTYPE").map(String::as_str), Some("en_US.UTF8")); + assert_eq!(env.get("LC_ALL").map(String::as_str), Some("C.UTF-8")); + } +} + +#[cfg_attr(mobile, tauri::mobile_entry_point)] +pub fn run() { + let builder = tauri::Builder::default() + .manage(ServerState::default()) + .manage(AdapterState::default()) + .manage(TerminalState::default()) + .manage(AppExitState::default()) + .plugin(tauri_plugin_shell::init()) + .plugin(tauri_plugin_dialog::init()) + .plugin(tauri_plugin_process::init()) + .plugin(tauri_plugin_updater::Builder::new().build()) + .invoke_handler(tauri::generate_handler![ + get_server_url, + restart_adapters_sidecar, + prepare_for_update_install, + terminal_spawn, + terminal_write, + terminal_resize, + terminal_kill + ]); + + // macOS: native menu bar (traffic-light overlay style) + #[cfg(target_os = "macos")] + let builder = builder + .menu(|app| { + let about_item = + MenuItemBuilder::with_id("nav_about", "关于 Claude Code Haha").build(app)?; + let settings_item = MenuItemBuilder::with_id("nav_settings", "设置...") + .accelerator("CmdOrCtrl+,") + .build(app)?; + + let app_submenu = SubmenuBuilder::new(app, "Claude Code Haha") + .item(&about_item) + .separator() + .item(&settings_item) + .separator() + .services() + .separator() + .hide() + .hide_others() + .show_all() + .separator() + .quit() + .build()?; + + let edit_submenu = SubmenuBuilder::new(app, "Edit") + .undo() + .redo() + .separator() + .cut() + .copy() + .paste() + .select_all() + .build()?; + + let view_submenu = SubmenuBuilder::new(app, "View").fullscreen().build()?; + + let window_submenu = SubmenuBuilder::new(app, "Window") + .minimize() + .maximize() + .close_window() + .build()?; + + MenuBuilder::new(app) + .item(&app_submenu) + .item(&edit_submenu) + .item(&view_submenu) + .item(&window_submenu) + .build() + }) + .on_menu_event(|app, event| match event.id().as_ref() { + "nav_about" => { + let _ = app.emit("native-menu-navigate", "about"); + } + "nav_settings" => { + let _ = app.emit("native-menu-navigate", "settings"); + } + _ => {} + }); + + let app = builder + .setup(|app| { + setup_system_tray(app)?; + + let state = app.state::(); + let mut guard = state + .0 + .lock() + .map_err(|_| IoError::new(ErrorKind::Other, "server state lock poisoned"))?; + + match start_server_sidecar(&app.handle()) { + Ok(runtime) => { + guard.runtime = Some(runtime); + guard.startup_error = None; + } + Err(err) => { + eprintln!("[desktop] failed to start local server: {err}"); + guard.runtime = None; + guard.startup_error = Some(err); + } + } + drop(guard); + + // server 起来之后再起 adapter sidecar —— start_adapters_sidecar + // 内部会从 ServerState 读 server URL 注入 ADAPTER_SERVER_URL env, + // 让 adapter 连上动态端口。 + spawn_and_track_adapters_sidecar(&app.handle()); + + Ok(()) + }) + .build(tauri::generate_context!()) + .expect("error while building tauri application"); + + app.run(|app_handle, event| match event { + RunEvent::WindowEvent { + label, + event: WindowEvent::CloseRequested { api, .. }, + .. + } if should_hide_to_tray(app_handle, &label) => { + api.prevent_close(); + if let Some(window) = app_handle.get_webview_window(&label) { + let _ = window.hide(); + } + } + #[cfg(target_os = "macos")] + RunEvent::Reopen { + has_visible_windows: false, + .. + } => { + show_main_window(app_handle); + } + RunEvent::ExitRequested { .. } => { + mark_app_quitting(app_handle); + stop_server_sidecar(app_handle); + stop_adapters_sidecar(app_handle); + } + RunEvent::Exit => { + mark_app_quitting(app_handle); + stop_server_sidecar(app_handle); + stop_adapters_sidecar(app_handle); + } + _ => {} + }); +} diff --git a/_all-in-one-upload/cc-haha/desktop/src-tauri/tauri.conf.json b/_all-in-one-upload/cc-haha/desktop/src-tauri/tauri.conf.json new file mode 100644 index 0000000..c9330e6 --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src-tauri/tauri.conf.json @@ -0,0 +1,76 @@ +{ + "$schema": "https://raw.githubusercontent.com/nicegui/nicegui/main/nicegui/static/tauri-schema-v2.json", + "productName": "HeiCode", + "version": "0.1.0", + "identifier": "com.heicode.desktop", + "build": { + "frontendDist": "../dist", + "devUrl": "http://localhost:1420", + "beforeDevCommand": "bun run build:sidecars && bun run dev", + "beforeBuildCommand": "bun run build && bun run build:sidecars" + }, + "app": { + "windows": [ + { + "title": "HeiCode", + "width": 1440, + "height": 960, + "minWidth": 960, + "minHeight": 640, + "decorations": true, + "transparent": false, + "acceptFirstMouse": true + } + ], + "security": { + "dangerousDisableAssetCspModification": ["style-src"], + "csp": "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: asset: https://asset.localhost; font-src 'self' data:; connect-src 'self' ws://127.0.0.1:* http://127.0.0.1:* ws://localhost:* http://localhost:*; media-src 'self' blob:" + } + }, + "plugins": { + "updater": { + "pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDlCOUIwRDExQTc5RTFGMzYKUldRMkg1Nm5FUTJibTJ2cGlHY0pkL0dGemxXMUlzc01pVTVMM1U3WGpmWUtrUC8wK2ErSXhLKzEK", + "endpoints": [], + "windows": { + "installMode": "passive" + } + } + }, + "bundle": { + "active": true, + "targets": "all", + "createUpdaterArtifacts": true, + "windows": { + "nsis": { + "installerHooks": "windows-installer-hooks.nsh" + } + }, + "externalBin": [ + "binaries/claude-sidecar" + ], + "resources": [], + "icon": [ + "icons/32x32.png", + "icons/128x128.png", + "icons/128x128@2x.png", + "icons/icon.icns", + "icons/icon.ico" + ], + "macOS": { + "dmg": { + "appPosition": { + "x": 180, + "y": 170 + }, + "applicationFolderPosition": { + "x": 480, + "y": 170 + }, + "windowSize": { + "width": 660, + "height": 400 + } + } + } + } +} diff --git a/_all-in-one-upload/cc-haha/desktop/src/api/skills.ts b/_all-in-one-upload/cc-haha/desktop/src/api/skills.ts new file mode 100644 index 0000000..c081699 --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/api/skills.ts @@ -0,0 +1,22 @@ +import { api } from './client' +import type { SkillMeta, SkillDetail } from '../types/skill' + +export const skillsApi = { + list: (cwd?: string) => { + const query = cwd ? `?cwd=${encodeURIComponent(cwd)}` : '' + return api.get<{ skills: SkillMeta[] }>(`/api/skills${query}`, { timeout: 120_000 }) + }, + + detail: (source: string, name: string, cwd?: string) => { + const query = new URLSearchParams({ + source, + name, + }) + if (cwd) query.set('cwd', cwd) + + return api.get<{ detail: SkillDetail }>( + `/api/skills/detail?${query.toString()}`, + { timeout: 120_000 }, + ) + }, +} diff --git a/_all-in-one-upload/cc-haha/desktop/src/api/websocket.test.ts b/_all-in-one-upload/cc-haha/desktop/src/api/websocket.test.ts new file mode 100644 index 0000000..7481319 --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/api/websocket.test.ts @@ -0,0 +1,92 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('./client', () => ({ + getBaseUrl: () => 'http://127.0.0.1:3456', +})) + +import { wsManager } from './websocket' + +type SocketHandler = (() => void) | ((event: { data: string }) => void) + +class FakeWebSocket { + static readonly CONNECTING = 0 + static readonly OPEN = 1 + static readonly CLOSING = 2 + static readonly CLOSED = 3 + static instances: FakeWebSocket[] = [] + + readonly url: string + readyState = FakeWebSocket.CONNECTING + onopen: SocketHandler | null = null + onmessage: SocketHandler | null = null + onclose: SocketHandler | null = null + onerror: SocketHandler | null = null + sent: string[] = [] + + constructor(url: string) { + this.url = url + FakeWebSocket.instances.push(this) + } + + send(data: string) { + this.sent.push(data) + } + + close() { + this.readyState = FakeWebSocket.CLOSED + ;(this.onclose as (() => void) | null)?.() + } + + open() { + this.readyState = FakeWebSocket.OPEN + ;(this.onopen as (() => void) | null)?.() + } + + fail() { + this.readyState = FakeWebSocket.CLOSED + ;(this.onclose as (() => void) | null)?.() + } +} + +describe('wsManager reconnect buffering', () => { + const originalWebSocket = globalThis.WebSocket + + beforeEach(() => { + vi.useFakeTimers() + FakeWebSocket.instances = [] + globalThis.WebSocket = FakeWebSocket as unknown as typeof WebSocket + wsManager.disconnectAll() + }) + + afterEach(() => { + wsManager.disconnectAll() + globalThis.WebSocket = originalWebSocket + vi.useRealTimers() + }) + + it('replays queued messages after an unexpected reconnect', async () => { + wsManager.connect('session-reconnect') + + const firstSocket = FakeWebSocket.instances[0] + expect(firstSocket?.url).toContain('/ws/session-reconnect') + + firstSocket!.open() + wsManager.send('session-reconnect', { type: 'user_message', content: 'first' }) + expect(firstSocket!.sent).toEqual([ + JSON.stringify({ type: 'user_message', content: 'first' }), + ]) + + firstSocket!.fail() + wsManager.send('session-reconnect', { type: 'user_message', content: 'queued while offline' }) + + await vi.advanceTimersByTimeAsync(1000) + + const secondSocket = FakeWebSocket.instances[1] + expect(secondSocket).toBeDefined() + secondSocket!.open() + + expect(secondSocket!.sent).toEqual([ + JSON.stringify({ type: 'user_message', content: 'queued while offline' }), + ]) + }) +}) diff --git a/_all-in-one-upload/cc-haha/desktop/src/components/shared/UpdateChecker.tsx b/_all-in-one-upload/cc-haha/desktop/src/components/shared/UpdateChecker.tsx new file mode 100644 index 0000000..d9bec6a --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/components/shared/UpdateChecker.tsx @@ -0,0 +1,106 @@ +import { useEffect } from 'react' +import { useTranslation } from '../../i18n' +import { MarkdownRenderer } from '../markdown/MarkdownRenderer' +import { isTauriRuntime } from '../../lib/desktopRuntime' +import { useUpdateStore } from '../../stores/updateStore' +import { formatBytes } from '../../lib/formatBytes' + +export function UpdateChecker() { + const t = useTranslation() + const status = useUpdateStore((s) => s.status) + const availableVersion = useUpdateStore((s) => s.availableVersion) + const releaseNotes = useUpdateStore((s) => s.releaseNotes) + const progressPercent = useUpdateStore((s) => s.progressPercent) + const downloadedBytes = useUpdateStore((s) => s.downloadedBytes) + const totalBytes = useUpdateStore((s) => s.totalBytes) + const error = useUpdateStore((s) => s.error) + const shouldPrompt = useUpdateStore((s) => s.shouldPrompt) + const initialize = useUpdateStore((s) => s.initialize) + const installUpdate = useUpdateStore((s) => s.installUpdate) + const dismissPrompt = useUpdateStore((s) => s.dismissPrompt) + + useEffect(() => { + void initialize() + }, [initialize]) + + if (!isTauriRuntime()) return null + + const showPopup = + shouldPrompt && !!availableVersion && ['available', 'downloading', 'restarting'].includes(status) + + if (!showPopup) return null + + const hasKnownProgress = typeof totalBytes === 'number' && totalBytes > 0 + const downloadedText = formatBytes(downloadedBytes) + const statusText = + status === 'restarting' + ? t('update.restarting') + : status === 'downloading' + ? hasKnownProgress + ? t('update.downloading') + : t('update.progressBytes', { downloaded: downloadedText }) + : null + + return ( +
+
+

+ {t('update.available', { version: availableVersion })} +

+ + {releaseNotes && ( +
+ +
+ )} + + {(status === 'downloading' || status === 'restarting') && ( +
+
+ {hasKnownProgress || status === 'restarting' ? ( +
+ ) : ( +
+ )} +
+ {statusText && ( +

+ {statusText} + {status === 'downloading' && hasKnownProgress ? ` ${progressPercent}%` : ''} +

+ )} +
+ )} + + {error && ( +

+ {t('update.failed', { error })} +

+ )} + + {status === 'available' && ( +
+ + +
+ )} +
+
+ ) +} diff --git a/_all-in-one-upload/cc-haha/desktop/src/components/tasks/DayOfWeekPicker.tsx b/_all-in-one-upload/cc-haha/desktop/src/components/tasks/DayOfWeekPicker.tsx new file mode 100644 index 0000000..577b848 --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/components/tasks/DayOfWeekPicker.tsx @@ -0,0 +1,57 @@ +import { useTranslation } from '../../i18n' + +type Props = { + selected: number[] + onChange: (days: number[]) => void +} + +// Display order: Mon(1) → Sun(0), matching Chinese convention +const DAY_ORDER = [1, 2, 3, 4, 5, 6, 0] + +const DAY_KEYS = [ + 'newTask.daySun', + 'newTask.dayMon', + 'newTask.dayTue', + 'newTask.dayWed', + 'newTask.dayThu', + 'newTask.dayFri', + 'newTask.daySat', +] as const + +export function DayOfWeekPicker({ selected, onChange }: Props) { + const t = useTranslation() + + const toggle = (day: number) => { + if (selected.includes(day)) { + // Don't allow deselecting the last day + if (selected.length <= 1) return + onChange(selected.filter((d) => d !== day)) + } else { + onChange([...selected, day]) + } + } + + return ( +
+ {DAY_ORDER.map((day) => { + const isActive = selected.includes(day) + return ( + + ) + })} +
+ ) +} diff --git a/_all-in-one-upload/cc-haha/desktop/src/pages/ActiveSession.tsx b/_all-in-one-upload/cc-haha/desktop/src/pages/ActiveSession.tsx new file mode 100644 index 0000000..0cc18ec --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/pages/ActiveSession.tsx @@ -0,0 +1,219 @@ +import { useEffect, useMemo } from 'react' +import { useTabStore } from '../stores/tabStore' +import { useSessionStore } from '../stores/sessionStore' +import { useChatStore } from '../stores/chatStore' +import { useCLITaskStore } from '../stores/cliTaskStore' +import { useTeamStore } from '../stores/teamStore' +import { useTranslation } from '../i18n' +import { MessageList } from '../components/chat/MessageList' +import { ChatInput } from '../components/chat/ChatInput' +import { ComputerUsePermissionModal } from '../components/chat/ComputerUsePermissionModal' +import { TeamStatusBar } from '../components/teams/TeamStatusBar' +import { SessionTaskBar } from '../components/chat/SessionTaskBar' + +const TASK_POLL_INTERVAL_MS = 1000 + +export function ActiveSession() { + const activeTabId = useTabStore((s) => s.activeTabId) + const sessions = useSessionStore((s) => s.sessions) + const connectToSession = useChatStore((s) => s.connectToSession) + const sessionState = useChatStore((s) => activeTabId ? s.sessions[activeTabId] : undefined) + const pendingComputerUsePermission = sessionState?.pendingComputerUsePermission ?? null + const fetchSessionTasks = useCLITaskStore((s) => s.fetchSessionTasks) + const trackedTaskSessionId = useCLITaskStore((s) => s.sessionId) + const hasIncompleteTasks = useCLITaskStore((s) => s.tasks.some((task) => task.status !== 'completed')) + const chatState = sessionState?.chatState ?? 'idle' + const tokenUsage = sessionState?.tokenUsage ?? { input_tokens: 0, output_tokens: 0 } + + const session = sessions.find((s) => s.id === activeTabId) + const memberInfo = useTeamStore((s) => activeTabId ? s.getMemberBySessionId(activeTabId) : null) + const activeTeam = useTeamStore((s) => s.activeTeam) + const isMemberSession = !!memberInfo + + useEffect(() => { + if (activeTabId && !isMemberSession) { + connectToSession(activeTabId) + } + }, [activeTabId, isMemberSession, connectToSession]) + + useEffect(() => { + if (!activeTabId || isMemberSession) return + + const shouldPollTasks = + chatState !== 'idle' || + (trackedTaskSessionId === activeTabId && hasIncompleteTasks) + + if (!shouldPollTasks) return + + void fetchSessionTasks(activeTabId) + + const timer = setInterval(() => { + void fetchSessionTasks(activeTabId) + }, TASK_POLL_INTERVAL_MS) + + return () => clearInterval(timer) + }, [ + activeTabId, + isMemberSession, + chatState, + trackedTaskSessionId, + hasIncompleteTasks, + fetchSessionTasks, + ]) + + const t = useTranslation() + const messages = sessionState?.messages ?? [] + const streamingText = sessionState?.streamingText ?? '' + const isEmpty = messages.length === 0 && !streamingText + + const isActive = chatState !== 'idle' + const totalTokens = tokenUsage.input_tokens + tokenUsage.output_tokens + + const lastUpdated = useMemo(() => { + if (!session?.modifiedAt) return '' + const diff = Date.now() - new Date(session.modifiedAt).getTime() + if (diff < 60000) return t('session.timeJustNow') + if (diff < 3600000) return t('session.timeMinutes', { n: Math.floor(diff / 60000) }) + if (diff < 86400000) return t('session.timeHours', { n: Math.floor(diff / 3600000) }) + return t('session.timeDays', { n: Math.floor(diff / 86400000) }) + }, [session?.modifiedAt, t]) + + if (!activeTabId) return null + + return ( +
+ {isMemberSession && ( +
+
+
+
+ {memberInfo?.status === 'running' && ( + + )} + {memberInfo?.status === 'completed' && ( + check_circle + )} + smart_toy + + {memberInfo?.role} + + {activeTeam && ( + + @ {activeTeam.name} + + )} +
+

+ {t('teams.memberSessionHint')} +

+
+ +
+
+ )} + + {isEmpty ? ( +
+
+ {isMemberSession ? ( + <> + smart_toy +

+ {memberInfo?.status === 'running' + ? `${memberInfo.role} ${t('teams.working')}` + : t('teams.noMessages')} +

+ + ) : ( + <> + Claude Code Haha +

+ {t('empty.title')} +

+

+ {t('empty.subtitle')} +

+ + )} +
+
+ ) : ( + <> + {!isMemberSession && ( +
+
+

+ {session?.title || t('session.untitled')} +

+
+ {isActive && ( + + + {t('session.active')} + + )} + {totalTokens > 0 && ( + <> + · + {totalTokens.toLocaleString()} t + + )} + {lastUpdated && ( + <> + · + {t('session.lastUpdated', { time: lastUpdated })} + + )} + {session?.messageCount !== undefined && session.messageCount > 0 && ( + <> + · + {t('session.messages', { count: session.messageCount })} + + )} +
+ {session?.workDirExists === false && ( +
+ warning + + {t('session.workspaceUnavailable', { dir: session.workDir || 'directory no longer exists' })} + +
+ )} +
+
+ )} + + + + )} + + {!isMemberSession && } + + + + + + {!isMemberSession && activeTabId ? ( + + ) : null} +
+ ) +} diff --git a/_all-in-one-upload/cc-haha/desktop/src/pages/ScheduledTasksList.tsx b/_all-in-one-upload/cc-haha/desktop/src/pages/ScheduledTasksList.tsx new file mode 100644 index 0000000..7c7c87b --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/pages/ScheduledTasksList.tsx @@ -0,0 +1,416 @@ +import { useTranslation } from '../i18n' +import { mockScheduledTasks, mockStatusBar } from '../mocks/data' + +export function ScheduledTasksList() { + const t = useTranslation() + const { stats, tasks } = mockScheduledTasks + const task0 = tasks[0]! + const task1 = tasks[1]! + const task2 = tasks[2]! + + return ( +
+ {/* SideNavBar */} + + + {/* Main Content Area */} +
+ {/* TopAppBar */} +
+ +
+
+ + +
+ +
+
+ + {/* Separation Line */} +
+ + {/* Scrollable Content */} +
+
+ {/* Page Header */} +
+
+

{t('scheduledPage.title')}

+

{t('scheduledPage.subtitle')}

+
+ +
+ + {/* Bento-style Summary Cards */} +
+ {/* Total Tasks */} +
+
+ {t('tasks.totalTasks')} + analytics +
+
{stats.totalTasks}
+
+ trending_up + {t('scheduledPage.thisMonth', { count: '+2' })} +
+
+ + {/* Next Run */} +
+
+ {t('scheduledPage.nextRun')} + schedule +
+
{stats.nextRun.name}
+

{stats.nextRun.time}

+
+ + {/* System Health */} +
+
+ {t('scheduledPage.systemHealth')} + check_circle +
+
{stats.systemHealth}%
+

{stats.healthPeriod}

+
+
+ + {/* Operational Tasks Table */} +
+ + + + + + + + + + + + {/* Task Row 1 - Nightly linting */} + + + + + + + + + {/* Task Row 2 - Clean up temp files */} + + + + + + + + + {/* Task Row 3 - Database Vacuum */} + + + + + + + + +
{t('scheduledPage.colTaskName')}{t('scheduledPage.colFrequency')}{t('scheduledPage.colLastResult')}{t('scheduledPage.colNextExecution')}{t('scheduledPage.colActions')}
+
+
+ code_blocks +
+
+
{task0.name}
+
Root: /projects/companion/src
+
+
+
+ {task0.frequency} + +
+ + check_circle + + {task0.lastResult} +
+
+
{task0.nextExecution}
+
+
+ + + +
+
+
+
+ cleaning_services +
+
+
{task1.name}
+
{task1.description}
+
+
+
+ {task1.frequency} + +
+ + check_circle + + {task1.lastResult} +
+
+
{task1.nextExecution}
+
+
+ + + +
+
+
+
+ database +
+
+
{task2.name}
+
{task2.description}
+
+
+
+ Monthly + +
+ + error + + {task2.lastResult} +
+
+
{task2.nextExecution}
+
+
+ + + +
+
+ + {/* End of list placeholder */} +
+
+ history_toggle_off +
+

{t('scheduledPage.endOfList')}

+

{t('scheduledPage.pausedTasks')}

+
+
+ + {/* System Logs / Details Panel */} +
+ {/* Recent Output Logs */} +
+

{t('scheduledPage.recentLogs')}

+
+
+ 2023-11-10 23:01 + [INFO] + Nightly linting started for repository: companion-main +
+
+ 2023-11-10 23:04 + [INFO] + Processed 1,422 files. No critical issues found. +
+
+ 2023-11-10 23:04 + [WARN] + Found 12 deprecated calls in /legacy/utils.js +
+
+ 2023-11-10 23:05 + [INFO] + Task completed successfully in 242.4s. +
+
+ Log stream: active + +
+
+
+ + {/* Resource Allocation Panel */} +
+
+

{t('scheduledPage.resourceAllocation')}

+
+
+
+ {t('scheduledPage.cpuCapacity')} + 42% +
+
+
+
+
+
+
+ {t('scheduledPage.memoryLoad')} + 68% +
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + {/* Footer */} +
+
+ {mockStatusBar.user} • {mockStatusBar.username} • {mockStatusBar.plan} +
+
+ + fiber_manual_record + + {t('scheduledPage.connectedLocal')} +
+
+
+ + + +
+
+
+
+ ) +} diff --git a/_all-in-one-upload/cc-haha/desktop/src/stores/agentStore.ts b/_all-in-one-upload/cc-haha/desktop/src/stores/agentStore.ts new file mode 100644 index 0000000..580bda1 --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/stores/agentStore.ts @@ -0,0 +1,45 @@ +import { create } from 'zustand' +import { agentsApi, type AgentDefinition } from '../api/agents' + +export type AgentDetailReturnTab = 'agents' | 'plugins' + +type AgentStore = { + activeAgents: AgentDefinition[] + allAgents: AgentDefinition[] + isLoading: boolean + error: string | null + selectedAgent: AgentDefinition | null + selectedAgentReturnTab: AgentDetailReturnTab + + fetchAgents: (cwd?: string) => Promise + selectAgent: ( + agent: AgentDefinition | null, + returnTab?: AgentDetailReturnTab, + ) => void +} + +export const useAgentStore = create((set) => ({ + activeAgents: [], + allAgents: [], + isLoading: false, + error: null, + selectedAgent: null, + selectedAgentReturnTab: 'agents', + + fetchAgents: async (cwd) => { + set({ isLoading: true, error: null }) + try { + const { activeAgents, allAgents } = await agentsApi.list(cwd) + set({ activeAgents, allAgents, isLoading: false }) + } catch (error) { + const message = error instanceof Error ? error.message : 'Failed to load agents' + set({ isLoading: false, error: message }) + } + }, + + selectAgent: (agent, returnTab = 'agents') => + set({ + selectedAgent: agent, + selectedAgentReturnTab: agent ? returnTab : 'agents', + }), +})) diff --git a/_all-in-one-upload/cc-haha/desktop/src/types/task.ts b/_all-in-one-upload/cc-haha/desktop/src/types/task.ts new file mode 100644 index 0000000..9ccbbba --- /dev/null +++ b/_all-in-one-upload/cc-haha/desktop/src/types/task.ts @@ -0,0 +1,56 @@ +// Source: src/server/services/cronService.ts + +export type TaskNotificationConfig = { + enabled: boolean + channels: ('telegram' | 'feishu')[] +} + +export type CronTask = { + id: string + name: string + description?: string + cron: string + prompt: string + enabled: boolean + recurring?: boolean + permanent?: boolean + createdAt: number + lastRunAt?: number + lastFiredAt?: string + nextRunAt?: number + permissionMode?: string + model?: string + folderPath?: string + useWorktree?: boolean + notification?: TaskNotificationConfig +} + +export type CreateTaskInput = { + name: string + description?: string + cron: string + prompt: string + enabled?: boolean + recurring?: boolean + permanent?: boolean + permissionMode?: string + model?: string + folderPath?: string + useWorktree?: boolean + notification?: TaskNotificationConfig +} + +export type TaskRun = { + id: string + taskId: string + taskName: string + startedAt: string + completedAt?: string + status: 'running' | 'completed' | 'failed' | 'timeout' + prompt: string + output?: string + error?: string + exitCode?: number + durationMs?: number + sessionId?: string +} diff --git a/_all-in-one-upload/cc-haha/docs/agent/images/.syncthing.12-agent-core-loop.png.tmp b/_all-in-one-upload/cc-haha/docs/agent/images/.syncthing.12-agent-core-loop.png.tmp new file mode 100644 index 0000000..9ba8cb6 Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/agent/images/.syncthing.12-agent-core-loop.png.tmp differ diff --git a/_all-in-one-upload/cc-haha/docs/agent/images/06-context-passing.png b/_all-in-one-upload/cc-haha/docs/agent/images/06-context-passing.png new file mode 100644 index 0000000..26d4d45 Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/agent/images/06-context-passing.png differ diff --git a/_all-in-one-upload/cc-haha/docs/channel/images/03-access-control.png b/_all-in-one-upload/cc-haha/docs/channel/images/03-access-control.png new file mode 100644 index 0000000..c97b20f Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/channel/images/03-access-control.png differ diff --git a/_all-in-one-upload/cc-haha/docs/en/guide/faq.md b/_all-in-one-upload/cc-haha/docs/en/guide/faq.md new file mode 100644 index 0000000..46be288 --- /dev/null +++ b/_all-in-one-upload/cc-haha/docs/en/guide/faq.md @@ -0,0 +1,34 @@ +# FAQ + + +## Q: `undefined is not an object (evaluating 'usage.input_tokens')` + +**Cause**: `ANTHROPIC_BASE_URL` is misconfigured. The API endpoint is returning HTML or another non-JSON format instead of a valid Anthropic protocol response. + +This project uses the **Anthropic Messages API protocol**. `ANTHROPIC_BASE_URL` must point to an endpoint compatible with Anthropic's `/v1/messages` interface. The Anthropic SDK automatically appends `/v1/messages` to the base URL, so: + +- MiniMax: `ANTHROPIC_BASE_URL=https://api.minimaxi.com/anthropic` ✅ +- OpenRouter: `ANTHROPIC_BASE_URL=https://openrouter.ai/api` ✅ +- OpenRouter (wrong): `ANTHROPIC_BASE_URL=https://openrouter.ai/anthropic` ❌ (returns HTML) + +If your model provider only supports the OpenAI protocol, you need a proxy like LiteLLM for protocol translation. See the [Third-Party Models Guide](./third-party-models.md). + +## Q: `Cannot find package 'bundle'` + +``` +error: Cannot find package 'bundle' from '.../claude-code-haha/src/entrypoints/cli.tsx' +``` + +**Cause**: Your Bun version is too old and doesn't support the required `bun:bundle` built-in module. + +**Fix**: Upgrade Bun to the latest version: + +```bash +bun upgrade +``` + +## Q: How to use OpenAI / DeepSeek / Ollama or other non-Anthropic models? + +This project only supports the Anthropic protocol. If your model provider doesn't natively support the Anthropic protocol, you need a proxy like [LiteLLM](https://github.com/BerriAI/litellm) for protocol translation (OpenAI → Anthropic). + +See the [Third-Party Models Guide](./third-party-models.md) for detailed setup instructions. diff --git a/_all-in-one-upload/cc-haha/docs/en/memory/images/05-architecture-overview.png b/_all-in-one-upload/cc-haha/docs/en/memory/images/05-architecture-overview.png new file mode 100644 index 0000000..a4dca2b Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/en/memory/images/05-architecture-overview.png differ diff --git a/_all-in-one-upload/cc-haha/docs/guide/quick-start.md b/_all-in-one-upload/cc-haha/docs/guide/quick-start.md new file mode 100644 index 0000000..fb262d6 --- /dev/null +++ b/_all-in-one-upload/cc-haha/docs/guide/quick-start.md @@ -0,0 +1,64 @@ +# 快速开始 + +## 1. 安装 Bun + +```bash +# macOS / Linux +curl -fsSL https://bun.sh/install | bash + +# macOS (Homebrew) +brew install bun + +# Windows (PowerShell) +powershell -c "irm bun.sh/install.ps1 | iex" +``` + +> 精简版 Linux 如提示 `unzip is required`,先运行 `apt update && apt install -y unzip` + +## 2. 安装依赖并配置 + +```bash +bun install +cp .env.example .env +# 编辑 .env 填入你的 API Key,详见「环境变量」文档 +``` + +环境变量的完整说明请参考 [环境变量配置](./env-vars.md)。 + +## 3. 启动 + +### macOS / Linux + +```bash +./bin/claude-haha # 交互 TUI 模式 +./bin/claude-haha -p "your prompt here" # 无头模式 +./bin/claude-haha --help # 查看所有选项 +``` + +### Windows + +> **前置要求**:必须安装 [Git for Windows](https://git-scm.com/download/win) + +```powershell +# PowerShell / cmd 直接调用 Bun +bun --env-file=.env ./src/entrypoints/cli.tsx + +# 或在 Git Bash 中运行 +./bin/claude-haha +``` + +## 4. 全局使用(可选) + +将 `bin/` 加入 PATH 后可在任意目录启动,详见 [全局使用指南](./global-usage.md): + +```bash +export PATH="$HOME/path/to/claude-code-haha/bin:$PATH" +``` + +## 5. 降级模式 + +如果 Ink TUI 出现问题,可以使用降级 Recovery CLI 模式: + +```bash +CLAUDE_CODE_FORCE_RECOVERY_CLI=1 ./bin/claude-haha +``` diff --git a/_all-in-one-upload/cc-haha/docs/images/desktop_ui/09_slash_command.png b/_all-in-one-upload/cc-haha/docs/images/desktop_ui/09_slash_command.png new file mode 100644 index 0000000..8cf3824 Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/images/desktop_ui/09_slash_command.png differ diff --git a/_all-in-one-upload/cc-haha/docs/images/im/feishu/03-dev-console.png b/_all-in-one-upload/cc-haha/docs/images/im/feishu/03-dev-console.png new file mode 100644 index 0000000..8e08efa Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/images/im/feishu/03-dev-console.png differ diff --git a/_all-in-one-upload/cc-haha/docs/images/im/feishu/07-menu-clear.png b/_all-in-one-upload/cc-haha/docs/images/im/feishu/07-menu-clear.png new file mode 100644 index 0000000..3e24c63 Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/images/im/feishu/07-menu-clear.png differ diff --git a/_all-in-one-upload/cc-haha/docs/images/im/telegram/03-bot-token.png b/_all-in-one-upload/cc-haha/docs/images/im/telegram/03-bot-token.png new file mode 100644 index 0000000..c07e983 Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/images/im/telegram/03-bot-token.png differ diff --git a/_all-in-one-upload/cc-haha/docs/images/logo-horizontal.png b/_all-in-one-upload/cc-haha/docs/images/logo-horizontal.png new file mode 100644 index 0000000..e1cc11e Binary files /dev/null and b/_all-in-one-upload/cc-haha/docs/images/logo-horizontal.png differ diff --git a/_all-in-one-upload/cc-haha/docs/memory/03-autodream.md b/_all-in-one-upload/cc-haha/docs/memory/03-autodream.md new file mode 100644 index 0000000..c58f817 --- /dev/null +++ b/_all-in-one-upload/cc-haha/docs/memory/03-autodream.md @@ -0,0 +1,316 @@ +# Claude Code 记忆系统 — AutoDream 记忆整合 + +> Claude 会"做梦"——在后台静默回顾近期会话,整合、更新、修剪记忆,就像人类睡眠中整理白天的记忆一样。 + +

+AutoDream · 触发条件 · 整合流程 · 安全限制 · UI 展示 · 配置开关 · 对比 · 源码导航 +

+ +![AutoDream 概览](./images/11-autodream-overview.png) + +--- + +## 一、什么是 AutoDream? + +AutoDream 是 Claude Code 的 **后台记忆整合机制**,内部代号 **"Dream: Memory Consolidation"**。 + +核心隐喻: + +| 人类 | Claude Code | +|------|-------------| +| 白天随手记笔记 | `extractMemories` — 每次对话后提取新记忆 | +| 晚上睡觉时整理笔记本 | `autoDream` — 定期回顾多个会话,整合全部记忆 | + +当你不活跃时(默认间隔 24 小时、积累 5 个会话后),Claude 会在后台静默启动一个 **"做梦"子智能体**(forked subagent),回顾所有近期会话记录,将零散的记忆整合为结构化的、去重的、去过时的持久化知识。 + +**关键源码**:`src/services/autoDream/autoDream.ts` + +```typescript +// Background memory consolidation. Fires the /dream prompt as a forked +// subagent when time-gate passes AND enough sessions have accumulated. +``` + +--- + +## 二、触发条件 + +![AutoDream 触发流程](./images/12-autodream-trigger.png) + +AutoDream 采用 **五重门控** 机制,按开销从低到高逐级检查: + +### 门控链 + +| 序号 | 门控 | 说明 | 开销 | +|------|------|------|------| +| 1 | **功能开关** | `isAutoDreamEnabled()` + 非 KAIROS + 非远程模式 + autoMemory 已启用 | 内存读取 | +| 2 | **时间门控** | 距上次整合 >= `minHours`(默认 24h) | 1 次 stat | +| 3 | **扫描节流** | 上次扫描后至少等 10 分钟再重新扫描 | 时间戳比较 | +| 4 | **会话门控** | 自上次整合后至少 `minSessions` 个新会话(默认 5 个,排除当前) | 目录扫描 | +| 5 | **锁门控** | 没有其他进程正在做梦(PID 锁文件) | stat + read | + +**关键源码** `src/services/autoDream/autoDream.ts:63-66`: + +```typescript +const DEFAULTS: AutoDreamConfig = { + minHours: 24, // 距离上次整合至少 24 小时 + minSessions: 5, // 期间至少积累了 5 个会话 +} +``` + +### 扫描节流 + +当时间门控通过但会话门控未通过时,锁文件的 mtime 不会更新,导致时间门控在后续每个 turn 都会通过。为避免频繁的目录扫描,设有 **10 分钟的扫描节流**: + +```typescript +const SESSION_SCAN_INTERVAL_MS = 10 * 60 * 1000 +``` + +### 执行入口 + +AutoDream 在每次 AI 回复完成后的 stop hook 阶段被触发(fire-and-forget,不阻塞主线程): + +**关键源码** `src/query/stopHooks.ts:154-156`: + +```typescript +if (!toolUseContext.agentId) { + void executeAutoDream(stopHookContext, toolUseContext.appendSystemMessage) +} +``` + +### 阻止条件 + +以下情况不会触发 AutoDream: + +- **KAIROS 模式**:使用独立的 disk-skill dream +- **远程模式**:`getIsRemoteMode() === true` +- **autoMemory 未启用** +- **`--bare` / SIMPLE 模式** +- **子代理内**:只有主代理才触发 + +--- + +## 三、四阶段整合流程 + +![AutoDream 四阶段流程](./images/13-autodream-phases.png) + +一旦所有门控通过,AutoDream 启动一个 **分叉子智能体**,按照 `consolidationPrompt.ts` 定义的 4 阶段提示词工作: + +### Phase 1 — Orient(定向) + +``` +- ls 记忆目录,查看已有文件 +- 读取 MEMORY.md 索引,理解当前知识结构 +- 浏览现有主题文件,避免创建重复 +- 如存在 logs/ 或 sessions/ 子目录,检查最近条目 +``` + +### Phase 2 — Gather recent signal(收集近期信号) + +按优先级从高到低搜集信息: + +1. **Daily logs** — `logs/YYYY/MM/YYYY-MM-DD.md`(追加流日志) +2. **漂移的记忆** — 与代码库现状矛盾的旧事实 +3. **会话记录搜索** — 用 `grep` 在 JSONL 转录文件中窄范围检索 + +``` +不要穷尽读取转录文件。只查找你已经怀疑重要的内容。 +``` + +### Phase 3 — Consolidate(整合) + +- **合并**新信号到已有主题文件(而非创建新的近似文件) +- **转换**相对日期为绝对日期("昨天" → "2026-04-03") +- **删除**被推翻的旧事实 + +### Phase 4 — Prune and index(修剪与索引) + +- 更新 `MEMORY.md`,保持 ≤ 规定行数且 ≤ 25KB +- 删除指向过时记忆的指针 +- 压缩冗长条目(>200 字符的索引行,将内容移入主题文件) +- 新增指向重要记忆的指针 +- 解决矛盾(两个文件意见不一致时,修复错误的那个) + +**关键源码** `src/services/autoDream/consolidationPrompt.ts:10-64` + +--- + +## 四、安全限制 + +AutoDream 子智能体受到严格的工具权限限制: + +### Bash 仅限只读 + +``` +允许:ls, find, grep, cat, stat, wc, head, tail +拒绝:所有写入、重定向、修改状态的命令 +``` + +### 文件操作仅限记忆目录 + +`createAutoMemCanUseTool()` 是 `extractMemories` 和 `autoDream` 共享的权限函数: + +``` +允许 Read / Grep / Glob — 无限制 +允许 Edit / Write — 仅 auto-memory 目录内 +拒绝 MCP / Agent / 非只读 Bash / 其他写操作 +``` + +**关键源码** `src/services/extractMemories/extractMemories.ts:167-171` + +### 锁文件机制 + +使用 `.consolidate-lock` 文件实现进程级互斥: + +| 机制 | 说明 | +|------|------| +| **锁内容** | 持有者 PID | +| **时间戳** | 锁文件 mtime = 上次整合时间 | +| **过期** | 持有超过 1 小时视为过期(防 PID 复用) | +| **竞争** | 两个进程同时写入 → 最后写入者获胜,失败者在 re-read 时退出 | +| **回滚** | 失败时回退 mtime 到获取前的值,让下次尝试不受影响 | +| **崩溃恢复** | mtime 卡住 + 死 PID → 下个进程回收锁 | + +**关键源码** `src/services/autoDream/consolidationLock.ts` + +--- + +## 五、UI 展示 + +### 底部状态栏 + +当 AutoDream 运行时,底部状态栏会显示 **"dreaming"** 标签: + +```typescript +// src/tasks/pillLabel.ts:61-62 +case 'dream': + return 'dreaming' +``` + +### 任务详情对话框 + +用户按 `Shift+Down` 可打开后台任务对话框,查看梦境的实时进度: + +- **DreamDetailDialog** 组件展示: + - 正在回顾的会话数量 + - 当前阶段:`starting`(正在分析)→ `updating`(正在修改记忆文件) + - 最近的助手文本响应和工具调用次数 + - 已触碰的文件路径列表 + +- **用户可按 `x` 键终止**正在进行的梦境(触发 abort + 锁回滚) + +### 完成通知 + +梦境完成后,如果有文件被修改,会在主会话中显示内联通知: + +```typescript +appendSystemMessage({ + ...createMemorySavedMessage(dreamState.filesTouched), + verb: 'Improved', +}) +``` + +**关键源码**: +- `src/tasks/DreamTask/DreamTask.ts` — 任务状态管理 +- `src/components/tasks/DreamDetailDialog.tsx` — UI 组件 + +--- + +## 六、配置与开关 + +### settings.json + +```json +{ + "autoDreamEnabled": true +} +``` + +- **显式设置时**:直接使用用户的值 +- **未设置时**:由远程 GrowthBook feature flag `tengu_onyx_plover` 控制 + +**关键源码** `src/services/autoDream/config.ts:13-21`: + +```typescript +export function isAutoDreamEnabled(): boolean { + const setting = getInitialSettings().autoDreamEnabled + if (setting !== undefined) return setting + const gb = getFeatureValue_CACHED_MAY_BE_STALE<{ enabled?: unknown } | null>( + 'tengu_onyx_plover', null, + ) + return gb?.enabled === true +} +``` + +### 远程配置参数 + +`tengu_onyx_plover` feature flag 可配置: + +| 参数 | 类型 | 默认值 | 说明 | +|------|------|--------|------| +| `enabled` | boolean | — | 功能总开关 | +| `minHours` | number | 24 | 最小间隔(小时) | +| `minSessions` | number | 5 | 最小会话数 | + +### 手动触发 `/dream` + +除了自动触发,用户可以通过 `/dream` 命令手动触发记忆整合。手动触发会调用 `recordConsolidation()` 更新锁文件时间戳。 + +--- + +## 七、与 extractMemories 的关系 + +| 维度 | extractMemories | autoDream | +|------|----------------|-----------| +| **触发频率** | 每次对话回合结束 | 每 24h + 5 个会话 | +| **触发位置** | `stopHooks.ts` L149 | `stopHooks.ts` L155 | +| **处理范围** | 当前对话最近的消息 | 多个会话的历史记录 | +| **目标** | 提取新记忆 | 整合/去重/修剪已有记忆 | +| **人类类比** | 白天随手记笔记 | 睡觉时整理笔记本 | +| **共享组件** | `createAutoMemCanUseTool` | `createAutoMemCanUseTool` | +| **分叉代理** | 最多 5 turns | 无 turn 限制 | +| **转录** | 不记录(`skipTranscript`) | 不记录(`skipTranscript`) | + +### 协作流 + +``` +每次对话结束 + ↓ +extractMemories → 提取新记忆片段 → 写入 *.md + MEMORY.md + ↓ +(积累 24h + 5个会话后) + ↓ +autoDream → 回顾所有记忆 + 会话记录 + ↓ +合并重复 / 修正过时 / 删除矛盾 / 压缩索引 + ↓ +MEMORY.md 和主题文件焕然一新 +``` + +--- + +## 八、源码导航 + +| 文件 | 职责 | +|------|------| +| `src/services/autoDream/autoDream.ts` | 主逻辑:门控检查、启动分叉代理、进度监控 | +| `src/services/autoDream/config.ts` | 开关控制:settings.json 或 GrowthBook | +| `src/services/autoDream/consolidationPrompt.ts` | 梦境提示词:4 阶段整合工作流 | +| `src/services/autoDream/consolidationLock.ts` | 锁文件机制:防并发、时间戳、回滚 | +| `src/tasks/DreamTask/DreamTask.ts` | UI 任务注册:状态管理、终止、回滚 | +| `src/tasks/pillLabel.ts` | 底部状态栏标签:"dreaming" | +| `src/components/tasks/DreamDetailDialog.tsx` | 梦境详情对话框 UI | +| `src/query/stopHooks.ts` | 执行入口:每次回复后触发 | +| `src/utils/backgroundHousekeeping.ts` | 初始化入口:`initAutoDream()` | +| `src/services/extractMemories/extractMemories.ts` | 共享的 `createAutoMemCanUseTool` | + +--- + +## 九、分析事件 + +AutoDream 通过以下事件记录运行状态: + +| 事件 | 时机 | 附带数据 | +|------|------|----------| +| `tengu_auto_dream_fired` | 梦境启动 | `hours_since`, `sessions_since` | +| `tengu_auto_dream_completed` | 梦境完成 | `cache_read`, `cache_created`, `output`, `sessions_reviewed` | +| `tengu_auto_dream_failed` | 梦境失败 | — | diff --git a/_all-in-one-upload/cc-haha/docs/ui-clone/stitch_claudecode_haha_frontend_design/active_session/code.html b/_all-in-one-upload/cc-haha/docs/ui-clone/stitch_claudecode_haha_frontend_design/active_session/code.html new file mode 100644 index 0000000..61c5619 --- /dev/null +++ b/_all-in-one-upload/cc-haha/docs/ui-clone/stitch_claudecode_haha_frontend_design/active_session/code.html @@ -0,0 +1,288 @@ + + + + + + + + + + + + + + + + +
+ +
+
+Claude Code Companion + +
+
+
+ + +
+ +
+
+ +
+ +
+
+ +
session-active-042 • last updated 2m ago
+
+ +
+ +
+ +
+
+

+ I need to refactor the login flow in auth.ts. Let's move the JWT signing logic to a separate helper and add validation for the user payload. +

+
+
+ +
+
+smart_toy +
+
+
+

Understood. I will begin by analyzing the current implementation of auth.ts. I'll extract the JWT logic into a new utility function and implement the validation layer as requested.

+
+ +
+ +
+ +
+
+
+edit_document +edit_file: auth.ts +
+Modified +
+
++ const validatePayload = (user) => { ... }
++ export const signToken = (payload) => jwt.sign(payload, SECRET);
+- // OLD LOGIC REMOVED +
+
+ +
+
+
+Crafting... + +00:42 +1.2k tokens +
+ +
+
+
+
+ +
+
+
+ + + +
+
+ + +
+Claude 3.5 Sonnet +
+
+
+
+ +
+
+User Avatar • username • Pro Plan +
+
+ + + +
+
+
+ \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/release-notes/v0.1.7.md b/_all-in-one-upload/cc-haha/release-notes/v0.1.7.md new file mode 100644 index 0000000..4666c10 --- /dev/null +++ b/_all-in-one-upload/cc-haha/release-notes/v0.1.7.md @@ -0,0 +1,35 @@ +# Claude Code Haha v0.1.7 + +这是一个以 Windows 桌面端在线更新稳定性和聊天展示体验修复为主的补丁版本。 + +相比 `v0.1.6`,这次重点修复了 Windows 桌面端自动更新后本地服务启动失败的问题,并补齐了 Windows 开发环境 WebUI 启动 CLI 时的宏预加载逻辑。同时修复了 Mermaid 渲染错误提示和聊天手动滚动被打断的问题。 + +## 主要更新 + +- 修复 Windows 桌面端在线自动更新流程:安装新版本前会先停止本地 server sidecar 和 adapter sidecar,避免升级后重启时出现 `os error 32`、`另一个程序正在使用此文件` 或“本地服务启动失败”。 +- Windows 发布产物改为优先使用 NSIS 安装包,并在安装器 hook 中处理 sidecar 进程清理,降低从旧版本自动升级时被文件锁卡住的概率。 +- 修复 Windows 开发环境中 WebUI 启动 CLI 失败的问题,源码 fallback 启动 CLI 时会正确加载 `preload.ts`,避免 `MACRO is not defined` 导致进程启动失败。 +- 优化聊天列表手动滚动体验:用户向上查看历史消息时,流式输出不会强制把视图拉回底部;当用户仍停留在底部附近时,会继续保持自动跟随最新回复。 + +## 问题修复 + +- 修复 Mermaid 图表渲染失败时可能出现开发错误覆盖层的问题,让错误状态保持在聊天消息内部展示。 +- 修复聊天流式输出期间手动滚动位置被覆盖的问题,并补充相关回归测试。 +- 修复 Windows updater 安装阶段没有提前释放 sidecar 文件句柄的问题。 +- 修复 Windows dev WebUI 会话启动时 CLI 进程直接以 code 1 退出的问题。 + +## 其他说明 + +- 已安装 `v0.1.5` 或 `v0.1.6` 的 Windows 用户正常情况下可以直接升级到 `v0.1.7`,不需要先卸载旧版本。 +- 如果某台机器已经卡在旧版本升级后的“本地服务启动失败”页面,可以先完全退出桌面端和残留的 `claude-sidecar.exe` 进程,再安装 `v0.1.7`。 +- GitHub Release 正文继续以 `release-notes/v0.1.7.md` 作为来源,发布时无需再手动复制 Markdown。 + +## 安装说明 + +### macOS + +首次打开如果提示“已损坏”或“无法验证开发者”,请执行: + +```bash +xattr -cr /Applications/Claude\ Code\ Haha.app +``` diff --git a/_all-in-one-upload/cc-haha/src/bridge/replBridgeHandle.ts b/_all-in-one-upload/cc-haha/src/bridge/replBridgeHandle.ts new file mode 100644 index 0000000..f04d745 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/bridge/replBridgeHandle.ts @@ -0,0 +1,36 @@ +import { updateSessionBridgeId } from '../utils/concurrentSessions.js' +import type { ReplBridgeHandle } from './replBridge.js' +import { toCompatSessionId } from './sessionIdCompat.js' + +/** + * Global pointer to the active REPL bridge handle, so callers outside + * useReplBridge's React tree (tools, slash commands) can invoke handle methods + * like subscribePR. Same one-bridge-per-process justification as bridgeDebug.ts + * — the handle's closure captures the sessionId and getAccessToken that created + * the session, and re-deriving those independently (BriefTool/upload.ts pattern) + * risks staging/prod token divergence. + * + * Set from useReplBridge.tsx when init completes; cleared on teardown. + */ + +let handle: ReplBridgeHandle | null = null + +export function setReplBridgeHandle(h: ReplBridgeHandle | null): void { + handle = h + // Publish (or clear) our bridge session ID in the session record so other + // local peers can dedup us out of their bridge list — local is preferred. + void updateSessionBridgeId(getSelfBridgeCompatId() ?? null).catch(() => {}) +} + +export function getReplBridgeHandle(): ReplBridgeHandle | null { + return handle +} + +/** + * Our own bridge session ID in the session_* compat format the API returns + * in /v1/sessions responses — or undefined if bridge isn't connected. + */ +export function getSelfBridgeCompatId(): string | undefined { + const h = getReplBridgeHandle() + return h ? toCompatSessionId(h.bridgeSessionId) : undefined +} diff --git a/_all-in-one-upload/cc-haha/src/bridge/sessionRunner.ts b/_all-in-one-upload/cc-haha/src/bridge/sessionRunner.ts new file mode 100644 index 0000000..bc232bc --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/bridge/sessionRunner.ts @@ -0,0 +1,550 @@ +import { type ChildProcess, spawn } from 'child_process' +import { createWriteStream, type WriteStream } from 'fs' +import { tmpdir } from 'os' +import { dirname, join } from 'path' +import { createInterface } from 'readline' +import { jsonParse, jsonStringify } from '../utils/slowOperations.js' +import { debugTruncate } from './debugUtils.js' +import type { + SessionActivity, + SessionDoneStatus, + SessionHandle, + SessionSpawner, + SessionSpawnOpts, +} from './types.js' + +const MAX_ACTIVITIES = 10 +const MAX_STDERR_LINES = 10 + +/** + * Sanitize a session ID for use in file names. + * Strips any characters that could cause path traversal (e.g. `../`, `/`) + * or other filesystem issues, replacing them with underscores. + */ +export function safeFilenameId(id: string): string { + return id.replace(/[^a-zA-Z0-9_-]/g, '_') +} + +/** + * A control_request emitted by the child CLI when it needs permission to + * execute a **specific** tool invocation (not a general capability check). + * The bridge forwards this to the server so the user can approve/deny. + */ +export type PermissionRequest = { + type: 'control_request' + request_id: string + request: { + /** Per-invocation permission check — "may I run this tool with these inputs?" */ + subtype: 'can_use_tool' + tool_name: string + input: Record + tool_use_id: string + } +} + +type SessionSpawnerDeps = { + execPath: string + /** + * Arguments that must precede the CLI flags when spawning. Empty for + * compiled binaries (where execPath is the claude binary itself); contains + * the script path (process.argv[1]) for npm installs where execPath is the + * node runtime. Without this, node sees --sdk-url as a node option and + * exits with "bad option: --sdk-url" (see anthropics/claude-code#28334). + */ + scriptArgs: string[] + env: NodeJS.ProcessEnv + verbose: boolean + sandbox: boolean + debugFile?: string + permissionMode?: string + onDebug: (msg: string) => void + onActivity?: (sessionId: string, activity: SessionActivity) => void + onPermissionRequest?: ( + sessionId: string, + request: PermissionRequest, + accessToken: string, + ) => void +} + +/** Map tool names to human-readable verbs for the status display. */ +const TOOL_VERBS: Record = { + Read: 'Reading', + Write: 'Writing', + Edit: 'Editing', + MultiEdit: 'Editing', + Bash: 'Running', + Glob: 'Searching', + Grep: 'Searching', + WebFetch: 'Fetching', + WebSearch: 'Searching', + Task: 'Running task', + FileReadTool: 'Reading', + FileWriteTool: 'Writing', + FileEditTool: 'Editing', + GlobTool: 'Searching', + GrepTool: 'Searching', + BashTool: 'Running', + NotebookEditTool: 'Editing notebook', + LSP: 'LSP', +} + +function toolSummary(name: string, input: Record): string { + const verb = TOOL_VERBS[name] ?? name + const target = + (input.file_path as string) ?? + (input.filePath as string) ?? + (input.pattern as string) ?? + (input.command as string | undefined)?.slice(0, 60) ?? + (input.url as string) ?? + (input.query as string) ?? + '' + if (target) { + return `${verb} ${target}` + } + return verb +} + +function extractActivities( + line: string, + sessionId: string, + onDebug: (msg: string) => void, +): SessionActivity[] { + let parsed: unknown + try { + parsed = jsonParse(line) + } catch { + return [] + } + + if (!parsed || typeof parsed !== 'object') { + return [] + } + + const msg = parsed as Record + const activities: SessionActivity[] = [] + const now = Date.now() + + switch (msg.type) { + case 'assistant': { + const message = msg.message as Record | undefined + if (!message) break + const content = message.content + if (!Array.isArray(content)) break + + for (const block of content) { + if (!block || typeof block !== 'object') continue + const b = block as Record + + if (b.type === 'tool_use') { + const name = (b.name as string) ?? 'Tool' + const input = (b.input as Record) ?? {} + const summary = toolSummary(name, input) + activities.push({ + type: 'tool_start', + summary, + timestamp: now, + }) + onDebug( + `[bridge:activity] sessionId=${sessionId} tool_use name=${name} ${inputPreview(input)}`, + ) + } else if (b.type === 'text') { + const text = (b.text as string) ?? '' + if (text.length > 0) { + activities.push({ + type: 'text', + summary: text.slice(0, 80), + timestamp: now, + }) + onDebug( + `[bridge:activity] sessionId=${sessionId} text "${text.slice(0, 100)}"`, + ) + } + } + } + break + } + case 'result': { + const subtype = msg.subtype as string | undefined + if (subtype === 'success') { + activities.push({ + type: 'result', + summary: 'Session completed', + timestamp: now, + }) + onDebug( + `[bridge:activity] sessionId=${sessionId} result subtype=success`, + ) + } else if (subtype) { + const errors = msg.errors as string[] | undefined + const errorSummary = errors?.[0] ?? `Error: ${subtype}` + activities.push({ + type: 'error', + summary: errorSummary, + timestamp: now, + }) + onDebug( + `[bridge:activity] sessionId=${sessionId} result subtype=${subtype} error="${errorSummary}"`, + ) + } else { + onDebug( + `[bridge:activity] sessionId=${sessionId} result subtype=undefined`, + ) + } + break + } + default: + break + } + + return activities +} + +/** + * Extract plain text from a replayed SDKUserMessage NDJSON line. Returns the + * trimmed text if this looks like a real human-authored message, otherwise + * undefined so the caller keeps waiting for the first real message. + */ +function extractUserMessageText( + msg: Record, +): string | undefined { + // Skip tool-result user messages (wrapped subagent results) and synthetic + // caveat messages — neither is human-authored. + if (msg.parent_tool_use_id != null || msg.isSynthetic || msg.isReplay) + return undefined + + const message = msg.message as Record | undefined + const content = message?.content + let text: string | undefined + if (typeof content === 'string') { + text = content + } else if (Array.isArray(content)) { + for (const block of content) { + if ( + block && + typeof block === 'object' && + (block as Record).type === 'text' + ) { + text = (block as Record).text as string | undefined + break + } + } + } + text = text?.trim() + return text ? text : undefined +} + +/** Build a short preview of tool input for debug logging. */ +function inputPreview(input: Record): string { + const parts: string[] = [] + for (const [key, val] of Object.entries(input)) { + if (typeof val === 'string') { + parts.push(`${key}="${val.slice(0, 100)}"`) + } + if (parts.length >= 3) break + } + return parts.join(' ') +} + +export function createSessionSpawner(deps: SessionSpawnerDeps): SessionSpawner { + return { + spawn(opts: SessionSpawnOpts, dir: string): SessionHandle { + // Debug file resolution: + // 1. If deps.debugFile is provided, use it with session ID suffix for uniqueness + // 2. If verbose or ant build, auto-generate a temp file path + // 3. Otherwise, no debug file + const safeId = safeFilenameId(opts.sessionId) + let debugFile: string | undefined + if (deps.debugFile) { + const ext = deps.debugFile.lastIndexOf('.') + if (ext > 0) { + debugFile = `${deps.debugFile.slice(0, ext)}-${safeId}${deps.debugFile.slice(ext)}` + } else { + debugFile = `${deps.debugFile}-${safeId}` + } + } else if (deps.verbose || process.env.USER_TYPE === 'ant') { + debugFile = join(tmpdir(), 'claude', `bridge-session-${safeId}.log`) + } + + // Transcript file: write raw NDJSON lines for post-hoc analysis. + // Placed alongside the debug file when one is configured. + let transcriptStream: WriteStream | null = null + let transcriptPath: string | undefined + if (deps.debugFile) { + transcriptPath = join( + dirname(deps.debugFile), + `bridge-transcript-${safeId}.jsonl`, + ) + transcriptStream = createWriteStream(transcriptPath, { flags: 'a' }) + transcriptStream.on('error', err => { + deps.onDebug( + `[bridge:session] Transcript write error: ${err.message}`, + ) + transcriptStream = null + }) + deps.onDebug(`[bridge:session] Transcript log: ${transcriptPath}`) + } + + const args = [ + ...deps.scriptArgs, + '--print', + '--sdk-url', + opts.sdkUrl, + '--session-id', + opts.sessionId, + '--input-format', + 'stream-json', + '--output-format', + 'stream-json', + '--replay-user-messages', + ...(deps.verbose ? ['--verbose'] : []), + ...(debugFile ? ['--debug-file', debugFile] : []), + ...(deps.permissionMode + ? ['--permission-mode', deps.permissionMode] + : []), + ] + + const env: NodeJS.ProcessEnv = { + ...deps.env, + // Strip the bridge's OAuth token so the child CC process uses + // the session access token for inference instead. + CLAUDE_CODE_OAUTH_TOKEN: undefined, + CLAUDE_CODE_ENVIRONMENT_KIND: 'bridge', + ...(deps.sandbox && { CLAUDE_CODE_FORCE_SANDBOX: '1' }), + CLAUDE_CODE_SESSION_ACCESS_TOKEN: opts.accessToken, + // v1: HybridTransport (WS reads + POST writes) to Session-Ingress. + // Harmless in v2 mode — transportUtils checks CLAUDE_CODE_USE_CCR_V2 first. + CLAUDE_CODE_POST_FOR_SESSION_INGRESS_V2: '1', + // v2: SSETransport + CCRClient to CCR's /v1/code/sessions/* endpoints. + // Same env vars environment-manager sets in the container path. + ...(opts.useCcrV2 && { + CLAUDE_CODE_USE_CCR_V2: '1', + CLAUDE_CODE_WORKER_EPOCH: String(opts.workerEpoch), + }), + } + + deps.onDebug( + `[bridge:session] Spawning sessionId=${opts.sessionId} sdkUrl=${opts.sdkUrl} accessToken=${opts.accessToken ? 'present' : 'MISSING'}`, + ) + deps.onDebug(`[bridge:session] Child args: ${args.join(' ')}`) + if (debugFile) { + deps.onDebug(`[bridge:session] Debug log: ${debugFile}`) + } + + // Pipe all three streams: stdin for control, stdout for NDJSON parsing, + // stderr for error capture and diagnostics. + const child: ChildProcess = spawn(deps.execPath, args, { + cwd: dir, + stdio: ['pipe', 'pipe', 'pipe'], + env, + windowsHide: true, + }) + + deps.onDebug( + `[bridge:session] sessionId=${opts.sessionId} pid=${child.pid}`, + ) + + const activities: SessionActivity[] = [] + let currentActivity: SessionActivity | null = null + const lastStderr: string[] = [] + let sigkillSent = false + let firstUserMessageSeen = false + + // Buffer stderr for error diagnostics + if (child.stderr) { + const stderrRl = createInterface({ input: child.stderr }) + stderrRl.on('line', line => { + // Forward stderr to bridge's stderr in verbose mode + if (deps.verbose) { + process.stderr.write(line + '\n') + } + // Ring buffer of last N lines + if (lastStderr.length >= MAX_STDERR_LINES) { + lastStderr.shift() + } + lastStderr.push(line) + }) + } + + // Parse NDJSON from child stdout + if (child.stdout) { + const rl = createInterface({ input: child.stdout }) + rl.on('line', line => { + // Write raw NDJSON to transcript file + if (transcriptStream) { + transcriptStream.write(line + '\n') + } + + // Log all messages flowing from the child CLI to the bridge + deps.onDebug( + `[bridge:ws] sessionId=${opts.sessionId} <<< ${debugTruncate(line)}`, + ) + + // In verbose mode, forward raw output to stderr + if (deps.verbose) { + process.stderr.write(line + '\n') + } + + const extracted = extractActivities( + line, + opts.sessionId, + deps.onDebug, + ) + for (const activity of extracted) { + // Maintain ring buffer + if (activities.length >= MAX_ACTIVITIES) { + activities.shift() + } + activities.push(activity) + currentActivity = activity + + deps.onActivity?.(opts.sessionId, activity) + } + + // Detect control_request and replayed user messages. + // extractActivities parses the same line but swallows parse errors + // and skips 'user' type — re-parse here is cheap (NDJSON lines are + // small) and keeps each path self-contained. + { + let parsed: unknown + try { + parsed = jsonParse(line) + } catch { + // Non-JSON line, skip detection + } + if (parsed && typeof parsed === 'object') { + const msg = parsed as Record + + if (msg.type === 'control_request') { + const request = msg.request as + | Record + | undefined + if ( + request?.subtype === 'can_use_tool' && + deps.onPermissionRequest + ) { + deps.onPermissionRequest( + opts.sessionId, + parsed as PermissionRequest, + opts.accessToken, + ) + } + // interrupt is turn-level; the child handles it internally (print.ts) + } else if ( + msg.type === 'user' && + !firstUserMessageSeen && + opts.onFirstUserMessage + ) { + const text = extractUserMessageText(msg) + if (text) { + firstUserMessageSeen = true + opts.onFirstUserMessage(text) + } + } + } + } + }) + } + + const done = new Promise(resolve => { + child.on('close', (code, signal) => { + // Close transcript stream on exit + if (transcriptStream) { + transcriptStream.end() + transcriptStream = null + } + + if (signal === 'SIGTERM' || signal === 'SIGINT') { + deps.onDebug( + `[bridge:session] sessionId=${opts.sessionId} interrupted signal=${signal} pid=${child.pid}`, + ) + resolve('interrupted') + } else if (code === 0) { + deps.onDebug( + `[bridge:session] sessionId=${opts.sessionId} completed exit_code=0 pid=${child.pid}`, + ) + resolve('completed') + } else { + deps.onDebug( + `[bridge:session] sessionId=${opts.sessionId} failed exit_code=${code} pid=${child.pid}`, + ) + resolve('failed') + } + }) + + child.on('error', err => { + deps.onDebug( + `[bridge:session] sessionId=${opts.sessionId} spawn error: ${err.message}`, + ) + resolve('failed') + }) + }) + + const handle: SessionHandle = { + sessionId: opts.sessionId, + done, + activities, + accessToken: opts.accessToken, + lastStderr, + get currentActivity(): SessionActivity | null { + return currentActivity + }, + kill(): void { + if (!child.killed) { + deps.onDebug( + `[bridge:session] Sending SIGTERM to sessionId=${opts.sessionId} pid=${child.pid}`, + ) + // On Windows, child.kill('SIGTERM') throws; use default signal. + if (process.platform === 'win32') { + child.kill() + } else { + child.kill('SIGTERM') + } + } + }, + forceKill(): void { + // Use separate flag because child.killed is set when kill() is called, + // not when the process exits. We need to send SIGKILL even after SIGTERM. + if (!sigkillSent && child.pid) { + sigkillSent = true + deps.onDebug( + `[bridge:session] Sending SIGKILL to sessionId=${opts.sessionId} pid=${child.pid}`, + ) + if (process.platform === 'win32') { + child.kill() + } else { + child.kill('SIGKILL') + } + } + }, + writeStdin(data: string): void { + if (child.stdin && !child.stdin.destroyed) { + deps.onDebug( + `[bridge:ws] sessionId=${opts.sessionId} >>> ${debugTruncate(data)}`, + ) + child.stdin.write(data) + } + }, + updateAccessToken(token: string): void { + handle.accessToken = token + // Send the fresh token to the child process via stdin. The child's + // StructuredIO handles update_environment_variables messages by + // setting process.env directly, so getSessionIngressAuthToken() + // picks up the new token on the next refreshHeaders call. + handle.writeStdin( + jsonStringify({ + type: 'update_environment_variables', + variables: { CLAUDE_CODE_SESSION_ACCESS_TOKEN: token }, + }) + '\n', + ) + deps.onDebug( + `[bridge:session] Sent token refresh via stdin for sessionId=${opts.sessionId}`, + ) + }, + } + + return handle + }, + } +} + +export { extractActivities as _extractActivitiesForTesting } diff --git a/_all-in-one-upload/cc-haha/src/buddy/useBuddyNotification.tsx b/_all-in-one-upload/cc-haha/src/buddy/useBuddyNotification.tsx new file mode 100644 index 0000000..fddcb70 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/buddy/useBuddyNotification.tsx @@ -0,0 +1,93 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { useEffect } from 'react'; +import { useNotifications } from '../context/notifications.js'; +import { Text } from '../ink.js'; +import { getGlobalConfig } from '../utils/config.js'; +import { getRainbowColor } from '../utils/thinking.js'; + +// Local date, not UTC — 24h rolling wave across timezones. Sustained Twitter +// buzz instead of a single UTC-midnight spike, gentler on soul-gen load. +// Teaser window: April 1-7, 2026 only. Command stays live forever after. +export function isBuddyTeaserWindow(): boolean { + if ("external" === 'ant') return true; + const d = new Date(); + return d.getFullYear() === 2026 && d.getMonth() === 3 && d.getDate() <= 7; +} +export function isBuddyLive(): boolean { + if ("external" === 'ant') return true; + const d = new Date(); + return d.getFullYear() > 2026 || d.getFullYear() === 2026 && d.getMonth() >= 3; +} +function RainbowText(t0) { + const $ = _c(2); + const { + text + } = t0; + let t1; + if ($[0] !== text) { + t1 = <>{[...text].map(_temp)}; + $[0] = text; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +// Rainbow /buddy teaser shown on startup when no companion hatched yet. +// Idle presence and reactions are handled by CompanionSprite directly. +function _temp(ch, i) { + return {ch}; +} +export function useBuddyNotification() { + const $ = _c(4); + const { + addNotification, + removeNotification + } = useNotifications(); + let t0; + let t1; + if ($[0] !== addNotification || $[1] !== removeNotification) { + t0 = () => { + const config = getGlobalConfig(); + if (config.companion || !isBuddyTeaserWindow()) { + return; + } + addNotification({ + key: "buddy-teaser", + jsx: , + priority: "immediate", + timeoutMs: 15000 + }); + return () => removeNotification("buddy-teaser"); + }; + t1 = [addNotification, removeNotification]; + $[0] = addNotification; + $[1] = removeNotification; + $[2] = t0; + $[3] = t1; + } else { + t0 = $[2]; + t1 = $[3]; + } + useEffect(t0, t1); +} +export function findBuddyTriggerPositions(text: string): Array<{ + start: number; + end: number; +}> { + const triggers: Array<{ + start: number; + end: number; + }> = []; + const re = /\/buddy\b/g; + let m: RegExpExecArray | null; + while ((m = re.exec(text)) !== null) { + triggers.push({ + start: m.index, + end: m.index + m[0].length + }); + } + return triggers; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJmZWF0dXJlIiwiUmVhY3QiLCJ1c2VFZmZlY3QiLCJ1c2VOb3RpZmljYXRpb25zIiwiVGV4dCIsImdldEdsb2JhbENvbmZpZyIsImdldFJhaW5ib3dDb2xvciIsImlzQnVkZHlUZWFzZXJXaW5kb3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xNQUFNO0FBQUMsQ0FBQyxDQUFDLENBQUM7RUFDdkMsSUFBSSxDQUFDcEMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxFQUFFLE9BQU8sRUFBRTtFQUNoQyxNQUFNcUMsUUFBUSxFQUFFSCxLQUFLLENBQUM7SUFBRUMsS0FBSyxFQUFFLE1BQU07SUFBRUMsR0FBRyxFQUFFLE1BQU07RUFBQyxDQUFDLENBQUMsR0FBRyxFQUFFO0VBQzFELE1BQU1FLEVBQUUsR0FBRyxZQUFZO0VBQ3ZCLElBQUlDLENBQUMsRUFBRUMsZUFBZSxHQUFHLElBQUk7RUFDN0IsT0FBTyxDQUFDRCxDQUFDLEdBQUdELEVBQUUsQ0FBQ0csSUFBSSxDQUFDdkIsSUFBSSxDQUFDLE1BQU0sSUFBSSxFQUFFO0lBQ25DbUIsUUFBUSxDQUFDSyxJQUFJLENBQUM7TUFBRVAsS0FBSyxFQUFFSSxDQUFDLENBQUNJLEtBQUs7TUFBRVAsR0FBRyxFQUFFRyxDQUFDLENBQUNJLEtBQUssR0FBR0osQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDSztJQUFPLENBQUMsQ0FBQztFQUMvRDtFQUNBLE9BQU9QLFFBQVE7QUFDakIiLCJpZ25vcmVMaXN0IjpbXX0= \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/break-cache/index.js b/_all-in-one-upload/cc-haha/src/commands/break-cache/index.js new file mode 100644 index 0000000..e1a619d --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/break-cache/index.js @@ -0,0 +1 @@ +export default { isEnabled: () => false, isHidden: true, name: 'stub' }; diff --git a/_all-in-one-upload/cc-haha/src/commands/compact/index.ts b/_all-in-one-upload/cc-haha/src/commands/compact/index.ts new file mode 100644 index 0000000..6db38d4 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/compact/index.ts @@ -0,0 +1,15 @@ +import type { Command } from '../../commands.js' +import { isEnvTruthy } from '../../utils/envUtils.js' + +const compact = { + type: 'local', + name: 'compact', + description: + 'Clear conversation history but keep a summary in context. Optional: /compact [instructions for summarization]', + isEnabled: () => !isEnvTruthy(process.env.DISABLE_COMPACT), + supportsNonInteractive: true, + argumentHint: '', + load: () => import('./compact.js'), +} satisfies Command + +export default compact diff --git a/_all-in-one-upload/cc-haha/src/commands/diff/index.ts b/_all-in-one-upload/cc-haha/src/commands/diff/index.ts new file mode 100644 index 0000000..a15b819 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/diff/index.ts @@ -0,0 +1,8 @@ +import type { Command } from '../../commands.js' + +export default { + type: 'local-jsx', + name: 'diff', + description: 'View uncommitted changes and per-turn diffs', + load: () => import('./diff.js'), +} satisfies Command diff --git a/_all-in-one-upload/cc-haha/src/commands/fast/fast.tsx b/_all-in-one-upload/cc-haha/src/commands/fast/fast.tsx new file mode 100644 index 0000000..398c3c3 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/fast/fast.tsx @@ -0,0 +1,269 @@ +import { c as _c } from "react/compiler-runtime"; +import * as React from 'react'; +import { useState } from 'react'; +import type { CommandResultDisplay, LocalJSXCommandContext } from '../../commands.js'; +import { Dialog } from '../../components/design-system/Dialog.js'; +import { FastIcon, getFastIconString } from '../../components/FastIcon.js'; +import { Box, Link, Text } from '../../ink.js'; +import { useKeybindings } from '../../keybindings/useKeybinding.js'; +import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent } from '../../services/analytics/index.js'; +import { type AppState, useAppState, useSetAppState } from '../../state/AppState.js'; +import type { LocalJSXCommandOnDone } from '../../types/command.js'; +import { clearFastModeCooldown, FAST_MODE_MODEL_DISPLAY, getFastModeModel, getFastModeRuntimeState, getFastModeUnavailableReason, isFastModeEnabled, isFastModeSupportedByModel, prefetchFastModeStatus } from '../../utils/fastMode.js'; +import { formatDuration } from '../../utils/format.js'; +import { formatModelPricing, getOpus46CostTier } from '../../utils/modelCost.js'; +import { updateSettingsForSource } from '../../utils/settings/settings.js'; +function applyFastMode(enable: boolean, setAppState: (f: (prev: AppState) => AppState) => void): void { + clearFastModeCooldown(); + updateSettingsForSource('userSettings', { + fastMode: enable ? true : undefined + }); + if (enable) { + setAppState(prev => { + // Only switch model if current model doesn't support fast mode + const needsModelSwitch = !isFastModeSupportedByModel(prev.mainLoopModel); + return { + ...prev, + ...(needsModelSwitch ? { + mainLoopModel: getFastModeModel(), + mainLoopModelForSession: null + } : {}), + fastMode: true + }; + }); + } else { + setAppState(prev => ({ + ...prev, + fastMode: false + })); + } +} +export function FastModePicker(t0) { + const $ = _c(30); + const { + onDone, + unavailableReason + } = t0; + const model = useAppState(_temp); + const initialFastMode = useAppState(_temp2); + const setAppState = useSetAppState(); + const [enableFastMode, setEnableFastMode] = useState(initialFastMode ?? false); + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = getFastModeRuntimeState(); + $[0] = t1; + } else { + t1 = $[0]; + } + const runtimeState = t1; + const isCooldown = runtimeState.status === "cooldown"; + const isUnavailable = unavailableReason !== null; + let t2; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t2 = formatModelPricing(getOpus46CostTier(true)); + $[1] = t2; + } else { + t2 = $[1]; + } + const pricing = t2; + let t3; + if ($[2] !== enableFastMode || $[3] !== isUnavailable || $[4] !== model || $[5] !== onDone || $[6] !== setAppState) { + t3 = function handleConfirm() { + if (isUnavailable) { + return; + } + applyFastMode(enableFastMode, setAppState); + logEvent("tengu_fast_mode_toggled", { + enabled: enableFastMode, + source: "picker" as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS + }); + if (enableFastMode) { + const fastIcon = getFastIconString(enableFastMode); + const modelUpdated = !isFastModeSupportedByModel(model) ? ` · model set to ${FAST_MODE_MODEL_DISPLAY}` : ""; + onDone(`${fastIcon} Fast mode ON${modelUpdated} · ${pricing}`); + } else { + setAppState(_temp3); + onDone("Fast mode OFF"); + } + }; + $[2] = enableFastMode; + $[3] = isUnavailable; + $[4] = model; + $[5] = onDone; + $[6] = setAppState; + $[7] = t3; + } else { + t3 = $[7]; + } + const handleConfirm = t3; + let t4; + if ($[8] !== initialFastMode || $[9] !== isUnavailable || $[10] !== onDone || $[11] !== setAppState) { + t4 = function handleCancel() { + if (isUnavailable) { + if (initialFastMode) { + applyFastMode(false, setAppState); + } + onDone("Fast mode OFF", { + display: "system" + }); + return; + } + const message = initialFastMode ? `${getFastIconString()} Kept Fast mode ON` : "Kept Fast mode OFF"; + onDone(message, { + display: "system" + }); + }; + $[8] = initialFastMode; + $[9] = isUnavailable; + $[10] = onDone; + $[11] = setAppState; + $[12] = t4; + } else { + t4 = $[12]; + } + const handleCancel = t4; + let t5; + if ($[13] !== isUnavailable) { + t5 = function handleToggle() { + if (isUnavailable) { + return; + } + setEnableFastMode(_temp4); + }; + $[13] = isUnavailable; + $[14] = t5; + } else { + t5 = $[14]; + } + const handleToggle = t5; + let t6; + if ($[15] !== handleConfirm || $[16] !== handleToggle) { + t6 = { + "confirm:yes": handleConfirm, + "confirm:nextField": handleToggle, + "confirm:next": handleToggle, + "confirm:previous": handleToggle, + "confirm:cycleMode": handleToggle, + "confirm:toggle": handleToggle + }; + $[15] = handleConfirm; + $[16] = handleToggle; + $[17] = t6; + } else { + t6 = $[17]; + } + let t7; + if ($[18] === Symbol.for("react.memo_cache_sentinel")) { + t7 = { + context: "Confirmation" + }; + $[18] = t7; + } else { + t7 = $[18]; + } + useKeybindings(t6, t7); + let t8; + if ($[19] === Symbol.for("react.memo_cache_sentinel")) { + t8 = Fast mode (research preview); + $[19] = t8; + } else { + t8 = $[19]; + } + const title = t8; + let t9; + if ($[20] !== isUnavailable) { + t9 = exitState => exitState.pending ? Press {exitState.keyName} again to exit : isUnavailable ? Esc to cancel : Tab to toggle · Enter to confirm · Esc to cancel; + $[20] = isUnavailable; + $[21] = t9; + } else { + t9 = $[21]; + } + let t10; + if ($[22] !== enableFastMode || $[23] !== unavailableReason) { + t10 = unavailableReason ? {unavailableReason} : <>Fast mode{enableFastMode ? "ON " : "OFF"}{pricing}{isCooldown && runtimeState.status === "cooldown" && {runtimeState.reason === "overloaded" ? "Fast mode overloaded and is temporarily unavailable" : "You've hit your fast limit"}{" \xB7 resets in "}{formatDuration(runtimeState.resetAt - Date.now(), { + hideTrailingZeros: true + })}}; + $[22] = enableFastMode; + $[23] = unavailableReason; + $[24] = t10; + } else { + t10 = $[24]; + } + let t11; + if ($[25] === Symbol.for("react.memo_cache_sentinel")) { + t11 = Learn more:{" "}https://code.claude.com/docs/en/fast-mode; + $[25] = t11; + } else { + t11 = $[25]; + } + let t12; + if ($[26] !== handleCancel || $[27] !== t10 || $[28] !== t9) { + t12 = {t10}{t11}; + $[26] = handleCancel; + $[27] = t10; + $[28] = t9; + $[29] = t12; + } else { + t12 = $[29]; + } + return t12; +} +function _temp4(prev_0) { + return !prev_0; +} +function _temp3(prev) { + return { + ...prev, + fastMode: false + }; +} +function _temp2(s_0) { + return s_0.fastMode; +} +function _temp(s) { + return s.mainLoopModel; +} +async function handleFastModeShortcut(enable: boolean, getAppState: () => AppState, setAppState: (f: (prev: AppState) => AppState) => void): Promise { + const unavailableReason = getFastModeUnavailableReason(); + if (unavailableReason) { + return `Fast mode unavailable: ${unavailableReason}`; + } + const { + mainLoopModel + } = getAppState(); + applyFastMode(enable, setAppState); + logEvent('tengu_fast_mode_toggled', { + enabled: enable, + source: 'shortcut' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS + }); + if (enable) { + const fastIcon = getFastIconString(true); + const modelUpdated = !isFastModeSupportedByModel(mainLoopModel) ? ` · model set to ${FAST_MODE_MODEL_DISPLAY}` : ''; + const pricing = formatModelPricing(getOpus46CostTier(true)); + return `${fastIcon} Fast mode ON${modelUpdated} · ${pricing}`; + } else { + return `Fast mode OFF`; + } +} +export async function call(onDone: LocalJSXCommandOnDone, context: LocalJSXCommandContext, args?: string): Promise { + if (!isFastModeEnabled()) { + return null; + } + + // Fetch org fast mode status before showing the picker. We must know + // whether the org has disabled fast mode before allowing any toggle. + // If a startup prefetch is already in flight, this awaits it. + await prefetchFastModeStatus(); + const arg = args?.trim().toLowerCase(); + if (arg === 'on' || arg === 'off') { + const result = await handleFastModeShortcut(arg === 'on', context.getAppState, context.setAppState); + onDone(result); + return null; + } + const unavailableReason = getFastModeUnavailableReason(); + logEvent('tengu_fast_mode_picker_shown', { + unavailable_reason: (unavailableReason ?? '') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS + }); + return ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsInVzZVN0YXRlIiwiQ29tbWFuZFJlc3VsdERpc3BsYXkiLCJMb2NhbEpTWENvbW1hbmRDb250ZXh0IiwiRGlhbG9nIiwiRmFzdEljb24iLCJnZXRGYXN0SWNvblN0cmluZyIsIkJveCIsIkxpbmsiLCJUZXh0IiwidXNlS2V5YmluZGluZ3MiLCJBbmFseXRpY3NNZXRhZGF0YV9JX1ZFUklGSUVEX1RISVNfSVNfTk9UX0NPREVfT1JfRklMRVBBVEhTIiwibG9nRXZlbnQiLCJBcHBTdGF0ZSIsInVzZUFwcFN0YXRlIiwidXNlU2V0QXBwU3RhdGUiLCJMb2NhbEpTWENvbW1hbmRPbkRvbmUiLCJjbGVhckZhc3RNb2RlQ29vbGRvd24iLCJGQVNUX01PREVfTU9ERUxfRElTUExBWSIsImdldEZhc3RNb2RlTW9kZWwiLCJnZXRGYXN0TW9kZVJ1bnRpbWVTdGF0ZSIsImdldEZhc3RNb2RlVW5hdmFpbGFibGVSZWFzb24iLCJpc0Zhc3RNb2RlRW5hYmxlZCIsImlzRmFzdE1vZGVTdXBwb3J0ZWRCeU1vZGVsIiwicHJlZmV0Y2hGYXN0TW9kZVN0YXR1cyIsImZvcm1hdER1cmF0aW9uIiwiZm9ybWF0TW9kZWxQcmljaW5nIiwiZ2V0T3B1czQ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SDCtyBFbnRlciB0byBjb25maXJtIMK3IEVzYyB0byBjYW5jZWw8L1RleHQ+XG4gICAgICAgIClcbiAgICAgIH1cbiAgICA+XG4gICAgICB7dW5hdmFpbGFibGVSZWFzb24gPyAoXG4gICAgICAgIDxCb3ggbWFyZ2luTGVmdD17Mn0+XG4gICAgICAgICAgPFRleHQgY29sb3I9XCJlcnJvclwiPnt1bmF2YWlsYWJsZVJlYXNvbn08L1RleHQ+XG4gICAgICAgIDwvQm94PlxuICAgICAgKSA6IChcbiAgICAgICAgPD5cbiAgICAgICAgICA8Qm94IGZsZXhEaXJlY3Rpb249XCJjb2x1bW5cIiBnYXA9ezB9IG1hcmdpbkxlZnQ9ezJ9PlxuICAgICAgICAgICAgPEJveCBmbGV4RGlyZWN0aW9uPVwicm93XCIgZ2FwPXsyfT5cbiAgICAgICAgICAgICAgPFRleHQgYm9sZD5GYXN0IG1vZGU8L1RleHQ+XG4gICAgICAgICAgICAgIDxUZXh0XG4gICAgICAgICAgICAgICAgY29sb3I9e2VuYWJsZUZhc3RNb2RlID8gJ2Zhc3RNb2RlJyA6IHVuZGVmaW5lZH1cbiAgICAgICAgICAgICAgICBib2xkPXtlbmFibGVGYXN0TW9kZX1cbiAgICAgICAgICAgICAgPlxuICAgICAgICAgICAgICAgIHtlbmFibGVGYXN0TW9kZSA/ICdPTiAnIDogJ09GRid9XG4gICAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgICAgPFRleHQgZGltQ29sb3I+e3ByaWNpbmd9PC9UZXh0PlxuICAgICAgICAgICAgPC9Cb3g+XG4gICAgICAgICAgPC9Cb3g+XG5cbiAgICAgICAgICB7aXNDb29sZG93biAmJiBydW50aW1lU3RhdGUuc3RhdHVzID09PSAnY29vbGRvd24nICYmIChcbiAgICAgICAgICAgIDxCb3ggbWFyZ2luTGVmdD17Mn0+XG4gICAgICAgICAgICAgIDxUZXh0IGNvbG9yPVwid2FybmluZ1wiPlxuICAgICAgICAgICAgICAgIHtydW50aW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U0sQ0FBQyxDQUFDZ0MsTUFBTSxDQUFDLENBQUMsaUJBQWlCLENBQUMsQ0FBQ0MsaUJBQWlCLENBQUMsR0FBRztBQUU1RSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/install-github-app/index.ts b/_all-in-one-upload/cc-haha/src/commands/install-github-app/index.ts new file mode 100644 index 0000000..9b5b882 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/install-github-app/index.ts @@ -0,0 +1,13 @@ +import type { Command } from '../../commands.js' +import { isEnvTruthy } from '../../utils/envUtils.js' + +const installGitHubApp = { + type: 'local-jsx', + name: 'install-github-app', + description: 'Set up Claude GitHub Actions for a repository', + availability: ['claude-ai', 'console'], + isEnabled: () => !isEnvTruthy(process.env.DISABLE_INSTALL_GITHUB_APP_COMMAND), + load: () => import('./install-github-app.js'), +} satisfies Command + +export default installGitHubApp diff --git a/_all-in-one-upload/cc-haha/src/commands/login/index.ts b/_all-in-one-upload/cc-haha/src/commands/login/index.ts new file mode 100644 index 0000000..f9bc5a2 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/login/index.ts @@ -0,0 +1,14 @@ +import type { Command } from '../../commands.js' +import { hasAnthropicApiKeyAuth } from '../../utils/auth.js' +import { isEnvTruthy } from '../../utils/envUtils.js' + +export default () => + ({ + type: 'local-jsx', + name: 'login', + description: hasAnthropicApiKeyAuth() + ? 'Switch Anthropic accounts' + : 'Sign in with your Anthropic account', + isEnabled: () => !isEnvTruthy(process.env.DISABLE_LOGIN_COMMAND), + load: () => import('./login.js'), + }) satisfies Command diff --git a/_all-in-one-upload/cc-haha/src/commands/mobile/index.ts b/_all-in-one-upload/cc-haha/src/commands/mobile/index.ts new file mode 100644 index 0000000..2657aa8 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/mobile/index.ts @@ -0,0 +1,11 @@ +import type { Command } from '../../commands.js' + +const mobile = { + type: 'local-jsx', + name: 'mobile', + aliases: ['ios', 'android'], + description: 'Show QR code to download the Claude mobile app', + load: () => import('./mobile.js'), +} satisfies Command + +export default mobile diff --git a/_all-in-one-upload/cc-haha/src/commands/passes/passes.tsx b/_all-in-one-upload/cc-haha/src/commands/passes/passes.tsx new file mode 100644 index 0000000..c17cb59 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/passes/passes.tsx @@ -0,0 +1,24 @@ +import * as React from 'react'; +import { Passes } from '../../components/Passes/Passes.js'; +import { logEvent } from '../../services/analytics/index.js'; +import { getCachedRemainingPasses } from '../../services/api/referral.js'; +import type { LocalJSXCommandOnDone } from '../../types/command.js'; +import { getGlobalConfig, saveGlobalConfig } from '../../utils/config.js'; +export async function call(onDone: LocalJSXCommandOnDone): Promise { + // Mark that user has visited /passes so we stop showing the upsell + const config = getGlobalConfig(); + const isFirstVisit = !config.hasVisitedPasses; + if (isFirstVisit) { + const remaining = getCachedRemainingPasses(); + saveGlobalConfig(current => ({ + ...current, + hasVisitedPasses: true, + passesLastSeenRemaining: remaining ?? current.passesLastSeenRemaining + })); + } + logEvent('tengu_guest_passes_visited', { + is_first_visit: isFirstVisit + }); + return ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIlBhc3NlcyIsImxvZ0V2ZW50IiwiZ2V0Q2FjaGVkUmVtYWluaW5nUGFzc2VzIiwiTG9jYWxKU1hDb21tYW5kT25Eb25lIiwiZ2V0R2xvYmFsQ29uZmlnIiwic2F2ZUdsb2JhbENvbmZpZyIsImNhbGwiLCJvbkRvbmUiLCJQcm9taXNlIiwiUmVhY3ROb2RlIiwiY29uZmlnIiwiaXNGaXJzdFZpc2l0IiwiaGFzVmlzaXRlZFBhc3NlcyIsInJlbWFpbmluZyIsImN1cnJlbnQiLCJwYXNzZXNMYXN0U2VlblJlbWFpbmluZyIsImlzX2ZpcnN0X3Zpc2l0Il0sInNvdXJjZXMiOlsicGFzc2VzLnRzeCJd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UMsT0FBTyxDQUFDQztJQUNoRCxDQUFDLENBQUMsQ0FBQztFQUNMO0VBQ0FkLFFBQVEsQ0FBQyw0QkFBNEIsRUFBRTtJQUFFZSxjQUFjLEVBQUVMO0VBQWEsQ0FBQyxDQUFDO0VBQ3hFLE9BQU8sQ0FBQyxNQUFNLENBQUMsTUFBTSxDQUFDLENBQUNKLE1BQU0sQ0FBQyxHQUFHO0FBQ25DIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/plugin/DiscoverPlugins.tsx b/_all-in-one-upload/cc-haha/src/commands/plugin/DiscoverPlugins.tsx new file mode 100644 index 0000000..d62a8db --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/plugin/DiscoverPlugins.tsx @@ -0,0 +1,781 @@ +import { c as _c } from "react/compiler-runtime"; +import figures from 'figures'; +import * as React from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; +import { ConfigurableShortcutHint } from '../../components/ConfigurableShortcutHint.js'; +import { Byline } from '../../components/design-system/Byline.js'; +import { SearchBox } from '../../components/SearchBox.js'; +import { useSearchInput } from '../../hooks/useSearchInput.js'; +import { useTerminalSize } from '../../hooks/useTerminalSize.js'; +// eslint-disable-next-line custom-rules/prefer-use-keybindings -- useInput needed for raw search mode text input +import { Box, Text, useInput, useTerminalFocus } from '../../ink.js'; +import { useKeybinding, useKeybindings } from '../../keybindings/useKeybinding.js'; +import type { LoadedPlugin } from '../../types/plugin.js'; +import { count } from '../../utils/array.js'; +import { openBrowser } from '../../utils/browser.js'; +import { logForDebugging } from '../../utils/debug.js'; +import { errorMessage } from '../../utils/errors.js'; +import { clearAllCaches } from '../../utils/plugins/cacheUtils.js'; +import { formatInstallCount, getInstallCounts } from '../../utils/plugins/installCounts.js'; +import { isPluginGloballyInstalled } from '../../utils/plugins/installedPluginsManager.js'; +import { createPluginId, detectEmptyMarketplaceReason, type EmptyMarketplaceReason, formatFailureDetails, formatMarketplaceLoadingErrors, loadMarketplacesWithGracefulDegradation } from '../../utils/plugins/marketplaceHelpers.js'; +import { loadKnownMarketplacesConfig } from '../../utils/plugins/marketplaceManager.js'; +import { OFFICIAL_MARKETPLACE_NAME } from '../../utils/plugins/officialMarketplace.js'; +import { installPluginFromMarketplace } from '../../utils/plugins/pluginInstallationHelpers.js'; +import { isPluginBlockedByPolicy } from '../../utils/plugins/pluginPolicy.js'; +import { plural } from '../../utils/stringUtils.js'; +import { truncateToWidth } from '../../utils/truncate.js'; +import { findPluginOptionsTarget, PluginOptionsFlow } from './PluginOptionsFlow.js'; +import { PluginTrustWarning } from './PluginTrustWarning.js'; +import { buildPluginDetailsMenuOptions, extractGitHubRepo, type InstallablePlugin } from './pluginDetailsHelpers.js'; +import type { ViewState as ParentViewState } from './types.js'; +import { usePagination } from './usePagination.js'; +type Props = { + error: string | null; + setError: (error: string | null) => void; + result: string | null; + setResult: (result: string | null) => void; + setViewState: (state: ParentViewState) => void; + onInstallComplete?: () => void | Promise; + onSearchModeChange?: (isActive: boolean) => void; + targetPlugin?: string; +}; +type ViewState = 'plugin-list' | 'plugin-details' | { + type: 'plugin-options'; + plugin: LoadedPlugin; + pluginId: string; +}; +export function DiscoverPlugins({ + error, + setError, + result: _result, + setResult, + setViewState: setParentViewState, + onInstallComplete, + onSearchModeChange, + targetPlugin +}: Props): React.ReactNode { + // View state + const [viewState, setViewState] = useState('plugin-list'); + const [selectedPlugin, setSelectedPlugin] = useState(null); + + // Data state + const [availablePlugins, setAvailablePlugins] = useState([]); + const [loading, setLoading] = useState(true); + const [installCounts, setInstallCounts] = useState | null>(null); + + // Search state + const [isSearchMode, setIsSearchModeRaw] = useState(false); + const setIsSearchMode = useCallback((active: boolean) => { + setIsSearchModeRaw(active); + onSearchModeChange?.(active); + }, [onSearchModeChange]); + const { + query: searchQuery, + setQuery: setSearchQuery, + cursorOffset: searchCursorOffset + } = useSearchInput({ + isActive: viewState === 'plugin-list' && isSearchMode && !loading, + onExit: () => { + setIsSearchMode(false); + } + }); + const isTerminalFocused = useTerminalFocus(); + const { + columns: terminalWidth + } = useTerminalSize(); + + // Filter plugins based on search query + const filteredPlugins = useMemo(() => { + if (!searchQuery) return availablePlugins; + const lowerQuery = searchQuery.toLowerCase(); + return availablePlugins.filter(plugin => plugin.entry.name.toLowerCase().includes(lowerQuery) || plugin.entry.description?.toLowerCase().includes(lowerQuery) || plugin.marketplaceName.toLowerCase().includes(lowerQuery)); + }, [availablePlugins, searchQuery]); + + // Selection state + const [selectedIndex, setSelectedIndex] = useState(0); + const [selectedForInstall, setSelectedForInstall] = useState>(new Set()); + const [installingPlugins, setInstallingPlugins] = useState>(new Set()); + + // Pagination for plugin list (continuous scrolling) + const pagination = usePagination({ + totalItems: filteredPlugins.length, + selectedIndex + }); + + // Reset selection when search query changes + useEffect(() => { + setSelectedIndex(0); + }, [searchQuery]); + + // Details view state + const [detailsMenuIndex, setDetailsMenuIndex] = useState(0); + const [isInstalling, setIsInstalling] = useState(false); + const [installError, setInstallError] = useState(null); + + // Warning state for non-critical errors + const [warning, setWarning] = useState(null); + + // Empty state reason + const [emptyReason, setEmptyReason] = useState(null); + + // Load all plugins from all marketplaces + useEffect(() => { + async function loadAllPlugins() { + try { + const config = await loadKnownMarketplacesConfig(); + + // Load marketplaces with graceful degradation + const { + marketplaces, + failures + } = await loadMarketplacesWithGracefulDegradation(config); + + // Collect all plugins from all marketplaces + const allPlugins: InstallablePlugin[] = []; + for (const { + name, + data: marketplace + } of marketplaces) { + if (marketplace) { + for (const entry of marketplace.plugins) { + const pluginId = createPluginId(entry.name, name); + allPlugins.push({ + entry, + marketplaceName: name, + pluginId, + // Only block when globally installed (user/managed scope). + // Project/local-scope installs don't block — user may want to + // promote to user scope so it's available everywhere (gh-29997). + isInstalled: isPluginGloballyInstalled(pluginId) + }); + } + } + } + + // Filter out installed and policy-blocked plugins + const uninstalledPlugins = allPlugins.filter(p => !p.isInstalled && !isPluginBlockedByPolicy(p.pluginId)); + + // Fetch install counts and sort by popularity + try { + const counts = await getInstallCounts(); + setInstallCounts(counts); + if (counts) { + // Sort by install count (descending), then alphabetically + uninstalledPlugins.sort((a_0, b_0) => { + const countA = counts.get(a_0.pluginId) ?? 0; + const countB = counts.get(b_0.pluginId) ?? 0; + if (countA !== countB) return countB - countA; + return a_0.entry.name.localeCompare(b_0.entry.name); + }); + } else { + // No counts available - sort alphabetically + uninstalledPlugins.sort((a_1, b_1) => a_1.entry.name.localeCompare(b_1.entry.name)); + } + } catch (error_0) { + // Log the error, then gracefully degrade to alphabetical sort + logForDebugging(`Failed to fetch install counts: ${errorMessage(error_0)}`); + uninstalledPlugins.sort((a, b) => a.entry.name.localeCompare(b.entry.name)); + } + setAvailablePlugins(uninstalledPlugins); + + // Detect empty reason if no plugins available + const configuredCount = Object.keys(config).length; + if (uninstalledPlugins.length === 0) { + const reason = await detectEmptyMarketplaceReason({ + configuredMarketplaceCount: configuredCount, + failedMarketplaceCount: failures.length + }); + setEmptyReason(reason); + } + + // Handle marketplace loading errors/warnings + const successCount = count(marketplaces, m => m.data !== null); + const errorResult = formatMarketplaceLoadingErrors(failures, successCount); + if (errorResult) { + if (errorResult.type === 'warning') { + setWarning(errorResult.message + '. Showing available plugins.'); + } else { + throw new Error(errorResult.message); + } + } + + // Handle targetPlugin - navigate directly to plugin details + // Search in allPlugins (before filtering) to handle installed plugins gracefully + if (targetPlugin) { + const foundPlugin = allPlugins.find(p_0 => p_0.entry.name === targetPlugin); + if (foundPlugin) { + if (foundPlugin.isInstalled) { + setError(`Plugin '${foundPlugin.pluginId}' is already installed. Use '/plugin' to manage existing plugins.`); + } else { + setSelectedPlugin(foundPlugin); + setViewState('plugin-details'); + } + } else { + setError(`Plugin "${targetPlugin}" not found in any marketplace`); + } + } + } catch (err) { + setError(err instanceof Error ? err.message : 'Failed to load plugins'); + } finally { + setLoading(false); + } + } + void loadAllPlugins(); + }, [setError, targetPlugin]); + + // Install selected plugins + const installSelectedPlugins = async () => { + if (selectedForInstall.size === 0) return; + const pluginsToInstall = availablePlugins.filter(p_1 => selectedForInstall.has(p_1.pluginId)); + setInstallingPlugins(new Set(pluginsToInstall.map(p_2 => p_2.pluginId))); + let successCount_0 = 0; + let failureCount = 0; + const newFailedPlugins: Array<{ + name: string; + reason: string; + }> = []; + for (const plugin_0 of pluginsToInstall) { + const result = await installPluginFromMarketplace({ + pluginId: plugin_0.pluginId, + entry: plugin_0.entry, + marketplaceName: plugin_0.marketplaceName, + scope: 'user' + }); + if (result.success) { + successCount_0++; + } else { + failureCount++; + newFailedPlugins.push({ + name: plugin_0.entry.name, + reason: result.error + }); + } + } + setInstallingPlugins(new Set()); + setSelectedForInstall(new Set()); + clearAllCaches(); + + // Handle installation results + if (failureCount === 0) { + const message = `✓ Installed ${successCount_0} ${plural(successCount_0, 'plugin')}. ` + `Run /reload-plugins to activate.`; + setResult(message); + } else if (successCount_0 === 0) { + setError(`Failed to install: ${formatFailureDetails(newFailedPlugins, true)}`); + } else { + const message_0 = `✓ Installed ${successCount_0} of ${successCount_0 + failureCount} plugins. ` + `Failed: ${formatFailureDetails(newFailedPlugins, false)}. ` + `Run /reload-plugins to activate successfully installed plugins.`; + setResult(message_0); + } + if (successCount_0 > 0) { + if (onInstallComplete) { + await onInstallComplete(); + } + } + setParentViewState({ + type: 'menu' + }); + }; + + // Install single plugin from details view + const handleSinglePluginInstall = async (plugin_1: InstallablePlugin, scope: 'user' | 'project' | 'local' = 'user') => { + setIsInstalling(true); + setInstallError(null); + const result_0 = await installPluginFromMarketplace({ + pluginId: plugin_1.pluginId, + entry: plugin_1.entry, + marketplaceName: plugin_1.marketplaceName, + scope + }); + if (result_0.success) { + const loaded = await findPluginOptionsTarget(plugin_1.pluginId); + if (loaded) { + setIsInstalling(false); + setViewState({ + type: 'plugin-options', + plugin: loaded, + pluginId: plugin_1.pluginId + }); + return; + } + setResult(result_0.message); + if (onInstallComplete) { + await onInstallComplete(); + } + setParentViewState({ + type: 'menu' + }); + } else { + setIsInstalling(false); + setInstallError(result_0.error); + } + }; + + // Handle error state + useEffect(() => { + if (error) { + setResult(error); + } + }, [error, setResult]); + + // Escape in plugin-details view - go back to plugin-list + useKeybinding('confirm:no', () => { + setViewState('plugin-list'); + setSelectedPlugin(null); + }, { + context: 'Confirmation', + isActive: viewState === 'plugin-details' + }); + + // Escape in plugin-list view (not search mode) - exit to parent menu + useKeybinding('confirm:no', () => { + setParentViewState({ + type: 'menu' + }); + }, { + context: 'Confirmation', + isActive: viewState === 'plugin-list' && !isSearchMode + }); + + // Handle entering search mode (non-escape keys) + useInput((input, _key) => { + const keyIsNotCtrlOrMeta = !_key.ctrl && !_key.meta; + if (!isSearchMode) { + // Enter search mode with '/' or any printable character + if (input === '/' && keyIsNotCtrlOrMeta) { + setIsSearchMode(true); + setSearchQuery(''); + } else if (keyIsNotCtrlOrMeta && input.length > 0 && !/^\s+$/.test(input) && + // Don't enter search mode for navigation keys + input !== 'j' && input !== 'k' && input !== 'i') { + setIsSearchMode(true); + setSearchQuery(input); + } + } + }, { + isActive: viewState === 'plugin-list' && !loading + }); + + // Plugin-list navigation (non-search mode) + useKeybindings({ + 'select:previous': () => { + if (selectedIndex === 0) { + setIsSearchMode(true); + } else { + pagination.handleSelectionChange(selectedIndex - 1, setSelectedIndex); + } + }, + 'select:next': () => { + if (selectedIndex < filteredPlugins.length - 1) { + pagination.handleSelectionChange(selectedIndex + 1, setSelectedIndex); + } + }, + 'select:accept': () => { + if (selectedIndex === filteredPlugins.length && selectedForInstall.size > 0) { + void installSelectedPlugins(); + } else if (selectedIndex < filteredPlugins.length) { + const plugin_2 = filteredPlugins[selectedIndex]; + if (plugin_2) { + if (plugin_2.isInstalled) { + setParentViewState({ + type: 'manage-plugins', + targetPlugin: plugin_2.entry.name, + targetMarketplace: plugin_2.marketplaceName + }); + } else { + setSelectedPlugin(plugin_2); + setViewState('plugin-details'); + setDetailsMenuIndex(0); + setInstallError(null); + } + } + } + } + }, { + context: 'Select', + isActive: viewState === 'plugin-list' && !isSearchMode + }); + useKeybindings({ + 'plugin:toggle': () => { + if (selectedIndex < filteredPlugins.length) { + const plugin_3 = filteredPlugins[selectedIndex]; + if (plugin_3 && !plugin_3.isInstalled) { + const newSelection = new Set(selectedForInstall); + if (newSelection.has(plugin_3.pluginId)) { + newSelection.delete(plugin_3.pluginId); + } else { + newSelection.add(plugin_3.pluginId); + } + setSelectedForInstall(newSelection); + } + } + }, + 'plugin:install': () => { + if (selectedForInstall.size > 0) { + void installSelectedPlugins(); + } + } + }, { + context: 'Plugin', + isActive: viewState === 'plugin-list' && !isSearchMode + }); + + // Plugin-details navigation + const detailsMenuOptions = React.useMemo(() => { + if (!selectedPlugin) return []; + const hasHomepage = selectedPlugin.entry.homepage; + const githubRepo = extractGitHubRepo(selectedPlugin); + return buildPluginDetailsMenuOptions(hasHomepage, githubRepo); + }, [selectedPlugin]); + useKeybindings({ + 'select:previous': () => { + if (detailsMenuIndex > 0) { + setDetailsMenuIndex(detailsMenuIndex - 1); + } + }, + 'select:next': () => { + if (detailsMenuIndex < detailsMenuOptions.length - 1) { + setDetailsMenuIndex(detailsMenuIndex + 1); + } + }, + 'select:accept': () => { + if (!selectedPlugin) return; + const action = detailsMenuOptions[detailsMenuIndex]?.action; + const hasHomepage_0 = selectedPlugin.entry.homepage; + const githubRepo_0 = extractGitHubRepo(selectedPlugin); + if (action === 'install-user') { + void handleSinglePluginInstall(selectedPlugin, 'user'); + } else if (action === 'install-project') { + void handleSinglePluginInstall(selectedPlugin, 'project'); + } else if (action === 'install-local') { + void handleSinglePluginInstall(selectedPlugin, 'local'); + } else if (action === 'homepage' && hasHomepage_0) { + void openBrowser(hasHomepage_0); + } else if (action === 'github' && githubRepo_0) { + void openBrowser(`https://github.com/${githubRepo_0}`); + } else if (action === 'back') { + setViewState('plugin-list'); + setSelectedPlugin(null); + } + } + }, { + context: 'Select', + isActive: viewState === 'plugin-details' && !!selectedPlugin + }); + if (typeof viewState === 'object' && viewState.type === 'plugin-options') { + const { + plugin: plugin_4, + pluginId: pluginId_0 + } = viewState; + function finish(msg: string): void { + setResult(msg); + if (onInstallComplete) { + void onInstallComplete(); + } + setParentViewState({ + type: 'menu' + }); + } + return { + switch (outcome) { + case 'configured': + finish(`✓ Installed and configured ${plugin_4.name}. Run /reload-plugins to apply.`); + break; + case 'skipped': + finish(`✓ Installed ${plugin_4.name}. Run /reload-plugins to apply.`); + break; + case 'error': + finish(`Installed but failed to save config: ${detail}`); + break; + } + }} />; + } + + // Loading state + if (loading) { + return Loading…; + } + + // Error state + if (error) { + return {error}; + } + + // Plugin details view + if (viewState === 'plugin-details' && selectedPlugin) { + const hasHomepage_1 = selectedPlugin.entry.homepage; + const githubRepo_1 = extractGitHubRepo(selectedPlugin); + const menuOptions = buildPluginDetailsMenuOptions(hasHomepage_1, githubRepo_1); + return + + Plugin details + + + + {selectedPlugin.entry.name} + from {selectedPlugin.marketplaceName} + {selectedPlugin.entry.version && Version: {selectedPlugin.entry.version}} + {selectedPlugin.entry.description && + {selectedPlugin.entry.description} + } + {selectedPlugin.entry.author && + + By:{' '} + {typeof selectedPlugin.entry.author === 'string' ? selectedPlugin.entry.author : selectedPlugin.entry.author.name} + + } + + + + + {installError && + Error: {installError} + } + + + {menuOptions.map((option, index) => + {detailsMenuIndex === index && {'> '}} + {detailsMenuIndex !== index && {' '}} + + {isInstalling && option.action.startsWith('install-') ? 'Installing…' : option.label} + + )} + + + + + + + + + + + ; + } + + // Empty state + if (availablePlugins.length === 0) { + return + + Discover plugins + + + + + Esc to go back + + + ; + } + + // Get visible plugins from pagination + const visiblePlugins = pagination.getVisibleItems(filteredPlugins); + return + + Discover plugins + {pagination.needsPagination && + {' '} + ({pagination.scrollPosition.current}/ + {pagination.scrollPosition.total}) + } + + + {/* Search box */} + + + + + {/* Warning banner */} + {warning && + + {figures.warning} {warning} + + } + + {/* No search results */} + {filteredPlugins.length === 0 && searchQuery && + No plugins match "{searchQuery}" + } + + {/* Scroll up indicator */} + {pagination.scrollPosition.canScrollUp && + {figures.arrowUp} more above + } + + {/* Plugin list - use startIndex in key to force re-render on scroll */} + {visiblePlugins.map((plugin_5, visibleIndex) => { + const actualIndex = pagination.toActualIndex(visibleIndex); + const isSelected = selectedIndex === actualIndex; + const isSelectedForInstall = selectedForInstall.has(plugin_5.pluginId); + const isInstallingThis = installingPlugins.has(plugin_5.pluginId); + const isLast = visibleIndex === visiblePlugins.length - 1; + return + + + {isSelected && !isSearchMode ? figures.pointer : ' '}{' '} + + + {isInstallingThis ? figures.ellipsis : isSelectedForInstall ? figures.radioOn : figures.radioOff}{' '} + {plugin_5.entry.name} + · {plugin_5.marketplaceName} + {plugin_5.entry.tags?.includes('community-managed') && [Community Managed]} + {installCounts && plugin_5.marketplaceName === OFFICIAL_MARKETPLACE_NAME && + {' · '} + {formatInstallCount(installCounts.get(plugin_5.pluginId) ?? 0)}{' '} + installs + } + + + {plugin_5.entry.description && + + {truncateToWidth(plugin_5.entry.description, 60)} + + } + ; + })} + + {/* Scroll down indicator */} + {pagination.scrollPosition.canScrollDown && + {figures.arrowDown} more below + } + + {/* Error messages */} + {error && + + {figures.cross} {error} + + } + + 0} canToggle={selectedIndex < filteredPlugins.length && !filteredPlugins[selectedIndex]?.isInstalled} /> + ; +} +function DiscoverPluginsKeyHint(t0) { + const $ = _c(10); + const { + hasSelection, + canToggle + } = t0; + let t1; + if ($[0] !== hasSelection) { + t1 = hasSelection && ; + $[0] = hasSelection; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t2 = type to search; + $[2] = t2; + } else { + t2 = $[2]; + } + let t3; + if ($[3] !== canToggle) { + t3 = canToggle && ; + $[3] = canToggle; + $[4] = t3; + } else { + t3 = $[4]; + } + let t4; + let t5; + if ($[5] === Symbol.for("react.memo_cache_sentinel")) { + t4 = ; + t5 = ; + $[5] = t4; + $[6] = t5; + } else { + t4 = $[5]; + t5 = $[6]; + } + let t6; + if ($[7] !== t1 || $[8] !== t3) { + t6 = {t1}{t2}{t3}{t4}{t5}; + $[7] = t1; + $[8] = t3; + $[9] = t6; + } else { + t6 = $[9]; + } + return t6; +} + +/** + * Context-aware empty state message for the Discover screen + */ +function EmptyStateMessage(t0) { + const $ = _c(6); + const { + reason + } = t0; + switch (reason) { + case "git-not-installed": + { + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = <>Git is required to install marketplaces.Please install git and restart Claude Code.; + $[0] = t1; + } else { + t1 = $[0]; + } + return t1; + } + case "all-blocked-by-policy": + { + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = <>Your organization policy does not allow any external marketplaces.Contact your administrator.; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; + } + case "policy-restricts-sources": + { + let t1; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t1 = <>Your organization restricts which marketplaces can be added.Switch to the Marketplaces tab to view allowed sources.; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; + } + case "all-marketplaces-failed": + { + let t1; + if ($[3] === Symbol.for("react.memo_cache_sentinel")) { + t1 = <>Failed to load marketplace data.Check your network connection.; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; + } + case "all-plugins-installed": + { + let t1; + if ($[4] === Symbol.for("react.memo_cache_sentinel")) { + t1 = <>All available plugins are already installed.Check for new plugins later or add more marketplaces.; + $[4] = t1; + } else { + t1 = $[4]; + } + return t1; + } + case "no-marketplaces-configured": + default: + { + let t1; + if ($[5] === Symbol.for("react.memo_cache_sentinel")) { + t1 = <>No plugins available.Add a marketplace first using the Marketplaces tab.; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; + } + } +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJmaWd1cmVzIiwiUmVhY3QiLCJ1c2VDYWxsYmFjayIsInVzZUVmZmVjdCIsInVzZU1lbW8iLCJ1c2VTdGF0ZSIsIkNvbmZpZ3VyYWJsZVNob3J0Y3V0SGludCIsIkJ5bGluZSIsIlNl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3VudHMpIHtcbiAgICAgICAgICAgIC8vIFNvcnQgYnkgaW5zdGFsbCBjb3VudCAoZGVzY2VuZGluZyksIHRoZW4gYWxwaGFiZXRpY2FsbHlcbiAgICAgICAgICAgIHVuaW5zdGFsbGVkUGx1Z2lucy5zb3J0KChhLCBiKSA9PiB7XG4gICAgICAgICAgICAgIGNvbnN0IGNvdW50QSA9IGNvdW50cy5nZXQoYS5wbHVnaW5JZCkgPz8gMFxuICAgICAgICAgICAgICBjb25zdCBjb3VudEIgPSBjb3VudHMuZ2V0KGIucGx1Z2luSWQpID8/IDBcbiAgICAgICAgICAgICAgaWYgKGNvdW50QSAhPT0gY291bnRCKSByZXR1cm4gY291bnRCIC0gY291bnRBXG4gICAgICAgICAgICAgIHJldHVybiBhLmVudHJ5Lm5hbWUubG9jYWxlQ29tcGFyZShiLmVudHJ5Lm5hbWUpXG4gICAgICAgICAgICB9KVxuICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAvLyBObyBjb3VudHMgYXZhaWxhYmxlIC0gc29ydCBhbHBoYWJldGljYWxseVxuICAgICAgICAgICAgdW5pbnN0YWxsZWRQbHVnaW5zLnNvcnQoKGEsIGIpID0+XG4gICAgICAgICAgICAgIGEuZW50cnkubmFtZS5sb2NhbGVDb21wYXJlKGIuZW50cnkubmFtZSksXG4gICAgICAgICAgICApXG4gICAgICAgICAgfVxuICAgICAgICB9IGNhdGNoIChlcnJvcikge1xuICAgICAgICAgIC8vIExvZyB0aGUgZXJyb3IsIHRoZW4gZ3JhY2VmdWxseSBkZWdyYWRlIHRvIGFscGhhYmV0aWNhbCBzb3J0XG4gICAgICAgICAgbG9nRm9yRGVidWdnaW5nKFxuICAgICAgICAgICAgYEZhaWxlZCB0byBmZXRjaCBpbnN0YWxsIGNvdW50czogJHtlcnJvck1lc3NhZ2UoZXJyb3IpfWAsXG4gICAgICAgICAgKVxuICAgICAgICAgIHVuaW5zdGFsbGVkUGx1Z2lucy5zb3J0KChhLCBiKSA9Plx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ZSB7XG4gICAgICAgICAgICAgIHNldFNlbGVjdGVkUGx1Z2luKHBsdWdpbilcbiAgICAgICAgICAgICAgc2V0Vmlld1N0YXRlKCdwbHVnaW4tZGV0YWlscycpXG4gICAgICAgICAgICAgIHNldERldGFpbHNNZW51SW5kZXgoMClcbiAgICAgICAgICAgICAgc2V0SW5zdGFsbEVycm9yKG51bGwpXG4gICAgICAgICAgICB9XG4gICAgICAgICAgfVxuICAgICAgICB9XG4gICAgICB9LFxuICAgIH0sXG4gICAge1xuICAgICAgY29udGV4dDogJ1NlbGVjdCcsXG4gICAgICBpc0FjdGl2ZTogdmlld1N0YXRlID09PSAncGx1Z2luLWxpc3QnICYmICFpc1NlYXJjaE1vZGUsXG4gICAgfSxcbiAgKVxuXG4gIHVzZUtleWJpbmRpbmdzKFxuICAgIHtcbiAgICAgICdwbHVnaW46dG9nZ2xlJzogKCkgPT4ge1xuICAgICAgICBpZiAoc2VsZWN0ZWRJbmRleCA8IGZpbHRlcmVkUGx1Z2lucy5sZW5ndGgpIHtcbiAgICAgICAgICBjb25zdCBwbHVnaW4gPSBmaWx0ZXJlZFBsdWdpbnNbc2VsZWN0ZWRJbmRleF1cbiAgICAgICAgICBpZiAocGx1Z2luICYmICFwbHVnaW4uaXNJbnN0YWxsZWQpIHtcbiAgICAgICAgICAgIGNvbnN0IG5ld1NlbGVjdGlvbiA9IG5ldyBTZXQoc2VsZWN0ZWRGb3JJbnN0YWxsKVxuICAgICAgICAgICAgaWYgKG5ld1NlbGVjdGlvbi5oYXMocGx1Z2luLnBsdWdpbklkKSkge1xuICAgICAgICAgICAgICBuZXdTZWxlY3Rpb24uZGVsZXRlKHBsdWdpbi5wbHVnaW5JZClcbiAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgIG5ld1NlbGVjdGlvbi5hZGQocGx1Z2luLnBsdWdpbklkKVxuICAgICAgICAgICAgfVxuICAgICAgICAgICAgc2V0U2VsZWN0ZWRGb3JJbnN0YWxsKG5ld1N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CBkZXRhaWwpID0+IHtcbiAgICAgICAgICBzd2l0Y2ggKG91dGNvbWUpIHtcbiAgICAgICAgICAgIGNhc2UgJ2NvbmZpZ3VyZWQnOlxuICAgICAgICAgICAgICBmaW5pc2goXG4gICAgICAgICAgICAgICAgYOKckyBJbnN0YWxsZWQgYW5kIGNvbmZpZ3VyZWQgJHtwbHVnaW4ubmFtZX0uIFJ1biAvcmVsb2FkLXBsdWdpbnMgdG8gYXBwbHkuYCxcbiAgICAgICAgICAgICAgKVxuICAgICAgICAgICAgICBicmVha1xuICAgICAgICAgICAgY2FzZSAnc2tpcHBlZCc6XG4gICAgICAgICAgICAgIGZpbmlzaChcbiAgICAgICAgICAgICAgICBg4pyTIEluc3RhbGxlZCAke3BsdWdpbi5uYW1lfS4gUnVuIC9yZWxvYWQtcGx1Z2lucyB0byBhcHBseS5gLFxuICAgICAgICAgICAgICApXG4gICAgICAgICAgICAgIGJyZWFrXG4gICAgICAgICAgICBjYXNlICdlcnJvcic6XG4gICAgICAgICAgICAgIGZpbmlzaChgSW5zdGFsbGVkIGJ1dCBmYWlsZWQgdG8gc2F2ZSBjb25maWc6ICR7ZGV0YWlsfWApXG4gICAgICAgICAgICAgIGJyZWFrXG4gICAgICAgICAgfVxuICAgICAgICB9fVxuICAgICAgLz5cbiAgICApXG4gIH1cblxuICAvLyBMb2FkaW5nIHN0YXRlXG4gIGlmIChsb2FkaW5nKSB7XG4gICAgcmV0dXJuIDxUZXh0PkxvYWRpbmfigKY8L1RleHQ+XG4gIH1cblxuICAvLyBFcnJvciBzdGF0ZVxuICBpZiAoZXJyb3IpIHtcbiAgICByZXR1cm4gPFRleHQgY29sb3I9XCJlcnJvclwiPntlcnJvcn08L1RleHQ+XG4gIH1cblxuICAvLyBQbHVnaW4gZGV0YWlscyB2aWV3XG4gIGlmICh2aWV3U3RhdGUgPT09ICdwbHVnaW4tZGV0YWlscycgJiYgc2VsZWN0ZWRQbHVnaW4pIHtcbiAgICBjb25zdCBoYXNIb21lc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AgICAgICB7c2VsZWN0ZWRQbHVnaW4uZW50cnkuYXV0aG9yICYmIChcbiAgICAgICAgICAgIDxCb3ggbWFyZ2luVG9wPXsxfT5cbiAgICAgICAgICAgICAgPFRleHQgZGltQ29sb3I+XG4gICAgICAgICAgICAgICAgQnk6eycgJ31cbiAgICAgICAgICAgICAgICB7dHlwZW9mIHNlbGVjdGVkUGx1Z2luLmVudHJ5LmF1dGhvciA9PT0gJ3N0cmluZydcbiAgICAgICAgICAgICAgICAgID8gc2VsZWN0ZWRQbHVnaW4uZW50cnkuYXV0aG9yXG4gICAgICAgICAgICAgICAgICA6IHNlbGVjdGVkUGx1Z2luLmVudHJ5LmF1dGhvci5uYW1lfVxuICAgICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICA8L0JveD5cbiAgICAgICAgICApfVxuICAgICAgICA8L0JveD5cblxuICAgICAgICA8UGx1Z2luVHJ1c3RXYXJuaW5nIC8+XG5cbiAgICAgICAge2luc3RhbGxFcnJvciAmJiAoXG4gICAgICAgICAgPEJveCBtYXJnaW5Cb3R0b209ezF9PlxuICAgICAgICAgICAgPFRleHQgY29sb3I9XCJlcnJvclwiPkVycm9yOiB7aW5zdGFsbEVycm9yfTwvVGV4dD5cbiAgICAgICAgICA8L0JveD5cbiAgICAgICAgKX1cblxuICAgICAgICA8Qm94IGZsZXhEaXJlY3Rpb249XCJjb2x1bW5cIj5cbiAgICAgICAgICB7bWVudU9wdGlvbnMubWFwKChvcHRpb24sIGluZGV4KSA9PiAoXG4gICAgICAgICAgICA8Qm94IGtleT17b3B0aW9uLmFjdGlvbn0+XG4gICAgICAgICAgICAgIHtkZXRhaWxzTWVudUluZGV4ID09PSBpbmRleCAmJiA8VGV4dD57Jz4gJ308L1RleHQ+fVxuICAgICAgICAgICAgICB7ZGV0YWlsc01lbnVJbmRleCAhPT0gaW5kZXggJiYgPFRleHQ+eycgICd9PC9UZXh0Pn1cbiAgICAgICAgICAgICAgPFRleHQgYm9sZD17ZGV0YWlsc01lbnVJbmRleCA9PT0gaW5kZXh9PlxuICAgICAgICAgICAgICAgIHtpc0luc3RhbGxpbmcgJiYgb3B0aW9uLmFjdGlvbi5zdGFydHNXaXRoKCdpbnN0YWxsLScpXG4gICAgICAgICAgICAgICAgICA/ICdJbnN0YWxsaW5n4oCmJ1xuICAgICAgICAgICAgICAgICAgOiBvcHRpb24ubGFiZWx9XG4gICAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgIDwvQm94PlxuICAgICAgICAgICkpfVxuICAgICAgICA8L0JveD5cblxuICAgICAgICA8Qm94IG1hcmdpblRvcD17MX0+XG4gICAgICAgICAgPFRleHQgZGltQ29sb3I+XG4gICAgICAgICAgICA8QnlsaW5lPlxuICAgICAgICAgICAgICA8Q29uZmlndXJhYmxlU2hvcnRjdXRIaW50XG4gICAgICAgICAgICAgICAgYWN0aW9uPVwic2VsZWN0OmFjY2VwdFwiXG4gICAgICAgICAgICAgICAgY29udGV4dD1cIlNlbGVjdFwiXG4gICAgICAgICAgICAgICAgZmFsbGJhY2s9XCJFbnRlclwiXG4gICAgICAgICAgICAgICAgZGVzY3JpcHRpb249XCJzZWxlY3RcIlxuICAgICAgICAgICAgICAvPlxuICAgICAgICAgICAgICA8Q29uZmlndXJhYmxlU2hvcnRjdXRIaW50XG4gICAgICAgICAgICAgICAgYWN0aW9uPVwiY29uZmlybTpub1wiXG4gICAgICAgICAgICAgICAgY29udGV4dD1cIkNvbmZpcm1hdGlvblwiXG4gICAgICAgICAgICAgICAgZmFsbGJhY2s9XCJFc2NcIlxuICAgICAgICAgICAgICAgIGRlc2NyaXB0aW9uPVwiYmFja1wiXG4gICAgICAgICAgICAgIC8+XG4gICAgICAgICAgICA8L0J5bGluZT5cbiAgICAgICAgICA8L1RleHQ+XG4gICAgICAgIDwvQm94PlxuICAgICAgPC9Cb3g+XG4gICAgKVxuICB9XG5cbiAgLy8gRW1wdHkgc3RhdGVcbiAgaWYgKGF2YWlsYWJsZVBsdWdpbnMubGVu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FyY2hNb2RlID8gJ3N1Z2dlc3Rpb24nIDogdW5kZWZpbmVkfVxuICAgICAgICAgICAgICA+XG4gICAgICAgICAgICAgICAge2lzU2VsZWN0ZWQgJiYgIWlzU2VhcmNoTW9kZSA/IGZpZ3VyZXMucG9pbnRlciA6ICcgJ317JyAnfVxuICAgICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICAgIDxUZXh0PlxuICAgICAgICAgICAgICAgIHtpc0luc3RhbGxpbmdUaGlzXG4gICAgICAgICAgICAgICAgICA/IGZpZ3VyZXMuZWxsaXBzaXNcbiAgICAgICAgICAgICAgICAgIDogaXNTZWxlY3RlZEZvckluc3RhbGxcbiAgICAgICAgICAgICAgICAgICAgPyBmaWd1cmVzLnJhZGlvT25cbiAgICAgICAgICAgICAgICAgICAgOiBmaWd1cmVzLnJhZGlvT2ZmfXsnICd9XG4gICAgICAgICAgICAgICAge3BsdWdpbi5lbnRyeS5uYW1lfVxuICAgICAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPiDCtyB7cGx1Z2luLm1hcmtldHBsYWNlTmFtZX08L1RleHQ+XG4gICAgICAgICAgICAgICAge3BsdWdpbi5lbnRyeS50YWdzPy5pbmNsdWRlcygnY29tbXVuaXR5LW1hbmFnZWQnKSAmJiAoXG4gICAgICAgICAgICAgICAgICA8VGV4dCBkaW1Db2xvcj4gW0NvbW11bml0eSBNYW5hZ2VkXTwvVGV4dD5cbiAgICAgICAgICAgICAgICApfVxuICAgICAgICAgICAgICAgIHtpbnN0YWxsQ291bnRzICYmXG4gICAgICAgICAgICAgICAgICBwbHVnaW4ubWFya2V0cGxhY2VOYW1lID09PSBPRkZJQ0lBTF9NQVJLRVRQTEFDRV9OQU1FICYmIChcbiAgICAgICAgICAgICAgICAgICAgPFRleHQgZGltQ29sb3I+XG4gICAgICAgICAgICAgICAgICAgICAgeycgwrcgJ31cbiAgICAgICAgICAgICAgICAgICAgICB7Zm9ybWF0SW5zdGFsbENvdW50KFxuICAgICAgICAgICAgICAgICAgICAgICAgaW5zdGFsbENvdW50cy5nZXQocGx1Z2luLnBsdWdpbklkKSA/PyAwLFxuICAgICAgICAgICAgICAgICAgICAgICl9eycgJ31cbiAgICAgICAgICAgICAgICAgICAgICBpbnN0YWxsc1xuICAgICAgICAgICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICAgICAgICApfVxuICAgICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICA8L0JveD5cbiAgICAgICAgICAgIHtwbHVnaW4uZW50cnkuZGVzY3JpcHRpb24gJiYgKFxuICAgICAgICAgICAgICA8Qm94IG1hcmdpbkxlZnQ9ezR9PlxuICAgICAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPlxuICAgICAgICAgICAgICAgICAge3RydW5jYXRlVG9XaWR0aChwbHVnaW4uZW50cnkuZGVzY3JpcHRpb24sIDYwKX1cbiAgICAgICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICAgIDwvQm94PlxuICAgICAgICAgICAgKX1cbiAgICAgICAgICA8L0JveD5cbiAgICAgICAgKVxuICAgICAgfSl9XG5cbiAgICAgIHsvKiBTY3JvbGwgZG93biBpbmRpY2F0b3IgKi99XG4gICAgICB7cGFnaW5hdGlvbi5zY3JvbGxQb3NpdGlvbi5jYW5TY3JvbGxEb3duICYmIChcbiAgICAgICAgPEJveD5cbiAgICAgICAgICA8VGV4dCBkaW1Db2xvcj4ge2ZpZ3VyZXMuYXJyb3dEb3dufSBtb3JlIGJlbG93PC9UZXh0PlxuICAgICAgICA8L0JveD5cbiAgICAgICl9XG5cbiAgICAgIHsvKiBFcnJvciBtZXNzYWdlcyAqL31cbiAgICAgIHtlcnJvciAmJiAoXG4gICAgICAgIDxCb3ggbWFyZ2luVG9wPXsxfT5cbiAgICAgICAgICA8VGV4dCBjb2xvcj1cImVycm9yXCI+XG4gICAgICAgICAgICB7ZmlndXJlcy5jcm9zc30ge2Vycm9yfVxuICAgICAgICAgIDwvVGV4dD5cbiAgICAg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ICAgICAgIDxDb25maWd1cmFibGVTaG9ydGN1dEhpbnRcbiAgICAgICAgICAgICAgYWN0aW9uPVwicGx1Z2luOnRvZ2dsZVwiXG4gICAgICAgICAgICAgIGNvbnRleHQ9XCJQbHVnaW5cIlxuICAgICAgICAgICAgICBmYWxsYmFjaz1cIlNwYWNlXCJcbiAgICAgICAgICAgICAgZGVzY3JpcHRpb249XCJ0b2dnbGVcIlxuICAgICAgICAgICAgLz5cbiAgICAgICAgICApfVxuICAgICAgICAgIDxDb25maWd1cmFibGVTaG9ydGN1dEhpbnRcbiAgICAgICAgICAgIGFjdGlvbj1cInNlbGVjdDphY2NlcHRcIlxuICAgICAgICAgICAgY29udGV4dD1cIlNlbGVjdFwiXG4gICAgICAgICAgICBmYWxsYmFjaz1cIkVudGVyXCJcbiAgICAgICAgICAgIGRlc2NyaXB0aW9uPVwiZGV0YWlsc1wiXG4gICAgICAgICAgLz5cbiAgICAgICAgICA8Q29uZmlndXJhYmxlU2hvcnRjdXRIaW50XG4gICAgICAgICAgICBhY3Rpb249XCJjb25maXJtOm5vXCJcbiAgICAgICAgICAgIGNvbnRleHQ9XCJDb25maXJtYXRpb25cIlxuICAgICAgICAgICAgZmFsbGJhY2s9XCJFc2NcIlxuICAgICAgICAgICAgZGVzY3JpcHRpb249XCJiYWNrXCJcbiAgICAgICAgICAvPlxuICAgICAgICA8L0J5bGluZT5cbiAgICAgIDwvVGV4dD5cbiAgICA8L0JveD5cbiAgKVxufVxuXG4vKipcbiAqIENvbnRleHQtYXdhcmUgZW1wdHkgc3RhdGUgbWVzc2FnZSBmb3IgdGhlIERpc2NvdmVyIHNjcmVlblxuICovXG5mdW5jdGlvbiBFbXB0eVN0YXRlTWVzc2FnZSh7XG4gIHJlYXNvbixcbn06IHtcbiAgcmVhc29uOiBFbXB0eU1hcmtldHBsYWNlUmVhc29uIHwgbnVsbFxufSk6IFJlYWN0LlJlYWN0Tm9kZSB7XG4gIHN3aXRjaCAocmVhc29uKSB7XG4gICAgY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tVQUFBSixDQUFBLE1BQUFJLEVBQUE7UUFBQTtVQUFBQSxFQUFBLEdBQUFKLENBQUE7UUFBQTtRQUFBLE9BTEhJLEVBS0c7TUFBQTtJQUFBLEtBRUYsNEJBQTRCO0lBQUE7TUFBQTtRQUFBLElBQUFBLEVBQUE7UUFBQSxJQUFBSixDQUFBLFFBQUFNLE1BQUEsQ0FBQUMsR0FBQTtVQUc3QkgsRUFBQSxLQUNFLENBQUMsSUFBSSxDQUFDLFFBQVEsQ0FBUixLQUFPLENBQUMsQ0FBQyxxQkFBcUIsRUFBbkMsSUFBSSxDQUNMLENBQUMsSUFBSSxDQUFDLFFBQVEsQ0FBUixLQUFPLENBQUMsQ0FBQyxtREFFZixFQUZDLElBQUksQ0FFRSxHQUNOO1VBQUFKLENBQUEsTUFBQUksRUFBQTtRQUFBO1VBQUFBLEVBQUEsR0FBQUosQ0FBQTtRQUFBO1FBQUEsT0FMSEksRUFLRztNQUFBO0VBRVQ7QUFBQyIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/plugin/unifiedTypes.ts b/_all-in-one-upload/cc-haha/src/commands/plugin/unifiedTypes.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/plugin/unifiedTypes.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/commands/remoteControlServer/index.ts b/_all-in-one-upload/cc-haha/src/commands/remoteControlServer/index.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/remoteControlServer/index.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/commands/rename/generateSessionName.ts b/_all-in-one-upload/cc-haha/src/commands/rename/generateSessionName.ts new file mode 100644 index 0000000..2ebd786 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/rename/generateSessionName.ts @@ -0,0 +1,67 @@ +import { queryHaiku } from '../../services/api/claude.js' +import type { Message } from '../../types/message.js' +import { logForDebugging } from '../../utils/debug.js' +import { errorMessage } from '../../utils/errors.js' +import { safeParseJSON } from '../../utils/json.js' +import { extractTextContent } from '../../utils/messages.js' +import { extractConversationText } from '../../utils/sessionTitle.js' +import { asSystemPrompt } from '../../utils/systemPromptType.js' + +export async function generateSessionName( + messages: Message[], + signal: AbortSignal, +): Promise { + const conversationText = extractConversationText(messages) + if (!conversationText) { + return null + } + + try { + const result = await queryHaiku({ + systemPrompt: asSystemPrompt([ + 'Generate a short kebab-case name (2-4 words) that captures the main topic of this conversation. Use lowercase words separated by hyphens. Examples: "fix-login-bug", "add-auth-feature", "refactor-api-client", "debug-test-failures". Return JSON with a "name" field.', + ]), + userPrompt: conversationText, + outputFormat: { + type: 'json_schema', + schema: { + type: 'object', + properties: { + name: { type: 'string' }, + }, + required: ['name'], + additionalProperties: false, + }, + }, + signal, + options: { + querySource: 'rename_generate_name', + agents: [], + isNonInteractiveSession: false, + hasAppendSystemPrompt: false, + mcpTools: [], + }, + }) + + const content = extractTextContent(result.message.content) + + const response = safeParseJSON(content) + if ( + response && + typeof response === 'object' && + 'name' in response && + typeof (response as { name: unknown }).name === 'string' + ) { + return (response as { name: string }).name + } + return null + } catch (error) { + // Haiku timeout/rate-limit/network are expected operational failures — + // logForDebugging, not logError. Called automatically on every 3rd bridge + // message (initReplBridge.ts), so errors here would flood the error file. + logForDebugging(`generateSessionName failed: ${errorMessage(error)}`, { + level: 'error', + }) + return null + } +} diff --git a/_all-in-one-upload/cc-haha/src/commands/rename/rename.ts b/_all-in-one-upload/cc-haha/src/commands/rename/rename.ts new file mode 100644 index 0000000..014d7eb --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/rename/rename.ts @@ -0,0 +1,87 @@ +import type { UUID } from 'crypto' +import { getSessionId } from '../../bootstrap/state.js' +import { + getBridgeBaseUrlOverride, + getBridgeTokenOverride, +} from '../../bridge/bridgeConfig.js' +import type { ToolUseContext } from '../../Tool.js' +import type { + LocalJSXCommandContext, + LocalJSXCommandOnDone, +} from '../../types/command.js' +import { getMessagesAfterCompactBoundary } from '../../utils/messages.js' +import { + getTranscriptPath, + saveAgentName, + saveCustomTitle, +} from '../../utils/sessionStorage.js' +import { isTeammate } from '../../utils/teammate.js' +import { generateSessionName } from './generateSessionName.js' + +export async function call( + onDone: LocalJSXCommandOnDone, + context: ToolUseContext & LocalJSXCommandContext, + args: string, +): Promise { + // Prevent teammates from renaming - their names are set by team leader + if (isTeammate()) { + onDone( + 'Cannot rename: This session is a swarm teammate. Teammate names are set by the team leader.', + { display: 'system' }, + ) + return null + } + + let newName: string + if (!args || args.trim() === '') { + const generated = await generateSessionName( + getMessagesAfterCompactBoundary(context.messages), + context.abortController.signal, + ) + if (!generated) { + onDone( + 'Could not generate a name: no conversation context yet. Usage: /rename ', + { display: 'system' }, + ) + return null + } + newName = generated + } else { + newName = args.trim() + } + + const sessionId = getSessionId() as UUID + const fullPath = getTranscriptPath() + + // Always save the custom title (session name) + await saveCustomTitle(sessionId, newName, fullPath) + + // Sync title to bridge session on claude.ai/code (best-effort, non-blocking). + // v2 env-less bridge stores cse_* in replBridgeSessionId — + // updateBridgeSessionTitle retags internally for the compat endpoint. + const appState = context.getAppState() + const bridgeSessionId = appState.replBridgeSessionId + if (bridgeSessionId) { + const tokenOverride = getBridgeTokenOverride() + void import('../../bridge/createSession.js').then( + ({ updateBridgeSessionTitle }) => + updateBridgeSessionTitle(bridgeSessionId, newName, { + baseUrl: getBridgeBaseUrlOverride(), + getAccessToken: tokenOverride ? () => tokenOverride : undefined, + }).catch(() => {}), + ) + } + + // Also persist as the session's agent name for prompt-bar display + await saveAgentName(sessionId, newName, fullPath) + context.setAppState(prev => ({ + ...prev, + standaloneAgentContext: { + ...prev.standaloneAgentContext, + name: newName, + }, + })) + + onDone(`Session renamed to: ${newName}`, { display: 'system' }) + return null +} diff --git a/_all-in-one-upload/cc-haha/src/commands/skills/skills.tsx b/_all-in-one-upload/cc-haha/src/commands/skills/skills.tsx new file mode 100644 index 0000000..c9bf0e6 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/skills/skills.tsx @@ -0,0 +1,8 @@ +import * as React from 'react'; +import type { LocalJSXCommandContext } from '../../commands.js'; +import { SkillsMenu } from '../../components/skills/SkillsMenu.js'; +import type { LocalJSXCommandOnDone } from '../../types/command.js'; +export async function call(onDone: LocalJSXCommandOnDone, context: LocalJSXCommandContext): Promise { + return ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkxvY2FsSlNYQ29tbWFuZENvbnRleHQiLCJTa2lsbHNNZW51IiwiTG9jYWxKU1hDb21tYW5kT25Eb25lIiwiY2FsbCIsIm9uRG9uZSIsImNvbnRleHQiLCJQcm9taXNlIiwiUmVhY3ROb2RlIiwib3B0aW9ucyIsImNvbW1hbmRzIl0sInNvdXJjZXMiOlsic2tpbGxzLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgKiBhcyBSZWFjdCBmcm9tICdyZWFjdCdcbmltcG9ydCB0eXBlIHsgTG9jYWxKU1hDb21tYW5kQ29udGV4dCB9IGZyb20gJy4uLy4uL2NvbW1hbmRzLmpzJ1xuaW1wb3J0IHsgU2tpbGxzTWVudSB9IGZyb20gJy4uLy4uL2NvbXBvbmVudHMvc2tpbGxzL1NraWxsc01lbnUuanMnXG5pbXBvcnQgdHlwZSB7IExvY2FsSlNYQ29tbWFuZE9uRG9uZSB9IGZyb20gJy4uLy4uL3R5cGVzL2NvbW1hbmQuanMnXG5cbmV4cG9ydCBhc3luYyBmdW5jdGlvbiBjYWxsKFxuICBvbkRvbmU6IExvY2FsSlNYQ29tbWFuZE9uRG9uZSxcbiAgY29udGV4dDogTG9jYWxKU1hDb21tYW5kQ29udGV4dCxcbik6IFByb21pc2U8UmVhY3QuUmVhY3ROb2RlPiB7XG4gIHJldHVybiA8U2tpbGxzTWVudSBvbkV4aXQ9e29uRG9uZX0gY29tbWFuZHM9e2NvbnRleHQub3B0aW9ucy5jb21tYW5kc30gLz5cbn1cbiJdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLQSxLQUFLLE1BQU0sT0FBTztBQUM5QixjQUFjQyxzQkFBc0IsUUFBUSxtQkFBbUI7QUFDL0QsU0FBU0MsVUFBVSxRQUFRLHVDQUF1QztBQUNsRSxjQUFjQyxxQkFBcUIsUUFBUSx3QkFBd0I7QUFFbkUsT0FBTyxlQUFlQyxJQUFJQSxDQUN4QkMsTUFBTSxFQUFFRixxQkFBcUIsRUFDN0JHLE9BQU8sRUFBRUwsc0JBQXNCLENBQ2hDLEVBQUVNLE9BQU8sQ0FBQ1AsS0FBSyxDQUFDUSxTQUFTLENBQUMsQ0FBQztFQUMxQixPQUFPLENBQUMsVUFBVSxDQUFDLE1BQU0sQ0FBQyxDQUFDSCxNQUFNLENBQUMsQ0FBQyxRQUFRLENBQUMsQ0FBQ0MsT0FBTyxDQUFDRyxPQUFPLENBQUNDLFFBQVEsQ0FBQyxHQUFHO0FBQzNFIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/statusline.tsx b/_all-in-one-upload/cc-haha/src/commands/statusline.tsx new file mode 100644 index 0000000..02c7f0b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/statusline.tsx @@ -0,0 +1,24 @@ +import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'; +import type { Command } from '../commands.js'; +import { AGENT_TOOL_NAME } from '../tools/AgentTool/constants.js'; +const statusline = { + type: 'prompt', + description: "Set up Claude Code's status line UI", + contentLength: 0, + // Dynamic content + aliases: [], + name: 'statusline', + progressMessage: 'setting up statusLine', + allowedTools: [AGENT_TOOL_NAME, 'Read(~/**)', 'Edit(~/.claude/settings.json)'], + source: 'builtin', + disableNonInteractive: true, + async getPromptForCommand(args): Promise { + const prompt = args.trim() || 'Configure my statusLine from my shell PS1 configuration'; + return [{ + type: 'text', + text: `Create an ${AGENT_TOOL_NAME} with subagent_type "statusline-setup" and the prompt "${prompt}"` + }]; + } +} satisfies Command; +export default statusline; +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJDb250ZW50QmxvY2tQYXJhbSIsIkNvbW1hbmQiLCJBR0VOVF9UT09MX05BTUUiLCJzdGF0dXNsaW5lIiwidHlwZSIsImRlc2NyaXB0aW9uIiwiY29udGVudExlbmd0aCIsImFsaWFzZXMiLCJuYW1lIiwicHJvZ3Jlc3NNZXNzYWdlIiwiYWxsb3dlZFRvb2xzIiwic291cmNlIiwiZGlzYWJsZU5vbkludGVyYWN0aXZlIiwiZ2V0UHJvbXB0Rm9yQ29tbWFuZCIsImFyZ3MiLCJQcm9taXNlIiwicHJvbXB0IiwidHJpbSIsInRleHQiXSwic291cmNlcyI6WyJzdGF0dXNsaW5lLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgdHlwZSB7IENvbnRlbnRCbG9ja1BhcmFtIH0gZnJvbSAnQGFudGhyb3BpYy1haS9zZGsvcmVzb3VyY2VzL2luZGV4Lm1qcydcbmltcG9ydCB0eXBlIHsgQ29tbWFuZCB9IGZyb20gJy4uL2NvbW1hbmRzLmpzJ1xuaW1wb3J0IHsgQUdFTlRfVE9PTF9OQU1FIH0gZnJvbSAnLi4vdG9vbHMvQWdlbnRUb29sL2NvbnN0YW50cy5qcydcblxuY29uc3Qgc3RhdHVzbGluZSA9IHtcbiAgdHlwZTogJ3Byb21wdCcsXG4gIGRlc2NyaXB0aW9uOiBcIlNldCB1cCBDbGF1ZGUgQ29kZSdzIHN0YXR1cyBsaW5lIFVJXCIsXG4gIGNvbnRlbnRMZW5ndGg6IDAsIC8vIER5bmFtaWMgY29udGVudFxuICBhbGlhc2VzOiBbXSxcbiAgbmFtZTogJ3N0YXR1c2x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VBQ2xCQyxPQUFPLEVBQUUsRUFBRTtFQUNYQyxJQUFJLEVBQUUsWUFBWTtFQUNsQkMsZUFBZSxFQUFFLHVCQUF1QjtFQUN4Q0MsWUFBWSxFQUFFLENBQ1pSLGVBQWUsRUFDZixZQUFZLEVBQ1osK0JBQStCLENBQ2hDO0VBQ0RTLE1BQU0sRUFBRSxTQUFTO0VBQ2pCQyxxQkFBcUIsRUFBRSxJQUFJO0VBQzNCLE1BQU1DLG1CQUFtQkEsQ0FBQ0MsSUFBSSxDQUFDLEVBQUVDLE9BQU8sQ0FBQ2YsaUJBQWlCLEVBQUUsQ0FBQyxDQUFDO0lBQzVELE1BQU1nQixNQUFNLEdBQ1ZGLElBQUksQ0FBQ0csSUFBSSxDQUFDLENBQUMsSUFBSSx5REFBeUQ7SUFDMUUsT0FBTyxDQUNMO01BQ0ViLElBQUksRUFBRSxNQUFNO01BQ1pjLElBQUksRUFBRSxhQUFhaEIsZUFBZSwwREFBMERjLE1BQU07SUFDcEcsQ0FBQyxDQUNGO0VBQ0g7QUFDRixDQUFDLFdBQVdmLE9BQU87QUFFbkIsZUFBZUUsVUFBVSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/subscribe-pr.ts b/_all-in-one-upload/cc-haha/src/commands/subscribe-pr.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/subscribe-pr.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/commands/tasks/tasks.tsx b/_all-in-one-upload/cc-haha/src/commands/tasks/tasks.tsx new file mode 100644 index 0000000..1215af7 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/tasks/tasks.tsx @@ -0,0 +1,8 @@ +import * as React from 'react'; +import type { LocalJSXCommandContext } from '../../commands.js'; +import { BackgroundTasksDialog } from '../../components/tasks/BackgroundTasksDialog.js'; +import type { LocalJSXCommandOnDone } from '../../types/command.js'; +export async function call(onDone: LocalJSXCommandOnDone, context: LocalJSXCommandContext): Promise { + return ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkxvY2FsSlNYQ29tbWFuZENvbnRleHQiLCJCYWNrZ3JvdW5kVGFza3NEaWFsb2ciLCJMb2NhbEpTWENvbW1hbmRPbkRvbmUiLCJjYWxsIiwib25Eb25lIiwiY29udGV4dCIsIlByb21pc2UiLCJSZWFjdE5vZGUiXSwic291cmNlcyI6WyJ0YXNrcy50c3giXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0ICogYXMgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgdHlwZSB7IExvY2FsSlNYQ29tbWFuZENvbnRleHQgfSBmcm9tICcuLi8uLi9jb21tYW5kcy5qcydcbmltcG9ydCB7IEJhY2tncm91bmRUYXNrc0RpYWxvZyB9IGZyb20gJy4uLy4uL2NvbXBvbmVudHMvdGFza3MvQmFja2dyb3VuZFRhc2tzRGlhbG9nLmpzJ1xuaW1wb3J0IHR5cGUgeyBMb2NhbEpTWENvbW1hbmRPbkRvbmUgfSBmcm9tICcuLi8uLi90eXBlcy9jb21tYW5kLmpzJ1xuXG5leHBvcnQgYXN5bmMgZnVuY3Rpb24gY2FsbChcbiAgb25Eb25lOiBMb2NhbEpTWENvbW1hbmRPbkRvbmUsXG4gIGNvbnRleHQ6IExvY2FsSlNYQ29tbWFuZENvbnRleHQsXG4pOiBQcm9taXNlPFJlYWN0LlJlYWN0Tm9kZT4ge1xuICByZXR1cm4gPEJhY2tncm91bmRUYXNrc0RpYWxvZyB0b29sVXNlQ29udGV4dD17Y29udGV4dH0gb25Eb25lPXtvbkRvbmV9IC8+XG59XG4iXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBS0EsS0FBSyxNQUFNLE9BQU87QUFDOUIsY0FBY0Msc0JBQXNCLFFBQVEsbUJBQW1CO0FBQy9ELFNBQVNDLHFCQUFxQixRQUFRLGlEQUFpRDtBQUN2RixjQUFjQyxxQkFBcUIsUUFBUSx3QkFBd0I7QUFFbkUsT0FBTyxlQUFlQyxJQUFJQSxDQUN4QkMsTUFBTSxFQUFFRixxQkFBcUIsRUFDN0JHLE9BQU8sRUFBRUwsc0JBQXNCLENBQ2hDLEVBQUVNLE9BQU8sQ0FBQ1AsS0FBSyxDQUFDUSxTQUFTLENBQUMsQ0FBQztFQUMxQixPQUFPLENBQUMscUJBQXFCLENBQUMsY0FBYyxDQUFDLENBQUNGLE9BQU8sQ0FBQyxDQUFDLE1BQU0sQ0FBQyxDQUFDRCxNQUFNLENBQUMsR0FBRztBQUMzRSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/terminalSetup/terminalSetup.tsx b/_all-in-one-upload/cc-haha/src/commands/terminalSetup/terminalSetup.tsx new file mode 100644 index 0000000..4e7ef5e --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/terminalSetup/terminalSetup.tsx @@ -0,0 +1,531 @@ +import chalk from 'chalk'; +import { randomBytes } from 'crypto'; +import { copyFile, mkdir, readFile, writeFile } from 'fs/promises'; +import { homedir, platform } from 'os'; +import { dirname, join } from 'path'; +import type { ThemeName } from 'src/utils/theme.js'; +import { pathToFileURL } from 'url'; +import { supportsHyperlinks } from '../../ink/supports-hyperlinks.js'; +import { color } from '../../ink.js'; +import { maybeMarkProjectOnboardingComplete } from '../../projectOnboardingState.js'; +import type { ToolUseContext } from '../../Tool.js'; +import type { LocalJSXCommandContext, LocalJSXCommandOnDone } from '../../types/command.js'; +import { backupTerminalPreferences, checkAndRestoreTerminalBackup, getTerminalPlistPath, markTerminalSetupComplete } from '../../utils/appleTerminalBackup.js'; +import { setupShellCompletion } from '../../utils/completionCache.js'; +import { getGlobalConfig, saveGlobalConfig } from '../../utils/config.js'; +import { env } from '../../utils/env.js'; +import { isFsInaccessible } from '../../utils/errors.js'; +import { execFileNoThrow } from '../../utils/execFileNoThrow.js'; +import { addItemToJSONCArray, safeParseJSONC } from '../../utils/json.js'; +import { logError } from '../../utils/log.js'; +import { getPlatform } from '../../utils/platform.js'; +import { jsonParse, jsonStringify } from '../../utils/slowOperations.js'; +const EOL = '\n'; + +// Terminals that natively support CSI u / Kitty keyboard protocol +const NATIVE_CSIU_TERMINALS: Record = { + ghostty: 'Ghostty', + kitty: 'Kitty', + 'iTerm.app': 'iTerm2', + WezTerm: 'WezTerm', + WarpTerminal: 'Warp' +}; + +/** + * Detect if we're running in a VSCode Remote SSH session. + * In this case, keybindings need to be installed on the LOCAL machine, + * not the remote server where Claude is running. + */ +function isVSCodeRemoteSSH(): boolean { + const askpassMain = process.env.VSCODE_GIT_ASKPASS_MAIN ?? ''; + const path = process.env.PATH ?? ''; + + // Check both env vars - VSCODE_GIT_ASKPASS_MAIN is more reliable when git extension + // is active, and PATH is a fallback. Omit path separator for Windows compatibility. + return askpassMain.includes('.vscode-server') || askpassMain.includes('.cursor-server') || askpassMain.includes('.windsurf-server') || path.includes('.vscode-server') || path.includes('.cursor-server') || path.includes('.windsurf-server'); +} +export function getNativeCSIuTerminalDisplayName(): string | null { + if (!env.terminal || !(env.terminal in NATIVE_CSIU_TERMINALS)) { + return null; + } + return NATIVE_CSIU_TERMINALS[env.terminal] ?? null; +} + +/** + * Format a file path as a clickable hyperlink. + * + * Paths containing spaces (e.g., "Application Support") are not clickable + * in most terminals - they get split at the space. OSC 8 hyperlinks solve + * this by embedding a file:// URL that the terminal can open on click, + * while displaying the clean path to the user. + * + * Unlike createHyperlink(), this doesn't apply any color styling so the + * path inherits the parent's styling (e.g., chalk.dim). + */ +function formatPathLink(filePath: string): string { + if (!supportsHyperlinks()) { + return filePath; + } + const fileUrl = pathToFileURL(filePath).href; + // OSC 8 hyperlink: \e]8;;URL\a TEXT \e]8;;\a + return `\x1b]8;;${fileUrl}\x07${filePath}\x1b]8;;\x07`; +} +export function shouldOfferTerminalSetup(): boolean { + // iTerm2, WezTerm, Ghostty, Kitty, and Warp natively support CSI u / Kitty + // keyboard protocol, which Claude Code already parses. No setup needed for + // these terminals. + return platform() === 'darwin' && env.terminal === 'Apple_Terminal' || env.terminal === 'vscode' || env.terminal === 'cursor' || env.terminal === 'windsurf' || env.terminal === 'alacritty' || env.terminal === 'zed'; +} +export async function setupTerminal(theme: ThemeName): Promise { + let result = ''; + switch (env.terminal) { + case 'Apple_Terminal': + result = await enableOptionAsMetaForTerminal(theme); + break; + case 'vscode': + result = await installBindingsForVSCodeTerminal('VSCode', theme); + break; + case 'cursor': + result = await installBindingsForVSCodeTerminal('Cursor', theme); + break; + case 'windsurf': + result = await installBindingsForVSCodeTerminal('Windsurf', theme); + break; + case 'alacritty': + result = await installBindingsForAlacritty(theme); + break; + case 'zed': + result = await installBindingsForZed(theme); + break; + case null: + break; + } + saveGlobalConfig(current => { + if (['vscode', 'cursor', 'windsurf', 'alacritty', 'zed'].includes(env.terminal ?? '')) { + if (current.shiftEnterKeyBindingInstalled === true) return current; + return { + ...current, + shiftEnterKeyBindingInstalled: true + }; + } else if (env.terminal === 'Apple_Terminal') { + if (current.optionAsMetaKeyInstalled === true) return current; + return { + ...current, + optionAsMetaKeyInstalled: true + }; + } + return current; + }); + maybeMarkProjectOnboardingComplete(); + + // Install shell completions (ant-only, since the completion command is ant-only) + if ("external" === 'ant') { + result += await setupShellCompletion(theme); + } + return result; +} +export function isShiftEnterKeyBindingInstalled(): boolean { + return getGlobalConfig().shiftEnterKeyBindingInstalled === true; +} +export function hasUsedBackslashReturn(): boolean { + return getGlobalConfig().hasUsedBackslashReturn === true; +} +export function markBackslashReturnUsed(): void { + const config = getGlobalConfig(); + if (!config.hasUsedBackslashReturn) { + saveGlobalConfig(current => ({ + ...current, + hasUsedBackslashReturn: true + })); + } +} +export async function call(onDone: LocalJSXCommandOnDone, context: ToolUseContext & LocalJSXCommandContext, _args: string): Promise { + if (env.terminal && env.terminal in NATIVE_CSIU_TERMINALS) { + const message = `Shift+Enter is natively supported in ${NATIVE_CSIU_TERMINALS[env.terminal]}. + +No configuration needed. Just use Shift+Enter to add newlines.`; + onDone(message); + return null; + } + + // Check if terminal is supported + if (!shouldOfferTerminalSetup()) { + const terminalName = env.terminal || 'your current terminal'; + const currentPlatform = getPlatform(); + + // Build platform-specific terminal suggestions + let platformTerminals = ''; + if (currentPlatform === 'macos') { + platformTerminals = ' • macOS: Apple Terminal\n'; + } else if (currentPlatform === 'windows') { + platformTerminals = ' • Windows: Windows Terminal\n'; + } + // For Linux and other platforms, we don't show native terminal options + // since they're not currently supported + + const message = `Terminal setup cannot be run from ${terminalName}. + +This command configures a convenient Shift+Enter shortcut for multi-line prompts. +${chalk.dim('Note: You can already use backslash (\\\\) + return to add newlines.')} + +To set up the shortcut (optional): +1. Exit tmux/screen temporarily +2. Run /terminal-setup directly in one of these terminals: +${platformTerminals} • IDE: VSCode, Cursor, Windsurf, Zed + • Other: Alacritty +3. Return to tmux/screen - settings will persist + +${chalk.dim('Note: iTerm2, WezTerm, Ghostty, Kitty, and Warp support Shift+Enter natively.')}`; + onDone(message); + return null; + } + const result = await setupTerminal(context.options.theme); + onDone(result); + return null; +} +type VSCodeKeybinding = { + key: string; + command: string; + args: { + text: string; + }; + when: string; +}; +async function installBindingsForVSCodeTerminal(editor: 'VSCode' | 'Cursor' | 'Windsurf' = 'VSCode', theme: ThemeName): Promise { + // Check if we're running in a VSCode Remote SSH session + // In this case, keybindings need to be installed on the LOCAL machine + if (isVSCodeRemoteSSH()) { + return `${color('warning', theme)(`Cannot install keybindings from a remote ${editor} session.`)}${EOL}${EOL}${editor} keybindings must be installed on your local machine, not the remote server.${EOL}${EOL}To install the Shift+Enter keybinding:${EOL}1. Open ${editor} on your local machine (not connected to remote)${EOL}2. Open the Command Palette (Cmd/Ctrl+Shift+P) → "Preferences: Open Keyboard Shortcuts (JSON)"${EOL}3. Add this keybinding (the file must be a JSON array):${EOL}${EOL}${chalk.dim(`[ + { + "key": "shift+enter", + "command": "workbench.action.terminal.sendSequence", + "args": { "text": "\\u001b\\r" }, + "when": "terminalFocus" + } +]`)}${EOL}`; + } + const editorDir = editor === 'VSCode' ? 'Code' : editor; + const userDirPath = join(homedir(), platform() === 'win32' ? join('AppData', 'Roaming', editorDir, 'User') : platform() === 'darwin' ? join('Library', 'Application Support', editorDir, 'User') : join('.config', editorDir, 'User')); + const keybindingsPath = join(userDirPath, 'keybindings.json'); + try { + // Ensure user directory exists (idempotent with recursive) + await mkdir(userDirPath, { + recursive: true + }); + + // Read existing keybindings file, or default to empty array if it doesn't exist + let content = '[]'; + let keybindings: VSCodeKeybinding[] = []; + let fileExists = false; + try { + content = await readFile(keybindingsPath, { + encoding: 'utf-8' + }); + fileExists = true; + keybindings = safeParseJSONC(content) as VSCodeKeybinding[] ?? []; + } catch (e: unknown) { + if (!isFsInaccessible(e)) throw e; + } + + // Backup the existing file before modifying it + if (fileExists) { + const randomSha = randomBytes(4).toString('hex'); + const backupPath = `${keybindingsPath}.${randomSha}.bak`; + try { + await copyFile(keybindingsPath, backupPath); + } catch { + return `${color('warning', theme)(`Error backing up existing ${editor} terminal keybindings. Bailing out.`)}${EOL}${chalk.dim(`See ${formatPathLink(keybindingsPath)}`)}${EOL}${chalk.dim(`Backup path: ${formatPathLink(backupPath)}`)}${EOL}`; + } + } + + // Check if keybinding already exists + const existingBinding = keybindings.find(binding => binding.key === 'shift+enter' && binding.command === 'workbench.action.terminal.sendSequence' && binding.when === 'terminalFocus'); + if (existingBinding) { + return `${color('warning', theme)(`Found existing ${editor} terminal Shift+Enter key binding. Remove it to continue.`)}${EOL}${chalk.dim(`See ${formatPathLink(keybindingsPath)}`)}${EOL}`; + } + + // Create the new keybinding + const newKeybinding: VSCodeKeybinding = { + key: 'shift+enter', + command: 'workbench.action.terminal.sendSequence', + args: { + text: '\u001b\r' + }, + when: 'terminalFocus' + }; + + // Modify the content by adding the new keybinding while preserving comments and formatting + const updatedContent = addItemToJSONCArray(content, newKeybinding); + + // Write the updated content back to the file + await writeFile(keybindingsPath, updatedContent, { + encoding: 'utf-8' + }); + return `${color('success', theme)(`Installed ${editor} terminal Shift+Enter key binding`)}${EOL}${chalk.dim(`See ${formatPathLink(keybindingsPath)}`)}${EOL}`; + } catch (error) { + logError(error); + throw new Error(`Failed to install ${editor} terminal Shift+Enter key binding`); + } +} +async function enableOptionAsMetaForProfile(profileName: string): Promise { + // First try to add the property (in case it doesn't exist) + // Quote the profile name to handle names with spaces (e.g., "Man Page", "Red Sands") + const { + code: addCode + } = await execFileNoThrow('/usr/libexec/PlistBuddy', ['-c', `Add :'Window Settings':'${profileName}':useOptionAsMetaKey bool true`, getTerminalPlistPath()]); + + // If adding fails (likely because it already exists), try setting it instead + if (addCode !== 0) { + const { + code: setCode + } = await execFileNoThrow('/usr/libexec/PlistBuddy', ['-c', `Set :'Window Settings':'${profileName}':useOptionAsMetaKey true`, getTerminalPlistPath()]); + if (setCode !== 0) { + logError(new Error(`Failed to enable Option as Meta key for Terminal.app profile: ${profileName}`)); + return false; + } + } + return true; +} +async function disableAudioBellForProfile(profileName: string): Promise { + // First try to add the property (in case it doesn't exist) + // Quote the profile name to handle names with spaces (e.g., "Man Page", "Red Sands") + const { + code: addCode + } = await execFileNoThrow('/usr/libexec/PlistBuddy', ['-c', `Add :'Window Settings':'${profileName}':Bell bool false`, getTerminalPlistPath()]); + + // If adding fails (likely because it already exists), try setting it instead + if (addCode !== 0) { + const { + code: setCode + } = await execFileNoThrow('/usr/libexec/PlistBuddy', ['-c', `Set :'Window Settings':'${profileName}':Bell false`, getTerminalPlistPath()]); + if (setCode !== 0) { + logError(new Error(`Failed to disable audio bell for Terminal.app profile: ${profileName}`)); + return false; + } + } + return true; +} + +// Enable Option as Meta key for Terminal.app +async function enableOptionAsMetaForTerminal(theme: ThemeName): Promise { + try { + // Create a backup of the current plist file + const backupPath = await backupTerminalPreferences(); + if (!backupPath) { + throw new Error('Failed to create backup of Terminal.app preferences, bailing out'); + } + + // Read the current default profile from the plist + const { + stdout: defaultProfile, + code: readCode + } = await execFileNoThrow('defaults', ['read', 'com.apple.Terminal', 'Default Window Settings']); + if (readCode !== 0 || !defaultProfile.trim()) { + throw new Error('Failed to read default Terminal.app profile'); + } + const { + stdout: startupProfile, + code: startupCode + } = await execFileNoThrow('defaults', ['read', 'com.apple.Terminal', 'Startup Window Settings']); + if (startupCode !== 0 || !startupProfile.trim()) { + throw new Error('Failed to read startup Terminal.app profile'); + } + let wasAnyProfileUpdated = false; + const defaultProfileName = defaultProfile.trim(); + const optionAsMetaEnabled = await enableOptionAsMetaForProfile(defaultProfileName); + const audioBellDisabled = await disableAudioBellForProfile(defaultProfileName); + if (optionAsMetaEnabled || audioBellDisabled) { + wasAnyProfileUpdated = true; + } + const startupProfileName = startupProfile.trim(); + + // Only proceed if the startup profile is different from the default profile + if (startupProfileName !== defaultProfileName) { + const startupOptionAsMetaEnabled = await enableOptionAsMetaForProfile(startupProfileName); + const startupAudioBellDisabled = await disableAudioBellForProfile(startupProfileName); + if (startupOptionAsMetaEnabled || startupAudioBellDisabled) { + wasAnyProfileUpdated = true; + } + } + if (!wasAnyProfileUpdated) { + throw new Error('Failed to enable Option as Meta key or disable audio bell for any Terminal.app profile'); + } + + // Flush the preferences cache + await execFileNoThrow('killall', ['cfprefsd']); + markTerminalSetupComplete(); + return `${color('success', theme)(`Configured Terminal.app settings:`)}${EOL}${color('success', theme)('- Enabled "Use Option as Meta key"')}${EOL}${color('success', theme)('- Switched to visual bell')}${EOL}${chalk.dim('Option+Enter will now enter a newline.')}${EOL}${chalk.dim('You must restart Terminal.app for changes to take effect.', theme)}${EOL}`; + } catch (error) { + logError(error); + + // Attempt to restore from backup + const restoreResult = await checkAndRestoreTerminalBackup(); + const errorMessage = 'Failed to enable Option as Meta key for Terminal.app.'; + if (restoreResult.status === 'restored') { + throw new Error(`${errorMessage} Your settings have been restored from backup.`); + } else if (restoreResult.status === 'failed') { + throw new Error(`${errorMessage} Restoring from backup failed, try manually with: defaults import com.apple.Terminal ${restoreResult.backupPath}`); + } else { + throw new Error(`${errorMessage} No backup was available to restore from.`); + } + } +} +async function installBindingsForAlacritty(theme: ThemeName): Promise { + const ALACRITTY_KEYBINDING = `[[keyboard.bindings]] +key = "Return" +mods = "Shift" +chars = "\\u001B\\r"`; + + // Get Alacritty config file paths in order of preference + const configPaths: string[] = []; + + // XDG config path (Linux and macOS) + const xdgConfigHome = process.env.XDG_CONFIG_HOME; + if (xdgConfigHome) { + configPaths.push(join(xdgConfigHome, 'alacritty', 'alacritty.toml')); + } else { + configPaths.push(join(homedir(), '.config', 'alacritty', 'alacritty.toml')); + } + + // Windows-specific path + if (platform() === 'win32') { + const appData = process.env.APPDATA; + if (appData) { + configPaths.push(join(appData, 'alacritty', 'alacritty.toml')); + } + } + + // Find existing config file by attempting to read it, or use first preferred path + let configPath: string | null = null; + let configContent = ''; + let configExists = false; + for (const path of configPaths) { + try { + configContent = await readFile(path, { + encoding: 'utf-8' + }); + configPath = path; + configExists = true; + break; + } catch (e: unknown) { + if (!isFsInaccessible(e)) throw e; + // File missing or inaccessible — try next config path + } + } + + // If no config exists, use the first path (XDG/default location) + if (!configPath) { + configPath = configPaths[0] ?? null; + } + if (!configPath) { + throw new Error('No valid config path found for Alacritty'); + } + try { + if (configExists) { + // Check if keybinding already exists (look for Shift+Return binding) + if (configContent.includes('mods = "Shift"') && configContent.includes('key = "Return"')) { + return `${color('warning', theme)('Found existing Alacritty Shift+Enter key binding. Remove it to continue.')}${EOL}${chalk.dim(`See ${formatPathLink(configPath)}`)}${EOL}`; + } + + // Create backup + const randomSha = randomBytes(4).toString('hex'); + const backupPath = `${configPath}.${randomSha}.bak`; + try { + await copyFile(configPath, backupPath); + } catch { + return `${color('warning', theme)('Error backing up existing Alacritty config. Bailing out.')}${EOL}${chalk.dim(`See ${formatPathLink(configPath)}`)}${EOL}${chalk.dim(`Backup path: ${formatPathLink(backupPath)}`)}${EOL}`; + } + } else { + // Ensure config directory exists (idempotent with recursive) + await mkdir(dirname(configPath), { + recursive: true + }); + } + + // Add the keybinding to the config + let updatedContent = configContent; + if (configContent && !configContent.endsWith('\n')) { + updatedContent += '\n'; + } + updatedContent += '\n' + ALACRITTY_KEYBINDING + '\n'; + + // Write the updated config + await writeFile(configPath, updatedContent, { + encoding: 'utf-8' + }); + return `${color('success', theme)('Installed Alacritty Shift+Enter key binding')}${EOL}${color('success', theme)('You may need to restart Alacritty for changes to take effect')}${EOL}${chalk.dim(`See ${formatPathLink(configPath)}`)}${EOL}`; + } catch (error) { + logError(error); + throw new Error('Failed to install Alacritty Shift+Enter key binding'); + } +} +async function installBindingsForZed(theme: ThemeName): Promise { + // Zed uses JSON keybindings similar to VSCode + const zedDir = join(homedir(), '.config', 'zed'); + const keymapPath = join(zedDir, 'keymap.json'); + try { + // Ensure zed directory exists (idempotent with recursive) + await mkdir(zedDir, { + recursive: true + }); + + // Read existing keymap file, or default to empty array if it doesn't exist + let keymapContent = '[]'; + let fileExists = false; + try { + keymapContent = await readFile(keymapPath, { + encoding: 'utf-8' + }); + fileExists = true; + } catch (e: unknown) { + if (!isFsInaccessible(e)) throw e; + } + if (fileExists) { + // Check if keybinding already exists + if (keymapContent.includes('shift-enter')) { + return `${color('warning', theme)('Found existing Zed Shift+Enter key binding. Remove it to continue.')}${EOL}${chalk.dim(`See ${formatPathLink(keymapPath)}`)}${EOL}`; + } + + // Create backup + const randomSha = randomBytes(4).toString('hex'); + const backupPath = `${keymapPath}.${randomSha}.bak`; + try { + await copyFile(keymapPath, backupPath); + } catch { + return `${color('warning', theme)('Error backing up existing Zed keymap. Bailing out.')}${EOL}${chalk.dim(`See ${formatPathLink(keymapPath)}`)}${EOL}${chalk.dim(`Backup path: ${formatPathLink(backupPath)}`)}${EOL}`; + } + } + + // Parse and modify the keymap + let keymap: Array<{ + context?: string; + bindings: Record; + }>; + try { + keymap = jsonParse(keymapContent); + if (!Array.isArray(keymap)) { + keymap = []; + } + } catch { + keymap = []; + } + + // Add the new keybinding for terminal context + keymap.push({ + context: 'Terminal', + bindings: { + 'shift-enter': ['terminal::SendText', '\u001b\r'] + } + }); + + // Write the updated keymap + await writeFile(keymapPath, jsonStringify(keymap, null, 2) + '\n', { + encoding: 'utf-8' + }); + return `${color('success', theme)('Installed Zed Shift+Enter key binding')}${EOL}${chalk.dim(`See ${formatPathLink(keymapPath)}`)}${EOL}`; + } catch (error) { + logError(error); + throw new Error('Failed to install Zed Shift+Enter key binding'); + } +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJjaGFsayIsInJhbmRvbUJ5dGVzIiwiY29weUZpbGUiLCJta2RpciIsInJlYWRGaWxlIiwid3JpdGVGaWxlIiwiaG9tZWRpciIsInBsYXRmb3JtIiwiZGlybmFtZSIsImpvaW4iLCJUaGVtZU5hbWUiLCJwYXRoVG9GaWxlVVJMIiwic3VwcG9ydHNIeXBlcmxpbmtzIiwiY29sb3IiLCJtYXliZU1hcmtQcm9qZWN0T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rade.tsx new file mode 100644 index 0000000..1daf73d --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/upgrade/upgrade.tsx @@ -0,0 +1,38 @@ +import * as React from 'react'; +import type { LocalJSXCommandContext } from '../../commands.js'; +import { getOauthProfileFromOauthToken } from '../../services/oauth/getOauthProfile.js'; +import type { LocalJSXCommandOnDone } from '../../types/command.js'; +import { getClaudeAIOAuthTokens, isClaudeAISubscriber } from '../../utils/auth.js'; +import { openBrowser } from '../../utils/browser.js'; +import { logError } from '../../utils/log.js'; +import { Login } from '../login/login.js'; +export async function call(onDone: LocalJSXCommandOnDone, context: LocalJSXCommandContext): Promise { + try { + // Check if user is already on the highest Max plan (20x) + if (isClaudeAISubscriber()) { + const tokens = getClaudeAIOAuthTokens(); + let isMax20x = false; + if (tokens?.subscriptionType && tokens?.rateLimitTier) { + isMax20x = tokens.subscriptionType === 'max' && tokens.rateLimitTier === 'default_claude_max_20x'; + } else if (tokens?.accessToken) { + const profile = await getOauthProfileFromOauthToken(tokens.accessToken); + isMax20x = profile?.organization?.organization_type === 'claude_max' && profile?.organization?.rate_limit_tier === 'default_claude_max_20x'; + } + if (isMax20x) { + setTimeout(onDone, 0, 'You are already on the highest Max subscription plan. For additional usage, run /login to switch to an API usage-billed account.'); + return null; + } + } + const url = 'https://claude.ai/upgrade/max'; + await openBrowser(url); + return { + context.onChangeAPIKey(); + onDone(success ? 'Login successful' : 'Login interrupted'); + }} />; + } catch (error) { + logError(error as Error); + setTimeout(onDone, 0, 'Failed to open browser. Please visit https://claude.ai/upgrade/max to upgrade.'); + } + return null; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkxvY2FsSlNYQ29tbWFuZENvbnRleHQiLCJnZXRPYXV0aFByb2ZpbGVGcm9tT2F1dGhUb2tlbiIsIkxvY2FsSlNYQ29tbWFuZE9uRG9uZSIsImdldENsYXVkZUFJT0F1dGhUb2tlbnMiLCJ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VcsRUFBRTtRQUM5QixNQUFNQyxPQUFPLEdBQUcsTUFBTWpCLDZCQUE2QixDQUFDWSxNQUFNLENBQUNJLFdBQVcsQ0FBQztRQUN2RUgsUUFBUSxHQUNOSSxPQUFPLEVBQUVDLFlBQVksRUFBRUMsaUJBQWlCLEtBQUssWUFBWSxJQUN6REYsT0FBTyxFQUFFQyxZQUFZLEVBQUVFLGVBQWUsS0FBSyx3QkFBd0I7TUFDdkU7TUFFQSxJQUFJUCxRQUFRLEVBQUU7UUFDWlEsVUFBVSxDQUNSYixNQUFNLEVBQ04sQ0FBQyxFQUNELGtJQUNGLENBQUM7UUFDRCxPQUFPLElBQUk7TUFDYjtJQUNGO0lBRUEsTUFBTWMsR0FBRyxHQUFHLCtCQUErQjtJQUMzQyxNQUFNbEIsV0FBVyxDQUFDa0IsR0FBRyxDQUFDO0lBRXRCLE9BQ0UsQ0FBQyxLQUFLLENBQ0osZUFBZSxDQUFDLENBQ2Qsa0ZBQ0YsQ0FBQyxDQUNELE1BQU0sQ0FBQyxDQUFDQyxPQUFPLElBQUk7TUFDakJkLE9BQU8sQ0FBQ2UsY0FBYyxDQUFDLENBQUM7TUFDeEJoQixNQUFNLENBQUNlLE9BQU8sR0FBRyxrQkFBa0IsR0FBRyxtQkFBbUIsQ0FBQztJQUM1RCxDQUFDLENBQUMsR0FDRjtFQUVOLENBQUMsQ0FBQyxPQUFPRSxLQUFLLEVBQUU7SUFDZHBCLFFBQVEsQ0FBQ29CLEtBQUssSUFBSUMsS0FBSyxDQUFDO0lBQ3hCTCxVQUFVLENBQ1JiLE1BQU0sRUFDTixDQUFDLEVBQ0QsZ0ZBQ0YsQ0FBQztFQUNIO0VBQ0EsT0FBTyxJQUFJO0FBQ2IiLCJpZ25vcmVMaXN0IjpbXX0= \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/commands/voice/index.ts b/_all-in-one-upload/cc-haha/src/commands/voice/index.ts new file mode 100644 index 0000000..61540d3 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/commands/voice/index.ts @@ -0,0 +1,20 @@ +import type { Command } from '../../commands.js' +import { + isVoiceGrowthBookEnabled, + isVoiceModeEnabled, +} from '../../voice/voiceModeEnabled.js' + +const voice = { + type: 'local', + name: 'voice', + description: 'Toggle voice mode', + availability: ['claude-ai'], + isEnabled: () => isVoiceGrowthBookEnabled(), + get isHidden() { + return !isVoiceModeEnabled() + }, + supportsNonInteractive: false, + load: () => import('./voice.js'), +} satisfies Command + +export default voice diff --git a/_all-in-one-upload/cc-haha/src/components/CustomSelect/select-input-option.tsx b/_all-in-one-upload/cc-haha/src/components/CustomSelect/select-input-option.tsx new file mode 100644 index 0000000..51e60ce --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/CustomSelect/select-input-option.tsx @@ -0,0 +1,488 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { type ReactNode, useEffect, useRef, useState } from 'react'; +// eslint-disable-next-line custom-rules/prefer-use-keybindings -- UP arrow exit not in Attachments bindings +import { Box, Text, useInput } from '../../ink.js'; +import { useKeybinding, useKeybindings } from '../../keybindings/useKeybinding.js'; +import type { PastedContent } from '../../utils/config.js'; +import { getImageFromClipboard } from '../../utils/imagePaste.js'; +import type { ImageDimensions } from '../../utils/imageResizer.js'; +import { ClickableImageRef } from '../ClickableImageRef.js'; +import { ConfigurableShortcutHint } from '../ConfigurableShortcutHint.js'; +import { Byline } from '../design-system/Byline.js'; +import TextInput from '../TextInput.js'; +import type { OptionWithDescription } from './select.js'; +import { SelectOption } from './select-option.js'; +type Props = { + option: Extract, { + type: 'input'; + }>; + isFocused: boolean; + isSelected: boolean; + shouldShowDownArrow: boolean; + shouldShowUpArrow: boolean; + maxIndexWidth: number; + index: number; + inputValue: string; + onInputChange: (value: string) => void; + onSubmit: (value: string) => void; + onExit?: () => void; + layout: 'compact' | 'expanded'; + children?: ReactNode; + /** + * When true, shows the label before the input field. + * When false (default), uses the label as the placeholder. + */ + showLabel?: boolean; + /** + * Callback to open external editor for editing the input value. + * When provided, ctrl+g will trigger this callback with the current value + * and a setter function to update the internal state. + */ + onOpenEditor?: (currentValue: string, setValue: (value: string) => void) => void; + /** + * When true, automatically reset cursor to end of line when: + * - Option becomes focused + * - Input value changes + * This prevents cursor position bugs when the input value updates asynchronously. + */ + resetCursorOnUpdate?: boolean; + /** + * Optional callback when an image is pasted into the input. + */ + onImagePaste?: (base64Image: string, mediaType?: string, filename?: string, dimensions?: ImageDimensions, sourcePath?: string) => void; + /** + * Pasted content to display inline above the input when focused. + */ + pastedContents?: Record; + /** + * Callback to remove a pasted image by its ID. + */ + onRemoveImage?: (id: number) => void; + /** + * Whether image selection mode is active. + */ + imagesSelected?: boolean; + /** + * Currently selected image index within the image attachments array. + */ + selectedImageIndex?: number; + /** + * Callback to set image selection mode on/off. + */ + onImagesSelectedChange?: (selected: boolean) => void; + /** + * Callback to change the selected image index. + */ + onSelectedImageIndexChange?: (index: number) => void; +}; +export function SelectInputOption(t0) { + const $ = _c(100); + const { + option, + isFocused, + isSelected, + shouldShowDownArrow, + shouldShowUpArrow, + maxIndexWidth, + index, + inputValue, + onInputChange, + onSubmit, + onExit, + layout, + children, + showLabel: t1, + onOpenEditor, + resetCursorOnUpdate: t2, + onImagePaste, + pastedContents, + onRemoveImage, + imagesSelected, + selectedImageIndex: t3, + onImagesSelectedChange, + onSelectedImageIndexChange + } = t0; + const showLabelProp = t1 === undefined ? false : t1; + const resetCursorOnUpdate = t2 === undefined ? false : t2; + const selectedImageIndex = t3 === undefined ? 0 : t3; + let t4; + if ($[0] !== pastedContents) { + t4 = pastedContents ? Object.values(pastedContents).filter(_temp) : []; + $[0] = pastedContents; + $[1] = t4; + } else { + t4 = $[1]; + } + const imageAttachments = t4; + const showLabel = showLabelProp || option.showLabelWithValue === true; + const [cursorOffset, setCursorOffset] = useState(inputValue.length); + const isUserEditing = useRef(false); + let t5; + if ($[2] !== inputValue.length || $[3] !== isFocused || $[4] !== resetCursorOnUpdate) { + t5 = () => { + if (resetCursorOnUpdate && isFocused) { + if (isUserEditing.current) { + isUserEditing.current = false; + } else { + setCursorOffset(inputValue.length); + } + } + }; + $[2] = inputValue.length; + $[3] = isFocused; + $[4] = resetCursorOnUpdate; + $[5] = t5; + } else { + t5 = $[5]; + } + let t6; + if ($[6] !== inputValue || $[7] !== isFocused || $[8] !== resetCursorOnUpdate) { + t6 = [resetCursorOnUpdate, isFocused, inputValue]; + $[6] = inputValue; + $[7] = isFocused; + $[8] = resetCursorOnUpdate; + $[9] = t6; + } else { + t6 = $[9]; + } + useEffect(t5, t6); + let t7; + if ($[10] !== inputValue || $[11] !== onInputChange || $[12] !== onOpenEditor) { + t7 = () => { + onOpenEditor?.(inputValue, onInputChange); + }; + $[10] = inputValue; + $[11] = onInputChange; + $[12] = onOpenEditor; + $[13] = t7; + } else { + t7 = $[13]; + } + const t8 = isFocused && !!onOpenEditor; + let t9; + if ($[14] !== t8) { + t9 = { + context: "Chat", + isActive: t8 + }; + $[14] = t8; + $[15] = t9; + } else { + t9 = $[15]; + } + useKeybinding("chat:externalEditor", t7, t9); + let t10; + if ($[16] !== onImagePaste) { + t10 = () => { + if (!onImagePaste) { + return; + } + getImageFromClipboard().then(imageData => { + if (imageData) { + onImagePaste(imageData.base64, imageData.mediaType, undefined, imageData.dimensions); + } + }); + }; + $[16] = onImagePaste; + $[17] = t10; + } else { + t10 = $[17]; + } + const t11 = isFocused && !!onImagePaste; + let t12; + if ($[18] !== t11) { + t12 = { + context: "Chat", + isActive: t11 + }; + $[18] = t11; + $[19] = t12; + } else { + t12 = $[19]; + } + useKeybinding("chat:imagePaste", t10, t12); + let t13; + if ($[20] !== imageAttachments || $[21] !== onRemoveImage) { + t13 = () => { + if (imageAttachments.length > 0 && onRemoveImage) { + onRemoveImage(imageAttachments.at(-1).id); + } + }; + $[20] = imageAttachments; + $[21] = onRemoveImage; + $[22] = t13; + } else { + t13 = $[22]; + } + const t14 = isFocused && !imagesSelected && inputValue === "" && imageAttachments.length > 0 && !!onRemoveImage; + let t15; + if ($[23] !== t14) { + t15 = { + context: "Attachments", + isActive: t14 + }; + $[23] = t14; + $[24] = t15; + } else { + t15 = $[24]; + } + useKeybinding("attachments:remove", t13, t15); + let t16; + let t17; + if ($[25] !== imageAttachments.length || $[26] !== onSelectedImageIndexChange || $[27] !== selectedImageIndex) { + t16 = () => { + if (imageAttachments.length > 1) { + onSelectedImageIndexChange?.((selectedImageIndex + 1) % imageAttachments.length); + } + }; + t17 = () => { + if (imageAttachments.length > 1) { + onSelectedImageIndexChange?.((selectedImageIndex - 1 + imageAttachments.length) % imageAttachments.length); + } + }; + $[25] = imageAttachments.length; + $[26] = onSelectedImageIndexChange; + $[27] = selectedImageIndex; + $[28] = t16; + $[29] = t17; + } else { + t16 = $[28]; + t17 = $[29]; + } + let t18; + if ($[30] !== imageAttachments || $[31] !== onImagesSelectedChange || $[32] !== onRemoveImage || $[33] !== onSelectedImageIndexChange || $[34] !== selectedImageIndex) { + t18 = () => { + const img = imageAttachments[selectedImageIndex]; + if (img && onRemoveImage) { + onRemoveImage(img.id); + if (imageAttachments.length <= 1) { + onImagesSelectedChange?.(false); + } else { + onSelectedImageIndexChange?.(Math.min(selectedImageIndex, imageAttachments.length - 2)); + } + } + }; + $[30] = imageAttachments; + $[31] = onImagesSelectedChange; + $[32] = onRemoveImage; + $[33] = onSelectedImageIndexChange; + $[34] = selectedImageIndex; + $[35] = t18; + } else { + t18 = $[35]; + } + let t19; + if ($[36] !== onImagesSelectedChange) { + t19 = () => { + onImagesSelectedChange?.(false); + }; + $[36] = onImagesSelectedChange; + $[37] = t19; + } else { + t19 = $[37]; + } + let t20; + if ($[38] !== t16 || $[39] !== t17 || $[40] !== t18 || $[41] !== t19) { + t20 = { + "attachments:next": t16, + "attachments:previous": t17, + "attachments:remove": t18, + "attachments:exit": t19 + }; + $[38] = t16; + $[39] = t17; + $[40] = t18; + $[41] = t19; + $[42] = t20; + } else { + t20 = $[42]; + } + const t21 = isFocused && !!imagesSelected; + let t22; + if ($[43] !== t21) { + t22 = { + context: "Attachments", + isActive: t21 + }; + $[43] = t21; + $[44] = t22; + } else { + t22 = $[44]; + } + useKeybindings(t20, t22); + let t23; + if ($[45] !== onImagesSelectedChange) { + t23 = (_input, key) => { + if (key.upArrow) { + onImagesSelectedChange?.(false); + } + }; + $[45] = onImagesSelectedChange; + $[46] = t23; + } else { + t23 = $[46]; + } + const t24 = isFocused && !!imagesSelected; + let t25; + if ($[47] !== t24) { + t25 = { + isActive: t24 + }; + $[47] = t24; + $[48] = t25; + } else { + t25 = $[48]; + } + useInput(t23, t25); + let t26; + let t27; + if ($[49] !== imagesSelected || $[50] !== isFocused || $[51] !== onImagesSelectedChange) { + t26 = () => { + if (!isFocused && imagesSelected) { + onImagesSelectedChange?.(false); + } + }; + t27 = [isFocused, imagesSelected, onImagesSelectedChange]; + $[49] = imagesSelected; + $[50] = isFocused; + $[51] = onImagesSelectedChange; + $[52] = t26; + $[53] = t27; + } else { + t26 = $[52]; + t27 = $[53]; + } + useEffect(t26, t27); + const descriptionPaddingLeft = layout === "expanded" ? maxIndexWidth + 3 : maxIndexWidth + 4; + const t28 = layout === "compact" ? 0 : undefined; + const t29 = `${index}.`; + let t30; + if ($[54] !== maxIndexWidth || $[55] !== t29) { + t30 = t29.padEnd(maxIndexWidth + 2); + $[54] = maxIndexWidth; + $[55] = t29; + $[56] = t30; + } else { + t30 = $[56]; + } + let t31; + if ($[57] !== t30) { + t31 = {t30}; + $[57] = t30; + $[58] = t31; + } else { + t31 = $[58]; + } + let t32; + if ($[59] !== cursorOffset || $[60] !== imagesSelected || $[61] !== inputValue || $[62] !== isFocused || $[63] !== onExit || $[64] !== onImagePaste || $[65] !== onInputChange || $[66] !== onSubmit || $[67] !== option || $[68] !== showLabel) { + t32 = showLabel ? <>{option.label}{isFocused ? <>{option.labelValueSeparator ?? ", "} { + isUserEditing.current = true; + onInputChange(value); + option.onChange(value); + }} onSubmit={onSubmit} onExit={onExit} placeholder={option.placeholder} focus={!imagesSelected} showCursor={true} multiline={true} cursorOffset={cursorOffset} onChangeCursorOffset={setCursorOffset} columns={80} onImagePaste={onImagePaste} onPaste={pastedText => { + isUserEditing.current = true; + const before = inputValue.slice(0, cursorOffset); + const after = inputValue.slice(cursorOffset); + const newValue = before + pastedText + after; + onInputChange(newValue); + option.onChange(newValue); + setCursorOffset(before.length + pastedText.length); + }} /> : inputValue && {option.labelValueSeparator ?? ", "}{inputValue}} : isFocused ? { + isUserEditing.current = true; + onInputChange(value_0); + option.onChange(value_0); + }} onSubmit={onSubmit} onExit={onExit} placeholder={option.placeholder || (typeof option.label === "string" ? option.label : undefined)} focus={!imagesSelected} showCursor={true} multiline={true} cursorOffset={cursorOffset} onChangeCursorOffset={setCursorOffset} columns={80} onImagePaste={onImagePaste} onPaste={pastedText_0 => { + isUserEditing.current = true; + const before_0 = inputValue.slice(0, cursorOffset); + const after_0 = inputValue.slice(cursorOffset); + const newValue_0 = before_0 + pastedText_0 + after_0; + onInputChange(newValue_0); + option.onChange(newValue_0); + setCursorOffset(before_0.length + pastedText_0.length); + }} /> : {inputValue || option.placeholder || option.label}; + $[59] = cursorOffset; + $[60] = imagesSelected; + $[61] = inputValue; + $[62] = isFocused; + $[63] = onExit; + $[64] = onImagePaste; + $[65] = onInputChange; + $[66] = onSubmit; + $[67] = option; + $[68] = showLabel; + $[69] = t32; + } else { + t32 = $[69]; + } + let t33; + if ($[70] !== children || $[71] !== t28 || $[72] !== t31 || $[73] !== t32) { + t33 = {t31}{children}{t32}; + $[70] = children; + $[71] = t28; + $[72] = t31; + $[73] = t32; + $[74] = t33; + } else { + t33 = $[74]; + } + let t34; + if ($[75] !== isFocused || $[76] !== isSelected || $[77] !== shouldShowDownArrow || $[78] !== shouldShowUpArrow || $[79] !== t33) { + t34 = {t33}; + $[75] = isFocused; + $[76] = isSelected; + $[77] = shouldShowDownArrow; + $[78] = shouldShowUpArrow; + $[79] = t33; + $[80] = t34; + } else { + t34 = $[80]; + } + let t35; + if ($[81] !== descriptionPaddingLeft || $[82] !== isFocused || $[83] !== isSelected || $[84] !== option.description || $[85] !== option.dimDescription) { + t35 = option.description && {option.description}; + $[81] = descriptionPaddingLeft; + $[82] = isFocused; + $[83] = isSelected; + $[84] = option.description; + $[85] = option.dimDescription; + $[86] = t35; + } else { + t35 = $[86]; + } + let t36; + if ($[87] !== descriptionPaddingLeft || $[88] !== imageAttachments || $[89] !== imagesSelected || $[90] !== isFocused || $[91] !== selectedImageIndex) { + t36 = imageAttachments.length > 0 && {imageAttachments.map((img_0, idx) => )}{imagesSelected ? {imageAttachments.length > 1 && <>} : isFocused ? "(\u2193 to select)" : null}; + $[87] = descriptionPaddingLeft; + $[88] = imageAttachments; + $[89] = imagesSelected; + $[90] = isFocused; + $[91] = selectedImageIndex; + $[92] = t36; + } else { + t36 = $[92]; + } + let t37; + if ($[93] !== layout) { + t37 = layout === "expanded" && ; + $[93] = layout; + $[94] = t37; + } else { + t37 = $[94]; + } + let t38; + if ($[95] !== t34 || $[96] !== t35 || $[97] !== t36 || $[98] !== t37) { + t38 = {t34}{t35}{t36}{t37}; + $[95] = t34; + $[96] = t35; + $[97] = t36; + $[98] = t37; + $[99] = t38; + } else { + t38 = $[99]; + } + return t38; +} +function _temp(c) { + return c.type === "image"; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIlJlYWN0Tm9kZSIsInVzZUVmZmVjdCIsInVzZVJlZiIsInVzZVN0YXRlIiwiQm94IiwiVGV4dCIsInVzZUlucHV0IiwidXNlS2V5YmluZGluZyIsInVzZUtleWJpbmRpbmdzIiwiUGFzdGVkQ29udGVudCIsImdldEltYWdlRnJvbUNsaXBib2FyZCIsIkltYWdlRGltZW5zaW9ucyIsIkNsaWNrYWJsZUltYWdlUmVmIiwiQ29uZmlndXJhYmxlU2hvcnRjdXRIaW50IiwiQnlsaW5lIiwiVGV4dElucHV0IiwiT3B0aW9uV2l0aERlc2NyaXB0aW9uIiwiU2VsZW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4gICAgICAgICAgICAgIHtpc0ZvY3VzZWQgPyAoXG4gICAgICAgICAgICAgICAgPD5cbiAgICAgICAgICAgICAgICAgIDxUZXh0IGNvbG9yPVwic3VnZ2VzdGlvblwiPlxuICAgICAgICAgICAgICAgICAgICB7b3B0aW9uLmxhYmVsVmFsdWVTZXBhcmF0b3IgPz8gJywgJ31cbiAgICAgICAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgICAgICAgIDxUZXh0SW5wdXRcbiAgICAgICAgICAgICAgICAgICAgdmFsdWU9e2lucHV0VmFsdWV9XG4gICAgICAgICAgICAgICAgICAgIG9uQ2hhbmdlPXt2YWx1ZSA9PiB7XG4gICAgICAgICAgICAgICAgICAgICAgaXNVc2VyRWRpdGluZy5jdXJyZW50ID0gdHJ1ZVxuICAgICAgICAgICAgICAgICAgICAgIG9uSW5wdXRDaGFuZ2UodmFsdWUpXG4gICAgICAgICAgICAgICAgICAgICAgb3B0aW9uLm9uQ2hhbmdlKHZhbHVlKVxuICAgICAgICAgICAgICAgICAgICB9fVxuICAgICAgICAgICAgICAgICAgICBvblN1Ym1pdD17b25TdWJtaXR9XG4gICAgICAgICAgICAgICAgICAgIG9uRXhpdD17b25FeGl0fVxuICAgICAgICAgICAgICAgICAgICBwbGFjZWhvbGRlcj17b3B0aW9uLnBsYWNlaG9sZGVyfVxuICAgICAgICAgICAgICAgICAgICBmb2N1cz17IWltYWdlc1NlbGVjdGVkfVxuICAgICAgICAgICAgICAgICAgICBzaG93Q3Vyc29yPXt0cnVlfVxuICAgICAgICAgICAgICAgICAgICBtdWx0aWxpbmU9e3RydWV9XG4gICAgICAgICAgICAgICAgICAgIGN1cnNvck9mZnNldD17Y3Vyc29yT2Zmc2V0fVxuICAgICAgICAgICAgICAgICAgICBvbkNoYW5nZUN1cnNvck9mZnNldD17c2V0Q3Vyc29yT2Zmc2V0fVxuICAgICAgICAgICAgICAgICAgICBjb2x1bW5zPXs4MH1cbiAgICAgICAgICAgICAgICAgICAgb25JbWFnZVBhc3RlPXtvbkltYWdlUGFzdGV9XG4gICAgICAgICAgICAgICAgICAgIG9uUGFzdGU9eyhwYXN0ZWRUZXh0OiBzdHJpbmcpID0+IHtcbiAgICAgICAgICAgICAgICAgICAgICBpc1VzZXJFZGl0aW5nLmN1cnJlbnQgPSB0cnVlXG4gICAgICAgICAgICAgICAgICAgICAgY29uc3QgYmVmb3JlID0gaW5wdXRWYWx1ZS5zbGljZSgwLCBjdXJzb3JPZmZzZXQpXG4gICAgICAgICAgICAgICAgICAgICAgY29uc3QgYWZ0ZXIgPSBpbnB1dFZhbHVlLnNsaWNlKGN1cnNvck9mZnNldClcbiAgICAgICAgICAgICAgICAgICAgICBjb25zdCBuZXdWYWx1ZSA9IGJlZm9yZSArIHBhc3RlZFRleHQgKyBhZnRlclxuICAgICAgICAgICAgICAgICAgICAgIG9uSW5wdXRDaGFuZ2UobmV3VmFsdWUpXG4gICAgICAgICAgICAgICAgICAgICAgb3B0aW9uLm9uQ2hhbmdlKG5ld1ZhbHVlKVxuICAgICAgICAgICAgICAgICAgICAgIHNldEN1cnNvck9mZnNldChiZWZvcmUubGVuZ3RoICsgcGFzdGVkVGV4dC5sZW5ndGgpXG4gICAgICAgICAgICAgICAgICAgIH19XG4gICAgICAgICAgICAgICAgICAvPlxuICAgICAgICAgICAgICAgIDwvPlxuICAgICAgICAgICAgICApIDogKFxuICAgICAgICAgICAgICAgIGlucHV0VmFsdWUgJiYgKFxuICAgICAgICAgICAgICAgICAgPFRleHQ+XG4gICAgICAgICAgICAgICAgICAgIHtvcHRpb24ubGFiZWxWYWx1ZVNlcGFyYXRvciA/PyAnLCAnfVxuICAgICAgICAgICAgICAgICAgICB7aW5wdXRWYWx1ZX1cbiAgICAgICAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgICAgICApXG4gICAgICAgICAgICAgICl9XG4gICAgICAgICAgICA8Lz5cbiAgICAgICAgICApIDogaXNGb2N1c2VkID8gKFxuICAgICAgICAgICAgPFRleHRJbnB1dFxuICAgICAgICAgICAgICB2YWx1ZT17aW5wdXRWYWx1ZX1cbiAgICAgICAgICAgICAgb25DaGFuZ2U9e3ZhbHVlID0+IHtcbiAgICAgICAgICAgICAgICBpc1VzZXJFZGl0aW5nLmN1cnJlbnQgPSB0cnVlXG4gICAgICAgICAgICAgICAgb25JbnB1dENoYW5nZSh2YWx1ZSlcbiAgICAgICAgICAgICAgICBvcHRpb24ub25DaGFuZ2UodmFsdWUpXG4gICAgICAgICAgICAgIH19XG4gICAgICAgICAgICAgIG9uU3VibWl0PXtvblN1Ym1pdH1cbiAgICAgICAgICAgICAgb25FeGl0PXtvbkV4aXR9XG4gICAgICAgICAgICAgIHBsYWNlaG9sZGVyPXtcbiAgICAgICAgICAgICAgICBvcHRpb24ucGxhY2Vob2xkZXIgfHxcbiAgICAgICAgICAgICAgICAodHlwZW9mIG9wdGlvbi5sYWJlbCA9PT0gJ3N0cmluZycgPyBvcHRpb24ubGFiZWwgOiB1bmRlZmluZWQpXG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgZm9jdXM9eyFpbWFnZXNTZWxlY3RlZH1cbiAgICAgICAgICAgICAgc2hvd0N1cnNvcj17dHJ1ZX1cbiAgICAgICAgICAgICAgbXVsdGlsaW5lPXt0cnVlfVxuICAgICAgICAgICAgICBjdXJzb3JPZmZzZXQ9e2N1cnNvck9mZnNldH1cbiAgICAgICAgICAgICAgb25DaGFuZ2VDdXJzb3JPZmZzZXQ9e3NldEN1cnNvck9mZnNldH1cbiAgICAgICAgICAgICAgY29sdW1ucz17ODB9XG4gICAgICAgICAgICAgIG9uSW1hZ2VQYXN0ZT17b25JbWFnZVBhc3RlfVxuICAgICAgICAgICAgICBvblBhc3RlPXsocGFzdGVkVGV4dDogc3RyaW5nKSA9PiB7XG4gICAgICAgICAgICAgICAgaXNVc2VyRWRpdGluZy5jdXJyZW50ID0gdHJ1ZVxuICAgICAgICAgICAgICAgIGNvbnN0IGJlZm9yZSA9IGlucHV0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AgID5cbiAgICAgICAgICAgIHtvcHRpb24uZGVzY3JpcHRpb259XG4gICAgICAgICAgPC9UZXh0PlxuICAgICAgICA8L0JveD5cbiAgICAgICl9XG4gICAgICB7aW1hZ2VBdHRhY2htZW50cy5sZW5ndGggPiAwICYmIChcbiAgICAgICAgPEJveCBmbGV4RGlyZWN0aW9uPVwicm93XCIgZ2FwPXsxfSBwYWRkaW5nTGVmdD17ZGVzY3JpcHRpb25QYWRkaW5nTGVmdH0+XG4gICAgICAgICAge2ltYWdlQXR0YWNobWVudHMubWFwKChpbWcsIGlkeCkgPT4gKFxuICAgICAgICAgICAgPENsaWNrYWJsZUltYWdlUmVmXG4gICAgICAgICAgICAgIGtleT17aW1nLmlkfVxuICAgICAgICAgICAgICBpbWFnZUlkPXtpbWcuaWR9XG4gICAgICAgICAgICAgIGlzU2VsZWN0ZWQ9eyEhaW1hZ2VzU2VsZWN0ZWQgJiYgaWR4ID09PSBzZWxlY3RlZEltYWdlSW5kZXh9XG4gICAgICAgICAgICAvPlxuICAgICAgICAgICkpfVxuICAgICAgICAgIDxCb3ggZmxleEdyb3c9ezF9IGp1c3RpZnlDb250ZW50PVwiZmxleC1zdGFydFwiIGZsZXhEaXJlY3Rpb249XCJyb3dcIj5cbiAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPlxuICAgICAgICAgICAgICB7aW1hZ2VzU2VsZWN0ZWQgPyAoXG4gICAgICAgICAgICAgICAgPEJ5bGluZT5cbiAgICAgICAgICAgICAgICAgIHtpbWFnZUF0dGFjaG1lbnRzLmxlbmd0aCA+IDEgJiYgKFxuICAgICAgICAgICAgICAgICAgICA8PlxuICAgICAgICAgICAgICAgICAgICAgIDxDb25maWd1cmFibGVTaG9ydGN1dEhpbnRcbiAgICAgICAgICAgICAgICAgICAgICAgIGFjdGlvbj1cImF0dGFjaG1lbnRzOm5leHRcIlxuICAgICAgICAgICAgICAgICAgICAgICAgY29udGV4dD1cIkF0dGFjaG1lbnRzXCJcbiAgICAgICAgICAgICAgICAgICAgICAgIGZhbGxiYWNrPVwi4oaSXCJcbiAgICAgICAgICAgICAgICAgICAgICAgIGRlc2NyaXB0aW9uPVwibmV4dFwiXG4gICAgICAgICAgICAgICAgICAgICAgLz5cbiAgICAgICAgICAgICAgICAgICAgICA8Q29uZmlndXJhYmxlU2hvcnRjdXRIaW50XG4gICAgICAgICAgICAgICAgICAgICAgICBhY3Rpb249XCJhdHRhY2htZW50czpwcmV2aW91c1wiXG4gICAgICAgICAgICAgICAgICAgICAgICBjb250ZXh0PVwiQXR0YWNobWVudHNcIlxuICAgICAgICAgICAgICAgICAgICAgICAgZmFsbGJhY2s9XCLihpBcIlxuICAgICAgICAgICAgICAgICAgICAgICAgZGVzY3JpcHRpb249XCJwcmV2XCJcbiAgICAgICAgICAgICAgICAgICAgICAvPlxuICAgICAgICAgICAgICAgICAgICA8Lz5cbiAgICAgICAgICAgICAgICAgICl9XG4gICAgICAgICAgICAgICAgICA8Q29uZmlndXJhYmxlU2hvcnRjdXRIaW50XG4gICAgICAgICAgICAgICAgICAgIGFjdGlvbj1cImF0dGFjaG1lbnRzOnJlbW92ZVwiXG4gICAgICAgICAgICAgICAgICAgIGNvbnRleHQ9XCJBdHRhY2htZW50c1wiXG4gICAgICAgICAgICAgICAgICAgIGZhbGxiYWNrPVwiYmFja3NwYWNlXCJcbiAgICAgICAgICAgICAgICAgICAgZGVzY3JpcHRpb249XCJyZW1vdmVcIlxuICAgICAgICAgICAgICAgICAgLz5cbiAgICAgICAgICAgICAgICAgIDxDb25maWd1cmFibGVTaG9ydGN1dEhpbnRcbiAgICAgICAgICAgICAgICAgICAgYWN0aW9uPVwiYXR0YWNobWVudHM6ZXhpdFwiXG4gICAgICAgICAgICAgICAgICAgIGNvbnRleHQ9XCJBdHRhY2htZW50c1wiXG4gICAgICAgICAgICAgICAgICAgIGZhbGxiYWNrPVwiZXNjXCJcbiAgICAgICAgICAgICAgICAgICAgZGVzY3JpcHRpb249XCJjYW5jZWxcIlxuICAg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LENBQUMsSUFBSSxDQUFDLENBQUMsRUFBTixJQUFJLENBQVM7SUFBQXdCLENBQUEsT0FBQXhCLE1BQUE7SUFBQXdCLENBQUEsT0FBQW9GLEdBQUE7RUFBQTtJQUFBQSxHQUFBLEdBQUFwRixDQUFBO0VBQUE7RUFBQSxJQUFBcUYsR0FBQTtFQUFBLElBQUFyRixDQUFBLFNBQUE0RSxHQUFBLElBQUE1RSxDQUFBLFNBQUE2RSxHQUFBLElBQUE3RSxDQUFBLFNBQUFnRixHQUFBLElBQUFoRixDQUFBLFNBQUFvRixHQUFBO0lBaEsxQ0MsR0FBQSxJQUFDLEdBQUcsQ0FBZSxhQUFRLENBQVIsUUFBUSxDQUFhLFVBQUMsQ0FBRCxHQUFDLENBQ3ZDLENBQUFULEdBaUdjLENBQ2IsQ0FBQUMsR0FXRCxDQUNDLENBQUFHLEdBZ0RELENBQ0MsQ0FBQUksR0FBc0MsQ0FDekMsRUFqS0MsR0FBRyxDQWlLRTtJQUFBcEYsQ0FBQSxPQUFBNEUsR0FBQTtJQUFBNUUsQ0FBQSxPQUFBNkUsR0FBQTtJQUFBN0UsQ0FBQSxPQUFBZ0YsR0FBQTtJQUFBaEYsQ0FBQSxPQUFBb0YsR0FBQTtJQUFBcEYsQ0FBQSxPQUFBcUYsR0FBQTtFQUFBO0lBQUFBLEdBQUEsR0FBQXJGLENBQUE7RUFBQTtFQUFBLE9BaktOcUYsR0FpS007QUFBQTtBQWpVSCxTQUFBMUUsTUFBQTJFLENBQUE7RUFBQSxPQTBCeUNBLENBQUMsQ0FBQTFILElBQUssS0FBSyxPQUFPO0FBQUEiLCJpZ25vcmVMaXN0IjpbXX0= \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/CustomSelect/use-select-navigation.ts b/_all-in-one-upload/cc-haha/src/components/CustomSelect/use-select-navigation.ts new file mode 100644 index 0000000..7ecb4e7 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/CustomSelect/use-select-navigation.ts @@ -0,0 +1,653 @@ +import { + useCallback, + useEffect, + useMemo, + useReducer, + useRef, + useState, +} from 'react' +import { isDeepStrictEqual } from 'util' +import OptionMap from './option-map.js' +import type { OptionWithDescription } from './select.js' + +type State = { + /** + * Map where key is option's value and value is option's index. + */ + optionMap: OptionMap + + /** + * Number of visible options. + */ + visibleOptionCount: number + + /** + * Value of the currently focused option. + */ + focusedValue: T | undefined + + /** + * Index of the first visible option. + */ + visibleFromIndex: number + + /** + * Index of the last visible option. + */ + visibleToIndex: number +} + +type Action = + | FocusNextOptionAction + | FocusPreviousOptionAction + | FocusNextPageAction + | FocusPreviousPageAction + | SetFocusAction + | ResetAction + +type SetFocusAction = { + type: 'set-focus' + value: T +} + +type FocusNextOptionAction = { + type: 'focus-next-option' +} + +type FocusPreviousOptionAction = { + type: 'focus-previous-option' +} + +type FocusNextPageAction = { + type: 'focus-next-page' +} + +type FocusPreviousPageAction = { + type: 'focus-previous-page' +} + +type ResetAction = { + type: 'reset' + state: State +} + +const reducer = (state: State, action: Action): State => { + switch (action.type) { + case 'focus-next-option': { + if (state.focusedValue === undefined) { + return state + } + + const item = state.optionMap.get(state.focusedValue) + + if (!item) { + return state + } + + // Wrap to first item if at the end + const next = item.next || state.optionMap.first + + if (!next) { + return state + } + + // When wrapping to first, reset viewport to start + if (!item.next && next === state.optionMap.first) { + return { + ...state, + focusedValue: next.value, + visibleFromIndex: 0, + visibleToIndex: state.visibleOptionCount, + } + } + + const needsToScroll = next.index >= state.visibleToIndex + + if (!needsToScroll) { + return { + ...state, + focusedValue: next.value, + } + } + + const nextVisibleToIndex = Math.min( + state.optionMap.size, + state.visibleToIndex + 1, + ) + + const nextVisibleFromIndex = nextVisibleToIndex - state.visibleOptionCount + + return { + ...state, + focusedValue: next.value, + visibleFromIndex: nextVisibleFromIndex, + visibleToIndex: nextVisibleToIndex, + } + } + + case 'focus-previous-option': { + if (state.focusedValue === undefined) { + return state + } + + const item = state.optionMap.get(state.focusedValue) + + if (!item) { + return state + } + + // Wrap to last item if at the beginning + const previous = item.previous || state.optionMap.last + + if (!previous) { + return state + } + + // When wrapping to last, reset viewport to end + if (!item.previous && previous === state.optionMap.last) { + const nextVisibleToIndex = state.optionMap.size + const nextVisibleFromIndex = Math.max( + 0, + nextVisibleToIndex - state.visibleOptionCount, + ) + return { + ...state, + focusedValue: previous.value, + visibleFromIndex: nextVisibleFromIndex, + visibleToIndex: nextVisibleToIndex, + } + } + + const needsToScroll = previous.index <= state.visibleFromIndex + + if (!needsToScroll) { + return { + ...state, + focusedValue: previous.value, + } + } + + const nextVisibleFromIndex = Math.max(0, state.visibleFromIndex - 1) + + const nextVisibleToIndex = nextVisibleFromIndex + state.visibleOptionCount + + return { + ...state, + focusedValue: previous.value, + visibleFromIndex: nextVisibleFromIndex, + visibleToIndex: nextVisibleToIndex, + } + } + + case 'focus-next-page': { + if (state.focusedValue === undefined) { + return state + } + + const item = state.optionMap.get(state.focusedValue) + + if (!item) { + return state + } + + // Move by a full page (visibleOptionCount items) + const targetIndex = Math.min( + state.optionMap.size - 1, + item.index + state.visibleOptionCount, + ) + + // Find the item at the target index + let targetItem = state.optionMap.first + while (targetItem && targetItem.index < targetIndex) { + if (targetItem.next) { + targetItem = targetItem.next + } else { + break + } + } + + if (!targetItem) { + return state + } + + // Update the visible range to include the new focused item + const nextVisibleToIndex = Math.min( + state.optionMap.size, + targetItem.index + 1, + ) + const nextVisibleFromIndex = Math.max( + 0, + nextVisibleToIndex - state.visibleOptionCount, + ) + + return { + ...state, + focusedValue: targetItem.value, + visibleFromIndex: nextVisibleFromIndex, + visibleToIndex: nextVisibleToIndex, + } + } + + case 'focus-previous-page': { + if (state.focusedValue === undefined) { + return state + } + + const item = state.optionMap.get(state.focusedValue) + + if (!item) { + return state + } + + // Move by a full page (visibleOptionCount items) + const targetIndex = Math.max(0, item.index - state.visibleOptionCount) + + // Find the item at the target index + let targetItem = state.optionMap.first + while (targetItem && targetItem.index < targetIndex) { + if (targetItem.next) { + targetItem = targetItem.next + } else { + break + } + } + + if (!targetItem) { + return state + } + + // Update the visible range to include the new focused item + const nextVisibleFromIndex = Math.max(0, targetItem.index) + const nextVisibleToIndex = Math.min( + state.optionMap.size, + nextVisibleFromIndex + state.visibleOptionCount, + ) + + return { + ...state, + focusedValue: targetItem.value, + visibleFromIndex: nextVisibleFromIndex, + visibleToIndex: nextVisibleToIndex, + } + } + + case 'reset': { + return action.state + } + + case 'set-focus': { + // Early return if already focused on this value + if (state.focusedValue === action.value) { + return state + } + + const item = state.optionMap.get(action.value) + if (!item) { + return state + } + + // Check if the item is already in view + if ( + item.index >= state.visibleFromIndex && + item.index < state.visibleToIndex + ) { + // Already visible, just update focus + return { + ...state, + focusedValue: action.value, + } + } + + // Need to scroll to make the item visible + // Scroll as little as possible - put item at edge of viewport + let nextVisibleFromIndex: number + let nextVisibleToIndex: number + + if (item.index < state.visibleFromIndex) { + // Item is above viewport - scroll up to put it at the top + nextVisibleFromIndex = item.index + nextVisibleToIndex = Math.min( + state.optionMap.size, + nextVisibleFromIndex + state.visibleOptionCount, + ) + } else { + // Item is below viewport - scroll down to put it at the bottom + nextVisibleToIndex = Math.min(state.optionMap.size, item.index + 1) + nextVisibleFromIndex = Math.max( + 0, + nextVisibleToIndex - state.visibleOptionCount, + ) + } + + return { + ...state, + focusedValue: action.value, + visibleFromIndex: nextVisibleFromIndex, + visibleToIndex: nextVisibleToIndex, + } + } + } +} + +export type UseSelectNavigationProps = { + /** + * Number of items to display. + * + * @default 5 + */ + visibleOptionCount?: number + + /** + * Options. + */ + options: OptionWithDescription[] + + /** + * Initially focused option's value. + */ + initialFocusValue?: T + + /** + * Callback for focusing an option. + */ + onFocus?: (value: T) => void + + /** + * Value to focus + */ + focusValue?: T +} + +export type SelectNavigation = { + /** + * Value of the currently focused option. + */ + focusedValue: T | undefined + + /** + * 1-based index of the focused option in the full list. + * Returns 0 if no option is focused. + */ + focusedIndex: number + + /** + * Index of the first visible option. + */ + visibleFromIndex: number + + /** + * Index of the last visible option. + */ + visibleToIndex: number + + /** + * All options. + */ + options: OptionWithDescription[] + + /** + * Visible options. + */ + visibleOptions: Array & { index: number }> + + /** + * Whether the focused option is an input type. + */ + isInInput: boolean + + /** + * Focus next option and scroll the list down, if needed. + */ + focusNextOption: () => void + + /** + * Focus previous option and scroll the list up, if needed. + */ + focusPreviousOption: () => void + + /** + * Focus next page and scroll the list down by a page. + */ + focusNextPage: () => void + + /** + * Focus previous page and scroll the list up by a page. + */ + focusPreviousPage: () => void + + /** + * Focus a specific option by value. + */ + focusOption: (value: T | undefined) => void +} + +const createDefaultState = ({ + visibleOptionCount: customVisibleOptionCount, + options, + initialFocusValue, + currentViewport, +}: Pick, 'visibleOptionCount' | 'options'> & { + initialFocusValue?: T + currentViewport?: { visibleFromIndex: number; visibleToIndex: number } +}): State => { + const visibleOptionCount = + typeof customVisibleOptionCount === 'number' + ? Math.min(customVisibleOptionCount, options.length) + : options.length + + const optionMap = new OptionMap(options) + const focusedItem = + initialFocusValue !== undefined && optionMap.get(initialFocusValue) + const focusedValue = focusedItem ? initialFocusValue : optionMap.first?.value + + let visibleFromIndex = 0 + let visibleToIndex = visibleOptionCount + + // When there's a valid focused item, adjust viewport to show it + if (focusedItem) { + const focusedIndex = focusedItem.index + + if (currentViewport) { + // If focused item is already in the current viewport range, try to preserve it + if ( + focusedIndex >= currentViewport.visibleFromIndex && + focusedIndex < currentViewport.visibleToIndex + ) { + // Keep the same viewport if it's valid + visibleFromIndex = currentViewport.visibleFromIndex + visibleToIndex = Math.min( + optionMap.size, + currentViewport.visibleToIndex, + ) + } else { + // Need to adjust viewport to show focused item + // Use minimal scrolling - put item at edge of viewport + if (focusedIndex < currentViewport.visibleFromIndex) { + // Item is above current viewport - scroll up to put it at the top + visibleFromIndex = focusedIndex + visibleToIndex = Math.min( + optionMap.size, + visibleFromIndex + visibleOptionCount, + ) + } else { + // Item is below current viewport - scroll down to put it at the bottom + visibleToIndex = Math.min(optionMap.size, focusedIndex + 1) + visibleFromIndex = Math.max(0, visibleToIndex - visibleOptionCount) + } + } + } else if (focusedIndex >= visibleOptionCount) { + // No current viewport but focused item is outside default viewport + // Scroll to show the focused item at the bottom of the viewport + visibleToIndex = Math.min(optionMap.size, focusedIndex + 1) + visibleFromIndex = Math.max(0, visibleToIndex - visibleOptionCount) + } + + // Ensure viewport bounds are valid + visibleFromIndex = Math.max( + 0, + Math.min(visibleFromIndex, optionMap.size - 1), + ) + visibleToIndex = Math.min( + optionMap.size, + Math.max(visibleOptionCount, visibleToIndex), + ) + } + + return { + optionMap, + visibleOptionCount, + focusedValue, + visibleFromIndex, + visibleToIndex, + } +} + +export function useSelectNavigation({ + visibleOptionCount = 5, + options, + initialFocusValue, + onFocus, + focusValue, +}: UseSelectNavigationProps): SelectNavigation { + const [state, dispatch] = useReducer( + reducer, + { + visibleOptionCount, + options, + initialFocusValue: focusValue || initialFocusValue, + } as Parameters>[0], + createDefaultState, + ) + + // Store onFocus in a ref to avoid re-running useEffect when callback changes + const onFocusRef = useRef(onFocus) + onFocusRef.current = onFocus + + const [lastOptions, setLastOptions] = useState(options) + + if (options !== lastOptions && !isDeepStrictEqual(options, lastOptions)) { + dispatch({ + type: 'reset', + state: createDefaultState({ + visibleOptionCount, + options, + initialFocusValue: + focusValue ?? state.focusedValue ?? initialFocusValue, + currentViewport: { + visibleFromIndex: state.visibleFromIndex, + visibleToIndex: state.visibleToIndex, + }, + }), + }) + + setLastOptions(options) + } + + const focusNextOption = useCallback(() => { + dispatch({ + type: 'focus-next-option', + }) + }, []) + + const focusPreviousOption = useCallback(() => { + dispatch({ + type: 'focus-previous-option', + }) + }, []) + + const focusNextPage = useCallback(() => { + dispatch({ + type: 'focus-next-page', + }) + }, []) + + const focusPreviousPage = useCallback(() => { + dispatch({ + type: 'focus-previous-page', + }) + }, []) + + const focusOption = useCallback((value: T | undefined) => { + if (value !== undefined) { + dispatch({ + type: 'set-focus', + value, + }) + } + }, []) + + const visibleOptions = useMemo(() => { + return options + .map((option, index) => ({ + ...option, + index, + })) + .slice(state.visibleFromIndex, state.visibleToIndex) + }, [options, state.visibleFromIndex, state.visibleToIndex]) + + // Validate that focusedValue exists in current options. + // This handles the case where options change during render but the reset + // action hasn't been processed yet - without this, the cursor would disappear + // because focusedValue points to an option that no longer exists. + const validatedFocusedValue = useMemo(() => { + if (state.focusedValue === undefined) { + return undefined + } + const exists = options.some(opt => opt.value === state.focusedValue) + if (exists) { + return state.focusedValue + } + // Fall back to first option if focused value doesn't exist + return options[0]?.value + }, [state.focusedValue, options]) + + const isInInput = useMemo(() => { + const focusedOption = options.find( + opt => opt.value === validatedFocusedValue, + ) + return focusedOption?.type === 'input' + }, [validatedFocusedValue, options]) + + // Call onFocus with the validated value (what's actually displayed), + // not the internal state value which may be stale if options changed. + // Use ref to avoid re-running when callback reference changes. + useEffect(() => { + if (validatedFocusedValue !== undefined) { + onFocusRef.current?.(validatedFocusedValue) + } + }, [validatedFocusedValue]) + + // Allow parent to programmatically set focus via focusValue prop + useEffect(() => { + if (focusValue !== undefined) { + dispatch({ + type: 'set-focus', + value: focusValue, + }) + } + }, [focusValue]) + + // Compute 1-based focused index for scroll position display + const focusedIndex = useMemo(() => { + if (validatedFocusedValue === undefined) { + return 0 + } + const index = options.findIndex(opt => opt.value === validatedFocusedValue) + return index >= 0 ? index + 1 : 0 + }, [validatedFocusedValue, options]) + + return { + focusedValue: validatedFocusedValue, + focusedIndex, + visibleFromIndex: state.visibleFromIndex, + visibleToIndex: state.visibleToIndex, + visibleOptions, + isInInput: isInInput ?? false, + focusNextOption, + focusPreviousOption, + focusNextPage, + focusPreviousPage, + focusOption, + options, + } +} diff --git a/_all-in-one-upload/cc-haha/src/components/DevChannelsDialog.tsx b/_all-in-one-upload/cc-haha/src/components/DevChannelsDialog.tsx new file mode 100644 index 0000000..5e68afb --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/DevChannelsDialog.tsx @@ -0,0 +1,105 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { useCallback } from 'react'; +import type { ChannelEntry } from '../bootstrap/state.js'; +import { Box, Text } from '../ink.js'; +import { gracefulShutdownSync } from '../utils/gracefulShutdown.js'; +import { Select } from './CustomSelect/index.js'; +import { Dialog } from './design-system/Dialog.js'; +type Props = { + channels: ChannelEntry[]; + onAccept(): void; +}; +export function DevChannelsDialog(t0) { + const $ = _c(14); + const { + channels, + onAccept + } = t0; + let t1; + if ($[0] !== onAccept) { + t1 = function onChange(value) { + bb2: switch (value) { + case "accept": + { + onAccept(); + break bb2; + } + case "exit": + { + gracefulShutdownSync(1); + } + } + }; + $[0] = onAccept; + $[1] = t1; + } else { + t1 = $[1]; + } + const onChange = t1; + const handleEscape = _temp; + let t2; + let t3; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t2 = --dangerously-load-development-channels is for local channel development only. Do not use this option to run channels you have downloaded off the internet.; + t3 = Please use --channels to run a list of approved channels.; + $[2] = t2; + $[3] = t3; + } else { + t2 = $[2]; + t3 = $[3]; + } + let t4; + if ($[4] !== channels) { + t4 = channels.map(_temp2).join(", "); + $[4] = channels; + $[5] = t4; + } else { + t4 = $[5]; + } + let t5; + if ($[6] !== t4) { + t5 = {t2}{t3}Channels:{" "}{t4}; + $[6] = t4; + $[7] = t5; + } else { + t5 = $[7]; + } + let t6; + if ($[8] === Symbol.for("react.memo_cache_sentinel")) { + t6 = [{ + label: "I am using this for local development", + value: "accept" + }, { + label: "Exit", + value: "exit" + }]; + $[8] = t6; + } else { + t6 = $[8]; + } + let t7; + if ($[9] !== onChange) { + t7 = onResponse('no')} /> + + + ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkJveCIsIlRleHQiLCJTZWxlY3QiLCJQZXJtaXNzaW9uRGlhbG9nIiwiUHJvcHMiLCJwbHVnaW5OYW1lIiwicGx1Z2luRGVzY3JpcHRpb24iLCJmaWxlRXh0ZW5zaW9uIiwib25SZXNwb25zZSIsInJlc3BvbnNlIiwiQVVUT19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a/_all-in-one-upload/cc-haha/src/components/Spinner/teammateSelectHint.ts b/_all-in-one-upload/cc-haha/src/components/Spinner/teammateSelectHint.ts new file mode 100644 index 0000000..420f949 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/Spinner/teammateSelectHint.ts @@ -0,0 +1 @@ +export const TEAMMATE_SELECT_HINT = 'shift + ↑/↓ to select' diff --git a/_all-in-one-upload/cc-haha/src/components/Spinner/useShimmerAnimation.ts b/_all-in-one-upload/cc-haha/src/components/Spinner/useShimmerAnimation.ts new file mode 100644 index 0000000..d1d4ea9 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/Spinner/useShimmerAnimation.ts @@ -0,0 +1,31 @@ +import { useMemo } from 'react' +import { stringWidth } from '../../ink/stringWidth.js' +import { type DOMElement, useAnimationFrame } from '../../ink.js' +import type { SpinnerMode } from './types.js' + +export function useShimmerAnimation( + mode: SpinnerMode, + message: string, + isStalled: boolean, +): [ref: (element: DOMElement | null) => void, glimmerIndex: number] { + const glimmerSpeed = mode === 'requesting' ? 50 : 200 + // Pass null when stalled to unsubscribe from the clock — otherwise the + // setInterval keeps firing at 20fps even when the shimmer isn't visible. + // Notably, if the caller never attaches `ref` (e.g. conditional JSX), + // useTerminalViewport stays at its initial isVisible:true and the + // viewport-pause never kicks in, so this is the only stop mechanism. + const [ref, time] = useAnimationFrame(isStalled ? null : glimmerSpeed) + const messageWidth = useMemo(() => stringWidth(message), [message]) + + if (isStalled) { + return [ref, -100] + } + + const cyclePosition = Math.floor(time / glimmerSpeed) + const cycleLength = messageWidth + 20 + + if (mode === 'requesting') { + return [ref, (cyclePosition % cycleLength) - 10] + } + return [ref, messageWidth + 10 - (cyclePosition % cycleLength)] +} diff --git a/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/CreateAgentWizard.tsx b/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/CreateAgentWizard.tsx new file mode 100644 index 0000000..4ca9ba3 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/CreateAgentWizard.tsx @@ -0,0 +1,97 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { type ReactNode } from 'react'; +import { isAutoMemoryEnabled } from '../../../memdir/paths.js'; +import type { Tools } from '../../../Tool.js'; +import type { AgentDefinition } from '../../../tools/AgentTool/loadAgentsDir.js'; +import { WizardProvider } from '../../wizard/index.js'; +import type { WizardStepComponent } from '../../wizard/types.js'; +import type { AgentWizardData } from './types.js'; +import { ColorStep } from './wizard-steps/ColorStep.js'; +import { ConfirmStepWrapper } from './wizard-steps/ConfirmStepWrapper.js'; +import { DescriptionStep } from './wizard-steps/DescriptionStep.js'; +import { GenerateStep } from './wizard-steps/GenerateStep.js'; +import { LocationStep } from './wizard-steps/LocationStep.js'; +import { MemoryStep } from './wizard-steps/MemoryStep.js'; +import { MethodStep } from './wizard-steps/MethodStep.js'; +import { ModelStep } from './wizard-steps/ModelStep.js'; +import { PromptStep } from './wizard-steps/PromptStep.js'; +import { ToolsStep } from './wizard-steps/ToolsStep.js'; +import { TypeStep } from './wizard-steps/TypeStep.js'; +type Props = { + tools: Tools; + existingAgents: AgentDefinition[]; + onComplete: (message: string) => void; + onCancel: () => void; +}; +export function CreateAgentWizard(t0) { + const $ = _c(17); + const { + tools, + existingAgents, + onComplete, + onCancel + } = t0; + let t1; + if ($[0] !== existingAgents) { + t1 = () => ; + $[0] = existingAgents; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; + if ($[2] !== tools) { + t2 = () => ; + $[2] = tools; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] === Symbol.for("react.memo_cache_sentinel")) { + t3 = isAutoMemoryEnabled() ? [MemoryStep] : []; + $[4] = t3; + } else { + t3 = $[4]; + } + let t4; + if ($[5] !== existingAgents || $[6] !== onComplete || $[7] !== tools) { + t4 = () => ; + $[5] = existingAgents; + $[6] = onComplete; + $[7] = tools; + $[8] = t4; + } else { + t4 = $[8]; + } + let t5; + if ($[9] !== t1 || $[10] !== t2 || $[11] !== t4) { + t5 = [LocationStep, MethodStep, GenerateStep, t1, PromptStep, DescriptionStep, t2, ModelStep, ColorStep, ...t3, t4]; + $[9] = t1; + $[10] = t2; + $[11] = t4; + $[12] = t5; + } else { + t5 = $[12]; + } + const steps = t5; + let t6; + if ($[13] === Symbol.for("react.memo_cache_sentinel")) { + t6 = {}; + $[13] = t6; + } else { + t6 = $[13]; + } + let t7; + if ($[14] !== onCancel || $[15] !== steps) { + t7 = ; + $[14] = onCancel; + $[15] = steps; + $[16] = t7; + } else { + t7 = $[16]; + } + return t7; +} +function _temp() {} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIlJlYWN0Tm9kZSIsImlzQXV0b01lbW9yeUVuYWJsZWQiLCJUb29scyIsIkFnZW50RGVmaW5pdGlvbiIsIldpemFyZFByb3ZpZGVyIiwiV2l6YXJkU3RlcENvbXBvbmVudCIsIkFnZW50V2l6YXJkRGF0YSIsIkNvbG9yU3RlcCIsIkNvbmZpcm1TdGVwV3JhcHBlciIsIkRlc2NyaXB0aW9uU3RlcCIsIkdlbmVyYXRlU3RlcCIsIkxvY2F0aW9uU3RlcCIsIk1lbW9yeVN0ZXAiLCJNZXRob2RTdGVwIiwiTW9kZWxTdGVwIiwiUHJvbXB0U3RlcCIsIlRvb2xzU3RlcCIsIlR5cGVTdGVwIiwiUHJvcHMiLCJ0b29scyIsImV4aXN0aW5nQWdlbnRzIiwib25Db21wbGV0ZSIsIm1lc3NhZ2UiLCJvbkNhbmNlbCIsIkNyZWF0ZUFnZW50V2l6YXJkIiwidDAiLCIkIiwiX2MiLCJ0MSIsInQyIiwidDMiLCJTeW1ib2wiLCJmb3IiLCJ0NCIsInQ1Iiwic3RlcHMiLCJ0NiIsInQ3IiwiX3RlbXAiXSwic291cmNlcyI6WyJDcmVhdGVBZ2VudFdpemFyZC50c3giXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0I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sV0FBRSxDQUFGLENBQUFDLEVBQUMsQ0FBQyxDQUNILFVBR1gsQ0FIVyxDQUFBRSxLQUdaLENBQUMsQ0FDU2YsUUFBUSxDQUFSQSxTQUFPLENBQUMsQ0FDWixLQUFrQixDQUFsQixrQkFBa0IsQ0FDUCxlQUFLLENBQUwsTUFBSSxDQUFDLEdBQ3RCO0lBQUFHLENBQUEsT0FBQUgsUUFBQTtJQUFBRyxDQUFBLE9BQUFTLEtBQUE7SUFBQVQsQ0FBQSxPQUFBVyxFQUFBO0VBQUE7SUFBQUEsRUFBQSxHQUFBWCxDQUFBO0VBQUE7RUFBQSxPQVZGVyxFQVVFO0FBQUE7QUF2Q0MsU0FBQUMsTUFBQSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/wizard-steps/ConfirmStepWrapper.tsx b/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/wizard-steps/ConfirmStepWrapper.tsx new file mode 100644 index 0000000..343eca2 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/wizard-steps/ConfirmStepWrapper.tsx @@ -0,0 +1,74 @@ +import chalk from 'chalk'; +import React, { type ReactNode, useCallback, useState } from 'react'; +import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent } from 'src/services/analytics/index.js'; +import { useSetAppState } from 'src/state/AppState.js'; +import type { Tools } from '../../../../Tool.js'; +import type { AgentDefinition } from '../../../../tools/AgentTool/loadAgentsDir.js'; +import { getActiveAgentsFromList } from '../../../../tools/AgentTool/loadAgentsDir.js'; +import { editFileInEditor } from '../../../../utils/promptEditor.js'; +import { useWizard } from '../../../wizard/index.js'; +import { getNewAgentFilePath, saveAgentToFile } from '../../agentFileUtils.js'; +import type { AgentWizardData } from '../types.js'; +import { ConfirmStep } from './ConfirmStep.js'; +type Props = { + tools: Tools; + existingAgents: AgentDefinition[]; + onComplete: (message: string) => void; +}; +export function ConfirmStepWrapper({ + tools, + existingAgents, + onComplete +}: Props): ReactNode { + const { + wizardData + } = useWizard(); + const [saveError, setSaveError] = useState(null); + const setAppState = useSetAppState(); + const saveAgent = useCallback(async (openInEditor: boolean): Promise => { + if (!wizardData?.finalAgent) return; + try { + await saveAgentToFile(wizardData.location!, wizardData.finalAgent.agentType, wizardData.finalAgent.whenToUse, wizardData.finalAgent.tools, wizardData.finalAgent.getSystemPrompt(), true, wizardData.finalAgent.color, wizardData.finalAgent.model, wizardData.finalAgent.memory); + setAppState(state => { + if (!wizardData.finalAgent) return state; + const allAgents = state.agentDefinitions.allAgents.concat(wizardData.finalAgent); + return { + ...state, + agentDefinitions: { + ...state.agentDefinitions, + activeAgents: getActiveAgentsFromList(allAgents), + allAgents + } + }; + }); + if (openInEditor) { + const filePath = getNewAgentFilePath({ + source: wizardData.location!, + agentType: wizardData.finalAgent.agentType + }); + await editFileInEditor(filePath); + } + logEvent('tengu_agent_created', { + agent_type: wizardData.finalAgent.agentType, + generation_method: wizardData.wasGenerated ? 'generated' : 'manual', + source: wizardData.location!, + tool_count: wizardData.finalAgent.tools?.length ?? 'all', + has_custom_model: !!wizardData.finalAgent.model, + has_custom_color: !!wizardData.finalAgent.color, + has_memory: !!wizardData.finalAgent.memory, + memory_scope: wizardData.finalAgent.memory ?? 'none', + ...(openInEditor ? { + opened_in_editor: true + } : {}) + } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS); + const message = openInEditor ? `Created agent: ${chalk.bold(wizardData.finalAgent.agentType)} and opened in editor. ` + `If you made edits, restart to load the latest version.` : `Created agent: ${chalk.bold(wizardData.finalAgent.agentType)}`; + onComplete(message); + } catch (err) { + setSaveError(err instanceof Error ? err.message : 'Failed to save agent'); + } + }, [wizardData, onComplete, setAppState]); + const handleSave = useCallback(() => saveAgent(false), [saveAgent]); + const handleSaveAndEdit = useCallback(() => saveAgent(true), [saveAgent]); + return ; +} +//# sourceMappingURL=data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3aXphcmREYXRhLmZpbmFsQWdlbnQud2hlblRvVXNlLFxuICAgICAgICAgIHdpemFyZERhdGEuZmluYWxBZ2VudC50b29scyxcbiAgICAgICAgICB3aXphcmREYXRhLmZpbmFsQWdlbnQuZ2V0U3lzdGVtUHJvbXB0KCksXG4gICAgICAgICAgdHJ1ZSxcbiAgICAgICAgICB3aXphcmREYXRhLmZpbmFsQWdlbnQuY29sb3IsXG4gICAgICAgICAgd2l6YXJkRGF0YS5maW5hbEFnZW50Lm1vZGVsLFxuICAgICAgICAgIHdpemFyZERhdGEuZmluYWxBZ2VudC5tZW1vcnksXG4gICAgICAgIClcblxuICAgICAgICBzZXRBcHBTdGF0ZShzdGF0ZSA9PiB7XG4gICAgICAgICAgaWYgKCF3aXphcmREYXRhLmZpbmFsQWdlbnQpIHJldHVybiBzdGF0ZVxuXG4gICAgICAgICAgY29uc3QgYWxsQWdlbnRzID0gc3RhdGUuYWdlbnREZWZpbml0aW9ucy5hbGxBZ2VudHMuY29uY2F0KFxuICAgICAgICAgICAgd2l6YXJkRGF0YS5maW5hbEFnZW50LFxuICAgICAgICAgIClcbiAgICAgICAgICByZXR1cm4ge1xuICAgICAgICAgICAgLi4uc3RhdGUsXG4gICAgICAgICAgICBhZ2VudERlZmluaXRpb25zOiB7XG4gICAgICAgICAgICAgIC4uLnN0YXRlLmFnZW50RGVmaW5pdGlvbnMsXG4gICAgICAgICAgICAgIGFjdGl2ZUFnZW50czogZ2V0QWN0aXZlQWdlbnRzRnJvbUxpc3QoYWxsQWdlbnRzKSxcbiAgICAgICAgICAgICAgYWxsQWdlbnRzLFxuICAgICAgICAgICAgfSxcbiAgICAgICAgICB9XG4gICAgICAgIH0pXG5cbiAgICAgICAgaWYgKG9wZW5JbkVkaXRvcikge1xuICAgICAgICAgIGNvbnN0IGZpbGVQYXRoID0gZ2V0TmV3QWdlbnRGaWxlUGF0aCh7XG4gICAgICAgICAgICBzb3VyY2U6IHdpemFyZERhdGEubG9jYXRpb24hLFxuICA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LENBQUNDLGlCQUFpQixDQUFDLENBQ2pDLEtBQUssQ0FBQyxDQUFDbkMsU0FBUyxDQUFDLEdBQ2pCO0FBRU4iLCJpZ25vcmVMaXN0IjpbXX0= \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/wizard-steps/MemoryStep.tsx b/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/wizard-steps/MemoryStep.tsx new file mode 100644 index 0000000..56c951a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/agents/new-agent-creation/wizard-steps/MemoryStep.tsx @@ -0,0 +1,113 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { type ReactNode } from 'react'; +import { Box } from '../../../../ink.js'; +import { useKeybinding } from '../../../../keybindings/useKeybinding.js'; +import { isAutoMemoryEnabled } from '../../../../memdir/paths.js'; +import { type AgentMemoryScope, loadAgentMemoryPrompt } from '../../../../tools/AgentTool/agentMemory.js'; +import { ConfigurableShortcutHint } from '../../../ConfigurableShortcutHint.js'; +import { Select } from '../../../CustomSelect/select.js'; +import { Byline } from '../../../design-system/Byline.js'; +import { KeyboardShortcutHint } from '../../../design-system/KeyboardShortcutHint.js'; +import { useWizard } from '../../../wizard/index.js'; +import { WizardDialogLayout } from '../../../wizard/WizardDialogLayout.js'; +import type { AgentWizardData } from '../types.js'; +type MemoryOption = { + label: string; + value: AgentMemoryScope | 'none'; +}; +export function MemoryStep() { + const $ = _c(13); + const { + goNext, + goBack, + updateWizardData, + wizardData + } = useWizard(); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { + context: "Confirmation" + }; + $[0] = t0; + } else { + t0 = $[0]; + } + useKeybinding("confirm:no", goBack, t0); + const isUserScope = wizardData.location === "userSettings"; + let t1; + if ($[1] !== isUserScope) { + t1 = isUserScope ? [{ + label: "User scope (~/.claude/agent-memory/) (Recommended)", + value: "user" + }, { + label: "None (no persistent memory)", + value: "none" + }, { + label: "Project scope (.claude/agent-memory/)", + value: "project" + }, { + label: "Local scope (.claude/agent-memory-local/)", + value: "local" + }] : [{ + label: "Project scope (.claude/agent-memory/) (Recommended)", + value: "project" + }, { + label: "None (no persistent memory)", + value: "none" + }, { + label: "User scope (~/.claude/agent-memory/)", + value: "user" + }, { + label: "Local scope (.claude/agent-memory-local/)", + value: "local" + }]; + $[1] = isUserScope; + $[2] = t1; + } else { + t1 = $[2]; + } + const memoryOptions = t1; + let t2; + if ($[3] !== goNext || $[4] !== updateWizardData || $[5] !== wizardData.finalAgent || $[6] !== wizardData.systemPrompt) { + t2 = value => { + const memory = value === "none" ? undefined : value as AgentMemoryScope; + const agentType = wizardData.finalAgent?.agentType; + updateWizardData({ + selectedMemory: memory, + finalAgent: wizardData.finalAgent ? { + ...wizardData.finalAgent, + memory, + getSystemPrompt: isAutoMemoryEnabled() && memory && agentType ? () => wizardData.systemPrompt + "\n\n" + loadAgentMemoryPrompt(agentType, memory) : () => wizardData.systemPrompt + } : undefined + }); + goNext(); + }; + $[3] = goNext; + $[4] = updateWizardData; + $[5] = wizardData.finalAgent; + $[6] = wizardData.systemPrompt; + $[7] = t2; + } else { + t2 = $[7]; + } + const handleSelect = t2; + let t3; + if ($[8] === Symbol.for("react.memo_cache_sentinel")) { + t3 = ; + $[8] = t3; + } else { + t3 = $[8]; + } + let t4; + if ($[9] !== goBack || $[10] !== handleSelect || $[11] !== memoryOptions) { + t4 = ; + $[10] = onCancel; + $[11] = t2; + $[12] = t3; + $[13] = t4; + } else { + t4 = $[13]; + } + let t5; + if ($[14] !== hookEventMetadata.description || $[15] !== onCancel || $[16] !== t4 || $[17] !== title) { + t5 = {t4}; + $[14] = hookEventMetadata.description; + $[15] = onCancel; + $[16] = t4; + $[17] = title; + $[18] = t5; + } else { + t5 = $[18]; + } + return t5; +} +function _temp2(hook, index) { + return { + label: `[${hook.config.type}] ${getHookDisplayText(hook.config)}`, + value: index.toString(), + description: hook.source === "pluginHook" && hook.pluginName ? `${hookSourceHeaderDisplayString(hook.source)} (${hook.pluginName})` : hookSourceHeaderDisplayString(hook.source) + }; +} +function _temp() { + return Esc to go back; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkhvb2tFdmVudCIsIkhvb2tFdmVudE1ldGFkYXRhIiwiQm94IiwiVGV4dCIsImdldEhvb2tEaXNwbGF5VGV4dCIsImhvb2tTb3VyY2VIZWFkZXJEaXNwbGF5U3RyaW5nIiwiSW5kaXZpZHVhbEhvb2tDb25maWciLCJTZWxlY3QiLCJEaWFsb2ciLCJQcm9wcyIsInNlbGVjdGVkRXZlbnQiLCJzZWxlY3RlZE1hdGNoZXIiLCJob29rc0ZvclNlbGVjdGVkTWF0Y2hlciIsImhvb2tFdmVudE1ldGFkYXRhIiwib25TZWxlY3QiLCJob29rIiwib25DYW5jZWwiLCJTZWxlY3RIb29rTW9kZSIsInQwIiwiJCIsIl9jIiwidGl0bGUiLCJtYXRjaGVyTWV0YWRhdGEiLCJ1bmRlZmluZWQiLCJsZW5ndGgiLCJ0MSIsIlN5bWJvbCIsImZvciIsInQyIiwiZGVzY3JpcHRpb24iLCJfdGVtcCIsIm1hcCIsIl90ZW1wMiIsInQzIiwidmFsdWUiLCJpbmRleF8wIiwicGFyc2VJbnQiLCJob29rXzAiLCJpbmRleCIsInQ0IiwidDUiLCJsYWJlbCIsImNvbmZpZyIsInR5cGUiLCJ0b1N0cmluZyIsInNvdXJjZSIsInBsdWdpbk5hbWUiXSwic291cmNlcyI6WyJTZWxlY3RIb29rTW9kZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiLyoqXG4gKiBTZWxlY3RIb29rTW9kZSBzaG93cyBhbGwgaG9va3MgY29uZmlndXJlZCBmb3IgYSBnaXZlbiBldmVudCttYXRjaGVyIHBhaXIuXG4gKlxuICogVGhlIC9ob29rcyBtZW51IGlzIHJlYWQtb25seTogdGhpcyB2aWV3IG5vIGxvbmdlciBvZmZlcnMgXCJhZGQgbmV3IGhv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ZGUuXG4gICAgICAgICAgPC9UZXh0PlxuICAgICAgICA8L0JveD5cbiAgICAgIDwvRGlhbG9nPlxuICAgIClcbiAgfVxuXG4gIHJldHVybiAoXG4gICAgPERpYWxvZ1xuICAgICAgdGl0bGU9e3RpdGxlfVxuICAgICAgc3VidGl0bGU9e2hvb2tFdmVudE1ldGFkYXRhLmRlc2NyaXB0aW9ufVxuICAgICAgb25DYW5jZWw9e29uQ2FuY2VsfVxuICAgID5cbiAgICAgIDxCb3ggZmxleERpcmVjdGlvbj1cImNvbHVtblwiPlxuICAgICAgICA8U2VsZWN0XG4gICAgICAgICAgb3B0aW9ucz17aG9va3NGb3JTZWxlY3RlZE1hdGNoZXIubWFwKChob29rLCBpbmRleCkgPT4gKHtcbiAgICAgICAgICAgIGxhYmVsOiBgWyR7aG9vay5jb25maWcudHlwZX1dICR7Z2V0SG9va0Rpc3BsYXlUZXh0KGhvb2suY29uZmlnKX1gLFxuICAgICAgICAgICAgdmFsdWU6IGluZGV4LnRvU3RyaW5nKCksXG4gICAgICAgICAgICBkZXNjcmlwdGlvbjpcbiAgICAgICAgICAgICAgaG9vay5zb3VyY2UgPT09ICdwbHVnaW5Ib29rJyAmJiBob29rLnBsdWdpbk5hbWVcbiAgICAgICAgICAgICAgICA/IGAke2hvb2tTb3VyY2VIZWFkZXJEaXNwbGF5U3RyaW5nKGhvb2suc291cmNlKX0gKCR7aG9vay5wbHVnaW5OYW1lfSlgXG4gICAgICAgICAgICAgICAgOiBob29rU291cmNlSGVhZGVyRGlzcGxheVN0cmluZyhob29rLnNvdXJjZSksXG4gICAgICAgICAgfSkpfVxuICAgICAgICAgIG9uQ2hhbmdlPXt2YWx1ZSA9PiB7XG4gICAgICAgICAgICBjb25zdCBpbmRleCA9IHBhcnNlSW50KHZhbHVlLCAxMClcbiAgICAgICAgICAgIGNvbnN0IGhvb2sgPSBob29rc0ZvclNlbGVjdGVkTWF0Y2hlcltpbmRleF1cbiAgICAgICAgICAgIGlmIChob29rKSB7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xPQXpCVHFCLEVBeUJTO0FBQUE7QUF6RE4sU0FBQVIsT0FBQWpCLElBQUEsRUFBQXVCLEtBQUE7RUFBQSxPQXVDMEQ7SUFBQUcsS0FBQSxFQUM5QyxJQUFJMUIsSUFBSSxDQUFBMkIsTUFBTyxDQUFBQyxJQUFLLEtBQUt2QyxrQkFBa0IsQ0FBQ1csSUFBSSxDQUFBMkIsTUFBTyxDQUFDLEVBQUU7SUFBQVIsS0FBQSxFQUMxREksS0FBSyxDQUFBTSxRQUFTLENBQUMsQ0FBQztJQUFBZixXQUFBLEVBRXJCZCxJQUFJLENBQUE4QixNQUFPLEtBQUssWUFBK0IsSUFBZjlCLElBQUksQ0FBQStCLFVBRVUsR0FGOUMsR0FDT3pDLDZCQUE2QixDQUFDVSxJQUFJLENBQUE4QixNQUFPLENBQUMsS0FBSzlCLElBQUksQ0FBQStCLFVBQVcsR0FDdkIsR0FBMUN6Qyw2QkFBNkIsQ0FBQ1UsSUFBSSxDQUFBOEIsTUFBTztFQUNqRCxDQUFDO0FBQUE7QUE5Q0osU0FBQWYsTUFBQTtFQUFBLE9BbUJtQixDQUFDLElBQUksQ0FBQyxjQUFjLEVBQW5CLElBQUksQ0FBc0I7QUFBQSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/hooks/SelectMatcherMode.tsx b/_all-in-one-upload/cc-haha/src/components/hooks/SelectMatcherMode.tsx new file mode 100644 index 0000000..d292fc8 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/hooks/SelectMatcherMode.tsx @@ -0,0 +1,144 @@ +import { c as _c } from "react/compiler-runtime"; +/** + * SelectMatcherMode shows the configured matchers for a selected hook event. + * + * The /hooks menu is read-only: this view no longer offers "add new matcher" + * and simply lets the user drill into each matcher to see its hooks. + */ +import * as React from 'react'; +import type { HookEvent } from 'src/entrypoints/agentSdkTypes.js'; +import { Box, Text } from '../../ink.js'; +import { type HookSource, hookSourceInlineDisplayString, type IndividualHookConfig } from '../../utils/hooks/hooksSettings.js'; +import { plural } from '../../utils/stringUtils.js'; +import { Select } from '../CustomSelect/select.js'; +import { Dialog } from '../design-system/Dialog.js'; +type MatcherWithSource = { + matcher: string; + sources: HookSource[]; + hookCount: number; +}; +type Props = { + selectedEvent: HookEvent; + matchersForSelectedEvent: string[]; + hooksByEventAndMatcher: Record>; + eventDescription: string; + onSelect: (matcher: string) => void; + onCancel: () => void; +}; +export function SelectMatcherMode(t0) { + const $ = _c(25); + const { + selectedEvent, + matchersForSelectedEvent, + hooksByEventAndMatcher, + eventDescription, + onSelect, + onCancel + } = t0; + let t1; + if ($[0] !== hooksByEventAndMatcher || $[1] !== matchersForSelectedEvent || $[2] !== selectedEvent) { + let t2; + if ($[4] !== hooksByEventAndMatcher || $[5] !== selectedEvent) { + t2 = matcher => { + const hooks = hooksByEventAndMatcher[selectedEvent]?.[matcher] || []; + const sources = Array.from(new Set(hooks.map(_temp))); + return { + matcher, + sources, + hookCount: hooks.length + }; + }; + $[4] = hooksByEventAndMatcher; + $[5] = selectedEvent; + $[6] = t2; + } else { + t2 = $[6]; + } + t1 = matchersForSelectedEvent.map(t2); + $[0] = hooksByEventAndMatcher; + $[1] = matchersForSelectedEvent; + $[2] = selectedEvent; + $[3] = t1; + } else { + t1 = $[3]; + } + const matchersWithSources = t1; + if (matchersForSelectedEvent.length === 0) { + const t2 = `${selectedEvent} - Matchers`; + let t3; + if ($[7] === Symbol.for("react.memo_cache_sentinel")) { + t3 = No hooks configured for this event.To add hooks, edit settings.json directly or ask Claude.; + $[7] = t3; + } else { + t3 = $[7]; + } + let t4; + if ($[8] !== eventDescription || $[9] !== onCancel || $[10] !== t2) { + t4 = {t3}; + $[8] = eventDescription; + $[9] = onCancel; + $[10] = t2; + $[11] = t4; + } else { + t4 = $[11]; + } + return t4; + } + const t2 = `${selectedEvent} - Matchers`; + let t3; + if ($[12] !== matchersWithSources) { + t3 = matchersWithSources.map(_temp3); + $[12] = matchersWithSources; + $[13] = t3; + } else { + t3 = $[13]; + } + let t4; + if ($[14] !== onSelect) { + t4 = value => { + onSelect(value); + }; + $[14] = onSelect; + $[15] = t4; + } else { + t4 = $[15]; + } + let t5; + if ($[16] !== onCancel || $[17] !== t3 || $[18] !== t4) { + t5 = ({ + ...o, + disabled: true + })) : options : options} isDisabled={feature('BASH_CLASSIFIER') ? toolUseConfirm.classifierAutoApproved : false} inlineDescriptions onChange={onSelect} onCancel={() => handleReject()} onFocus={handleFocus} onInputModeToggle={handleInputModeToggle} /> + + + + Esc to cancel + {(focusedOption === 'yes' && !yesInputMode || focusedOption === 'no' && !noInputMode) && ' · Tab to amend'} + {explainerState.enabled && ` · ctrl+e to ${explainerState.visible ? 'hide' : 'explain'}`} + + {toolUseContext.options.debug && Ctrl+d to show debug info} + + } + ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJmZWF0dXJlIiwiZmlndXJlcyIsIlJlYWN0IiwidXNlQ2FsbGJhY2siLCJ1c2VFZmZlY3QiLCJ1c2VNZW1vIiwidXNlUmVmIiwidXNlU3RhdGUiLCJCb3giLCJUZXh0IiwidXNlVGhlbWUiLCJ1c2VLZXliaW5kaW5nIiwiZ2V0RmVhdHVyZVZhbHVlX0NBQ0hFRF9NQVlfQkVfU1RBTEUiLCJBbmFseXRpY3NNZXRhZGF0YV9JX1ZFUklGSUVEX1RISVNfSVNfTk9UX0NPREVfT1JfRklMRVBBVEhTIiwibG9nRXZlbnQiLCJzYW5pdGl6ZVRvb2xOYW1lRm9yQW5hbHl0aWNzIiwidXNlQXBwU3RhdGUiLCJCYXNoVG9vbCIsImdldEZpcnN0V29yZFByZWZpeCIsImdldFNpbXBsZUNvbW1hbmRQcmVmaXgiLCJnZXREZXN0cnVjdGl2ZUNvbW1hbmRXYXJuaW5nIiwicGFyc2VTZWRFZGl0Q29tbWFuZCIsInNob3VsZFVzZVNhbmRib3giLCJnZXRDb21wb3VuZENvbW1hbmR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5lclN0YXRlLnZpc2libGUgJiYgKFxuICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPnt0b29sVXNlQ29uZmlybS5kZXNjcmlwdGlvbn08L1RleHQ+XG4gICAgICAgICl9XG4gICAgICAgIDxQZXJtaXNzaW9uRXhwbGFpbmVyQ29udGVudFxuICAgICAgICAgIHZpc2libGU9e2V4cGxhaW5lclN0YXRlLnZpc2libGV9XG4gICAgICAgICAgcHJvbWlzZT17ZXhwbGFpbmVyU3RhdGUucHJvbWlzZX1cbiAgICAgICAgLz5cbiAgICAgIDwvQm94PlxuICAgICAge3Nob3dQZXJtaXNzaW9uRGVidWcgPyAoXG4gICAgICAgIDw+XG4gICAgICAgICAgPFBlcm1pc3Npb25EZWNpc2lvbkRlYnVnSW5mb1xuICAgICAgICAgICAgcGVybWlzc2lvblJlc3VsdD17dG9vbFVzZUNvbmZpcm0ucGVybWlzc2lvblJlc3VsdH1cbiAgICAgICAgICAgIHRvb2xOYW1lPVwiQmFzaFwiXG4gICAgICAgICAgLz5cbiAgICAgICAgICB7dG9vbFVzZUNvbnRleHQub3B0aW9ucy5kZWJ1ZyAmJiAoXG4gICAgICAgICAgICA8Qm94IGp1c3RpZnlDb250ZW50PVwiZmxleC1lbmRcIiBtYXJnaW5Ub3A9ezF9PlxuICAgICAgICAgICAgICA8VGV4dCBkaW1Db2xvcj5DdHJsLUQgdG8gaGlkZSBkZWJ1ZyBpbmZvPC9UZXh0PlxuICAgICAgICAgICAgPC9Cb3g+XG4gICAgICAgICAgKX1cbiAgICAgICAgPC8+XG4gICAgICApIDogKFxuICAgICAgICA8PlxuICAgICAgICAgIDxCb3ggZmxleERpcmVjdGlvbj1cImNvbHVtblwiPlxuICAgICAgICAgICAgPFBlcm1pc3Npb25SdWxlRXhwbGFuYXRpb25cbiAgICAgICAgICAgICAgcGVybWlzc2lvblJlc3VsdD17dG9vbFVzZUNvbmZpcm0ucGVybWlzc2lvblJlc3VsdH1cbiAgICAgICAgICAgICAgdG9vbFR5cGU9XCJjb21tYW5kXCJcbiAgICAgICAgICAgIC8+XG4gICAgICAgICAgICB7ZGVzdHJ1Y3RpdmVXYXJuaW5nICYmIChcbiAgICAgICAgICAgICAgPEJveCBtYXJnaW5Cb3R0b209ezF9PlxuICAgICAgICAgICAgICAgIDxUZXh0XG4gICAgICAgICAgICAgICAgICBjb2xvcj1cIndhcm5pbmdcIlxuICAgICAgICAgICAgICAgICAgZGltQ29sb3I9e1xuICAgICAgICAgICAgICAgICAgICBmZWF0dXJlKCdCQVNIX0NMQVNTSUZJRVInKVxuICAgICAgICAgICAgICAgICAgICAgID8gdG9vbFVzZUNvbmZpcm0uY2xhc3NpZmllckF1dG9BcHByb3ZlZFxuICAgICAgICAgICAgICAgICAgICAgIDogZmFsc2VcbiAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICA+XG4gICAgICAgICAgICAgICAgICB7ZGVzdHJ1Y3RpdmVXYXJuaW5nfVxuICAgICAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgICAgPC9Cb3g+XG4gICAgICAgICAgICApfVxuICAgICAgICAgICAgPFRleHRcbiAgICAgICAgICAgICAgZGltQ29sb3I9e1xuICAgICAgICAgICAgICAgIGZlYXR1cmUoJ0JBU0hfQ0xBU1NJRklFUicpXG4gICAgICAgICAgICAgICAgICA/IHRvb2xVc2VDb25maXJtLmNsYXNzaWZpZXJBdXRvQXBwcm92ZWRcbiAgICAgICAgICAgICAgICAgIDogZmFsc2VcbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgPlxuICAgICAgICAgICAgICBEbyB5b3Ugd2FudCB0byBwcm9jZWVkP1xuICAgICAgICAgICAgPC9UZXh0PlxuICAgICAgICAgICAgPFNlbGVjdFxuICAgICAgICAgICAgICBvcHRpb25zPXtcbiAgICAgICAgICAgICAgICBmZWF0dXJlKCdCQVNIX0NMQVNTSUZJRVInKVxuICAgICAgICAgICAgICAgICAgPyB0b29sVXNlQ29uZmlybS5jbGFzc2lmaWVyQXV0b0FwcHJvdmVkXG4gICAgICAgICAgICAgICAgICAgID8gb3B0aW9ucy5tYXAobyA9PiAoeyAuLi5vLCBkaXNhYmxlZDogdHJ1ZSB9KSlcbiAgICAgICAgICAgICAgICAgICAgOiBvcHRpb25zXG4gICAgICAgICAgICAgICAgICA6IG9wdGlvbnNcbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICBpc0Rpc2FibGVkPXtcbiAgICAgICAgICAgICAgICBmZWF0dXJlKCdCQVNIX0NMQVNTSUZJRVInKVxuICAgICAgICAgICAgICAgICAgPyB0b29sVXNlQ29uZmlybS5jbGFzc2lmaWVyQXV0b0FwcHJvdmVkXG4gICAgICAgICAgICAgICAgICA6IGZhbHNlXG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgaW5saW5lRGVzY3JpcHRpb25zXG4gICAgICAgICAgICAgIG9uQ2hhbmdlPXtvblNlbGVjdH1cbiAgICAgICAgICAgICAgb25DYW5jZWw9eygpID0+IGhhbmRsZVJlamVjdCgpfVxuICAgICAgICAgICAgICBvbkZvY3VzPXtoYW5kbGVGb2N1c31cbiAgICAgICAgICAgICAgb25JbnB1dE1vZGVUb2dnbGU9e2hhbmRsZUlucHV0TW9kZVRvZ2dsZX1cbiAgICAgICAgICAgIC8+XG4gICAgICAgICAgPC9Cb3g+XG4gICAgICAgICAgPEJveCBqdXN0aWZ5Q29udGVudD1cInNwYWNlLWJldHdlZW5cIiBtYXJnaW5Ub3A9ezF9PlxuICAgICAgICAgICAgPFRleHQgZGltQ29sb3I+XG4gICAgICAgICAgICAgIEVzYyB0byBjYW5jZWxcbiAgICAgICAgICAgICAgeygoZm9jdXNlZE9wdGlvbiA9PT0gJ3llcycgJiYgIXllc0lucHV0TW9kZSkgfHxcbiAgICAgICAgICAgICAgICAoZm9jdXNlZE9wdGlvbiA9PT0gJ25vJyAmJiAhbm9JbnB1dE1vZGUpKSAmJlxuICAgICAgICAgICAgICAgICcgwrcgVGFiIHRvIGFtZW5kJ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 No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/permissions/FilePermissionDialog/useFilePermissionDialog.ts b/_all-in-one-upload/cc-haha/src/components/permissions/FilePermissionDialog/useFilePermissionDialog.ts new file mode 100644 index 0000000..3272c96 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/permissions/FilePermissionDialog/useFilePermissionDialog.ts @@ -0,0 +1,212 @@ +import { useCallback, useMemo, useState } from 'react' +import { useAppState } from 'src/state/AppState.js' +import { useKeybindings } from '../../../keybindings/useKeybinding.js' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../../../services/analytics/index.js' +import { sanitizeToolNameForAnalytics } from '../../../services/analytics/metadata.js' +import type { PermissionUpdate } from '../../../utils/permissions/PermissionUpdateSchema.js' +import type { CompletionType } from '../../../utils/unaryLogging.js' +import type { ToolUseConfirm } from '../PermissionRequest.js' +import { + type FileOperationType, + getFilePermissionOptions, + type PermissionOption, + type PermissionOptionWithLabel, +} from './permissionOptions.js' +import { + PERMISSION_HANDLERS, + type PermissionHandlerParams, +} from './usePermissionHandler.js' + +export interface ToolInput { + [key: string]: unknown +} + +export type UseFilePermissionDialogProps = { + filePath: string + completionType: CompletionType + languageName: string | Promise + toolUseConfirm: ToolUseConfirm + onDone: () => void + onReject: () => void + parseInput: (input: unknown) => T + operationType?: FileOperationType +} + +export type UseFilePermissionDialogResult = { + options: PermissionOptionWithLabel[] + onChange: (option: PermissionOption, input: T, feedback?: string) => void + acceptFeedback: string + rejectFeedback: string + focusedOption: string + setFocusedOption: (option: string) => void + handleInputModeToggle: (value: string) => void + yesInputMode: boolean + noInputMode: boolean +} + +/** + * Hook for handling file permission dialogs with common logic + */ +export function useFilePermissionDialog({ + filePath, + completionType, + languageName, + toolUseConfirm, + onDone, + onReject, + parseInput, + operationType = 'write', +}: UseFilePermissionDialogProps): UseFilePermissionDialogResult { + const toolPermissionContext = useAppState(s => s.toolPermissionContext) + const [acceptFeedback, setAcceptFeedback] = useState('') + const [rejectFeedback, setRejectFeedback] = useState('') + const [focusedOption, setFocusedOption] = useState('yes') + const [yesInputMode, setYesInputMode] = useState(false) + const [noInputMode, setNoInputMode] = useState(false) + // Track whether user ever entered feedback mode (persists after collapse) + const [yesFeedbackModeEntered, setYesFeedbackModeEntered] = useState(false) + const [noFeedbackModeEntered, setNoFeedbackModeEntered] = useState(false) + + // Generate options based on context + const options = useMemo( + () => + getFilePermissionOptions({ + filePath, + toolPermissionContext, + operationType, + onRejectFeedbackChange: setRejectFeedback, + onAcceptFeedbackChange: setAcceptFeedback, + yesInputMode, + noInputMode, + }), + [filePath, toolPermissionContext, operationType, yesInputMode, noInputMode], + ) + + // Handle option selection using shared handlers + const onChange = useCallback( + (option: PermissionOption, input: T, feedback?: string) => { + const params: PermissionHandlerParams = { + messageId: toolUseConfirm.assistantMessage.message.id, + path: filePath, + toolUseConfirm, + toolPermissionContext, + onDone, + onReject, + completionType, + languageName, + operationType, + } + + // Override the input in toolUseConfirm to pass the parsed input + const originalOnAllow = toolUseConfirm.onAllow + toolUseConfirm.onAllow = ( + _input: unknown, + permissionUpdates: PermissionUpdate[], + feedback?: string, + ) => { + originalOnAllow(input, permissionUpdates, feedback) + } + + const handler = PERMISSION_HANDLERS[option.type] + handler(params, { + feedback, + hasFeedback: !!feedback, + enteredFeedbackMode: + option.type === 'accept-once' + ? yesFeedbackModeEntered + : noFeedbackModeEntered, + scope: option.type === 'accept-session' ? option.scope : undefined, + }) + }, + [ + filePath, + completionType, + languageName, + toolUseConfirm, + toolPermissionContext, + onDone, + onReject, + operationType, + yesFeedbackModeEntered, + noFeedbackModeEntered, + ], + ) + + // Handler for confirm:cycleMode - select accept-session option + const handleCycleMode = useCallback(() => { + const sessionOption = options.find(o => o.option.type === 'accept-session') + if (sessionOption) { + const parsedInput = parseInput(toolUseConfirm.input) + onChange(sessionOption.option, parsedInput) + } + }, [options, parseInput, toolUseConfirm.input, onChange]) + + // Register keyboard shortcut handler via keybindings system + useKeybindings( + { 'confirm:cycleMode': handleCycleMode }, + { context: 'Confirmation' }, + ) + + // Wrap setFocusedOption and reset input mode when navigating away + const handleFocusedOptionChange = useCallback( + (value: string) => { + // Reset input mode when navigating away, but only if no text typed + if (value !== 'yes' && yesInputMode && !acceptFeedback.trim()) { + setYesInputMode(false) + } + if (value !== 'no' && noInputMode && !rejectFeedback.trim()) { + setNoInputMode(false) + } + setFocusedOption(value) + }, + [yesInputMode, noInputMode, acceptFeedback, rejectFeedback], + ) + + // Handle Tab key toggling input mode for Yes/No options + const handleInputModeToggle = useCallback( + (value: string) => { + const analyticsProps = { + toolName: sanitizeToolNameForAnalytics( + toolUseConfirm.tool.name, + ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + isMcp: toolUseConfirm.tool.isMcp ?? false, + } + + if (value === 'yes') { + if (yesInputMode) { + setYesInputMode(false) + logEvent('tengu_accept_feedback_mode_collapsed', analyticsProps) + } else { + setYesInputMode(true) + setYesFeedbackModeEntered(true) + logEvent('tengu_accept_feedback_mode_entered', analyticsProps) + } + } else if (value === 'no') { + if (noInputMode) { + setNoInputMode(false) + logEvent('tengu_reject_feedback_mode_collapsed', analyticsProps) + } else { + setNoInputMode(true) + setNoFeedbackModeEntered(true) + logEvent('tengu_reject_feedback_mode_entered', analyticsProps) + } + } + }, + [yesInputMode, noInputMode, toolUseConfirm], + ) + + return { + options, + onChange, + acceptFeedback, + rejectFeedback, + focusedOption, + setFocusedOption: handleFocusedOptionChange, + handleInputModeToggle, + yesInputMode, + noInputMode, + } +} diff --git a/_all-in-one-upload/cc-haha/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx b/_all-in-one-upload/cc-haha/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx new file mode 100644 index 0000000..e6275cb --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx @@ -0,0 +1,115 @@ +import { c as _c } from "react/compiler-runtime"; +import React from 'react'; +import { Box, Text, useTheme } from '../../../ink.js'; +import { FallbackPermissionRequest } from '../FallbackPermissionRequest.js'; +import { FilePermissionDialog } from '../FilePermissionDialog/FilePermissionDialog.js'; +import type { ToolInput } from '../FilePermissionDialog/useFilePermissionDialog.js'; +import type { PermissionRequestProps, ToolUseConfirm } from '../PermissionRequest.js'; +function pathFromToolUse(toolUseConfirm: ToolUseConfirm): string | null { + const tool = toolUseConfirm.tool; + if ('getPath' in tool && typeof tool.getPath === 'function') { + try { + return tool.getPath(toolUseConfirm.input); + } catch { + return null; + } + } + return null; +} +export function FilesystemPermissionRequest(t0) { + const $ = _c(30); + const { + toolUseConfirm, + onDone, + onReject, + verbose, + toolUseContext, + workerBadge + } = t0; + const [theme] = useTheme(); + let t1; + if ($[0] !== toolUseConfirm) { + t1 = pathFromToolUse(toolUseConfirm); + $[0] = toolUseConfirm; + $[1] = t1; + } else { + t1 = $[1]; + } + const path = t1; + let t2; + if ($[2] !== toolUseConfirm.input || $[3] !== toolUseConfirm.tool) { + t2 = toolUseConfirm.tool.userFacingName(toolUseConfirm.input as never); + $[2] = toolUseConfirm.input; + $[3] = toolUseConfirm.tool; + $[4] = t2; + } else { + t2 = $[4]; + } + const userFacingName = t2; + const isReadOnly = toolUseConfirm.tool.isReadOnly(toolUseConfirm.input); + const userFacingReadOrEdit = isReadOnly ? "Read" : "Edit"; + const title = `${userFacingReadOrEdit} file`; + const parseInput = _temp; + if (!path) { + let t3; + if ($[5] !== onDone || $[6] !== onReject || $[7] !== toolUseConfirm || $[8] !== toolUseContext || $[9] !== verbose || $[10] !== workerBadge) { + t3 = ; + $[5] = onDone; + $[6] = onReject; + $[7] = toolUseConfirm; + $[8] = toolUseContext; + $[9] = verbose; + $[10] = workerBadge; + $[11] = t3; + } else { + t3 = $[11]; + } + return t3; + } + let t3; + if ($[12] !== theme || $[13] !== toolUseConfirm.input || $[14] !== toolUseConfirm.tool || $[15] !== verbose) { + t3 = toolUseConfirm.tool.renderToolUseMessage(toolUseConfirm.input as never, { + theme, + verbose + }); + $[12] = theme; + $[13] = toolUseConfirm.input; + $[14] = toolUseConfirm.tool; + $[15] = verbose; + $[16] = t3; + } else { + t3 = $[16]; + } + let t4; + if ($[17] !== t3 || $[18] !== userFacingName) { + t4 = {userFacingName}({t3}); + $[17] = t3; + $[18] = userFacingName; + $[19] = t4; + } else { + t4 = $[19]; + } + const content = t4; + const t5 = isReadOnly ? "read" : "write"; + let t6; + if ($[20] !== content || $[21] !== onDone || $[22] !== onReject || $[23] !== path || $[24] !== t5 || $[25] !== title || $[26] !== toolUseConfirm || $[27] !== toolUseContext || $[28] !== workerBadge) { + t6 = ; + $[20] = content; + $[21] = onDone; + $[22] = onReject; + $[23] = path; + $[24] = t5; + $[25] = title; + $[26] = toolUseConfirm; + $[27] = toolUseContext; + $[28] = workerBadge; + $[29] = t6; + } else { + t6 = $[29]; + } + return t6; +} +function _temp(input) { + return input as ToolInput; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkJveCIsIlRleHQiLCJ1c2VUaGVtZSIsIkZhbGxiYWNrUGVybWlzc2lvblJlcXVlc3QiLCJGaWxlUGVybWlzc2lvbkRpYWxvZyIsIlRvb2xJbnB1dCIsIlBlcm1pc3Npb25SZXF1ZXN0UHJvcHMiLCJUb29sVXNlQ29uZmlybSIsInBhdGhGcm9tVG9vbFVzZSIsInRvb2xVc2VDb25maXJtIiwidG9vbCIsImdldFBhdGgiLCJpbnB1dCIsIkZpbGVzeXN0ZW1QZXJtaXNzaW9uUmVxdWVzdCIsInQwIiwiJCIsIl9jIiwib25Eb25lIiwib25SZWplY3QiLCJ2ZXJib3NlIiwidG9vbFVzZUNvbnRleHQiLCJ3b3JrZXJCYWRnZSIsInRoZW1lIiwidDEiLCJwYXRoIiwidDIiLCJ1c2VyRmFjaW5nTmFtZSIsImlzUmVhZE9ubH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QUssY0FBQTtJQUFBTCxDQUFBLE9BQUFNLFdBQUE7SUFBQU4sQ0FBQSxPQUFBc0IsRUFBQTtFQUFBO0lBQUFBLEVBQUEsR0FBQXRCLENBQUE7RUFBQTtFQUFBLE9BWkZzQixFQVlFO0FBQUE7QUFoRUMsU0FBQU4sTUFBQW5CLEtBQUE7RUFBQSxPQXFCNkNBLEtBQUssSUFBSVAsU0FBUztBQUFBIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/permissions/PermissionExplanation.tsx b/_all-in-one-upload/cc-haha/src/components/permissions/PermissionExplanation.tsx new file mode 100644 index 0000000..b7fd36b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/permissions/PermissionExplanation.tsx @@ -0,0 +1,272 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { Suspense, use, useState } from 'react'; +import { Box, Text } from '../../ink.js'; +import { useKeybinding } from '../../keybindings/useKeybinding.js'; +import { logEvent } from '../../services/analytics/index.js'; +import type { Message } from '../../types/message.js'; +import { generatePermissionExplanation, isPermissionExplainerEnabled, type PermissionExplanation as PermissionExplanationType, type RiskLevel } from '../../utils/permissions/permissionExplainer.js'; +import { ShimmerChar } from '../Spinner/ShimmerChar.js'; +import { useShimmerAnimation } from '../Spinner/useShimmerAnimation.js'; +const LOADING_MESSAGE = 'Loading explanation…'; +function ShimmerLoadingText() { + const $ = _c(7); + const [ref, glimmerIndex] = useShimmerAnimation("responding", LOADING_MESSAGE, false); + let t0; + if ($[0] !== glimmerIndex) { + t0 = LOADING_MESSAGE.split("").map((char, index) => ); + $[0] = glimmerIndex; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 = {t0}; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + let t2; + if ($[4] !== ref || $[5] !== t1) { + t2 = {t1}; + $[4] = ref; + $[5] = t1; + $[6] = t2; + } else { + t2 = $[6]; + } + return t2; +} +function getRiskColor(riskLevel: RiskLevel): 'success' | 'warning' | 'error' { + switch (riskLevel) { + case 'LOW': + return 'success'; + case 'MEDIUM': + return 'warning'; + case 'HIGH': + return 'error'; + } +} +function getRiskLabel(riskLevel: RiskLevel): string { + switch (riskLevel) { + case 'LOW': + return 'Low risk'; + case 'MEDIUM': + return 'Med risk'; + case 'HIGH': + return 'High risk'; + } +} +type PermissionExplanationProps = { + toolName: string; + toolInput: unknown; + toolDescription?: string; + messages?: Message[]; +}; +type ExplainerState = { + visible: boolean; + enabled: boolean; + promise: Promise | null; +}; + +/** + * Creates an explanation promise that never rejects. + * Errors are caught and returned as null. + */ +function createExplanationPromise(props: PermissionExplanationProps): Promise { + return generatePermissionExplanation({ + toolName: props.toolName, + toolInput: props.toolInput, + toolDescription: props.toolDescription, + messages: props.messages, + signal: new AbortController().signal // Won't abort - request is fast enough + }).catch(() => null); +} + +/** + * Hook that manages the permission explainer state. + * Creates the fetch promise lazily (only when user hits Ctrl+E) + * to avoid consuming tokens for explanations users never view. + */ +export function usePermissionExplainerUI(props) { + const $ = _c(9); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = isPermissionExplainerEnabled(); + $[0] = t0; + } else { + t0 = $[0]; + } + const enabled = t0; + const [visible, setVisible] = useState(false); + const [promise, setPromise] = useState(null); + let t1; + if ($[1] !== promise || $[2] !== props || $[3] !== visible) { + t1 = () => { + if (!visible) { + logEvent("tengu_permission_explainer_shortcut_used", {}); + if (!promise) { + setPromise(createExplanationPromise(props)); + } + } + setVisible(_temp); + }; + $[1] = promise; + $[2] = props; + $[3] = visible; + $[4] = t1; + } else { + t1 = $[4]; + } + let t2; + if ($[5] === Symbol.for("react.memo_cache_sentinel")) { + t2 = { + context: "Confirmation", + isActive: enabled + }; + $[5] = t2; + } else { + t2 = $[5]; + } + useKeybinding("confirm:toggleExplanation", t1, t2); + let t3; + if ($[6] !== promise || $[7] !== visible) { + t3 = { + visible, + enabled, + promise + }; + $[6] = promise; + $[7] = visible; + $[8] = t3; + } else { + t3 = $[8]; + } + return t3; +} + +/** + * Inner component that uses React 19's use() to read the promise. + * Suspends while loading, returns null on error. + */ +function _temp(v) { + return !v; +} +function ExplanationResult(t0) { + const $ = _c(21); + const { + promise + } = t0; + const explanation = use(promise); + if (!explanation) { + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = Explanation unavailable; + $[0] = t1; + } else { + t1 = $[0]; + } + return t1; + } + let t1; + if ($[1] !== explanation.explanation) { + t1 = {explanation.explanation}; + $[1] = explanation.explanation; + $[2] = t1; + } else { + t1 = $[2]; + } + let t2; + if ($[3] !== explanation.reasoning) { + t2 = {explanation.reasoning}; + $[3] = explanation.reasoning; + $[4] = t2; + } else { + t2 = $[4]; + } + let t3; + if ($[5] !== explanation.riskLevel) { + t3 = getRiskColor(explanation.riskLevel); + $[5] = explanation.riskLevel; + $[6] = t3; + } else { + t3 = $[6]; + } + let t4; + if ($[7] !== explanation.riskLevel) { + t4 = getRiskLabel(explanation.riskLevel); + $[7] = explanation.riskLevel; + $[8] = t4; + } else { + t4 = $[8]; + } + let t5; + if ($[9] !== t3 || $[10] !== t4) { + t5 = {t4}:; + $[9] = t3; + $[10] = t4; + $[11] = t5; + } else { + t5 = $[11]; + } + let t6; + if ($[12] !== explanation.risk) { + t6 = {explanation.risk}; + $[12] = explanation.risk; + $[13] = t6; + } else { + t6 = $[13]; + } + let t7; + if ($[14] !== t5 || $[15] !== t6) { + t7 = {t5}{t6}; + $[14] = t5; + $[15] = t6; + $[16] = t7; + } else { + t7 = $[16]; + } + let t8; + if ($[17] !== t1 || $[18] !== t2 || $[19] !== t7) { + t8 = {t1}{t2}{t7}; + $[17] = t1; + $[18] = t2; + $[19] = t7; + $[20] = t8; + } else { + t8 = $[20]; + } + return t8; +} + +/** + * Content component - shows loading (via Suspense) or explanation when visible + */ +export function PermissionExplainerContent(t0) { + const $ = _c(3); + const { + visible, + promise + } = t0; + if (!visible || !promise) { + return null; + } + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = ; + $[0] = t1; + } else { + t1 = $[0]; + } + let t2; + if ($[1] !== promise) { + t2 = ; + $[1] = promise; + $[2] = t2; + } else { + t2 = $[2]; + } + return t2; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIlN1c3BlbnNlIiwidXNlIiwidXNlU3RhdGUiLCJCb3giLCJUZXh0IiwidXNlS2V5YmluZGluZyIsImxvZ0V2ZW50IiwiTWVzc2FnZSIsImdlbmVyYXRlUGVybWlzc2lvbkV4cGxhbmF0aW9uIiwiaXNQZXJtaXNzaW9uRXhwbGFpbmVyRW5hYmxlZCIsIlBlcm1pc3Npb25FeHBsYW5hdGlvbiIsIlBlcm1pc3Npb25FeHBsYW5hdGlvblR5cGUiLCJSaXNrTGV2ZWwiLCJTaGltbWVyQ2hhciIsInVzZVNoaW1tZXJBbmltYXRpb24iLCJMT0FESU5HX01FU1NBR0UiLCJTaGltbWVyTG9hZGluZ1RleHQiLCIkIiwiX2MiLCJyZWYiLCJnbGltbWVySW5kZXgiLCJ0MCIsInNwbGl0IiwibWFwIiwiY2hhciIsImluZGV4IiwidDEiLCJ0MiIsImdldFJpc2tDb2xvciIsInJpc2tMZXZlbCIsImdldFJpc2tMYWJlbCIsIlBlcm1pc3Npb25FeHBsYW5hdGlvblByb3BzIiwidG9vbE5hbWUiLCJ0b29sSW5wdXQiLCJ0b29sRGVzY3JpcHRpb24iLCJtZXNzYWdlcyIsIkV4cGxhaW5lclN0YXRlIiwidmlzaWJsZSIsImVuYWJsZWQiLCJwcm9taXNlIiwiUHJvbWlzZSIsImNyZWF0ZUV4cGxhbmF0aW9uUHJvbWlzZSIsInByb3BzIiwic2lnbmFsIiwiQWJvcnRDb250cm9sbGVyIiwiY2F0Y2giLCJ1c2VQZXJtaXNzaW9uRXhwbGFpbmVyVUkiLCJTeW1ib2wiLCJmb3IiLCJzZXRWaXNpYmxlI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QUFBLE9BUlhVLEVBUVc7QUFBQSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/permissions/rules/PermissionRuleInput.tsx b/_all-in-one-upload/cc-haha/src/components/permissions/rules/PermissionRuleInput.tsx new file mode 100644 index 0000000..13b7b0b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/permissions/rules/PermissionRuleInput.tsx @@ -0,0 +1,138 @@ +import { c as _c } from "react/compiler-runtime"; +import figures from 'figures'; +import * as React from 'react'; +import { useState } from 'react'; +import TextInput from '../../../components/TextInput.js'; +import { useExitOnCtrlCDWithKeybindings } from '../../../hooks/useExitOnCtrlCDWithKeybindings.js'; +import { useTerminalSize } from '../../../hooks/useTerminalSize.js'; +import { Box, Newline, Text } from '../../../ink.js'; +import { useKeybinding } from '../../../keybindings/useKeybinding.js'; +import { BashTool } from '../../../tools/BashTool/BashTool.js'; +import { WebFetchTool } from '../../../tools/WebFetchTool/WebFetchTool.js'; +import type { PermissionBehavior, PermissionRuleValue } from '../../../utils/permissions/PermissionRule.js'; +import { permissionRuleValueFromString, permissionRuleValueToString } from '../../../utils/permissions/permissionRuleParser.js'; +export type PermissionRuleInputProps = { + onCancel: () => void; + onSubmit: (ruleValue: PermissionRuleValue, ruleBehavior: PermissionBehavior) => void; + ruleBehavior: PermissionBehavior; +}; +export function PermissionRuleInput(t0) { + const $ = _c(24); + const { + onCancel, + onSubmit, + ruleBehavior + } = t0; + const [inputValue, setInputValue] = useState(""); + const [cursorOffset, setCursorOffset] = useState(0); + const exitState = useExitOnCtrlCDWithKeybindings(); + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = { + context: "Settings" + }; + $[0] = t1; + } else { + t1 = $[0]; + } + useKeybinding("confirm:no", onCancel, t1); + const { + columns + } = useTerminalSize(); + const textInputColumns = columns - 6; + let t2; + if ($[1] !== onSubmit || $[2] !== ruleBehavior) { + t2 = value => { + const trimmedValue = value.trim(); + if (trimmedValue.length === 0) { + return; + } + const ruleValue = permissionRuleValueFromString(trimmedValue); + onSubmit(ruleValue, ruleBehavior); + }; + $[1] = onSubmit; + $[2] = ruleBehavior; + $[3] = t2; + } else { + t2 = $[3]; + } + const handleSubmit = t2; + let t3; + if ($[4] !== ruleBehavior) { + t3 = Add {ruleBehavior} permission rule; + $[4] = ruleBehavior; + $[5] = t3; + } else { + t3 = $[5]; + } + let t4; + if ($[6] === Symbol.for("react.memo_cache_sentinel")) { + t4 = ; + $[6] = t4; + } else { + t4 = $[6]; + } + let t5; + let t6; + if ($[7] === Symbol.for("react.memo_cache_sentinel")) { + t5 = {permissionRuleValueToString({ + toolName: WebFetchTool.name + })}; + t6 = or ; + $[7] = t5; + $[8] = t6; + } else { + t5 = $[7]; + t6 = $[8]; + } + let t7; + if ($[9] === Symbol.for("react.memo_cache_sentinel")) { + t7 = Permission rules are a tool name, optionally followed by a specifier in parentheses.{t4}e.g.,{" "}{t5}{t6}{permissionRuleValueToString({ + toolName: BashTool.name, + ruleContent: "ls:*" + })}; + $[9] = t7; + } else { + t7 = $[9]; + } + let t8; + if ($[10] !== cursorOffset || $[11] !== handleSubmit || $[12] !== inputValue || $[13] !== textInputColumns) { + t8 = {t7}; + $[10] = cursorOffset; + $[11] = handleSubmit; + $[12] = inputValue; + $[13] = textInputColumns; + $[14] = t8; + } else { + t8 = $[14]; + } + let t9; + if ($[15] !== t3 || $[16] !== t8) { + t9 = {t3}{t8}; + $[15] = t3; + $[16] = t8; + $[17] = t9; + } else { + t9 = $[17]; + } + let t10; + if ($[18] !== exitState.keyName || $[19] !== exitState.pending) { + t10 = {exitState.pending ? Press {exitState.keyName} again to exit : Enter to submit · Esc to cancel}; + $[18] = exitState.keyName; + $[19] = exitState.pending; + $[20] = t10; + } else { + t10 = $[20]; + } + let t11; + if ($[21] !== t10 || $[22] !== t9) { + t11 = <>{t9}{t10}; + $[21] = t10; + $[22] = t9; + $[23] = t11; + } else { + t11 = $[23]; + } + return t11; +} +//# s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GVWYWx1ZVRvU3RyaW5nKHsgdG9vbE5hbWU6IFdlYkZldGNoVG9vbC5uYW1lIH0pfVxuICAgICAgICAgICAgPC9UZXh0PlxuICAgICAgICAgICAgPFRleHQgYm9sZD17ZmFsc2V9PiBvciA8L1RleHQ+XG4gICAgICAgICAgICA8VGV4dCBib2xkPlxuICAgICAgICAgICAgICB7cGVybWlzc2lvblJ1bGVWYWx1ZVRvU3RyaW5nKHtcbiAgICAgICAgICAgICAgICB0b29sTmFtZTogQmFzaFRvb2wubmFtZSxcbiAgICAgICAgICAgICAgICBydWxlQ29udGVudDogJ2xzOionLFxuICAgICAgICAgICAgICB9KX1cbiAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgPEJveCBib3JkZXJEaW1Db2xvciBib3JkZXJTdHlsZT1cInJvdW5kXCIgbWFyZ2luWT17MX0gcGFkZGluZ0xlZnQ9ezF9PlxuICAgICAgICAgICAgPFRleHRJbnB1dFxuICAgICAgICAgICAgICBzaG93Q3Vyc29yXG4gICAgICAgICAgICAgIHZhbHVlPXtpbnB1dFZhbHVlfVxuICAgICAgICAgICAgICBvbkNoYW5nZT17c2V0SW5wdXRWYWx1ZX1cbiAgICAgICAgICAgICAgb25TdWJtaXQ9e2hhbmRsZVN1Ym1pdH1cbiAgICAgICAgICAgICAgcGxhY2Vob2xkZXI9e2BFbnRlciBwZXJtaXNzaW9uIHJ1bGUke2ZpZ3VyZXMuZWxsaXBzaXN9YH1cbiAgICAgICAgICAgICAgY29sdW1ucz17dGV4dElucHV0Q29sdW1uc31cbiAgICAgICAgICAgICAgY3Vyc29yT2Zmc2V0PXtjdXJzb3JPZmZzZXR9XG4gICAgICAgICAgICAgIG9uQ2hhbmdlQ3Vyc29yT2Zmc2V0PXtzZXRDdXJzb3JPZmZzZXR9XG4gICAgICAgICAgICAvPlxuICAgICAgICAgIDwvQm94PlxuICAgICAgICA8L0JveD5cbiAgICAgIDwvQm94PlxuICAgICAg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EdBQUEsR0FBQWpDLENBQUE7RUFBQTtFQUFBLE9BbERIaUMsR0FrREc7QUFBQSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/sandbox/SandboxDependenciesTab.tsx b/_all-in-one-upload/cc-haha/src/components/sandbox/SandboxDependenciesTab.tsx new file mode 100644 index 0000000..c9ecbc5 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/sandbox/SandboxDependenciesTab.tsx @@ -0,0 +1,120 @@ +import { c as _c } from "react/compiler-runtime"; +import React from 'react'; +import { Box, Text } from '../../ink.js'; +import { getPlatform } from '../../utils/platform.js'; +import type { SandboxDependencyCheck } from '../../utils/sandbox/sandbox-adapter.js'; +type Props = { + depCheck: SandboxDependencyCheck; +}; +export function SandboxDependenciesTab(t0) { + const $ = _c(24); + const { + depCheck + } = t0; + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = getPlatform(); + $[0] = t1; + } else { + t1 = $[0]; + } + const platform = t1; + const isMac = platform === "macos"; + let t2; + if ($[1] !== depCheck.errors) { + t2 = depCheck.errors.some(_temp); + $[1] = depCheck.errors; + $[2] = t2; + } else { + t2 = $[2]; + } + const rgMissing = t2; + let t3; + if ($[3] !== depCheck.errors) { + t3 = depCheck.errors.some(_temp2); + $[3] = depCheck.errors; + $[4] = t3; + } else { + t3 = $[4]; + } + const bwrapMissing = t3; + let t4; + if ($[5] !== depCheck.errors) { + t4 = depCheck.errors.some(_temp3); + $[5] = depCheck.errors; + $[6] = t4; + } else { + t4 = $[6]; + } + const socatMissing = t4; + const seccompMissing = depCheck.warnings.length > 0; + let t5; + if ($[7] !== bwrapMissing || $[8] !== depCheck.errors || $[9] !== rgMissing || $[10] !== seccompMissing || $[11] !== socatMissing) { + const otherErrors = depCheck.errors.filter(_temp4); + const rgInstallHint = isMac ? "brew install ripgrep" : "apt install ripgrep"; + let t6; + if ($[13] === Symbol.for("react.memo_cache_sentinel")) { + t6 = isMac && seatbelt: built-in (macOS); + $[13] = t6; + } else { + t6 = $[13]; + } + let t7; + let t8; + if ($[14] !== rgMissing) { + t7 = ripgrep (rg):{" "}{rgMissing ? not found : found}; + t8 = rgMissing && {" "}· {rgInstallHint}; + $[14] = rgMissing; + $[15] = t7; + $[16] = t8; + } else { + t7 = $[15]; + t8 = $[16]; + } + let t9; + if ($[17] !== t7 || $[18] !== t8) { + t9 = {t7}{t8}; + $[17] = t7; + $[18] = t8; + $[19] = t9; + } else { + t9 = $[19]; + } + let t10; + if ($[20] !== bwrapMissing || $[21] !== seccompMissing || $[22] !== socatMissing) { + t10 = !isMac && <>bubblewrap (bwrap):{" "}{bwrapMissing ? not installed : installed}{bwrapMissing && {" "}· apt install bubblewrap}socat:{" "}{socatMissing ? not installed : installed}{socatMissing && {" "}· apt install socat}seccomp filter:{" "}{seccompMissing ? not installed : installed}{seccompMissing && (required to block unix domain sockets)}{seccompMissing && {" "}· npm install -g @anthropic-ai/sandbox-runtime{" "}· or copy vendor/seccomp/* from sandbox-runtime and set{" "}sandbox.seccomp.bpfPath and applyPath in settings.json}; + $[20] = bwrapMissing; + $[21] = seccompMissing; + $[22] = socatMissing; + $[23] = t10; + } else { + t10 = $[23]; + } + t5 = {t6}{t9}{t10}{otherErrors.map(_temp5)}; + $[7] = bwrapMissing; + $[8] = depCheck.errors; + $[9] = rgMissing; + $[10] = seccompMissing; + $[11] = socatMissing; + $[12] = t5; + } else { + t5 = $[12]; + } + return t5; +} +function _temp5(err) { + return {err}; +} +function _temp4(e_2) { + return !e_2.includes("ripgrep") && !e_2.includes("bwrap") && !e_2.includes("socat"); +} +function _temp3(e_1) { + return e_1.includes("socat"); +} +function _temp2(e_0) { + return e_0.includes("bwrap"); +} +function _temp(e) { + return e.includes("ripgrep"); +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkJveCIsIlRleHQiLCJnZXRQbGF0Zm9ybSIsIlN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gICAgPFRleHQgY29sb3I9XCJzdWNjZXNzXCI+Zm91bmQ8L1RleHQ+XG4gICAgICAgICAgKX1cbiAgICAgICAgPC9UZXh0PlxuICAgICAgICB7cmdNaXNzaW5nICYmIChcbiAgICAgICAgICA8VGV4dCBkaW1Db2xvcj5cbiAgICAgICAgICAgIHsnICAnfcK3IHtyZ0luc3RhbGxIaW50fVxuICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgKX1cbiAgICAgIDwvQm94PlxuXG4gICAgICB7IWlzTWFjICYmIChcbiAgICAgICAgPD5cbiAgICAgICAgICA8Qm94IGZsZXhEaXJlY3Rpb249XCJjb2x1bW5cIj5cbiAgICAgICAgICAgIDxUZXh0PlxuICAgICAgICAgICAgICBidWJibGV3cmFwIChid3JhcCk6eycgJ31cbiAgICAgICAgICAgICAge2J3cmFwTWlzc2luZyA/IChcbiAgICAgICAgICAgICAgICA8VGV4dCBjb2xvcj1cImVycm9yXCI+bm90IGluc3RhbGxlZDwvVGV4dD5cbiAgICAgICAgICAgICAgKSA6IChcbiAgICAgICAgICAgICAgICA8VGV4dCBjb2xvcj1cInN1Y2Nlc3NcIj5pbnN0YWxsZWQ8L1RleHQ+XG4gICAgICAgICAgICAgICl9XG4gICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICB7YndyYXBNaXNzaW5nICYmIChcbiAgICAgICAgICAgICAgPFRleHQgZGltQ29sb3I+eycgICd9wrcgYXB0IGluc3RhbGwgYnViYmxld3JhcDwvVGV4dD5cbiAgICAgICAgICAgICl9XG4gICAgICAgICAgPC9Cb3g+XG5cbiAgICAgICAgICA8Qm94IGZsZXhEaXJlY3Rpb249XCJjb2x1bW5cIj5cbiAgICAgICAgICAgIDxUZXh0PlxuICAgICAgICAgICAgICBzb2NhdDp7JyAnfVxuICAgICAgICAgICAgICB7c29jYXRNaXNzaW5nID8gKFxuICAgICAgICAgICAgICAgIDxUZXh0IGNvbG9yPVwiZXJyb3JcIj5ub3QgaW5zdGFsbGVkPC9UZXh0PlxuICAgICAgICAgICAgICApIDogKFxuICAgICAgICAgICAgICAgIDxUZXh0IGNvbG9yPVwic3VjY2Vzc1wiPmluc3RhbGxlZDwvVGV4dD5cbiAgICAgICAgICAgICAgKX1cbiAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgIHtzb2NhdE1pc3NpbmcgJiYgPFRleHQgZGltQ29sb3I+eycgICd9wrcgYXB0IGluc3RhbGwgc29jYXQ8L1RleHQ+fVxuICAgICAgICAgIDwvQm94PlxuXG4gICAgICAgICAgPEJveCBmbGV4RGlyZWN0aW9uPVwiY29sdW1uXCI+XG4gICAgICAgICAgICA8VGV4dD5cbiAgICAgICAgICAgICAgc2VjY29tcCBmaWx0ZXI6eycgJ31cbiAgICAgICAgICAgICAge3NlY2NvbXBNaXNzaW5nID8gKFxuICAgICAgICAgICAgICAgIDxUZXh0IGNvbG9yPVwid2FybmluZ1wiPm5vdCBpbnN0YWxsZWQ8L1RleHQ+XG4gICAgICAgICAgICAgICkgOiAoXG4gICAgICAgICAgICAgICAgPFRleHQgY29sb3I9XCJzdWNjZXNzXCI+aW5zdGFsbGVkPC9UZXh0PlxuICAgICAgICAgICAgICApfVxuICAgICAgICAgICAgICB7c2VjY29tcE1pc3NpbmcgJiYgKFxuICAgICAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPiAocmVxdWlyZWQgdG8gYmxvY2sgdW5peCBkb21haW4gc29ja2V0cyk8L1RleHQ+XG4gICAgICAgICAgICAgICl9XG4gICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICB7c2VjY29tcE1pc3NpbmcgJiYgKFxuICAgICAgICAgICAgICA8Qm94IGZsZXhEaXJlY3Rpb249XCJjb2x1bW5cIj5cbiAgICAgICAgICAgICAgICA8VGV4dCBkaW1Db2xvcj5cbiAgICAgICAgICAgICAgICAgIHsnICAnfcK3IG5wbSBpbnN0YWxsIC1nIEBhbnRocm9waWMtYWkvc2FuZGJveC1ydW50aW1lXG4gICAgICA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N0IjpbXX0= \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/scheduled-tasks/types.ts b/_all-in-one-upload/cc-haha/src/components/scheduled-tasks/types.ts new file mode 100644 index 0000000..8f6034a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/scheduled-tasks/types.ts @@ -0,0 +1,12 @@ +export type ScheduledTaskWizardData = { + name?: string + description?: string + prompt?: string + model?: string + permissionMode?: string + folder?: string + worktree?: boolean + frequency?: string + scheduledTime?: string + cron?: string +} diff --git a/_all-in-one-upload/cc-haha/src/components/shell/ExpandShellOutputContext.tsx b/_all-in-one-upload/cc-haha/src/components/shell/ExpandShellOutputContext.tsx new file mode 100644 index 0000000..2452208 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/shell/ExpandShellOutputContext.tsx @@ -0,0 +1,36 @@ +import { c as _c } from "react/compiler-runtime"; +import * as React from 'react'; +import { useContext } from 'react'; + +/** + * Context to indicate that shell output should be shown in full (not truncated). + * Used to auto-expand the most recent user `!` command output. + * + * This follows the same pattern as MessageResponseContext and SubAgentContext - + * a boolean context that child components can check to modify their behavior. + */ +const ExpandShellOutputContext = React.createContext(false); +export function ExpandShellOutputProvider(t0) { + const $ = _c(2); + const { + children + } = t0; + let t1; + if ($[0] !== children) { + t1 = {children}; + $[0] = children; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +/** + * Returns true if this component is rendered inside an ExpandShellOutputProvider, + * indicating the shell output should be shown in full rather than truncated. + */ +export function useExpandShellOutput() { + return useContext(ExpandShellOutputContext); +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsInVzZUNvbnRleHQiLCJFeHBhbmRTaGVsbE91dHB1dENvbnRleHQiLCJjcmVhdGVDb250ZXh0IiwiRXhwYW5kU2hlbGxPdXRwdXRQcm92aWRlciIsInQwIiwiJCIsIl9jIiwiY2hpbGRyZW4iLCJ0MSIsInVzZUV4cGFuZF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0Msd0JBQXdCLENBQUM7QUFBQSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/components/wizard/index.ts b/_all-in-one-upload/cc-haha/src/components/wizard/index.ts new file mode 100644 index 0000000..29a4c8e --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/components/wizard/index.ts @@ -0,0 +1,9 @@ +export type { + WizardContextValue, + WizardProviderProps, + WizardStepComponent, +} from './types.js' +export { useWizard } from './useWizard.js' +export { WizardDialogLayout } from './WizardDialogLayout.js' +export { WizardNavigationFooter } from './WizardNavigationFooter.js' +export { WizardProvider } from './WizardProvider.js' diff --git a/_all-in-one-upload/cc-haha/src/constants/github-app.ts b/_all-in-one-upload/cc-haha/src/constants/github-app.ts new file mode 100644 index 0000000..1df9f51 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/constants/github-app.ts @@ -0,0 +1,144 @@ +export const PR_TITLE = 'Add Claude Code GitHub Workflow' + +export const GITHUB_ACTION_SETUP_DOCS_URL = + 'https://github.com/anthropics/claude-code-action/blob/main/docs/setup.md' + +export const WORKFLOW_CONTENT = `name: Claude Code + +on: + issue_comment: + types: [created] + pull_request_review_comment: + types: [created] + issues: + types: [opened, assigned] + pull_request_review: + types: [submitted] + +jobs: + claude: + if: | + (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) || + (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) || + (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) || + (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude'))) + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + issues: read + id-token: write + actions: read # Required for Claude to read CI results on PRs + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run Claude Code + id: claude + uses: anthropics/claude-code-action@v1 + with: + anthropic_api_key: \${{ secrets.ANTHROPIC_API_KEY }} + + # This is an optional setting that allows Claude to read CI results on PRs + additional_permissions: | + actions: read + + # Optional: Give a custom prompt to Claude. If this is not specified, Claude will perform the instructions specified in the comment that tagged it. + # prompt: 'Update the pull request description to include a summary of changes.' + + # Optional: Add claude_args to customize behavior and configuration + # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md + # or https://code.claude.com/docs/en/cli-reference for available options + # claude_args: '--allowed-tools Bash(gh pr:*)' + +` + +export const PR_BODY = `## 🤖 Installing Claude Code GitHub App + +This PR adds a GitHub Actions workflow that enables Claude Code integration in our repository. + +### What is Claude Code? + +[Claude Code](https://claude.com/claude-code) is an AI coding agent that can help with: +- Bug fixes and improvements +- Documentation updates +- Implementing new features +- Code reviews and suggestions +- Writing tests +- And more! + +### How it works + +Once this PR is merged, we'll be able to interact with Claude by mentioning @claude in a pull request or issue comment. +Once the workflow is triggered, Claude will analyze the comment and surrounding context, and execute on the request in a GitHub action. + +### Important Notes + +- **This workflow won't take effect until this PR is merged** +- **@claude mentions won't work until after the merge is complete** +- The workflow runs automatically whenever Claude is mentioned in PR or issue comments +- Claude gets access to the entire PR or issue context including files, diffs, and previous comments + +### Security + +- Our Anthropic API key is securely stored as a GitHub Actions secret +- Only users with write access to the repository can trigger the workflow +- All Claude runs are stored in the GitHub Actions run history +- Claude's default tools are limited to reading/writing files and interacting with our repo by creating comments, branches, and commits. +- We can add more allowed tools by adding them to the workflow file like: + +\`\`\` +allowed_tools: Bash(npm install),Bash(npm run build),Bash(npm run lint),Bash(npm run test) +\`\`\` + +There's more information in the [Claude Code action repo](https://github.com/anthropics/claude-code-action). + +After merging this PR, let's try mentioning @claude in a comment on any PR to get started!` + +export const CODE_REVIEW_PLUGIN_WORKFLOW_CONTENT = `name: Claude Code Review + +on: + pull_request: + types: [opened, synchronize, ready_for_review, reopened] + # Optional: Only run on specific file changes + # paths: + # - "src/**/*.ts" + # - "src/**/*.tsx" + # - "src/**/*.js" + # - "src/**/*.jsx" + +jobs: + claude-review: + # Optional: Filter by PR author + # if: | + # github.event.pull_request.user.login == 'external-contributor' || + # github.event.pull_request.user.login == 'new-developer' || + # github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR' + + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + issues: read + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run Claude Code Review + id: claude-review + uses: anthropics/claude-code-action@v1 + with: + anthropic_api_key: \${{ secrets.ANTHROPIC_API_KEY }} + plugin_marketplaces: 'https://github.com/anthropics/claude-code.git' + plugins: 'code-review@claude-code-plugins' + prompt: '/code-review:code-review \${{ github.repository }}/pull/\${{ github.event.pull_request.number }}' + # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md + # or https://code.claude.com/docs/en/cli-reference for available options + +` diff --git a/_all-in-one-upload/cc-haha/src/context/voice.tsx b/_all-in-one-upload/cc-haha/src/context/voice.tsx new file mode 100644 index 0000000..04e14dd --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/context/voice.tsx @@ -0,0 +1,88 @@ +import { c as _c } from "react/compiler-runtime"; +import React, { createContext, useContext, useState, useSyncExternalStore } from 'react'; +import { createStore, type Store } from '../state/store.js'; +export type VoiceState = { + voiceState: 'idle' | 'recording' | 'processing'; + voiceError: string | null; + voiceInterimTranscript: string; + voiceAudioLevels: number[]; + voiceWarmingUp: boolean; +}; +const DEFAULT_STATE: VoiceState = { + voiceState: 'idle', + voiceError: null, + voiceInterimTranscript: '', + voiceAudioLevels: [], + voiceWarmingUp: false +}; +type VoiceStore = Store; +const VoiceContext = createContext(null); +type Props = { + children: React.ReactNode; +}; +export function VoiceProvider(t0) { + const $ = _c(3); + const { + children + } = t0; + const [store] = useState(_temp); + let t1; + if ($[0] !== children || $[1] !== store) { + t1 = {children}; + $[0] = children; + $[1] = store; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} +function _temp() { + return createStore(DEFAULT_STATE); +} +function useVoiceStore() { + const store = useContext(VoiceContext); + if (!store) { + throw new Error("useVoiceState must be used within a VoiceProvider"); + } + return store; +} + +/** + * Subscribe to a slice of voice state. Only re-renders when the selected + * value changes (compared via Object.is). + */ +export function useVoiceState(selector) { + const $ = _c(3); + const store = useVoiceStore(); + let t0; + if ($[0] !== selector || $[1] !== store) { + t0 = () => selector(store.getState()); + $[0] = selector; + $[1] = store; + $[2] = t0; + } else { + t0 = $[2]; + } + const get = t0; + return useSyncExternalStore(store.subscribe, get, get); +} + +/** + * Get the voice state setter. Stable reference — never causes re-renders. + * store.setState is synchronous: callers can read getVoiceState() immediately + * after to observe the new value (VoiceKeybindingHandler relies on this). + */ +export function useSetVoiceState() { + return useVoiceStore().setState; +} + +/** + * Get a synchronous reader for fresh state inside callbacks. Unlike + * useVoiceState (which subscribes), this doesn't cause re-renders — use + * inside event handlers that need to read state set earlier in the same tick. + */ +export function useGetVoiceState() { + return useVoiceStore().getState; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsImNyZWF0ZUNvbnRleHQiLCJ1c2VDb250ZXh0IiwidXNlU3RhdGUiLCJ1c2VTeW5jRXh0ZXJuYWxTdG9yZSIsImNyZWF0ZVN0b3JlIiwiU3RvcmUiLCJWb2ljZVN0YXRlIiwidm9pY2VTdGF0ZSIsInZvaWNlRXJyb3IiLCJ2b2ljZUludGVyaW1UcmFuc2NyaXB0Iiwidm9pY2VBdWRpb0xldmVscyIsInZvaWNlV2FybWluZ1VwIiwiREVGQVVMVF9TVEFURSIsIlZvaWNlU3RvcmUiLCJWb2ljZUNvbnRleHQiLCJQcm9wcyIsImNoaWxkcmVuIiwiUmVhY3ROb2RlIiwiVm9pY2VQcm92aWRlciIsInQwIiwiJCIsIl9jIiwic3RvcmUiLCJfdGVtcCIsInQxIiwidXNlVm9pY2VTdG9yZSIsIkVycm9yIiwidXNlVm9pY2VTdGF0ZSIsInNlbGVjdG9yIiwiZ2V0U3RhdGUiLCJnZXQiLCJzdWJzY3JpYmUiL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LQUFBO0lBQUFGLENBQUEsTUFBQUQsRUFBQTtFQUFBO0lBQUFBLEVBQUEsR0FBQUMsQ0FBQTtFQUFBO0VBQTVDLE1BQUFVLEdBQUEsR0FBWVgsRUFBZ0M7RUFBQSxPQUNyQ2hCLG9CQUFvQixDQUFDbUIsS0FBSyxDQUFBUyxTQUFVLEVBQUVELEdBQUcsRUFBRUEsR0FBRyxDQUFDO0FBQUE7O0FBR3hEO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQSxPQUFPLFNBQUFFLGlCQUFBO0VBQUEsT0FHRVAsYUFBYSxDQUFDLENBQUMsQ0FBQVEsUUFBUztBQUFBOztBQUdqQztBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0EsT0FBTyxTQUFBQyxpQkFBQTtFQUFBLE9BQ0VULGFBQWEsQ0FBQyxDQUFDLENBQUFJLFFBQVM7QUFBQSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/entrypoints/sdk/coreTypes.generated.ts b/_all-in-one-upload/cc-haha/src/entrypoints/sdk/coreTypes.generated.ts new file mode 100644 index 0000000..51d022e --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/entrypoints/sdk/coreTypes.generated.ts @@ -0,0 +1,3 @@ +// Local recovery stub for missing generated SDK types. +// The leaked source tree does not include this codegen artifact. +export {} diff --git a/_all-in-one-upload/cc-haha/src/hooks/.syncthing.usePromptsFromClaudeInChrome.tsx.tmp b/_all-in-one-upload/cc-haha/src/hooks/.syncthing.usePromptsFromClaudeInChrome.tsx.tmp new file mode 100644 index 0000000..ed949a1 Binary files /dev/null and b/_all-in-one-upload/cc-haha/src/hooks/.syncthing.usePromptsFromClaudeInChrome.tsx.tmp differ diff --git a/_all-in-one-upload/cc-haha/src/hooks/notifs/useSettingsErrors.tsx b/_all-in-one-upload/cc-haha/src/hooks/notifs/useSettingsErrors.tsx new file mode 100644 index 0000000..d269482 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/notifs/useSettingsErrors.tsx @@ -0,0 +1,69 @@ +import { c as _c } from "react/compiler-runtime"; +import { useCallback, useEffect, useState } from 'react'; +import { useNotifications } from 'src/context/notifications.js'; +import { getIsRemoteMode } from '../../bootstrap/state.js'; +import { getSettingsWithAllErrors } from '../../utils/settings/allErrors.js'; +import type { ValidationError } from '../../utils/settings/validation.js'; +import { useSettingsChange } from '../useSettingsChange.js'; +const SETTINGS_ERRORS_NOTIFICATION_KEY = 'settings-errors'; +export function useSettingsErrors() { + const $ = _c(6); + const { + addNotification, + removeNotification + } = useNotifications(); + const [errors_0, setErrors] = useState(_temp); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = () => { + const { + errors: errors_1 + } = getSettingsWithAllErrors(); + setErrors(errors_1); + }; + $[0] = t0; + } else { + t0 = $[0]; + } + const handleSettingsChange = t0; + useSettingsChange(handleSettingsChange); + let t1; + let t2; + if ($[1] !== addNotification || $[2] !== errors_0 || $[3] !== removeNotification) { + t1 = () => { + if (getIsRemoteMode()) { + return; + } + if (errors_0.length > 0) { + const message = `Found ${errors_0.length} settings ${errors_0.length === 1 ? "issue" : "issues"} · /doctor for details`; + addNotification({ + key: SETTINGS_ERRORS_NOTIFICATION_KEY, + text: message, + color: "warning", + priority: "high", + timeoutMs: 60000 + }); + } else { + removeNotification(SETTINGS_ERRORS_NOTIFICATION_KEY); + } + }; + t2 = [errors_0, addNotification, removeNotification]; + $[1] = addNotification; + $[2] = errors_0; + $[3] = removeNotification; + $[4] = t1; + $[5] = t2; + } else { + t1 = $[4]; + t2 = $[5]; + } + useEffect(t1, t2); + return errors_0; +} +function _temp() { + const { + errors + } = getSettingsWithAllErrors(); + return errors; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJ1c2VDYWxsYmFjayIsInVzZUVmZmVjdCIsInVzZVN0YXRlIiwidXNlTm90aWZpY2F0aW9ucyIsImdldElzUm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UgsQ0FBQSxNQUFBYSxFQUFBO0lBQUFiLENBQUEsTUFBQWMsRUFBQTtFQUFBO0lBQUFELEVBQUEsR0FBQWIsQ0FBQTtJQUFBYyxFQUFBLEdBQUFkLENBQUE7RUFBQTtFQWRoRFQsU0FBUyxDQUFDc0IsRUFjVCxFQUFFQyxFQUE2QyxDQUFDO0VBQUEsT0FFMUNKLFFBQU07QUFBQTtBQTlCUixTQUFBSixNQUFBO0VBR0g7SUFBQUk7RUFBQSxJQUFtQmYsd0JBQXdCLENBQUMsQ0FBQztFQUFBLE9BQ3RDZSxNQUFNO0FBQUEiLCJpZ25vcmVMaXN0IjpbXX0= \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/hooks/notifs/useStartupNotification.ts b/_all-in-one-upload/cc-haha/src/hooks/notifs/useStartupNotification.ts new file mode 100644 index 0000000..4e2fa8f --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/notifs/useStartupNotification.ts @@ -0,0 +1,41 @@ +import { useEffect, useRef } from 'react' +import { getIsRemoteMode } from '../../bootstrap/state.js' +import { + type Notification, + useNotifications, +} from '../../context/notifications.js' +import { logError } from '../../utils/log.js' + +type Result = Notification | Notification[] | null + +/** + * Fires notification(s) once on mount. Encapsulates the remote-mode gate and + * once-per-session ref guard that was hand-rolled across 10+ notifs/ hooks. + * + * The compute fn runs exactly once on first effect. Return null to skip, + * a Notification to fire one, or an array to fire several. Sync or async. + * Rejections are routed to logError. + */ +export function useStartupNotification( + compute: () => Result | Promise, +): void { + const { addNotification } = useNotifications() + const hasRunRef = useRef(false) + const computeRef = useRef(compute) + computeRef.current = compute + + useEffect(() => { + if (getIsRemoteMode() || hasRunRef.current) return + hasRunRef.current = true + + void Promise.resolve() + .then(() => computeRef.current()) + .then(result => { + if (!result) return + for (const n of Array.isArray(result) ? result : [result]) { + addNotification(n) + } + }) + .catch(logError) + }, [addNotification]) +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/renderPlaceholder.ts b/_all-in-one-upload/cc-haha/src/hooks/renderPlaceholder.ts new file mode 100644 index 0000000..50913ff --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/renderPlaceholder.ts @@ -0,0 +1,51 @@ +import chalk from 'chalk' + +type PlaceholderRendererProps = { + placeholder?: string + value: string + showCursor?: boolean + focus?: boolean + terminalFocus: boolean + invert?: (text: string) => string + hidePlaceholderText?: boolean +} + +export function renderPlaceholder({ + placeholder, + value, + showCursor, + focus, + terminalFocus = true, + invert = chalk.inverse, + hidePlaceholderText = false, +}: PlaceholderRendererProps): { + renderedPlaceholder: string | undefined + showPlaceholder: boolean +} { + let renderedPlaceholder: string | undefined = undefined + + if (placeholder) { + if (hidePlaceholderText) { + // Voice recording: show only the cursor, no placeholder text + renderedPlaceholder = + showCursor && focus && terminalFocus ? invert(' ') : '' + } else { + renderedPlaceholder = chalk.dim(placeholder) + + // Show inverse cursor only when both input and terminal are focused + if (showCursor && focus && terminalFocus) { + renderedPlaceholder = + placeholder.length > 0 + ? invert(placeholder[0]!) + chalk.dim(placeholder.slice(1)) + : invert(' ') + } + } + } + + const showPlaceholder = value.length === 0 && Boolean(placeholder) + + return { + renderedPlaceholder, + showPlaceholder, + } +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/toolPermission/PermissionContext.ts b/_all-in-one-upload/cc-haha/src/hooks/toolPermission/PermissionContext.ts new file mode 100644 index 0000000..43ae7e8 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/toolPermission/PermissionContext.ts @@ -0,0 +1,388 @@ +import { feature } from 'bun:bundle' +import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages.mjs' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from 'src/services/analytics/index.js' +import { sanitizeToolNameForAnalytics } from 'src/services/analytics/metadata.js' +import type { ToolUseConfirm } from '../../components/permissions/PermissionRequest.js' +import type { + ToolPermissionContext, + Tool as ToolType, + ToolUseContext, +} from '../../Tool.js' +import { awaitClassifierAutoApproval } from '../../tools/BashTool/bashPermissions.js' +import { BASH_TOOL_NAME } from '../../tools/BashTool/toolName.js' +import type { AssistantMessage } from '../../types/message.js' +import type { + PendingClassifierCheck, + PermissionAllowDecision, + PermissionDecisionReason, + PermissionDenyDecision, +} from '../../types/permissions.js' +import { setClassifierApproval } from '../../utils/classifierApprovals.js' +import { logForDebugging } from '../../utils/debug.js' +import { executePermissionRequestHooks } from '../../utils/hooks.js' +import { + REJECT_MESSAGE, + REJECT_MESSAGE_WITH_REASON_PREFIX, + SUBAGENT_REJECT_MESSAGE, + SUBAGENT_REJECT_MESSAGE_WITH_REASON_PREFIX, + withMemoryCorrectionHint, +} from '../../utils/messages.js' +import type { PermissionDecision } from '../../utils/permissions/PermissionResult.js' +import { + applyPermissionUpdates, + persistPermissionUpdates, + supportsPersistence, +} from '../../utils/permissions/PermissionUpdate.js' +import type { PermissionUpdate } from '../../utils/permissions/PermissionUpdateSchema.js' +import { + logPermissionDecision, + type PermissionDecisionArgs, +} from './permissionLogging.js' + +type PermissionApprovalSource = + | { type: 'hook'; permanent?: boolean } + | { type: 'user'; permanent: boolean } + | { type: 'classifier' } + +type PermissionRejectionSource = + | { type: 'hook' } + | { type: 'user_abort' } + | { type: 'user_reject'; hasFeedback: boolean } + +// Generic interface for permission queue operations, decoupled from React. +// In the REPL, these are backed by React state. +type PermissionQueueOps = { + push(item: ToolUseConfirm): void + remove(toolUseID: string): void + update(toolUseID: string, patch: Partial): void +} + +type ResolveOnce = { + resolve(value: T): void + isResolved(): boolean + /** + * Atomically check-and-mark as resolved. Returns true if this caller + * won the race (nobody else has resolved yet), false otherwise. + * Use this in async callbacks BEFORE awaiting, to close the window + * between the `isResolved()` check and the actual `resolve()` call. + */ + claim(): boolean +} + +function createResolveOnce(resolve: (value: T) => void): ResolveOnce { + let claimed = false + let delivered = false + return { + resolve(value: T) { + if (delivered) return + delivered = true + claimed = true + resolve(value) + }, + isResolved() { + return claimed + }, + claim() { + if (claimed) return false + claimed = true + return true + }, + } +} + +function createPermissionContext( + tool: ToolType, + input: Record, + toolUseContext: ToolUseContext, + assistantMessage: AssistantMessage, + toolUseID: string, + setToolPermissionContext: (context: ToolPermissionContext) => void, + queueOps?: PermissionQueueOps, +) { + const messageId = assistantMessage.message.id + const ctx = { + tool, + input, + toolUseContext, + assistantMessage, + messageId, + toolUseID, + logDecision( + args: PermissionDecisionArgs, + opts?: { + input?: Record + permissionPromptStartTimeMs?: number + }, + ) { + logPermissionDecision( + { + tool, + input: opts?.input ?? input, + toolUseContext, + messageId, + toolUseID, + }, + args, + opts?.permissionPromptStartTimeMs, + ) + }, + logCancelled() { + logEvent('tengu_tool_use_cancelled', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + }) + }, + async persistPermissions(updates: PermissionUpdate[]) { + if (updates.length === 0) return false + persistPermissionUpdates(updates) + const appState = toolUseContext.getAppState() + setToolPermissionContext( + applyPermissionUpdates(appState.toolPermissionContext, updates), + ) + return updates.some(update => supportsPersistence(update.destination)) + }, + resolveIfAborted(resolve: (decision: PermissionDecision) => void) { + if (!toolUseContext.abortController.signal.aborted) return false + this.logCancelled() + resolve(this.cancelAndAbort(undefined, true)) + return true + }, + cancelAndAbort( + feedback?: string, + isAbort?: boolean, + contentBlocks?: ContentBlockParam[], + ): PermissionDecision { + const sub = !!toolUseContext.agentId + const baseMessage = feedback + ? `${sub ? SUBAGENT_REJECT_MESSAGE_WITH_REASON_PREFIX : REJECT_MESSAGE_WITH_REASON_PREFIX}${feedback}` + : sub + ? SUBAGENT_REJECT_MESSAGE + : REJECT_MESSAGE + const message = sub ? baseMessage : withMemoryCorrectionHint(baseMessage) + if (isAbort || (!feedback && !contentBlocks?.length && !sub)) { + logForDebugging( + `Aborting: tool=${tool.name} isAbort=${isAbort} hasFeedback=${!!feedback} isSubagent=${sub}`, + ) + toolUseContext.abortController.abort() + } + return { behavior: 'ask', message, contentBlocks } + }, + ...(feature('BASH_CLASSIFIER') + ? { + async tryClassifier( + pendingClassifierCheck: PendingClassifierCheck | undefined, + updatedInput: Record | undefined, + ): Promise { + if (tool.name !== BASH_TOOL_NAME || !pendingClassifierCheck) { + return null + } + const classifierDecision = await awaitClassifierAutoApproval( + pendingClassifierCheck, + toolUseContext.abortController.signal, + toolUseContext.options.isNonInteractiveSession, + ) + if (!classifierDecision) { + return null + } + if ( + feature('TRANSCRIPT_CLASSIFIER') && + classifierDecision.type === 'classifier' + ) { + const matchedRule = classifierDecision.reason.match( + /^Allowed by prompt rule: "(.+)"$/, + )?.[1] + if (matchedRule) { + setClassifierApproval(toolUseID, matchedRule) + } + } + logPermissionDecision( + { tool, input, toolUseContext, messageId, toolUseID }, + { decision: 'accept', source: { type: 'classifier' } }, + undefined, + ) + return { + behavior: 'allow' as const, + updatedInput: updatedInput ?? input, + userModified: false, + decisionReason: classifierDecision, + } + }, + } + : {}), + async runHooks( + permissionMode: string | undefined, + suggestions: PermissionUpdate[] | undefined, + updatedInput?: Record, + permissionPromptStartTimeMs?: number, + ): Promise { + for await (const hookResult of executePermissionRequestHooks( + tool.name, + toolUseID, + input, + toolUseContext, + permissionMode, + suggestions, + toolUseContext.abortController.signal, + )) { + if (hookResult.permissionRequestResult) { + const decision = hookResult.permissionRequestResult + if (decision.behavior === 'allow') { + const finalInput = decision.updatedInput ?? updatedInput ?? input + return await this.handleHookAllow( + finalInput, + decision.updatedPermissions ?? [], + permissionPromptStartTimeMs, + ) + } else if (decision.behavior === 'deny') { + this.logDecision( + { decision: 'reject', source: { type: 'hook' } }, + { permissionPromptStartTimeMs }, + ) + if (decision.interrupt) { + logForDebugging( + `Hook interrupt: tool=${tool.name} hookMessage=${decision.message}`, + ) + toolUseContext.abortController.abort() + } + return this.buildDeny( + decision.message || 'Permission denied by hook', + { + type: 'hook', + hookName: 'PermissionRequest', + reason: decision.message, + }, + ) + } + } + } + return null + }, + buildAllow( + updatedInput: Record, + opts?: { + userModified?: boolean + decisionReason?: PermissionDecisionReason + acceptFeedback?: string + contentBlocks?: ContentBlockParam[] + }, + ): PermissionAllowDecision { + return { + behavior: 'allow' as const, + updatedInput, + userModified: opts?.userModified ?? false, + ...(opts?.decisionReason && { decisionReason: opts.decisionReason }), + ...(opts?.acceptFeedback && { acceptFeedback: opts.acceptFeedback }), + ...(opts?.contentBlocks && + opts.contentBlocks.length > 0 && { + contentBlocks: opts.contentBlocks, + }), + } + }, + buildDeny( + message: string, + decisionReason: PermissionDecisionReason, + ): PermissionDenyDecision { + return { behavior: 'deny' as const, message, decisionReason } + }, + async handleUserAllow( + updatedInput: Record, + permissionUpdates: PermissionUpdate[], + feedback?: string, + permissionPromptStartTimeMs?: number, + contentBlocks?: ContentBlockParam[], + decisionReason?: PermissionDecisionReason, + ): Promise { + const acceptedPermanentUpdates = + await this.persistPermissions(permissionUpdates) + this.logDecision( + { + decision: 'accept', + source: { type: 'user', permanent: acceptedPermanentUpdates }, + }, + { input: updatedInput, permissionPromptStartTimeMs }, + ) + const userModified = tool.inputsEquivalent + ? !tool.inputsEquivalent(input, updatedInput) + : false + const trimmedFeedback = feedback?.trim() + return this.buildAllow(updatedInput, { + userModified, + decisionReason, + acceptFeedback: trimmedFeedback || undefined, + contentBlocks, + }) + }, + async handleHookAllow( + finalInput: Record, + permissionUpdates: PermissionUpdate[], + permissionPromptStartTimeMs?: number, + ): Promise { + const acceptedPermanentUpdates = + await this.persistPermissions(permissionUpdates) + this.logDecision( + { + decision: 'accept', + source: { type: 'hook', permanent: acceptedPermanentUpdates }, + }, + { input: finalInput, permissionPromptStartTimeMs }, + ) + return this.buildAllow(finalInput, { + decisionReason: { type: 'hook', hookName: 'PermissionRequest' }, + }) + }, + pushToQueue(item: ToolUseConfirm) { + queueOps?.push(item) + }, + removeFromQueue() { + queueOps?.remove(toolUseID) + }, + updateQueueItem(patch: Partial) { + queueOps?.update(toolUseID, patch) + }, + } + return Object.freeze(ctx) +} + +type PermissionContext = ReturnType + +/** + * Create a PermissionQueueOps backed by a React state setter. + * This is the bridge between React's `setToolUseConfirmQueue` and the + * generic queue interface used by PermissionContext. + */ +function createPermissionQueueOps( + setToolUseConfirmQueue: React.Dispatch< + React.SetStateAction + >, +): PermissionQueueOps { + return { + push(item: ToolUseConfirm) { + setToolUseConfirmQueue(queue => [...queue, item]) + }, + remove(toolUseID: string) { + setToolUseConfirmQueue(queue => + queue.filter(item => item.toolUseID !== toolUseID), + ) + }, + update(toolUseID: string, patch: Partial) { + setToolUseConfirmQueue(queue => + queue.map(item => + item.toolUseID === toolUseID ? { ...item, ...patch } : item, + ), + ) + }, + } +} + +export { createPermissionContext, createPermissionQueueOps, createResolveOnce } +export type { + PermissionContext, + PermissionApprovalSource, + PermissionQueueOps, + PermissionRejectionSource, + ResolveOnce, +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useCanUseTool.tsx b/_all-in-one-upload/cc-haha/src/hooks/useCanUseTool.tsx new file mode 100644 index 0000000..1eb77d5 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useCanUseTool.tsx @@ -0,0 +1,204 @@ +import { c as _c } from "react/compiler-runtime"; +import { feature } from 'bun:bundle'; +import { APIUserAbortError } from '@anthropic-ai/sdk'; +import * as React from 'react'; +import { useCallback } from 'react'; +import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent } from 'src/services/analytics/index.js'; +import { sanitizeToolNameForAnalytics } from 'src/services/analytics/metadata.js'; +import type { ToolUseConfirm } from '../components/permissions/PermissionRequest.js'; +import { Text } from '../ink.js'; +import type { ToolPermissionContext, Tool as ToolType, ToolUseContext } from '../Tool.js'; +import { consumeSpeculativeClassifierCheck, peekSpeculativeClassifierCheck } from '../tools/BashTool/bashPermissions.js'; +import { BASH_TOOL_NAME } from '../tools/BashTool/toolName.js'; +import type { AssistantMessage } from '../types/message.js'; +import { recordAutoModeDenial } from '../utils/autoModeDenials.js'; +import { clearClassifierChecking, setClassifierApproval, setYoloClassifierApproval } from '../utils/classifierApprovals.js'; +import { logForDebugging } from '../utils/debug.js'; +import { AbortError } from '../utils/errors.js'; +import { logError } from '../utils/log.js'; +import type { PermissionDecision } from '../utils/permissions/PermissionResult.js'; +import { hasPermissionsToUseTool } from '../utils/permissions/permissions.js'; +import { jsonStringify } from '../utils/slowOperations.js'; +import { handleCoordinatorPermission } from './toolPermission/handlers/coordinatorHandler.js'; +import { handleInteractivePermission } from './toolPermission/handlers/interactiveHandler.js'; +import { handleSwarmWorkerPermission } from './toolPermission/handlers/swarmWorkerHandler.js'; +import { createPermissionContext, createPermissionQueueOps } from './toolPermission/PermissionContext.js'; +import { logPermissionDecision } from './toolPermission/permissionLogging.js'; +export type CanUseToolFn = Record> = (tool: ToolType, input: Input, toolUseContext: ToolUseContext, assistantMessage: AssistantMessage, toolUseID: string, forceDecision?: PermissionDecision) => Promise>; +function useCanUseTool(setToolUseConfirmQueue, setToolPermissionContext) { + const $ = _c(3); + let t0; + if ($[0] !== setToolPermissionContext || $[1] !== setToolUseConfirmQueue) { + t0 = async (tool, input, toolUseContext, assistantMessage, toolUseID, forceDecision) => new Promise(resolve => { + const ctx = createPermissionContext(tool, input, toolUseContext, assistantMessage, toolUseID, setToolPermissionContext, createPermissionQueueOps(setToolUseConfirmQueue)); + if (ctx.resolveIfAborted(resolve)) { + return; + } + const decisionPromise = forceDecision !== undefined ? Promise.resolve(forceDecision) : hasPermissionsToUseTool(tool, input, toolUseContext, assistantMessage, toolUseID); + return decisionPromise.then(async result => { + if (result.behavior === "allow") { + if (ctx.resolveIfAborted(resolve)) { + return; + } + if (feature("TRANSCRIPT_CLASSIFIER") && result.decisionReason?.type === "classifier" && result.decisionReason.classifier === "auto-mode") { + setYoloClassifierApproval(toolUseID, result.decisionReason.reason); + } + ctx.logDecision({ + decision: "accept", + source: "config" + }); + resolve(ctx.buildAllow(result.updatedInput ?? input, { + decisionReason: result.decisionReason + })); + return; + } + const appState = toolUseContext.getAppState(); + const description = await tool.description(input as never, { + isNonInteractiveSession: toolUseContext.options.isNonInteractiveSession, + toolPermissionContext: appState.toolPermissionContext, + tools: toolUseContext.options.tools + }); + if (ctx.resolveIfAborted(resolve)) { + return; + } + switch (result.behavior) { + case "deny": + { + logPermissionDecision({ + tool, + input, + toolUseContext, + messageId: ctx.messageId, + toolUseID + }, { + decision: "reject", + source: "config" + }); + if (feature("TRANSCRIPT_CLASSIFIER") && result.decisionReason?.type === "classifier" && result.decisionReason.classifier === "auto-mode") { + recordAutoModeDenial({ + toolName: tool.name, + display: description, + reason: result.decisionReason.reason ?? "", + timestamp: Date.now() + }); + toolUseContext.addNotification?.({ + key: "auto-mode-denied", + priority: "immediate", + jsx: <>{tool.userFacingName(input).toLowerCase()} denied by auto mode · /permissions + }); + } + resolve(result); + return; + } + case "ask": + { + if (appState.toolPermissionContext.awaitAutomatedChecksBeforeDialog) { + const coordinatorDecision = await handleCoordinatorPermission({ + ctx, + ...(feature("BASH_CLASSIFIER") ? { + pendingClassifierCheck: result.pendingClassifierCheck + } : {}), + updatedInput: result.updatedInput, + suggestions: result.suggestions, + permissionMode: appState.toolPermissionContext.mode + }); + if (coordinatorDecision) { + resolve(coordinatorDecision); + return; + } + } + if (ctx.resolveIfAborted(resolve)) { + return; + } + const swarmDecision = await handleSwarmWorkerPermission({ + ctx, + description, + ...(feature("BASH_CLASSIFIER") ? { + pendingClassifierCheck: result.pendingClassifierCheck + } : {}), + updatedInput: result.updatedInput, + suggestions: result.suggestions + }); + if (swarmDecision) { + resolve(swarmDecision); + return; + } + if (feature("BASH_CLASSIFIER") && result.pendingClassifierCheck && tool.name === BASH_TOOL_NAME && !appState.toolPermissionContext.awaitAutomatedChecksBeforeDialog) { + const speculativePromise = peekSpeculativeClassifierCheck((input as { + command: string; + }).command); + if (speculativePromise) { + const raceResult = await Promise.race([speculativePromise.then(_temp), new Promise(_temp2)]); + if (ctx.resolveIfAborted(resolve)) { + return; + } + if (raceResult.type === "result" && raceResult.result.matches && raceResult.result.confidence === "high" && feature("BASH_CLASSIFIER")) { + consumeSpeculativeClassifierCheck((input as { + command: string; + }).command); + const matchedRule = raceResult.result.matchedDescription ?? undefined; + if (matchedRule) { + setClassifierApproval(toolUseID, matchedRule); + } + ctx.logDecision({ + decision: "accept", + source: { + type: "classifier" + } + }); + resolve(ctx.buildAllow(result.updatedInput ?? input as Record, { + decisionReason: { + type: "classifier" as const, + classifier: "bash_allow" as const, + reason: `Allowed by prompt rule: "${raceResult.result.matchedDescription}"` + } + })); + return; + } + } + } + handleInteractivePermission({ + ctx, + description, + result, + awaitAutomatedChecksBeforeDialog: appState.toolPermissionContext.awaitAutomatedChecksBeforeDialog, + bridgeCallbacks: feature("BRIDGE_MODE") ? appState.replBridgePermissionCallbacks : undefined, + channelCallbacks: feature("KAIROS") || feature("KAIROS_CHANNELS") ? appState.channelPermissionCallbacks : undefined + }, resolve); + return; + } + } + }).catch(error => { + if (error instanceof AbortError || error instanceof APIUserAbortError) { + logForDebugging(`Permission check threw ${error.constructor.name} for tool=${tool.name}: ${error.message}`); + ctx.logCancelled(); + resolve(ctx.cancelAndAbort(undefined, true)); + } else { + logError(error); + resolve(ctx.cancelAndAbort(undefined, true)); + } + }).finally(() => { + clearClassifierChecking(toolUseID); + }); + }); + $[0] = setToolPermissionContext; + $[1] = setToolUseConfirmQueue; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} +function _temp2(res) { + return setTimeout(res, 2000, { + type: "timeout" as const + }); +} +function _temp(r) { + return { + type: "result" as const, + result: r + }; +} +export default useCanUseTool; +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJmZWF0dXJlIiwiQVBJVXNlckFib3J0RXJyb3IiLCJSZWFjdCIsInVzZUNhbGxiYWNrIiwiQW5hbHl0aWNzTWV0YWRhdGFfSV9WRVJJRklFRF9USElTX0lTX05PVF9DT0RFX09SX0ZJTEVQQVRIUyIsImxvZ0V2ZW50Iiwic2FuaX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QZXJtaXNzaW9uc1RvVXNlVG9vbChcbiAgICAgICAgICAgICAgICB0b29sLFxuICAgICAgICAgICAgICAgIGlucHV0LFxuICAgICAgICAgICAgICAgIHRvb2xVc2VDb250ZXh0LFxuICAgICAgICAgICAgICAgIGFzc2lzdGFudE1lc3NhZ2UsXG4gICAgICAgICAgICAgICAgdG9vbFVzZUlELFxuICAgICAgICAgICAgICApXG5cbiAgICAgICAgcmV0dXJuIGRlY2lzaW9uUHJvbWlzZVxuICAgICAgICAgIC50aGVuKGFzeW5jIHJlc3VsdCA9PiB7XG4gICAgICAgICAgICAvLyBbQU5ULU9OTFldIExvZyBhbGwgdG9vbCBwZXJtaXNzaW9uIGRlY2lzaW9ucyB3aXRoIHRvb2wgbmFtZSBhbmQgYXJnc1xuICAgICAgICAgICAgaWYgKFwiZXh0ZXJuYWxcIiA9PT0gJ2FudCcpIHtcbiAgICAgICAgICAgICAgbG9nRXZlbnQoJ3Rlbmd1X2ludGVybmFsX3Rvb2xfcGVybWlzc2lvbl9kZWNpc2lvbicsIHtcbiAgICAgICAgICAgICAgICB0b29sTmFtZTogc2FuaXRpemVUb29sTmFtZUZvckFuYWx5dGljcyh0b29sLm5hbWUpLFxuICAgICAgICAgICAgICAgIGJlaGF2aW9yOlxuICAgICAgICAgICAgICAgICAgcmVzdWx0LmJlaGF2aW9yIGFzIEFuYWx5dGljc01ldGFkYXRhX0lfVkVSSUZJRURfVEhJU19JU19OT1RfQ09ERV9PUl9GSUxFUEFUSFMsXG4gICAgICAgICAgICAgICAgLy8gTm90ZTogaW5wdXQgY29udGFpbnMgY29kZS9maWxlcGF0aHMsIG9ubHkgbG9nIGZvciBhbnRzXG4gICAgICAgICAgICAgICAgaW5wdXQ6IGpzb25TdHJpbmdpZnkoXG4gICAgICAgICAgICAgICAgICBpbnB1dCxcbiAgICAgICAgICAgICAgICApIGFzIEFuYWx5dGljc01ldGFkYXRhX0lfVkVSSUZJRURfVEhJU19JU19OT1RfQ09ERV9PUl9GSUxFUEFUSFMsXG4gICAgICAgICAgICAgICAgbWVzc2FnZUlEOlxuICAgICAgICAgICAgICAgICAgY3R4Lm1lc3NhZ2VJZCBhcyBBbmFseXRpY3NNZXRhZGF0YV9JX1ZFUklGSUVEX1RISVNfSVNfTk9UX0NPREVfT1JfRklMRVBBVEhTLFxuICAgICAgICAgICAgICAgIGlzTWNwOiB0b29sLmlzTWNwID8/IGZhbHNlLFxuICAgICAgICAgICAgICB9KVxuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICAvLyBIYXMgcGVybWlzc2lvbnMgdG8gdXNlIHRvb2wsIGdyYW50ZWQgaW4gY29uZmlnXG4gICAgICAgICAgICBpZiAocmVzdWx0LmJlaGF2aW9yID09PSAnYWxsb3cnKSB7XG4gICAgICAgICAgICAgIGlmIChjdHgucmVzb2x2ZUlmQWJvcnRlZChyZXNvbHZlKSkgcmV0dXJuXG4gICAgICAgICAgICAgIC8vIFRyYWNrIGF1dG8gbW9kZSBjbGFzc2lmaWVyIGFwcHJvdmFscyBmb3IgVUkgZGlzcGxheVxuICAgICAgICAgICAgICBpZiAoXG4gICAgICAgICAgICAgICAgZmVhdHVyZSgnVFJBTlNDUklQVF9DTEFTU0lGSUVSJykgJiZcbiAgICAgICAgICAgICAgICByZXN1bHQuZGVjaXNpb25SZWFzb24/LnR5cGUgPT09ICdjbGFzc2lmaWVyJyAmJlxuICAgICAgICAgICAgICAgIHJlc3VsdC5kZWNpc2lvblJlYXNvbi5jbGFzc2lmaWVyID09PSAnYXV0by1tb2RlJ1xuICAgICAgICAgICAgICApIHtcbiAgICAgICAgICAgICAgICBzZXRZb2xvQ2xhc3NpZmllckFwcHJvdmFsKFxuICAgICAgICAgICAgICAgICAgdG9vbFVzZUlELFxuICAgICAgICAgICAgICAgICAgcmVzdWx0LmRlY2lzaW9uUmVhc29uLnJlYXNvbixcbiAgICAgICAgICAgICAgICApXG4gICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICBjdHgubG9nRGVjaXNpb24oeyBkZWNpc2lvbjogJ2FjY2VwdCcsIHNvdXJjZTogJ2NvbmZpZycgfSlcblxuICAgICAgICAgICAgICByZXNvbHZlKFxuICAgICAgICAgICAgICAgIGN0eC5idWlsZEFsbG93KHJlc3VsdC51cGRhdGVkSW5wdXQgPz8gaW5wdXQsIHtcbiAgICAgICAgICAgICAgICAgIGRlY2lzaW9uUmVhc29uOiByZXN1bHQuZGVjaXNpb25SZWFzb24sXG4gICAgICAgICAgICAgICAgfSksXG4gICAgICAgICAgICAgIClcbiAgICAgICAgICAgICAgcmV0dXJuXG4gICAgICAgICAgICB9XG5cbiAgICAgICAgICAgIGNvbnN0IGFwcFN0YXRlID0gdG9vbFVzZUNvbnRleHQuZ2V0QXBwU3RhdGUoKVxuICAgICAgICAgICAgY29uc3QgZGVzY3JpcHRpb24gPSBhd2FpdCB0b29sLmRlc2NyaXB0aW9uKGlucHV0IGFzIG5ldmVyLCB7XG4gICAgICAgICAgICAgIGlzTm9uSW50ZXJhY3RpdmVTZXNzaW9uOlxuICAgICAgICAgICAgICAgIHRvb2xVc2VDb250ZXh0Lm9wdGlvbnMuaXNOb25JbnRlcmFjdGl2ZVNlc3Npb24sXG4gICAgICAgICAgICAgIHRvb2xQZXJtaXNzaW9uQ29udGV4dDogYXBwU3RhdGUudG9vbFBlcm1pc3Npb25Db250ZXh0LFxuICAgICAgICAgICAgICB0b29sczogdG9vbFVzZUNvbnRleHQub3B0aW9ucy50b29scyxcbiAgICAgICAgICAgIH0pXG5cbiAgICAgICAgICAgIGlmIChjdHgucmVzb2x2ZUlmQWJvcnRlZChyZXNvbHZlKSkgcmV0dXJuXG5cbiAgICAgICAgICAgIC8vIERvZXMgbm90IGhhdmUgcGVybWlzc2lvbnMgdG8gdXNlIHRvb2wsIGNoZWNrIHRoZSBiZWhhdmlvclxuICAgICAgICAgICAgc3dpdGNoIChyZXN1bHQuYmVoYXZpb3IpIHtcbiAgICAgICAgICAgICAgY2FzZSAnZGVueSc6IHtcbiAgICAgICAgICAgICAgICBsb2dQZXJtaXNzaW9uRGVjaXNpb24oXG4gICAgICAgICAgICAgICAgICB7XG4gICAgICAgICAgICAgICAgICAgIHRvb2wsXG4gICAgICAgICAgICAgICAgICAgIGlucHV0LFxuICAgICAgICAgICAgICAgICAgICB0b29sVXNlQ29udGV4dCxcbiAgICAgICAgICAgICAgICAgICAgbWVzc2FnZUlkOiBjdHgubWVzc2FnZUlkLFxuICAgICAgICAgICAgICAgICAgICB0b29sVXNlSUQsXG4gICAgICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICAgICAgeyBkZWNpc2lvbjogJ3JlamVjdCcsIHNvdXJjZTogJ2NvbmZpZycgfSxcbiAgICAgICAgICAgICAgICApXG4gICAgICAgICAgICAgICAgaWYgKFxuICAgICAgICAgICAgICAgICAgZmVhdHVyZSgnVFJBTlNDUklQVF9DTEFTU0lGSUVSJykgJiZcbiAgICAgICAgICAgICAgICAgIHJlc3VsdC5kZWNpc2lvblJlYXNvbj8udHlwZSA9PT0gJ2NsYXNzaWZpZXInICYmXG4gICAgICAgICAgICAgICAgICByZXN1bHQuZGVjaXNpb25SZWFzb24uY2xhc3NpZmllciA9PT0gJ2F1dG8tbW9kZSdcbiAgICAgICAgICAgICAgICApIHtcbiAgICAgICAgICAgICAgICAgIHJlY29yZEF1dG9Nb2RlRGVuaWFsKHtcbiAgICAgICAgICAgICAgICAgICAgdG9vbE5hbWU6IHRvb2wubmFtZSxcbiAgICAgICAgICAgICAgICAgICAgZGlzcGxheTogZGVzY3JpcHRpb24sXG4gICAgICAgICAgICAgICAgICAgIHJlYXNvbjogcmVzdWx0LmRlY2lzaW9uUmVhc29uLnJlYXNvbiA/PyAnJyxcbiAgICAgICAgICAgICAgICAgICAgdGltZXN0YW1wOiBEYXRlLm5vdygpLFxuICAgICAgICAgICAgICAgICAgfSlcbiAgICAgICAgICAgICAgICAgIHRvb2xVc2VDb250ZXh0LmFkZE5vdGlmaWNhdGlvbj8uKHtcbiAgICAgICAgICAgICAgICAgICAga2V5OiAnYXV0by1tb2RlLWRlbmllZCcsXG4gICAgICAgICAgICAgICAgICAgIHByaW9yaXR5OiAnaW1tZWRpYXRlJyxcbiAgICAgICAgICAgICAgICAgICAganN4OiAoXG4gICAgICAgICAgICAgICAgICAgICAgPD5cbiAgICAgICAgICAgICAgICAgICAgICAgIDxUZXh0IGNvbG9yPVwiZXJyb3JcIj5cbiAgICAgICAgICAgICAgICAgICAgICAgICAge3Rvb2wudXNlckZhY2luZ05hbWUoaW5wdXQpLnRvTG93ZXJDYXNlKCl9IGRlbmllZCBieVxuICAgICAgICAgICAgICAgICAgICAgICAgICBhdXRvIG1vZGVcbiAgICAgICAgICAgICAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICAgICAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPiDCtyAvcGVybWlzc2lvbnM8L1RleHQ+XG4gICAgICAgICAgICAgICAgICAgICAgPC8+XG4gICAgICAgICAgICAgICAgICAgICksXG4gICAgICAgICAgICAgICAgICB9KVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICByZXNvbHZlKHJlc3VsdClcbiAgICAgICAgICAgICAgICByZXR1cm5cbiAgICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICAgIGNhc2UgJ2Fzayc6IHtcbiAgICAgICAgICAgICAgICAvLyBGb3IgY29vcmRpbmF0b3Igd29ya2VycywgYXdhaXQgYXV0b21hdGVkIGNoZWNrcyBiZWZvcmUgc2hvd2luZyBkaWFsb2cuXG4gICAgICAgICAgICAgICAgLy8gQmFja2dyb3VuZCB3b3JrZXJzIHNob3VsZCBvbmx5IGludGVycnVwdCB0aGUgdXNlciB3aGVuIGF1dG9tYXRlZCBjaGVja3MgY2FuJ3QgZGVjaWRlLlxuICAgICAgICAgICAgICAgIGlmIChcbiAgICAgICAgICAgICAgICAgIGFwcFN0YXRlLnRvb2xQZXJtaXNzaW9uQ29udGV4dFxuICAgICAgICAgICAgICAgICAgICAuYXdhaXRBdXRvbWF0ZWRDaGVja3NCZWZvcmVEaWFsb2dcbiAgICAgICAgICAgICAgICApIHtcbiAgICAgICAgICAgICAgICAgIGNvbnN0IGNvb3JkaW5hdG9yRGVjaXNpb24gPSBhd2FpdCBoYW5kbGVDb29yZGluYXRvclBlcm1pc3Npb24oXG4gICAgICAgICAgICAgICAgICAgIHtcbiAgICAgICAgICAgICAgICAgICAgICBjdHgsXG4gICAgICAgICAgICAgICAgICAgICAgLi4uKGZlYXR1cmUoJ0JBU0hfQ0xBU1NJRklFUicpXG4gICAgICAgICAgICAgICAgICAgICAgICA/IHtcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZW5kaW5nQ2xhc3NpZmllckNoZWNrOlxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0LnBlbmRpbmdDbGFzc2lmaWVyQ2hlY2ssXG4gICAgICAgICAgICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICAgICAgICAgIDoge30pLFxuICAgICAgICAgICAgICAgICAgICAgIHVwZGF0ZWRJbnB1dDogcmVzdWx0LnVwZGF0ZWRJbnB1dCxcbiAgICAgICAgICAgICAgICAgICAgICBzdWdnZXN0aW9uczogcmVzdWx0LnN1Z2dlc3Rpb25zLFxuICAgICAgICAgICAgICAgICAgICAgIHBlcm1pc3Npb25Nb2RlOiBhcHBTdGF0ZS50b29sUGVybWlzc2lvbkNvbnRleHQubW9kZSxcbiAgICAgICAgICAgICAgICAgICAgfSxcbiAgICAgICAgICAgICAgICAgIClcbiAgICAgICAgICAgICAgICAgIGlmIChjb29yZGluYXRvckRlY2lzaW9uKSB7XG4gICAgICAgICAgICAgICAgICAgIHJlc29sdmUoY29vcmRpbmF0b3JEZWNpc2lvbilcbiAgICAgICAgICAgICAgICAgICAgcmV0dXJuXG4gICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgICAvLyBudWxsIG1lYW5zIG5laXRoZXIgYXV0b21hdGVkIGNoZWNrIHJlc29sdmVkIC0tIGZhbGwgdGhyb3VnaCB0byBkaWFsb2cgYmVsb3cuXG4gICAgICAgICAgICAgICAgICAvLyBIb29rcyBhbHJlYWR5IHJhbiwgY2xhc3NpZmllciBhbHJlYWR5IGNvbnN1bWVkLlxuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIC8vIEFmdGVyIGF3YWl0aW5nIGF1dG9tYXRlZCBjaGVja3MsIHZlcmlmeSB0aGUgcmVxdWVzdCB3YXNuJ3QgYWJvcnRlZFxuICAgICAgICAgICAgICAgIC8vIHdoaWxlIHdlIHdlcmUgd2FpdGluZy4gV2l0aG91dCB0aGlzIGNoZWNrLCBhIHN0YWxlIGRpYWxvZyBjb3VsZCBhcHBlYXIuXG4gICAgICAgICAgICAgICAgaWYgKGN0eC5yZXNvbHZlSWZBYm9ydGVkKHJlc29sdmUpKSByZXR1cm5cblxuICAgICAgICAgICAgICAgIC8vIEZvciBzd2FybSB3b3JrZXJzLCB0cnkgY2xhc3NpZmllciBhdXRvLWFwcHJvdmFsIHRoZW5cbiAgICAgICAgICAgICAgICAvLyBmb3J3YXJkIHBlcm1pc3Npb24gcmVxdWVzdHMgdG8gdGhlIGxlYWRlciB2aWEgbWFpbGJveC5cbiAgICAgICAgICAgICAgICBjb25zdCBzd2FybURlY2lzaW9uID0gYXdhaXQgaGFuZGxlU3dhcm1Xb3JrZXJQZXJtaXNzaW9uKHtcbiAgICAgICAgICAgICAgICAgIGN0eCxcbiAgICAgICAgICAgICAgICAgIGRlc2NyaXB0aW9uLFxuICAgICAgICAgICAgICAgICAgLi4uKGZlYXR1cmUoJ0JBU0hfQ0xBU1NJRklFUicpXG4gICAgICAgICAgICAgICAgICAgID8ge1xuICAgICAgICAgICAgICAgICAgICAgICAgcGVuZGluZ0NsYXNzaWZpZXJDaGVjazogcmVzdWx0LnBlbmRpbmdDbGFzc2lmaWVyQ2hlY2ssXG4gICAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgICAgICA6IHt9KSxcbiAgICAgICAgICAgICAgICAgIHVwZGF0ZWRJbnB1dDogcmVzdWx0LnVwZGF0ZWRJbnB1dCxcbiAgICAgICAgICAgICAgICAgIHN1Z2dlc3Rpb25zOiByZXN1bHQuc3VnZ2VzdGlvbnMsXG4gICAgICAgICAgICAgICAgfSlcbiAgICAgICAgICAgICAgICBpZiAoc3dhcm1EZWNpc2lvbikge1xuICAgICAgICAgICAgICAgICAgcmVzb2x2ZShzd2FybURlY2lzaW9uKVxuICAgICAgICAgICAgICAgICAgcmV0dXJuXG4gICAgICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICAgICAgLy8gR3JhY2UgcGVyaW9kOiB3YWl0IHVwIHRvIDJzIGZvciBzcGVjdWxhdGl2ZSBjbGFzc2lmaWVyXG4gICAgICAgICAgICAgICAgLy8gdG8gcmVzb2x2ZSBiZWZvcmUgc2hvd2luZyB0aGUgZGlhbG9nIChtYWluIGFnZW50IG9ubHkpXG4gICAgICAgICAgICAgICAgaWYgKFxuICAgICAgICAgICAgICAgICAgZmVhdHVyZSgnQkFTSF9DTEFTU0lGSUVSJykgJiZcbiAgICAgICAgICAgICAgICAgIHJlc3VsdC5wZW5kaW5nQ2xhc3NpZmllckNoZWNrICYmXG4gICAgICAgICAgICAgICAgICB0b29sLm5hbWUgPT09IEJBU0hfVE9PTF9OQU1FICYmXG4gICAgICAgICAgICAgICAgICAhYXBwU3RhdGUudG9vbFBlcm1pc3Npb25Db250ZXh0XG4gICAgICAgICAgICAgICAgICAgIC5hd2FpdEF1dG9tYXRlZENoZWNrc0JlZm9yZURpYWxvZ1xuICAgICAgICAgICAgICAgICkge1xuICAgICAgICAgICAgICAgICAgY29uc3Qgc3BlY3VsYXRpdmVQcm9taXNlID0gcGVla1NwZWN1bGF0aXZlQ2xhc3NpZmllckNoZWNrKFxuICAgICAgICAgICAgICAgICAgICAoaW5wdXQgYXMgeyBjb21tYW5kOiBzdHJpbmcgfSkuY29tbWFuZCxcbiAgICAgICAgICAgICAgICAgIClcbiAgICAgICAgICAgICAgICAgIGlmIChzcGVjdWxhdGl2ZVByb21pc2UpIHtcbiAgICAgICAgICAgICAgICAgICAgY29uc3QgcmFjZVJlc3VsdCA9IGF3YWl0IFByb21pc2UucmFjZShbXG4gICAgICAgICAgICAgICAgICAgICAgc3BlY3VsYXRpdmVQcm9taXNlLnRoZW4ociA9PiAoe1xuICAgICAgICAgICAgICAgICAgICAgICAgdHlwZTogJ3Jlc3VsdCcgYXMgY29uc3QsXG4gICAgICAgICAgICAgICAgICAgICAgICByZXN1bHQ6IHIsXG4gICAgICAgICAgICAgICAgICAgICAgfSkpLFxuICAgICAgICAgICAgICAgICAgICAgIG5ldyBQcm9taXNlPHsgdHlwZTogJ3RpbWVvdXQnIH0+KHJlcyA9PlxuICAgICAgICAgICAgICAgICAgICAgICAgLy8gZXNsaW50LWRpc2FibGUtbmV4dC1saW5lIG5vLXJlc3RyaWN0ZWQtc3ludGF4IC0tIHJlc29sdmVzIHdpdGggYSB2YWx1ZSwgbm90IHZvaWRcbiAgICAgICAgICAgICAgICAgICAgICAgIHNldFRpbWVvdXQocmVzLCAyMDAwLCB7IHR5cGU6ICd0aW1lb3V0JyBhcyBjb25zdCB9KSxcbiAgICAgICAgICAgICAgICAgICAgICApLFxuICAgICAgICAgICAgICAgICAgICBdKVxuXG4gICAgICAgICAgICAgICAgICAgIGlmIChjdHgucmVzb2x2ZUlmQWJvcnRlZChyZXNvbHZlKSkgcmV0dXJuXG5cbiAgICAgICAgICAgICAgICAgICAgaWYgKFxuICAgICAgICAgICAgICAgICAgICAgIHJhY2VSZXN1bHQudHlwZSA9PT0gJ3Jlc3VsdCcgJiZcbiAgICAgICAgICAgICAgICAgICAgICByYWNlUmVzdWx0LnJlc3VsdC5tYXRjaGVzICYmXG4gICAgICAgICAgICAgICAgICAgICAgcmFjZVJlc3VsdC5yZXN1bHQuY29uZmlkZW5jZSA9PT0gJ2hpZ2gnICYmXG4gICAgICAgICAgICAgICAgICAgICAgZmVhdHVyZSgnQkFTSF9DTEFTU0lGSUVSJylcbiAgICAgICAgICAgICAgICAgICAgKSB7XG4gICAgICAgICAgICAgICAgICAgICAgLy8gQ2xhc3NpZmllciBhcHByb3ZlZCB3aXRoaW4gZ3JhY2UgcGVyaW9kIOKAlCBza2lwIGRpYWxvZ1xuICAgICAgICAgICAgICAgICAgICAgIHZvaWQgY29uc3VtZVNwZWN1bGF0aXZlQ2xhc3NpZmllckNoZWNrKFxuICAgICAgICAgICAgICAgICAgICAgICAgKGlucHV0IGFzIHsgY29tbWFuZDogc3RyaW5nIH0pLmNvbW1hbmQsXG4gICAgICAgICAgICAgICAgICAgICAgKVxuXG4gICAgICAgICAgICAgICAgICAgICAgY29uc3QgbWF0Y2hlZFJ1bGUgPVxuICAgICAgICAgICAgICAgICAgICAgICAgcmFjZVJlc3VsdC5yZXN1bHQubWF0Y2hlZERlc2NyaXB0aW9uID8/IHVuZGVmaW5lZFxuICAgICAgICAgICAgICAgICAgICAgIGlmIChtYXRjaGVkUnVsZSkge1xuICAgICAgICAgICAgICAgICAgICAgICAgc2V0Q2xhc3NpZmllckFwcHJvdmFsKHRvb2xVc2VJRCwgbWF0Y2hlZFJ1bGUpXG4gICAgICAgICAgICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICAgICAgICAgICAgY3R4LmxvZ0RlY2lzaW9uKHtcbiAgICAgICAgICAgICAgICAgICAgICAgIGRlY2lzaW9uOiAnYWNjZXB0JyxcbiAgICAgICAgICAgICAgICAgICAgICAgIHNvdXJjZTogeyB0eXBlOiAnY2xhc3NpZmllcicgfSxcbiAgICAgICAgICAgICAgICAgICAgICB9KVxuICAgICAgICAgICAgICAgICAgICAgIHJlc29sdmUoXG4gICAgICAgICAgICAgICAgICAgICAgICBjdHguYnVpbGRBbGxvdyhcbiAgICAgICAgICAgICAgICAgICAgICAgICAgcmVzdWx0LnVwZGF0ZWRJbnB1dCA/P1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgIChpbnB1dCBhcyBSZWNvcmQ8c3RyaW5nLCB1bmtub3duPiksXG4gICAgICAgICAgICAgICAgICAgICAgICAgIHtcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkZWNpc2lvblJlYXNvbjoge1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdHlwZTogJ2NsYXNzaWZpZXInIGFzIGNvbnN0LFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2xhc3NpZmllcjogJ2Jhc2hfYWxsb3cnIGFzIGNvbnN0LFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmVhc29uOiBgQWxsb3dlZCBieSBwcm9tcHQgcnVsZTogXCIke3JhY2VSZXN1bHQucmVzdWx0Lm1hdGNoZWREZXNjcmlwdGlvbn1cImAsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgfSxcbiAgICAgICAgICAgICAgICAgICAgICAgICAgfSxcbiAgICAgICAgICAgICAgICAgICAgICAgICksXG4gICAgICAgICAgICAgICAgICAgICAgKVxuICAgICAgICAgICAgICAgICAgICAgIHJldHVyblxuICAgICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgICAgIC8vIFRpbWVvdXQgb3Igbm8gbWF0Y2gg4oCUIGZhbGwgdGhyb3VnaCB0byBzaG93IGRpYWxvZ1xuICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIC8vIFNob3cgZGlhbG9nIGFuZCBzdGFydCBob29rcy9jbGFzc2lmaWVyIGluIGJhY2tncm91bmRcbiAgICAgICAgICAgICAgICBoYW5kbGVJbnRlcmFjdGl2ZVBlcm1pc3Npb24oXG4gICAgICAgICAgICAgICAgICB7XG4gICAgICAgICAgICAgICAgICAgIGN0eCxcbiAgICAgICAgICAgICAgICAgICAgZGVzY3JpcHRpb24sXG4gICAgICAgICAgICAgICAgICAgIHJlc3VsdCxcbiAgICAgICAgICAgICAgICAgICAgYXdhaXRBdXRvbWF0ZWRDaGVja3NCZWZvcmVEaWFsb2c6XG4gICAgICAgICAgICAgICAgICAgICAgYXBwU3RhdGUudG9vbFBlcm1pc3Npb25Db250ZXh0XG4gICAgICAgICAgICAgICAgICAgICAgICAuYXdhaXRBdXRvbWF0ZWRDaGVja3NCZWZvcmVEaWFsb2csXG4gICAgICAgICAgICAgICAgICAgIGJyaWRnZUNhbGxiYWNrczogZmVhdHVyZSgnQlJJREdFX01PREUnKVxuICAgICAgICAgICAgICAgICAgICAgID8gYXBwU3RhdGUucmVwbEJyaWRnZVBlcm1pc3Npb25DYWxsYmFja3NcbiAgICAgICAgICAgICAgICAgICAgICA6IHVuZGVmaW5lZCxcbiAgICAgICAgICAgICAgICAgICAgY2hhbm5lbENhbGxiYWNrczpcbiAgICAgICAgICAgICAgICAgICAgICBmZWF0dXJlKCdLQUlST1MnKSB8fCBmZWF0dXJlKCdLQUlST1NfQ0hBTk5FTFMnKVxuICAgICAgICAgICAgICAgICAgICAgICAgPyBhcHBTdGF0ZS5jaGFubmVsUGVybWlzc2lvbkNhbGxiYWNrc1xuICAgICAgICAgICAgICAgICAgICAgICAgOiB1bmRlZmluZWQsXG4gICAgICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICAgICAgcmVzb2x2ZSxcbiAgICAgICAgICAgICAgICApXG5cbiAgICAgICAgICAgICAgICByZXR1cm5cbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICAgIH0pXG4gICAgICAgICAgLmNhdGNoKGVycm9yID0+IHtcbiAgICAgICAgICAgIGlmIChcbiAgICAgICAgICAgICAgZXJyb3IgaW5zdGFuY2VvZiBBYm9ydEVycm9yIHx8XG4gICAgICAgICAgICAgIGVycm9yIGluc3RhbmNlb2YgQVBJVXNlckFib3J0RXJyb3JcbiAgICAgICAgICAgICkge1xuICAgICAgICAgICAgICBsb2dGb3JEZWJ1Z2dpbmcoXG4gICAgICAgICAgICAgICAgYFBlcm1pc3Npb24gY2hlY2sgdGhyZXcgJHtlcnJvci5jb25zdHJ1Y3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FQUFBO0VBQUE7SUFBQUEsRUFBQSxHQUFBRixDQUFBO0VBQUE7RUFBQSxPQWhTSUUsRUFrU047QUFBQTtBQXhTSCxTQUFBa0QsT0FBQWlCLEdBQUE7RUFBQSxPQTZNd0JDLFVBQVUsQ0FBQ0QsR0FBRyxFQUFFLElBQUksRUFBRTtJQUFBekQsSUFBQSxFQUFRLFNBQVMsSUFBSTZDO0VBQU0sQ0FBQyxDQUFDO0FBQUE7QUE3TTNFLFNBQUFOLE1BQUFvQixDQUFBO0VBQUEsT0F1TW9EO0lBQUEzRCxJQUFBLEVBQ3RCLFFBQVEsSUFBSTZDLEtBQUs7SUFBQWhELE1BQUEsRUFDZjhEO0VBQ1YsQ0FBQztBQUFBO0FBaUd2QixlQUFlMUUsYUFBYSIsImlnbm9yZUxpc3QiOltdfQ== \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/hooks/useClipboardImageHint.ts b/_all-in-one-upload/cc-haha/src/hooks/useClipboardImageHint.ts new file mode 100644 index 0000000..48aa528 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useClipboardImageHint.ts @@ -0,0 +1,77 @@ +import { useEffect, useRef } from 'react' +import { useNotifications } from '../context/notifications.js' +import { getShortcutDisplay } from '../keybindings/shortcutFormat.js' +import { hasImageInClipboard } from '../utils/imagePaste.js' + +const NOTIFICATION_KEY = 'clipboard-image-hint' +// Small debounce to batch rapid focus changes +const FOCUS_CHECK_DEBOUNCE_MS = 1000 +// Don't show the hint more than once per this interval +const HINT_COOLDOWN_MS = 30000 + +/** + * Hook that shows a notification when the terminal regains focus + * and the clipboard contains an image. + * + * @param isFocused - Whether the terminal is currently focused + * @param enabled - Whether image paste is enabled (onImagePaste is defined) + */ +export function useClipboardImageHint( + isFocused: boolean, + enabled: boolean, +): void { + const { addNotification } = useNotifications() + const lastFocusedRef = useRef(isFocused) + const lastHintTimeRef = useRef(0) + const checkTimeoutRef = useRef(null) + + useEffect(() => { + // Only trigger on focus regain (was unfocused, now focused) + const wasFocused = lastFocusedRef.current + lastFocusedRef.current = isFocused + + if (!enabled || !isFocused || wasFocused) { + return + } + + // Clear any pending check + if (checkTimeoutRef.current) { + clearTimeout(checkTimeoutRef.current) + } + + // Small debounce to batch rapid focus changes + checkTimeoutRef.current = setTimeout( + async (checkTimeoutRef, lastHintTimeRef, addNotification) => { + checkTimeoutRef.current = null + + // Check cooldown to avoid spamming the user + const now = Date.now() + if (now - lastHintTimeRef.current < HINT_COOLDOWN_MS) { + return + } + + // Check if clipboard has an image (async osascript call) + if (await hasImageInClipboard()) { + lastHintTimeRef.current = now + addNotification({ + key: NOTIFICATION_KEY, + text: `Image in clipboard · ${getShortcutDisplay('chat:imagePaste', 'Chat', 'ctrl+v')} to paste`, + priority: 'immediate', + timeoutMs: 8000, + }) + } + }, + FOCUS_CHECK_DEBOUNCE_MS, + checkTimeoutRef, + lastHintTimeRef, + addNotification, + ) + + return () => { + if (checkTimeoutRef.current) { + clearTimeout(checkTimeoutRef.current) + checkTimeoutRef.current = null + } + } + }, [isFocused, enabled, addNotification]) +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useDiffData.ts b/_all-in-one-upload/cc-haha/src/hooks/useDiffData.ts new file mode 100644 index 0000000..176bcf0 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useDiffData.ts @@ -0,0 +1,110 @@ +import type { StructuredPatchHunk } from 'diff' +import { useEffect, useMemo, useState } from 'react' +import { + fetchGitDiff, + fetchGitDiffHunks, + type GitDiffResult, + type GitDiffStats, +} from '../utils/gitDiff.js' + +const MAX_LINES_PER_FILE = 400 + +export type DiffFile = { + path: string + linesAdded: number + linesRemoved: number + isBinary: boolean + isLargeFile: boolean + isTruncated: boolean + isNewFile?: boolean + isUntracked?: boolean +} + +export type DiffData = { + stats: GitDiffStats | null + files: DiffFile[] + hunks: Map + loading: boolean +} + +/** + * Hook to fetch current git diff data on demand. + * Fetches both stats and hunks when component mounts. + */ +export function useDiffData(): DiffData { + const [diffResult, setDiffResult] = useState(null) + const [hunks, setHunks] = useState>( + new Map(), + ) + const [loading, setLoading] = useState(true) + + // Fetch diff data on mount + useEffect(() => { + let cancelled = false + + async function loadDiffData() { + try { + // Fetch both stats and hunks + const [statsResult, hunksResult] = await Promise.all([ + fetchGitDiff(), + fetchGitDiffHunks(), + ]) + + if (!cancelled) { + setDiffResult(statsResult) + setHunks(hunksResult) + setLoading(false) + } + } catch (_error) { + if (!cancelled) { + setDiffResult(null) + setHunks(new Map()) + setLoading(false) + } + } + } + + void loadDiffData() + + return () => { + cancelled = true + } + }, []) + + return useMemo(() => { + if (!diffResult) { + return { stats: null, files: [], hunks: new Map(), loading } + } + + const { stats, perFileStats } = diffResult + const files: DiffFile[] = [] + + // Iterate over perFileStats to get all files including large/skipped ones + for (const [path, fileStats] of perFileStats) { + const fileHunks = hunks.get(path) + const isUntracked = fileStats.isUntracked ?? false + + // Detect large file (in perFileStats but not in hunks, and not binary/untracked) + const isLargeFile = !fileStats.isBinary && !isUntracked && !fileHunks + + // Detect truncated file (total > limit means we truncated) + const totalLines = fileStats.added + fileStats.removed + const isTruncated = + !isLargeFile && !fileStats.isBinary && totalLines > MAX_LINES_PER_FILE + + files.push({ + path, + linesAdded: fileStats.added, + linesRemoved: fileStats.removed, + isBinary: fileStats.isBinary, + isLargeFile, + isTruncated, + isUntracked, + }) + } + + files.sort((a, b) => a.path.localeCompare(b.path)) + + return { stats, files, hunks, loading: false } + }, [diffResult, hunks, loading]) +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useExitOnCtrlCDWithKeybindings.ts b/_all-in-one-upload/cc-haha/src/hooks/useExitOnCtrlCDWithKeybindings.ts new file mode 100644 index 0000000..7f30f55 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useExitOnCtrlCDWithKeybindings.ts @@ -0,0 +1,24 @@ +import { useKeybindings } from '../keybindings/useKeybinding.js' +import { type ExitState, useExitOnCtrlCD } from './useExitOnCtrlCD.js' + +export type { ExitState } + +/** + * Convenience hook that wires up useExitOnCtrlCD with useKeybindings. + * + * This is the standard way to use useExitOnCtrlCD in components. + * The separation exists to avoid import cycles - useExitOnCtrlCD.ts + * doesn't import from the keybindings module directly. + * + * @param onExit - Optional custom exit handler + * @param onInterrupt - Optional callback for features to handle interrupt (ctrl+c). + * Return true if handled, false to fall through to double-press exit. + * @param isActive - Whether the keybinding is active (default true). + */ +export function useExitOnCtrlCDWithKeybindings( + onExit?: () => void, + onInterrupt?: () => boolean, + isActive?: boolean, +): ExitState { + return useExitOnCtrlCD(useKeybindings, onInterrupt, onExit, isActive) +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useIdeConnectionStatus.ts b/_all-in-one-upload/cc-haha/src/hooks/useIdeConnectionStatus.ts new file mode 100644 index 0000000..418e3dc --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useIdeConnectionStatus.ts @@ -0,0 +1,33 @@ +import { useMemo } from 'react' +import type { MCPServerConnection } from '../services/mcp/types.js' + +export type IdeStatus = 'connected' | 'disconnected' | 'pending' | null + +type IdeConnectionResult = { + status: IdeStatus + ideName: string | null +} + +export function useIdeConnectionStatus( + mcpClients?: MCPServerConnection[], +): IdeConnectionResult { + return useMemo(() => { + const ideClient = mcpClients?.find(client => client.name === 'ide') + if (!ideClient) { + return { status: null, ideName: null } + } + // Extract IDE name from config if available + const config = ideClient.config + const ideName = + config.type === 'sse-ide' || config.type === 'ws-ide' + ? config.ideName + : null + if (ideClient.type === 'connected') { + return { status: 'connected', ideName } + } + if (ideClient.type === 'pending') { + return { status: 'pending', ideName } + } + return { status: 'disconnected', ideName } + }, [mcpClients]) +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useIssueFlagBanner.ts b/_all-in-one-upload/cc-haha/src/hooks/useIssueFlagBanner.ts new file mode 100644 index 0000000..adb3083 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useIssueFlagBanner.ts @@ -0,0 +1,133 @@ +import { useMemo, useRef } from 'react' +import { BASH_TOOL_NAME } from '../tools/BashTool/toolName.js' +import type { Message } from '../types/message.js' +import { getUserMessageText } from '../utils/messages.js' + +const EXTERNAL_COMMAND_PATTERNS = [ + /\bcurl\b/, + /\bwget\b/, + /\bssh\b/, + /\bkubectl\b/, + /\bsrun\b/, + /\bdocker\b/, + /\bbq\b/, + /\bgsutil\b/, + /\bgcloud\b/, + /\baws\b/, + /\bgit\s+push\b/, + /\bgit\s+pull\b/, + /\bgit\s+fetch\b/, + /\bgh\s+(pr|issue)\b/, + /\bnc\b/, + /\bncat\b/, + /\btelnet\b/, + /\bftp\b/, +] + +const FRICTION_PATTERNS = [ + // "No," or "No!" at start — comma/exclamation implies correction tone + // (avoids "No problem", "No thanks", "No I think we should...") + /^no[,!]\s/i, + // Direct corrections about Claude's output + /\bthat'?s (wrong|incorrect|not (what|right|correct))\b/i, + /\bnot what I (asked|wanted|meant|said)\b/i, + // Referencing prior instructions Claude missed + /\bI (said|asked|wanted|told you|already said)\b/i, + // Questioning Claude's actions + /\bwhy did you\b/i, + /\byou should(n'?t| not)? have\b/i, + /\byou were supposed to\b/i, + // Explicit retry/revert of Claude's work + /\btry again\b/i, + /\b(undo|revert) (that|this|it|what you)\b/i, +] + +export function isSessionContainerCompatible(messages: Message[]): boolean { + for (const msg of messages) { + if (msg.type !== 'assistant') { + continue + } + const content = msg.message.content + if (!Array.isArray(content)) { + continue + } + for (const block of content) { + if (block.type !== 'tool_use' || !('name' in block)) { + continue + } + const toolName = block.name as string + if (toolName.startsWith('mcp__')) { + return false + } + if (toolName === BASH_TOOL_NAME) { + const input = (block as { input?: Record }).input + const command = (input?.command as string) || '' + if (EXTERNAL_COMMAND_PATTERNS.some(p => p.test(command))) { + return false + } + } + } + } + return true +} + +export function hasFrictionSignal(messages: Message[]): boolean { + for (let i = messages.length - 1; i >= 0; i--) { + const msg = messages[i]! + if (msg.type !== 'user') { + continue + } + const text = getUserMessageText(msg) + if (!text) { + continue + } + return FRICTION_PATTERNS.some(p => p.test(text)) + } + return false +} + +const MIN_SUBMIT_COUNT = 3 +const COOLDOWN_MS = 30 * 60 * 1000 + +export function useIssueFlagBanner( + messages: Message[], + submitCount: number, +): boolean { + if (process.env.USER_TYPE !== 'ant') { + return false + } + + // biome-ignore lint/correctness/useHookAtTopLevel: process.env.USER_TYPE is a compile-time constant + const lastTriggeredAtRef = useRef(0) + // biome-ignore lint/correctness/useHookAtTopLevel: process.env.USER_TYPE is a compile-time constant + const activeForSubmitRef = useRef(-1) + + // Memoize the O(messages) scans. This hook runs on every REPL render + // (including every keystroke), but messages is stable during typing. + // isSessionContainerCompatible walks all messages + regex-tests each + // bash command — by far the heaviest work here. + // biome-ignore lint/correctness/useHookAtTopLevel: process.env.USER_TYPE is a compile-time constant + const shouldTrigger = useMemo( + () => isSessionContainerCompatible(messages) && hasFrictionSignal(messages), + [messages], + ) + + // Keep showing the banner until the user submits another message + if (activeForSubmitRef.current === submitCount) { + return true + } + + if (Date.now() - lastTriggeredAtRef.current < COOLDOWN_MS) { + return false + } + if (submitCount < MIN_SUBMIT_COUNT) { + return false + } + if (!shouldTrigger) { + return false + } + + lastTriggeredAtRef.current = Date.now() + activeForSubmitRef.current = submitCount + return true +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/usePluginRecommendationBase.tsx b/_all-in-one-upload/cc-haha/src/hooks/usePluginRecommendationBase.tsx new file mode 100644 index 0000000..9a2a2d4 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/usePluginRecommendationBase.tsx @@ -0,0 +1,105 @@ +import { c as _c } from "react/compiler-runtime"; +/** + * Shared state machine + install helper for plugin-recommendation hooks + * (LSP, claude-code-hint). Centralizes the gate chain, async-guard, + * and success/failure notification JSX so new sources stay small. + */ + +import figures from 'figures'; +import * as React from 'react'; +import { getIsRemoteMode } from '../bootstrap/state.js'; +import type { useNotifications } from '../context/notifications.js'; +import { Text } from '../ink.js'; +import { logError } from '../utils/log.js'; +import { getPluginById } from '../utils/plugins/marketplaceManager.js'; +type AddNotification = ReturnType['addNotification']; +type PluginData = NonNullable>>; + +/** + * Call tryResolve inside a useEffect; it applies standard gates (remote + * mode, already-showing, in-flight) then runs resolve(). Non-null return + * becomes the recommendation. Include tryResolve in effect deps — its + * identity tracks recommendation, so clearing re-triggers resolution. + */ +export function usePluginRecommendationBase() { + const $ = _c(6); + const [recommendation, setRecommendation] = React.useState(null); + const isCheckingRef = React.useRef(false); + let t0; + if ($[0] !== recommendation) { + t0 = resolve => { + if (getIsRemoteMode()) { + return; + } + if (recommendation) { + return; + } + if (isCheckingRef.current) { + return; + } + isCheckingRef.current = true; + resolve().then(rec => { + if (rec) { + setRecommendation(rec); + } + }).catch(logError).finally(() => { + isCheckingRef.current = false; + }); + }; + $[0] = recommendation; + $[1] = t0; + } else { + t0 = $[1]; + } + const tryResolve = t0; + let t1; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t1 = () => setRecommendation(null); + $[2] = t1; + } else { + t1 = $[2]; + } + const clearRecommendation = t1; + let t2; + if ($[3] !== recommendation || $[4] !== tryResolve) { + t2 = { + recommendation, + clearRecommendation, + tryResolve + }; + $[3] = recommendation; + $[4] = tryResolve; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +} + +/** Look up plugin, run install(), emit standard success/failure notification. */ +export async function installPluginAndNotify(pluginId: string, pluginName: string, keyPrefix: string, addNotification: AddNotification, install: (pluginData: PluginData) => Promise): Promise { + try { + const pluginData = await getPluginById(pluginId); + if (!pluginData) { + throw new Error(`Plugin ${pluginId} not found in marketplace`); + } + await install(pluginData); + addNotification({ + key: `${keyPrefix}-installed`, + jsx: + {figures.tick} {pluginName} installed · restart to apply + , + priority: 'immediate', + timeoutMs: 5000 + }); + } catch (error) { + logError(error); + addNotification({ + key: `${keyPrefix}-install-failed`, + jsx: Failed to install {pluginName}, + priority: 'immediate', + timeoutMs: 5000 + }); + } +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJmaWd1cmVzIiwiUmVhY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2RLLEdBQUcsRUFBRSxHQUFHTixTQUFTLFlBQVk7TUFDN0JPLEdBQUcsRUFDRCxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUMsU0FBUztBQUM3QixVQUFVLENBQUMzQyxPQUFPLENBQUM0QyxJQUFJLENBQUMsQ0FBQyxDQUFDVCxVQUFVLENBQUM7QUFDckMsUUFBUSxFQUFFLElBQUksQ0FDUDtNQUNEVSxRQUFRLEVBQUUsV0FBVztNQUNyQkMsU0FBUyxFQUFFO0lBQ2IsQ0FBQyxDQUFDO0VBQ0osQ0FBQyxDQUFDLE9BQU9DLEtBQUssRUFBRTtJQUNkMUMsUUFBUSxDQUFDMEMsS0FBSyxDQUFDO0lBQ2ZWLGVBQWUsQ0FBQztNQUNkSyxHQUFHLEVBQUUsR0FBR04sU0FBUyxpQkFBaUI7TUFDbENPLEdBQUcsRUFBRSxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUMsT0FBTyxDQUFDLGtCQUFrQixDQUFDUixVQUFVLENBQUMsRUFBRSxJQUFJLENBQUM7TUFDOURVLFFBQVEsRUFBRSxXQUFXO01BQ3JCQyxTQUFTLEVBQUU7SUFDYixDQUFDLENBQUM7RUFDSjtBQUNGIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/hooks/useTerminalSize.ts b/_all-in-one-upload/cc-haha/src/hooks/useTerminalSize.ts new file mode 100644 index 0000000..68e24df --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useTerminalSize.ts @@ -0,0 +1,15 @@ +import { useContext } from 'react' +import { + type TerminalSize, + TerminalSizeContext, +} from 'src/ink/components/TerminalSizeContext.js' + +export function useTerminalSize(): TerminalSize { + const size = useContext(TerminalSizeContext) + + if (!size) { + throw new Error('useTerminalSize must be used within an Ink App component') + } + + return size +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useUpdateNotification.ts b/_all-in-one-upload/cc-haha/src/hooks/useUpdateNotification.ts new file mode 100644 index 0000000..c9a7b2a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useUpdateNotification.ts @@ -0,0 +1,34 @@ +import { useState } from 'react' +import { major, minor, patch } from 'semver' + +export function getSemverPart(version: string): string { + return `${major(version, { loose: true })}.${minor(version, { loose: true })}.${patch(version, { loose: true })}` +} + +export function shouldShowUpdateNotification( + updatedVersion: string, + lastNotifiedSemver: string | null, +): boolean { + const updatedSemver = getSemverPart(updatedVersion) + return updatedSemver !== lastNotifiedSemver +} + +export function useUpdateNotification( + updatedVersion: string | null | undefined, + initialVersion: string = MACRO.VERSION, +): string | null { + const [lastNotifiedSemver, setLastNotifiedSemver] = useState( + () => getSemverPart(initialVersion), + ) + + if (!updatedVersion) { + return null + } + + const updatedSemver = getSemverPart(updatedVersion) + if (updatedSemver !== lastNotifiedSemver) { + setLastNotifiedSemver(updatedSemver) + return updatedSemver + } + return null +} diff --git a/_all-in-one-upload/cc-haha/src/hooks/useVoiceEnabled.ts b/_all-in-one-upload/cc-haha/src/hooks/useVoiceEnabled.ts new file mode 100644 index 0000000..ece0691 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/hooks/useVoiceEnabled.ts @@ -0,0 +1,25 @@ +import { useMemo } from 'react' +import { useAppState } from '../state/AppState.js' +import { + hasVoiceAuth, + isVoiceGrowthBookEnabled, +} from '../voice/voiceModeEnabled.js' + +/** + * Combines user intent (settings.voiceEnabled) with auth + GB kill-switch. + * Only the auth half is memoized on authVersion — it's the expensive one + * (cold getClaudeAIOAuthTokens memoize → sync `security` spawn, ~60ms/call, + * ~180ms total in profile v5 when token refresh cleared the cache mid-session). + * GB is a cheap cached-map lookup and stays outside the memo so a mid-session + * kill-switch flip still takes effect on the next render. + * + * authVersion bumps on /login only. Background token refresh leaves it alone + * (user is still authed), so the auth memo stays correct without re-eval. + */ +export function useVoiceEnabled(): boolean { + const userIntent = useAppState(s => s.settings.voiceEnabled === true) + const authVersion = useAppState(s => s.authVersion) + // eslint-disable-next-line react-hooks/exhaustive-deps + const authed = useMemo(hasVoiceAuth, [authVersion]) + return userIntent && authed && isVoiceGrowthBookEnabled() +} diff --git a/_all-in-one-upload/cc-haha/src/ink/clearTerminal.ts b/_all-in-one-upload/cc-haha/src/ink/clearTerminal.ts new file mode 100644 index 0000000..38d4a68 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/clearTerminal.ts @@ -0,0 +1,74 @@ +/** + * Cross-platform terminal clearing with scrollback support. + * Detects modern terminals that support ESC[3J for clearing scrollback. + */ + +import { + CURSOR_HOME, + csi, + ERASE_SCREEN, + ERASE_SCROLLBACK, +} from './termio/csi.js' + +// HVP (Horizontal Vertical Position) - legacy Windows cursor home +const CURSOR_HOME_WINDOWS = csi(0, 'f') + +function isWindowsTerminal(): boolean { + return process.platform === 'win32' && !!process.env.WT_SESSION +} + +function isMintty(): boolean { + // mintty 3.1.5+ sets TERM_PROGRAM to 'mintty' + if (process.env.TERM_PROGRAM === 'mintty') { + return true + } + // GitBash/MSYS2/MINGW use mintty and set MSYSTEM + if (process.platform === 'win32' && process.env.MSYSTEM) { + return true + } + return false +} + +function isModernWindowsTerminal(): boolean { + // Windows Terminal sets WT_SESSION environment variable + if (isWindowsTerminal()) { + return true + } + + // VS Code integrated terminal on Windows with ConPTY support + if ( + process.platform === 'win32' && + process.env.TERM_PROGRAM === 'vscode' && + process.env.TERM_PROGRAM_VERSION + ) { + return true + } + + // mintty (GitBash/MSYS2/Cygwin) supports modern escape sequences + if (isMintty()) { + return true + } + + return false +} + +/** + * Returns the ANSI escape sequence to clear the terminal including scrollback. + * Automatically detects terminal capabilities. + */ +export function getClearTerminalSequence(): string { + if (process.platform === 'win32') { + if (isModernWindowsTerminal()) { + return ERASE_SCREEN + ERASE_SCROLLBACK + CURSOR_HOME + } else { + // Legacy Windows console - can't clear scrollback + return ERASE_SCREEN + CURSOR_HOME_WINDOWS + } + } + return ERASE_SCREEN + ERASE_SCROLLBACK + CURSOR_HOME +} + +/** + * Clears the terminal screen. On supported terminals, also clears scrollback. + */ +export const clearTerminal = getClearTerminalSequence() diff --git a/_all-in-one-upload/cc-haha/src/ink/components/RawAnsi.tsx b/_all-in-one-upload/cc-haha/src/ink/components/RawAnsi.tsx new file mode 100644 index 0000000..919e453 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/components/RawAnsi.tsx @@ -0,0 +1,57 @@ +import { c as _c } from "react/compiler-runtime"; +import React from 'react'; +type Props = { + /** + * Pre-rendered ANSI lines. Each element must be exactly one terminal row + * (already wrapped to `width` by the producer) with ANSI escape codes inline. + */ + lines: string[]; + /** Column width the producer wrapped to. Sent to Yoga as the fixed leaf width. */ + width: number; +}; + +/** + * Bypass the → React tree → Yoga → squash → re-serialize roundtrip for + * content that is already terminal-ready. + * + * Use this when an external renderer (e.g. the ColorDiff NAPI module) has + * already produced ANSI-escaped, width-wrapped output. A normal mount + * reparses that output into one React per style span, lays out each + * span as a Yoga flex child, then walks the tree to re-emit the same escape + * codes it was given. For a long transcript full of syntax-highlighted diffs + * that roundtrip is the dominant cost of the render. + * + * This component emits a single Yoga leaf with a constant-time measure func + * (width × lines.length) and hands the joined string straight to output.write(), + * which already splits on '\n' and parses ANSI into the screen buffer. + */ +export function RawAnsi(t0) { + const $ = _c(6); + const { + lines, + width + } = t0; + if (lines.length === 0) { + return null; + } + let t1; + if ($[0] !== lines) { + t1 = lines.join("\n"); + $[0] = lines; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; + if ($[2] !== lines.length || $[3] !== t1 || $[4] !== width) { + t2 = ; + $[2] = lines.length; + $[3] = t1; + $[4] = width; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIlByb3BzIiwibGluZXMiLCJ3aWR0aCIsIlJhd0Fuc2kiLCJ0MCIsIiQiLCJfYyIsImxlbmd0aCIsInQxIiwiam9pbiIsInQyIl0sInNvdXJjZXMiOls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lTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/ink/events/dispatcher.ts b/_all-in-one-upload/cc-haha/src/ink/events/dispatcher.ts new file mode 100644 index 0000000..a310d38 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/events/dispatcher.ts @@ -0,0 +1,233 @@ +import { + ContinuousEventPriority, + DefaultEventPriority, + DiscreteEventPriority, + NoEventPriority, +} from 'react-reconciler/constants.js' +import { logError } from '../../utils/log.js' +import { HANDLER_FOR_EVENT } from './event-handlers.js' +import type { EventTarget, TerminalEvent } from './terminal-event.js' + +// -- + +type DispatchListener = { + node: EventTarget + handler: (event: TerminalEvent) => void + phase: 'capturing' | 'at_target' | 'bubbling' +} + +function getHandler( + node: EventTarget, + eventType: string, + capture: boolean, +): ((event: TerminalEvent) => void) | undefined { + const handlers = node._eventHandlers + if (!handlers) return undefined + + const mapping = HANDLER_FOR_EVENT[eventType] + if (!mapping) return undefined + + const propName = capture ? mapping.capture : mapping.bubble + if (!propName) return undefined + + return handlers[propName] as ((event: TerminalEvent) => void) | undefined +} + +/** + * Collect all listeners for an event in dispatch order. + * + * Uses react-dom's two-phase accumulation pattern: + * - Walk from target to root + * - Capture handlers are prepended (unshift) → root-first + * - Bubble handlers are appended (push) → target-first + * + * Result: [root-cap, ..., parent-cap, target-cap, target-bub, parent-bub, ..., root-bub] + */ +function collectListeners( + target: EventTarget, + event: TerminalEvent, +): DispatchListener[] { + const listeners: DispatchListener[] = [] + + let node: EventTarget | undefined = target + while (node) { + const isTarget = node === target + + const captureHandler = getHandler(node, event.type, true) + const bubbleHandler = getHandler(node, event.type, false) + + if (captureHandler) { + listeners.unshift({ + node, + handler: captureHandler, + phase: isTarget ? 'at_target' : 'capturing', + }) + } + + if (bubbleHandler && (event.bubbles || isTarget)) { + listeners.push({ + node, + handler: bubbleHandler, + phase: isTarget ? 'at_target' : 'bubbling', + }) + } + + node = node.parentNode + } + + return listeners +} + +/** + * Execute collected listeners with propagation control. + * + * Before each handler, calls event._prepareForTarget(node) so event + * subclasses can do per-node setup. + */ +function processDispatchQueue( + listeners: DispatchListener[], + event: TerminalEvent, +): void { + let previousNode: EventTarget | undefined + + for (const { node, handler, phase } of listeners) { + if (event._isImmediatePropagationStopped()) { + break + } + + if (event._isPropagationStopped() && node !== previousNode) { + break + } + + event._setEventPhase(phase) + event._setCurrentTarget(node) + event._prepareForTarget(node) + + try { + handler(event) + } catch (error) { + logError(error) + } + + previousNode = node + } +} + +// -- + +/** + * Map terminal event types to React scheduling priorities. + * Mirrors react-dom's getEventPriority() switch. + */ +function getEventPriority(eventType: string): number { + switch (eventType) { + case 'keydown': + case 'keyup': + case 'click': + case 'focus': + case 'blur': + case 'paste': + return DiscreteEventPriority as number + case 'resize': + case 'scroll': + case 'mousemove': + return ContinuousEventPriority as number + default: + return DefaultEventPriority as number + } +} + +// -- + +type DiscreteUpdates = ( + fn: (a: A, b: B) => boolean, + a: A, + b: B, + c: undefined, + d: undefined, +) => boolean + +/** + * Owns event dispatch state and the capture/bubble dispatch loop. + * + * The reconciler host config reads currentEvent and currentUpdatePriority + * to implement resolveUpdatePriority, resolveEventType, and + * resolveEventTimeStamp — mirroring how react-dom's host config reads + * ReactDOMSharedInternals and window.event. + * + * discreteUpdates is injected after construction (by InkReconciler) + * to break the import cycle. + */ +export class Dispatcher { + currentEvent: TerminalEvent | null = null + currentUpdatePriority: number = DefaultEventPriority as number + discreteUpdates: DiscreteUpdates | null = null + + /** + * Infer event priority from the currently-dispatching event. + * Called by the reconciler host config's resolveUpdatePriority + * when no explicit priority has been set. + */ + resolveEventPriority(): number { + if (this.currentUpdatePriority !== (NoEventPriority as number)) { + return this.currentUpdatePriority + } + if (this.currentEvent) { + return getEventPriority(this.currentEvent.type) + } + return DefaultEventPriority as number + } + + /** + * Dispatch an event through capture and bubble phases. + * Returns true if preventDefault() was NOT called. + */ + dispatch(target: EventTarget, event: TerminalEvent): boolean { + const previousEvent = this.currentEvent + this.currentEvent = event + try { + event._setTarget(target) + + const listeners = collectListeners(target, event) + processDispatchQueue(listeners, event) + + event._setEventPhase('none') + event._setCurrentTarget(null) + + return !event.defaultPrevented + } finally { + this.currentEvent = previousEvent + } + } + + /** + * Dispatch with discrete (sync) priority. + * For user-initiated events: keyboard, click, focus, paste. + */ + dispatchDiscrete(target: EventTarget, event: TerminalEvent): boolean { + if (!this.discreteUpdates) { + return this.dispatch(target, event) + } + return this.discreteUpdates( + (t, e) => this.dispatch(t, e), + target, + event, + undefined, + undefined, + ) + } + + /** + * Dispatch with continuous priority. + * For high-frequency events: resize, scroll, mouse move. + */ + dispatchContinuous(target: EventTarget, event: TerminalEvent): boolean { + const previousPriority = this.currentUpdatePriority + try { + this.currentUpdatePriority = ContinuousEventPriority as number + return this.dispatch(target, event) + } finally { + this.currentUpdatePriority = previousPriority + } + } +} diff --git a/_all-in-one-upload/cc-haha/src/ink/events/terminal-event.ts b/_all-in-one-upload/cc-haha/src/ink/events/terminal-event.ts new file mode 100644 index 0000000..9a86bf8 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/events/terminal-event.ts @@ -0,0 +1,107 @@ +import { Event } from './event.js' + +type EventPhase = 'none' | 'capturing' | 'at_target' | 'bubbling' + +type TerminalEventInit = { + bubbles?: boolean + cancelable?: boolean +} + +/** + * Base class for all terminal events with DOM-style propagation. + * + * Extends Event so existing event types (ClickEvent, InputEvent, + * TerminalFocusEvent) share a common ancestor and can migrate later. + * + * Mirrors the browser's Event API: target, currentTarget, eventPhase, + * stopPropagation(), preventDefault(), timeStamp. + */ +export class TerminalEvent extends Event { + readonly type: string + readonly timeStamp: number + readonly bubbles: boolean + readonly cancelable: boolean + + private _target: EventTarget | null = null + private _currentTarget: EventTarget | null = null + private _eventPhase: EventPhase = 'none' + private _propagationStopped = false + private _defaultPrevented = false + + constructor(type: string, init?: TerminalEventInit) { + super() + this.type = type + this.timeStamp = performance.now() + this.bubbles = init?.bubbles ?? true + this.cancelable = init?.cancelable ?? true + } + + get target(): EventTarget | null { + return this._target + } + + get currentTarget(): EventTarget | null { + return this._currentTarget + } + + get eventPhase(): EventPhase { + return this._eventPhase + } + + get defaultPrevented(): boolean { + return this._defaultPrevented + } + + stopPropagation(): void { + this._propagationStopped = true + } + + override stopImmediatePropagation(): void { + super.stopImmediatePropagation() + this._propagationStopped = true + } + + preventDefault(): void { + if (this.cancelable) { + this._defaultPrevented = true + } + } + + // -- Internal setters used by the Dispatcher + + /** @internal */ + _setTarget(target: EventTarget): void { + this._target = target + } + + /** @internal */ + _setCurrentTarget(target: EventTarget | null): void { + this._currentTarget = target + } + + /** @internal */ + _setEventPhase(phase: EventPhase): void { + this._eventPhase = phase + } + + /** @internal */ + _isPropagationStopped(): boolean { + return this._propagationStopped + } + + /** @internal */ + _isImmediatePropagationStopped(): boolean { + return this.didStopImmediatePropagation() + } + + /** + * Hook for subclasses to do per-node setup before each handler fires. + * Default is a no-op. + */ + _prepareForTarget(_target: EventTarget): void {} +} + +export type EventTarget = { + parentNode: EventTarget | undefined + _eventHandlers?: Record +} diff --git a/_all-in-one-upload/cc-haha/src/ink/hooks/use-terminal-title.ts b/_all-in-one-upload/cc-haha/src/ink/hooks/use-terminal-title.ts new file mode 100644 index 0000000..d820cd7 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/hooks/use-terminal-title.ts @@ -0,0 +1,31 @@ +import { useContext, useEffect } from 'react' +import stripAnsi from 'strip-ansi' +import { OSC, osc } from '../termio/osc.js' +import { TerminalWriteContext } from '../useTerminalNotification.js' + +/** + * Declaratively set the terminal tab/window title. + * + * Pass a string to set the title. ANSI escape sequences are stripped + * automatically so callers don't need to know about terminal encoding. + * Pass `null` to opt out — the hook becomes a no-op and leaves the + * terminal title untouched. + * + * On Windows, uses `process.title` (classic conhost doesn't support OSC). + * Elsewhere, writes OSC 0 (set title+icon) via Ink's stdout. + */ +export function useTerminalTitle(title: string | null): void { + const writeRaw = useContext(TerminalWriteContext) + + useEffect(() => { + if (title === null || !writeRaw) return + + const clean = stripAnsi(title) + + if (process.platform === 'win32') { + process.title = clean + } else { + writeRaw(osc(OSC.SET_TITLE_AND_ICON, clean)) + } + }, [title, writeRaw]) +} diff --git a/_all-in-one-upload/cc-haha/src/ink/layout/engine.ts b/_all-in-one-upload/cc-haha/src/ink/layout/engine.ts new file mode 100644 index 0000000..38f6dcb --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/layout/engine.ts @@ -0,0 +1,6 @@ +import type { LayoutNode } from './node.js' +import { createYogaLayoutNode } from './yoga.js' + +export function createLayoutNode(): LayoutNode { + return createYogaLayoutNode() +} diff --git a/_all-in-one-upload/cc-haha/src/ink/measure-element.ts b/_all-in-one-upload/cc-haha/src/ink/measure-element.ts new file mode 100644 index 0000000..ed56eaf --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/measure-element.ts @@ -0,0 +1,23 @@ +import type { DOMElement } from './dom.js' + +type Output = { + /** + * Element width. + */ + width: number + + /** + * Element height. + */ + height: number +} + +/** + * Measure the dimensions of a particular `` element. + */ +const measureElement = (node: DOMElement): Output => ({ + width: node.yogaNode?.getComputedWidth() ?? 0, + height: node.yogaNode?.getComputedHeight() ?? 0, +}) + +export default measureElement diff --git a/_all-in-one-upload/cc-haha/src/ink/render-node-to-output.ts b/_all-in-one-upload/cc-haha/src/ink/render-node-to-output.ts new file mode 100644 index 0000000..73bbbbe --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/ink/render-node-to-output.ts @@ -0,0 +1,1462 @@ +import indentString from 'indent-string' +import { applyTextStyles } from './colorize.js' +import type { DOMElement } from './dom.js' +import getMaxWidth from './get-max-width.js' +import type { Rectangle } from './layout/geometry.js' +import { LayoutDisplay, LayoutEdge, type LayoutNode } from './layout/node.js' +import { nodeCache, pendingClears } from './node-cache.js' +import type Output from './output.js' +import renderBorder from './render-border.js' +import type { Screen } from './screen.js' +import { + type StyledSegment, + squashTextNodesToSegments, +} from './squash-text-nodes.js' +import type { Color } from './styles.js' +import { isXtermJs } from './terminal.js' +import { widestLine } from './widest-line.js' +import wrapText from './wrap-text.js' + +// Matches detectXtermJsWheel() in ScrollKeybindingHandler.tsx — the curve +// and drain must agree on terminal detection. TERM_PROGRAM check is the sync +// fallback; isXtermJs() is the authoritative XTVERSION-probe result. +function isXtermJsHost(): boolean { + return process.env.TERM_PROGRAM === 'vscode' || isXtermJs() +} + +// Per-frame scratch: set when any node's yoga position/size differs from +// its cached value, or a child was removed. Read by ink.tsx to decide +// whether the full-damage sledgehammer (PR #20120) is needed this frame. +// Applies on both alt-screen and main-screen. Steady-state frames +// (spinner tick, clock tick, text append into a fixed-height box) don't +// shift layout → narrow damage bounds → O(changed cells) diff instead of +// O(rows×cols). +let layoutShifted = false + +export function resetLayoutShifted(): void { + layoutShifted = false +} + +export function didLayoutShift(): boolean { + return layoutShifted +} + +// DECSTBM scroll optimization hint. When a ScrollBox's scrollTop changes +// between frames (and nothing else moved), log-update.ts can emit a +// hardware scroll (DECSTBM + SU/SD) instead of rewriting the whole +// viewport. top/bottom are 0-indexed inclusive screen rows; delta > 0 = +// content moved up (scrollTop increased, CSI n S). +export type ScrollHint = { top: number; bottom: number; delta: number } +let scrollHint: ScrollHint | null = null + +// Rects of position:absolute nodes from the PREVIOUS frame, used by +// ScrollBox's blit+shift third-pass repair (see usage site). Recorded at +// three paths — full-render nodeCache.set, node-level blit early-return, +// blitEscapingAbsoluteDescendants — so clean-overlay consecutive scrolls +// still have the rect. +let absoluteRectsPrev: Rectangle[] = [] +let absoluteRectsCur: Rectangle[] = [] + +export function resetScrollHint(): void { + scrollHint = null + absoluteRectsPrev = absoluteRectsCur + absoluteRectsCur = [] +} + +export function getScrollHint(): ScrollHint | null { + return scrollHint +} + +// The ScrollBox DOM node (if any) with pendingScrollDelta left after this +// frame's drain. renderer.ts calls markDirty(it) post-render so the NEXT +// frame's root blit check fails and we descend to continue draining. +// Without this, after the scrollbox's dirty flag is cleared (line ~721), +// the next frame blits root and never reaches the scrollbox — drain stalls. +let scrollDrainNode: DOMElement | null = null + +export function resetScrollDrainNode(): void { + scrollDrainNode = null +} + +export function getScrollDrainNode(): DOMElement | null { + return scrollDrainNode +} + +// At-bottom follow scroll event this frame. When streaming content +// triggers scrollTop = maxScroll, the ScrollBox records the delta + +// viewport bounds here. ink.tsx consumes it post-render to translate any active +// text selection by -delta so the highlight stays anchored to the TEXT +// (native terminal behavior — the selection walks up the screen as content +// scrolls, eventually clipping at the top). The frontFrame screen buffer +// still holds the old content at that point — captureScrolledRows reads +// from it before the front/back swap to preserve the text for copy. +export type FollowScroll = { + delta: number + viewportTop: number + viewportBottom: number +} +let followScroll: FollowScroll | null = null + +export function consumeFollowScroll(): FollowScroll | null { + const f = followScroll + followScroll = null + return f +} + +// ── Native terminal drain (iTerm2/Ghostty/etc. — proportional events) ── +// Minimum rows applied per frame. Above this, drain is proportional (~3/4 +// of remaining) so big bursts catch up in log₄ frames while the tail +// decelerates smoothly. Hard cap is innerHeight-1 so DECSTBM hint fires. +const SCROLL_MIN_PER_FRAME = 4 + +// ── xterm.js (VS Code) smooth drain ── +// Low pending (≤5) drains ALL in one frame — slow wheel clicks should be +// instant (click → visible jump → done), not micro-stutter 1-row frames. +// Higher pending drains at a small fixed step so fast-scroll animation +// stays smooth (no big jumps). Pending >MAX snaps excess. +const SCROLL_INSTANT_THRESHOLD = 5 // ≤ this: drain all at once +const SCROLL_HIGH_PENDING = 12 // threshold for HIGH step +const SCROLL_STEP_MED = 2 // pending (INSTANT, HIGH): catch-up +const SCROLL_STEP_HIGH = 3 // pending ≥ HIGH: fast flick +const SCROLL_MAX_PENDING = 30 // snap excess beyond this + +// xterm.js adaptive drain. Returns rows applied; mutates pendingScrollDelta. +function drainAdaptive( + node: DOMElement, + pending: number, + innerHeight: number, +): number { + const sign = pending > 0 ? 1 : -1 + let abs = Math.abs(pending) + let applied = 0 + // Snap excess beyond animation window so big flicks don't coast. + if (abs > SCROLL_MAX_PENDING) { + applied += sign * (abs - SCROLL_MAX_PENDING) + abs = SCROLL_MAX_PENDING + } + // ≤5: drain all (slow click = instant). Above: small fixed step. + const step = + abs <= SCROLL_INSTANT_THRESHOLD + ? abs + : abs < SCROLL_HIGH_PENDING + ? SCROLL_STEP_MED + : SCROLL_STEP_HIGH + applied += sign * step + const rem = abs - step + // Cap total at innerHeight-1 so DECSTBM blit+shift fast path fires + // (matches drainProportional). Excess stays in pendingScrollDelta. + const cap = Math.max(1, innerHeight - 1) + const totalAbs = Math.abs(applied) + if (totalAbs > cap) { + const excess = totalAbs - cap + node.pendingScrollDelta = sign * (rem + excess) + return sign * cap + } + node.pendingScrollDelta = rem > 0 ? sign * rem : undefined + return applied +} + +// Native proportional drain. step = max(MIN, floor(abs*3/4)), capped at +// innerHeight-1 so DECSTBM + blit+shift fast path fire. +function drainProportional( + node: DOMElement, + pending: number, + innerHeight: number, +): number { + const abs = Math.abs(pending) + const cap = Math.max(1, innerHeight - 1) + const step = Math.min(cap, Math.max(SCROLL_MIN_PER_FRAME, (abs * 3) >> 2)) + if (abs <= step) { + node.pendingScrollDelta = undefined + return pending + } + const applied = pending > 0 ? step : -step + node.pendingScrollDelta = pending - applied + return applied +} + +// OSC 8 hyperlink escape sequences. Empty params (;;) — ansi-tokenize only +// recognizes this exact prefix. The id= param (for grouping wrapped lines) +// is added at terminal-output time in termio/osc.ts link(). +const OSC = '\u001B]' +const BEL = '\u0007' + +function wrapWithOsc8Link(text: string, url: string): string { + return `${OSC}8;;${url}${BEL}${text}${OSC}8;;${BEL}` +} + +/** + * Build a mapping from each character position in the plain text to its segment index. + * Returns an array where charToSegment[i] is the segment index for character i. + */ +function buildCharToSegmentMap(segments: StyledSegment[]): number[] { + const map: number[] = [] + for (let i = 0; i < segments.length; i++) { + const len = segments[i]!.text.length + for (let j = 0; j < len; j++) { + map.push(i) + } + } + return map +} + +/** + * Apply styles to wrapped text by mapping each character back to its original segment. + * This preserves per-segment styles even when text wraps across lines. + * + * @param trimEnabled - Whether whitespace trimming is enabled (wrap-trim mode). + * When true, we skip whitespace in the original that was trimmed from the output. + * When false (wrap mode), all whitespace is preserved so no skipping is needed. + */ +function applyStylesToWrappedText( + wrappedPlain: string, + segments: StyledSegment[], + charToSegment: number[], + originalPlain: string, + trimEnabled: boolean = false, +): string { + const lines = wrappedPlain.split('\n') + const resultLines: string[] = [] + + let charIndex = 0 + for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) { + const line = lines[lineIdx]! + + // In trim mode, skip leading whitespace that was trimmed from this line. + // Only skip if the original has whitespace but the output line doesn't start + // with whitespace (meaning it was trimmed). If both have whitespace, the + // whitespace was preserved and we shouldn't skip. + if (trimEnabled && line.length > 0) { + const lineStartsWithWhitespace = /\s/.test(line[0]!) + const originalHasWhitespace = + charIndex < originalPlain.length && /\s/.test(originalPlain[charIndex]!) + + // Only skip if original has whitespace but line doesn't + if (originalHasWhitespace && !lineStartsWithWhitespace) { + while ( + charIndex < originalPlain.length && + /\s/.test(originalPlain[charIndex]!) + ) { + charIndex++ + } + } + } + + let styledLine = '' + let runStart = 0 + let runSegmentIndex = charToSegment[charIndex] ?? 0 + + for (let i = 0; i < line.length; i++) { + const currentSegmentIndex = charToSegment[charIndex] ?? runSegmentIndex + + if (currentSegmentIndex !== runSegmentIndex) { + // Flush the current run + const runText = line.slice(runStart, i) + const segment = segments[runSegmentIndex] + if (segment) { + let styled = applyTextStyles(runText, segment.styles) + if (segment.hyperlink) { + styled = wrapWithOsc8Link(styled, segment.hyperlink) + } + styledLine += styled + } else { + styledLine += runText + } + runStart = i + runSegmentIndex = currentSegmentIndex + } + + charIndex++ + } + + // Flush the final run + const runText = line.slice(runStart) + const segment = segments[runSegmentIndex] + if (segment) { + let styled = applyTextStyles(runText, segment.styles) + if (segment.hyperlink) { + styled = wrapWithOsc8Link(styled, segment.hyperlink) + } + styledLine += styled + } else { + styledLine += runText + } + + resultLines.push(styledLine) + + // Skip newline character in original that corresponds to this line break. + // This is needed when the original text contains actual newlines (not just + // wrapping-inserted newlines). Without this, charIndex gets out of sync + // because the newline is in originalPlain/charToSegment but not in the + // split lines. + if (charIndex < originalPlain.length && originalPlain[charIndex] === '\n') { + charIndex++ + } + + // In trim mode, skip whitespace that was replaced by newline when wrapping. + // We skip whitespace in the original until we reach a character that matches + // the first character of the next line. This handles cases like: + // - "AB \tD" wrapped to "AB\n\tD" - skip spaces until we hit the tab + // In non-trim mode, whitespace is preserved so no skipping is needed. + if (trimEnabled && lineIdx < lines.length - 1) { + const nextLine = lines[lineIdx + 1]! + const nextLineFirstChar = nextLine.length > 0 ? nextLine[0] : null + + // Skip whitespace until we hit a char that matches the next line's first char + while ( + charIndex < originalPlain.length && + /\s/.test(originalPlain[charIndex]!) + ) { + // Stop if we found the character that starts the next line + if ( + nextLineFirstChar !== null && + originalPlain[charIndex] === nextLineFirstChar + ) { + break + } + charIndex++ + } + } + } + + return resultLines.join('\n') +} + +/** + * Wrap text and record which output lines are soft-wrap continuations + * (i.e. the `\n` before them was inserted by word-wrap, not in the + * source). wrapAnsi already processes each input line independently, so + * wrapping per-input-line here gives identical output to a single + * whole-string wrap while letting us mark per-piece provenance. + * Truncate modes never add newlines (cli-truncate is whole-string) so + * they fall through with softWrap undefined — no tracking, no behavior + * change from the pre-softWrap path. + */ +function wrapWithSoftWrap( + plainText: string, + maxWidth: number, + textWrap: Parameters[2], +): { wrapped: string; softWrap: boolean[] | undefined } { + if (textWrap !== 'wrap' && textWrap !== 'wrap-trim') { + return { + wrapped: wrapText(plainText, maxWidth, textWrap), + softWrap: undefined, + } + } + const origLines = plainText.split('\n') + const outLines: string[] = [] + const softWrap: boolean[] = [] + for (const orig of origLines) { + const pieces = wrapText(orig, maxWidth, textWrap).split('\n') + for (let i = 0; i < pieces.length; i++) { + outLines.push(pieces[i]!) + softWrap.push(i > 0) + } + } + return { wrapped: outLines.join('\n'), softWrap } +} + +// If parent container is ``, text nodes will be treated as separate nodes in +// the tree and will have their own coordinates in the layout. +// To ensure text nodes are aligned correctly, take X and Y of the first text node +// and use it as offset for the rest of the nodes +// Only first node is taken into account, because other text nodes can't have margin or padding, +// so their coordinates will be relative to the first node anyway +function applyPaddingToText( + node: DOMElement, + text: string, + softWrap?: boolean[], +): string { + const yogaNode = node.childNodes[0]?.yogaNode + + if (yogaNode) { + const offsetX = yogaNode.getComputedLeft() + const offsetY = yogaNode.getComputedTop() + text = '\n'.repeat(offsetY) + indentString(text, offsetX) + if (softWrap && offsetY > 0) { + // Prepend `false` for each padding line so indices stay aligned + // with text.split('\n'). Mutate in place — caller owns the array. + softWrap.unshift(...Array(offsetY).fill(false)) + } + } + + return text +} + +// After nodes are laid out, render each to output object, which later gets rendered to terminal +function renderNodeToOutput( + node: DOMElement, + output: Output, + { + offsetX = 0, + offsetY = 0, + prevScreen, + skipSelfBlit = false, + inheritedBackgroundColor, + }: { + offsetX?: number + offsetY?: number + prevScreen: Screen | undefined + // Force this node to descend instead of blitting its own rect, while + // still passing prevScreen to children. Used for non-opaque absolute + // overlays over a dirty clipped region: the overlay's full rect has + // transparent gaps (stale underlying content in prevScreen), but its + // opaque descendants' narrower rects are safe to blit. + skipSelfBlit?: boolean + inheritedBackgroundColor?: Color + }, +): void { + const { yogaNode } = node + + if (yogaNode) { + if (yogaNode.getDisplay() === LayoutDisplay.None) { + // Clear old position if node was visible before becoming hidden + if (node.dirty) { + const cached = nodeCache.get(node) + if (cached) { + output.clear({ + x: Math.floor(cached.x), + y: Math.floor(cached.y), + width: Math.floor(cached.width), + height: Math.floor(cached.height), + }) + // Drop descendants' cache too — hideInstance's markDirty walks UP + // only, so descendants' .dirty stays false. Their nodeCache entries + // survive with pre-hide rects. On unhide, if position didn't shift, + // the blit check at line ~432 passes and copies EMPTY cells from + // prevScreen (cleared here) → content vanishes. + dropSubtreeCache(node) + layoutShifted = true + } + } + return + } + + // Left and top positions in Yoga are relative to their parent node + const x = offsetX + yogaNode.getComputedLeft() + const yogaTop = yogaNode.getComputedTop() + let y = offsetY + yogaTop + const width = yogaNode.getComputedWidth() + const height = yogaNode.getComputedHeight() + + // Absolute-positioned overlays (e.g. autocomplete menus with bottom='100%') + // can compute negative screen y when they extend above the viewport. Without + // clamping, setCellAt drops cells at y<0, clipping the TOP of the content + // (best matches in an autocomplete). By clamping to 0, we shift the element + // down so the top rows are visible and the bottom overflows below — the + // opaque prop ensures it paints over whatever is underneath. + if (y < 0 && node.style.position === 'absolute') { + y = 0 + } + + // Check if we can skip this subtree (clean node with unchanged layout). + // Blit cells from previous screen instead of re-rendering. + const cached = nodeCache.get(node) + if ( + !node.dirty && + !skipSelfBlit && + node.pendingScrollDelta === undefined && + cached && + cached.x === x && + cached.y === y && + cached.width === width && + cached.height === height && + prevScreen + ) { + const fx = Math.floor(x) + const fy = Math.floor(y) + const fw = Math.floor(width) + const fh = Math.floor(height) + output.blit(prevScreen, fx, fy, fw, fh) + if (node.style.position === 'absolute') { + absoluteRectsCur.push(cached) + } + // Absolute descendants can paint outside this node's layout bounds + // (e.g. a slash menu with position='absolute' bottom='100%' floats + // above). If a dirty clipped sibling re-rendered and overwrote those + // cells, the blit above only restored this node's own rect — the + // absolute descendants' cells are lost. Re-blit them from prevScreen + // so the overlays survive. + blitEscapingAbsoluteDescendants(node, output, prevScreen, fx, fy, fw, fh) + return + } + + // Clear stale content from the old position when re-rendering. + // Dirty: content changed. Moved: position/size changed (e.g., sibling + // above changed height), old cells still on the terminal. + const positionChanged = + cached !== undefined && + (cached.x !== x || + cached.y !== y || + cached.width !== width || + cached.height !== height) + if (positionChanged) { + layoutShifted = true + } + if (cached && (node.dirty || positionChanged)) { + output.clear( + { + x: Math.floor(cached.x), + y: Math.floor(cached.y), + width: Math.floor(cached.width), + height: Math.floor(cached.height), + }, + node.style.position === 'absolute', + ) + } + + // Read before deleting — hasRemovedChild disables prevScreen blitting + // for siblings to prevent stale overflow content from being restored. + const clears = pendingClears.get(node) + const hasRemovedChild = clears !== undefined + if (hasRemovedChild) { + layoutShifted = true + for (const rect of clears) { + output.clear({ + x: Math.floor(rect.x), + y: Math.floor(rect.y), + width: Math.floor(rect.width), + height: Math.floor(rect.height), + }) + } + pendingClears.delete(node) + } + + // Yoga squeezed this node to zero height (overflow in a height-constrained + // parent) AND a sibling lands at the same y. Skip rendering — both would + // write to the same row; if the sibling's content is shorter, this node's + // tail chars ghost (e.g. "false" + "true" = "truee"). The clear above + // already handled the visible→squeezed transition. + // + // The sibling-overlap check is load-bearing: Yoga's pixel-grid rounding + // can give a box h=0 while still leaving a row for it (next sibling at + // y+1, not y). HelpV2's third shortcuts column hits this — skipping + // unconditionally drops "ctrl + z to suspend" from /help output. + if (height === 0 && siblingSharesY(node, yogaNode)) { + nodeCache.set(node, { x, y, width, height, top: yogaTop }) + node.dirty = false + return + } + + if (node.nodeName === 'ink-raw-ansi') { + // Pre-rendered ANSI content. The producer already wrapped to width and + // emitted terminal-ready escape codes. Skip squash, measure, wrap, and + // style re-application — output.write() parses ANSI directly into cells. + const text = node.attributes['rawText'] as string + if (text) { + output.write(x, y, text) + } + } else if (node.nodeName === 'ink-text') { + const segments = squashTextNodesToSegments( + node, + inheritedBackgroundColor + ? { backgroundColor: inheritedBackgroundColor } + : undefined, + ) + + // First, get plain text to check if wrapping is needed + const plainText = segments.map(s => s.text).join('') + + if (plainText.length > 0) { + // Upstream Ink uses getMaxWidth(yogaNode) unclamped here. That + // width comes from Yoga's AtMost pass and can exceed the actual + // screen space (see getMaxWidth docstring). Yoga's height for this + // node already reflects the constrained Exactly pass, so clamping + // the wrap width here keeps line count consistent with layout. + // Without this, characters past the screen edge are dropped by + // setCellAt's bounds check. + const maxWidth = Math.min(getMaxWidth(yogaNode), output.width - x) + const textWrap = node.style.textWrap ?? 'wrap' + + // Check if wrapping is needed + const needsWrapping = widestLine(plainText) > maxWidth + + let text: string + let softWrap: boolean[] | undefined + if (needsWrapping && segments.length === 1) { + // Single segment: wrap plain text first, then apply styles to each line + const segment = segments[0]! + const w = wrapWithSoftWrap(plainText, maxWidth, textWrap) + softWrap = w.softWrap + text = w.wrapped + .split('\n') + .map(line => { + let styled = applyTextStyles(line, segment.styles) + // Apply OSC 8 hyperlink per-line so each line is independently + // clickable. output.ts splits on newlines and tokenizes each + // line separately, so a single wrapper around the whole block + // would only apply the hyperlink to the first line. + if (segment.hyperlink) { + styled = wrapWithOsc8Link(styled, segment.hyperlink) + } + return styled + }) + .join('\n') + } else if (needsWrapping) { + // Multiple segments with wrapping: wrap plain text first, then re-apply + // each segment's styles based on character positions. This preserves + // per-segment styles even when text wraps across lines. + const w = wrapWithSoftWrap(plainText, maxWidth, textWrap) + softWrap = w.softWrap + const charToSegment = buildCharToSegmentMap(segments) + text = applyStylesToWrappedText( + w.wrapped, + segments, + charToSegment, + plainText, + textWrap === 'wrap-trim', + ) + // Hyperlinks are handled per-run in applyStylesToWrappedText via + // wrapWithOsc8Link, similar to how styles are applied per-run. + } else { + // No wrapping needed: apply styles directly + text = segments + .map(segment => { + let styledText = applyTextStyles(segment.text, segment.styles) + if (segment.hyperlink) { + styledText = wrapWithOsc8Link(styledText, segment.hyperlink) + } + return styledText + }) + .join('') + } + + text = applyPaddingToText(node, text, softWrap) + + output.write(x, y, text, softWrap) + } + } else if (node.nodeName === 'ink-box') { + const boxBackgroundColor = + node.style.backgroundColor ?? inheritedBackgroundColor + + // Mark this box's region as non-selectable (fullscreen text + // selection). noSelect ops are applied AFTER blits/writes in + // output.get(), so this wins regardless of what's rendered into + // the region — including blits from prevScreen when the box is + // clean (the op is emitted on both the dirty-render path here + // AND on the blit fast-path at line ~235 since blitRegion copies + // the noSelect bitmap alongside cells). + // + // 'from-left-edge' extends the exclusion from col 0 so any + // upstream indentation (tool prefix, tree lines) is covered too + // — a multi-row drag over a diff gutter shouldn't pick up the + // ` ⎿ ` prefix on row 0 or the blank cells under it on row 1+. + if (node.style.noSelect) { + const boxX = Math.floor(x) + const fromEdge = node.style.noSelect === 'from-left-edge' + output.noSelect({ + x: fromEdge ? 0 : boxX, + y: Math.floor(y), + width: fromEdge ? boxX + Math.floor(width) : Math.floor(width), + height: Math.floor(height), + }) + } + + const overflowX = node.style.overflowX ?? node.style.overflow + const overflowY = node.style.overflowY ?? node.style.overflow + const clipHorizontally = overflowX === 'hidden' || overflowX === 'scroll' + const clipVertically = overflowY === 'hidden' || overflowY === 'scroll' + const isScrollY = overflowY === 'scroll' + + const needsClip = clipHorizontally || clipVertically + let y1: number | undefined + let y2: number | undefined + if (needsClip) { + const x1 = clipHorizontally + ? x + yogaNode.getComputedBorder(LayoutEdge.Left) + : undefined + + const x2 = clipHorizontally + ? x + + yogaNode.getComputedWidth() - + yogaNode.getComputedBorder(LayoutEdge.Right) + : undefined + + y1 = clipVertically + ? y + yogaNode.getComputedBorder(LayoutEdge.Top) + : undefined + + y2 = clipVertically + ? y + + yogaNode.getComputedHeight() - + yogaNode.getComputedBorder(LayoutEdge.Bottom) + : undefined + + output.clip({ x1, x2, y1, y2 }) + } + + if (isScrollY) { + // Scroll containers follow the ScrollBox component structure: + // a single content-wrapper child with flexShrink:0 (doesn't shrink + // to fit), whose children are the scrollable items. scrollHeight + // comes from the wrapper's intrinsic Yoga height. The wrapper is + // rendered with its Y translated by -scrollTop; its children are + // culled against the visible window. + const padTop = yogaNode.getComputedPadding(LayoutEdge.Top) + const innerHeight = Math.max( + 0, + (y2 ?? y + height) - + (y1 ?? y) - + padTop - + yogaNode.getComputedPadding(LayoutEdge.Bottom), + ) + + const content = node.childNodes.find(c => (c as DOMElement).yogaNode) as + | DOMElement + | undefined + const contentYoga = content?.yogaNode + // scrollHeight is the intrinsic height of the content wrapper. + // Do NOT add getComputedTop() — that's the wrapper's offset + // within the viewport (equal to the scroll container's + // paddingTop), and innerHeight already subtracts padding, so + // including it double-counts padding and inflates maxScroll. + const scrollHeight = contentYoga?.getComputedHeight() ?? 0 + // Capture previous scroll bounds BEFORE overwriting — the at-bottom + // follow check compares against last frame's max. + const prevScrollHeight = node.scrollHeight ?? scrollHeight + const prevInnerHeight = node.scrollViewportHeight ?? innerHeight + node.scrollHeight = scrollHeight + node.scrollViewportHeight = innerHeight + // Absolute screen-buffer row where the scrollable area (inside + // padding) begins. Exposed via ScrollBoxHandle.getViewportTop() so + // drag-to-scroll can detect when the drag leaves the scroll viewport. + node.scrollViewportTop = (y1 ?? y) + padTop + + const maxScroll = Math.max(0, scrollHeight - innerHeight) + // scrollAnchor: scroll so the anchored element's top is at the + // viewport top (plus offset). Yoga is FRESH — same calculateLayout + // pass that just produced scrollHeight. Deterministic alternative + // to scrollTo(N) which bakes a number that's stale by the throttled + // render; the element ref defers the read to now. One-shot snap. + // A prior eased-seek version (proportional drain over ~5 frames) + // moved scrollTop without firing React's notify → parent's quantized + // store snapshot never updated → StickyTracker got stale range props + // → firstVisible wrong. Also: SCROLL_MIN_PER_FRAME=4 with snap-at-1 + // ping-ponged forever at delta=2. Smooth needs drain-end notify + // plumbing; shipping instant first. stickyScroll overrides. + if (node.scrollAnchor) { + const anchorTop = node.scrollAnchor.el.yogaNode?.getComputedTop() + if (anchorTop != null) { + node.scrollTop = anchorTop + node.scrollAnchor.offset + node.pendingScrollDelta = undefined + } + node.scrollAnchor = undefined + } + // At-bottom follow. Positional: if scrollTop was at (or past) the + // previous max, pin to the new max. Scroll away → stop following; + // scroll back (or scrollToBottom/sticky attr) → resume. The sticky + // flag is OR'd in for cold start (scrollTop=0 before first layout) + // and scrollToBottom-from-far-away (flag set before scrollTop moves) + // — the imperative field takes precedence over the attribute so + // scrollTo/scrollBy can break stickiness. pendingDelta<0 guard: + // don't cancel an in-flight scroll-up when content races in. + // Capture scrollTop before follow so ink.tsx can translate any + // active text selection by the same delta (native terminal behavior: + // view keeps scrolling, highlight walks up with the text). + const scrollTopBeforeFollow = node.scrollTop ?? 0 + const sticky = + node.stickyScroll ?? Boolean(node.attributes['stickyScroll']) + const prevMaxScroll = Math.max(0, prevScrollHeight - prevInnerHeight) + // Positional check only valid when content grew — virtualization can + // transiently SHRINK scrollHeight (tail unmount + stale heightCache + // spacer) making scrollTop >= prevMaxScroll true by artifact, not + // because the user was at bottom. + const grew = scrollHeight >= prevScrollHeight + const atBottom = + sticky || (grew && scrollTopBeforeFollow >= prevMaxScroll) + if (atBottom && (node.pendingScrollDelta ?? 0) >= 0) { + node.scrollTop = maxScroll + node.pendingScrollDelta = undefined + // Sync flag so useVirtualScroll's isSticky() agrees with positional + // state — sticky-broken-but-at-bottom (wheel tremor, click-select + // at max) otherwise leaves useVirtualScroll's clamp holding the + // viewport short of new streaming content. scrollTo/scrollBy set + // false; this restores true, same as scrollToBottom() would. + // Only restore when (a) positionally at bottom and (b) the flag + // was explicitly broken (===false) by scrollTo/scrollBy. When + // undefined (never set by user action) leave it alone — setting it + // would make the sticky flag sticky-by-default and lock out + // direct scrollTop writes (e.g. the alt-screen-perf test). + if ( + node.stickyScroll === false && + scrollTopBeforeFollow >= prevMaxScroll + ) { + node.stickyScroll = true + } + } + const followDelta = (node.scrollTop ?? 0) - scrollTopBeforeFollow + if (followDelta > 0) { + const vpTop = node.scrollViewportTop ?? 0 + followScroll = { + delta: followDelta, + viewportTop: vpTop, + viewportBottom: vpTop + innerHeight - 1, + } + } + // Drain pendingScrollDelta. Native terminals (proportional burst + // events) use proportional drain; xterm.js (VS Code, sparse events + + // app-side accel curve) uses adaptive small-step drain. isXtermJs() + // depends on the async XTVERSION probe, but by the time this runs + // (pendingScrollDelta is only set by wheel events, >>50ms after + // startup) the probe has resolved — same timing guarantee the + // wheel-accel curve relies on. + let cur = node.scrollTop ?? 0 + const pending = node.pendingScrollDelta + const cMin = node.scrollClampMin + const cMax = node.scrollClampMax + const haveClamp = cMin !== undefined && cMax !== undefined + if (pending !== undefined && pending !== 0) { + // Drain continues even past the clamp — the render-clamp below + // holds the VISUAL at the mounted edge regardless. Hard-stopping + // here caused stop-start jutter: drain hits edge → pause → React + // commits → clamp widens → drain resumes → edge again. Letting + // scrollTop advance smoothly while the clamp lags gives continuous + // visual scroll at React's commit rate (the clamp catches up each + // commit). But THROTTLE the drain when already past the clamp so + // scrollTop doesn't race 5000 rows ahead of the mounted range + // (slide-cap would then take 200 commits to catch up = long + // perceived stall at the edge). Past-clamp drain caps at ~4 rows/ + // frame, roughly matching React's slide rate so the gap stays + // bounded and catch-up is quick once input stops. + const pastClamp = + haveClamp && + ((pending < 0 && cur < cMin) || (pending > 0 && cur > cMax)) + const eff = pastClamp ? Math.min(4, innerHeight >> 3) : innerHeight + cur += isXtermJsHost() + ? drainAdaptive(node, pending, eff) + : drainProportional(node, pending, eff) + } else if (pending === 0) { + // Opposite scrollBy calls cancelled to zero — clear so we don't + // schedule an infinite loop of no-op drain frames. + node.pendingScrollDelta = undefined + } + let scrollTop = Math.max(0, Math.min(cur, maxScroll)) + // Virtual-scroll clamp: if scrollTop raced past the currently-mounted + // range (burst PageUp before React re-renders), render at the EDGE of + // the mounted children instead of blank spacer. Do NOT write back to + // node.scrollTop — the clamped value is for this paint only; the real + // scrollTop stays so React's next commit sees the target and mounts + // the right range. Not scheduling scrollDrainNode here keeps the + // clamp passive — React's commit → resetAfterCommit → onRender will + // paint again with fresh bounds. + const clamped = haveClamp + ? Math.max(cMin, Math.min(scrollTop, cMax)) + : scrollTop + node.scrollTop = scrollTop + // Clamp hitting top/bottom consumes any remainder. Set drainPending + // only after clamp so a wasted no-op frame isn't scheduled. + if (scrollTop !== cur) node.pendingScrollDelta = undefined + if (node.pendingScrollDelta !== undefined) scrollDrainNode = node + scrollTop = clamped + + if (content && contentYoga) { + // Compute content wrapper's absolute render position with scroll + // offset applied, then render its children with culling. + const contentX = x + contentYoga.getComputedLeft() + const contentY = y + contentYoga.getComputedTop() - scrollTop + // layoutShifted detection gap: when scrollTop moves by >= viewport + // height (batched PageUps, fast wheel), every visible child gets + // culled (cache dropped) and every newly-visible child has no + // cache — so the children's positionChanged check can't fire. + // The content wrapper's cached y (which encodes -scrollTop) is + // the only node that survives to witness the scroll. + const contentCached = nodeCache.get(content) + let hint: ScrollHint | null = null + if (contentCached && contentCached.y !== contentY) { + // delta = newScrollTop - oldScrollTop (positive = scrolled down). + // Capture a DECSTBM hint if the container itself didn't move + // and the shift fits within the viewport — otherwise the full + // rewrite is needed anyway, and layoutShifted stays the fallback. + const delta = contentCached.y - contentY + const regionTop = Math.floor(y + contentYoga.getComputedTop()) + const regionBottom = regionTop + innerHeight - 1 + if ( + cached?.y === y && + cached.height === height && + innerHeight > 0 && + Math.abs(delta) < innerHeight + ) { + hint = { top: regionTop, bottom: regionBottom, delta } + scrollHint = hint + } else { + layoutShifted = true + } + } + // Fast path: scroll (hint captured) with usable prevScreen. + // Blit prevScreen's scroll region into next.screen, shift in-place + // by delta (mirrors DECSTBM), then render ONLY the edge rows. The + // nested clip keeps child writes out of stable rows — a tall child + // that spans edge+stable still renders but stable cells are + // clipped, preserving the blit. Avoids re-rendering every visible + // child (expensive for long syntax-highlighted transcripts). + // + // When content.dirty (e.g. streaming text at the bottom of the + // scroll), we still use the fast path — the dirty child is almost + // always in the edge rows (the bottom, where new content appears). + // After edge rendering, any dirty children in stable rows are + // re-rendered in a second pass to avoid showing stale blitted + // content. + // + // Guard: the fast path only handles pure scroll or bottom-append. + // Child removal/insertion changes the content height in a way that + // doesn't match the scroll delta — fall back to the full path so + // removed children don't leave stale cells and shifted siblings + // render at their new positions. + const scrollHeight = contentYoga.getComputedHeight() + const prevHeight = contentCached?.height ?? scrollHeight + const heightDelta = scrollHeight - prevHeight + const safeForFastPath = + !hint || + heightDelta === 0 || + (hint.delta > 0 && heightDelta === hint.delta) + // scrollHint is set above when hint is captured. If safeForFastPath + // is false the full path renders a next.screen that doesn't match + // the DECSTBM shift — emitting DECSTBM leaves stale rows (seen as + // content bleeding through during scroll-up + streaming). Clear it. + if (!safeForFastPath) scrollHint = null + if (hint && prevScreen && safeForFastPath) { + const { top, bottom, delta } = hint + const w = Math.floor(width) + output.blit(prevScreen, Math.floor(x), top, w, bottom - top + 1) + output.shift(top, bottom, delta) + // Edge rows: new content entering the viewport. + const edgeTop = delta > 0 ? bottom - delta + 1 : top + const edgeBottom = delta > 0 ? bottom : top - delta - 1 + output.clear({ + x: Math.floor(x), + y: edgeTop, + width: w, + height: edgeBottom - edgeTop + 1, + }) + output.clip({ + x1: undefined, + x2: undefined, + y1: edgeTop, + y2: edgeBottom + 1, + }) + // Snapshot dirty children before the first pass — the first + // pass clears dirty flags, and edge-spanning children would be + // missed by the second pass without this snapshot. + const dirtyChildren = content.dirty + ? new Set(content.childNodes.filter(c => (c as DOMElement).dirty)) + : null + renderScrolledChildren( + content, + output, + contentX, + contentY, + hasRemovedChild, + undefined, + // Cull to edge in child-local coords (inverse of contentY offset). + edgeTop - contentY, + edgeBottom + 1 - contentY, + boxBackgroundColor, + true, + ) + output.unclip() + + // Second pass: re-render children in stable rows whose screen + // position doesn't match where the shift put their old pixels. + // Covers TWO cases: + // 1. Dirty children — their content changed, blitted pixels are + // stale regardless of position. + // 2. Clean children BELOW a middle-growth point — when a dirty + // sibling above them grows, their yogaTop increases but + // scrollTop increases by the same amount (sticky), so their + // screenY is CONSTANT. The shift moved their old pixels to + // screenY-delta (wrong); they should stay at screenY. Without + // this, the spinner/tmux-monitor ghost at shifted positions + // during streaming (e.g. triple spinner, pill duplication). + // For bottom-append (the common case), all clean children are + // ABOVE the growth point; their screenY decreased by delta and + // the shift put them at the right place — skipped here, fast + // path preserved. + if (dirtyChildren) { + const edgeTopLocal = edgeTop - contentY + const edgeBottomLocal = edgeBottom + 1 - contentY + const spaces = ' '.repeat(w) + // Track cumulative height change of children iterated so far. + // A clean child's yogaTop is unchanged iff this is zero (no + // sibling above it grew/shrank/mounted). When zero, the skip + // check cached.y−delta === screenY reduces to delta === delta + // (tautology) → skip without yoga reads. Restores O(dirty) + // that #24536 traded away: for bottom-append the dirty child + // is last (all clean children skip); for virtual-scroll range + // shift the topSpacer shrink + new-item heights self-balance + // to zero before reaching the clean block. Middle-growth + // leaves shift non-zero → clean children after the growth + // point fall through to yoga + the fine-grained check below, + // preserving the ghost-box fix. + let cumHeightShift = 0 + for (const childNode of content.childNodes) { + const childElem = childNode as DOMElement + const isDirty = dirtyChildren.has(childNode) + if (!isDirty && cumHeightShift === 0) { + if (nodeCache.has(childElem)) continue + // Uncached = culled last frame, now re-entering. blit + // never painted it → fall through to yoga + render. + // Height unchanged (clean), so cumHeightShift stays 0. + } + const cy = childElem.yogaNode + if (!cy) continue + const childTop = cy.getComputedTop() + const childH = cy.getComputedHeight() + const childBottom = childTop + childH + if (isDirty) { + const prev = nodeCache.get(childElem) + cumHeightShift += childH - (prev ? prev.height : 0) + } + // Skip culled children (outside viewport) + if ( + childBottom <= scrollTop || + childTop >= scrollTop + innerHeight + ) + continue + // Skip children entirely within edge rows (already rendered) + if (childTop >= edgeTopLocal && childBottom <= edgeBottomLocal) + continue + const screenY = Math.floor(contentY + childTop) + // Clean children reaching here have cumHeightShift ≠ 0 OR + // no cache. Re-check precisely: cached.y − delta is where + // the shift left old pixels; if it equals new screenY the + // blit is correct (shift re-balanced at this child, or + // yogaTop happens to net out). No cache → blit never + // painted it → render. + if (!isDirty) { + const childCached = nodeCache.get(childElem) + if ( + childCached && + Math.floor(childCached.y) - delta === screenY + ) { + continue + } + } + // Wipe this child's region with spaces to overwrite stale + // blitted content — output.clear() only expands damage and + // cannot zero cells that the blit already wrote. + const screenBottom = Math.min( + Math.floor(contentY + childBottom), + Math.floor((y1 ?? y) + padTop + innerHeight), + ) + if (screenY < screenBottom) { + const fill = Array(screenBottom - screenY) + .fill(spaces) + .join('\n') + output.write(Math.floor(x), screenY, fill) + output.clip({ + x1: undefined, + x2: undefined, + y1: screenY, + y2: screenBottom, + }) + renderNodeToOutput(childElem, output, { + offsetX: contentX, + offsetY: contentY, + prevScreen: undefined, + inheritedBackgroundColor: boxBackgroundColor, + }) + output.unclip() + } + } + } + + // Third pass: repair rows where shifted copies of absolute + // overlays landed. The blit copied prevScreen cells INCLUDING + // overlay pixels (overlays render AFTER this ScrollBox so they + // painted into prevScreen's scroll region). After shift, those + // pixels sit at (rect.y - delta) — neither edge render nor the + // overlay's own re-render covers them. Wipe and re-render + // ScrollBox content so the diff writes correct cells. + const spaces = absoluteRectsPrev.length ? ' '.repeat(w) : '' + for (const r of absoluteRectsPrev) { + if (r.y >= bottom + 1 || r.y + r.height <= top) continue + const shiftedTop = Math.max(top, Math.floor(r.y) - delta) + const shiftedBottom = Math.min( + bottom + 1, + Math.floor(r.y + r.height) - delta, + ) + // Skip if entirely within edge rows (already rendered). + if (shiftedTop >= edgeTop && shiftedBottom <= edgeBottom + 1) + continue + if (shiftedTop >= shiftedBottom) continue + const fill = Array(shiftedBottom - shiftedTop) + .fill(spaces) + .join('\n') + output.write(Math.floor(x), shiftedTop, fill) + output.clip({ + x1: undefined, + x2: undefined, + y1: shiftedTop, + y2: shiftedBottom, + }) + renderScrolledChildren( + content, + output, + contentX, + contentY, + hasRemovedChild, + undefined, + shiftedTop - contentY, + shiftedBottom - contentY, + boxBackgroundColor, + true, + ) + output.unclip() + } + } else { + // Full path. Two sub-cases: + // + // Scrolled without a usable hint (big jump, container moved): + // child positions in prevScreen are stale. Clear the viewport + // and disable blit so children don't restore shifted content. + // + // No scroll (spinner tick, content edit): child positions in + // prevScreen are still valid. Skip the viewport clear and pass + // prevScreen so unchanged children blit. Dirty children already + // self-clear via their own cached-rect clear. Without this, a + // spinner inside ScrollBox forces a full-content rewrite every + // frame — on wide terminals over tmux (no BSU/ESU) the + // bandwidth crosses the chunk boundary and the frame tears. + const scrolled = contentCached && contentCached.y !== contentY + if (scrolled && y1 !== undefined && y2 !== undefined) { + output.clear({ + x: Math.floor(x), + y: Math.floor(y1), + width: Math.floor(width), + height: Math.floor(y2 - y1), + }) + } + // positionChanged (ScrollBox height shrunk — pill mount) means a + // child spanning the old bottom edge would blit its full cached + // rect past the new clip. output.ts clips blits now, but also + // disable prevScreen here so the partial-row child re-renders at + // correct bounds instead of blitting a clipped (truncated) old + // rect. + renderScrolledChildren( + content, + output, + contentX, + contentY, + hasRemovedChild, + scrolled || positionChanged ? undefined : prevScreen, + scrollTop, + scrollTop + innerHeight, + boxBackgroundColor, + ) + } + nodeCache.set(content, { + x: contentX, + y: contentY, + width: contentYoga.getComputedWidth(), + height: contentYoga.getComputedHeight(), + }) + content.dirty = false + } + } else { + // Fill interior with background color before rendering children. + // This covers padding areas and empty space; child text inherits + // the color via inheritedBackgroundColor so written cells also + // get the background. + // Disable prevScreen for children: the fill overwrites the entire + // interior each render, so child blits from prevScreen would restore + // stale cells (wrong bg if it changed) on top of the fresh fill. + const ownBackgroundColor = node.style.backgroundColor + if (ownBackgroundColor || node.style.opaque) { + const borderLeft = yogaNode.getComputedBorder(LayoutEdge.Left) + const borderRight = yogaNode.getComputedBorder(LayoutEdge.Right) + const borderTop = yogaNode.getComputedBorder(LayoutEdge.Top) + const borderBottom = yogaNode.getComputedBorder(LayoutEdge.Bottom) + const innerWidth = Math.floor(width) - borderLeft - borderRight + const innerHeight = Math.floor(height) - borderTop - borderBottom + if (innerWidth > 0 && innerHeight > 0) { + const spaces = ' '.repeat(innerWidth) + const fillLine = ownBackgroundColor + ? applyTextStyles(spaces, { backgroundColor: ownBackgroundColor }) + : spaces + const fill = Array(innerHeight).fill(fillLine).join('\n') + output.write(x + borderLeft, y + borderTop, fill) + } + } + + renderChildren( + node, + output, + x, + y, + hasRemovedChild, + // backgroundColor and opaque both disable child blit: the fill + // overwrites the entire interior each render, so any child whose + // layout position shifted would blit stale cells from prevScreen + // on top of the fresh fill. Previously opaque kept blit enabled + // on the assumption that plain-space fill + unchanged children = + // valid composite, but children CAN reposition (ScrollBox remeasure + // on re-render → /permissions body blanked on Down arrow, #25436). + ownBackgroundColor || node.style.opaque ? undefined : prevScreen, + boxBackgroundColor, + ) + } + + if (needsClip) { + output.unclip() + } + + // Render border AFTER children to ensure it's not overwritten by child + // clearing operations. When a child shrinks, it clears its old area, + // which may overlap with where the parent's border now is. + renderBorder(x, y, node, output) + } else if (node.nodeName === 'ink-root') { + renderChildren( + node, + output, + x, + y, + hasRemovedChild, + prevScreen, + inheritedBackgroundColor, + ) + } + + // Cache layout bounds for dirty tracking + const rect = { x, y, width, height, top: yogaTop } + nodeCache.set(node, rect) + if (node.style.position === 'absolute') { + absoluteRectsCur.push(rect) + } + node.dirty = false + } +} + +// Overflow contamination: content overflows right/down, so clean siblings +// AFTER a dirty/removed sibling can contain stale overflow in prevScreen. +// Disable blit for siblings after a dirty child — but still pass prevScreen +// TO the dirty child itself so its clean descendants can blit. The dirty +// child's own blit check already fails (node.dirty=true at line 216), so +// passing prevScreen only benefits its subtree. +// For removed children we don't know their original position, so +// conservatively disable blit for all. +// +// Clipped children (overflow hidden/scroll on both axes) cannot overflow +// onto later siblings — their content is confined to their layout bounds. +// Skip the contamination guard for them so later siblings can still blit. +// Without this, a spinner inside a ScrollBox dirties the wrapper on every +// tick and the bottom prompt section never blits → 100% writes every frame. +// +// Exception: absolute-positioned clipped children may have layout bounds +// that overlap arbitrary siblings, so the clipping does not help. +// +// Overlap contamination (seenDirtyClipped): a later ABSOLUTE sibling whose +// rect sits inside a dirty clipped child's bounds would blit stale cells +// from prevScreen — the clipped child just rewrote those cells this frame. +// The clipsBothAxes skip only protects against OVERFLOW (clipped child +// painting outside its bounds), not overlap (absolute sibling painting +// inside them). For non-opaque absolute siblings, skipSelfBlit forces +// descent (the full-width rect has transparent gaps → stale blit) while +// still passing prevScreen so opaque descendants can blit their narrower +// rects (NewMessagesPill's inner Text with backgroundColor). Opaque +// absolute siblings fill their entire rect — direct blit is safe. +function renderChildren( + node: DOMElement, + output: Output, + offsetX: number, + offsetY: number, + hasRemovedChild: boolean, + prevScreen: Screen | undefined, + inheritedBackgroundColor: Color | undefined, +): void { + let seenDirtyChild = false + let seenDirtyClipped = false + for (const childNode of node.childNodes) { + const childElem = childNode as DOMElement + // Capture dirty before rendering — renderNodeToOutput clears the flag + const wasDirty = childElem.dirty + const isAbsolute = childElem.style.position === 'absolute' + renderNodeToOutput(childElem, output, { + offsetX, + offsetY, + prevScreen: hasRemovedChild || seenDirtyChild ? undefined : prevScreen, + // Short-circuits on seenDirtyClipped (false in the common case) so + // the opaque/bg reads don't happen per-child per-frame. + skipSelfBlit: + seenDirtyClipped && + isAbsolute && + !childElem.style.opaque && + childElem.style.backgroundColor === undefined, + inheritedBackgroundColor, + }) + if (wasDirty && !seenDirtyChild) { + if (!clipsBothAxes(childElem) || isAbsolute) { + seenDirtyChild = true + } else { + seenDirtyClipped = true + } + } + } +} + +function clipsBothAxes(node: DOMElement): boolean { + const ox = node.style.overflowX ?? node.style.overflow + const oy = node.style.overflowY ?? node.style.overflow + return ( + (ox === 'hidden' || ox === 'scroll') && (oy === 'hidden' || oy === 'scroll') + ) +} + +// When Yoga squeezes a box to h=0, the ghost only happens if a sibling +// lands at the same computed top — then both write to that row and the +// shorter content leaves the longer's tail visible. Yoga's pixel-grid +// rounding can give h=0 while still advancing the next sibling's top +// (HelpV2's third shortcuts column), so h=0 alone isn't sufficient. +function siblingSharesY(node: DOMElement, yogaNode: LayoutNode): boolean { + const parent = node.parentNode + if (!parent) return false + const myTop = yogaNode.getComputedTop() + const siblings = parent.childNodes + const idx = siblings.indexOf(node) + for (let i = idx + 1; i < siblings.length; i++) { + const sib = (siblings[i] as DOMElement).yogaNode + if (!sib) continue + return sib.getComputedTop() === myTop + } + // No next sibling with a yoga node — check previous. A run of h=0 boxes + // at the tail would all share y with each other. + for (let i = idx - 1; i >= 0; i--) { + const sib = (siblings[i] as DOMElement).yogaNode + if (!sib) continue + return sib.getComputedTop() === myTop + } + return false +} + +// When a node blits, its absolute-positioned descendants that paint outside +// the node's layout bounds are NOT covered by the blit (which only copies +// the node's own rect). If a dirty sibling re-rendered and overwrote those +// cells, we must re-blit them from prevScreen so the overlays survive. +// Example: PromptInputFooter's slash menu uses position='absolute' bottom='100%' +// to float above the prompt; a spinner tick in the ScrollBox above re-renders +// and overwrites those cells. Without this, the menu vanishes on the next frame. +function blitEscapingAbsoluteDescendants( + node: DOMElement, + output: Output, + prevScreen: Screen, + px: number, + py: number, + pw: number, + ph: number, +): void { + const pr = px + pw + const pb = py + ph + for (const child of node.childNodes) { + if (child.nodeName === '#text') continue + const elem = child as DOMElement + if (elem.style.position === 'absolute') { + const cached = nodeCache.get(elem) + if (cached) { + absoluteRectsCur.push(cached) + const cx = Math.floor(cached.x) + const cy = Math.floor(cached.y) + const cw = Math.floor(cached.width) + const ch = Math.floor(cached.height) + // Only blit rects that extend outside the parent's layout bounds — + // cells within the parent rect are already covered by the parent blit. + if (cx < px || cy < py || cx + cw > pr || cy + ch > pb) { + output.blit(prevScreen, cx, cy, cw, ch) + } + } + } + // Recurse — absolute descendants can be nested arbitrarily deep + blitEscapingAbsoluteDescendants(elem, output, prevScreen, px, py, pw, ph) + } +} + +// Render children of a scroll container with viewport culling. +// scrollTopY..scrollBottomY are the visible window in CHILD-LOCAL Yoga coords +// (i.e. what getComputedTop() returns). Children entirely outside this window +// are skipped; their nodeCache entry is deleted so if they re-enter the +// viewport later they don't emit a stale clear for a position now occupied +// by a sibling. +function renderScrolledChildren( + node: DOMElement, + output: Output, + offsetX: number, + offsetY: number, + hasRemovedChild: boolean, + prevScreen: Screen | undefined, + scrollTopY: number, + scrollBottomY: number, + inheritedBackgroundColor: Color | undefined, + // When true (DECSTBM fast path), culled children keep their cache — + // the blit+shift put stable rows in next.screen so stale cache is + // never read. Avoids walking O(total_children * subtree_depth) per frame. + preserveCulledCache = false, +): void { + let seenDirtyChild = false + // Track cumulative height shift of dirty children iterated so far. When + // zero, a clean child's yogaTop is unchanged (no sibling above it grew), + // so cached.top is fresh and the cull check skips yoga. Bottom-append + // has the dirty child last → all prior clean children hit cache → + // O(dirty) not O(mounted). Middle-growth leaves shift non-zero after + // the dirty child → subsequent children yoga-read (needed for correct + // culling since their yogaTop shifted). + let cumHeightShift = 0 + for (const childNode of node.childNodes) { + const childElem = childNode as DOMElement + const cy = childElem.yogaNode + if (cy) { + const cached = nodeCache.get(childElem) + let top: number + let height: number + if ( + cached?.top !== undefined && + !childElem.dirty && + cumHeightShift === 0 + ) { + top = cached.top + height = cached.height + } else { + top = cy.getComputedTop() + height = cy.getComputedHeight() + if (childElem.dirty) { + cumHeightShift += height - (cached ? cached.height : 0) + } + // Refresh cached top so next frame's cumShift===0 path stays + // correct. For culled children with preserveCulledCache=true this + // is the ONLY refresh point — without it, a middle-growth frame + // leaves stale tops that misfire next frame. + if (cached) cached.top = top + } + const bottom = top + height + if (bottom <= scrollTopY || top >= scrollBottomY) { + // Culled — outside visible window. Drop stale cache entries from + // the subtree so when this child re-enters it doesn't fire clears + // at positions now occupied by siblings. The viewport-clear on + // scroll-change handles the visible-area repaint. + if (!preserveCulledCache) dropSubtreeCache(childElem) + continue + } + } + const wasDirty = childElem.dirty + renderNodeToOutput(childElem, output, { + offsetX, + offsetY, + prevScreen: hasRemovedChild || seenDirtyChild ? undefined : prevScreen, + inheritedBackgroundColor, + }) + if (wasDirty) { + seenDirtyChild = true + } + } +} + +function dropSubtreeCache(node: DOMElement): void { + nodeCache.delete(node) + for (const child of node.childNodes) { + if (child.nodeName !== '#text') { + dropSubtreeCache(child as DOMElement) + } + } +} + +// Exported for testing +export { buildCharToSegmentMap, applyStylesToWrappedText } + +export default renderNodeToOutput diff --git a/_all-in-one-upload/cc-haha/src/keybindings/shortcutFormat.ts b/_all-in-one-upload/cc-haha/src/keybindings/shortcutFormat.ts new file mode 100644 index 0000000..45db3b0 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/keybindings/shortcutFormat.ts @@ -0,0 +1,63 @@ +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../services/analytics/index.js' +import { loadKeybindingsSync } from './loadUserBindings.js' +import { getBindingDisplayText } from './resolver.js' +import type { KeybindingContextName } from './types.js' + +// TODO(keybindings-migration): Remove fallback parameter after migration is +// complete and we've confirmed no 'keybinding_fallback_used' events are being +// logged. The fallback exists as a safety net during migration - if bindings +// fail to load or an action isn't found, we fall back to hardcoded values. +// Once stable, callers should be able to trust that getBindingDisplayText +// always returns a value for known actions, and we can remove this defensive +// pattern. + +// Track which action+context pairs have already logged a fallback event +// to avoid duplicate events from repeated calls in non-React contexts. +const LOGGED_FALLBACKS = new Set() + +/** + * Get the display text for a configured shortcut without React hooks. + * Use this in non-React contexts (commands, services, etc.). + * + * This lives in its own module (not useShortcutDisplay.ts) so that + * non-React callers like query/stopHooks.ts don't pull React into their + * module graph via the sibling hook. + * + * @param action - The action name (e.g., 'app:toggleTranscript') + * @param context - The keybinding context (e.g., 'Global') + * @param fallback - Fallback text if binding not found + * @returns The configured shortcut display text + * + * @example + * const expandShortcut = getShortcutDisplay('app:toggleTranscript', 'Global', 'ctrl+o') + * // Returns the user's configured binding, or 'ctrl+o' as default + */ +export function getShortcutDisplay( + action: string, + context: KeybindingContextName, + fallback: string, +): string { + const bindings = loadKeybindingsSync() + const resolved = getBindingDisplayText(action, context, bindings) + if (resolved === undefined) { + const key = `${action}:${context}` + if (!LOGGED_FALLBACKS.has(key)) { + LOGGED_FALLBACKS.add(key) + logEvent('tengu_keybinding_fallback_used', { + action: + action as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + context: + context as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + fallback: + fallback as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + reason: + 'action_not_found' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }) + } + return fallback + } + return resolved +} diff --git a/_all-in-one-upload/cc-haha/src/memdir/memoryShapeTelemetry.ts b/_all-in-one-upload/cc-haha/src/memdir/memoryShapeTelemetry.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/memdir/memoryShapeTelemetry.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/migrations/resetProToOpusDefault.ts b/_all-in-one-upload/cc-haha/src/migrations/resetProToOpusDefault.ts new file mode 100644 index 0000000..601872f --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/migrations/resetProToOpusDefault.ts @@ -0,0 +1,51 @@ +import { logEvent } from 'src/services/analytics/index.js' +import { isProSubscriber } from '../utils/auth.js' +import { getGlobalConfig, saveGlobalConfig } from '../utils/config.js' +import { getAPIProvider } from '../utils/model/providers.js' +import { getSettings_DEPRECATED } from '../utils/settings/settings.js' + +export function resetProToOpusDefault(): void { + const config = getGlobalConfig() + + if (config.opusProMigrationComplete) { + return + } + + const apiProvider = getAPIProvider() + + // Pro users on firstParty get auto-migrated to Opus 4.5 default + if (apiProvider !== 'firstParty' || !isProSubscriber()) { + saveGlobalConfig(current => ({ + ...current, + opusProMigrationComplete: true, + })) + logEvent('tengu_reset_pro_to_opus_default', { skipped: true }) + return + } + + const settings = getSettings_DEPRECATED() + + // Only show notification if user was on default (no custom model setting) + if (settings?.model === undefined) { + const opusProMigrationTimestamp = Date.now() + saveGlobalConfig(current => ({ + ...current, + opusProMigrationComplete: true, + opusProMigrationTimestamp, + })) + logEvent('tengu_reset_pro_to_opus_default', { + skipped: false, + had_custom_model: false, + }) + } else { + // User has a custom model setting, just mark migration complete + saveGlobalConfig(current => ({ + ...current, + opusProMigrationComplete: true, + })) + logEvent('tengu_reset_pro_to_opus_default', { + skipped: false, + had_custom_model: true, + }) + } +} diff --git a/_all-in-one-upload/cc-haha/src/query/transitions.ts b/_all-in-one-upload/cc-haha/src/query/transitions.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/query/transitions.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/server/__tests__/.syncthing.providers-real.test.ts.tmp b/_all-in-one-upload/cc-haha/src/server/__tests__/.syncthing.providers-real.test.ts.tmp new file mode 100644 index 0000000..81c4911 Binary files /dev/null and b/_all-in-one-upload/cc-haha/src/server/__tests__/.syncthing.providers-real.test.ts.tmp differ diff --git a/_all-in-one-upload/cc-haha/src/server/__tests__/fixtures/mock-sdk-cli.ts b/_all-in-one-upload/cc-haha/src/server/__tests__/fixtures/mock-sdk-cli.ts new file mode 100644 index 0000000..605aaae --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/server/__tests__/fixtures/mock-sdk-cli.ts @@ -0,0 +1,274 @@ +const args = process.argv.slice(2) + +function getArg(name: string): string | undefined { + const index = args.indexOf(name) + return index >= 0 ? args[index + 1] : undefined +} + +function emit(ws: WebSocket, payload: Record) { + ws.send(JSON.stringify(payload) + '\n') +} + +function delay(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +function extractUserText(message: any): string { + const content = message?.message?.content + if (!Array.isArray(content)) return '' + return content + .filter((block: any) => block?.type === 'text' && typeof block.text === 'string') + .map((block: any) => block.text) + .join(' ') +} + +const sdkUrl = getArg('--sdk-url') +const sessionId = getArg('--session-id') || crypto.randomUUID() +const initMode = process.env.MOCK_SDK_INIT_MODE || 'on_open' +const streamDelayMs = Number(process.env.MOCK_SDK_STREAM_DELAY_MS || '0') +const exitAfterOpenMs = Number(process.env.MOCK_SDK_EXIT_AFTER_OPEN_MS || '0') +const exitAfterFirstUserMs = Number(process.env.MOCK_SDK_EXIT_AFTER_FIRST_USER_MS || '0') +let initSent = false +let firstUserExitScheduled = false + +if (!sdkUrl) { + console.error('Missing --sdk-url') + process.exit(1) +} + +const ws = new WebSocket(sdkUrl) + +function sendInit() { + if (initSent) return + initSent = true + emit(ws, { + type: 'system', + subtype: 'init', + model: 'mock-opus', + slash_commands: [{ name: 'help', description: 'Show help' }], + session_id: sessionId, + }) +} + +ws.addEventListener('open', () => { + if (initMode !== 'on_first_user') { + sendInit() + } + if (exitAfterOpenMs > 0) { + setTimeout(() => process.exit(1), exitAfterOpenMs) + } +}) + +ws.addEventListener('message', (event) => { + const payload = typeof event.data === 'string' ? event.data : String(event.data) + const lines = payload.split('\n').map(line => line.trim()).filter(Boolean) + + void (async () => { + for (const line of lines) { + const parsed = JSON.parse(line) + + if (parsed.type === 'user') { + sendInit() + if (exitAfterFirstUserMs > 0 && !firstUserExitScheduled) { + firstUserExitScheduled = true + setTimeout(() => process.exit(1), exitAfterFirstUserMs) + continue + } + const text = extractUserText(parsed) + const slashCommand = text.trim() + if (slashCommand === '/cost') { + emit(ws, { + type: 'system', + subtype: 'local_command_output', + content: 'Total cost: $0.0000\nTotal duration: 0s', + session_id: sessionId, + }) + emit(ws, { + type: 'result', + subtype: 'success', + is_error: false, + result: 'Total cost: $0.0000', + usage: { input_tokens: 0, output_tokens: 0 }, + session_id: sessionId, + }) + continue + } + if (slashCommand === '/context') { + emit(ws, { + type: 'system', + subtype: 'local_command_output', + content: '## Context Usage\n\n| Type | Tokens |\n| --- | ---: |\n| System prompt | 123 |', + session_id: sessionId, + }) + emit(ws, { + type: 'result', + subtype: 'success', + is_error: false, + result: 'Context usage', + usage: { input_tokens: 0, output_tokens: 0 }, + session_id: sessionId, + }) + continue + } + emit(ws, { + type: 'stream_event', + event: { type: 'message_start' }, + session_id: sessionId, + }) + emit(ws, { + type: 'stream_event', + event: { + type: 'content_block_start', + index: 0, + content_block: { type: 'text', text: '' }, + }, + session_id: sessionId, + }) + emit(ws, { + type: 'stream_event', + event: { + type: 'content_block_delta', + index: 0, + delta: { type: 'thinking_delta', thinking: 'Mock thinking...' }, + }, + session_id: sessionId, + }) + if (streamDelayMs > 0) await delay(streamDelayMs) + emit(ws, { + type: 'stream_event', + event: { + type: 'content_block_delta', + index: 0, + delta: { type: 'text_delta', text: `Echo: ${text}` }, + }, + session_id: sessionId, + }) + if (streamDelayMs > 0) await delay(streamDelayMs) + emit(ws, { + type: 'stream_event', + event: { type: 'content_block_stop', index: 0 }, + session_id: sessionId, + }) + emit(ws, { + type: 'result', + subtype: 'success', + is_error: false, + result: `Echo: ${text}`, + usage: { input_tokens: 3, output_tokens: 2 }, + session_id: sessionId, + }) + } + + if (parsed.type === 'control_request' && parsed.request?.subtype === 'interrupt') { + emit(ws, { + type: 'result', + subtype: 'success', + is_error: false, + result: 'Interrupted', + usage: { input_tokens: 0, output_tokens: 0 }, + session_id: sessionId, + }) + } + + if (parsed.type === 'control_request' && parsed.request?.subtype === 'get_session_usage') { + emit(ws, { + type: 'control_response', + response: { + subtype: 'success', + request_id: parsed.request_id, + response: { + totalCostUSD: 0.1234, + costDisplay: '$0.1234', + hasUnknownModelCost: false, + totalAPIDuration: 4, + totalDuration: 43, + totalLinesAdded: 0, + totalLinesRemoved: 0, + totalInputTokens: 27000, + totalOutputTokens: 41, + totalCacheReadInputTokens: 0, + totalCacheCreationInputTokens: 0, + totalWebSearchRequests: 0, + models: [{ + model: 'mock-opus', + displayName: 'mock-opus', + inputTokens: 27000, + outputTokens: 41, + cacheReadInputTokens: 0, + cacheCreationInputTokens: 0, + webSearchRequests: 0, + costUSD: 0.1234, + costDisplay: '$0.1234', + contextWindow: 200000, + maxOutputTokens: 8192, + }], + }, + }, + session_id: sessionId, + }) + } + + if (parsed.type === 'control_request' && parsed.request?.subtype === 'get_context_usage') { + emit(ws, { + type: 'control_response', + response: { + subtype: 'success', + request_id: parsed.request_id, + response: { + categories: [ + { name: 'System prompt', tokens: 6800, color: '#8a8a8a' }, + { name: 'MCP tools', tokens: 5900, color: '#06b6d4' }, + { name: 'Messages', tokens: 2400, color: '#7c3aed' }, + { name: 'Free space', tokens: 132000, color: '#a1a1aa' }, + ], + totalTokens: 27000, + maxTokens: 200000, + rawMaxTokens: 200000, + percentage: 13, + gridRows: Array.from({ length: 10 }, (_, row) => + Array.from({ length: 10 }, (_, col) => { + const index = row * 10 + col + return { + color: index < 13 ? '#06b6d4' : '#a1a1aa', + isFilled: index < 13, + categoryName: index < 13 ? 'Used' : 'Free space', + tokens: 2000, + percentage: 1, + squareFullness: index < 13 ? 1 : 0, + } + }), + ), + model: 'mock-opus', + estimateOnly: parsed.request.estimateOnly === true, + memoryFiles: [], + mcpTools: [{ name: 'mock_tool', serverName: 'mock', tokens: 144, isLoaded: true }], + agents: [], + skills: { totalSkills: 1, includedSkills: 1, tokens: 3000, skillFrontmatter: [] }, + isAutoCompactEnabled: true, + apiUsage: null, + }, + }, + session_id: sessionId, + }) + } + + if (parsed.type === 'control_request' && parsed.request?.subtype === 'mcp_status') { + emit(ws, { + type: 'control_response', + response: { + subtype: 'success', + request_id: parsed.request_id, + response: { + mcpServers: [{ name: 'mock', status: 'connected' }], + }, + }, + session_id: sessionId, + }) + } + } + })() +}) + +ws.addEventListener('close', () => { + process.exit(0) +}) diff --git a/_all-in-one-upload/cc-haha/src/server/__tests__/scheduled-tasks.test.ts b/_all-in-one-upload/cc-haha/src/server/__tests__/scheduled-tasks.test.ts new file mode 100644 index 0000000..8b03198 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/server/__tests__/scheduled-tasks.test.ts @@ -0,0 +1,371 @@ +/** + * Unit tests for CronService, SearchService, and Scheduled Tasks API + */ + +import { describe, it, expect, beforeEach, afterEach, spyOn } from 'bun:test' +import * as fs from 'fs/promises' +import * as path from 'path' +import * as os from 'os' +import { CronService } from '../services/cronService.js' +import { SearchService } from '../services/searchService.js' + +// ─── Test helpers ─────────────────────────────────────────────────────────── + +let tmpDir: string +const originalConfigDir = process.env.CLAUDE_CONFIG_DIR + +async function createTmpDir(): Promise { + const dir = path.join(os.tmpdir(), `claude-test-${Date.now()}-${Math.random().toString(36).slice(2)}`) + await fs.mkdir(dir, { recursive: true }) + return dir +} + +async function cleanupTmpDir(dir: string): Promise { + try { + await fs.rm(dir, { recursive: true, force: true }) + } catch { + // ignore + } +} + +// ─── CronService tests ───────────────────────────────────────────────────── + +describe('CronService', () => { + let service: CronService + + beforeEach(async () => { + tmpDir = await createTmpDir() + process.env.CLAUDE_CONFIG_DIR = tmpDir + service = new CronService() + }) + + afterEach(async () => { + if (originalConfigDir) { + process.env.CLAUDE_CONFIG_DIR = originalConfigDir + } else { + delete process.env.CLAUDE_CONFIG_DIR + } + await cleanupTmpDir(tmpDir) + }) + + it('should return empty list when no tasks file exists', async () => { + const tasks = await service.listTasks() + expect(tasks).toEqual([]) + }) + + it('should create a task with generated id and createdAt', async () => { + const task = await service.createTask({ + cron: '0 9 * * *', + prompt: 'Review commits', + recurring: true, + }) + + expect(task.id).toBeDefined() + expect(task.id).toHaveLength(8) // 4 bytes hex + expect(task.cron).toBe('0 9 * * *') + expect(task.prompt).toBe('Review commits') + expect(task.recurring).toBe(true) + expect(task.createdAt).toBeGreaterThan(0) + }) + + it('should persist tasks to file', async () => { + await service.createTask({ cron: '0 9 * * *', prompt: 'Task 1' }) + await service.createTask({ cron: '30 18 * * 5', prompt: 'Task 2' }) + + const tasks = await service.listTasks() + expect(tasks).toHaveLength(2) + expect(tasks[0].prompt).toBe('Task 1') + expect(tasks[1].prompt).toBe('Task 2') + }) + + it('should update an existing task', async () => { + const created = await service.createTask({ + cron: '0 9 * * *', + prompt: 'Original prompt', + }) + + const updated = await service.updateTask(created.id, { + prompt: 'Updated prompt', + recurring: true, + }) + + expect(updated.id).toBe(created.id) + expect(updated.prompt).toBe('Updated prompt') + expect(updated.recurring).toBe(true) + expect(updated.createdAt).toBe(created.createdAt) + }) + + it('should throw when updating a non-existent task', async () => { + await expect( + service.updateTask('nonexistent', { prompt: 'x' }), + ).rejects.toThrow('Task not found') + }) + + it('should delete a task', async () => { + const created = await service.createTask({ + cron: '0 9 * * *', + prompt: 'To delete', + }) + + await service.deleteTask(created.id) + const tasks = await service.listTasks() + expect(tasks).toHaveLength(0) + }) + + it('should throw when deleting a non-existent task', async () => { + await expect(service.deleteTask('nonexistent')).rejects.toThrow( + 'Task not found', + ) + }) + + it('should generate unique IDs', async () => { + const ids = new Set() + for (let i = 0; i < 20; i++) { + const task = await service.createTask({ + cron: '* * * * *', + prompt: `Task ${i}`, + }) + ids.add(task.id) + } + expect(ids.size).toBe(20) + }) + + it('should reject create when cron or prompt is missing', async () => { + await expect( + service.createTask({ cron: '', prompt: 'something' }), + ).rejects.toThrow() + + await expect( + service.createTask({ cron: '* * * * *', prompt: '' }), + ).rejects.toThrow() + }) + + it('should retry the atomic write when rename returns ENOENT', async () => { + const originalRename = fs.rename + let renameCalls = 0 + + const renameSpy = spyOn(fs, 'rename') + renameSpy.mockImplementation(async (...args) => { + renameCalls += 1 + + if (renameCalls === 1) { + const error = new Error( + 'ENOENT: no such file or directory, rename tmp -> scheduled_tasks.json', + ) as NodeJS.ErrnoException + error.code = 'ENOENT' + throw error + } + + return originalRename(...args) + }) + + try { + const task = await service.createTask({ + cron: '0 9 * * *', + prompt: 'Retry rename once', + }) + + const tasks = await service.listTasks() + expect(task.id).toBeDefined() + expect(tasks).toHaveLength(1) + expect(tasks[0]?.prompt).toBe('Retry rename once') + expect(renameCalls).toBe(2) + } finally { + renameSpy.mockRestore() + } + }) +}) + +// ─── SearchService tests ──────────────────────────────────────────────────── + +describe('SearchService', () => { + let service: SearchService + let searchDir: string + + beforeEach(async () => { + tmpDir = await createTmpDir() + process.env.CLAUDE_CONFIG_DIR = tmpDir + service = new SearchService() + + // 创建搜索用的临时文件 + searchDir = path.join(tmpDir, 'workspace') + await fs.mkdir(searchDir, { recursive: true }) + await fs.writeFile( + path.join(searchDir, 'hello.txt'), + 'Hello World\nThis is a test\nAnother line\n', + ) + await fs.writeFile( + path.join(searchDir, 'code.ts'), + 'function greet() {\n return "hello"\n}\n', + ) + }) + + afterEach(async () => { + if (originalConfigDir) { + process.env.CLAUDE_CONFIG_DIR = originalConfigDir + } else { + delete process.env.CLAUDE_CONFIG_DIR + } + await cleanupTmpDir(tmpDir) + }) + + it('should find matches in workspace files', async () => { + const results = await service.searchWorkspace('Hello', { cwd: searchDir }) + expect(results.length).toBeGreaterThan(0) + expect(results[0].text).toContain('Hello') + }) + + it('should return empty results when nothing matches', async () => { + const results = await service.searchWorkspace('ZZZZNONEXISTENT', { + cwd: searchDir, + }) + expect(results).toHaveLength(0) + }) + + it('should respect maxResults limit', async () => { + // 写入多行匹配 + const lines = Array.from({ length: 50 }, (_, i) => `match line ${i}`).join( + '\n', + ) + await fs.writeFile(path.join(searchDir, 'many.txt'), lines) + + const results = await service.searchWorkspace('match', { + cwd: searchDir, + maxResults: 5, + }) + expect(results.length).toBeLessThanOrEqual(5) + }) + + it('should reject empty query', async () => { + await expect(service.searchWorkspace('')).rejects.toThrow() + }) + + it('should return empty session results when no projects dir exists', async () => { + const results = await service.searchSessions('test') + expect(results).toEqual([]) + }) +}) + +// ─── Scheduled Tasks API integration ──────────────────────────────────────── + +describe('Scheduled Tasks API', () => { + // 直接测试 handler 函数,不需要启动完整服务器 + let handleScheduledTasksApi: ( + req: Request, + url: URL, + segments: string[], + ) => Promise + + beforeEach(async () => { + tmpDir = await createTmpDir() + process.env.CLAUDE_CONFIG_DIR = tmpDir + + // 动态导入以获取最新的环境变量 + const mod = await import('../api/scheduled-tasks.js') + handleScheduledTasksApi = mod.handleScheduledTasksApi + }) + + afterEach(async () => { + if (originalConfigDir) { + process.env.CLAUDE_CONFIG_DIR = originalConfigDir + } else { + delete process.env.CLAUDE_CONFIG_DIR + } + await cleanupTmpDir(tmpDir) + }) + + it('should list empty tasks via GET', async () => { + const req = new Request('http://localhost/api/scheduled-tasks', { + method: 'GET', + }) + const url = new URL(req.url) + const resp = await handleScheduledTasksApi(req, url, [ + 'api', + 'scheduled-tasks', + ]) + const body = (await resp.json()) as { tasks: unknown[] } + expect(resp.status).toBe(200) + expect(body.tasks).toEqual([]) + }) + + it('should create a task via POST', async () => { + const req = new Request('http://localhost/api/scheduled-tasks', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + cron: '0 9 * * *', + prompt: 'Daily review', + recurring: true, + }), + }) + const url = new URL(req.url) + const resp = await handleScheduledTasksApi(req, url, [ + 'api', + 'scheduled-tasks', + ]) + const body = (await resp.json()) as { task: { id: string; prompt: string } } + expect(resp.status).toBe(201) + expect(body.task.id).toBeDefined() + expect(body.task.prompt).toBe('Daily review') + }) + + it('should CRUD a full lifecycle', async () => { + // Create + const createReq = new Request('http://localhost/api/scheduled-tasks', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ cron: '0 9 * * *', prompt: 'Test task' }), + }) + const createResp = await handleScheduledTasksApi( + createReq, + new URL(createReq.url), + ['api', 'scheduled-tasks'], + ) + const { task } = (await createResp.json()) as { + task: { id: string; prompt: string } + } + + // Update + const updateReq = new Request( + `http://localhost/api/scheduled-tasks/${task.id}`, + { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ prompt: 'Updated task' }), + }, + ) + const updateResp = await handleScheduledTasksApi( + updateReq, + new URL(updateReq.url), + ['api', 'scheduled-tasks', task.id], + ) + const updated = (await updateResp.json()) as { + task: { id: string; prompt: string } + } + expect(updated.task.prompt).toBe('Updated task') + + // Delete + const deleteReq = new Request( + `http://localhost/api/scheduled-tasks/${task.id}`, + { method: 'DELETE' }, + ) + const deleteResp = await handleScheduledTasksApi( + deleteReq, + new URL(deleteReq.url), + ['api', 'scheduled-tasks', task.id], + ) + expect(deleteResp.status).toBe(200) + + // Verify empty + const listReq = new Request('http://localhost/api/scheduled-tasks', { + method: 'GET', + }) + const listResp = await handleScheduledTasksApi( + listReq, + new URL(listReq.url), + ['api', 'scheduled-tasks'], + ) + const list = (await listResp.json()) as { tasks: unknown[] } + expect(list.tasks).toHaveLength(0) + }) +}) diff --git a/_all-in-one-upload/cc-haha/src/server/api/agents.ts b/_all-in-one-upload/cc-haha/src/server/api/agents.ts new file mode 100644 index 0000000..de23d84 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/server/api/agents.ts @@ -0,0 +1,234 @@ +/** + * Agents REST API + * + * GET /api/agents — 获取 Agent 列表 + * GET /api/agents/:name — 获取 Agent 详情 + * POST /api/agents — 创建 Agent + * PUT /api/agents/:name — 更新 Agent + * DELETE /api/agents/:name — 删除 Agent + * + * GET /api/tasks — 获取后台任务列表 + * GET /api/tasks/:id — 获取任务详情 + */ + +import { AgentService } from '../services/agentService.js' +import { taskService } from '../services/taskService.js' +import { ApiError, errorResponse } from '../middleware/errorHandler.js' +import { resetTaskList } from '../../utils/tasks.js' +import { + resolveAgentModelDisplay, + resolveAgentOverrides, + type ResolvedAgent, +} from '../../tools/AgentTool/agentDisplay.js' +import { + clearAgentDefinitionsCache, + getAgentDefinitionsWithOverrides, + type AgentDefinition as SharedAgentDefinition, +} from '../../tools/AgentTool/loadAgentsDir.js' +import { getCwd } from '../../utils/cwd.js' + +const agentService = new AgentService() + +export async function handleAgentsApi( + req: Request, + url: URL, + segments: string[], +): Promise { + try { + const resource = segments[1] // 'agents' | 'tasks' + + if (resource === 'tasks') { + return await handleTasksApi(req, segments) + } + + return await handleAgents(req, url, segments) + } catch (error) { + return errorResponse(error) + } +} + +// ─── Agent CRUD ───────────────────────────────────────────────────────────── + +async function handleAgents( + req: Request, + url: URL, + segments: string[], +): Promise { + const method = req.method + const agentName = segments[2] ? decodeURIComponent(segments[2]) : undefined + + // ── GET /api/agents ────────────────────────────────────────────────── + if (method === 'GET' && !agentName) { + const cwd = url.searchParams.get('cwd') || getCwd() + const { activeAgents, allAgents } = await getAgentDefinitionsWithOverrides(cwd) + const resolvedAgents = resolveAgentOverrides(allAgents, activeAgents) + + return Response.json({ + activeAgents: activeAgents.map(agent => serializeActiveAgent(agent, true)), + allAgents: resolvedAgents.map(serializeResolvedAgent), + }) + } + + // ── GET /api/agents/:name ──────────────────────────────────────────── + if (method === 'GET' && agentName) { + const agent = await agentService.getAgent(agentName) + if (!agent) { + throw ApiError.notFound(`Agent not found: ${agentName}`) + } + return Response.json({ agent }) + } + + // ── POST /api/agents ───────────────────────────────────────────────── + if (method === 'POST' && !agentName) { + const body = await parseJsonBody(req) + if (!body.name || typeof body.name !== 'string') { + throw ApiError.badRequest('Missing or invalid "name" in request body') + } + await agentService.createAgent({ + name: body.name as string, + description: body.description as string | undefined, + model: body.model as string | undefined, + tools: body.tools as string[] | undefined, + systemPrompt: body.systemPrompt as string | undefined, + color: body.color as string | undefined, + }) + clearAgentDefinitionsCache() + return Response.json({ ok: true }, { status: 201 }) + } + + // ── PUT /api/agents/:name ──────────────────────────────────────────── + if (method === 'PUT' && agentName) { + const body = await parseJsonBody(req) + await agentService.updateAgent(agentName, body as Record) + clearAgentDefinitionsCache() + const updated = await agentService.getAgent(agentName) + return Response.json({ agent: updated }) + } + + // ── DELETE /api/agents/:name ───────────────────────────────────────── + if (method === 'DELETE' && agentName) { + await agentService.deleteAgent(agentName) + clearAgentDefinitionsCache() + return Response.json({ ok: true }) + } + + throw new ApiError( + 405, + `Method ${method} not allowed on /api/agents${agentName ? `/${agentName}` : ''}`, + 'METHOD_NOT_ALLOWED', + ) +} + +// ─── Tasks API ───────────────────────────────────────────────────────────── +// +// GET /api/tasks → list all tasks (across all task lists) +// GET /api/tasks/lists → list all task lists with summaries +// GET /api/tasks/lists/:taskListId → get all tasks for a specific task list +// GET /api/tasks/lists/:taskListId/:id → get a single task +// POST /api/tasks/lists/:taskListId/reset → clear a completed task list + +async function handleTasksApi( + req: Request, + segments: string[], +): Promise { + const sub = segments[2] // 'lists' or undefined + + if (sub === 'lists') { + const taskListId = segments[3] + const taskId = segments[4] + + if (req.method === 'POST' && taskListId && taskId === 'reset') { + await resetTaskList(taskListId) + return Response.json({ ok: true }) + } + + if (req.method !== 'GET') { + throw new ApiError( + 405, + `Method ${req.method} not allowed on /api/tasks/lists`, + 'METHOD_NOT_ALLOWED', + ) + } + + if (taskListId && taskId) { + // GET /api/tasks/lists/:taskListId/:taskId + const task = await taskService.getTask(taskListId, taskId) + if (!task) throw ApiError.notFound(`Task not found: ${taskListId}/${taskId}`) + return Response.json({ task }) + } + + if (taskListId) { + // GET /api/tasks/lists/:taskListId + const tasks = await taskService.getTasksForList(taskListId) + return Response.json({ tasks }) + } + + // GET /api/tasks/lists + const lists = await taskService.listTaskLists() + return Response.json({ lists }) + } + + if (req.method !== 'GET') { + throw new ApiError( + 405, + `Method ${req.method} not allowed on /api/tasks`, + 'METHOD_NOT_ALLOWED', + ) + } + + // GET /api/tasks — list all tasks + const tasks = await taskService.listTasks() + return Response.json({ tasks }) +} + +// ─── Helpers ──────────────────────────────────────────────────────────────── + +async function parseJsonBody(req: Request): Promise> { + try { + return (await req.json()) as Record + } catch { + throw ApiError.badRequest('Invalid JSON body') + } +} + +type ApiAgentDefinition = { + agentType: string + description?: string + model?: string + modelDisplay?: string + tools?: string[] + systemPrompt?: string + color?: string + source: SharedAgentDefinition['source'] + baseDir?: string + isActive: boolean +} + +type ApiResolvedAgentDefinition = ApiAgentDefinition & { + overriddenBy?: SharedAgentDefinition['source'] +} + +function serializeActiveAgent( + agent: SharedAgentDefinition, + isActive: boolean, +): ApiAgentDefinition { + return { + agentType: agent.agentType, + description: agent.whenToUse, + model: agent.model, + modelDisplay: resolveAgentModelDisplay(agent), + tools: agent.tools, + systemPrompt: agent.getSystemPrompt.length === 0 ? agent.getSystemPrompt() : undefined, + color: agent.color, + source: agent.source, + baseDir: agent.baseDir, + isActive, + } +} + +function serializeResolvedAgent(agent: ResolvedAgent): ApiResolvedAgentDefinition { + return { + ...serializeActiveAgent(agent, !agent.overriddenBy), + overriddenBy: agent.overriddenBy, + } +} diff --git a/_all-in-one-upload/cc-haha/src/server/index.ts b/_all-in-one-upload/cc-haha/src/server/index.ts new file mode 100644 index 0000000..7e6816a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/server/index.ts @@ -0,0 +1,271 @@ +/** + * Claude Code Desktop App — HTTP + WebSocket Server + * + * 为桌面端 UI 提供 REST API 和 WebSocket 实时通信。 + * 读写与 CLI 完全相同的文件系统,确保 CLI/UI 数据互通。 + */ + +import { handleApiRequest } from './router.js' +import { handleWebSocket, type WebSocketData } from './ws/handler.js' +import { corsHeaders } from './middleware/cors.js' +import { requireAuth } from './middleware/auth.js' +import { teamWatcher } from './services/teamWatcher.js' +import { cronScheduler } from './services/cronScheduler.js' +import { handleProxyRequest } from './proxy/handler.js' +import { ProviderService } from './services/providerService.js' +import { handleHahaOAuthCallback } from './api/haha-oauth.js' +import { ensureDesktopCliLauncherInstalled } from './services/desktopCliLauncherService.js' + +function readArgValue(flag: string): string | undefined { + const args = process.argv.slice(2) + const index = args.indexOf(flag) + if (index === -1) return undefined + return args[index + 1] +} + +function hasArgFlag(flag: string): boolean { + return process.argv.slice(2).includes(flag) +} + +function resolveServerOptions() { + const portArg = readArgValue('--port') + const port = Number.parseInt(portArg || process.env.SERVER_PORT || '3456', 10) + const host = readArgValue('--host') || process.env.SERVER_HOST || '127.0.0.1' + const cliPath = readArgValue('--cli-path') + const authRequired = hasArgFlag('--auth-required') + + if (cliPath) { + process.env.CLAUDE_CLI_PATH = cliPath + } + + return { port, host, authRequired } +} + +const SERVER_OPTIONS = resolveServerOptions() +const PORT = SERVER_OPTIONS.port +const HOST = SERVER_OPTIONS.host + +export function startServer(port = PORT, host = HOST) { + ProviderService.setServerPort(port) + const localConnectHost = + host === '0.0.0.0' || host === '127.0.0.1' || host === 'localhost' + ? '127.0.0.1' + : host + + /** + * Auth is required when explicitly opted in or when bound to a non-localhost address. + * - Default localhost dev: no auth needed (tests pass as-is). + * - Production / non-localhost (e.g. 0.0.0.0): auth enforced automatically. + * - Explicit opt-in: SERVER_AUTH_REQUIRED=1 forces auth even on localhost. + * - Explicit opt-out: SERVER_AUTH_REQUIRED=0 disables auth even on 0.0.0.0 + * (use only in trusted environments — e.g. a Docker container whose port + * is published to host loopback only). Takes precedence over the auto rule. + */ + const authForcedOff = process.env.SERVER_AUTH_REQUIRED === '0' + const authRequired = authForcedOff + ? false + : SERVER_OPTIONS.authRequired || + process.env.SERVER_AUTH_REQUIRED === '1' || + host !== '127.0.0.1' + + const server = Bun.serve({ + port, + hostname: host, + idleTimeout: 60, + + async fetch(req, server) { + const url = new URL(req.url) + + const origin = req.headers.get('Origin') + + // Handle CORS preflight + if (req.method === 'OPTIONS') { + return new Response(null, { status: 204, headers: corsHeaders(origin) }) + } + + // WebSocket upgrade + if (url.pathname.startsWith('/ws/')) { + // Enforce authentication when required + if (authRequired) { + const authError = requireAuth(req) + if (authError) { + const headers = new Headers(authError.headers) + for (const [key, value] of Object.entries(corsHeaders(origin))) { + headers.set(key, value) + } + return new Response(authError.body, { status: authError.status, headers }) + } + } + + // Validate session ID format + const sessionId = url.pathname.split('/').pop() || '' + if (!sessionId || !/^[0-9a-zA-Z_-]{1,64}$/.test(sessionId)) { + return new Response('Invalid session ID', { status: 400 }) + } + const upgraded = server.upgrade(req, { + data: { + sessionId, + connectedAt: Date.now(), + channel: 'client', + sdkToken: null, + serverPort: port, + serverHost: localConnectHost, + }, + }) + if (upgraded) return undefined + return new Response('WebSocket upgrade failed', { status: 400 }) + } + + // Internal SDK WebSocket used by the spawned Claude CLI. + if (url.pathname.startsWith('/sdk/')) { + const sessionId = url.pathname.split('/').pop() || '' + if (!sessionId || !/^[0-9a-zA-Z_-]{1,64}$/.test(sessionId)) { + return new Response('Invalid session ID', { status: 400 }) + } + const upgraded = server.upgrade(req, { + data: { + sessionId, + connectedAt: Date.now(), + channel: 'sdk', + sdkToken: url.searchParams.get('token'), + serverPort: port, + serverHost: localConnectHost, + }, + }) + if (upgraded) return undefined + return new Response('WebSocket upgrade failed', { status: 400 }) + } + + if (url.pathname === '/callback') { + return handleHahaOAuthCallback(url) + } + + // REST API + if (url.pathname.startsWith('/api/')) { + // Enforce authentication when required + if (authRequired) { + const authError = requireAuth(req) + if (authError) { + const headers = new Headers(authError.headers) + for (const [key, value] of Object.entries(corsHeaders(origin))) { + headers.set(key, value) + } + return new Response(authError.body, { status: authError.status, headers }) + } + } + + try { + const response = await handleApiRequest(req, url) + // Add CORS headers to all responses + const headers = new Headers(response.headers) + for (const [key, value] of Object.entries(corsHeaders(origin))) { + headers.set(key, value) + } + return new Response(response.body, { + status: response.status, + headers, + }) + } catch (error) { + console.error('[Server] API error:', error) + return Response.json( + { error: 'Internal server error' }, + { status: 500, headers: corsHeaders() } + ) + } + } + + // Proxy — protocol-translating reverse proxy for OpenAI-compatible APIs + if (url.pathname.startsWith('/proxy/')) { + if (authRequired) { + const authError = requireAuth(req) + if (authError) { + const headers = new Headers(authError.headers) + for (const [key, value] of Object.entries(corsHeaders(origin))) { + headers.set(key, value) + } + return new Response(authError.body, { status: authError.status, headers }) + } + } + try { + const response = await handleProxyRequest(req, url) + const headers = new Headers(response.headers) + for (const [key, value] of Object.entries(corsHeaders(origin))) { + headers.set(key, value) + } + return new Response(response.body, { + status: response.status, + headers, + }) + } catch (error) { + console.error('[Server] Proxy error:', error) + return Response.json( + { type: 'error', error: { type: 'api_error', message: 'Internal proxy error' } }, + { status: 500, headers: corsHeaders() }, + ) + } + } + + // Health check + if (url.pathname === '/health') { + return Response.json( + { status: 'ok', timestamp: new Date().toISOString() }, + { headers: corsHeaders(origin) }, + ) + } + + return new Response('Not Found', { status: 404 }) + }, + + websocket: handleWebSocket, + }) + + // Start watching ~/.claude/teams/ for real-time WebSocket push + teamWatcher.start() + + // Start the cron scheduler to execute scheduled tasks + cronScheduler.start() + + void ensureDesktopCliLauncherInstalled().catch((error) => { + console.error( + '[desktop-cli-launcher] failed to install bundled launcher:', + error instanceof Error ? error.message : error, + ) + }) + + console.log(`[Server] Claude Code API server running at http://${host}:${port}`) + return server +} + +// ─── Graceful shutdown: kill all CLI subprocesses on exit ──────────────────── +import { conversationService } from './services/conversationService.js' + +function cleanupAllSessions() { + const active = conversationService.getActiveSessions() + if (active.length > 0) { + console.log(`[Server] Shutting down — killing ${active.length} CLI subprocess(es)`) + for (const sessionId of active) { + conversationService.stopSession(sessionId) + } + } +} + +process.on('SIGTERM', () => { + console.log('[Server] Received SIGTERM') + cleanupAllSessions() + process.exit(0) +}) + +process.on('SIGINT', () => { + console.log('[Server] Received SIGINT') + cleanupAllSessions() + process.exit(0) +}) + +process.on('exit', () => { + cleanupAllSessions() +}) + +// Direct execution +if (import.meta.main) { + startServer() +} diff --git a/_all-in-one-upload/cc-haha/src/server/middleware/cors.test.ts b/_all-in-one-upload/cc-haha/src/server/middleware/cors.test.ts new file mode 100644 index 0000000..5753ada --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/server/middleware/cors.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from 'bun:test' +import { corsHeaders } from './cors' + +describe('corsHeaders', () => { + it('allows localhost browser origins', () => { + expect(corsHeaders('http://127.0.0.1:1420')['Access-Control-Allow-Origin']).toBe('http://127.0.0.1:1420') + expect(corsHeaders('http://localhost:3000')['Access-Control-Allow-Origin']).toBe('http://localhost:3000') + }) + + it('allows tauri webview origins used in production builds', () => { + expect(corsHeaders('http://tauri.localhost')['Access-Control-Allow-Origin']).toBe('http://tauri.localhost') + expect(corsHeaders('https://tauri.localhost')['Access-Control-Allow-Origin']).toBe('https://tauri.localhost') + expect(corsHeaders('tauri://localhost')['Access-Control-Allow-Origin']).toBe('tauri://localhost') + }) + + it('falls back for unknown origins', () => { + expect(corsHeaders('https://example.com')['Access-Control-Allow-Origin']).toBe('http://localhost:3000') + expect(corsHeaders(null)['Access-Control-Allow-Origin']).toBe('http://localhost:3000') + }) +}) diff --git a/_all-in-one-upload/cc-haha/src/server/proxy/streaming/openaiChatStreamToAnthropic.ts b/_all-in-one-upload/cc-haha/src/server/proxy/streaming/openaiChatStreamToAnthropic.ts new file mode 100644 index 0000000..054ecc7 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/server/proxy/streaming/openaiChatStreamToAnthropic.ts @@ -0,0 +1,542 @@ +/** + * Streaming SSE transformation: OpenAI Chat Completions → Anthropic Messages + * + * Converts an OpenAI-compatible streaming response into Anthropic Messages + * streaming format. Follows the patterns established by LiteLLM's + * AnthropicStreamWrapper for correctness across many providers. + * + * Anthropic event order: + * message_start + * → (content_block_start → content_block_delta* → content_block_stop)* + * → message_delta + * → message_stop + * + * Derived from cc-switch (https://github.com/farion1231/cc-switch) + * Original work by Jason Young, MIT License + * + * Provider-specific reasoning formats handled: + * - delta.reasoning_content (DeepSeek, OpenRouter, XAI, Perplexity, …) + * - delta.thinking_blocks (OpenAI o-series) + * - delta.reasoning (GLM-5, Cerebras, Groq — mapped to reasoning_content) + */ + +import type { OpenAIChatStreamChunk } from '../transform/types.js' + +// ─── Types ───────────────────────────────────────────────── + +type ContentBlockType = 'text' | 'thinking' | 'tool_use' + +type ToolBlockState = { + id: string + name: string + argsBuffer: string + started: boolean + anthropicIndex: number +} + +type SseEvent = { event: string; data: unknown } + +type StreamState = { + // Event queue — guarantees correct multi-event ordering + queue: SseEvent[] + + // Content block tracking (mirrors LiteLLM's state machine) + currentBlockType: ContentBlockType + currentBlockIndex: number + nextContentIndex: number + blockStartSent: boolean // content_block_start emitted for current block? + blockStopSent: boolean // content_block_stop emitted for current block? + + // Tool call tracking + toolBlocks: Map + + // Message lifecycle + model: string + messageStartSent: boolean + messageDeltaSent: boolean + messageStopSent: boolean + + // Holding pattern: hold message_delta until usage arrives + // (some providers send finish_reason and usage in separate chunks) + heldMessageDelta: SseEvent | null +} + +// ─── Helpers ─────────────────────────────────────────────── + +function formatSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n` +} + +function createState(model: string): StreamState { + return { + queue: [], + currentBlockType: 'text', + currentBlockIndex: -1, + nextContentIndex: 0, + blockStartSent: false, + blockStopSent: false, + toolBlocks: new Map(), + model, + messageStartSent: false, + messageDeltaSent: false, + messageStopSent: false, + heldMessageDelta: null, + } +} + +// ─── Public entry point ──────────────────────────────────── + +/** + * Transform an OpenAI Chat Completions SSE stream into an Anthropic Messages SSE stream. + */ +export function openaiChatStreamToAnthropic( + upstream: ReadableStream, + model: string, +): ReadableStream { + const encoder = new TextEncoder() + const decoder = new TextDecoder() + let buffer = '' + const state = createState(model) + + return new ReadableStream({ + async start(controller) { + const reader = upstream.getReader() + let errored = false + + try { + while (true) { + const { done, value } = await reader.read() + if (done) break + + buffer += decoder.decode(value, { stream: true }) + const lines = buffer.split('\n') + buffer = lines.pop() || '' + + for (const line of lines) { + const trimmed = line.trim() + if (!trimmed || trimmed.startsWith(':')) continue + + if (trimmed === 'data: [DONE]') { + finalizeStream(state) + flushQueue(state, controller, encoder) + continue + } + + if (!trimmed.startsWith('data: ')) continue + const jsonStr = trimmed.slice(6) + + let chunk: OpenAIChatStreamChunk + try { + chunk = JSON.parse(jsonStr) + } catch { + continue + } + + processChunk(chunk, state) + flushQueue(state, controller, encoder) + } + } + } catch (err) { + errored = true + controller.error(err) + } finally { + if (!errored) { + finalizeStream(state) + flushQueue(state, controller, encoder) + controller.close() + } + } + }, + }) +} + +// ─── Queue management ────────────────────────────────────── + +function enqueue(state: StreamState, event: string, data: unknown): void { + state.queue.push({ event, data }) +} + +function flushQueue( + state: StreamState, + controller: ReadableStreamDefaultController, + encoder: TextEncoder, +): void { + for (const item of state.queue) { + controller.enqueue(encoder.encode(formatSse(item.event, item.data))) + } + state.queue.length = 0 +} + +// ─── Message lifecycle events ────────────────────────────── + +function ensureMessageStart(state: StreamState, chunkId?: string): void { + if (state.messageStartSent) return + state.messageStartSent = true + enqueue(state, 'message_start', { + type: 'message_start', + message: { + id: chunkId || `msg_${Date.now()}`, + type: 'message', + role: 'assistant', + content: [], + model: state.model, + stop_reason: null, + stop_sequence: null, + usage: { input_tokens: 0, output_tokens: 0 }, + }, + }) +} + +// ─── Content block lifecycle ─────────────────────────────── + +function openBlock(state: StreamState, blockType: ContentBlockType, block: Record): number { + const index = state.nextContentIndex++ + state.currentBlockType = blockType + state.currentBlockIndex = index + state.blockStartSent = true + state.blockStopSent = false + enqueue(state, 'content_block_start', { + type: 'content_block_start', + index, + content_block: block, + }) + return index +} + +function emitDelta(state: StreamState, index: number, delta: Record): void { + enqueue(state, 'content_block_delta', { + type: 'content_block_delta', + index, + delta, + }) +} + +function closeCurrentBlock(state: StreamState): void { + if (!state.blockStartSent || state.blockStopSent) return + state.blockStopSent = true + enqueue(state, 'content_block_stop', { + type: 'content_block_stop', + index: state.currentBlockIndex, + }) +} + +function closeAllToolBlocks(state: StreamState): void { + for (const [, block] of state.toolBlocks) { + if (block.started) { + enqueue(state, 'content_block_stop', { + type: 'content_block_stop', + index: block.anthropicIndex, + }) + } + } + state.toolBlocks.clear() +} + +function closeAllOpenBlocks(state: StreamState): void { + // Close current text/thinking block + closeCurrentBlock(state) + // Close all tool blocks + closeAllToolBlocks(state) +} + +// ─── Block type detection (follows LiteLLM priority) ─────── + +type DeltaEx = Record & { + content?: string | null + tool_calls?: Array<{ + index: number + id?: string + type?: string + function?: { name?: string; arguments?: string } + }> +} + +/** + * Extract reasoning/thinking content from delta regardless of provider format. + * + * Handles: + * delta.reasoning_content — DeepSeek, OpenRouter, XAI, Perplexity + * delta.reasoning — GLM-5, Cerebras, Groq + * delta.thinking_blocks — OpenAI o-series + */ +function extractReasoning(delta: DeltaEx): { thinking: string; signature: string } | null { + // Format 1: reasoning_content (most common) + if (typeof delta.reasoning_content === 'string' && delta.reasoning_content) { + return { thinking: delta.reasoning_content, signature: '' } + } + + // Format 2: reasoning (GLM-5, Cerebras, Groq) + if (typeof delta.reasoning === 'string' && delta.reasoning) { + return { thinking: delta.reasoning, signature: '' } + } + + // Format 3: thinking_blocks (OpenAI o-series) + const thinkingBlocks = delta.thinking_blocks as Array> | undefined + if (Array.isArray(thinkingBlocks) && thinkingBlocks.length > 0) { + const block = thinkingBlocks[0] + if (block.type === 'thinking') { + const thinking = (block.thinking as string) || '' + const signature = (block.signature as string) || '' + if (thinking || signature) { + return { thinking, signature } + } + } + } + + return null +} + +/** + * Determine what block type this chunk carries and whether it's a new block. + * Priority (matches LiteLLM): tool_calls > text > reasoning > ignore + */ +function detectBlockTransition( + delta: DeltaEx, + state: StreamState, +): { type: ContentBlockType; isNew: boolean } | null { + // Priority 1: Tool calls + if (delta.tool_calls && delta.tool_calls.length > 0) { + const tc = delta.tool_calls[0] + // A tool call with function.name signals a NEW tool block + const isNew = state.currentBlockType !== 'tool_use' || !!(tc.function?.name) + return { type: 'tool_use', isNew } + } + + // Priority 2: Text content + if (delta.content != null && delta.content !== '') { + const isNew = state.currentBlockType !== 'text' || !state.blockStartSent + return { type: 'text', isNew } + } + + // Priority 3: Reasoning/thinking + const reasoning = extractReasoning(delta) + if (reasoning) { + const isNew = state.currentBlockType !== 'thinking' || !state.blockStartSent + return { type: 'thinking', isNew } + } + + return null +} + +// ─── Main chunk processing ───────────────────────────────── + +function processChunk(chunk: OpenAIChatStreamChunk, state: StreamState): void { + const choice = chunk.choices?.[0] + + // Handle chunks with empty/missing choices (some providers send these) + if (!choice) { + // Check if this is a usage-only chunk (no choices but has usage) + if (chunk.usage && state.heldMessageDelta) { + mergeUsageIntoHeldDelta(state, chunk.usage) + } + return + } + + // Update model from first chunk + state.model = chunk.model || state.model + ensureMessageStart(state, chunk.id) + + const delta = choice.delta as DeltaEx + + // Detect what this chunk carries + const transition = detectBlockTransition(delta, state) + + if (transition) { + // Handle block transition: close previous block if type changed + if (transition.isNew && state.blockStartSent && !state.blockStopSent) { + if (transition.type !== 'tool_use') { + // For text/thinking, close the current block + closeCurrentBlock(state) + } else if (state.currentBlockType !== 'tool_use') { + // Switching TO tool_use from text/thinking: close current + closeCurrentBlock(state) + } + } + + switch (transition.type) { + case 'thinking': + handleThinking(delta, state) + break + case 'text': + handleText(delta, state) + break + case 'tool_use': + handleToolCalls(delta, state) + break + } + } + + // Handle finish_reason + if (choice.finish_reason) { + handleFinishReason(choice.finish_reason, chunk, state) + } +} + +// ─── Content handlers ────────────────────────────────────── + +function handleThinking(delta: DeltaEx, state: StreamState): void { + const reasoning = extractReasoning(delta) + if (!reasoning) return + + if (state.currentBlockType !== 'thinking' || !state.blockStartSent) { + openBlock(state, 'thinking', { type: 'thinking', thinking: '' }) + } + + if (reasoning.thinking) { + emitDelta(state, state.currentBlockIndex, { + type: 'thinking_delta', thinking: reasoning.thinking, + }) + } + if (reasoning.signature) { + emitDelta(state, state.currentBlockIndex, { + type: 'signature_delta', signature: reasoning.signature, + }) + } +} + +function handleText(delta: DeltaEx, state: StreamState): void { + if (delta.content == null || delta.content === '') return + + if (state.currentBlockType !== 'text' || !state.blockStartSent) { + openBlock(state, 'text', { type: 'text', text: '' }) + } + + emitDelta(state, state.currentBlockIndex, { + type: 'text_delta', text: delta.content, + }) +} + +function handleToolCalls(delta: DeltaEx, state: StreamState): void { + if (!delta.tool_calls) return + + for (const tc of delta.tool_calls) { + const tcIndex = tc.index + + if (!state.toolBlocks.has(tcIndex)) { + state.toolBlocks.set(tcIndex, { + id: '', name: '', argsBuffer: '', started: false, anthropicIndex: -1, + }) + } + + const block = state.toolBlocks.get(tcIndex)! + if (tc.id) block.id = tc.id + if (tc.function?.name) block.name += tc.function.name + if (tc.function?.arguments) block.argsBuffer += tc.function.arguments + + // Start tool block once we have id + name + if (!block.started && block.id && block.name) { + block.started = true + block.anthropicIndex = state.nextContentIndex++ + state.currentBlockType = 'tool_use' + state.blockStartSent = true + state.blockStopSent = false + + enqueue(state, 'content_block_start', { + type: 'content_block_start', + index: block.anthropicIndex, + content_block: { type: 'tool_use', id: block.id, name: block.name, input: {} }, + }) + + // Flush buffered arguments + if (block.argsBuffer) { + emitDelta(state, block.anthropicIndex, { + type: 'input_json_delta', partial_json: block.argsBuffer, + }) + } + } else if (block.started && tc.function?.arguments) { + emitDelta(state, block.anthropicIndex, { + type: 'input_json_delta', partial_json: tc.function.arguments, + }) + } + } +} + +// ─── Finish & usage handling ─────────────────────────────── + +function handleFinishReason( + finishReason: string, + chunk: OpenAIChatStreamChunk, + state: StreamState, +): void { + if (state.messageDeltaSent) return + + // CRITICAL: close ALL content blocks BEFORE message_delta + closeAllOpenBlocks(state) + + const stopReason = mapFinishReason(finishReason) + const usage = chunk.usage + ? { output_tokens: chunk.usage.completion_tokens || 0 } + : { output_tokens: 0 } + + const messageDelta: SseEvent = { + event: 'message_delta', + data: { + type: 'message_delta', + delta: { stop_reason: stopReason, stop_sequence: null }, + usage, + }, + } + + // If usage is available in the same chunk, emit immediately + if (chunk.usage) { + state.messageDeltaSent = true + state.queue.push(messageDelta) + } else { + // Hold message_delta, wait for usage chunk + state.heldMessageDelta = messageDelta + } +} + +function mergeUsageIntoHeldDelta( + state: StreamState, + usage: NonNullable, +): void { + if (!state.heldMessageDelta) return + + const data = state.heldMessageDelta.data as Record + data.usage = { output_tokens: usage.completion_tokens || 0 } + state.messageDeltaSent = true + state.queue.push(state.heldMessageDelta) + state.heldMessageDelta = null +} + +function finalizeStream(state: StreamState): void { + if (state.messageStopSent) return + state.messageStopSent = true + + ensureMessageStart(state) + + // Close any remaining open blocks + closeAllOpenBlocks(state) + + // Flush held message_delta if still waiting for usage + if (state.heldMessageDelta && !state.messageDeltaSent) { + state.messageDeltaSent = true + state.queue.push(state.heldMessageDelta) + state.heldMessageDelta = null + } + + // Emit message_delta if never sent (e.g., stream ended without finish_reason) + if (!state.messageDeltaSent) { + state.messageDeltaSent = true + enqueue(state, 'message_delta', { + type: 'message_delta', + delta: { stop_reason: 'end_turn', stop_sequence: null }, + usage: { output_tokens: 0 }, + }) + } + + enqueue(state, 'message_stop', { type: 'message_stop' }) +} + +// ─── Utilities ───────────────────────────────────────────── + +function mapFinishReason(reason: string): string { + switch (reason) { + case 'stop': return 'end_turn' + case 'tool_calls': return 'tool_use' + case 'length': return 'max_tokens' + case 'content_filter': return 'end_turn' + default: return 'end_turn' + } +} diff --git a/_all-in-one-upload/cc-haha/src/services/autoDream/autoDream.ts b/_all-in-one-upload/cc-haha/src/services/autoDream/autoDream.ts new file mode 100644 index 0000000..d387d9f --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/autoDream/autoDream.ts @@ -0,0 +1,324 @@ +// biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered +// Background memory consolidation. Fires the /dream prompt as a forked +// subagent when time-gate passes AND enough sessions have accumulated. +// +// Gate order (cheapest first): +// 1. Time: hours since lastConsolidatedAt >= minHours (one stat) +// 2. Sessions: transcript count with mtime > lastConsolidatedAt >= minSessions +// 3. Lock: no other process mid-consolidation +// +// State is closure-scoped inside initAutoDream() rather than module-level +// (tests call initAutoDream() in beforeEach for a fresh closure). + +import type { REPLHookContext } from '../../utils/hooks/postSamplingHooks.js' +import { + createCacheSafeParams, + runForkedAgent, +} from '../../utils/forkedAgent.js' +import { + createUserMessage, + createMemorySavedMessage, +} from '../../utils/messages.js' +import type { Message } from '../../types/message.js' +import { logForDebugging } from '../../utils/debug.js' +import type { ToolUseContext } from '../../Tool.js' +import { logEvent } from '../analytics/index.js' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js' +import { isAutoMemoryEnabled, getAutoMemPath } from '../../memdir/paths.js' +import { isAutoDreamEnabled } from './config.js' +import { getProjectDir } from '../../utils/sessionStorage.js' +import { + getOriginalCwd, + getKairosActive, + getIsRemoteMode, + getSessionId, +} from '../../bootstrap/state.js' +import { createAutoMemCanUseTool } from '../extractMemories/extractMemories.js' +import { buildConsolidationPrompt } from './consolidationPrompt.js' +import { + readLastConsolidatedAt, + listSessionsTouchedSince, + tryAcquireConsolidationLock, + rollbackConsolidationLock, +} from './consolidationLock.js' +import { + registerDreamTask, + addDreamTurn, + completeDreamTask, + failDreamTask, + isDreamTask, +} from '../../tasks/DreamTask/DreamTask.js' +import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js' +import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js' + +// Scan throttle: when time-gate passes but session-gate doesn't, the lock +// mtime doesn't advance, so the time-gate keeps passing every turn. +const SESSION_SCAN_INTERVAL_MS = 10 * 60 * 1000 + +type AutoDreamConfig = { + minHours: number + minSessions: number +} + +const DEFAULTS: AutoDreamConfig = { + minHours: 24, + minSessions: 5, +} + +/** + * Thresholds from tengu_onyx_plover. The enabled gate lives in config.ts + * (isAutoDreamEnabled); this returns only the scheduling knobs. Defensive + * per-field validation since GB cache can return stale wrong-type values. + */ +function getConfig(): AutoDreamConfig { + const raw = + getFeatureValue_CACHED_MAY_BE_STALE | null>( + 'tengu_onyx_plover', + null, + ) + return { + minHours: + typeof raw?.minHours === 'number' && + Number.isFinite(raw.minHours) && + raw.minHours > 0 + ? raw.minHours + : DEFAULTS.minHours, + minSessions: + typeof raw?.minSessions === 'number' && + Number.isFinite(raw.minSessions) && + raw.minSessions > 0 + ? raw.minSessions + : DEFAULTS.minSessions, + } +} + +function isGateOpen(): boolean { + if (getKairosActive()) return false // KAIROS mode uses disk-skill dream + if (getIsRemoteMode()) return false + if (!isAutoMemoryEnabled()) return false + return isAutoDreamEnabled() +} + +// Ant-build-only test override. Bypasses enabled/time/session gates but NOT +// the lock (so repeated turns don't pile up dreams) or the memory-dir +// precondition. Still scans sessions so the prompt's session-hint is populated. +function isForced(): boolean { + return false +} + +type AppendSystemMessageFn = NonNullable + +let runner: + | (( + context: REPLHookContext, + appendSystemMessage?: AppendSystemMessageFn, + ) => Promise) + | null = null + +/** + * Call once at startup (from backgroundHousekeeping alongside + * initExtractMemories), or per-test in beforeEach for a fresh closure. + */ +export function initAutoDream(): void { + let lastSessionScanAt = 0 + + runner = async function runAutoDream(context, appendSystemMessage) { + const cfg = getConfig() + const force = isForced() + if (!force && !isGateOpen()) return + + // --- Time gate --- + let lastAt: number + try { + lastAt = await readLastConsolidatedAt() + } catch (e: unknown) { + logForDebugging( + `[autoDream] readLastConsolidatedAt failed: ${(e as Error).message}`, + ) + return + } + const hoursSince = (Date.now() - lastAt) / 3_600_000 + if (!force && hoursSince < cfg.minHours) return + + // --- Scan throttle --- + const sinceScanMs = Date.now() - lastSessionScanAt + if (!force && sinceScanMs < SESSION_SCAN_INTERVAL_MS) { + logForDebugging( + `[autoDream] scan throttle — time-gate passed but last scan was ${Math.round(sinceScanMs / 1000)}s ago`, + ) + return + } + lastSessionScanAt = Date.now() + + // --- Session gate --- + let sessionIds: string[] + try { + sessionIds = await listSessionsTouchedSince(lastAt) + } catch (e: unknown) { + logForDebugging( + `[autoDream] listSessionsTouchedSince failed: ${(e as Error).message}`, + ) + return + } + // Exclude the current session (its mtime is always recent). + const currentSession = getSessionId() + sessionIds = sessionIds.filter(id => id !== currentSession) + if (!force && sessionIds.length < cfg.minSessions) { + logForDebugging( + `[autoDream] skip — ${sessionIds.length} sessions since last consolidation, need ${cfg.minSessions}`, + ) + return + } + + // --- Lock --- + // Under force, skip acquire entirely — use the existing mtime so + // kill's rollback is a no-op (rewinds to where it already is). + // The lock file stays untouched; next non-force turn sees it as-is. + let priorMtime: number | null + if (force) { + priorMtime = lastAt + } else { + try { + priorMtime = await tryAcquireConsolidationLock() + } catch (e: unknown) { + logForDebugging( + `[autoDream] lock acquire failed: ${(e as Error).message}`, + ) + return + } + if (priorMtime === null) return + } + + logForDebugging( + `[autoDream] firing — ${hoursSince.toFixed(1)}h since last, ${sessionIds.length} sessions to review`, + ) + logEvent('tengu_auto_dream_fired', { + hours_since: Math.round(hoursSince), + sessions_since: sessionIds.length, + }) + + const setAppState = + context.toolUseContext.setAppStateForTasks ?? + context.toolUseContext.setAppState + const abortController = new AbortController() + const taskId = registerDreamTask(setAppState, { + sessionsReviewing: sessionIds.length, + priorMtime, + abortController, + }) + + try { + const memoryRoot = getAutoMemPath() + const transcriptDir = getProjectDir(getOriginalCwd()) + // Tool constraints note goes in `extra`, not the shared prompt body — + // manual /dream runs in the main loop with normal permissions and this + // would be misleading there. + const extra = ` + +**Tool constraints for this run:** Bash is restricted to read-only commands (\`ls\`, \`find\`, \`grep\`, \`cat\`, \`stat\`, \`wc\`, \`head\`, \`tail\`, and similar). Anything that writes, redirects to a file, or modifies state will be denied. Plan your exploration with this in mind — no need to probe. + +Sessions since last consolidation (${sessionIds.length}): +${sessionIds.map(id => `- ${id}`).join('\n')}` + const prompt = buildConsolidationPrompt(memoryRoot, transcriptDir, extra) + + const result = await runForkedAgent({ + promptMessages: [createUserMessage({ content: prompt })], + cacheSafeParams: createCacheSafeParams(context), + canUseTool: createAutoMemCanUseTool(memoryRoot), + querySource: 'auto_dream', + forkLabel: 'auto_dream', + skipTranscript: true, + overrides: { abortController }, + onMessage: makeDreamProgressWatcher(taskId, setAppState), + }) + + completeDreamTask(taskId, setAppState) + // Inline completion summary in the main transcript (same surface as + // extractMemories's "Saved N memories" message). + const dreamState = context.toolUseContext.getAppState().tasks?.[taskId] + if ( + appendSystemMessage && + isDreamTask(dreamState) && + dreamState.filesTouched.length > 0 + ) { + appendSystemMessage({ + ...createMemorySavedMessage(dreamState.filesTouched), + verb: 'Improved', + }) + } + logForDebugging( + `[autoDream] completed — cache: read=${result.totalUsage.cache_read_input_tokens} created=${result.totalUsage.cache_creation_input_tokens}`, + ) + logEvent('tengu_auto_dream_completed', { + cache_read: result.totalUsage.cache_read_input_tokens, + cache_created: result.totalUsage.cache_creation_input_tokens, + output: result.totalUsage.output_tokens, + sessions_reviewed: sessionIds.length, + }) + } catch (e: unknown) { + // If the user killed from the bg-tasks dialog, DreamTask.kill already + // aborted, rolled back the lock, and set status=killed. Don't overwrite + // or double-rollback. + if (abortController.signal.aborted) { + logForDebugging('[autoDream] aborted by user') + return + } + logForDebugging(`[autoDream] fork failed: ${(e as Error).message}`) + logEvent('tengu_auto_dream_failed', {}) + failDreamTask(taskId, setAppState) + // Rewind mtime so time-gate passes again. Scan throttle is the backoff. + await rollbackConsolidationLock(priorMtime) + } + } +} + +/** + * Watch the forked agent's messages. For each assistant turn, extracts any + * text blocks (the agent's reasoning/summary — what the user wants to see) + * and collapses tool_use blocks to a count. Edit/Write file_paths are + * collected for phase-flip + the inline completion message. + */ +function makeDreamProgressWatcher( + taskId: string, + setAppState: import('../../Task.js').SetAppState, +): (msg: Message) => void { + return msg => { + if (msg.type !== 'assistant') return + let text = '' + let toolUseCount = 0 + const touchedPaths: string[] = [] + for (const block of msg.message.content) { + if (block.type === 'text') { + text += block.text + } else if (block.type === 'tool_use') { + toolUseCount++ + if ( + block.name === FILE_EDIT_TOOL_NAME || + block.name === FILE_WRITE_TOOL_NAME + ) { + const input = block.input as { file_path?: unknown } + if (typeof input.file_path === 'string') { + touchedPaths.push(input.file_path) + } + } + } + } + addDreamTurn( + taskId, + { text: text.trim(), toolUseCount }, + touchedPaths, + setAppState, + ) + } +} + +/** + * Entry point from stopHooks. No-op until initAutoDream() has been called. + * Per-turn cost when enabled: one GB cache read + one stat. + */ +export async function executeAutoDream( + context: REPLHookContext, + appendSystemMessage?: AppendSystemMessageFn, +): Promise { + await runner?.(context, appendSystemMessage) +} diff --git a/_all-in-one-upload/cc-haha/src/services/autoDream/config.ts b/_all-in-one-upload/cc-haha/src/services/autoDream/config.ts new file mode 100644 index 0000000..3ed70ef --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/autoDream/config.ts @@ -0,0 +1,21 @@ +// Leaf config module — intentionally minimal imports so UI components +// can read the auto-dream enabled state without dragging in the forked +// agent / task registry / message builder chain that autoDream.ts pulls in. + +import { getInitialSettings } from '../../utils/settings/settings.js' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js' + +/** + * Whether background memory consolidation should run. User setting + * (autoDreamEnabled in settings.json) overrides the GrowthBook default + * when explicitly set; otherwise falls through to tengu_onyx_plover. + */ +export function isAutoDreamEnabled(): boolean { + const setting = getInitialSettings().autoDreamEnabled + if (setting !== undefined) return setting + const gb = getFeatureValue_CACHED_MAY_BE_STALE<{ enabled?: unknown } | null>( + 'tengu_onyx_plover', + null, + ) + return gb?.enabled === true +} diff --git a/_all-in-one-upload/cc-haha/src/services/compact/apiMicrocompact.ts b/_all-in-one-upload/cc-haha/src/services/compact/apiMicrocompact.ts new file mode 100644 index 0000000..4a6b84b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/compact/apiMicrocompact.ts @@ -0,0 +1,153 @@ +import { FILE_EDIT_TOOL_NAME } from 'src/tools/FileEditTool/constants.js' +import { FILE_READ_TOOL_NAME } from 'src/tools/FileReadTool/prompt.js' +import { FILE_WRITE_TOOL_NAME } from 'src/tools/FileWriteTool/prompt.js' +import { GLOB_TOOL_NAME } from 'src/tools/GlobTool/prompt.js' +import { GREP_TOOL_NAME } from 'src/tools/GrepTool/prompt.js' +import { NOTEBOOK_EDIT_TOOL_NAME } from 'src/tools/NotebookEditTool/constants.js' +import { WEB_FETCH_TOOL_NAME } from 'src/tools/WebFetchTool/prompt.js' +import { WEB_SEARCH_TOOL_NAME } from 'src/tools/WebSearchTool/prompt.js' +import { SHELL_TOOL_NAMES } from 'src/utils/shell/shellToolUtils.js' +import { isEnvTruthy } from '../../utils/envUtils.js' + +// docs: https://docs.google.com/document/d/1oCT4evvWTh3P6z-kcfNQwWTCxAhkoFndSaNS9Gm40uw/edit?tab=t.0 + +// Default values for context management strategies +// Match client-side microcompact token values +const DEFAULT_MAX_INPUT_TOKENS = 180_000 // Typical warning threshold +const DEFAULT_TARGET_INPUT_TOKENS = 40_000 // Keep last 40k tokens like client-side + +const TOOLS_CLEARABLE_RESULTS = [ + ...SHELL_TOOL_NAMES, + GLOB_TOOL_NAME, + GREP_TOOL_NAME, + FILE_READ_TOOL_NAME, + WEB_FETCH_TOOL_NAME, + WEB_SEARCH_TOOL_NAME, +] + +const TOOLS_CLEARABLE_USES = [ + FILE_EDIT_TOOL_NAME, + FILE_WRITE_TOOL_NAME, + NOTEBOOK_EDIT_TOOL_NAME, +] + +// Context management strategy types matching API documentation +export type ContextEditStrategy = + | { + type: 'clear_tool_uses_20250919' + trigger?: { + type: 'input_tokens' + value: number + } + keep?: { + type: 'tool_uses' + value: number + } + clear_tool_inputs?: boolean | string[] + exclude_tools?: string[] + clear_at_least?: { + type: 'input_tokens' + value: number + } + } + | { + type: 'clear_thinking_20251015' + keep: { type: 'thinking_turns'; value: number } | 'all' + } + +// Context management configuration wrapper +export type ContextManagementConfig = { + edits: ContextEditStrategy[] +} + +// API-based microcompact implementation that uses native context management +export function getAPIContextManagement(options?: { + hasThinking?: boolean + isRedactThinkingActive?: boolean + clearAllThinking?: boolean +}): ContextManagementConfig | undefined { + const { + hasThinking = false, + isRedactThinkingActive = false, + clearAllThinking = false, + } = options ?? {} + + const strategies: ContextEditStrategy[] = [] + + // Preserve thinking blocks in previous assistant turns. Skip when + // redact-thinking is active — redacted blocks have no model-visible content. + // When clearAllThinking is set (>1h idle = cache miss), keep only the last + // thinking turn — the API schema requires value >= 1, and omitting the edit + // falls back to the model-policy default (often "all"), which wouldn't clear. + if (hasThinking && !isRedactThinkingActive) { + strategies.push({ + type: 'clear_thinking_20251015', + keep: clearAllThinking ? { type: 'thinking_turns', value: 1 } : 'all', + }) + } + + // Tool clearing strategies are ant-only + if (process.env.USER_TYPE !== 'ant') { + return strategies.length > 0 ? { edits: strategies } : undefined + } + + const useClearToolResults = isEnvTruthy( + process.env.USE_API_CLEAR_TOOL_RESULTS, + ) + const useClearToolUses = isEnvTruthy(process.env.USE_API_CLEAR_TOOL_USES) + + // If no tool clearing strategy is enabled, return early + if (!useClearToolResults && !useClearToolUses) { + return strategies.length > 0 ? { edits: strategies } : undefined + } + + if (useClearToolResults) { + const triggerThreshold = process.env.API_MAX_INPUT_TOKENS + ? parseInt(process.env.API_MAX_INPUT_TOKENS) + : DEFAULT_MAX_INPUT_TOKENS + const keepTarget = process.env.API_TARGET_INPUT_TOKENS + ? parseInt(process.env.API_TARGET_INPUT_TOKENS) + : DEFAULT_TARGET_INPUT_TOKENS + + const strategy: ContextEditStrategy = { + type: 'clear_tool_uses_20250919', + trigger: { + type: 'input_tokens', + value: triggerThreshold, + }, + clear_at_least: { + type: 'input_tokens', + value: triggerThreshold - keepTarget, + }, + clear_tool_inputs: TOOLS_CLEARABLE_RESULTS, + } + + strategies.push(strategy) + } + + if (useClearToolUses) { + const triggerThreshold = process.env.API_MAX_INPUT_TOKENS + ? parseInt(process.env.API_MAX_INPUT_TOKENS) + : DEFAULT_MAX_INPUT_TOKENS + const keepTarget = process.env.API_TARGET_INPUT_TOKENS + ? parseInt(process.env.API_TARGET_INPUT_TOKENS) + : DEFAULT_TARGET_INPUT_TOKENS + + const strategy: ContextEditStrategy = { + type: 'clear_tool_uses_20250919', + trigger: { + type: 'input_tokens', + value: triggerThreshold, + }, + clear_at_least: { + type: 'input_tokens', + value: triggerThreshold - keepTarget, + }, + exclude_tools: TOOLS_CLEARABLE_USES, + } + + strategies.push(strategy) + } + + return strategies.length > 0 ? { edits: strategies } : undefined +} diff --git a/_all-in-one-upload/cc-haha/src/services/compact/compact.ts b/_all-in-one-upload/cc-haha/src/services/compact/compact.ts new file mode 100644 index 0000000..f8f86ea --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/compact/compact.ts @@ -0,0 +1,1705 @@ +import { feature } from 'bun:bundle' +import type { UUID } from 'crypto' +import uniqBy from 'lodash-es/uniqBy.js' + +/* eslint-disable @typescript-eslint/no-require-imports */ +const sessionTranscriptModule = feature('KAIROS') + ? (require('../sessionTranscript/sessionTranscript.js') as typeof import('../sessionTranscript/sessionTranscript.js')) + : null + +import { APIUserAbortError } from '@anthropic-ai/sdk' +import { markPostCompaction } from 'src/bootstrap/state.js' +import { getInvokedSkillsForAgent } from '../../bootstrap/state.js' +import type { QuerySource } from '../../constants/querySource.js' +import type { CanUseToolFn } from '../../hooks/useCanUseTool.js' +import type { Tool, ToolUseContext } from '../../Tool.js' +import type { LocalAgentTaskState } from '../../tasks/LocalAgentTask/LocalAgentTask.js' +import { FileReadTool } from '../../tools/FileReadTool/FileReadTool.js' +import { + FILE_READ_TOOL_NAME, + FILE_UNCHANGED_STUB, +} from '../../tools/FileReadTool/prompt.js' +import { ToolSearchTool } from '../../tools/ToolSearchTool/ToolSearchTool.js' +import type { AgentId } from '../../types/ids.js' +import type { + AssistantMessage, + AttachmentMessage, + HookResultMessage, + Message, + PartialCompactDirection, + SystemCompactBoundaryMessage, + SystemMessage, + UserMessage, +} from '../../types/message.js' +import { + createAttachmentMessage, + generateFileAttachment, + getAgentListingDeltaAttachment, + getDeferredToolsDeltaAttachment, + getMcpInstructionsDeltaAttachment, +} from '../../utils/attachments.js' +import { getMemoryPath } from '../../utils/config.js' +import { COMPACT_MAX_OUTPUT_TOKENS } from '../../utils/context.js' +import { + analyzeContext, + tokenStatsToStatsigMetrics, +} from '../../utils/contextAnalysis.js' +import { logForDebugging } from '../../utils/debug.js' +import { hasExactErrorMessage } from '../../utils/errors.js' +import { cacheToObject } from '../../utils/fileStateCache.js' +import { + type CacheSafeParams, + runForkedAgent, +} from '../../utils/forkedAgent.js' +import { + executePostCompactHooks, + executePreCompactHooks, +} from '../../utils/hooks.js' +import { logError } from '../../utils/log.js' +import { MEMORY_TYPE_VALUES } from '../../utils/memory/types.js' +import { + createCompactBoundaryMessage, + createUserMessage, + getAssistantMessageText, + getLastAssistantMessage, + getMessagesAfterCompactBoundary, + isCompactBoundaryMessage, + normalizeMessagesForAPI, +} from '../../utils/messages.js' +import { expandPath } from '../../utils/path.js' +import { getPlan, getPlanFilePath } from '../../utils/plans.js' +import { + isSessionActivityTrackingActive, + sendSessionActivitySignal, +} from '../../utils/sessionActivity.js' +import { processSessionStartHooks } from '../../utils/sessionStart.js' +import { + getTranscriptPath, + reAppendSessionMetadata, +} from '../../utils/sessionStorage.js' +import { sleep } from '../../utils/sleep.js' +import { jsonStringify } from '../../utils/slowOperations.js' +/* eslint-enable @typescript-eslint/no-require-imports */ +import { asSystemPrompt } from '../../utils/systemPromptType.js' +import { getTaskOutputPath } from '../../utils/task/diskOutput.js' +import { + getTokenUsage, + tokenCountFromLastAPIResponse, + tokenCountWithEstimation, +} from '../../utils/tokens.js' +import { + extractDiscoveredToolNames, + isToolSearchEnabled, +} from '../../utils/toolSearch.js' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../analytics/index.js' +import { + getMaxOutputTokensForModel, + queryModelWithStreaming, +} from '../api/claude.js' +import { + getPromptTooLongTokenGap, + PROMPT_TOO_LONG_ERROR_MESSAGE, + startsWithApiErrorPrefix, +} from '../api/errors.js' +import { notifyCompaction } from '../api/promptCacheBreakDetection.js' +import { getRetryDelay } from '../api/withRetry.js' +import { logPermissionContextForAnts } from '../internalLogging.js' +import { + roughTokenCountEstimation, + roughTokenCountEstimationForMessages, +} from '../tokenEstimation.js' +import { groupMessagesByApiRound } from './grouping.js' +import { + getCompactPrompt, + getCompactUserSummaryMessage, + getPartialCompactPrompt, +} from './prompt.js' + +export const POST_COMPACT_MAX_FILES_TO_RESTORE = 5 +export const POST_COMPACT_TOKEN_BUDGET = 50_000 +export const POST_COMPACT_MAX_TOKENS_PER_FILE = 5_000 +// Skills can be large (verify=18.7KB, claude-api=20.1KB). Previously re-injected +// unbounded on every compact → 5-10K tok/compact. Per-skill truncation beats +// dropping — instructions at the top of a skill file are usually the critical +// part. Budget sized to hold ~5 skills at the per-skill cap. +export const POST_COMPACT_MAX_TOKENS_PER_SKILL = 5_000 +export const POST_COMPACT_SKILLS_TOKEN_BUDGET = 25_000 +const MAX_COMPACT_STREAMING_RETRIES = 2 + +/** + * Strip image blocks from user messages before sending for compaction. + * Images are not needed for generating a conversation summary and can + * cause the compaction API call itself to hit the prompt-too-long limit, + * especially in CCD sessions where users frequently attach images. + * Replaces image blocks with a text marker so the summary still notes + * that an image was shared. + * + * Note: Only user messages contain images (either directly attached or within + * tool_result content from tools). Assistant messages contain text, tool_use, + * and thinking blocks but not images. + */ +export function stripImagesFromMessages(messages: Message[]): Message[] { + return messages.map(message => { + if (message.type !== 'user') { + return message + } + + const content = message.message.content + if (!Array.isArray(content)) { + return message + } + + let hasMediaBlock = false + const newContent = content.flatMap(block => { + if (block.type === 'image') { + hasMediaBlock = true + return [{ type: 'text' as const, text: '[image]' }] + } + if (block.type === 'document') { + hasMediaBlock = true + return [{ type: 'text' as const, text: '[document]' }] + } + // Also strip images/documents nested inside tool_result content arrays + if (block.type === 'tool_result' && Array.isArray(block.content)) { + let toolHasMedia = false + const newToolContent = block.content.map(item => { + if (item.type === 'image') { + toolHasMedia = true + return { type: 'text' as const, text: '[image]' } + } + if (item.type === 'document') { + toolHasMedia = true + return { type: 'text' as const, text: '[document]' } + } + return item + }) + if (toolHasMedia) { + hasMediaBlock = true + return [{ ...block, content: newToolContent }] + } + } + return [block] + }) + + if (!hasMediaBlock) { + return message + } + + return { + ...message, + message: { + ...message.message, + content: newContent, + }, + } as typeof message + }) +} + +/** + * Strip attachment types that are re-injected post-compaction anyway. + * skill_discovery/skill_listing are re-surfaced by resetSentSkillNames() + * + the next turn's discovery signal, so feeding them to the summarizer + * wastes tokens and pollutes the summary with stale skill suggestions. + * + * No-op when EXPERIMENTAL_SKILL_SEARCH is off (the attachment types + * don't exist on external builds). + */ +export function stripReinjectedAttachments(messages: Message[]): Message[] { + if (feature('EXPERIMENTAL_SKILL_SEARCH')) { + return messages.filter( + m => + !( + m.type === 'attachment' && + (m.attachment.type === 'skill_discovery' || + m.attachment.type === 'skill_listing') + ), + ) + } + return messages +} + +export const ERROR_MESSAGE_NOT_ENOUGH_MESSAGES = + 'Not enough messages to compact.' +const MAX_PTL_RETRIES = 3 +const PTL_RETRY_MARKER = '[earlier conversation truncated for compaction retry]' + +/** + * Drops the oldest API-round groups from messages until tokenGap is covered. + * Falls back to dropping 20% of groups when the gap is unparseable (some + * Vertex/Bedrock error formats). Returns null when nothing can be dropped + * without leaving an empty summarize set. + * + * This is the last-resort escape hatch for CC-1180 — when the compact request + * itself hits prompt-too-long, the user is otherwise stuck. Dropping the + * oldest context is lossy but unblocks them. The reactive-compact path + * (compactMessages.ts) has the proper retry loop that peels from the tail; + * this helper is the dumb-but-safe fallback for the proactive/manual path + * that wasn't migrated in bfdb472f's unification. + */ +export function truncateHeadForPTLRetry( + messages: Message[], + ptlResponse: AssistantMessage, +): Message[] | null { + // Strip our own synthetic marker from a previous retry before grouping. + // Otherwise it becomes its own group 0 and the 20% fallback stalls + // (drops only the marker, re-adds it, zero progress on retry 2+). + const input = + messages[0]?.type === 'user' && + messages[0].isMeta && + messages[0].message.content === PTL_RETRY_MARKER + ? messages.slice(1) + : messages + + const groups = groupMessagesByApiRound(input) + if (groups.length < 2) return null + + const tokenGap = getPromptTooLongTokenGap(ptlResponse) + let dropCount: number + if (tokenGap !== undefined) { + let acc = 0 + dropCount = 0 + for (const g of groups) { + acc += roughTokenCountEstimationForMessages(g) + dropCount++ + if (acc >= tokenGap) break + } + } else { + dropCount = Math.max(1, Math.floor(groups.length * 0.2)) + } + + // Keep at least one group so there's something to summarize. + dropCount = Math.min(dropCount, groups.length - 1) + if (dropCount < 1) return null + + const sliced = groups.slice(dropCount).flat() + // groupMessagesByApiRound puts the preamble in group 0 and starts every + // subsequent group with an assistant message. Dropping group 0 leaves an + // assistant-first sequence which the API rejects (first message must be + // role=user). Prepend a synthetic user marker — ensureToolResultPairing + // already handles any orphaned tool_results this creates. + if (sliced[0]?.type === 'assistant') { + return [ + createUserMessage({ content: PTL_RETRY_MARKER, isMeta: true }), + ...sliced, + ] + } + return sliced +} + +export const ERROR_MESSAGE_PROMPT_TOO_LONG = + 'Conversation too long. Press esc twice to go up a few messages and try again.' +export const ERROR_MESSAGE_USER_ABORT = 'API Error: Request was aborted.' +export const ERROR_MESSAGE_INCOMPLETE_RESPONSE = + 'Compaction interrupted · This may be due to network issues — please try again.' + +export interface CompactionResult { + boundaryMarker: SystemMessage + summaryMessages: UserMessage[] + attachments: AttachmentMessage[] + hookResults: HookResultMessage[] + messagesToKeep?: Message[] + userDisplayMessage?: string + preCompactTokenCount?: number + postCompactTokenCount?: number + truePostCompactTokenCount?: number + compactionUsage?: ReturnType +} + +/** + * Diagnosis context passed from autoCompactIfNeeded into compactConversation. + * Lets the tengu_compact event disambiguate same-chain loops (H2) from + * cross-agent (H1/H5) and manual-vs-auto (H3) compactions without joins. + */ +export type RecompactionInfo = { + isRecompactionInChain: boolean + turnsSincePreviousCompact: number + previousCompactTurnId?: string + autoCompactThreshold: number + querySource?: QuerySource +} + +/** + * Build the base post-compact messages array from a CompactionResult. + * This ensures consistent ordering across all compaction paths. + * Order: boundaryMarker, summaryMessages, messagesToKeep, attachments, hookResults + */ +export function buildPostCompactMessages(result: CompactionResult): Message[] { + return [ + result.boundaryMarker, + ...result.summaryMessages, + ...(result.messagesToKeep ?? []), + ...result.attachments, + ...result.hookResults, + ] +} + +/** + * Annotate a compact boundary with relink metadata for messagesToKeep. + * Preserved messages keep their original parentUuids on disk (dedup-skipped); + * the loader uses this to patch head→anchor and anchor's-other-children→tail. + * + * `anchorUuid` = what sits immediately before keep[0] in the desired chain: + * - suffix-preserving (reactive/session-memory): last summary message + * - prefix-preserving (partial compact): the boundary itself + */ +export function annotateBoundaryWithPreservedSegment( + boundary: SystemCompactBoundaryMessage, + anchorUuid: UUID, + messagesToKeep: readonly Message[] | undefined, +): SystemCompactBoundaryMessage { + const keep = messagesToKeep ?? [] + if (keep.length === 0) return boundary + return { + ...boundary, + compactMetadata: { + ...boundary.compactMetadata, + preservedSegment: { + headUuid: keep[0]!.uuid, + anchorUuid, + tailUuid: keep.at(-1)!.uuid, + }, + }, + } +} + +/** + * Merges user-supplied custom instructions with hook-provided instructions. + * User instructions come first; hook instructions are appended. + * Empty strings normalize to undefined. + */ +export function mergeHookInstructions( + userInstructions: string | undefined, + hookInstructions: string | undefined, +): string | undefined { + if (!hookInstructions) return userInstructions || undefined + if (!userInstructions) return hookInstructions + return `${userInstructions}\n\n${hookInstructions}` +} + +/** + * Creates a compact version of a conversation by summarizing older messages + * and preserving recent conversation history. + */ +export async function compactConversation( + messages: Message[], + context: ToolUseContext, + cacheSafeParams: CacheSafeParams, + suppressFollowUpQuestions: boolean, + customInstructions?: string, + isAutoCompact: boolean = false, + recompactionInfo?: RecompactionInfo, +): Promise { + try { + if (messages.length === 0) { + throw new Error(ERROR_MESSAGE_NOT_ENOUGH_MESSAGES) + } + + const preCompactTokenCount = tokenCountWithEstimation(messages) + + const appState = context.getAppState() + void logPermissionContextForAnts(appState.toolPermissionContext, 'summary') + + context.onCompactProgress?.({ + type: 'hooks_start', + hookType: 'pre_compact', + }) + + // Execute PreCompact hooks + context.setSDKStatus?.('compacting') + const hookResult = await executePreCompactHooks( + { + trigger: isAutoCompact ? 'auto' : 'manual', + customInstructions: customInstructions ?? null, + }, + context.abortController.signal, + ) + customInstructions = mergeHookInstructions( + customInstructions, + hookResult.newCustomInstructions, + ) + const userDisplayMessage = hookResult.userDisplayMessage + + // Show requesting mode with up arrow and custom message + context.setStreamMode?.('requesting') + context.setResponseLength?.(() => 0) + context.onCompactProgress?.({ type: 'compact_start' }) + + // 3P default: true — forked-agent path reuses main conversation's prompt cache. + // Experiment (Jan 2026) confirmed: false path is 98% cache miss, costs ~0.76% of + // fleet cache_creation (~38B tok/day), concentrated in ephemeral envs (CCR/GHA/SDK) + // with cold GB cache and 3P providers where GB is disabled. GB gate kept as kill-switch. + const promptCacheSharingEnabled = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_compact_cache_prefix', + true, + ) + + const compactPrompt = getCompactPrompt(customInstructions) + const summaryRequest = createUserMessage({ + content: compactPrompt, + }) + + let messagesToSummarize = messages + let retryCacheSafeParams = cacheSafeParams + let summaryResponse: AssistantMessage + let summary: string | null + let ptlAttempts = 0 + for (;;) { + summaryResponse = await streamCompactSummary({ + messages: messagesToSummarize, + summaryRequest, + appState, + context, + preCompactTokenCount, + cacheSafeParams: retryCacheSafeParams, + }) + summary = getAssistantMessageText(summaryResponse) + if (!summary?.startsWith(PROMPT_TOO_LONG_ERROR_MESSAGE)) break + + // CC-1180: compact request itself hit prompt-too-long. Truncate the + // oldest API-round groups and retry rather than leaving the user stuck. + ptlAttempts++ + const truncated = + ptlAttempts <= MAX_PTL_RETRIES + ? truncateHeadForPTLRetry(messagesToSummarize, summaryResponse) + : null + if (!truncated) { + logEvent('tengu_compact_failed', { + reason: + 'prompt_too_long' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + preCompactTokenCount, + promptCacheSharingEnabled, + ptlAttempts, + }) + throw new Error(ERROR_MESSAGE_PROMPT_TOO_LONG) + } + logEvent('tengu_compact_ptl_retry', { + attempt: ptlAttempts, + droppedMessages: messagesToSummarize.length - truncated.length, + remainingMessages: truncated.length, + }) + messagesToSummarize = truncated + // The forked-agent path reads from cacheSafeParams.forkContextMessages, + // not the messages param — thread the truncated set through both paths. + retryCacheSafeParams = { + ...retryCacheSafeParams, + forkContextMessages: truncated, + } + } + + if (!summary) { + logForDebugging( + `Compact failed: no summary text in response. Response: ${jsonStringify(summaryResponse)}`, + { level: 'error' }, + ) + logEvent('tengu_compact_failed', { + reason: + 'no_summary' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + preCompactTokenCount, + promptCacheSharingEnabled, + }) + throw new Error( + `Failed to generate conversation summary - response did not contain valid text content`, + ) + } else if (startsWithApiErrorPrefix(summary)) { + logEvent('tengu_compact_failed', { + reason: + 'api_error' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + preCompactTokenCount, + promptCacheSharingEnabled, + }) + throw new Error(summary) + } + + // Store the current file state before clearing + const preCompactReadFileState = cacheToObject(context.readFileState) + + // Clear the cache + context.readFileState.clear() + context.loadedNestedMemoryPaths?.clear() + + // Intentionally NOT resetting sentSkillNames: re-injecting the full + // skill_listing (~4K tokens) post-compact is pure cache_creation with + // marginal benefit. The model still has SkillTool in its schema and + // invoked_skills attachment (below) preserves used-skill content. Ants + // with EXPERIMENTAL_SKILL_SEARCH already skip re-injection via the + // early-return in getSkillListingAttachments. + + // Run async attachment generation in parallel + const [fileAttachments, asyncAgentAttachments] = await Promise.all([ + createPostCompactFileAttachments( + preCompactReadFileState, + context, + POST_COMPACT_MAX_FILES_TO_RESTORE, + ), + createAsyncAgentAttachmentsIfNeeded(context), + ]) + + const postCompactFileAttachments: AttachmentMessage[] = [ + ...fileAttachments, + ...asyncAgentAttachments, + ] + const planAttachment = createPlanAttachmentIfNeeded(context.agentId) + if (planAttachment) { + postCompactFileAttachments.push(planAttachment) + } + + // Add plan mode instructions if currently in plan mode, so the model + // continues operating in plan mode after compaction + const planModeAttachment = await createPlanModeAttachmentIfNeeded(context) + if (planModeAttachment) { + postCompactFileAttachments.push(planModeAttachment) + } + + // Add skill attachment if skills were invoked in this session + const skillAttachment = createSkillAttachmentIfNeeded(context.agentId) + if (skillAttachment) { + postCompactFileAttachments.push(skillAttachment) + } + + // Compaction ate prior delta attachments. Re-announce from the current + // state so the model has tool/instruction context on the first + // post-compact turn. Empty message history → diff against nothing → + // announces the full set. + for (const att of getDeferredToolsDeltaAttachment( + context.options.tools, + context.options.mainLoopModel, + [], + { callSite: 'compact_full' }, + )) { + postCompactFileAttachments.push(createAttachmentMessage(att)) + } + for (const att of getAgentListingDeltaAttachment(context, [])) { + postCompactFileAttachments.push(createAttachmentMessage(att)) + } + for (const att of getMcpInstructionsDeltaAttachment( + context.options.mcpClients, + context.options.tools, + context.options.mainLoopModel, + [], + )) { + postCompactFileAttachments.push(createAttachmentMessage(att)) + } + + context.onCompactProgress?.({ + type: 'hooks_start', + hookType: 'session_start', + }) + // Execute SessionStart hooks after successful compaction + const hookMessages = await processSessionStartHooks('compact', { + model: context.options.mainLoopModel, + }) + + // Create the compact boundary marker and summary messages before the + // event so we can compute the true resulting-context size. + const boundaryMarker = createCompactBoundaryMessage( + isAutoCompact ? 'auto' : 'manual', + preCompactTokenCount ?? 0, + messages.at(-1)?.uuid, + ) + // Carry loaded-tool state — the summary doesn't preserve tool_reference + // blocks, so the post-compact schema filter needs this to keep sending + // already-loaded deferred tool schemas to the API. + const preCompactDiscovered = extractDiscoveredToolNames(messages) + if (preCompactDiscovered.size > 0) { + boundaryMarker.compactMetadata.preCompactDiscoveredTools = [ + ...preCompactDiscovered, + ].sort() + } + + const transcriptPath = getTranscriptPath() + const summaryMessages: UserMessage[] = [ + createUserMessage({ + content: getCompactUserSummaryMessage( + summary, + suppressFollowUpQuestions, + transcriptPath, + ), + isCompactSummary: true, + isVisibleInTranscriptOnly: true, + }), + ] + + // Previously "postCompactTokenCount" — renamed because this is the + // compact API call's total usage (input_tokens ≈ preCompactTokenCount), + // NOT the size of the resulting context. Kept for event-field continuity. + const compactionCallTotalTokens = tokenCountFromLastAPIResponse([ + summaryResponse, + ]) + + // Message-payload estimate of the resulting context. The next iteration's + // shouldAutoCompact will see this PLUS ~20-40K for system prompt + tools + + // userContext (via API usage.input_tokens). So `willRetriggerNextTurn: true` + // is a strong signal; `false` may still retrigger when this is close to threshold. + const truePostCompactTokenCount = roughTokenCountEstimationForMessages([ + boundaryMarker, + ...summaryMessages, + ...postCompactFileAttachments, + ...hookMessages, + ]) + + // Extract compaction API usage metrics + const compactionUsage = getTokenUsage(summaryResponse) + + const querySourceForEvent = + recompactionInfo?.querySource ?? context.options.querySource ?? 'unknown' + + logEvent('tengu_compact', { + preCompactTokenCount, + // Kept for continuity — semantically the compact API call's total usage + postCompactTokenCount: compactionCallTotalTokens, + truePostCompactTokenCount, + autoCompactThreshold: recompactionInfo?.autoCompactThreshold ?? -1, + willRetriggerNextTurn: + recompactionInfo !== undefined && + truePostCompactTokenCount >= recompactionInfo.autoCompactThreshold, + isAutoCompact, + querySource: + querySourceForEvent as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryChainId: (context.queryTracking?.chainId ?? + '') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: context.queryTracking?.depth ?? -1, + isRecompactionInChain: recompactionInfo?.isRecompactionInChain ?? false, + turnsSincePreviousCompact: + recompactionInfo?.turnsSincePreviousCompact ?? -1, + previousCompactTurnId: (recompactionInfo?.previousCompactTurnId ?? + '') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + compactionInputTokens: compactionUsage?.input_tokens, + compactionOutputTokens: compactionUsage?.output_tokens, + compactionCacheReadTokens: compactionUsage?.cache_read_input_tokens ?? 0, + compactionCacheCreationTokens: + compactionUsage?.cache_creation_input_tokens ?? 0, + compactionTotalTokens: compactionUsage + ? compactionUsage.input_tokens + + (compactionUsage.cache_creation_input_tokens ?? 0) + + (compactionUsage.cache_read_input_tokens ?? 0) + + compactionUsage.output_tokens + : 0, + promptCacheSharingEnabled, + // analyzeContext walks every content block (~11ms on a 4.5K-message + // session) purely for this telemetry breakdown. Computed here, past + // the compaction-API await, so the sync walk doesn't starve the + // render loop before compaction even starts. Same deferral pattern + // as reactiveCompact.ts. + ...(() => { + try { + return tokenStatsToStatsigMetrics(analyzeContext(messages)) + } catch (error) { + logError(error as Error) + return {} + } + })(), + }) + + // Reset cache read baseline so the post-compact drop isn't flagged as a break + if (feature('PROMPT_CACHE_BREAK_DETECTION')) { + notifyCompaction( + context.options.querySource ?? 'compact', + context.agentId, + ) + } + markPostCompaction() + + // Re-append session metadata (custom title, tag) so it stays within + // the 16KB tail window that readLiteMetadata reads for --resume display. + // Without this, enough post-compaction messages push the metadata entry + // out of the window, causing --resume to show the auto-generated title + // instead of the user-set session name. + reAppendSessionMetadata() + + // Write a reduced transcript segment for the pre-compaction messages + // (assistant mode only). Fire-and-forget — errors are logged internally. + if (feature('KAIROS')) { + void sessionTranscriptModule?.writeSessionTranscriptSegment(messages) + } + + context.onCompactProgress?.({ + type: 'hooks_start', + hookType: 'post_compact', + }) + const postCompactHookResult = await executePostCompactHooks( + { + trigger: isAutoCompact ? 'auto' : 'manual', + compactSummary: summary, + }, + context.abortController.signal, + ) + + const combinedUserDisplayMessage = [ + userDisplayMessage, + postCompactHookResult.userDisplayMessage, + ] + .filter(Boolean) + .join('\n') + + return { + boundaryMarker, + summaryMessages, + attachments: postCompactFileAttachments, + hookResults: hookMessages, + userDisplayMessage: combinedUserDisplayMessage || undefined, + preCompactTokenCount, + postCompactTokenCount: compactionCallTotalTokens, + truePostCompactTokenCount, + compactionUsage, + } + } catch (error) { + // Only show the error notification for manual /compact. + // Auto-compact failures are retried on the next turn and the + // notification is confusing when compaction eventually succeeds. + if (!isAutoCompact) { + addErrorNotificationIfNeeded(error, context) + } + throw error + } finally { + context.setStreamMode?.('requesting') + context.setResponseLength?.(() => 0) + context.onCompactProgress?.({ type: 'compact_end' }) + context.setSDKStatus?.(null) + } +} + +/** + * Performs a partial compaction around the selected message index. + * Direction 'from': summarizes messages after the index, keeps earlier ones. + * Prompt cache for kept (earlier) messages is preserved. + * Direction 'up_to': summarizes messages before the index, keeps later ones. + * Prompt cache is invalidated since the summary precedes the kept messages. + */ +export async function partialCompactConversation( + allMessages: Message[], + pivotIndex: number, + context: ToolUseContext, + cacheSafeParams: CacheSafeParams, + userFeedback?: string, + direction: PartialCompactDirection = 'from', +): Promise { + try { + const messagesToSummarize = + direction === 'up_to' + ? allMessages.slice(0, pivotIndex) + : allMessages.slice(pivotIndex) + // 'up_to' must strip old compact boundaries/summaries: for 'up_to', + // summary_B sits BEFORE kept, so a stale boundary_A in kept wins + // findLastCompactBoundaryIndex's backward scan and drops summary_B. + // 'from' keeps them: summary_B sits AFTER kept (backward scan still + // works), and removing an old summary would lose its covered history. + const messagesToKeep = + direction === 'up_to' + ? allMessages + .slice(pivotIndex) + .filter( + m => + m.type !== 'progress' && + !isCompactBoundaryMessage(m) && + !(m.type === 'user' && m.isCompactSummary), + ) + : allMessages.slice(0, pivotIndex).filter(m => m.type !== 'progress') + + if (messagesToSummarize.length === 0) { + throw new Error( + direction === 'up_to' + ? 'Nothing to summarize before the selected message.' + : 'Nothing to summarize after the selected message.', + ) + } + + const preCompactTokenCount = tokenCountWithEstimation(allMessages) + + context.onCompactProgress?.({ + type: 'hooks_start', + hookType: 'pre_compact', + }) + + context.setSDKStatus?.('compacting') + const hookResult = await executePreCompactHooks( + { + trigger: 'manual', + customInstructions: null, + }, + context.abortController.signal, + ) + + // Merge hook instructions with user feedback + let customInstructions: string | undefined + if (hookResult.newCustomInstructions && userFeedback) { + customInstructions = `${hookResult.newCustomInstructions}\n\nUser context: ${userFeedback}` + } else if (hookResult.newCustomInstructions) { + customInstructions = hookResult.newCustomInstructions + } else if (userFeedback) { + customInstructions = `User context: ${userFeedback}` + } + + context.setStreamMode?.('requesting') + context.setResponseLength?.(() => 0) + context.onCompactProgress?.({ type: 'compact_start' }) + + const compactPrompt = getPartialCompactPrompt(customInstructions, direction) + const summaryRequest = createUserMessage({ + content: compactPrompt, + }) + + const failureMetadata = { + preCompactTokenCount, + direction: + direction as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + messagesSummarized: messagesToSummarize.length, + } + + // 'up_to' prefix hits cache directly; 'from' sends all (tail wouldn't cache). + // PTL retry breaks the cache prefix but unblocks the user (CC-1180). + let apiMessages = direction === 'up_to' ? messagesToSummarize : allMessages + let retryCacheSafeParams = + direction === 'up_to' + ? { ...cacheSafeParams, forkContextMessages: messagesToSummarize } + : cacheSafeParams + let summaryResponse: AssistantMessage + let summary: string | null + let ptlAttempts = 0 + for (;;) { + summaryResponse = await streamCompactSummary({ + messages: apiMessages, + summaryRequest, + appState: context.getAppState(), + context, + preCompactTokenCount, + cacheSafeParams: retryCacheSafeParams, + }) + summary = getAssistantMessageText(summaryResponse) + if (!summary?.startsWith(PROMPT_TOO_LONG_ERROR_MESSAGE)) break + + ptlAttempts++ + const truncated = + ptlAttempts <= MAX_PTL_RETRIES + ? truncateHeadForPTLRetry(apiMessages, summaryResponse) + : null + if (!truncated) { + logEvent('tengu_partial_compact_failed', { + reason: + 'prompt_too_long' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...failureMetadata, + ptlAttempts, + }) + throw new Error(ERROR_MESSAGE_PROMPT_TOO_LONG) + } + logEvent('tengu_compact_ptl_retry', { + attempt: ptlAttempts, + droppedMessages: apiMessages.length - truncated.length, + remainingMessages: truncated.length, + path: 'partial' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }) + apiMessages = truncated + retryCacheSafeParams = { + ...retryCacheSafeParams, + forkContextMessages: truncated, + } + } + if (!summary) { + logEvent('tengu_partial_compact_failed', { + reason: + 'no_summary' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...failureMetadata, + }) + throw new Error( + 'Failed to generate conversation summary - response did not contain valid text content', + ) + } else if (startsWithApiErrorPrefix(summary)) { + logEvent('tengu_partial_compact_failed', { + reason: + 'api_error' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...failureMetadata, + }) + throw new Error(summary) + } + + // Store the current file state before clearing + const preCompactReadFileState = cacheToObject(context.readFileState) + context.readFileState.clear() + context.loadedNestedMemoryPaths?.clear() + // Intentionally NOT resetting sentSkillNames — see compactConversation() + // for rationale (~4K tokens saved per compact event). + + const [fileAttachments, asyncAgentAttachments] = await Promise.all([ + createPostCompactFileAttachments( + preCompactReadFileState, + context, + POST_COMPACT_MAX_FILES_TO_RESTORE, + messagesToKeep, + ), + createAsyncAgentAttachmentsIfNeeded(context), + ]) + + const postCompactFileAttachments: AttachmentMessage[] = [ + ...fileAttachments, + ...asyncAgentAttachments, + ] + const planAttachment = createPlanAttachmentIfNeeded(context.agentId) + if (planAttachment) { + postCompactFileAttachments.push(planAttachment) + } + + // Add plan mode instructions if currently in plan mode + const planModeAttachment = await createPlanModeAttachmentIfNeeded(context) + if (planModeAttachment) { + postCompactFileAttachments.push(planModeAttachment) + } + + const skillAttachment = createSkillAttachmentIfNeeded(context.agentId) + if (skillAttachment) { + postCompactFileAttachments.push(skillAttachment) + } + + // Re-announce only what was in the summarized portion — messagesToKeep + // is scanned, so anything already announced there is skipped. + for (const att of getDeferredToolsDeltaAttachment( + context.options.tools, + context.options.mainLoopModel, + messagesToKeep, + { callSite: 'compact_partial' }, + )) { + postCompactFileAttachments.push(createAttachmentMessage(att)) + } + for (const att of getAgentListingDeltaAttachment(context, messagesToKeep)) { + postCompactFileAttachments.push(createAttachmentMessage(att)) + } + for (const att of getMcpInstructionsDeltaAttachment( + context.options.mcpClients, + context.options.tools, + context.options.mainLoopModel, + messagesToKeep, + )) { + postCompactFileAttachments.push(createAttachmentMessage(att)) + } + + context.onCompactProgress?.({ + type: 'hooks_start', + hookType: 'session_start', + }) + const hookMessages = await processSessionStartHooks('compact', { + model: context.options.mainLoopModel, + }) + + const postCompactTokenCount = tokenCountFromLastAPIResponse([ + summaryResponse, + ]) + const compactionUsage = getTokenUsage(summaryResponse) + + logEvent('tengu_partial_compact', { + preCompactTokenCount, + postCompactTokenCount, + messagesKept: messagesToKeep.length, + messagesSummarized: messagesToSummarize.length, + direction: + direction as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + hasUserFeedback: !!userFeedback, + trigger: + 'message_selector' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + compactionInputTokens: compactionUsage?.input_tokens, + compactionOutputTokens: compactionUsage?.output_tokens, + compactionCacheReadTokens: compactionUsage?.cache_read_input_tokens ?? 0, + compactionCacheCreationTokens: + compactionUsage?.cache_creation_input_tokens ?? 0, + }) + + // Progress messages aren't loggable, so forkSessionImpl would null out + // a logicalParentUuid pointing at one. Both directions skip them. + const lastPreCompactUuid = + direction === 'up_to' + ? allMessages.slice(0, pivotIndex).findLast(m => m.type !== 'progress') + ?.uuid + : messagesToKeep.at(-1)?.uuid + const boundaryMarker = createCompactBoundaryMessage( + 'manual', + preCompactTokenCount ?? 0, + lastPreCompactUuid, + userFeedback, + messagesToSummarize.length, + ) + // allMessages not just messagesToSummarize — set union is idempotent, + // simpler than tracking which half each tool lived in. + const preCompactDiscovered = extractDiscoveredToolNames(allMessages) + if (preCompactDiscovered.size > 0) { + boundaryMarker.compactMetadata.preCompactDiscoveredTools = [ + ...preCompactDiscovered, + ].sort() + } + + const transcriptPath = getTranscriptPath() + const summaryMessages: UserMessage[] = [ + createUserMessage({ + content: getCompactUserSummaryMessage(summary, false, transcriptPath), + isCompactSummary: true, + ...(messagesToKeep.length > 0 + ? { + summarizeMetadata: { + messagesSummarized: messagesToSummarize.length, + userContext: userFeedback, + direction, + }, + } + : { isVisibleInTranscriptOnly: true as const }), + }), + ] + + if (feature('PROMPT_CACHE_BREAK_DETECTION')) { + notifyCompaction( + context.options.querySource ?? 'compact', + context.agentId, + ) + } + markPostCompaction() + + // Re-append session metadata (custom title, tag) so it stays within + // the 16KB tail window that readLiteMetadata reads for --resume display. + reAppendSessionMetadata() + + if (feature('KAIROS')) { + void sessionTranscriptModule?.writeSessionTranscriptSegment( + messagesToSummarize, + ) + } + + context.onCompactProgress?.({ + type: 'hooks_start', + hookType: 'post_compact', + }) + const postCompactHookResult = await executePostCompactHooks( + { + trigger: 'manual', + compactSummary: summary, + }, + context.abortController.signal, + ) + + // 'from': prefix-preserving → boundary; 'up_to': suffix → last summary + const anchorUuid = + direction === 'up_to' + ? (summaryMessages.at(-1)?.uuid ?? boundaryMarker.uuid) + : boundaryMarker.uuid + return { + boundaryMarker: annotateBoundaryWithPreservedSegment( + boundaryMarker, + anchorUuid, + messagesToKeep, + ), + summaryMessages, + messagesToKeep, + attachments: postCompactFileAttachments, + hookResults: hookMessages, + userDisplayMessage: postCompactHookResult.userDisplayMessage, + preCompactTokenCount, + postCompactTokenCount, + compactionUsage, + } + } catch (error) { + addErrorNotificationIfNeeded(error, context) + throw error + } finally { + context.setStreamMode?.('requesting') + context.setResponseLength?.(() => 0) + context.onCompactProgress?.({ type: 'compact_end' }) + context.setSDKStatus?.(null) + } +} + +function addErrorNotificationIfNeeded( + error: unknown, + context: Pick, +) { + if ( + !hasExactErrorMessage(error, ERROR_MESSAGE_USER_ABORT) && + !hasExactErrorMessage(error, ERROR_MESSAGE_NOT_ENOUGH_MESSAGES) + ) { + context.addNotification?.({ + key: 'error-compacting-conversation', + text: 'Error compacting conversation', + priority: 'immediate', + color: 'error', + }) + } +} + +export function createCompactCanUseTool(): CanUseToolFn { + return async () => ({ + behavior: 'deny' as const, + message: 'Tool use is not allowed during compaction', + decisionReason: { + type: 'other' as const, + reason: 'compaction agent should only produce text summary', + }, + }) +} + +async function streamCompactSummary({ + messages, + summaryRequest, + appState, + context, + preCompactTokenCount, + cacheSafeParams, +}: { + messages: Message[] + summaryRequest: UserMessage + appState: Awaited> + context: ToolUseContext + preCompactTokenCount: number + cacheSafeParams: CacheSafeParams +}): Promise { + // When prompt cache sharing is enabled, use forked agent to reuse the + // main conversation's cached prefix (system prompt, tools, context messages). + // Falls back to regular streaming path on failure. + // 3P default: true — see comment at the other tengu_compact_cache_prefix read above. + const promptCacheSharingEnabled = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_compact_cache_prefix', + true, + ) + // Send keep-alive signals during compaction to prevent remote session + // WebSocket idle timeouts from dropping bridge connections. Compaction + // API calls can take 5-10+ seconds, during which no other messages + // flow through the transport — without keep-alives, the server may + // close the WebSocket for inactivity. + // Two signals: (1) PUT /worker heartbeat via sessionActivity, and + // (2) re-emit 'compacting' status so the SDK event stream stays active + // and the server doesn't consider the session stale. + const activityInterval = isSessionActivityTrackingActive() + ? setInterval( + (statusSetter?: (status: 'compacting' | null) => void) => { + sendSessionActivitySignal() + statusSetter?.('compacting') + }, + 30_000, + context.setSDKStatus, + ) + : undefined + + try { + if (promptCacheSharingEnabled) { + try { + // DO NOT set maxOutputTokens here. The fork piggybacks on the main thread's + // prompt cache by sending identical cache-key params (system, tools, model, + // messages prefix, thinking config). Setting maxOutputTokens would clamp + // budget_tokens via Math.min(budget, maxOutputTokens-1) in claude.ts, + // creating a thinking config mismatch that invalidates the cache. + // The streaming fallback path (below) can safely set maxOutputTokensOverride + // since it doesn't share cache with the main thread. + const result = await runForkedAgent({ + promptMessages: [summaryRequest], + cacheSafeParams, + canUseTool: createCompactCanUseTool(), + querySource: 'compact', + forkLabel: 'compact', + maxTurns: 1, + skipCacheWrite: true, + // Pass the compact context's abortController so user Esc aborts the + // fork — same signal the streaming fallback uses at + // `signal: context.abortController.signal` below. + overrides: { abortController: context.abortController }, + }) + const assistantMsg = getLastAssistantMessage(result.messages) + const assistantText = assistantMsg + ? getAssistantMessageText(assistantMsg) + : null + // Guard isApiErrorMessage: query() catches API errors (including + // APIUserAbortError on ESC) and yields them as synthetic assistant + // messages. Without this check, an aborted compact "succeeds" with + // "Request was aborted." as the summary — the text doesn't start with + // "API Error" so the caller's startsWithApiErrorPrefix guard misses it. + if (assistantMsg && assistantText && !assistantMsg.isApiErrorMessage) { + // Skip success logging for PTL error text — it's returned so the + // caller's retry loop catches it, but it's not a successful summary. + if (!assistantText.startsWith(PROMPT_TOO_LONG_ERROR_MESSAGE)) { + logEvent('tengu_compact_cache_sharing_success', { + preCompactTokenCount, + outputTokens: result.totalUsage.output_tokens, + cacheReadInputTokens: result.totalUsage.cache_read_input_tokens, + cacheCreationInputTokens: + result.totalUsage.cache_creation_input_tokens, + cacheHitRate: + result.totalUsage.cache_read_input_tokens > 0 + ? result.totalUsage.cache_read_input_tokens / + (result.totalUsage.cache_read_input_tokens + + result.totalUsage.cache_creation_input_tokens + + result.totalUsage.input_tokens) + : 0, + }) + } + return assistantMsg + } + logForDebugging( + `Compact cache sharing: no text in response, falling back. Response: ${jsonStringify(assistantMsg)}`, + { level: 'warn' }, + ) + logEvent('tengu_compact_cache_sharing_fallback', { + reason: + 'no_text_response' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + preCompactTokenCount, + }) + } catch (error) { + logError(error) + logEvent('tengu_compact_cache_sharing_fallback', { + reason: + 'error' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + preCompactTokenCount, + }) + } + } + + // Regular streaming path (fallback when cache sharing fails or is disabled) + const retryEnabled = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_compact_streaming_retry', + false, + ) + const maxAttempts = retryEnabled ? MAX_COMPACT_STREAMING_RETRIES : 1 + + for (let attempt = 1; attempt <= maxAttempts; attempt++) { + // Reset state for retry + let hasStartedStreaming = false + let response: AssistantMessage | undefined + context.setResponseLength?.(() => 0) + + // Check if tool search is enabled using the main loop's tools list. + // context.options.tools includes MCP tools merged via useMergedTools. + const useToolSearch = await isToolSearchEnabled( + context.options.mainLoopModel, + context.options.tools, + async () => appState.toolPermissionContext, + context.options.agentDefinitions.activeAgents, + 'compact', + ) + + // When tool search is enabled, include ToolSearchTool and MCP tools. They get + // defer_loading: true and don't count against context - the API filters them out + // of system_prompt_tools before token counting (see api/token_count_api/counting.py:188 + // and api/public_api/messages/handler.py:324). + // Filter MCP tools from context.options.tools (not appState.mcp.tools) so we + // get the permission-filtered set from useMergedTools — same source used for + // isToolSearchEnabled above and normalizeMessagesForAPI below. + // Deduplicate by name to avoid API errors when MCP tools share names with built-in tools. + const tools: Tool[] = useToolSearch + ? uniqBy( + [ + FileReadTool, + ToolSearchTool, + ...context.options.tools.filter(t => t.isMcp), + ], + 'name', + ) + : [FileReadTool] + + const streamingGen = queryModelWithStreaming({ + messages: normalizeMessagesForAPI( + stripImagesFromMessages( + stripReinjectedAttachments([ + ...getMessagesAfterCompactBoundary(messages), + summaryRequest, + ]), + ), + context.options.tools, + ), + systemPrompt: asSystemPrompt([ + 'You are a helpful AI assistant tasked with summarizing conversations.', + ]), + thinkingConfig: { type: 'disabled' as const }, + tools, + signal: context.abortController.signal, + options: { + async getToolPermissionContext() { + const appState = context.getAppState() + return appState.toolPermissionContext + }, + model: context.options.mainLoopModel, + toolChoice: undefined, + isNonInteractiveSession: context.options.isNonInteractiveSession, + hasAppendSystemPrompt: !!context.options.appendSystemPrompt, + maxOutputTokensOverride: Math.min( + COMPACT_MAX_OUTPUT_TOKENS, + getMaxOutputTokensForModel(context.options.mainLoopModel), + ), + querySource: 'compact', + agents: context.options.agentDefinitions.activeAgents, + mcpTools: [], + effortValue: appState.effortValue, + }, + }) + const streamIter = streamingGen[Symbol.asyncIterator]() + let next = await streamIter.next() + + while (!next.done) { + const event = next.value + + if ( + !hasStartedStreaming && + event.type === 'stream_event' && + event.event.type === 'content_block_start' && + event.event.content_block.type === 'text' + ) { + hasStartedStreaming = true + context.setStreamMode?.('responding') + } + + if ( + event.type === 'stream_event' && + event.event.type === 'content_block_delta' && + event.event.delta.type === 'text_delta' + ) { + const charactersStreamed = event.event.delta.text.length + context.setResponseLength?.(length => length + charactersStreamed) + } + + if (event.type === 'assistant') { + response = event + } + + next = await streamIter.next() + } + + if (response) { + return response + } + + if (attempt < maxAttempts) { + logEvent('tengu_compact_streaming_retry', { + attempt, + preCompactTokenCount, + hasStartedStreaming, + }) + await sleep(getRetryDelay(attempt), context.abortController.signal, { + abortError: () => new APIUserAbortError(), + }) + continue + } + + logForDebugging( + `Compact streaming failed after ${attempt} attempts. hasStartedStreaming=${hasStartedStreaming}`, + { level: 'error' }, + ) + logEvent('tengu_compact_failed', { + reason: + 'no_streaming_response' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + preCompactTokenCount, + hasStartedStreaming, + retryEnabled, + attempts: attempt, + promptCacheSharingEnabled, + }) + throw new Error(ERROR_MESSAGE_INCOMPLETE_RESPONSE) + } + + // This should never be reached due to the throw above, but TypeScript needs it + throw new Error(ERROR_MESSAGE_INCOMPLETE_RESPONSE) + } finally { + clearInterval(activityInterval) + } +} + +/** + * Creates attachment messages for recently accessed files to restore them after compaction. + * This prevents the model from having to re-read files that were recently accessed. + * Re-reads files using FileReadTool to get fresh content with proper validation. + * Files are selected based on recency, but constrained by both file count and token budget limits. + * + * Files already present as Read tool results in preservedMessages are skipped — + * re-injecting identical content the model can already see in the preserved tail + * is pure waste (up to 25K tok/compact). Mirrors the diff-against-preserved + * pattern that getDeferredToolsDeltaAttachment uses at the same call sites. + * + * @param readFileState The current file state tracking recently read files + * @param toolUseContext The tool use context for calling FileReadTool + * @param maxFiles Maximum number of files to restore (default: 5) + * @param preservedMessages Messages kept post-compact; Read results here are skipped + * @returns Array of attachment messages for the most recently accessed files that fit within token budget + */ +export async function createPostCompactFileAttachments( + readFileState: Record, + toolUseContext: ToolUseContext, + maxFiles: number, + preservedMessages: Message[] = [], +): Promise { + const preservedReadPaths = collectReadToolFilePaths(preservedMessages) + const recentFiles = Object.entries(readFileState) + .map(([filename, state]) => ({ filename, ...state })) + .filter( + file => + !shouldExcludeFromPostCompactRestore( + file.filename, + toolUseContext.agentId, + ) && !preservedReadPaths.has(expandPath(file.filename)), + ) + .sort((a, b) => b.timestamp - a.timestamp) + .slice(0, maxFiles) + + const results = await Promise.all( + recentFiles.map(async file => { + const attachment = await generateFileAttachment( + file.filename, + { + ...toolUseContext, + fileReadingLimits: { + maxTokens: POST_COMPACT_MAX_TOKENS_PER_FILE, + }, + }, + 'tengu_post_compact_file_restore_success', + 'tengu_post_compact_file_restore_error', + 'compact', + ) + return attachment ? createAttachmentMessage(attachment) : null + }), + ) + + let usedTokens = 0 + return results.filter((result): result is AttachmentMessage => { + if (result === null) { + return false + } + const attachmentTokens = roughTokenCountEstimation(jsonStringify(result)) + if (usedTokens + attachmentTokens <= POST_COMPACT_TOKEN_BUDGET) { + usedTokens += attachmentTokens + return true + } + return false + }) +} + +/** + * Creates a plan file attachment if a plan file exists for the current session. + * This ensures the plan is preserved after compaction. + */ +export function createPlanAttachmentIfNeeded( + agentId?: AgentId, +): AttachmentMessage | null { + const planContent = getPlan(agentId) + + if (!planContent) { + return null + } + + const planFilePath = getPlanFilePath(agentId) + + return createAttachmentMessage({ + type: 'plan_file_reference', + planFilePath, + planContent, + }) +} + +/** + * Creates an attachment for invoked skills to preserve their content across compaction. + * Only includes skills scoped to the given agent (or main session when agentId is null/undefined). + * This ensures skill guidelines remain available after the conversation is summarized + * without leaking skills from other agent contexts. + */ +export function createSkillAttachmentIfNeeded( + agentId?: string, +): AttachmentMessage | null { + const invokedSkills = getInvokedSkillsForAgent(agentId) + + if (invokedSkills.size === 0) { + return null + } + + // Sorted most-recent-first so budget pressure drops the least-relevant skills. + // Per-skill truncation keeps the head of each file (where setup/usage + // instructions typically live) rather than dropping whole skills. + let usedTokens = 0 + const skills = Array.from(invokedSkills.values()) + .sort((a, b) => b.invokedAt - a.invokedAt) + .map(skill => ({ + name: skill.skillName, + path: skill.skillPath, + content: truncateToTokens( + skill.content, + POST_COMPACT_MAX_TOKENS_PER_SKILL, + ), + })) + .filter(skill => { + const tokens = roughTokenCountEstimation(skill.content) + if (usedTokens + tokens > POST_COMPACT_SKILLS_TOKEN_BUDGET) { + return false + } + usedTokens += tokens + return true + }) + + if (skills.length === 0) { + return null + } + + return createAttachmentMessage({ + type: 'invoked_skills', + skills, + }) +} + +/** + * Creates a plan_mode attachment if the user is currently in plan mode. + * This ensures the model continues to operate in plan mode after compaction + * (otherwise it would lose the plan mode instructions since those are + * normally only injected on tool-use turns via getAttachmentMessages). + */ +export async function createPlanModeAttachmentIfNeeded( + context: ToolUseContext, +): Promise { + const appState = context.getAppState() + if (appState.toolPermissionContext.mode !== 'plan') { + return null + } + + const planFilePath = getPlanFilePath(context.agentId) + const planExists = getPlan(context.agentId) !== null + + return createAttachmentMessage({ + type: 'plan_mode', + reminderType: 'full', + isSubAgent: !!context.agentId, + planFilePath, + planExists, + }) +} + +/** + * Creates attachments for async agents so the model knows about them after + * compaction. Covers both agents still running in the background (so the model + * doesn't spawn a duplicate) and agents that have finished but whose results + * haven't been retrieved yet. + */ +export async function createAsyncAgentAttachmentsIfNeeded( + context: ToolUseContext, +): Promise { + const appState = context.getAppState() + const asyncAgents = Object.values(appState.tasks).filter( + (task): task is LocalAgentTaskState => task.type === 'local_agent', + ) + + return asyncAgents.flatMap(agent => { + if ( + agent.retrieved || + agent.status === 'pending' || + agent.agentId === context.agentId + ) { + return [] + } + return [ + createAttachmentMessage({ + type: 'task_status', + taskId: agent.agentId, + taskType: 'local_agent', + description: agent.description, + status: agent.status, + deltaSummary: + agent.status === 'running' + ? (agent.progress?.summary ?? null) + : (agent.error ?? null), + outputFilePath: getTaskOutputPath(agent.agentId), + }), + ] + }) +} + +/** + * Scan messages for Read tool_use blocks and collect their file_path inputs + * (normalized via expandPath). Used to dedup post-compact file restoration + * against what's already visible in the preserved tail. + * + * Skips Reads whose tool_result is a dedup stub — the stub points at an + * earlier full Read that may have been compacted away, so we want + * createPostCompactFileAttachments to re-inject the real content. + */ +function collectReadToolFilePaths(messages: Message[]): Set { + const stubIds = new Set() + for (const message of messages) { + if (message.type !== 'user' || !Array.isArray(message.message.content)) { + continue + } + for (const block of message.message.content) { + if ( + block.type === 'tool_result' && + typeof block.content === 'string' && + block.content.startsWith(FILE_UNCHANGED_STUB) + ) { + stubIds.add(block.tool_use_id) + } + } + } + + const paths = new Set() + for (const message of messages) { + if ( + message.type !== 'assistant' || + !Array.isArray(message.message.content) + ) { + continue + } + for (const block of message.message.content) { + if ( + block.type !== 'tool_use' || + block.name !== FILE_READ_TOOL_NAME || + stubIds.has(block.id) + ) { + continue + } + const input = block.input + if ( + input && + typeof input === 'object' && + 'file_path' in input && + typeof input.file_path === 'string' + ) { + paths.add(expandPath(input.file_path)) + } + } + } + return paths +} + +const SKILL_TRUNCATION_MARKER = + '\n\n[... skill content truncated for compaction; use Read on the skill path if you need the full text]' + +/** + * Truncate content to roughly maxTokens, keeping the head. roughTokenCountEstimation + * uses ~4 chars/token (its default bytesPerToken), so char budget = maxTokens * 4 + * minus the marker so the result stays within budget. Marker tells the model it + * can Read the full file if needed. + */ +function truncateToTokens(content: string, maxTokens: number): string { + if (roughTokenCountEstimation(content) <= maxTokens) { + return content + } + const charBudget = maxTokens * 4 - SKILL_TRUNCATION_MARKER.length + return content.slice(0, charBudget) + SKILL_TRUNCATION_MARKER +} + +function shouldExcludeFromPostCompactRestore( + filename: string, + agentId?: AgentId, +): boolean { + const normalizedFilename = expandPath(filename) + // Exclude plan files + try { + const planFilePath = expandPath(getPlanFilePath(agentId)) + if (normalizedFilename === planFilePath) { + return true + } + } catch { + // If we can't get plan file path, continue with other checks + } + + // Exclude all types of claude.md files + // TODO: Refactor to use isMemoryFilePath() from claudemd.ts for consistency + // and to also match child directory memory files (.claude/rules/*.md, etc.) + try { + const normalizedMemoryPaths = new Set( + MEMORY_TYPE_VALUES.map(type => expandPath(getMemoryPath(type))), + ) + + if (normalizedMemoryPaths.has(normalizedFilename)) { + return true + } + } catch { + // If we can't get memory paths, continue + } + + return false +} diff --git a/_all-in-one-upload/cc-haha/src/services/remoteManagedSettings/syncCacheState.ts b/_all-in-one-upload/cc-haha/src/services/remoteManagedSettings/syncCacheState.ts new file mode 100644 index 0000000..6186c6b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/remoteManagedSettings/syncCacheState.ts @@ -0,0 +1,96 @@ +/** + * Leaf state module for the remote-managed-settings sync cache. + * + * Split from syncCache.ts to break the settings.ts → syncCache.ts → auth.ts → + * settings.ts cycle. auth.ts sits inside the large settings SCC; importing it + * from settings.ts's own dependency chain pulls hundreds of modules into the + * eagerly-evaluated SCC at startup. + * + * This module imports only leaves (path, envUtils, file, json, types, + * settings/settingsCache — also a leaf, only type-imports validation). settings.ts + * reads the cache from here. syncCache.ts keeps isRemoteManagedSettingsEligible + * (the auth-touching part) and re-exports everything from here for callers that + * don't care about the cycle. + * + * Eligibility is a tri-state here: undefined (not yet determined — return + * null), false (ineligible — return null), true (proceed). managedEnv.ts + * calls isRemoteManagedSettingsEligible() just before the policySettings + * read — after userSettings/flagSettings env vars are applied, so the check + * sees config-provided CLAUDE_CODE_USE_BEDROCK/ANTHROPIC_BASE_URL. That call + * computes once and mirrors the result here via setEligibility(). Every + * subsequent read hits the cached bool instead of re-running the auth chain. + */ + +import { join } from 'path' +import { getClaudeConfigHomeDir } from '../../utils/envUtils.js' +import { readFileSync } from '../../utils/fileRead.js' +import { stripBOM } from '../../utils/jsonRead.js' +import { resetSettingsCache } from '../../utils/settings/settingsCache.js' +import type { SettingsJson } from '../../utils/settings/types.js' +import { jsonParse } from '../../utils/slowOperations.js' + +const SETTINGS_FILENAME = 'remote-settings.json' + +let sessionCache: SettingsJson | null = null +let eligible: boolean | undefined + +export function setSessionCache(value: SettingsJson | null): void { + sessionCache = value +} + +export function resetSyncCache(): void { + sessionCache = null + eligible = undefined +} + +export function setEligibility(v: boolean): boolean { + eligible = v + return v +} + +export function getSettingsPath(): string { + return join(getClaudeConfigHomeDir(), SETTINGS_FILENAME) +} + +// sync IO — settings pipeline is sync. fileRead and jsonRead are leaves; +// file.ts and json.ts both sit in the settings SCC. +function loadSettings(): SettingsJson | null { + try { + const content = readFileSync(getSettingsPath()) + const data: unknown = jsonParse(stripBOM(content)) + if (!data || typeof data !== 'object' || Array.isArray(data)) { + return null + } + return data as SettingsJson + } catch { + return null + } +} + +export function getRemoteManagedSettingsSyncFromCache(): SettingsJson | null { + if (eligible !== true) return null + if (sessionCache) return sessionCache + const cachedSettings = loadSettings() + if (cachedSettings) { + sessionCache = cachedSettings + // Remote settings just became available for the first time. Any merged + // getSettings_DEPRECATED() result cached before this moment is missing + // the policySettings layer (the `eligible !== true` guard above returned + // null). Flush so the next merged read re-merges with this layer visible. + // + // Fires at most once: subsequent calls hit `if (sessionCache)` above. + // When called from loadSettingsFromDisk() (settings.ts:546), the merged + // cache is still null (setSessionSettingsCache runs at :732 after + // loadSettingsFromDisk returns) — no-op. The async-fetch arm (index.ts + // setSessionCache + notifyChange) already handles its own reset. + // + // gh-23085: isBridgeEnabled() at main.tsx Commander-definition time + // (before preAction → init() → isRemoteManagedSettingsEligible()) reached + // getSettings_DEPRECATED() at auth.ts:115. The try/catch in bridgeEnabled + // swallowed the later getGlobalConfig() throw, but the merged settings + // cache was already poisoned. See managedSettingsHeadless.int.test.ts. + resetSettingsCache() + return cachedSettings + } + return null +} diff --git a/_all-in-one-upload/cc-haha/src/services/teamMemorySync/types.ts b/_all-in-one-upload/cc-haha/src/services/teamMemorySync/types.ts new file mode 100644 index 0000000..452cd1e --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/teamMemorySync/types.ts @@ -0,0 +1,156 @@ +/** + * Team Memory Sync Types + * + * Zod schemas and types for the repo-scoped team memory sync API. + * Based on the backend API contract from anthropic/anthropic#250711. + */ + +import { z } from 'zod/v4' +import { lazySchema } from '../../utils/lazySchema.js' + +/** + * Content portion of team memory data - flat key-value storage. + * Keys are file paths relative to the team memory directory (e.g. "MEMORY.md", "patterns.md"). + * Values are UTF-8 string content (typically Markdown). + */ +export const TeamMemoryContentSchema = lazySchema(() => + z.object({ + entries: z.record(z.string(), z.string()), + // Per-key SHA-256 of entry content (`sha256:`). Added in + // anthropic/anthropic#283027. Optional for forward-compat with older + // server deployments; empty map when entries is empty. + entryChecksums: z.record(z.string(), z.string()).optional(), + }), +) + +/** + * Full response from GET /api/claude_code/team_memory + */ +export const TeamMemoryDataSchema = lazySchema(() => + z.object({ + organizationId: z.string(), + repo: z.string(), + version: z.number(), + lastModified: z.string(), // ISO 8601 timestamp + checksum: z.string(), // SHA256 with 'sha256:' prefix + content: TeamMemoryContentSchema(), + }), +) + +/** + * Structured 413 error body from the server (anthropic/anthropic#293258). + * The server's RequestTooLargeException serializes error_code and the + * extra_details dict flattened into error.details. We only model the + * too-many-entries case; entry-too-large is handled via MAX_FILE_SIZE_BYTES + * pre-check on the client side and would need a separate schema. + */ +export const TeamMemoryTooManyEntriesSchema = lazySchema(() => + z.object({ + error: z.object({ + details: z.object({ + error_code: z.literal('team_memory_too_many_entries'), + max_entries: z.number().int().positive(), + received_entries: z.number().int().positive(), + }), + }), + }), +) + +export type TeamMemoryData = z.infer> + +/** + * A file skipped during push because it contains a detected secret. + * The path is relative to the team memory directory. Only the matched + * gitleaks rule ID is recorded — never the secret value itself. + */ +export type SkippedSecretFile = { + path: string + /** Gitleaks rule ID (e.g., "github-pat", "aws-access-token") */ + ruleId: string + /** Human-readable label derived from rule ID */ + label: string +} + +/** + * Result from fetching team memory + */ +export type TeamMemorySyncFetchResult = { + success: boolean + data?: TeamMemoryData + isEmpty?: boolean // true if 404 (no data exists) + notModified?: boolean // true if 304 (ETag matched, no changes) + checksum?: string // ETag from response header + error?: string + skipRetry?: boolean + errorType?: 'auth' | 'timeout' | 'network' | 'parse' | 'unknown' + httpStatus?: number +} + +/** + * Lightweight metadata-only probe result (GET ?view=hashes). + * Contains per-key checksums without entry bodies. Used to refresh + * serverChecksums cheaply during 412 conflict resolution. + */ +export type TeamMemoryHashesResult = { + success: boolean + version?: number + checksum?: string + entryChecksums?: Record + error?: string + errorType?: 'auth' | 'timeout' | 'network' | 'parse' | 'unknown' + httpStatus?: number +} + +/** + * Result from uploading team memory with conflict info + */ +export type TeamMemorySyncPushResult = { + success: boolean + filesUploaded: number + checksum?: string + conflict?: boolean // true if 412 Precondition Failed + error?: string + /** Files skipped because they contain detected secrets (PSR M22174). */ + skippedSecrets?: SkippedSecretFile[] + errorType?: + | 'auth' + | 'timeout' + | 'network' + | 'conflict' + | 'unknown' + | 'no_oauth' + | 'no_repo' + httpStatus?: number +} + +/** + * Result from uploading team memory + */ +export type TeamMemorySyncUploadResult = { + success: boolean + checksum?: string + lastModified?: string + conflict?: boolean // true if 412 Precondition Failed + error?: string + errorType?: 'auth' | 'timeout' | 'network' | 'unknown' + httpStatus?: number + /** + * Structured error_code from a parsed 413 body (anthropic/anthropic#293258). + * Currently only 'team_memory_too_many_entries' is modelled; if the server + * adds more (entry_too_large, total_bytes_exceeded) they'd extend this + * union. Passed straight through to the tengu_team_mem_sync_push event + * as a Datadog-filterable facet. + */ + serverErrorCode?: 'team_memory_too_many_entries' + /** + * Server-enforced max_entries, populated when serverErrorCode is + * team_memory_too_many_entries. Lets the caller cache the effective + * (possibly per-org) limit for subsequent pushes. + */ + serverMaxEntries?: number + /** + * How many entries the rejected push would have produced after merge. + * Populated alongside serverMaxEntries. + */ + serverReceivedEntries?: number +} diff --git a/_all-in-one-upload/cc-haha/src/services/tools/toolExecution.ts b/_all-in-one-upload/cc-haha/src/services/tools/toolExecution.ts new file mode 100644 index 0000000..40ef38e --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/services/tools/toolExecution.ts @@ -0,0 +1,1745 @@ +import { feature } from 'bun:bundle' +import type { + ContentBlockParam, + ToolResultBlockParam, + ToolUseBlock, +} from '@anthropic-ai/sdk/resources/index.mjs' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from 'src/services/analytics/index.js' +import { + extractMcpToolDetails, + extractSkillName, + extractToolInputForTelemetry, + getFileExtensionForAnalytics, + getFileExtensionsFromBashCommand, + isToolDetailsLoggingEnabled, + mcpToolDetailsForAnalytics, + sanitizeToolNameForAnalytics, +} from 'src/services/analytics/metadata.js' +import { + addToToolDuration, + getCodeEditToolDecisionCounter, + getStatsStore, +} from '../../bootstrap/state.js' +import { + buildCodeEditToolAttributes, + isCodeEditingTool, +} from '../../hooks/toolPermission/permissionLogging.js' +import type { CanUseToolFn } from '../../hooks/useCanUseTool.js' +import { + findToolByName, + type Tool, + type ToolProgress, + type ToolProgressData, + type ToolUseContext, +} from '../../Tool.js' +import type { BashToolInput } from '../../tools/BashTool/BashTool.js' +import { startSpeculativeClassifierCheck } from '../../tools/BashTool/bashPermissions.js' +import { BASH_TOOL_NAME } from '../../tools/BashTool/toolName.js' +import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js' +import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js' +import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js' +import { NOTEBOOK_EDIT_TOOL_NAME } from '../../tools/NotebookEditTool/constants.js' +import { POWERSHELL_TOOL_NAME } from '../../tools/PowerShellTool/toolName.js' +import { parseGitCommitId } from '../../tools/shared/gitOperationTracking.js' +import { + isDeferredTool, + TOOL_SEARCH_TOOL_NAME, +} from '../../tools/ToolSearchTool/prompt.js' +import { getAllBaseTools } from '../../tools.js' +import type { HookProgress } from '../../types/hooks.js' +import type { + AssistantMessage, + AttachmentMessage, + Message, + ProgressMessage, + StopHookInfo, +} from '../../types/message.js' +import { count } from '../../utils/array.js' +import { createAttachmentMessage } from '../../utils/attachments.js' +import { logForDebugging } from '../../utils/debug.js' +import { + AbortError, + errorMessage, + getErrnoCode, + ShellError, + TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, +} from '../../utils/errors.js' +import { executePermissionDeniedHooks } from '../../utils/hooks.js' +import { logError } from '../../utils/log.js' +import { + CANCEL_MESSAGE, + createProgressMessage, + createStopHookSummaryMessage, + createToolResultStopMessage, + createUserMessage, + withMemoryCorrectionHint, +} from '../../utils/messages.js' +import type { + PermissionDecisionReason, + PermissionResult, +} from '../../utils/permissions/PermissionResult.js' +import { + startSessionActivity, + stopSessionActivity, +} from '../../utils/sessionActivity.js' +import { jsonStringify } from '../../utils/slowOperations.js' +import { Stream } from '../../utils/stream.js' +import { logOTelEvent } from '../../utils/telemetry/events.js' +import { + addToolContentEvent, + endToolBlockedOnUserSpan, + endToolExecutionSpan, + endToolSpan, + isBetaTracingEnabled, + startToolBlockedOnUserSpan, + startToolExecutionSpan, + startToolSpan, +} from '../../utils/telemetry/sessionTracing.js' +import { + formatError, + formatZodValidationError, +} from '../../utils/toolErrors.js' +import { + processPreMappedToolResultBlock, + processToolResultBlock, +} from '../../utils/toolResultStorage.js' +import { + extractDiscoveredToolNames, + isToolSearchEnabledOptimistic, + isToolSearchToolAvailable, +} from '../../utils/toolSearch.js' +import { + McpAuthError, + McpToolCallError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, +} from '../mcp/client.js' +import { mcpInfoFromString } from '../mcp/mcpStringUtils.js' +import { normalizeNameForMCP } from '../mcp/normalization.js' +import type { MCPServerConnection } from '../mcp/types.js' +import { + getLoggingSafeMcpBaseUrl, + getMcpServerScopeFromToolName, + isMcpTool, +} from '../mcp/utils.js' +import { + resolveHookPermissionDecision, + runPostToolUseFailureHooks, + runPostToolUseHooks, + runPreToolUseHooks, +} from './toolHooks.js' + +/** Minimum total hook duration (ms) to show inline timing summary */ +export const HOOK_TIMING_DISPLAY_THRESHOLD_MS = 500 +/** Log a debug warning when hooks/permission-decision block for this long. Matches + * BashTool's PROGRESS_THRESHOLD_MS — the collapsed view feels stuck past this. */ +const SLOW_PHASE_LOG_THRESHOLD_MS = 2000 + +/** + * Classify a tool execution error into a telemetry-safe string. + * + * In minified/external builds, `error.constructor.name` is mangled into + * short identifiers like "nJT" or "Chq" — useless for diagnostics. + * This function extracts structured, telemetry-safe information instead: + * - TelemetrySafeError: use its telemetryMessage (already vetted) + * - Node.js fs errors: log the error code (ENOENT, EACCES, etc.) + * - Known error types: use their unminified name + * - Fallback: "Error" (better than a mangled 3-char identifier) + */ +export function classifyToolError(error: unknown): string { + if ( + error instanceof TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS + ) { + return error.telemetryMessage.slice(0, 200) + } + if (error instanceof Error) { + // Node.js filesystem errors have a `code` property (ENOENT, EACCES, etc.) + // These are safe to log and much more useful than the constructor name. + const errnoCode = getErrnoCode(error) + if (typeof errnoCode === 'string') { + return `Error:${errnoCode}` + } + // ShellError, ImageSizeError, etc. have stable `.name` properties + // that survive minification (they're set in the constructor). + if (error.name && error.name !== 'Error' && error.name.length > 3) { + return error.name.slice(0, 60) + } + return 'Error' + } + return 'UnknownError' +} + +/** + * Map a rule's origin to the documented OTel `source` vocabulary, matching + * the interactive path's semantics (permissionLogging.ts:81): session-scoped + * grants are temporary, on-disk grants are permanent, and user-authored + * denies are user_reject regardless of persistence. Everything the user + * didn't write (cliArg, policySettings, projectSettings, flagSettings) is + * config. + */ +function ruleSourceToOTelSource( + ruleSource: string, + behavior: 'allow' | 'deny', +): string { + switch (ruleSource) { + case 'session': + return behavior === 'allow' ? 'user_temporary' : 'user_reject' + case 'localSettings': + case 'userSettings': + return behavior === 'allow' ? 'user_permanent' : 'user_reject' + default: + return 'config' + } +} + +/** + * Map a PermissionDecisionReason to the OTel `source` label for the + * non-interactive tool_decision path, staying within the documented + * vocabulary (config, hook, user_permanent, user_temporary, user_reject). + * + * For permissionPromptTool, the SDK host may set decisionClassification on + * the PermissionResult to tell us exactly what happened (once vs always vs + * cache hit — the host knows, we can't tell from {behavior:'allow'} alone). + * Without it, we fall back conservatively: allow → user_temporary, + * deny → user_reject. + */ +function decisionReasonToOTelSource( + reason: PermissionDecisionReason | undefined, + behavior: 'allow' | 'deny', +): string { + if (!reason) { + return 'config' + } + switch (reason.type) { + case 'permissionPromptTool': { + // toolResult is typed `unknown` on PermissionDecisionReason but carries + // the parsed Output from PermissionPromptToolResultSchema. Narrow at + // runtime rather than widen the cross-file type. + const toolResult = reason.toolResult as + | { decisionClassification?: string } + | undefined + const classified = toolResult?.decisionClassification + if ( + classified === 'user_temporary' || + classified === 'user_permanent' || + classified === 'user_reject' + ) { + return classified + } + return behavior === 'allow' ? 'user_temporary' : 'user_reject' + } + case 'rule': + return ruleSourceToOTelSource(reason.rule.source, behavior) + case 'hook': + return 'hook' + case 'mode': + case 'classifier': + case 'subcommandResults': + case 'asyncAgent': + case 'sandboxOverride': + case 'workingDir': + case 'safetyCheck': + case 'other': + return 'config' + default: { + const _exhaustive: never = reason + return 'config' + } + } +} + +function getNextImagePasteId(messages: Message[]): number { + let maxId = 0 + for (const message of messages) { + if (message.type === 'user' && message.imagePasteIds) { + for (const id of message.imagePasteIds) { + if (id > maxId) maxId = id + } + } + } + return maxId + 1 +} + +export type MessageUpdateLazy = { + message: M + contextModifier?: { + toolUseID: string + modifyContext: (context: ToolUseContext) => ToolUseContext + } +} + +export type McpServerType = + | 'stdio' + | 'sse' + | 'http' + | 'ws' + | 'sdk' + | 'sse-ide' + | 'ws-ide' + | 'claudeai-proxy' + | undefined + +function findMcpServerConnection( + toolName: string, + mcpClients: MCPServerConnection[], +): MCPServerConnection | undefined { + if (!toolName.startsWith('mcp__')) { + return undefined + } + + const mcpInfo = mcpInfoFromString(toolName) + if (!mcpInfo) { + return undefined + } + + // mcpInfo.serverName is normalized (e.g., "claude_ai_Slack"), but client.name + // is the original name (e.g., "claude.ai Slack"). Normalize both for comparison. + return mcpClients.find( + client => normalizeNameForMCP(client.name) === mcpInfo.serverName, + ) +} + +/** + * Extracts the MCP server transport type from a tool name. + * Returns the server type (stdio, sse, http, ws, sdk, etc.) for MCP tools, + * or undefined for built-in tools. + */ +function getMcpServerType( + toolName: string, + mcpClients: MCPServerConnection[], +): McpServerType { + const serverConnection = findMcpServerConnection(toolName, mcpClients) + + if (serverConnection?.type === 'connected') { + // Handle stdio configs where type field is optional (defaults to 'stdio') + return serverConnection.config.type ?? 'stdio' + } + + return undefined +} + +/** + * Extracts the MCP server base URL for a tool by looking up its server connection. + * Returns undefined for stdio servers, built-in tools, or if the server is not connected. + */ +function getMcpServerBaseUrlFromToolName( + toolName: string, + mcpClients: MCPServerConnection[], +): string | undefined { + const serverConnection = findMcpServerConnection(toolName, mcpClients) + if (serverConnection?.type !== 'connected') { + return undefined + } + return getLoggingSafeMcpBaseUrl(serverConnection.config) +} + +export async function* runToolUse( + toolUse: ToolUseBlock, + assistantMessage: AssistantMessage, + canUseTool: CanUseToolFn, + toolUseContext: ToolUseContext, +): AsyncGenerator { + const toolName = toolUse.name + // First try to find in the available tools (what the model sees) + let tool = findToolByName(toolUseContext.options.tools, toolName) + + // If not found, check if it's a deprecated tool being called by alias + // (e.g., old transcripts calling "KillShell" which is now an alias for "TaskStop") + // Only fall back for tools where the name matches an alias, not the primary name + if (!tool) { + const fallbackTool = findToolByName(getAllBaseTools(), toolName) + // Only use fallback if the tool was found via alias (deprecated name) + if (fallbackTool && fallbackTool.aliases?.includes(toolName)) { + tool = fallbackTool + } + } + const messageId = assistantMessage.message.id + const requestId = assistantMessage.requestId + const mcpServerType = getMcpServerType( + toolName, + toolUseContext.options.mcpClients, + ) + const mcpServerBaseUrl = getMcpServerBaseUrlFromToolName( + toolName, + toolUseContext.options.mcpClients, + ) + + // Check if the tool exists + if (!tool) { + const sanitizedToolName = sanitizeToolNameForAnalytics(toolName) + logForDebugging(`Unknown tool ${toolName}: ${toolUse.id}`) + logEvent('tengu_tool_use_error', { + error: + `No such tool available: ${sanitizedToolName}` as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizedToolName, + toolUseID: + toolUse.id as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + isMcp: toolName.startsWith('mcp__'), + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics(toolName, mcpServerType, mcpServerBaseUrl), + }) + yield { + message: createUserMessage({ + content: [ + { + type: 'tool_result', + content: `Error: No such tool available: ${toolName}`, + is_error: true, + tool_use_id: toolUse.id, + }, + ], + toolUseResult: `Error: No such tool available: ${toolName}`, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + } + return + } + + const toolInput = toolUse.input as { [key: string]: string } + try { + if (toolUseContext.abortController.signal.aborted) { + logEvent('tengu_tool_use_cancelled', { + toolName: sanitizeToolNameForAnalytics(tool.name), + toolUseID: + toolUse.id as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + isMcp: tool.isMcp ?? false, + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics( + tool.name, + mcpServerType, + mcpServerBaseUrl, + ), + }) + const content = createToolResultStopMessage(toolUse.id) + content.content = withMemoryCorrectionHint(CANCEL_MESSAGE) + yield { + message: createUserMessage({ + content: [content], + toolUseResult: CANCEL_MESSAGE, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + } + return + } + + for await (const update of streamedCheckPermissionsAndCallTool( + tool, + toolUse.id, + toolInput, + toolUseContext, + canUseTool, + assistantMessage, + messageId, + requestId, + mcpServerType, + mcpServerBaseUrl, + )) { + yield update + } + } catch (error) { + logError(error) + const errorMessage = error instanceof Error ? error.message : String(error) + const toolInfo = tool ? ` (${tool.name})` : '' + const detailedError = `Error calling tool${toolInfo}: ${errorMessage}` + + yield { + message: createUserMessage({ + content: [ + { + type: 'tool_result', + content: `${detailedError}`, + is_error: true, + tool_use_id: toolUse.id, + }, + ], + toolUseResult: detailedError, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + } + } +} + +function streamedCheckPermissionsAndCallTool( + tool: Tool, + toolUseID: string, + input: { [key: string]: boolean | string | number }, + toolUseContext: ToolUseContext, + canUseTool: CanUseToolFn, + assistantMessage: AssistantMessage, + messageId: string, + requestId: string | undefined, + mcpServerType: McpServerType, + mcpServerBaseUrl: ReturnType, +): AsyncIterable { + // This is a bit of a hack to get progress events and final results + // into a single async iterable. + // + // Ideally the progress reporting and tool call reporting would + // be via separate mechanisms. + const stream = new Stream() + checkPermissionsAndCallTool( + tool, + toolUseID, + input, + toolUseContext, + canUseTool, + assistantMessage, + messageId, + requestId, + mcpServerType, + mcpServerBaseUrl, + progress => { + logEvent('tengu_tool_use_progress', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + isMcp: tool.isMcp ?? false, + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics( + tool.name, + mcpServerType, + mcpServerBaseUrl, + ), + }) + stream.enqueue({ + message: createProgressMessage({ + toolUseID: progress.toolUseID, + parentToolUseID: toolUseID, + data: progress.data, + }), + }) + }, + ) + .then(results => { + for (const result of results) { + stream.enqueue(result) + } + }) + .catch(error => { + stream.error(error) + }) + .finally(() => { + stream.done() + }) + return stream +} + +/** + * Appended to Zod errors when a deferred tool wasn't in the discovered-tool + * set — re-runs the claude.ts schema-filter scan dispatch-time to detect the + * mismatch. The raw Zod error ("expected array, got string") doesn't tell the + * model to re-load the tool; this hint does. Null if the schema was sent. + */ +export function buildSchemaNotSentHint( + tool: Tool, + messages: Message[], + tools: readonly { name: string }[], +): string | null { + // Optimistic gating — reconstructing claude.ts's full useToolSearch + // computation is fragile. These two gates prevent pointing at a ToolSearch + // that isn't callable; occasional misfires (Haiku, tst-auto below threshold) + // cost one extra round-trip on an already-failing path. + if (!isToolSearchEnabledOptimistic()) return null + if (!isToolSearchToolAvailable(tools)) return null + if (!isDeferredTool(tool)) return null + const discovered = extractDiscoveredToolNames(messages) + if (discovered.has(tool.name)) return null + return ( + `\n\nThis tool's schema was not sent to the API — it was not in the discovered-tool set derived from message history. ` + + `Without the schema in your prompt, typed parameters (arrays, numbers, booleans) get emitted as strings and the client-side parser rejects them. ` + + `Load the tool first: call ${TOOL_SEARCH_TOOL_NAME} with query "select:${tool.name}", then retry this call.` + ) +} + +async function checkPermissionsAndCallTool( + tool: Tool, + toolUseID: string, + input: { [key: string]: boolean | string | number }, + toolUseContext: ToolUseContext, + canUseTool: CanUseToolFn, + assistantMessage: AssistantMessage, + messageId: string, + requestId: string | undefined, + mcpServerType: McpServerType, + mcpServerBaseUrl: ReturnType, + onToolProgress: ( + progress: ToolProgress | ProgressMessage, + ) => void, +): Promise { + // Validate input types with zod (surprisingly, the model is not great at generating valid input) + const parsedInput = tool.inputSchema.safeParse(input) + if (!parsedInput.success) { + let errorContent = formatZodValidationError(tool.name, parsedInput.error) + + const schemaHint = buildSchemaNotSentHint( + tool, + toolUseContext.messages, + toolUseContext.options.tools, + ) + if (schemaHint) { + logEvent('tengu_deferred_tool_schema_not_sent', { + toolName: sanitizeToolNameForAnalytics(tool.name), + isMcp: tool.isMcp ?? false, + }) + errorContent += schemaHint + } + + logForDebugging( + `${tool.name} tool input error: ${errorContent.slice(0, 200)}`, + ) + logEvent('tengu_tool_use_error', { + error: + 'InputValidationError' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + errorDetails: errorContent.slice( + 0, + 2000, + ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + isMcp: tool.isMcp ?? false, + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics(tool.name, mcpServerType, mcpServerBaseUrl), + }) + return [ + { + message: createUserMessage({ + content: [ + { + type: 'tool_result', + content: `InputValidationError: ${errorContent}`, + is_error: true, + tool_use_id: toolUseID, + }, + ], + toolUseResult: `InputValidationError: ${parsedInput.error.message}`, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + }, + ] + } + + // Validate input values. Each tool has its own validation logic + const isValidCall = await tool.validateInput?.( + parsedInput.data, + toolUseContext, + ) + if (isValidCall?.result === false) { + logForDebugging( + `${tool.name} tool validation error: ${isValidCall.message?.slice(0, 200)}`, + ) + logEvent('tengu_tool_use_error', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + error: + isValidCall.message as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + errorCode: isValidCall.errorCode, + isMcp: tool.isMcp ?? false, + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics(tool.name, mcpServerType, mcpServerBaseUrl), + }) + return [ + { + message: createUserMessage({ + content: [ + { + type: 'tool_result', + content: `${isValidCall.message}`, + is_error: true, + tool_use_id: toolUseID, + }, + ], + toolUseResult: `Error: ${isValidCall.message}`, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + }, + ] + } + // Speculatively start the bash allow classifier check early so it runs in + // parallel with pre-tool hooks, deny/ask classifiers, and permission dialog + // setup. The UI indicator (setClassifierChecking) is NOT set here — it's + // set in interactiveHandler.ts only when the permission check returns `ask` + // with a pendingClassifierCheck. This avoids flashing "classifier running" + // for commands that auto-allow via prefix rules. + if ( + tool.name === BASH_TOOL_NAME && + parsedInput.data && + 'command' in parsedInput.data + ) { + const appState = toolUseContext.getAppState() + startSpeculativeClassifierCheck( + (parsedInput.data as BashToolInput).command, + appState.toolPermissionContext, + toolUseContext.abortController.signal, + toolUseContext.options.isNonInteractiveSession, + ) + } + + const resultingMessages = [] + + // Defense-in-depth: strip _simulatedSedEdit from model-provided Bash input. + // This field is internal-only — it must only be injected by the permission + // system (SedEditPermissionRequest) after user approval. If the model supplies + // it, the schema's strictObject should already reject it, but we strip here + // as a safeguard against future regressions. + let processedInput = parsedInput.data + if ( + tool.name === BASH_TOOL_NAME && + processedInput && + typeof processedInput === 'object' && + '_simulatedSedEdit' in processedInput + ) { + const { _simulatedSedEdit: _, ...rest } = + processedInput as typeof processedInput & { + _simulatedSedEdit: unknown + } + processedInput = rest as typeof processedInput + } + + // Backfill legacy/derived fields on a shallow clone so hooks/canUseTool see + // them without affecting tool.call(). SendMessageTool adds fields; file + // tools overwrite file_path with expandPath — that mutation must not reach + // call() because tool results embed the input path verbatim (e.g. "File + // created successfully at: {path}"), and changing it alters the serialized + // transcript and VCR fixture hashes. If a hook/permission later returns a + // fresh updatedInput, callInput converges on it below — that replacement + // is intentional and should reach call(). + let callInput = processedInput + const backfilledClone = + tool.backfillObservableInput && + typeof processedInput === 'object' && + processedInput !== null + ? ({ ...processedInput } as typeof processedInput) + : null + if (backfilledClone) { + tool.backfillObservableInput!(backfilledClone as Record) + processedInput = backfilledClone + } + + let shouldPreventContinuation = false + let stopReason: string | undefined + let hookPermissionResult: PermissionResult | undefined + const preToolHookInfos: StopHookInfo[] = [] + const preToolHookStart = Date.now() + for await (const result of runPreToolUseHooks( + toolUseContext, + tool, + processedInput, + toolUseID, + assistantMessage.message.id, + requestId, + mcpServerType, + mcpServerBaseUrl, + )) { + switch (result.type) { + case 'message': + if (result.message.message.type === 'progress') { + onToolProgress(result.message.message) + } else { + resultingMessages.push(result.message) + const att = result.message.message.attachment + if ( + att && + 'command' in att && + att.command !== undefined && + 'durationMs' in att && + att.durationMs !== undefined + ) { + preToolHookInfos.push({ + command: att.command, + durationMs: att.durationMs, + }) + } + } + break + case 'hookPermissionResult': + hookPermissionResult = result.hookPermissionResult + break + case 'hookUpdatedInput': + // Hook provided updatedInput without making a permission decision (passthrough) + // Update processedInput so it's used in the normal permission flow + processedInput = result.updatedInput + break + case 'preventContinuation': + shouldPreventContinuation = result.shouldPreventContinuation + break + case 'stopReason': + stopReason = result.stopReason + break + case 'additionalContext': + resultingMessages.push(result.message) + break + case 'stop': + getStatsStore()?.observe( + 'pre_tool_hook_duration_ms', + Date.now() - preToolHookStart, + ) + resultingMessages.push({ + message: createUserMessage({ + content: [createToolResultStopMessage(toolUseID)], + toolUseResult: `Error: ${stopReason}`, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + }) + return resultingMessages + } + } + const preToolHookDurationMs = Date.now() - preToolHookStart + getStatsStore()?.observe('pre_tool_hook_duration_ms', preToolHookDurationMs) + if (preToolHookDurationMs >= SLOW_PHASE_LOG_THRESHOLD_MS) { + logForDebugging( + `Slow PreToolUse hooks: ${preToolHookDurationMs}ms for ${tool.name} (${preToolHookInfos.length} hooks)`, + { level: 'info' }, + ) + } + + // Emit PreToolUse summary immediately so it's visible while the tool executes. + // Use wall-clock time (not sum of individual durations) since hooks run in parallel. + if (process.env.USER_TYPE === 'ant' && preToolHookInfos.length > 0) { + if (preToolHookDurationMs > HOOK_TIMING_DISPLAY_THRESHOLD_MS) { + resultingMessages.push({ + message: createStopHookSummaryMessage( + preToolHookInfos.length, + preToolHookInfos, + [], + false, + undefined, + false, + 'suggestion', + undefined, + 'PreToolUse', + preToolHookDurationMs, + ), + }) + } + } + + const toolAttributes: Record = {} + if (processedInput && typeof processedInput === 'object') { + if (tool.name === FILE_READ_TOOL_NAME && 'file_path' in processedInput) { + toolAttributes.file_path = String(processedInput.file_path) + } else if ( + (tool.name === FILE_EDIT_TOOL_NAME || + tool.name === FILE_WRITE_TOOL_NAME) && + 'file_path' in processedInput + ) { + toolAttributes.file_path = String(processedInput.file_path) + } else if (tool.name === BASH_TOOL_NAME && 'command' in processedInput) { + const bashInput = processedInput as BashToolInput + toolAttributes.full_command = bashInput.command + } + } + + startToolSpan( + tool.name, + toolAttributes, + isBetaTracingEnabled() ? jsonStringify(processedInput) : undefined, + ) + startToolBlockedOnUserSpan() + + // Check whether we have permission to use the tool, + // and ask the user for permission if we don't + const permissionMode = toolUseContext.getAppState().toolPermissionContext.mode + const permissionStart = Date.now() + + const resolved = await resolveHookPermissionDecision( + hookPermissionResult, + tool, + processedInput, + toolUseContext, + canUseTool, + assistantMessage, + toolUseID, + ) + const permissionDecision = resolved.decision + processedInput = resolved.input + const permissionDurationMs = Date.now() - permissionStart + // In auto mode, canUseTool awaits the classifier (side_query) — if that's + // slow the collapsed view shows "Running…" with no (Ns) tick since + // bash_progress hasn't started yet. Auto-only: in default mode this timer + // includes interactive-dialog wait (user think time), which is just noise. + if ( + permissionDurationMs >= SLOW_PHASE_LOG_THRESHOLD_MS && + permissionMode === 'auto' + ) { + logForDebugging( + `Slow permission decision: ${permissionDurationMs}ms for ${tool.name} ` + + `(mode=${permissionMode}, behavior=${permissionDecision.behavior})`, + { level: 'info' }, + ) + } + + // Emit tool_decision OTel event and code-edit counter if the interactive + // permission path didn't already log it (headless mode bypasses permission + // logging, so we need to emit both the generic event and the code-edit + // counter here) + if ( + permissionDecision.behavior !== 'ask' && + !toolUseContext.toolDecisions?.has(toolUseID) + ) { + const decision = + permissionDecision.behavior === 'allow' ? 'accept' : 'reject' + const source = decisionReasonToOTelSource( + permissionDecision.decisionReason, + permissionDecision.behavior, + ) + void logOTelEvent('tool_decision', { + decision, + source, + tool_name: sanitizeToolNameForAnalytics(tool.name), + }) + + // Increment code-edit tool decision counter for headless mode + if (isCodeEditingTool(tool.name)) { + void buildCodeEditToolAttributes( + tool, + processedInput, + decision, + source, + ).then(attributes => getCodeEditToolDecisionCounter()?.add(1, attributes)) + } + } + + // Add message if permission was granted/denied by PermissionRequest hook + if ( + permissionDecision.decisionReason?.type === 'hook' && + permissionDecision.decisionReason.hookName === 'PermissionRequest' && + permissionDecision.behavior !== 'ask' + ) { + resultingMessages.push({ + message: createAttachmentMessage({ + type: 'hook_permission_decision', + decision: permissionDecision.behavior, + toolUseID, + hookEvent: 'PermissionRequest', + }), + }) + } + + if (permissionDecision.behavior !== 'allow') { + logForDebugging(`${tool.name} tool permission denied`) + const decisionInfo = toolUseContext.toolDecisions?.get(toolUseID) + endToolBlockedOnUserSpan('reject', decisionInfo?.source || 'unknown') + endToolSpan() + + logEvent('tengu_tool_use_can_use_tool_rejected', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics(tool.name, mcpServerType, mcpServerBaseUrl), + }) + let errorMessage = permissionDecision.message + // Only use generic "Execution stopped" message if we don't have a detailed hook message + if (shouldPreventContinuation && !errorMessage) { + errorMessage = `Execution stopped by PreToolUse hook${stopReason ? `: ${stopReason}` : ''}` + } + + // Build top-level content: tool_result (text-only for is_error compatibility) + images alongside + const messageContent: ContentBlockParam[] = [ + { + type: 'tool_result', + content: errorMessage, + is_error: true, + tool_use_id: toolUseID, + }, + ] + + // Add image blocks at top level (not inside tool_result, which rejects non-text with is_error) + const rejectContentBlocks = + permissionDecision.behavior === 'ask' + ? permissionDecision.contentBlocks + : undefined + if (rejectContentBlocks?.length) { + messageContent.push(...rejectContentBlocks) + } + + // Generate sequential imagePasteIds so each image renders with a distinct label + let rejectImageIds: number[] | undefined + if (rejectContentBlocks?.length) { + const imageCount = count( + rejectContentBlocks, + (b: ContentBlockParam) => b.type === 'image', + ) + if (imageCount > 0) { + const startId = getNextImagePasteId(toolUseContext.messages) + rejectImageIds = Array.from( + { length: imageCount }, + (_, i) => startId + i, + ) + } + } + + resultingMessages.push({ + message: createUserMessage({ + content: messageContent, + imagePasteIds: rejectImageIds, + toolUseResult: `Error: ${errorMessage}`, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + }) + + // Run PermissionDenied hooks for auto mode classifier denials. + // If a hook returns {retry: true}, tell the model it may retry. + if ( + feature('TRANSCRIPT_CLASSIFIER') && + permissionDecision.decisionReason?.type === 'classifier' && + permissionDecision.decisionReason.classifier === 'auto-mode' + ) { + let hookSaysRetry = false + for await (const result of executePermissionDeniedHooks( + tool.name, + toolUseID, + processedInput, + permissionDecision.decisionReason.reason ?? 'Permission denied', + toolUseContext, + permissionMode, + toolUseContext.abortController.signal, + )) { + if (result.retry) hookSaysRetry = true + } + if (hookSaysRetry) { + resultingMessages.push({ + message: createUserMessage({ + content: + 'The PermissionDenied hook indicated this command is now approved. You may retry it if you would like.', + isMeta: true, + }), + }) + } + } + + return resultingMessages + } + logEvent('tengu_tool_use_can_use_tool_allowed', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics(tool.name, mcpServerType, mcpServerBaseUrl), + }) + + // Use the updated input from permissions if provided + // (Don't overwrite if undefined - processedInput may have been modified by passthrough hooks) + if (permissionDecision.updatedInput !== undefined) { + processedInput = permissionDecision.updatedInput + } + + // Prepare tool parameters for logging in tool_result event. + // Gated by OTEL_LOG_TOOL_DETAILS — tool parameters can contain sensitive + // content (bash commands, MCP server names, etc.) so they're opt-in only. + const telemetryToolInput = extractToolInputForTelemetry(processedInput) + let toolParameters: Record = {} + if (isToolDetailsLoggingEnabled()) { + if (tool.name === BASH_TOOL_NAME && 'command' in processedInput) { + const bashInput = processedInput as BashToolInput + const commandParts = bashInput.command.trim().split(/\s+/) + const bashCommand = commandParts[0] || '' + + toolParameters = { + bash_command: bashCommand, + full_command: bashInput.command, + ...(bashInput.timeout !== undefined && { + timeout: bashInput.timeout, + }), + ...(bashInput.description !== undefined && { + description: bashInput.description, + }), + ...('dangerouslyDisableSandbox' in bashInput && { + dangerouslyDisableSandbox: bashInput.dangerouslyDisableSandbox, + }), + } + } + + const mcpDetails = extractMcpToolDetails(tool.name) + if (mcpDetails) { + toolParameters.mcp_server_name = mcpDetails.serverName + toolParameters.mcp_tool_name = mcpDetails.mcpToolName + } + const skillName = extractSkillName(tool.name, processedInput) + if (skillName) { + toolParameters.skill_name = skillName + } + } + + const decisionInfo = toolUseContext.toolDecisions?.get(toolUseID) + endToolBlockedOnUserSpan( + decisionInfo?.decision || 'unknown', + decisionInfo?.source || 'unknown', + ) + startToolExecutionSpan() + + const startTime = Date.now() + + startSessionActivity('tool_exec') + // If processedInput still points at the backfill clone, no hook/permission + // replaced it — pass the pre-backfill callInput so call() sees the model's + // original field values. Otherwise converge on the hook-supplied input. + // Permission/hook flows may return a fresh object derived from the + // backfilled clone (e.g. via inputSchema.parse). If its file_path matches + // the backfill-expanded value, restore the model's original so the tool + // result string embeds the path the model emitted — keeps transcript/VCR + // hashes stable. Other hook modifications flow through unchanged. + if ( + backfilledClone && + processedInput !== callInput && + typeof processedInput === 'object' && + processedInput !== null && + 'file_path' in processedInput && + 'file_path' in (callInput as Record) && + (processedInput as Record).file_path === + (backfilledClone as Record).file_path + ) { + callInput = { + ...processedInput, + file_path: (callInput as Record).file_path, + } as typeof processedInput + } else if (processedInput !== backfilledClone) { + callInput = processedInput + } + try { + const result = await tool.call( + callInput, + { + ...toolUseContext, + toolUseId: toolUseID, + userModified: permissionDecision.userModified ?? false, + }, + canUseTool, + assistantMessage, + progress => { + onToolProgress({ + toolUseID: progress.toolUseID, + data: progress.data, + }) + }, + ) + const durationMs = Date.now() - startTime + addToToolDuration(durationMs) + + // Log tool content/output as span event if enabled + if (result.data && typeof result.data === 'object') { + const contentAttributes: Record = {} + + // Read tool: capture file_path and content + if (tool.name === FILE_READ_TOOL_NAME && 'content' in result.data) { + if ('file_path' in processedInput) { + contentAttributes.file_path = String(processedInput.file_path) + } + contentAttributes.content = String(result.data.content) + } + + // Edit/Write tools: capture file_path and diff + if ( + (tool.name === FILE_EDIT_TOOL_NAME || + tool.name === FILE_WRITE_TOOL_NAME) && + 'file_path' in processedInput + ) { + contentAttributes.file_path = String(processedInput.file_path) + + // For Edit, capture the actual changes made + if (tool.name === FILE_EDIT_TOOL_NAME && 'diff' in result.data) { + contentAttributes.diff = String(result.data.diff) + } + // For Write, capture the written content + if (tool.name === FILE_WRITE_TOOL_NAME && 'content' in processedInput) { + contentAttributes.content = String(processedInput.content) + } + } + + // Bash tool: capture command + if (tool.name === BASH_TOOL_NAME && 'command' in processedInput) { + const bashInput = processedInput as BashToolInput + contentAttributes.bash_command = bashInput.command + // Also capture output if available + if ('output' in result.data) { + contentAttributes.output = String(result.data.output) + } + } + + if (Object.keys(contentAttributes).length > 0) { + addToolContentEvent('tool.output', contentAttributes) + } + } + + // Capture structured output from tool result if present + if (typeof result === 'object' && 'structured_output' in result) { + // Store the structured output in an attachment message + resultingMessages.push({ + message: createAttachmentMessage({ + type: 'structured_output', + data: result.structured_output, + }), + }) + } + + endToolExecutionSpan({ success: true }) + // Pass tool result for new_context logging + const toolResultStr = + result.data && typeof result.data === 'object' + ? jsonStringify(result.data) + : String(result.data ?? '') + endToolSpan(toolResultStr) + + // Map the tool result to API format once and cache it. This block is reused + // by addToolResult (skipping the remap) and measured here for analytics. + const mappedToolResultBlock = tool.mapToolResultToToolResultBlockParam( + result.data, + toolUseID, + ) + const mappedContent = mappedToolResultBlock.content + const toolResultSizeBytes = !mappedContent + ? 0 + : typeof mappedContent === 'string' + ? mappedContent.length + : jsonStringify(mappedContent).length + + // Extract file extension for file-related tools + let fileExtension: ReturnType + if (processedInput && typeof processedInput === 'object') { + if ( + (tool.name === FILE_READ_TOOL_NAME || + tool.name === FILE_EDIT_TOOL_NAME || + tool.name === FILE_WRITE_TOOL_NAME) && + 'file_path' in processedInput + ) { + fileExtension = getFileExtensionForAnalytics( + String(processedInput.file_path), + ) + } else if ( + tool.name === NOTEBOOK_EDIT_TOOL_NAME && + 'notebook_path' in processedInput + ) { + fileExtension = getFileExtensionForAnalytics( + String(processedInput.notebook_path), + ) + } else if (tool.name === BASH_TOOL_NAME && 'command' in processedInput) { + const bashInput = processedInput as BashToolInput + fileExtension = getFileExtensionsFromBashCommand( + bashInput.command, + bashInput._simulatedSedEdit?.filePath, + ) + } + } + + logEvent('tengu_tool_use_success', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + isMcp: tool.isMcp ?? false, + durationMs, + preToolHookDurationMs, + toolResultSizeBytes, + ...(fileExtension !== undefined && { fileExtension }), + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics(tool.name, mcpServerType, mcpServerBaseUrl), + }) + + // Enrich tool parameters with git commit ID from successful git commit output + if ( + isToolDetailsLoggingEnabled() && + (tool.name === BASH_TOOL_NAME || tool.name === POWERSHELL_TOOL_NAME) && + 'command' in processedInput && + typeof processedInput.command === 'string' && + processedInput.command.match(/\bgit\s+commit\b/) && + result.data && + typeof result.data === 'object' && + 'stdout' in result.data + ) { + const gitCommitId = parseGitCommitId(String(result.data.stdout)) + if (gitCommitId) { + toolParameters.git_commit_id = gitCommitId + } + } + + // Log tool result event for OTLP with tool parameters and decision context + const mcpServerScope = isMcpTool(tool) + ? getMcpServerScopeFromToolName(tool.name) + : null + + void logOTelEvent('tool_result', { + tool_name: sanitizeToolNameForAnalytics(tool.name), + success: 'true', + duration_ms: String(durationMs), + ...(Object.keys(toolParameters).length > 0 && { + tool_parameters: jsonStringify(toolParameters), + }), + ...(telemetryToolInput && { tool_input: telemetryToolInput }), + tool_result_size_bytes: String(toolResultSizeBytes), + ...(decisionInfo && { + decision_source: decisionInfo.source, + decision_type: decisionInfo.decision, + }), + ...(mcpServerScope && { mcp_server_scope: mcpServerScope }), + }) + + // Run PostToolUse hooks + let toolOutput = result.data + const hookResults = [] + const toolContextModifier = result.contextModifier + const mcpMeta = result.mcpMeta + + async function addToolResult( + toolUseResult: unknown, + preMappedBlock?: ToolResultBlockParam, + ) { + // Use the pre-mapped block when available (non-MCP tools where hooks + // don't modify the output), otherwise map from scratch. + const toolResultBlock = preMappedBlock + ? await processPreMappedToolResultBlock( + preMappedBlock, + tool.name, + tool.maxResultSizeChars, + ) + : await processToolResultBlock(tool, toolUseResult, toolUseID) + + // Build content blocks - tool result first, then optional feedback + const contentBlocks: ContentBlockParam[] = [toolResultBlock] + // Add accept feedback if user provided feedback when approving + // (acceptFeedback only exists on PermissionAllowDecision, which is guaranteed here) + if ( + 'acceptFeedback' in permissionDecision && + permissionDecision.acceptFeedback + ) { + contentBlocks.push({ + type: 'text', + text: permissionDecision.acceptFeedback, + }) + } + + // Add content blocks (e.g., pasted images) from the permission decision + const allowContentBlocks = + 'contentBlocks' in permissionDecision + ? permissionDecision.contentBlocks + : undefined + if (allowContentBlocks?.length) { + contentBlocks.push(...allowContentBlocks) + } + + // Generate sequential imagePasteIds so each image renders with a distinct label + let allowImageIds: number[] | undefined + if (allowContentBlocks?.length) { + const imageCount = count( + allowContentBlocks, + (b: ContentBlockParam) => b.type === 'image', + ) + if (imageCount > 0) { + const startId = getNextImagePasteId(toolUseContext.messages) + allowImageIds = Array.from( + { length: imageCount }, + (_, i) => startId + i, + ) + } + } + + resultingMessages.push({ + message: createUserMessage({ + content: contentBlocks, + imagePasteIds: allowImageIds, + toolUseResult: + toolUseContext.agentId && !toolUseContext.preserveToolUseResults + ? undefined + : toolUseResult, + mcpMeta: toolUseContext.agentId ? undefined : mcpMeta, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + contextModifier: toolContextModifier + ? { + toolUseID: toolUseID, + modifyContext: toolContextModifier, + } + : undefined, + }) + } + + // TOOD(hackyon): refactor so we don't have different experiences for MCP tools + if (!isMcpTool(tool)) { + await addToolResult(toolOutput, mappedToolResultBlock) + } + + const postToolHookInfos: StopHookInfo[] = [] + const postToolHookStart = Date.now() + for await (const hookResult of runPostToolUseHooks( + toolUseContext, + tool, + toolUseID, + assistantMessage.message.id, + processedInput, + toolOutput, + requestId, + mcpServerType, + mcpServerBaseUrl, + )) { + if ('updatedMCPToolOutput' in hookResult) { + if (isMcpTool(tool)) { + toolOutput = hookResult.updatedMCPToolOutput + } + } else if (isMcpTool(tool)) { + hookResults.push(hookResult) + if (hookResult.message.type === 'attachment') { + const att = hookResult.message.attachment + if ( + 'command' in att && + att.command !== undefined && + 'durationMs' in att && + att.durationMs !== undefined + ) { + postToolHookInfos.push({ + command: att.command, + durationMs: att.durationMs, + }) + } + } + } else { + resultingMessages.push(hookResult) + if (hookResult.message.type === 'attachment') { + const att = hookResult.message.attachment + if ( + 'command' in att && + att.command !== undefined && + 'durationMs' in att && + att.durationMs !== undefined + ) { + postToolHookInfos.push({ + command: att.command, + durationMs: att.durationMs, + }) + } + } + } + } + const postToolHookDurationMs = Date.now() - postToolHookStart + if (postToolHookDurationMs >= SLOW_PHASE_LOG_THRESHOLD_MS) { + logForDebugging( + `Slow PostToolUse hooks: ${postToolHookDurationMs}ms for ${tool.name} (${postToolHookInfos.length} hooks)`, + { level: 'info' }, + ) + } + + if (isMcpTool(tool)) { + await addToolResult(toolOutput) + } + + // Show PostToolUse hook timing inline below tool result when > 500ms. + // Use wall-clock time (not sum of individual durations) since hooks run in parallel. + if (process.env.USER_TYPE === 'ant' && postToolHookInfos.length > 0) { + if (postToolHookDurationMs > HOOK_TIMING_DISPLAY_THRESHOLD_MS) { + resultingMessages.push({ + message: createStopHookSummaryMessage( + postToolHookInfos.length, + postToolHookInfos, + [], + false, + undefined, + false, + 'suggestion', + undefined, + 'PostToolUse', + postToolHookDurationMs, + ), + }) + } + } + + // If the tool provided new messages, add them to the list to return. + if (result.newMessages && result.newMessages.length > 0) { + for (const message of result.newMessages) { + resultingMessages.push({ message }) + } + } + // If hook indicated to prevent continuation after successful execution, yield a stop reason message + if (shouldPreventContinuation) { + resultingMessages.push({ + message: createAttachmentMessage({ + type: 'hook_stopped_continuation', + message: stopReason || 'Execution stopped by hook', + hookName: `PreToolUse:${tool.name}`, + toolUseID: toolUseID, + hookEvent: 'PreToolUse', + }), + }) + } + + // Yield the remaining hook results after the other messages are sent + for (const hookResult of hookResults) { + resultingMessages.push(hookResult) + } + return resultingMessages + } catch (error) { + const durationMs = Date.now() - startTime + addToToolDuration(durationMs) + + endToolExecutionSpan({ + success: false, + error: errorMessage(error), + }) + endToolSpan() + + // Handle MCP auth errors by updating the client status to 'needs-auth' + // This updates the /mcp display to show the server needs re-authorization + if (error instanceof McpAuthError) { + toolUseContext.setAppState(prevState => { + const serverName = error.serverName + const existingClientIndex = prevState.mcp.clients.findIndex( + c => c.name === serverName, + ) + if (existingClientIndex === -1) { + return prevState + } + const existingClient = prevState.mcp.clients[existingClientIndex] + // Only update if client was connected (don't overwrite other states) + if (!existingClient || existingClient.type !== 'connected') { + return prevState + } + const updatedClients = [...prevState.mcp.clients] + updatedClients[existingClientIndex] = { + name: serverName, + type: 'needs-auth' as const, + config: existingClient.config, + } + return { + ...prevState, + mcp: { + ...prevState.mcp, + clients: updatedClients, + }, + } + }) + } + + if (!(error instanceof AbortError)) { + const errorMsg = errorMessage(error) + logForDebugging( + `${tool.name} tool error (${durationMs}ms): ${errorMsg.slice(0, 200)}`, + ) + if (!(error instanceof ShellError)) { + logError(error) + } + logEvent('tengu_tool_use_error', { + messageID: + messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + toolName: sanitizeToolNameForAnalytics(tool.name), + error: classifyToolError( + error, + ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + isMcp: tool.isMcp ?? false, + + queryChainId: toolUseContext.queryTracking + ?.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: toolUseContext.queryTracking?.depth, + ...(mcpServerType && { + mcpServerType: + mcpServerType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(mcpServerBaseUrl && { + mcpServerBaseUrl: + mcpServerBaseUrl as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(requestId && { + requestId: + requestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...mcpToolDetailsForAnalytics( + tool.name, + mcpServerType, + mcpServerBaseUrl, + ), + }) + // Log tool result error event for OTLP with tool parameters and decision context + const mcpServerScope = isMcpTool(tool) + ? getMcpServerScopeFromToolName(tool.name) + : null + + void logOTelEvent('tool_result', { + tool_name: sanitizeToolNameForAnalytics(tool.name), + use_id: toolUseID, + success: 'false', + duration_ms: String(durationMs), + error: errorMessage(error), + ...(Object.keys(toolParameters).length > 0 && { + tool_parameters: jsonStringify(toolParameters), + }), + ...(telemetryToolInput && { tool_input: telemetryToolInput }), + ...(decisionInfo && { + decision_source: decisionInfo.source, + decision_type: decisionInfo.decision, + }), + ...(mcpServerScope && { mcp_server_scope: mcpServerScope }), + }) + } + const content = formatError(error) + + // Determine if this was a user interrupt + const isInterrupt = error instanceof AbortError + + // Run PostToolUseFailure hooks + const hookMessages: MessageUpdateLazy< + AttachmentMessage | ProgressMessage + >[] = [] + for await (const hookResult of runPostToolUseFailureHooks( + toolUseContext, + tool, + toolUseID, + messageId, + processedInput, + content, + isInterrupt, + requestId, + mcpServerType, + mcpServerBaseUrl, + )) { + hookMessages.push(hookResult) + } + + return [ + { + message: createUserMessage({ + content: [ + { + type: 'tool_result', + content, + is_error: true, + tool_use_id: toolUseID, + }, + ], + toolUseResult: `Error: ${content}`, + mcpMeta: toolUseContext.agentId + ? undefined + : error instanceof + McpToolCallError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS + ? error.mcpMeta + : undefined, + sourceToolAssistantUUID: assistantMessage.uuid, + }), + }, + ...hookMessages, + ] + } finally { + stopSessionActivity('tool_exec') + // Clean up decision info after logging + if (decisionInfo) { + toolUseContext.toolDecisions?.delete(toolUseID) + } + } +} diff --git a/_all-in-one-upload/cc-haha/src/skills/bundled/claude-api/go/claude-api.md b/_all-in-one-upload/cc-haha/src/skills/bundled/claude-api/go/claude-api.md new file mode 100644 index 0000000..7a4d285 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/skills/bundled/claude-api/go/claude-api.md @@ -0,0 +1,2 @@ + +stub diff --git a/_all-in-one-upload/cc-haha/src/skills/bundled/remember.ts b/_all-in-one-upload/cc-haha/src/skills/bundled/remember.ts new file mode 100644 index 0000000..40bd1a9 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/skills/bundled/remember.ts @@ -0,0 +1,82 @@ +import { isAutoMemoryEnabled } from '../../memdir/paths.js' +import { registerBundledSkill } from '../bundledSkills.js' + +export function registerRememberSkill(): void { + if (process.env.USER_TYPE !== 'ant') { + return + } + + const SKILL_PROMPT = `# Memory Review + +## Goal +Review the user's memory landscape and produce a clear report of proposed changes, grouped by action type. Do NOT apply changes — present proposals for user approval. + +## Steps + +### 1. Gather all memory layers +Read CLAUDE.md and CLAUDE.local.md from the project root (if they exist). Your auto-memory content is already in your system prompt — review it there. Note which team memory sections exist, if any. + +**Success criteria**: You have the contents of all memory layers and can compare them. + +### 2. Classify each auto-memory entry +For each substantive entry in auto-memory, determine the best destination: + +| Destination | What belongs there | Examples | +|---|---|---| +| **CLAUDE.md** | Project conventions and instructions for Claude that all contributors should follow | "use bun not npm", "API routes use kebab-case", "test command is bun test", "prefer functional style" | +| **CLAUDE.local.md** | Personal instructions for Claude specific to this user, not applicable to other contributors | "I prefer concise responses", "always explain trade-offs", "don't auto-commit", "run tests before committing" | +| **Team memory** | Org-wide knowledge that applies across repositories (only if team memory is configured) | "deploy PRs go through #deploy-queue", "staging is at staging.internal", "platform team owns infra" | +| **Stay in auto-memory** | Working notes, temporary context, or entries that don't clearly fit elsewhere | Session-specific observations, uncertain patterns | + +**Important distinctions:** +- CLAUDE.md and CLAUDE.local.md contain instructions for Claude, not user preferences for external tools (editor theme, IDE keybindings, etc. don't belong in either) +- Workflow practices (PR conventions, merge strategies, branch naming) are ambiguous — ask the user whether they're personal or team-wide +- When unsure, ask rather than guess + +**Success criteria**: Each entry has a proposed destination or is flagged as ambiguous. + +### 3. Identify cleanup opportunities +Scan across all layers for: +- **Duplicates**: Auto-memory entries already captured in CLAUDE.md or CLAUDE.local.md → propose removing from auto-memory +- **Outdated**: CLAUDE.md or CLAUDE.local.md entries contradicted by newer auto-memory entries → propose updating the older layer +- **Conflicts**: Contradictions between any two layers → propose resolution, noting which is more recent + +**Success criteria**: All cross-layer issues identified. + +### 4. Present the report +Output a structured report grouped by action type: +1. **Promotions** — entries to move, with destination and rationale +2. **Cleanup** — duplicates, outdated entries, conflicts to resolve +3. **Ambiguous** — entries where you need the user's input on destination +4. **No action needed** — brief note on entries that should stay put + +If auto-memory is empty, say so and offer to review CLAUDE.md for cleanup. + +**Success criteria**: User can review and approve/reject each proposal individually. + +## Rules +- Present ALL proposals before making any changes +- Do NOT modify files without explicit user approval +- Do NOT create new files unless the target doesn't exist yet +- Ask about ambiguous entries — don't guess +` + + registerBundledSkill({ + name: 'remember', + description: + 'Review auto-memory entries and propose promotions to CLAUDE.md, CLAUDE.local.md, or shared memory. Also detects outdated, conflicting, and duplicate entries across memory layers.', + whenToUse: + 'Use when the user wants to review, organize, or promote their auto-memory entries. Also useful for cleaning up outdated or conflicting entries across CLAUDE.md, CLAUDE.local.md, and auto-memory.', + userInvocable: true, + isEnabled: () => isAutoMemoryEnabled(), + async getPromptForCommand(args) { + let prompt = SKILL_PROMPT + + if (args) { + prompt += `\n## Additional context from user\n\n${args}` + } + + return [{ type: 'text', text: prompt }] + }, + }) +} diff --git a/_all-in-one-upload/cc-haha/src/tools/AgentTool/UI.tsx b/_all-in-one-upload/cc-haha/src/tools/AgentTool/UI.tsx new file mode 100644 index 0000000..497805a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/AgentTool/UI.tsx @@ -0,0 +1,872 @@ +import { c as _c } from "react/compiler-runtime"; +import type { ToolResultBlockParam, ToolUseBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'; +import * as React from 'react'; +import { ConfigurableShortcutHint } from 'src/components/ConfigurableShortcutHint.js'; +import { CtrlOToExpand, SubAgentProvider } from 'src/components/CtrlOToExpand.js'; +import { Byline } from 'src/components/design-system/Byline.js'; +import { KeyboardShortcutHint } from 'src/components/design-system/KeyboardShortcutHint.js'; +import type { z } from 'zod/v4'; +import { AgentProgressLine } from '../../components/AgentProgressLine.js'; +import { FallbackToolUseErrorMessage } from '../../components/FallbackToolUseErrorMessage.js'; +import { FallbackToolUseRejectedMessage } from '../../components/FallbackToolUseRejectedMessage.js'; +import { Markdown } from '../../components/Markdown.js'; +import { Message as MessageComponent } from '../../components/Message.js'; +import { MessageResponse } from '../../components/MessageResponse.js'; +import { ToolUseLoader } from '../../components/ToolUseLoader.js'; +import { Box, Text } from '../../ink.js'; +import { getDumpPromptsPath } from '../../services/api/dumpPrompts.js'; +import { findToolByName, type Tools } from '../../Tool.js'; +import type { Message, ProgressMessage } from '../../types/message.js'; +import type { AgentToolProgress } from '../../types/tools.js'; +import { count } from '../../utils/array.js'; +import { getSearchOrReadFromContent, getSearchReadSummaryText } from '../../utils/collapseReadSearch.js'; +import { getDisplayPath } from '../../utils/file.js'; +import { formatDuration, formatNumber } from '../../utils/format.js'; +import { buildSubagentLookups, createAssistantMessage, EMPTY_LOOKUPS } from '../../utils/messages.js'; +import type { ModelAlias } from '../../utils/model/aliases.js'; +import { getMainLoopModel, parseUserSpecifiedModel, renderModelName } from '../../utils/model/model.js'; +import type { Theme, ThemeName } from '../../utils/theme.js'; +import type { outputSchema, Progress, RemoteLaunchedOutput } from './AgentTool.js'; +import { inputSchema } from './AgentTool.js'; +import { getAgentColor } from './agentColorManager.js'; +import { GENERAL_PURPOSE_AGENT } from './built-in/generalPurposeAgent.js'; +const MAX_PROGRESS_MESSAGES_TO_SHOW = 3; + +/** + * Guard: checks if progress data has a `message` field (agent_progress or + * skill_progress). Other progress types (e.g. bash_progress forwarded from + * sub-agents) lack this field and must be skipped by UI helpers. + */ +function hasProgressMessage(data: Progress): data is AgentToolProgress { + if (!('message' in data)) { + return false; + } + const msg = (data as AgentToolProgress).message; + return msg != null && typeof msg === 'object' && 'type' in msg; +} + +/** + * Check if a progress message is a search/read/REPL operation (tool use or result). + * Returns { isSearch, isRead, isREPL } if it's a collapsible operation, null otherwise. + * + * For tool_result messages, uses the provided `toolUseByID` map to find the + * corresponding tool_use block instead of relying on `normalizedMessages`. + */ +function getSearchOrReadInfo(progressMessage: ProgressMessage, tools: Tools, toolUseByID: Map): { + isSearch: boolean; + isRead: boolean; + isREPL: boolean; +} | null { + if (!hasProgressMessage(progressMessage.data)) { + return null; + } + const message = progressMessage.data.message; + + // Check tool_use (assistant message) + if (message.type === 'assistant') { + return getSearchOrReadFromContent(message.message.content[0], tools); + } + + // Check tool_result (user message) - find corresponding tool use from the map + if (message.type === 'user') { + const content = message.message.content[0]; + if (content?.type === 'tool_result') { + const toolUse = toolUseByID.get(content.tool_use_id); + if (toolUse) { + return getSearchOrReadFromContent(toolUse, tools); + } + } + } + return null; +} +type SummaryMessage = { + type: 'summary'; + searchCount: number; + readCount: number; + replCount: number; + uuid: string; + isActive: boolean; // true if still in progress (last message was tool_use, not tool_result) +}; +type ProcessedMessage = { + type: 'original'; + message: ProgressMessage; +} | SummaryMessage; + +/** + * Process progress messages to group consecutive search/read operations into summaries. + * For ants only - returns original messages for non-ants. + * @param isAgentRunning - If true, the last group is always marked as active (in progress) + */ +function processProgressMessages(messages: ProgressMessage[], tools: Tools, isAgentRunning: boolean): ProcessedMessage[] { + // Only process for ants + if ("external" !== 'ant') { + return messages.filter((m): m is ProgressMessage => hasProgressMessage(m.data) && m.data.message.type !== 'user').map(m => ({ + type: 'original', + message: m + })); + } + const result: ProcessedMessage[] = []; + let currentGroup: { + searchCount: number; + readCount: number; + replCount: number; + startUuid: string; + } | null = null; + function flushGroup(isActive: boolean): void { + if (currentGroup && (currentGroup.searchCount > 0 || currentGroup.readCount > 0 || currentGroup.replCount > 0)) { + result.push({ + type: 'summary', + searchCount: currentGroup.searchCount, + readCount: currentGroup.readCount, + replCount: currentGroup.replCount, + uuid: `summary-${currentGroup.startUuid}`, + isActive + }); + } + currentGroup = null; + } + const agentMessages = messages.filter((m): m is ProgressMessage => hasProgressMessage(m.data)); + + // Build tool_use lookup incrementally as we iterate + const toolUseByID = new Map(); + for (const msg of agentMessages) { + // Track tool_use blocks as we see them + if (msg.data.message.type === 'assistant') { + for (const c of msg.data.message.message.content) { + if (c.type === 'tool_use') { + toolUseByID.set(c.id, c as ToolUseBlockParam); + } + } + } + const info = getSearchOrReadInfo(msg, tools, toolUseByID); + if (info && (info.isSearch || info.isRead || info.isREPL)) { + // This is a search/read/REPL operation - add to current group + if (!currentGroup) { + currentGroup = { + searchCount: 0, + readCount: 0, + replCount: 0, + startUuid: msg.uuid + }; + } + // Only count tool_result messages (not tool_use) to avoid double counting + if (msg.data.message.type === 'user') { + if (info.isSearch) { + currentGroup.searchCount++; + } else if (info.isREPL) { + currentGroup.replCount++; + } else if (info.isRead) { + currentGroup.readCount++; + } + } + } else { + // Non-search/read/REPL message - flush current group (completed) and add this message + flushGroup(false); + // Skip user tool_result messages — subagent progress messages lack + // toolUseResult, so UserToolSuccessMessage returns null and the + // height=1 Box in renderToolUseProgressMessage shows as a blank line. + if (msg.data.message.type !== 'user') { + result.push({ + type: 'original', + message: msg + }); + } + } + } + + // Flush any remaining group - it's active if the agent is still running + flushGroup(isAgentRunning); + return result; +} +const ESTIMATED_LINES_PER_TOOL = 9; +const TERMINAL_BUFFER_LINES = 7; +type Output = z.input>; +export function AgentPromptDisplay(t0) { + const $ = _c(3); + const { + prompt, + dim: t1 + } = t0; + t1 === undefined ? false : t1; + let t2; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t2 = Prompt:; + $[0] = t2; + } else { + t2 = $[0]; + } + let t3; + if ($[1] !== prompt) { + t3 = {t2}{prompt}; + $[1] = prompt; + $[2] = t3; + } else { + t3 = $[2]; + } + return t3; +} +export function AgentResponseDisplay(t0) { + const $ = _c(5); + const { + content + } = t0; + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = Response:; + $[0] = t1; + } else { + t1 = $[0]; + } + let t2; + if ($[1] !== content) { + t2 = content.map(_temp); + $[1] = content; + $[2] = t2; + } else { + t2 = $[2]; + } + let t3; + if ($[3] !== t2) { + t3 = {t1}{t2}; + $[3] = t2; + $[4] = t3; + } else { + t3 = $[4]; + } + return t3; +} +function _temp(block, index) { + return {block.text}; +} +type VerboseAgentTranscriptProps = { + progressMessages: ProgressMessage[]; + tools: Tools; + verbose: boolean; +}; +function VerboseAgentTranscript(t0) { + const $ = _c(15); + const { + progressMessages, + tools, + verbose + } = t0; + let t1; + if ($[0] !== progressMessages) { + t1 = buildSubagentLookups(progressMessages.filter(_temp2).map(_temp3)); + $[0] = progressMessages; + $[1] = t1; + } else { + t1 = $[1]; + } + const { + lookups: agentLookups, + inProgressToolUseIDs + } = t1; + let t2; + if ($[2] !== agentLookups || $[3] !== inProgressToolUseIDs || $[4] !== progressMessages || $[5] !== tools || $[6] !== verbose) { + const filteredMessages = progressMessages.filter(_temp4); + let t3; + if ($[8] !== agentLookups || $[9] !== inProgressToolUseIDs || $[10] !== tools || $[11] !== verbose) { + t3 = progressMessage => ; + $[8] = agentLookups; + $[9] = inProgressToolUseIDs; + $[10] = tools; + $[11] = verbose; + $[12] = t3; + } else { + t3 = $[12]; + } + t2 = filteredMessages.map(t3); + $[2] = agentLookups; + $[3] = inProgressToolUseIDs; + $[4] = progressMessages; + $[5] = tools; + $[6] = verbose; + $[7] = t2; + } else { + t2 = $[7]; + } + let t3; + if ($[13] !== t2) { + t3 = <>{t2}; + $[13] = t2; + $[14] = t3; + } else { + t3 = $[14]; + } + return t3; +} +function _temp4(pm_1) { + if (!hasProgressMessage(pm_1.data)) { + return false; + } + const msg = pm_1.data.message; + if (msg.type === "user" && msg.toolUseResult === undefined) { + return false; + } + return true; +} +function _temp3(pm_0) { + return pm_0.data; +} +function _temp2(pm) { + return hasProgressMessage(pm.data); +} +export function renderToolResultMessage(data: Output, progressMessagesForMessage: ProgressMessage[], { + tools, + verbose, + theme, + isTranscriptMode = false +}: { + tools: Tools; + verbose: boolean; + theme: ThemeName; + isTranscriptMode?: boolean; +}): React.ReactNode { + // Remote-launched agents (ant-only) use a private output type not in the + // public schema. Narrow via the internal discriminant. + const internal = data as Output | RemoteLaunchedOutput; + if (internal.status === 'remote_launched') { + return + + + Remote agent launched{' '} + + · {internal.taskId} · {internal.sessionUrl} + + + + ; + } + if (data.status === 'async_launched') { + const { + prompt + } = data; + return + + + Backgrounded agent + {!isTranscriptMode && + {' ('} + + + {prompt && } + + {')'} + } + + + {isTranscriptMode && prompt && + + } + ; + } + if (data.status !== 'completed') { + return null; + } + const { + agentId, + totalDurationMs, + totalToolUseCount, + totalTokens, + usage, + content, + prompt + } = data; + const result = [totalToolUseCount === 1 ? '1 tool use' : `${totalToolUseCount} tool uses`, formatNumber(totalTokens) + ' tokens', formatDuration(totalDurationMs)]; + const completionMessage = `Done (${result.join(' · ')})`; + const finalAssistantMessage = createAssistantMessage({ + content: completionMessage, + usage: { + ...usage, + inference_geo: null, + iterations: null, + speed: null + } + }); + return + {"external" === 'ant' && + + [ANT-ONLY] API calls: {getDisplayPath(getDumpPromptsPath(agentId))} + + } + {isTranscriptMode && prompt && + + } + {isTranscriptMode ? + + : null} + {isTranscriptMode && content && content.length > 0 && + + } + + + + {!isTranscriptMode && + {' '} + + } + ; +} +export function renderToolUseMessage({ + description, + prompt +}: Partial<{ + description: string; + prompt: string; +}>): React.ReactNode { + if (!description || !prompt) { + return null; + } + return description; +} +export function renderToolUseTag(input: Partial<{ + description: string; + prompt: string; + subagent_type: string; + model?: ModelAlias; +}>): React.ReactNode { + const tags: React.ReactNode[] = []; + if (input.model) { + const mainModel = getMainLoopModel(); + const agentModel = parseUserSpecifiedModel(input.model); + if (agentModel !== mainModel) { + tags.push( + {renderModelName(agentModel)} + ); + } + } + if (tags.length === 0) { + return null; + } + return <>{tags}; +} +const INITIALIZING_TEXT = 'Initializing…'; +export function renderToolUseProgressMessage(progressMessages: ProgressMessage[], { + tools, + verbose, + terminalSize, + inProgressToolCallCount, + isTranscriptMode = false +}: { + tools: Tools; + verbose: boolean; + terminalSize?: { + columns: number; + rows: number; + }; + inProgressToolCallCount?: number; + isTranscriptMode?: boolean; +}): React.ReactNode { + if (!progressMessages.length) { + return + {INITIALIZING_TEXT} + ; + } + + // Checks to see if we should show a super condensed progress message summary. + // This prevents flickers when the terminal size is too small to render all the dynamic content + const toolToolRenderLinesEstimate = (inProgressToolCallCount ?? 1) * ESTIMATED_LINES_PER_TOOL + TERMINAL_BUFFER_LINES; + const shouldUseCondensedMode = !isTranscriptMode && terminalSize && terminalSize.rows && terminalSize.rows < toolToolRenderLinesEstimate; + const getProgressStats = () => { + const toolUseCount = count(progressMessages, msg => { + if (!hasProgressMessage(msg.data)) { + return false; + } + const message = msg.data.message; + return message.message.content.some(content => content.type === 'tool_use'); + }); + const latestAssistant = progressMessages.findLast((msg): msg is ProgressMessage => hasProgressMessage(msg.data) && msg.data.message.type === 'assistant'); + let tokens = null; + if (latestAssistant?.data.message.type === 'assistant') { + const usage = latestAssistant.data.message.message.usage; + tokens = (usage.cache_creation_input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0) + usage.input_tokens + usage.output_tokens; + } + return { + toolUseCount, + tokens + }; + }; + if (shouldUseCondensedMode) { + const { + toolUseCount, + tokens + } = getProgressStats(); + return + + In progress… · {toolUseCount} tool{' '} + {toolUseCount === 1 ? 'use' : 'uses'} + {tokens && ` · ${formatNumber(tokens)} tokens`} ·{' '} + + + ; + } + + // Process messages to group consecutive search/read operations into summaries (ants only) + // isAgentRunning=true since this is the progress view while the agent is still running + const processedMessages = processProgressMessages(progressMessages, tools, true); + + // For display, take the last few processed messages + const displayedMessages = isTranscriptMode ? processedMessages : processedMessages.slice(-MAX_PROGRESS_MESSAGES_TO_SHOW); + + // Count hidden tool uses specifically (not all messages) to match the + // final "Done (N tool uses)" count. Each tool use generates multiple + // progress messages (tool_use + tool_result + text), so counting all + // hidden messages inflates the number shown to the user. + const hiddenMessages = isTranscriptMode ? [] : processedMessages.slice(0, Math.max(0, processedMessages.length - MAX_PROGRESS_MESSAGES_TO_SHOW)); + const hiddenToolUseCount = count(hiddenMessages, m => { + if (m.type === 'summary') { + return m.searchCount + m.readCount + m.replCount > 0; + } + const data = m.message.data; + if (!hasProgressMessage(data)) { + return false; + } + return data.message.message.content.some(content => content.type === 'tool_use'); + }); + const firstData = progressMessages[0]?.data; + const prompt = firstData && hasProgressMessage(firstData) ? firstData.prompt : undefined; + + // After grouping, displayedMessages can be empty when the only progress so + // far is an assistant tool_use for a search/read op (grouped but not yet + // counted, since counts increment on tool_result). Fall back to the + // initializing text so MessageResponse doesn't render a bare ⎿. + if (displayedMessages.length === 0 && !(isTranscriptMode && prompt)) { + return + {INITIALIZING_TEXT} + ; + } + const { + lookups: subagentLookups, + inProgressToolUseIDs: collapsedInProgressIDs + } = buildSubagentLookups(progressMessages.filter((pm): pm is ProgressMessage => hasProgressMessage(pm.data)).map(pm => pm.data)); + return + + + {isTranscriptMode && prompt && + + } + {displayedMessages.map(processed => { + if (processed.type === 'summary') { + // Render summary for grouped search/read/REPL operations using shared formatting + const summaryText = getSearchReadSummaryText(processed.searchCount, processed.readCount, processed.isActive, processed.replCount); + return + {summaryText} + ; + } + // Render original message without height=1 wrapper so null + // content (tool not found, renderToolUseMessage returns null) + // doesn't leave a blank line. Tool call headers are single-line + // anyway so truncation isn't needed. + return ; + })} + + {hiddenToolUseCount > 0 && + +{hiddenToolUseCount} more tool{' '} + {hiddenToolUseCount === 1 ? 'use' : 'uses'} + } + + ; +} +export function renderToolUseRejectedMessage(_input: { + description: string; + prompt: string; + subagent_type: string; +}, { + progressMessagesForMessage, + tools, + verbose, + isTranscriptMode +}: { + columns: number; + messages: Message[]; + style?: 'condensed'; + theme: ThemeName; + progressMessagesForMessage: ProgressMessage[]; + tools: Tools; + verbose: boolean; + isTranscriptMode?: boolean; +}): React.ReactNode { + // Get agentId from progress messages if available (agent was running before rejection) + const firstData = progressMessagesForMessage[0]?.data; + const agentId = firstData && hasProgressMessage(firstData) ? firstData.agentId : undefined; + return <> + {"external" === 'ant' && agentId && + + [ANT-ONLY] API calls: {getDisplayPath(getDumpPromptsPath(agentId))} + + } + {renderToolUseProgressMessage(progressMessagesForMessage, { + tools, + verbose, + isTranscriptMode + })} + + ; +} +export function renderToolUseErrorMessage(result: ToolResultBlockParam['content'], { + progressMessagesForMessage, + tools, + verbose, + isTranscriptMode +}: { + progressMessagesForMessage: ProgressMessage[]; + tools: Tools; + verbose: boolean; + isTranscriptMode?: boolean; +}): React.ReactNode { + return <> + {renderToolUseProgressMessage(progressMessagesForMessage, { + tools, + verbose, + isTranscriptMode + })} + + ; +} +function calculateAgentStats(progressMessages: ProgressMessage[]): { + toolUseCount: number; + tokens: number | null; +} { + const toolUseCount = count(progressMessages, msg => { + if (!hasProgressMessage(msg.data)) { + return false; + } + const message = msg.data.message; + return message.type === 'user' && message.message.content.some(content => content.type === 'tool_result'); + }); + const latestAssistant = progressMessages.findLast((msg): msg is ProgressMessage => hasProgressMessage(msg.data) && msg.data.message.type === 'assistant'); + let tokens = null; + if (latestAssistant?.data.message.type === 'assistant') { + const usage = latestAssistant.data.message.message.usage; + tokens = (usage.cache_creation_input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0) + usage.input_tokens + usage.output_tokens; + } + return { + toolUseCount, + tokens + }; +} +export function renderGroupedAgentToolUse(toolUses: Array<{ + param: ToolUseBlockParam; + isResolved: boolean; + isError: boolean; + isInProgress: boolean; + progressMessages: ProgressMessage[]; + result?: { + param: ToolResultBlockParam; + output: Output; + }; +}>, options: { + shouldAnimate: boolean; + tools: Tools; +}): React.ReactNode | null { + const { + shouldAnimate, + tools + } = options; + + // Calculate stats for each agent + const agentStats = toolUses.map(({ + param, + isResolved, + isError, + progressMessages, + result + }) => { + const stats = calculateAgentStats(progressMessages); + const lastToolInfo = extractLastToolInfo(progressMessages, tools); + const parsedInput = inputSchema().safeParse(param.input); + + // teammate_spawned is not part of the exported Output type (cast through unknown + // for dead code elimination), so check via string comparison on the raw value + const isTeammateSpawn = result?.output?.status as string === 'teammate_spawned'; + + // For teammate spawns, show @name with type in parens and description as status + let agentType: string; + let description: string | undefined; + let color: keyof Theme | undefined; + let descriptionColor: keyof Theme | undefined; + let taskDescription: string | undefined; + if (isTeammateSpawn && parsedInput.success && parsedInput.data.name) { + agentType = `@${parsedInput.data.name}`; + const subagentType = parsedInput.data.subagent_type; + description = isCustomSubagentType(subagentType) ? subagentType : undefined; + taskDescription = parsedInput.data.description; + // Use the custom agent definition's color on the type, not the name + descriptionColor = isCustomSubagentType(subagentType) ? getAgentColor(subagentType) as keyof Theme | undefined : undefined; + } else { + agentType = parsedInput.success ? userFacingName(parsedInput.data) : 'Agent'; + description = parsedInput.success ? parsedInput.data.description : undefined; + color = parsedInput.success ? userFacingNameBackgroundColor(parsedInput.data) : undefined; + taskDescription = undefined; + } + + // Check if this was launched as a background agent OR backgrounded mid-execution + const launchedAsAsync = parsedInput.success && 'run_in_background' in parsedInput.data && parsedInput.data.run_in_background === true; + const outputStatus = (result?.output as { + status?: string; + } | undefined)?.status; + const backgroundedMidExecution = outputStatus === 'async_launched' || outputStatus === 'remote_launched'; + const isAsync = launchedAsAsync || backgroundedMidExecution || isTeammateSpawn; + const name = parsedInput.success ? parsedInput.data.name : undefined; + return { + id: param.id, + agentType, + description, + toolUseCount: stats.toolUseCount, + tokens: stats.tokens, + isResolved, + isError, + isAsync, + color, + descriptionColor, + lastToolInfo, + taskDescription, + name + }; + }); + const anyUnresolved = toolUses.some(t => !t.isResolved); + const anyError = toolUses.some(t => t.isError); + const allComplete = !anyUnresolved; + + // Check if all agents are the same type + const allSameType = agentStats.length > 0 && agentStats.every(stat => stat.agentType === agentStats[0]?.agentType); + const commonType = allSameType && agentStats[0]?.agentType !== 'Agent' ? agentStats[0]?.agentType : null; + + // Check if all resolved agents are async (background) + const allAsync = agentStats.every(stat => stat.isAsync); + return + + + + {allComplete ? allAsync ? <> + {toolUses.length} background agents launched{' '} + + + + : <> + {toolUses.length}{' '} + {commonType ? `${commonType} agents` : 'agents'} finished + : <> + Running {toolUses.length}{' '} + {commonType ? `${commonType} agents` : 'agents'}… + }{' '} + + {!allAsync && } + + {agentStats.map((stat, index) => )} + ; +} +export function userFacingName(input: Partial<{ + description: string; + prompt: string; + subagent_type: string; + name: string; + team_name: string; +}> | undefined): string { + if (input?.subagent_type && input.subagent_type !== GENERAL_PURPOSE_AGENT.agentType) { + // Display "worker" agents as "Agent" for cleaner UI + if (input.subagent_type === 'worker') { + return 'Agent'; + } + return input.subagent_type; + } + return 'Agent'; +} +export function userFacingNameBackgroundColor(input: Partial<{ + description: string; + prompt: string; + subagent_type: string; +}> | undefined): keyof Theme | undefined { + if (!input?.subagent_type) { + return undefined; + } + + // Get the color for this agent + return getAgentColor(input.subagent_type) as keyof Theme | undefined; +} +export function extractLastToolInfo(progressMessages: ProgressMessage[], tools: Tools): string | null { + // Build tool_use lookup from all progress messages (needed for reverse iteration) + const toolUseByID = new Map(); + for (const pm of progressMessages) { + if (!hasProgressMessage(pm.data)) { + continue; + } + if (pm.data.message.type === 'assistant') { + for (const c of pm.data.message.message.content) { + if (c.type === 'tool_use') { + toolUseByID.set(c.id, c as ToolUseBlockParam); + } + } + } + } + + // Count trailing consecutive search/read operations from the end + let searchCount = 0; + let readCount = 0; + for (let i = progressMessages.length - 1; i >= 0; i--) { + const msg = progressMessages[i]!; + if (!hasProgressMessage(msg.data)) { + continue; + } + const info = getSearchOrReadInfo(msg, tools, toolUseByID); + if (info && (info.isSearch || info.isRead)) { + // Only count tool_result messages to avoid double counting + if (msg.data.message.type === 'user') { + if (info.isSearch) { + searchCount++; + } else if (info.isRead) { + readCount++; + } + } + } else { + break; + } + } + if (searchCount + readCount >= 2) { + return getSearchReadSummaryText(searchCount, readCount, true); + } + + // Find the last tool_result message + const lastToolResult = progressMessages.findLast((msg): msg is ProgressMessage => { + if (!hasProgressMessage(msg.data)) { + return false; + } + const message = msg.data.message; + return message.type === 'user' && message.message.content.some(c => c.type === 'tool_result'); + }); + if (lastToolResult?.data.message.type === 'user') { + const toolResultBlock = lastToolResult.data.message.message.content.find(c => c.type === 'tool_result'); + if (toolResultBlock?.type === 'tool_result') { + // Look up the corresponding tool_use — already indexed above + const toolUseBlock = toolUseByID.get(toolResultBlock.tool_use_id); + if (toolUseBlock) { + const tool = findToolByName(tools, toolUseBlock.name); + if (!tool) { + return toolUseBlock.name; // Fallback to raw name + } + const input = toolUseBlock.input as Record; + const parsedInput = tool.inputSchema.safeParse(input); + + // Get user-facing tool name + const userFacingToolName = tool.userFacingName(parsedInput.success ? parsedInput.data : undefined); + + // Try to get summary from the tool itself + if (tool.getToolUseSummary) { + const summary = tool.getToolUseSummary(parsedInput.success ? parsedInput.data : undefined); + if (summary) { + return `${userFacingToolName}: ${summary}`; + } + } + + // Default: just show user-facing tool name + return userFacingToolName; + } + } + } + return null; +} +function isCustomSubagentType(subagentType: string | undefined): subagentType is string { + return !!subagentType && subagentType !== GENERAL_PURPOSE_AGENT.agentType && subagentType !== 'worker'; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJUb29sUmVzdWx0QmxvY2tQYXJhbSIsIlRvb2xVc2VCbG9ja1BhcmFtIiwiUmVhY3QiLCJDb25maWd1cmFibGVTaG9ydGN1dEhpbnQiLCJDdHJsT1RvRXhwYW5kIiwiU3ViQWdlbnRQcm92aWRlciIsIkJ5bGluZSIsIktleWJvYXJkU2hvcnRjdXRIaW50IiwieiIsIkFnZW50UHJvZ3Jlc3NMaW5lIiwiRmFsbGJhY2tUb29sVXNlRXJyb3JNZXNzYWdlIiwiRmFsbGJhY2tUb29sVXNlUmVqZWN0ZWRNZXNzYWdlIiwiTWFya2Rvd24iLCJNZXNzYWdlIiwiTWVzc2FnZUNvbXBvbmVudCIsIk1lc3NhZ2VSZ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5cbiAgICAgICAgPE1lc3NhZ2VSZXNwb25zZSBoZWlnaHQ9ezF9PlxuICAgICAgICAgIDxUZXh0PlxuICAgICAgICAgICAgUmVtb3RlIGFnZW50IGxhdW5jaGVkeycgJ31cbiAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPlxuICAgICAgICAgICAgICDCtyB7aW50ZXJuYWwudGFza0lkfSDCtyB7aW50ZXJuYWwuc2Vzc2lvblVybH1cbiAgICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgICA8L1RleHQ+XG4gICAgICAgIDwvTWVzc2FnZVJlc3BvbnNlPlxuICAgICAgPC9Cb3g+XG4gICAgKVxuICB9XG4gIGlmIChkYXRhLnN0YXR1cyA9PT0gJ2FzeW5jX2xhdW5jaGVkJykge1xuICAgIGNvbnN0IHsgcHJvbXB0IH0gPSBkYXRhXG4gICAgcmV0dXJuIChcbiAgICAgIDxCb3ggZmxleERpcmVjdGlvbj1cImNvbHVtblwiPlxuICAgICAgICA8TWVzc2FnZVJlc3BvbnNlIGhlaWdodD17MX0+XG4gICAgICAgICAgPFRleHQ+XG4gICAgICAgICAgICBCYWNrZ3JvdW5kZWQgYWdlbnRcbiAgICAgICAgICAgIHshaXNUcmFuc2NyaXB0TW9kZSAmJiAoXG4gICAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPlxuICAgICAgICAgICAgICAgIHsnICgnfVxuICAgICAgICAgICAgICAgIDxCeWxpbmU+XG4gICAgICAgICAgICAgICAgICA8S2V5Ym9hcmRTaG9ydGN1dEhpbnQgc2hvcnRjdXQ9XCLihpNcIiBhY3Rpb249XCJtYW5hZ2VcIiAvPlxuICAgICAgICAgICAgICAgICAge3Byb21wdCAmJiAoXG4gICAgICAgICAgICAgICAgICAgIDxDb25maWd1cmFibGVTaG9ydGN1dEhpbnRcbiAgICAgICAgICAgICAgICAgICAgICBhY3Rpb249XCJhcHA6dG9nZ2xlVHJhbnNjcmlwdFwiXG4gICAgICAgICAgICAgICAgICAgICAgY29udGV4dD1cIkdsb2JhbFwiXG4gICAgICAgICAgICAgICAgICAgICAgZmFsbGJhY2s9XCJjdHJsK29cIlxuICAgICAgICAgICAgICAgICAgICAgIGRlc2NyaXB0aW9uPVwiZXhwYW5kXCJcbiAgICAgICAgICAgICAgICAgICAgLz5cbiAgICAgICAgICAgICAgICAgICl9XG4gICAgICAgICAgICAgICAgPC9CeWxpbmU+XG4gICAgICAgICAgICAgICAgeycpJ31cbiAgICAgICAgICAgICAgPC9UZXh0PlxuICAgICAgICAgICAgKX1cbiAgICAgICAgICA8L1RleHQ+XG4gICAgICAgIDwvTWVzc2FnZVJlc3BvbnNlPlxuICAgICAgICB7aXNUcmFuc2NyaXB0TW9kZSAmJiBwcm9tcHQgJiYgKFxuICAgICAgICAgIDxNZXNzYWdlUmVzcG9uc2U+XG4gICAgICAgICAgICA8QWdlbnRQcm9tcHREaXNwbGF5IHByb21wdD17cHJvbXB0fSB0aGVtZT17dGhlbWV9IC8+XG4gICAgICAgICAgPC9NZXNzYWdlUmVzcG9uc2U+XG4gICAgICAgICl9XG4gICAgICA8L0JveD5cbiAgICApXG4gIH1cblxuICBpZiAoZGF0YS5zdGF0dXMgIT09ICdjb21wbGV0ZWQnKSB7XG4gICAgcmV0dXJuIG51bGxcbiAgfVxuXG4gIGNvbnN0IHtcbiAgICBhZ2VudElkLFxuICAgIHRvdGFsRHVyYXRpb25NcyxcbiAgICB0b3RhbFRvb2xVc2VDb3VudCxcbiAgICB0b3RhbFRva2VucyxcbiAgICB1c2FnZSxcbiAgICBjb250ZW50LFxuICAgIHByb21wdCxcbiAgfSA9IGRhdGFcbiAgY29uc3QgcmVzdWx0ID0gW1xuICAgIHRvdGFsVG9vbFVzZUNvdW50ID09PSAxID8gJzEgdG9vbCB1c2UnIDogYCR7dG90YWxUb29sVXNlQ291bnR9IHRvb2wgdXNlc2AsXG4gICAgZm9ybWF0TnVtYmVyKHRvdGFsVG9rZW5zKSArICcgdG9rZW5zJyxcbiAgICBmb3JtYXRE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ZC5yZWFkQ291bnQsXG4gICAgICAgICAgICAgICAgcHJvY2Vzc2VkLmlzQWN0aXZlLFxuICAgICAgICAgICAgICAgIHByb2Nlc3NlZC5yZXBsQ291bnQsXG4gICAgICAgICAgICAgIClcbiAgICAgICAgICAgICAgcmV0dXJuIChcbiAgICAgICAgICAgICAgICA8Qm94IGtleT17cHJvY2Vzc2VkLnV1aWR9IGhlaWdodD17MX0gb3ZlcmZsb3c9XCJoaWRkZW5cIj5cbiAgICAgICAgICAgICAgICAgIDxUZXh0IGRpbUNvbG9yPntzdW1tYXJ5VGV4dH08L1RleHQ+XG4gICAgICAgICAgICAgICAgPC9Cb3g+XG4gICAgICAgICAgICAgIClcbiAgICAgICAgICAgIH1cbiAgICAgICAgICAgIC8vIFJlbmRlciBvcmlnaW5hbCBtZXNzYWdlIHdpdGhvdXQgaGVpZ2h0PTEgd3JhcHBlciBzbyBudWxsXG4gICAgICAgICAgICAvLyBjb250ZW50ICh0b29sIG5vdCBmb3VuZCwgcmVuZGVyVG9vbFVzZU1lc3NhZ2UgcmV0dXJucyBudWxsKVxuICAgICAgICAgICAgLy8gZG9lc24ndCBsZWF2ZSBhIGJsYW5rIGxpbmUuIFRvb2wgY2FsbCBoZWFkZXJzIGFyZSBzaW5nbGUtbGluZVxuICAgICAgICAgICAgLy8gYW55d2F5IHNvIHRydW5jYXRpb24gaXNuJ3QgbmVlZGVkLlxuICAgICAgICAgICAgcmV0dXJuIChcbiAgICAgICAgICAgICAgPE1lc3NhZ2VDb21wb25lbnRcbiAgICAgICAgICAgICAgICBrZXk9e3Byb2Nlc3NlZC5tZXNzYWdlLnV1aWR9XG4gICAgICAgICAgICAgICAgbWVzc2FnZT17cHJvY2Vzc2VkLm1lc3NhZ2UuZGF0YS5tZXNzYWdlfVxuICAgICAgICAgICAgICAgIGxvb2t1cHM9e3N1YmFnZW50TG9va3Vwc31cbiAgICAgICAgICAgICAgICBhZGRNYXJnaW49e2ZhbHNlfVxuICAgICAgICAgICAgICAgIHRvb2xzPXt0b29sc31cbiAgICAgICAgICAgICAgICBjb21tYW5kcz17W119XG4gICAgICAgICAgICAgICAgdmVyYm9zZT17dmVyYm9zZX1cbiAgICAgICAgICAgICAgICBpblByb2dyZXNzVG9vbFVzZUlEcz17Y29sbGFwc2VkSW5Qcm9ncmVzc0lEc31cbiAgICAgICAgICAgICAgICBwcm9ncmVzc01lc3NhZ2VzRm9yTWVzc2FnZT17W119XG4gICAgICAgICAgICAgICAgc2hvdWxkQW5pbWF0ZT17ZmFsc2V9XG4gICAgICAgICAgICAgICAgc2hvdWxkU2hvd0RvdD17ZmFsc2V9XG4gICAgICAgICAgICAgICAgc3R5bGU9XCJjb25kZW5zZWRcIlxuICAgICAgICAgICAgICAgIGlzVHJhbnNjcmlwdE1vZGU9e2ZhbHNlfVxuICAgICAgICAgICAgICAgIGlzU3RhdGljPXt0cnVlfVxuICAgICAgICAgICAgICAvPlxuICAgICAgICAgICAgKVxuICAgICAgICAgIH0pfVxuICAgICAgICA8L1N1YkFnZW50UHJvdmlkZXI+XG4gICAgICAgIHtoaWRkZW5Ub29sVXNlQ291bnQgPiAwICYmIChcbiAgICAgICAgICA8VGV4dCBkaW1Db2xvcj5cbiAgICAgICAgICAgICt7aGlkZGVuVG9vbFVzZUNvdW50fSBtb3JlIHRvb2x7JyAnfVxuICAgICAgICAgICAge2hpZGRlblRvb2xVc2VDb3VudCA9PT0gMSA/ICd1c2UnIDogJ3VzZXMnfSA8Q3RybE9Ub0V4cGFuZCAvPlxuICAgICAgICAgIDwvVGV4dD5cbiAgICAgICAgKX1cbiAgICAgIDwvQm94PlxuICAgIDwvTWVzc2FnZVJlc3BvbnNlPlxuICApXG59XG5cbmV4cG9ydCBmdW5jdGlvbiByZW5kZXJUb29sVXNlUmVqZWN0ZWRNZXNzYWdlKFxuICBfaW5wdXQ6IHsgZGVzY3JpcHRpb246IHN0cmluZzsgcHJvbXB0OiBzdHJpbmc7IHN1YmFnZW50X3R5cGU6IHN0cmluZyB9LFx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TdGF0cy5ldmVyeShzdGF0ID0+IHN0YXQuaXNBc3luYylcblxuICByZXR1cm4gKFxuICAgIDxCb3ggZmxleERpcmVjdGlvbj1cImNvbHVtblwiIG1hcmdpblRvcD17MX0+XG4gICAgICA8Qm94IGZsZXhEaXJlY3Rpb249XCJyb3dcIj5cbiAgICAgICAgPFRvb2xVc2VMb2FkZXJcbiAgICAgICAgICBzaG91bGRBbmltYXRlPXtzaG91bGRBbmltYXRlICYmIGFueVVucmVzb2x2ZWR9XG4gICAgICAgICAgaXNVbnJlc29sdmVkPXthbnlVbnJlc29sdmVkfVxuICAgICAgICAgIGlzRXJyb3I9e2FueUVycm9yfVxuICAgICAgICAvPlxuICAgICAgICA8VGV4dD5cbiAgICAgICAgICB7YWxsQ29tcGxldGUgPyAoXG4gICAgICAgICAgICBhbGxBc3luYyA/IChcbiAgICAgICAgICAgICAgPD5cbiAgICAgICAgICAgICAgICA8VGV4dCBib2xkPnt0b29sVXNlcy5sZW5ndGh9PC9UZXh0PiBiYWNrZ3JvdW5kIGFnZW50cyBsYXVuY2hlZHsnICd9XG4gICAgICAgICAgICAgICAgPFRleHQgZGltQ29sb3I+XG4gICAgICAgICAgICAgICAgICA8S2V5Ym9hcmRTaG9ydGN1dEhpbnQgc2hvcnRjdXQ9XCLihpNcIiBhY3Rpb249XCJtYW5hZ2VcIiBwYXJlbnMgLz5cbiAgICAgICAgICAgICAgICA8L1RleHQ+XG4gICAgICAgICAgICAgIDwvPlxuICAgICAgICAgICAgKSA6IChcbiAgICAgICAgICAgICAgPD5cbiAgICAgICAgICAgICAgICA8VGV4dCBib2xkPnt0b29sVXNlcy5sZW5ndGh9PC9UZXh0PnsnICd9XG4gICAgICAgICAgICAgICAge2NvbW1vblR5cGUgPyBgJHtjb21tb25UeXBlfSBhZ2VudHNgIDogJ2FnZW50cyd9IGZpbmlzaGVkXG4gICAgICAgICAgICAgIDwvPlxuICAgICAgICAgICAgKVxuICAgICAgICAgICkgOiAoXG4gICAgICAgICAg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b0JBLENBQzNCRCxZQUFZLEVBQUUsTUFBTSxHQUFHLFNBQVMsQ0FDakMsRUFBRUEsWUFBWSxJQUFJLE1BQU0sQ0FBQztFQUN4QixPQUNFLENBQUMsQ0FBQ0EsWUFBWSxJQUNkQSxZQUFZLEtBQUt4SyxxQkFBcUIsQ0FBQ2tLLFNBQVMsSUFDaERNLFlBQVksS0FBSyxRQUFRO0FBRTdCIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/tools/AgentTool/forkSubagent.ts b/_all-in-one-upload/cc-haha/src/tools/AgentTool/forkSubagent.ts new file mode 100644 index 0000000..3f5c354 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/AgentTool/forkSubagent.ts @@ -0,0 +1,210 @@ +import { feature } from 'bun:bundle' +import type { BetaToolUseBlock } from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs' +import { randomUUID } from 'crypto' +import { getIsNonInteractiveSession } from '../../bootstrap/state.js' +import { + FORK_BOILERPLATE_TAG, + FORK_DIRECTIVE_PREFIX, +} from '../../constants/xml.js' +import { isCoordinatorMode } from '../../coordinator/coordinatorMode.js' +import type { + AssistantMessage, + Message as MessageType, +} from '../../types/message.js' +import { logForDebugging } from '../../utils/debug.js' +import { createUserMessage } from '../../utils/messages.js' +import type { BuiltInAgentDefinition } from './loadAgentsDir.js' + +/** + * Fork subagent feature gate. + * + * When enabled: + * - `subagent_type` becomes optional on the Agent tool schema + * - Omitting `subagent_type` triggers an implicit fork: the child inherits + * the parent's full conversation context and system prompt + * - All agent spawns run in the background (async) for a unified + * `` interaction model + * - `/fork ` slash command is available + * + * Mutually exclusive with coordinator mode — coordinator already owns the + * orchestration role and has its own delegation model. + */ +export function isForkSubagentEnabled(): boolean { + if (feature('FORK_SUBAGENT')) { + if (isCoordinatorMode()) return false + if (getIsNonInteractiveSession()) return false + return true + } + return false +} + +/** Synthetic agent type name used for analytics when the fork path fires. */ +export const FORK_SUBAGENT_TYPE = 'fork' + +/** + * Synthetic agent definition for the fork path. + * + * Not registered in builtInAgents — used only when `!subagent_type` and the + * experiment is active. `tools: ['*']` with `useExactTools` means the fork + * child receives the parent's exact tool pool (for cache-identical API + * prefixes). `permissionMode: 'bubble'` surfaces permission prompts to the + * parent terminal. `model: 'inherit'` keeps the parent's model for context + * length parity. + * + * The getSystemPrompt here is unused: the fork path passes + * `override.systemPrompt` with the parent's already-rendered system prompt + * bytes, threaded via `toolUseContext.renderedSystemPrompt`. Reconstructing + * by re-calling getSystemPrompt() can diverge (GrowthBook cold→warm) and + * bust the prompt cache; threading the rendered bytes is byte-exact. + */ +export const FORK_AGENT = { + agentType: FORK_SUBAGENT_TYPE, + whenToUse: + 'Implicit fork — inherits full conversation context. Not selectable via subagent_type; triggered by omitting subagent_type when the fork experiment is active.', + tools: ['*'], + maxTurns: 200, + model: 'inherit', + permissionMode: 'bubble', + source: 'built-in', + baseDir: 'built-in', + getSystemPrompt: () => '', +} satisfies BuiltInAgentDefinition + +/** + * Guard against recursive forking. Fork children keep the Agent tool in their + * tool pool for cache-identical tool definitions, so we reject fork attempts + * at call time by detecting the fork boilerplate tag in conversation history. + */ +export function isInForkChild(messages: MessageType[]): boolean { + return messages.some(m => { + if (m.type !== 'user') return false + const content = m.message.content + if (!Array.isArray(content)) return false + return content.some( + block => + block.type === 'text' && + block.text.includes(`<${FORK_BOILERPLATE_TAG}>`), + ) + }) +} + +/** Placeholder text used for all tool_result blocks in the fork prefix. + * Must be identical across all fork children for prompt cache sharing. */ +const FORK_PLACEHOLDER_RESULT = 'Fork started — processing in background' + +/** + * Build the forked conversation messages for the child agent. + * + * For prompt cache sharing, all fork children must produce byte-identical + * API request prefixes. This function: + * 1. Keeps the full parent assistant message (all tool_use blocks, thinking, text) + * 2. Builds a single user message with tool_results for every tool_use block + * using an identical placeholder, then appends a per-child directive text block + * + * Result: [...history, assistant(all_tool_uses), user(placeholder_results..., directive)] + * Only the final text block differs per child, maximizing cache hits. + */ +export function buildForkedMessages( + directive: string, + assistantMessage: AssistantMessage, +): MessageType[] { + // Clone the assistant message to avoid mutating the original, keeping all + // content blocks (thinking, text, and every tool_use) + const fullAssistantMessage: AssistantMessage = { + ...assistantMessage, + uuid: randomUUID(), + message: { + ...assistantMessage.message, + content: [...assistantMessage.message.content], + }, + } + + // Collect all tool_use blocks from the assistant message + const toolUseBlocks = assistantMessage.message.content.filter( + (block): block is BetaToolUseBlock => block.type === 'tool_use', + ) + + if (toolUseBlocks.length === 0) { + logForDebugging( + `No tool_use blocks found in assistant message for fork directive: ${directive.slice(0, 50)}...`, + { level: 'error' }, + ) + return [ + createUserMessage({ + content: [ + { type: 'text' as const, text: buildChildMessage(directive) }, + ], + }), + ] + } + + // Build tool_result blocks for every tool_use, all with identical placeholder text + const toolResultBlocks = toolUseBlocks.map(block => ({ + type: 'tool_result' as const, + tool_use_id: block.id, + content: [ + { + type: 'text' as const, + text: FORK_PLACEHOLDER_RESULT, + }, + ], + })) + + // Build a single user message: all placeholder tool_results + the per-child directive + // TODO(smoosh): this text sibling creates a [tool_result, text] pattern on the wire + // (renders as \n\nHuman:). One-off per-child construction, + // not a repeated teacher, so low-priority. If we ever care, use smooshIntoToolResult + // from src/utils/messages.ts to fold the directive into the last tool_result.content. + const toolResultMessage = createUserMessage({ + content: [ + ...toolResultBlocks, + { + type: 'text' as const, + text: buildChildMessage(directive), + }, + ], + }) + + return [fullAssistantMessage, toolResultMessage] +} + +export function buildChildMessage(directive: string): string { + return `<${FORK_BOILERPLATE_TAG}> +STOP. READ THIS FIRST. + +You are a forked worker process. You are NOT the main agent. + +RULES (non-negotiable): +1. Your system prompt says "default to forking." IGNORE IT \u2014 that's for the parent. You ARE the fork. Do NOT spawn sub-agents; execute directly. +2. Do NOT converse, ask questions, or suggest next steps +3. Do NOT editorialize or add meta-commentary +4. USE your tools directly: Bash, Read, Write, etc. +5. If you modify files, commit your changes before reporting. Include the commit hash in your report. +6. Do NOT emit text between tool calls. Use tools silently, then report once at the end. +7. Stay strictly within your directive's scope. If you discover related systems outside your scope, mention them in one sentence at most — other workers cover those areas. +8. Keep your report under 500 words unless the directive specifies otherwise. Be factual and concise. +9. Your response MUST begin with "Scope:". No preamble, no thinking-out-loud. +10. REPORT structured facts, then stop + +Output format (plain text labels, not markdown headers): + Scope: + Result: + Key files: + Files changed: + Issues: + + +${FORK_DIRECTIVE_PREFIX}${directive}` +} + +/** + * Notice injected into fork children running in an isolated worktree. + * Tells the child to translate paths from the inherited context, re-read + * potentially stale files, and that its changes are isolated. + */ +export function buildWorktreeNotice( + parentCwd: string, + worktreeCwd: string, +): string { + return `You've inherited the conversation context above from a parent agent working in ${parentCwd}. You are operating in an isolated git worktree at ${worktreeCwd} — same repository, same relative file structure, separate working copy. Paths in the inherited context refer to the parent's working directory; translate them to your worktree root. Re-read files before editing if the parent may have modified them since they appear in the context. Your changes stay in this worktree and will not affect the parent's files.` +} diff --git a/_all-in-one-upload/cc-haha/src/tools/AgentTool/runAgent.ts b/_all-in-one-upload/cc-haha/src/tools/AgentTool/runAgent.ts new file mode 100644 index 0000000..e2cb942 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/AgentTool/runAgent.ts @@ -0,0 +1,973 @@ +import { feature } from 'bun:bundle' +import type { UUID } from 'crypto' +import { randomUUID } from 'crypto' +import uniqBy from 'lodash-es/uniqBy.js' +import { logForDebugging } from 'src/utils/debug.js' +import { getProjectRoot, getSessionId } from '../../bootstrap/state.js' +import { getCommand, getSkillToolCommands, hasCommand } from '../../commands.js' +import { + DEFAULT_AGENT_PROMPT, + enhanceSystemPromptWithEnvDetails, +} from '../../constants/prompts.js' +import type { QuerySource } from '../../constants/querySource.js' +import { getSystemContext, getUserContext } from '../../context.js' +import type { CanUseToolFn } from '../../hooks/useCanUseTool.js' +import { query } from '../../query.js' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../../services/analytics/growthbook.js' +import { getDumpPromptsPath } from '../../services/api/dumpPrompts.js' +import { cleanupAgentTracking } from '../../services/api/promptCacheBreakDetection.js' +import { + connectToServer, + fetchToolsForClient, +} from '../../services/mcp/client.js' +import { getMcpConfigByName } from '../../services/mcp/config.js' +import type { + MCPServerConnection, + ScopedMcpServerConfig, +} from '../../services/mcp/types.js' +import type { Tool, Tools, ToolUseContext } from '../../Tool.js' +import { killShellTasksForAgent } from '../../tasks/LocalShellTask/killShellTasks.js' +import type { Command } from '../../types/command.js' +import type { AgentId } from '../../types/ids.js' +import type { + AssistantMessage, + Message, + ProgressMessage, + RequestStartEvent, + StreamEvent, + SystemCompactBoundaryMessage, + TombstoneMessage, + ToolUseSummaryMessage, + UserMessage, +} from '../../types/message.js' +import { createAttachmentMessage } from '../../utils/attachments.js' +import { AbortError } from '../../utils/errors.js' +import { getDisplayPath } from '../../utils/file.js' +import { + cloneFileStateCache, + createFileStateCacheWithSizeLimit, + READ_FILE_STATE_CACHE_SIZE, +} from '../../utils/fileStateCache.js' +import { + type CacheSafeParams, + createSubagentContext, +} from '../../utils/forkedAgent.js' +import { registerFrontmatterHooks } from '../../utils/hooks/registerFrontmatterHooks.js' +import { clearSessionHooks } from '../../utils/hooks/sessionHooks.js' +import { executeSubagentStartHooks } from '../../utils/hooks.js' +import { createUserMessage } from '../../utils/messages.js' +import { getAgentModel } from '../../utils/model/agent.js' +import type { ModelAlias } from '../../utils/model/aliases.js' +import { + clearAgentTranscriptSubdir, + recordSidechainTranscript, + setAgentTranscriptSubdir, + writeAgentMetadata, +} from '../../utils/sessionStorage.js' +import { + isRestrictedToPluginOnly, + isSourceAdminTrusted, +} from '../../utils/settings/pluginOnlyPolicy.js' +import { + asSystemPrompt, + type SystemPrompt, +} from '../../utils/systemPromptType.js' +import { + isPerfettoTracingEnabled, + registerAgent as registerPerfettoAgent, + unregisterAgent as unregisterPerfettoAgent, +} from '../../utils/telemetry/perfettoTracing.js' +import type { ContentReplacementState } from '../../utils/toolResultStorage.js' +import { createAgentId } from '../../utils/uuid.js' +import { resolveAgentTools } from './agentToolUtils.js' +import { type AgentDefinition, isBuiltInAgent } from './loadAgentsDir.js' + +/** + * Initialize agent-specific MCP servers + * Agents can define their own MCP servers in their frontmatter that are additive + * to the parent's MCP clients. These servers are connected when the agent starts + * and cleaned up when the agent finishes. + * + * @param agentDefinition The agent definition with optional mcpServers + * @param parentClients MCP clients inherited from parent context + * @returns Merged clients (parent + agent-specific), agent MCP tools, and cleanup function + */ +async function initializeAgentMcpServers( + agentDefinition: AgentDefinition, + parentClients: MCPServerConnection[], +): Promise<{ + clients: MCPServerConnection[] + tools: Tools + cleanup: () => Promise +}> { + // If no agent-specific servers defined, return parent clients as-is + if (!agentDefinition.mcpServers?.length) { + return { + clients: parentClients, + tools: [], + cleanup: async () => {}, + } + } + + // When MCP is locked to plugin-only, skip frontmatter MCP servers for + // USER-CONTROLLED agents only. Plugin, built-in, and policySettings agents + // are admin-trusted — their frontmatter MCP is part of the admin-approved + // surface. Blocking them (as the first cut did) breaks plugin agents that + // legitimately need MCP, contradicting "plugin-provided always loads." + const agentIsAdminTrusted = isSourceAdminTrusted(agentDefinition.source) + if (isRestrictedToPluginOnly('mcp') && !agentIsAdminTrusted) { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Skipping MCP servers: strictPluginOnlyCustomization locks MCP to plugin-only (agent source: ${agentDefinition.source})`, + ) + return { + clients: parentClients, + tools: [], + cleanup: async () => {}, + } + } + + const agentClients: MCPServerConnection[] = [] + // Track which clients were newly created (inline definitions) vs. shared from parent + // Only newly created clients should be cleaned up when the agent finishes + const newlyCreatedClients: MCPServerConnection[] = [] + const agentTools: Tool[] = [] + + for (const spec of agentDefinition.mcpServers) { + let config: ScopedMcpServerConfig | null = null + let name: string + let isNewlyCreated = false + + if (typeof spec === 'string') { + // Reference by name - look up in existing MCP configs + // This uses the memoized connectToServer, so we may get a shared client + name = spec + config = getMcpConfigByName(spec) + if (!config) { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] MCP server not found: ${spec}`, + { level: 'warn' }, + ) + continue + } + } else { + // Inline definition as { [name]: config } + // These are agent-specific servers that should be cleaned up + const entries = Object.entries(spec) + if (entries.length !== 1) { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Invalid MCP server spec: expected exactly one key`, + { level: 'warn' }, + ) + continue + } + const [serverName, serverConfig] = entries[0]! + name = serverName + config = { + ...serverConfig, + scope: 'dynamic' as const, + } as ScopedMcpServerConfig + isNewlyCreated = true + } + + // Connect to the server + const client = await connectToServer(name, config) + agentClients.push(client) + if (isNewlyCreated) { + newlyCreatedClients.push(client) + } + + // Fetch tools if connected + if (client.type === 'connected') { + const tools = await fetchToolsForClient(client) + agentTools.push(...tools) + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Connected to MCP server '${name}' with ${tools.length} tools`, + ) + } else { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Failed to connect to MCP server '${name}': ${client.type}`, + { level: 'warn' }, + ) + } + } + + // Create cleanup function for agent-specific servers + // Only clean up newly created clients (inline definitions), not shared/referenced ones + // Shared clients (referenced by string name) are memoized and used by the parent context + const cleanup = async () => { + for (const client of newlyCreatedClients) { + if (client.type === 'connected') { + try { + await client.cleanup() + } catch (error) { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Error cleaning up MCP server '${client.name}': ${error}`, + { level: 'warn' }, + ) + } + } + } + } + + // Return merged clients (parent + agent-specific) and agent tools + return { + clients: [...parentClients, ...agentClients], + tools: agentTools, + cleanup, + } +} + +type QueryMessage = + | StreamEvent + | RequestStartEvent + | Message + | ToolUseSummaryMessage + | TombstoneMessage + +/** + * Type guard to check if a message from query() is a recordable Message type. + * Matches the types we want to record: assistant, user, progress, or system compact_boundary. + */ +function isRecordableMessage( + msg: QueryMessage, +): msg is + | AssistantMessage + | UserMessage + | ProgressMessage + | SystemCompactBoundaryMessage { + return ( + msg.type === 'assistant' || + msg.type === 'user' || + msg.type === 'progress' || + (msg.type === 'system' && + 'subtype' in msg && + msg.subtype === 'compact_boundary') + ) +} + +export async function* runAgent({ + agentDefinition, + promptMessages, + toolUseContext, + canUseTool, + isAsync, + canShowPermissionPrompts, + forkContextMessages, + querySource, + override, + model, + maxTurns, + preserveToolUseResults, + availableTools, + allowedTools, + onCacheSafeParams, + contentReplacementState, + useExactTools, + worktreePath, + description, + transcriptSubdir, + onQueryProgress, +}: { + agentDefinition: AgentDefinition + promptMessages: Message[] + toolUseContext: ToolUseContext + canUseTool: CanUseToolFn + isAsync: boolean + /** Whether this agent can show permission prompts. Defaults to !isAsync. + * Set to true for in-process teammates that run async but share the terminal. */ + canShowPermissionPrompts?: boolean + forkContextMessages?: Message[] + querySource: QuerySource + override?: { + userContext?: { [k: string]: string } + systemContext?: { [k: string]: string } + systemPrompt?: SystemPrompt + abortController?: AbortController + agentId?: AgentId + } + model?: ModelAlias + maxTurns?: number + /** Preserve toolUseResult on messages for subagents with viewable transcripts */ + preserveToolUseResults?: boolean + /** Precomputed tool pool for the worker agent. Computed by the caller + * (AgentTool.tsx) to avoid a circular dependency between runAgent and tools.ts. + * Always contains the full tool pool assembled with the worker's own permission + * mode, independent of the parent's tool restrictions. */ + availableTools: Tools + /** Tool permission rules to add to the agent's session allow rules. + * When provided, replaces ALL allow rules so the agent only has what's + * explicitly listed (parent approvals don't leak through). */ + allowedTools?: string[] + /** Optional callback invoked with CacheSafeParams after constructing the agent's + * system prompt, context, and tools. Used by background summarization to fork + * the agent's conversation for periodic progress summaries. */ + onCacheSafeParams?: (params: CacheSafeParams) => void + /** Replacement state reconstructed from a resumed sidechain transcript so + * the same tool results are re-replaced (prompt cache stability). When + * omitted, createSubagentContext clones the parent's state. */ + contentReplacementState?: ContentReplacementState + /** When true, use availableTools directly without filtering through + * resolveAgentTools(). Also inherits the parent's thinkingConfig and + * isNonInteractiveSession instead of overriding them. Used by the fork + * subagent path to produce byte-identical API request prefixes for + * prompt cache hits. */ + useExactTools?: boolean + /** Worktree path if the agent was spawned with isolation: "worktree". + * Persisted to metadata so resume can restore the correct cwd. */ + worktreePath?: string + /** Original task description from AgentTool input. Persisted to metadata + * so a resumed agent's notification can show the original description. */ + description?: string + /** Optional subdirectory under subagents/ to group this agent's transcript + * with related ones (e.g. workflows/ for workflow subagents). */ + transcriptSubdir?: string + /** Optional callback fired on every message yielded by query() — including + * stream_event deltas that runAgent otherwise drops. Use to detect liveness + * during long single-block streams (e.g. thinking) where no assistant + * message is yielded for >60s. */ + onQueryProgress?: () => void +}): AsyncGenerator { + // Track subagent usage for feature discovery + + const appState = toolUseContext.getAppState() + const permissionMode = appState.toolPermissionContext.mode + // Always-shared channel to the root AppState store. toolUseContext.setAppState + // is a no-op when the *parent* is itself an async agent (nested async→async), + // so session-scoped writes (hooks, bash tasks) must go through this instead. + const rootSetAppState = + toolUseContext.setAppStateForTasks ?? toolUseContext.setAppState + + const resolvedAgentModel = getAgentModel( + agentDefinition.model, + toolUseContext.options.mainLoopModel, + model, + permissionMode, + ) + + const agentId = override?.agentId ? override.agentId : createAgentId() + + // Route this agent's transcript into a grouping subdirectory if requested + // (e.g. workflow subagents write to subagents/workflows//). + if (transcriptSubdir) { + setAgentTranscriptSubdir(agentId, transcriptSubdir) + } + + // Register agent in Perfetto trace for hierarchy visualization + if (isPerfettoTracingEnabled()) { + const parentId = toolUseContext.agentId ?? getSessionId() + registerPerfettoAgent(agentId, agentDefinition.agentType, parentId) + } + + // Log API calls path for subagents (ant-only) + if (process.env.USER_TYPE === 'ant') { + logForDebugging( + `[Subagent ${agentDefinition.agentType}] API calls: ${getDisplayPath(getDumpPromptsPath(agentId))}`, + ) + } + + // Handle message forking for context sharing + // Filter out incomplete tool calls from parent messages to avoid API errors + const contextMessages: Message[] = forkContextMessages + ? filterIncompleteToolCalls(forkContextMessages) + : [] + const initialMessages: Message[] = [...contextMessages, ...promptMessages] + + const agentReadFileState = + forkContextMessages !== undefined + ? cloneFileStateCache(toolUseContext.readFileState) + : createFileStateCacheWithSizeLimit(READ_FILE_STATE_CACHE_SIZE) + + const [baseUserContext, baseSystemContext] = await Promise.all([ + override?.userContext ?? getUserContext(), + override?.systemContext ?? getSystemContext(), + ]) + + // Read-only agents (Explore, Plan) don't act on commit/PR/lint rules from + // CLAUDE.md — the main agent has full context and interprets their output. + // Dropping claudeMd here saves ~5-15 Gtok/week across 34M+ Explore spawns. + // Explicit override.userContext from callers is preserved untouched. + // Kill-switch defaults true; flip tengu_slim_subagent_claudemd=false to revert. + const shouldOmitClaudeMd = + agentDefinition.omitClaudeMd && + !override?.userContext && + getFeatureValue_CACHED_MAY_BE_STALE('tengu_slim_subagent_claudemd', true) + const { claudeMd: _omittedClaudeMd, ...userContextNoClaudeMd } = + baseUserContext + const resolvedUserContext = shouldOmitClaudeMd + ? userContextNoClaudeMd + : baseUserContext + + // Explore/Plan are read-only search agents — the parent-session-start + // gitStatus (up to 40KB, explicitly labeled stale) is dead weight. If they + // need git info they run `git status` themselves and get fresh data. + // Saves ~1-3 Gtok/week fleet-wide. + const { gitStatus: _omittedGitStatus, ...systemContextNoGit } = + baseSystemContext + const resolvedSystemContext = + agentDefinition.agentType === 'Explore' || + agentDefinition.agentType === 'Plan' + ? systemContextNoGit + : baseSystemContext + + // Override permission mode if agent defines one + // However, don't override if parent is in bypassPermissions or acceptEdits mode - those should always take precedence + // For async agents, also set shouldAvoidPermissionPrompts since they can't show UI + const agentPermissionMode = agentDefinition.permissionMode + const agentGetAppState = () => { + const state = toolUseContext.getAppState() + let toolPermissionContext = state.toolPermissionContext + + // Override permission mode if agent defines one (unless parent is bypassPermissions, acceptEdits, or auto) + if ( + agentPermissionMode && + state.toolPermissionContext.mode !== 'bypassPermissions' && + state.toolPermissionContext.mode !== 'acceptEdits' && + !( + feature('TRANSCRIPT_CLASSIFIER') && + state.toolPermissionContext.mode === 'auto' + ) + ) { + toolPermissionContext = { + ...toolPermissionContext, + mode: agentPermissionMode, + } + } + + // Set flag to auto-deny prompts for agents that can't show UI + // Use explicit canShowPermissionPrompts if provided, otherwise: + // - bubble mode: always show prompts (bubbles to parent terminal) + // - default: !isAsync (sync agents show prompts, async agents don't) + const shouldAvoidPrompts = + canShowPermissionPrompts !== undefined + ? !canShowPermissionPrompts + : agentPermissionMode === 'bubble' + ? false + : isAsync + if (shouldAvoidPrompts) { + toolPermissionContext = { + ...toolPermissionContext, + shouldAvoidPermissionPrompts: true, + } + } + + // For background agents that can show prompts, await automated checks + // (classifier, permission hooks) before showing the permission dialog. + // Since these are background agents, waiting is fine — the user should + // only be interrupted when automated checks can't resolve the permission. + // This applies to bubble mode (always) and explicit canShowPermissionPrompts. + if (isAsync && !shouldAvoidPrompts) { + toolPermissionContext = { + ...toolPermissionContext, + awaitAutomatedChecksBeforeDialog: true, + } + } + + // Scope tool permissions: when allowedTools is provided, use them as session rules. + // IMPORTANT: Preserve cliArg rules (from SDK's --allowedTools) since those are + // explicit permissions from the SDK consumer that should apply to all agents. + // Only clear session-level rules from the parent to prevent unintended leakage. + if (allowedTools !== undefined) { + toolPermissionContext = { + ...toolPermissionContext, + alwaysAllowRules: { + // Preserve SDK-level permissions from --allowedTools + cliArg: state.toolPermissionContext.alwaysAllowRules.cliArg, + // Use the provided allowedTools as session-level permissions + session: [...allowedTools], + }, + } + } + + // Override effort level if agent defines one + const effortValue = + agentDefinition.effort !== undefined + ? agentDefinition.effort + : state.effortValue + + if ( + toolPermissionContext === state.toolPermissionContext && + effortValue === state.effortValue + ) { + return state + } + return { + ...state, + toolPermissionContext, + effortValue, + } + } + + const resolvedTools = useExactTools + ? availableTools + : resolveAgentTools(agentDefinition, availableTools, isAsync).resolvedTools + + const additionalWorkingDirectories = Array.from( + appState.toolPermissionContext.additionalWorkingDirectories.keys(), + ) + + const agentSystemPrompt = override?.systemPrompt + ? override.systemPrompt + : asSystemPrompt( + await getAgentSystemPrompt( + agentDefinition, + toolUseContext, + resolvedAgentModel, + additionalWorkingDirectories, + resolvedTools, + ), + ) + + // Determine abortController: + // - Override takes precedence + // - Async agents get a new unlinked controller (runs independently) + // - Sync agents share parent's controller + const agentAbortController = override?.abortController + ? override.abortController + : isAsync + ? new AbortController() + : toolUseContext.abortController + + // Execute SubagentStart hooks and collect additional context + const additionalContexts: string[] = [] + for await (const hookResult of executeSubagentStartHooks( + agentId, + agentDefinition.agentType, + agentAbortController.signal, + )) { + if ( + hookResult.additionalContexts && + hookResult.additionalContexts.length > 0 + ) { + additionalContexts.push(...hookResult.additionalContexts) + } + } + + // Add SubagentStart hook context as a user message (consistent with SessionStart/UserPromptSubmit) + if (additionalContexts.length > 0) { + const contextMessage = createAttachmentMessage({ + type: 'hook_additional_context', + content: additionalContexts, + hookName: 'SubagentStart', + toolUseID: randomUUID(), + hookEvent: 'SubagentStart', + }) + initialMessages.push(contextMessage) + } + + // Register agent's frontmatter hooks (scoped to agent lifecycle) + // Pass isAgent=true to convert Stop hooks to SubagentStop (since subagents trigger SubagentStop) + // Same admin-trusted gate for frontmatter hooks: under ["hooks"] alone + // (skills/agents not locked), user agents still load — block their + // frontmatter-hook REGISTRATION here where source is known, rather than + // blanket-blocking all session hooks at execution time (which would + // also kill plugin agents' hooks). + const hooksAllowedForThisAgent = + !isRestrictedToPluginOnly('hooks') || + isSourceAdminTrusted(agentDefinition.source) + if (agentDefinition.hooks && hooksAllowedForThisAgent) { + registerFrontmatterHooks( + rootSetAppState, + agentId, + agentDefinition.hooks, + `agent '${agentDefinition.agentType}'`, + true, // isAgent - converts Stop to SubagentStop + ) + } + + // Preload skills from agent frontmatter + const skillsToPreload = agentDefinition.skills ?? [] + if (skillsToPreload.length > 0) { + const allSkills = await getSkillToolCommands(getProjectRoot()) + + // Filter valid skills and warn about missing ones + const validSkills: Array<{ + skillName: string + skill: (typeof allSkills)[0] & { type: 'prompt' } + }> = [] + + for (const skillName of skillsToPreload) { + // Resolve the skill name, trying multiple strategies: + // 1. Exact match (hasCommand checks name, userFacingName, aliases) + // 2. Fully-qualified with agent's plugin prefix (e.g., "my-skill" → "plugin:my-skill") + // 3. Suffix match on ":skillName" for plugin-namespaced skills + const resolvedName = resolveSkillName( + skillName, + allSkills, + agentDefinition, + ) + if (!resolvedName) { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Warning: Skill '${skillName}' specified in frontmatter was not found`, + { level: 'warn' }, + ) + continue + } + + const skill = getCommand(resolvedName, allSkills) + if (skill.type !== 'prompt') { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Warning: Skill '${skillName}' is not a prompt-based skill`, + { level: 'warn' }, + ) + continue + } + validSkills.push({ skillName, skill }) + } + + // Load all skill contents concurrently and add to initial messages + const { formatSkillLoadingMetadata } = await import( + '../../utils/processUserInput/processSlashCommand.js' + ) + const loaded = await Promise.all( + validSkills.map(async ({ skillName, skill }) => ({ + skillName, + skill, + content: await skill.getPromptForCommand('', toolUseContext), + })), + ) + for (const { skillName, skill, content } of loaded) { + logForDebugging( + `[Agent: ${agentDefinition.agentType}] Preloaded skill '${skillName}'`, + ) + + // Add command-message metadata so the UI shows which skill is loading + const metadata = formatSkillLoadingMetadata( + skillName, + skill.progressMessage, + ) + + initialMessages.push( + createUserMessage({ + content: [{ type: 'text', text: metadata }, ...content], + isMeta: true, + }), + ) + } + } + + // Initialize agent-specific MCP servers (additive to parent's servers) + const { + clients: mergedMcpClients, + tools: agentMcpTools, + cleanup: mcpCleanup, + } = await initializeAgentMcpServers( + agentDefinition, + toolUseContext.options.mcpClients, + ) + + // Merge agent MCP tools with resolved agent tools, deduplicating by name. + // resolvedTools is already deduplicated (see resolveAgentTools), so skip + // the spread + uniqBy overhead when there are no agent-specific MCP tools. + const allTools = + agentMcpTools.length > 0 + ? uniqBy([...resolvedTools, ...agentMcpTools], 'name') + : resolvedTools + + // Build agent-specific options + const agentOptions: ToolUseContext['options'] = { + isNonInteractiveSession: useExactTools + ? toolUseContext.options.isNonInteractiveSession + : isAsync + ? true + : (toolUseContext.options.isNonInteractiveSession ?? false), + appendSystemPrompt: toolUseContext.options.appendSystemPrompt, + tools: allTools, + commands: [], + debug: toolUseContext.options.debug, + verbose: toolUseContext.options.verbose, + mainLoopModel: resolvedAgentModel, + // For fork children (useExactTools), inherit thinking config to match the + // parent's API request prefix for prompt cache hits. For regular + // sub-agents, disable thinking to control output token costs. + thinkingConfig: useExactTools + ? toolUseContext.options.thinkingConfig + : { type: 'disabled' as const }, + mcpClients: mergedMcpClients, + mcpResources: toolUseContext.options.mcpResources, + agentDefinitions: toolUseContext.options.agentDefinitions, + // Fork children (useExactTools path) need querySource on context.options + // for the recursive-fork guard at AgentTool.tsx call() — it checks + // options.querySource === 'agent:builtin:fork'. This survives autocompact + // (which rewrites messages, not context.options). Without this, the guard + // reads undefined and only the message-scan fallback fires — which + // autocompact defeats by replacing the fork-boilerplate message. + ...(useExactTools && { querySource }), + } + + // Create subagent context using shared helper + // - Sync agents share setAppState, setResponseLength, abortController with parent + // - Async agents are fully isolated (but with explicit unlinked abortController) + const agentToolUseContext = createSubagentContext(toolUseContext, { + options: agentOptions, + agentId, + agentType: agentDefinition.agentType, + messages: initialMessages, + readFileState: agentReadFileState, + abortController: agentAbortController, + getAppState: agentGetAppState, + // Sync agents share these callbacks with parent + shareSetAppState: !isAsync, + shareSetResponseLength: true, // Both sync and async contribute to response metrics + criticalSystemReminder_EXPERIMENTAL: + agentDefinition.criticalSystemReminder_EXPERIMENTAL, + contentReplacementState, + }) + + // Preserve tool use results for subagents with viewable transcripts (in-process teammates) + if (preserveToolUseResults) { + agentToolUseContext.preserveToolUseResults = true + } + + // Expose cache-safe params for background summarization (prompt cache sharing) + if (onCacheSafeParams) { + onCacheSafeParams({ + systemPrompt: agentSystemPrompt, + userContext: resolvedUserContext, + systemContext: resolvedSystemContext, + toolUseContext: agentToolUseContext, + forkContextMessages: initialMessages, + }) + } + + // Record initial messages before the query loop starts, plus the agentType + // so resume can route correctly when subagent_type is omitted. Both writes + // are fire-and-forget — persistence failure shouldn't block the agent. + void recordSidechainTranscript(initialMessages, agentId).catch(_err => + logForDebugging(`Failed to record sidechain transcript: ${_err}`), + ) + void writeAgentMetadata(agentId, { + agentType: agentDefinition.agentType, + ...(worktreePath && { worktreePath }), + ...(description && { description }), + }).catch(_err => logForDebugging(`Failed to write agent metadata: ${_err}`)) + + // Track the last recorded message UUID for parent chain continuity + let lastRecordedUuid: UUID | null = initialMessages.at(-1)?.uuid ?? null + + try { + for await (const message of query({ + messages: initialMessages, + systemPrompt: agentSystemPrompt, + userContext: resolvedUserContext, + systemContext: resolvedSystemContext, + canUseTool, + toolUseContext: agentToolUseContext, + querySource, + maxTurns: maxTurns ?? agentDefinition.maxTurns, + })) { + onQueryProgress?.() + // Forward subagent API request starts to parent's metrics display + // so TTFT/OTPS update during subagent execution. + if ( + message.type === 'stream_event' && + message.event.type === 'message_start' && + message.ttftMs != null + ) { + toolUseContext.pushApiMetricsEntry?.(message.ttftMs) + continue + } + + // Yield attachment messages (e.g., structured_output) without recording them + if (message.type === 'attachment') { + // Handle max turns reached signal from query.ts + if (message.attachment.type === 'max_turns_reached') { + logForDebugging( + `[Agent +: $ +{ + agentDefinition.agentType +} +] Reached max turns limit ($ +{ + message.attachment.maxTurns +} +)`, + ) + break + } + yield message + continue + } + + if (isRecordableMessage(message)) { + // Record only the new message with correct parent (O(1) per message) + await recordSidechainTranscript( + [message], + agentId, + lastRecordedUuid, + ).catch(err => + logForDebugging(`Failed to record sidechain transcript: ${err}`), + ) + if (message.type !== 'progress') { + lastRecordedUuid = message.uuid + } + yield message + } + } + + if (agentAbortController.signal.aborted) { + throw new AbortError() + } + + // Run callback if provided (only built-in agents have callbacks) + if (isBuiltInAgent(agentDefinition) && agentDefinition.callback) { + agentDefinition.callback() + } + } finally { + // Clean up agent-specific MCP servers (runs on normal completion, abort, or error) + await mcpCleanup() + // Clean up agent's session hooks + if (agentDefinition.hooks) { + clearSessionHooks(rootSetAppState, agentId) + } + // Clean up prompt cache tracking state for this agent + if (feature('PROMPT_CACHE_BREAK_DETECTION')) { + cleanupAgentTracking(agentId) + } + // Release cloned file state cache memory + agentToolUseContext.readFileState.clear() + // Release the cloned fork context messages + initialMessages.length = 0 + // Release perfetto agent registry entry + unregisterPerfettoAgent(agentId) + // Release transcript subdir mapping + clearAgentTranscriptSubdir(agentId) + // Release this agent's todos entry. Without this, every subagent that + // called TodoWrite leaves a key in AppState.todos forever (even after all + // items complete, the value is [] but the key stays). Whale sessions + // spawn hundreds of agents; each orphaned key is a small leak that adds up. + rootSetAppState(prev => { + if (!(agentId in prev.todos)) return prev + const { [agentId]: _removed, ...todos } = prev.todos + return { ...prev, todos } + }) + // Kill any background bash tasks this agent spawned. Without this, a + // `run_in_background` shell loop (e.g. test fixture fake-logs.sh) outlives + // the agent as a PPID=1 zombie once the main session eventually exits. + killShellTasksForAgent(agentId, toolUseContext.getAppState, rootSetAppState) + /* eslint-disable @typescript-eslint/no-require-imports */ + if (feature('MONITOR_TOOL')) { + const mcpMod = + require('../../tasks/MonitorMcpTask/MonitorMcpTask.js') as typeof import('../../tasks/MonitorMcpTask/MonitorMcpTask.js') + mcpMod.killMonitorMcpTasksForAgent( + agentId, + toolUseContext.getAppState, + rootSetAppState, + ) + } + /* eslint-enable @typescript-eslint/no-require-imports */ + } +} + +/** + * Filters out assistant messages with incomplete tool calls (tool uses without results). + * This prevents API errors when sending messages with orphaned tool calls. + */ +export function filterIncompleteToolCalls(messages: Message[]): Message[] { + // Build a set of tool use IDs that have results + const toolUseIdsWithResults = new Set() + + for (const message of messages) { + if (message?.type === 'user') { + const userMessage = message as UserMessage + const content = userMessage.message.content + if (Array.isArray(content)) { + for (const block of content) { + if (block.type === 'tool_result' && block.tool_use_id) { + toolUseIdsWithResults.add(block.tool_use_id) + } + } + } + } + } + + // Filter out assistant messages that contain tool calls without results + return messages.filter(message => { + if (message?.type === 'assistant') { + const assistantMessage = message as AssistantMessage + const content = assistantMessage.message.content + if (Array.isArray(content)) { + // Check if this assistant message has any tool uses without results + const hasIncompleteToolCall = content.some( + block => + block.type === 'tool_use' && + block.id && + !toolUseIdsWithResults.has(block.id), + ) + // Exclude messages with incomplete tool calls + return !hasIncompleteToolCall + } + } + // Keep all non-assistant messages and assistant messages without tool calls + return true + }) +} + +async function getAgentSystemPrompt( + agentDefinition: AgentDefinition, + toolUseContext: Pick, + resolvedAgentModel: string, + additionalWorkingDirectories: string[], + resolvedTools: readonly Tool[], +): Promise { + const enabledToolNames = new Set(resolvedTools.map(t => t.name)) + try { + const agentPrompt = agentDefinition.getSystemPrompt({ toolUseContext }) + const prompts = [agentPrompt] + + return await enhanceSystemPromptWithEnvDetails( + prompts, + resolvedAgentModel, + additionalWorkingDirectories, + enabledToolNames, + ) + } catch (_error) { + return enhanceSystemPromptWithEnvDetails( + [DEFAULT_AGENT_PROMPT], + resolvedAgentModel, + additionalWorkingDirectories, + enabledToolNames, + ) + } +} + +/** + * Resolve a skill name from agent frontmatter to a registered command name. + * + * Plugin skills are registered with namespaced names (e.g., "my-plugin:my-skill") + * but agents reference them with bare names (e.g., "my-skill"). This function + * tries multiple resolution strategies: + * + * 1. Exact match via hasCommand (name, userFacingName, aliases) + * 2. Prefix with agent's plugin name (e.g., "my-skill" → "my-plugin:my-skill") + * 3. Suffix match — find any command whose name ends with ":skillName" + */ +function resolveSkillName( + skillName: string, + allSkills: Command[], + agentDefinition: AgentDefinition, +): string | null { + // 1. Direct match + if (hasCommand(skillName, allSkills)) { + return skillName + } + + // 2. Try prefixing with the agent's plugin name + // Plugin agents have agentType like "pluginName:agentName" + const pluginPrefix = agentDefinition.agentType.split(':')[0] + if (pluginPrefix) { + const qualifiedName = `${pluginPrefix}:${skillName}` + if (hasCommand(qualifiedName, allSkills)) { + return qualifiedName + } + } + + // 3. Suffix match — find a skill whose name ends with ":skillName" + const suffix = `:${skillName}` + const match = allSkills.find(cmd => cmd.name.endsWith(suffix)) + if (match) { + return match.name + } + + return null +} diff --git a/_all-in-one-upload/cc-haha/src/tools/BashTool/bashPermissions.ts b/_all-in-one-upload/cc-haha/src/tools/BashTool/bashPermissions.ts new file mode 100644 index 0000000..83b3c63 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/BashTool/bashPermissions.ts @@ -0,0 +1,2621 @@ +import { feature } from 'bun:bundle' +import { APIUserAbortError } from '@anthropic-ai/sdk' +import type { z } from 'zod/v4' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../../services/analytics/growthbook.js' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../../services/analytics/index.js' +import type { ToolPermissionContext, ToolUseContext } from '../../Tool.js' +import type { PendingClassifierCheck } from '../../types/permissions.js' +import { count } from '../../utils/array.js' +import { + checkSemantics, + nodeTypeId, + type ParseForSecurityResult, + parseForSecurityFromAst, + type Redirect, + type SimpleCommand, +} from '../../utils/bash/ast.js' +import { + type CommandPrefixResult, + extractOutputRedirections, + getCommandSubcommandPrefix, + splitCommand_DEPRECATED, +} from '../../utils/bash/commands.js' +import { parseCommandRaw } from '../../utils/bash/parser.js' +import { tryParseShellCommand } from '../../utils/bash/shellQuote.js' +import { getCwd } from '../../utils/cwd.js' +import { logForDebugging } from '../../utils/debug.js' +import { isEnvTruthy } from '../../utils/envUtils.js' +import { AbortError } from '../../utils/errors.js' +import type { + ClassifierBehavior, + ClassifierResult, +} from '../../utils/permissions/bashClassifier.js' +import { + classifyBashCommand, + getBashPromptAllowDescriptions, + getBashPromptAskDescriptions, + getBashPromptDenyDescriptions, + isClassifierPermissionsEnabled, +} from '../../utils/permissions/bashClassifier.js' +import type { + PermissionDecisionReason, + PermissionResult, +} from '../../utils/permissions/PermissionResult.js' +import type { + PermissionRule, + PermissionRuleValue, +} from '../../utils/permissions/PermissionRule.js' +import { extractRules } from '../../utils/permissions/PermissionUpdate.js' +import type { PermissionUpdate } from '../../utils/permissions/PermissionUpdateSchema.js' +import { permissionRuleValueToString } from '../../utils/permissions/permissionRuleParser.js' +import { + createPermissionRequestMessage, + getRuleByContentsForTool, +} from '../../utils/permissions/permissions.js' +import { + parsePermissionRule, + type ShellPermissionRule, + matchWildcardPattern as sharedMatchWildcardPattern, + permissionRuleExtractPrefix as sharedPermissionRuleExtractPrefix, + suggestionForExactCommand as sharedSuggestionForExactCommand, + suggestionForPrefix as sharedSuggestionForPrefix, +} from '../../utils/permissions/shellRuleMatching.js' +import { getPlatform } from '../../utils/platform.js' +import { SandboxManager } from '../../utils/sandbox/sandbox-adapter.js' +import { jsonStringify } from '../../utils/slowOperations.js' +import { windowsPathToPosixPath } from '../../utils/windowsPaths.js' +import { BashTool } from './BashTool.js' +import { checkCommandOperatorPermissions } from './bashCommandHelpers.js' +import { + bashCommandIsSafeAsync_DEPRECATED, + stripSafeHeredocSubstitutions, +} from './bashSecurity.js' +import { checkPermissionMode } from './modeValidation.js' +import { checkPathConstraints } from './pathValidation.js' +import { checkSedConstraints } from './sedValidation.js' +import { shouldUseSandbox } from './shouldUseSandbox.js' + +// DCE cliff: Bun's feature() evaluator has a per-function complexity budget. +// bashToolHasPermission is right at the limit. `import { X as Y }` aliases +// inside the import block count toward this budget; when they push it over +// the threshold Bun can no longer prove feature('BASH_CLASSIFIER') is a +// constant and silently evaluates the ternaries to `false`, dropping every +// pendingClassifierCheck spread. Keep aliases as top-level const rebindings +// instead. (See also the comment on checkSemanticsDeny below.) +const bashCommandIsSafeAsync = bashCommandIsSafeAsync_DEPRECATED +const splitCommand = splitCommand_DEPRECATED + +// Env-var assignment prefix (VAR=value). Shared across three while-loops that +// skip safe env vars before extracting the command name. +const ENV_VAR_ASSIGN_RE = /^[A-Za-z_]\w*=/ + +// CC-643: On complex compound commands, splitCommand_DEPRECATED can produce a +// very large subcommands array (possible exponential growth; #21405's ReDoS fix +// may have been incomplete). Each subcommand then runs tree-sitter parse + +// ~20 validators + logEvent (bashSecurity.ts), and with memoized metadata the +// resulting microtask chain starves the event loop — REPL freeze at 100% CPU, +// strace showed /proc/self/stat reads at ~127Hz with no epoll_wait. Fifty is +// generous: legitimate user commands don't split that wide. Above the cap we +// fall back to 'ask' (safe default — we can't prove safety, so we prompt). +export const MAX_SUBCOMMANDS_FOR_SECURITY_CHECK = 50 + +// GH#11380: Cap the number of per-subcommand rules suggested for compound +// commands. Beyond this, the "Yes, and don't ask again for X, Y, Z…" label +// degrades to "similar commands" anyway, and saving 10+ rules from one prompt +// is more likely noise than intent. Users chaining this many write commands +// in one && list are rare; they can always approve once and add rules manually. +export const MAX_SUGGESTED_RULES_FOR_COMPOUND = 5 + +/** + * [ANT-ONLY] Log classifier evaluation results for analysis. + * This helps us understand which classifier rules are being evaluated + * and how the classifier is deciding on commands. + */ +function logClassifierResultForAnts( + command: string, + behavior: ClassifierBehavior, + descriptions: string[], + result: ClassifierResult, +): void { + if (process.env.USER_TYPE !== 'ant') { + return + } + + logEvent('tengu_internal_bash_classifier_result', { + behavior: + behavior as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + descriptions: jsonStringify( + descriptions, + ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + matches: result.matches, + matchedDescription: (result.matchedDescription ?? + '') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + confidence: + result.confidence as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + reason: + result.reason as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + // Note: command contains code/filepaths - this is ANT-ONLY so it's OK + command: + command as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }) +} + +/** + * Extract a stable command prefix (command + subcommand) from a raw command string. + * Skips leading env var assignments only if they are in SAFE_ENV_VARS (or + * ANT_ONLY_SAFE_ENV_VARS for ant users). Returns null if a non-safe env var is + * encountered (to fall back to exact match), or if the second token doesn't look + * like a subcommand (lowercase alphanumeric, e.g., "commit", "run"). + * + * Examples: + * 'git commit -m "fix typo"' → 'git commit' + * 'NODE_ENV=prod npm run build' → 'npm run' (NODE_ENV is safe) + * 'MY_VAR=val npm run build' → null (MY_VAR is not safe) + * 'ls -la' → null (flag, not a subcommand) + * 'cat file.txt' → null (filename, not a subcommand) + * 'chmod 755 file' → null (number, not a subcommand) + */ +export function getSimpleCommandPrefix(command: string): string | null { + const tokens = command.trim().split(/\s+/).filter(Boolean) + if (tokens.length === 0) return null + + // Skip env var assignments (VAR=value) at the start, but only if they are + // in SAFE_ENV_VARS (or ANT_ONLY_SAFE_ENV_VARS for ant users). If a non-safe + // env var is encountered, return null to fall back to exact match. This + // prevents generating prefix rules like Bash(npm run:*) that can never match + // at allow-rule check time, because stripSafeWrappers only strips safe vars. + let i = 0 + while (i < tokens.length && ENV_VAR_ASSIGN_RE.test(tokens[i]!)) { + const varName = tokens[i]!.split('=')[0]! + const isAntOnlySafe = + process.env.USER_TYPE === 'ant' && ANT_ONLY_SAFE_ENV_VARS.has(varName) + if (!SAFE_ENV_VARS.has(varName) && !isAntOnlySafe) { + return null + } + i++ + } + + const remaining = tokens.slice(i) + if (remaining.length < 2) return null + const subcmd = remaining[1]! + // Second token must look like a subcommand (e.g., "commit", "run", "compose"), + // not a flag (-rf), filename (file.txt), path (/tmp), URL, or number (755). + if (!/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(subcmd)) return null + return remaining.slice(0, 2).join(' ') +} + +// Bare-prefix suggestions like `bash:*` or `sh:*` would allow arbitrary code +// via `-c`. Wrapper suggestions like `env:*` or `sudo:*` would do the same: +// `env` is NOT in SAFE_WRAPPER_PATTERNS, so `env bash -c "evil"` survives +// stripSafeWrappers unchanged and hits the startsWith("env ") check at +// the prefix-rule matcher. Shell list mirrors DANGEROUS_SHELL_PREFIXES in +// src/utils/shell/prefix.ts which guarded the old Haiku extractor. +const BARE_SHELL_PREFIXES = new Set([ + 'sh', + 'bash', + 'zsh', + 'fish', + 'csh', + 'tcsh', + 'ksh', + 'dash', + 'cmd', + 'powershell', + 'pwsh', + // wrappers that exec their args as a command + 'env', + 'xargs', + // SECURITY: checkSemantics (ast.ts) strips these wrappers to check the + // wrapped command. Suggesting `Bash(nice:*)` would be ≈ `Bash(*)` — users + // would add it after a prompt, then `nice rm -rf /` passes semantics while + // deny/cd+git gates see 'nice' (SAFE_WRAPPER_PATTERNS below didn't strip + // bare `nice` until this fix). Block these from ever being suggested. + 'nice', + 'stdbuf', + 'nohup', + 'timeout', + 'time', + // privilege escalation — sudo:* from `sudo -u foo ...` would auto-approve + // any future sudo invocation + 'sudo', + 'doas', + 'pkexec', +]) + +/** + * UI-only fallback: extract the first word alone when getSimpleCommandPrefix + * declines. In external builds TREE_SITTER_BASH is off, so the async + * tree-sitter refinement in BashPermissionRequest never fires — without this, + * pipes and compounds (`python3 file.py 2>&1 | tail -20`) dump into the + * editable field verbatim. + * + * Deliberately not used by suggestionForExactCommand: a backend-suggested + * `Bash(rm:*)` is too broad to auto-generate, but as an editable starting + * point it's what users expect (Slack C07VBSHV7EV/p1772670433193449). + * + * Reuses the same SAFE_ENV_VARS gate as getSimpleCommandPrefix — a rule like + * `Bash(python3:*)` can never match `RUN=/path python3 ...` at check time + * because stripSafeWrappers won't strip RUN. + */ +export function getFirstWordPrefix(command: string): string | null { + const tokens = command.trim().split(/\s+/).filter(Boolean) + + let i = 0 + while (i < tokens.length && ENV_VAR_ASSIGN_RE.test(tokens[i]!)) { + const varName = tokens[i]!.split('=')[0]! + const isAntOnlySafe = + process.env.USER_TYPE === 'ant' && ANT_ONLY_SAFE_ENV_VARS.has(varName) + if (!SAFE_ENV_VARS.has(varName) && !isAntOnlySafe) { + return null + } + i++ + } + + const cmd = tokens[i] + if (!cmd) return null + // Same shape check as the subcommand regex in getSimpleCommandPrefix: + // rejects paths (./script.sh, /usr/bin/python), flags, numbers, filenames. + if (!/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(cmd)) return null + if (BARE_SHELL_PREFIXES.has(cmd)) return null + return cmd +} + +function suggestionForExactCommand(command: string): PermissionUpdate[] { + // Heredoc commands contain multi-line content that changes each invocation, + // making exact-match rules useless (they'll never match again). Extract a + // stable prefix before the heredoc operator and suggest a prefix rule instead. + const heredocPrefix = extractPrefixBeforeHeredoc(command) + if (heredocPrefix) { + return sharedSuggestionForPrefix(BashTool.name, heredocPrefix) + } + + // Multiline commands without heredoc also make poor exact-match rules. + // Saving the full multiline text can produce patterns containing `:*` in + // the middle, which fails permission validation and corrupts the settings + // file. Use the first line as a prefix rule instead. + if (command.includes('\n')) { + const firstLine = command.split('\n')[0]!.trim() + if (firstLine) { + return sharedSuggestionForPrefix(BashTool.name, firstLine) + } + } + + // Single-line commands: extract a 2-word prefix for reusable rules. + // Without this, exact-match rules are saved that never match future + // invocations with different arguments. + const prefix = getSimpleCommandPrefix(command) + if (prefix) { + return sharedSuggestionForPrefix(BashTool.name, prefix) + } + + return sharedSuggestionForExactCommand(BashTool.name, command) +} + +/** + * If the command contains a heredoc (<<), extract the command prefix before it. + * Returns the first word(s) before the heredoc operator as a stable prefix, + * or null if the command doesn't contain a heredoc. + * + * Examples: + * 'git commit -m "$(cat <<\'EOF\'\n...\nEOF\n)"' → 'git commit' + * 'cat <= tokens.length) return null + return tokens.slice(i, i + 2).join(' ') || null +} + +function suggestionForPrefix(prefix: string): PermissionUpdate[] { + return sharedSuggestionForPrefix(BashTool.name, prefix) +} + +/** + * Extract prefix from legacy :* syntax (e.g., "npm:*" -> "npm") + * Delegates to shared implementation. + */ +export const permissionRuleExtractPrefix = sharedPermissionRuleExtractPrefix + +/** + * Match a command against a wildcard pattern (case-sensitive for Bash). + * Delegates to shared implementation. + */ +export function matchWildcardPattern( + pattern: string, + command: string, +): boolean { + return sharedMatchWildcardPattern(pattern, command) +} + +/** + * Parse a permission rule into a structured rule object. + * Delegates to shared implementation. + */ +export const bashPermissionRule: ( + permissionRule: string, +) => ShellPermissionRule = parsePermissionRule + +/** + * Whitelist of environment variables that are safe to strip from commands. + * These variables CANNOT execute code or load libraries. + * + * SECURITY: These must NEVER be added to the whitelist: + * - PATH, LD_PRELOAD, LD_LIBRARY_PATH, DYLD_* (execution/library loading) + * - PYTHONPATH, NODE_PATH, CLASSPATH, RUBYLIB (module loading) + * - GOFLAGS, RUSTFLAGS, NODE_OPTIONS (can contain code execution flags) + * - HOME, TMPDIR, SHELL, BASH_ENV (affect system behavior) + */ +const SAFE_ENV_VARS = new Set([ + // Go - build/runtime settings only + 'GOEXPERIMENT', // experimental features + 'GOOS', // target OS + 'GOARCH', // target architecture + 'CGO_ENABLED', // enable/disable CGO + 'GO111MODULE', // module mode + + // Rust - logging/debugging only + 'RUST_BACKTRACE', // backtrace verbosity + 'RUST_LOG', // logging filter + + // Node - environment name only (not NODE_OPTIONS!) + 'NODE_ENV', + + // Python - behavior flags only (not PYTHONPATH!) + 'PYTHONUNBUFFERED', // disable buffering + 'PYTHONDONTWRITEBYTECODE', // no .pyc files + + // Pytest - test configuration + 'PYTEST_DISABLE_PLUGIN_AUTOLOAD', // disable plugin loading + 'PYTEST_DEBUG', // debug output + + // API keys and authentication + 'ANTHROPIC_API_KEY', // API authentication + + // Locale and character encoding + 'LANG', // default locale + 'LANGUAGE', // language preference list + 'LC_ALL', // override all locale settings + 'LC_CTYPE', // character classification + 'LC_TIME', // time format + 'CHARSET', // character set preference + + // Terminal and display + 'TERM', // terminal type + 'COLORTERM', // color terminal indicator + 'NO_COLOR', // disable color output (universal standard) + 'FORCE_COLOR', // force color output + 'TZ', // timezone + + // Color configuration for various tools + 'LS_COLORS', // colors for ls (GNU) + 'LSCOLORS', // colors for ls (BSD/macOS) + 'GREP_COLOR', // grep match color (deprecated) + 'GREP_COLORS', // grep color scheme + 'GCC_COLORS', // GCC diagnostic colors + + // Display formatting + 'TIME_STYLE', // time display format for ls + 'BLOCK_SIZE', // block size for du/df + 'BLOCKSIZE', // alternative block size +]) + +/** + * ANT-ONLY environment variables that are safe to strip from commands. + * These are only enabled when USER_TYPE === 'ant'. + * + * SECURITY: These env vars are stripped before permission-rule matching, which + * means `DOCKER_HOST=tcp://evil.com docker ps` matches a `Bash(docker ps:*)` + * rule after stripping. This is INTENTIONALLY ANT-ONLY (gated at line ~380) + * and MUST NEVER ship to external users. DOCKER_HOST redirects the Docker + * daemon endpoint — stripping it defeats prefix-based permission restrictions + * by hiding the network endpoint from the permission check. KUBECONFIG + * similarly controls which cluster kubectl talks to. These are convenience + * strippings for internal power users who accept the risk. + * + * Based on analysis of 30 days of tengu_internal_bash_tool_use_permission_request events. + */ +const ANT_ONLY_SAFE_ENV_VARS = new Set([ + // Kubernetes and container config (config file pointers, not execution) + 'KUBECONFIG', // kubectl config file path — controls which cluster kubectl uses + 'DOCKER_HOST', // Docker daemon socket/endpoint — controls which daemon docker talks to + + // Cloud provider project/profile selection (just names/identifiers) + 'AWS_PROFILE', // AWS profile name selection + 'CLOUDSDK_CORE_PROJECT', // GCP project ID + 'CLUSTER', // generic cluster name + + // Anthropic internal cluster selection (just names/identifiers) + 'COO_CLUSTER', // coo cluster name + 'COO_CLUSTER_NAME', // coo cluster name (alternate) + 'COO_NAMESPACE', // coo namespace + 'COO_LAUNCH_YAML_DRY_RUN', // dry run mode + + // Feature flags (boolean/string flags only) + 'SKIP_NODE_VERSION_CHECK', // skip version check + 'EXPECTTEST_ACCEPT', // accept test expectations + 'CI', // CI environment indicator + 'GIT_LFS_SKIP_SMUDGE', // skip LFS downloads + + // GPU/Device selection (just device IDs) + 'CUDA_VISIBLE_DEVICES', // GPU device selection + 'JAX_PLATFORMS', // JAX platform selection + + // Display/terminal settings + 'COLUMNS', // terminal width + 'TMUX', // TMUX socket info + + // Test/debug configuration + 'POSTGRESQL_VERSION', // postgres version string + 'FIRESTORE_EMULATOR_HOST', // emulator host:port + 'HARNESS_QUIET', // quiet mode flag + 'TEST_CROSSCHECK_LISTS_MATCH_UPDATE', // test update flag + 'DBT_PER_DEVELOPER_ENVIRONMENTS', // DBT config + 'STATSIG_FORD_DB_CHECKS', // statsig DB check flag + + // Build configuration + 'ANT_ENVIRONMENT', // Anthropic environment name + 'ANT_SERVICE', // Anthropic service name + 'MONOREPO_ROOT_DIR', // monorepo root path + + // Version selectors + 'PYENV_VERSION', // Python version selection + + // Credentials (approved subset - these don't change exfil risk) + 'PGPASSWORD', // Postgres password + 'GH_TOKEN', // GitHub token + 'GROWTHBOOK_API_KEY', // self-hosted growthbook +]) + +/** + * Strips full-line comments from a command. + * This handles cases where Claude adds comments in bash commands, e.g.: + * "# Check the logs directory\nls /home/user/logs" + * Should be stripped to: "ls /home/user/logs" + * + * Only strips full-line comments (lines where the entire line is a comment), + * not inline comments that appear after a command on the same line. + */ +function stripCommentLines(command: string): string { + const lines = command.split('\n') + const nonCommentLines = lines.filter(line => { + const trimmed = line.trim() + // Keep lines that are not empty and don't start with # + return trimmed !== '' && !trimmed.startsWith('#') + }) + + // If all lines were comments/empty, return original + if (nonCommentLines.length === 0) { + return command + } + + return nonCommentLines.join('\n') +} + +export function stripSafeWrappers(command: string): string { + // SECURITY: Use [ \t]+ not \s+ — \s matches \n/\r which are command + // separators in bash. Matching across a newline would strip the wrapper from + // one line and leave a different command on the next line for bash to execute. + // + // SECURITY: `(?:--[ \t]+)?` consumes the wrapper's own `--` so + // `nohup -- rm -- -/../foo` strips to `rm -- -/../foo` (not `-- rm ...` + // which would skip path validation with `--` as an unknown baseCmd). + const SAFE_WRAPPER_PATTERNS = [ + // timeout: enumerate GNU long flags — no-value (--foreground, + // --preserve-status, --verbose), value-taking in both =fused and + // space-separated forms (--kill-after=5, --kill-after 5, --signal=TERM, + // --signal TERM). Short: -v (no-arg), -k/-s with separate or fused value. + // SECURITY: flag VALUES use allowlist [A-Za-z0-9_.+-] (signals are + // TERM/KILL/9, durations are 5/5s/10.5). Previously [^ \t]+ matched + // $ ( ) ` | ; & — `timeout -k$(id) 10 ls` stripped to `ls`, matched + // Bash(ls:*), while bash expanded $(id) during word splitting BEFORE + // timeout ran. Contrast ENV_VAR_PATTERN below which already allowlists. + /^timeout[ \t]+(?:(?:--(?:foreground|preserve-status|verbose)|--(?:kill-after|signal)=[A-Za-z0-9_.+-]+|--(?:kill-after|signal)[ \t]+[A-Za-z0-9_.+-]+|-v|-[ks][ \t]+[A-Za-z0-9_.+-]+|-[ks][A-Za-z0-9_.+-]+)[ \t]+)*(?:--[ \t]+)?\d+(?:\.\d+)?[smhd]?[ \t]+/, + /^time[ \t]+(?:--[ \t]+)?/, + // SECURITY: keep in sync with checkSemantics wrapper-strip (ast.ts + // ~:1990-2080) AND stripWrappersFromArgv (pathValidation.ts ~:1260). + // Previously this pattern REQUIRED `-n N`; checkSemantics already handled + // bare `nice` and legacy `-N`. Asymmetry meant checkSemantics exposed the + // wrapped command to semantic checks but deny-rule matching and the cd+git + // gate saw the wrapper name. `nice rm -rf /` with Bash(rm:*) deny became + // ask instead of deny; `cd evil && nice git status` skipped the bare-repo + // RCE gate. PR #21503 fixed stripWrappersFromArgv; this was missed. + // Now matches: `nice cmd`, `nice -n N cmd`, `nice -N cmd` (all forms + // checkSemantics strips). + /^nice(?:[ \t]+-n[ \t]+-?\d+|[ \t]+-\d+)?[ \t]+(?:--[ \t]+)?/, + // stdbuf: fused short flags only (-o0, -eL). checkSemantics handles more + // (space-separated, long --output=MODE), but we fail-closed on those + // above so not over-stripping here is safe. Main need: `stdbuf -o0 cmd`. + /^stdbuf(?:[ \t]+-[ioe][LN0-9]+)+[ \t]+(?:--[ \t]+)?/, + /^nohup[ \t]+(?:--[ \t]+)?/, + ] as const + + // Pattern for environment variables: + // ^([A-Za-z_][A-Za-z0-9_]*) - Variable name (standard identifier) + // = - Equals sign + // ([A-Za-z0-9_./:-]+) - Value: alphanumeric + safe punctuation only + // [ \t]+ - Required HORIZONTAL whitespace after value + // + // SECURITY: Only matches unquoted values with safe characters (no $(), `, $var, ;|&). + // + // SECURITY: Trailing whitespace MUST be [ \t]+ (horizontal only), NOT \s+. + // \s matches \n/\r. If reconstructCommand emits an unquoted newline between + // `TZ=UTC` and `echo`, \s+ would match across it and strip `TZ=UTC`, + // leaving `echo curl evil.com` to match Bash(echo:*). But bash treats the + // newline as a command separator. Defense-in-depth with needsQuoting fix. + const ENV_VAR_PATTERN = /^([A-Za-z_][A-Za-z0-9_]*)=([A-Za-z0-9_./:-]+)[ \t]+/ + + let stripped = command + let previousStripped = '' + + // Phase 1: Strip leading env vars and comments only. + // In bash, env var assignments before a command (VAR=val cmd) are genuine + // shell-level assignments. These are safe to strip for permission matching. + while (stripped !== previousStripped) { + previousStripped = stripped + stripped = stripCommentLines(stripped) + + const envVarMatch = stripped.match(ENV_VAR_PATTERN) + if (envVarMatch) { + const varName = envVarMatch[1]! + const isAntOnlySafe = + process.env.USER_TYPE === 'ant' && ANT_ONLY_SAFE_ENV_VARS.has(varName) + if (SAFE_ENV_VARS.has(varName) || isAntOnlySafe) { + stripped = stripped.replace(ENV_VAR_PATTERN, '') + } + } + } + + // Phase 2: Strip wrapper commands and comments only. Do NOT strip env vars. + // Wrapper commands (timeout, time, nice, nohup) use execvp to run their + // arguments, so VAR=val after a wrapper is treated as the COMMAND to execute, + // not as an env var assignment. Stripping env vars here would create a + // mismatch between what the parser sees and what actually executes. + // (HackerOne #3543050) + previousStripped = '' + while (stripped !== previousStripped) { + previousStripped = stripped + stripped = stripCommentLines(stripped) + + for (const pattern of SAFE_WRAPPER_PATTERNS) { + stripped = stripped.replace(pattern, '') + } + } + + return stripped.trim() +} + +// SECURITY: allowlist for timeout flag VALUES (signals are TERM/KILL/9, +// durations are 5/5s/10.5). Rejects $ ( ) ` | ; & and newlines that +// previously matched via [^ \t]+ — `timeout -k$(id) 10 ls` must NOT strip. +const TIMEOUT_FLAG_VALUE_RE = /^[A-Za-z0-9_.+-]+$/ + +/** + * Parse timeout's GNU flags (long + short, fused + space-separated) and + * return the argv index of the DURATION token, or -1 if flags are unparseable. + * Enumerates: --foreground/--preserve-status/--verbose (no value), + * --kill-after/--signal (value, both =fused and space-separated), -v (no + * value), -k/-s (value, both fused and space-separated). + * + * Extracted from stripWrappersFromArgv to keep bashToolHasPermission under + * Bun's feature() DCE complexity threshold — inlining this breaks + * feature('BASH_CLASSIFIER') evaluation in classifier tests. + */ +function skipTimeoutFlags(a: readonly string[]): number { + let i = 1 + while (i < a.length) { + const arg = a[i]! + const next = a[i + 1] + if ( + arg === '--foreground' || + arg === '--preserve-status' || + arg === '--verbose' + ) + i++ + else if (/^--(?:kill-after|signal)=[A-Za-z0-9_.+-]+$/.test(arg)) i++ + else if ( + (arg === '--kill-after' || arg === '--signal') && + next && + TIMEOUT_FLAG_VALUE_RE.test(next) + ) + i += 2 + else if (arg === '--') { + i++ + break + } // end-of-options marker + else if (arg.startsWith('--')) return -1 + else if (arg === '-v') i++ + else if ( + (arg === '-k' || arg === '-s') && + next && + TIMEOUT_FLAG_VALUE_RE.test(next) + ) + i += 2 + else if (/^-[ks][A-Za-z0-9_.+-]+$/.test(arg)) i++ + else if (arg.startsWith('-')) return -1 + else break + } + return i +} + +/** + * Argv-level counterpart to stripSafeWrappers. Strips the same wrapper + * commands (timeout, time, nice, nohup) from AST-derived argv. Env vars + * are already separated into SimpleCommand.envVars so no env-var stripping. + * + * KEEP IN SYNC with SAFE_WRAPPER_PATTERNS above — if you add a wrapper + * there, add it here too. + */ +export function stripWrappersFromArgv(argv: string[]): string[] { + // SECURITY: Consume optional `--` after wrapper options, matching what the + // wrapper does. Otherwise `['nohup','--','rm','--','-/../foo']` yields `--` + // as baseCmd and skips path validation. See SAFE_WRAPPER_PATTERNS comment. + let a = argv + for (;;) { + if (a[0] === 'time' || a[0] === 'nohup') { + a = a.slice(a[1] === '--' ? 2 : 1) + } else if (a[0] === 'timeout') { + const i = skipTimeoutFlags(a) + if (i < 0 || !a[i] || !/^\d+(?:\.\d+)?[smhd]?$/.test(a[i]!)) return a + a = a.slice(i + 1) + } else if ( + a[0] === 'nice' && + a[1] === '-n' && + a[2] && + /^-?\d+$/.test(a[2]) + ) { + a = a.slice(a[3] === '--' ? 4 : 3) + } else { + return a + } + } +} + +/** + * Env vars that make a *different binary* run (injection or resolution hijack). + * Heuristic only — export-&& form bypasses this, and excludedCommands isn't a + * security boundary anyway. + */ +export const BINARY_HIJACK_VARS = /^(LD_|DYLD_|PATH$)/ + +/** + * Strip ALL leading env var prefixes from a command, regardless of whether the + * var name is in the safe-list. + * + * Used for deny/ask rule matching: when a user denies `claude` or `rm`, the + * command should stay blocked even if prefixed with arbitrary env vars like + * `FOO=bar claude`. The safe-list restriction in stripSafeWrappers is correct + * for allow rules (prevents `DOCKER_HOST=evil docker ps` from auto-matching + * `Bash(docker ps:*)`), but deny rules must be harder to circumvent. + * + * Also used for sandbox.excludedCommands matching (not a security boundary — + * permission prompts are), with BINARY_HIJACK_VARS as a blocklist. + * + * SECURITY: Uses a broader value pattern than stripSafeWrappers. The value + * pattern excludes only actual shell injection characters ($, backtick, ;, |, + * &, parens, redirects, quotes, backslash) and whitespace. Characters like + * =, +, @, ~, , are harmless in unquoted env var assignment position and must + * be matched to prevent trivial bypass via e.g. `FOO=a=b denied_command`. + * + * @param blocklist - optional regex tested against each var name; matching vars + * are NOT stripped (and stripping stops there). Omit for deny rules; pass + * BINARY_HIJACK_VARS for excludedCommands. + */ +export function stripAllLeadingEnvVars( + command: string, + blocklist?: RegExp, +): string { + // Broader value pattern for deny-rule stripping. Handles: + // + // - Standard assignment (FOO=bar), append (FOO+=bar), array (FOO[0]=bar) + // - Single-quoted values: '[^'\n\r]*' — bash suppresses all expansion + // - Double-quoted values with backslash escapes: "(?:\\.|[^"$`\\\n\r])*" + // In bash double quotes, only \$, \`, \", \\, and \newline are special. + // Other \x sequences are harmless, so we allow \. inside double quotes. + // We still exclude raw $ and ` (without backslash) to block expansion. + // - Unquoted values: excludes shell metacharacters, allows backslash escapes + // - Concatenated segments: FOO='x'y"z" — bash concatenates adjacent segments + // + // SECURITY: Trailing whitespace MUST be [ \t]+ (horizontal only), NOT \s+. + // + // The outer * matches one atomic unit per iteration: a complete quoted + // string, a backslash-escape pair, or a single unquoted safe character. + // The inner double-quote alternation (?:...|...)* is bounded by the + // closing ", so it cannot interact with the outer * for backtracking. + // + // Note: $ is excluded from unquoted/double-quoted value classes to block + // dangerous forms like $(cmd), ${var}, and $((expr)). This means + // FOO=$VAR is not stripped — adding $VAR matching creates ReDoS risk + // (CodeQL #671) and $VAR bypasses are low-priority. + const ENV_VAR_PATTERN = + /^([A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?)\+?=(?:'[^'\n\r]*'|"(?:\\.|[^"$`\\\n\r])*"|\\.|[^ \t\n\r$`;|&()<>\\\\'"])*[ \t]+/ + + let stripped = command + let previousStripped = '' + + while (stripped !== previousStripped) { + previousStripped = stripped + stripped = stripCommentLines(stripped) + + const m = stripped.match(ENV_VAR_PATTERN) + if (!m) continue + if (blocklist?.test(m[1]!)) break + stripped = stripped.slice(m[0].length) + } + + return stripped.trim() +} + +function filterRulesByContentsMatchingInput( + input: z.infer, + rules: Map, + matchMode: 'exact' | 'prefix', + { + stripAllEnvVars = false, + skipCompoundCheck = false, + }: { stripAllEnvVars?: boolean; skipCompoundCheck?: boolean } = {}, +): PermissionRule[] { + const command = input.command.trim() + + // Strip output redirections for permission matching + // This allows rules like Bash(python:*) to match "python script.py > output.txt" + // Security validation of redirection targets happens separately in checkPathConstraints + const commandWithoutRedirections = + extractOutputRedirections(command).commandWithoutRedirections + + // For exact matching, try both the original command (to preserve quotes) + // and the command without redirections (to allow rules without redirections to match) + // For prefix matching, only use the command without redirections + const commandsForMatching = + matchMode === 'exact' + ? [command, commandWithoutRedirections] + : [commandWithoutRedirections] + + // Strip safe wrapper commands (timeout, time, nice, nohup) and env vars for matching + // This allows rules like Bash(npm install:*) to match "timeout 10 npm install foo" + // or "GOOS=linux go build" + const commandsToTry = commandsForMatching.flatMap(cmd => { + const strippedCommand = stripSafeWrappers(cmd) + return strippedCommand !== cmd ? [cmd, strippedCommand] : [cmd] + }) + + // SECURITY: For deny/ask rules, also try matching after stripping ALL leading + // env var prefixes. This prevents bypass via `FOO=bar denied_command` where + // FOO is not in the safe-list. The safe-list restriction in stripSafeWrappers + // is intentional for allow rules (see HackerOne #3543050), but deny rules + // must be harder to circumvent — a denied command should stay denied + // regardless of env var prefixes. + // + // We iteratively apply both stripping operations to all candidates until no + // new candidates are produced (fixed-point). This handles interleaved patterns + // like `nohup FOO=bar timeout 5 claude` where: + // 1. stripSafeWrappers strips `nohup` → `FOO=bar timeout 5 claude` + // 2. stripAllLeadingEnvVars strips `FOO=bar` → `timeout 5 claude` + // 3. stripSafeWrappers strips `timeout 5` → `claude` (deny match) + // + // Without iteration, single-pass compositions miss multi-layer interleaving. + if (stripAllEnvVars) { + const seen = new Set(commandsToTry) + let startIdx = 0 + + // Iterate until no new candidates are produced (fixed-point) + while (startIdx < commandsToTry.length) { + const endIdx = commandsToTry.length + for (let i = startIdx; i < endIdx; i++) { + const cmd = commandsToTry[i] + if (!cmd) { + continue + } + // Try stripping env vars + const envStripped = stripAllLeadingEnvVars(cmd) + if (!seen.has(envStripped)) { + commandsToTry.push(envStripped) + seen.add(envStripped) + } + // Try stripping safe wrappers + const wrapperStripped = stripSafeWrappers(cmd) + if (!seen.has(wrapperStripped)) { + commandsToTry.push(wrapperStripped) + seen.add(wrapperStripped) + } + } + startIdx = endIdx + } + } + + // Precompute compound-command status for each candidate to avoid re-parsing + // inside the rule filter loop (which would scale splitCommand calls with + // rules.length × commandsToTry.length). The compound check only applies to + // prefix/wildcard matching in 'prefix' mode, and only for allow rules. + // SECURITY: deny/ask rules must match compound commands so they can't be + // bypassed by wrapping a denied command in a compound expression. + const isCompoundCommand = new Map() + if (matchMode === 'prefix' && !skipCompoundCheck) { + for (const cmd of commandsToTry) { + if (!isCompoundCommand.has(cmd)) { + isCompoundCommand.set(cmd, splitCommand(cmd).length > 1) + } + } + } + + return Array.from(rules.entries()) + .filter(([ruleContent]) => { + const bashRule = bashPermissionRule(ruleContent) + + return commandsToTry.some(cmdToMatch => { + switch (bashRule.type) { + case 'exact': + return bashRule.command === cmdToMatch + case 'prefix': + switch (matchMode) { + // In 'exact' mode, only return true if the command exactly matches the prefix rule + case 'exact': + return bashRule.prefix === cmdToMatch + case 'prefix': { + // SECURITY: Don't allow prefix rules to match compound commands. + // e.g., Bash(cd:*) must NOT match "cd /path && python3 evil.py". + // In the normal flow commands are split before reaching here, but + // shell escaping can defeat the first splitCommand pass — e.g., + // cd src\&\& python3 hello.py → splitCommand → ["cd src&& python3 hello.py"] + // which then looks like a single command that starts with "cd ". + // Re-splitting the candidate here catches those cases. + if (isCompoundCommand.get(cmdToMatch)) { + return false + } + // Ensure word boundary: prefix must be followed by space or end of string + // This prevents "ls:*" from matching "lsof" or "lsattr" + if (cmdToMatch === bashRule.prefix) { + return true + } + if (cmdToMatch.startsWith(bashRule.prefix + ' ')) { + return true + } + // Also match "xargs " for bare xargs with no flags. + // This allows Bash(grep:*) to match "xargs grep pattern", + // and deny rules like Bash(rm:*) to block "xargs rm file". + // Natural word-boundary: "xargs -n1 grep" does NOT start with + // "xargs grep " so flagged xargs invocations are not matched. + const xargsPrefix = 'xargs ' + bashRule.prefix + if (cmdToMatch === xargsPrefix) { + return true + } + return cmdToMatch.startsWith(xargsPrefix + ' ') + } + } + break + case 'wildcard': + // SECURITY FIX: In exact match mode, wildcards must NOT match because we're + // checking the full unparsed command. Wildcard matching on unparsed commands + // allows "foo *" to match "foo arg && curl evil.com" since .* matches operators. + // Wildcards should only match after splitting into individual subcommands. + if (matchMode === 'exact') { + return false + } + // SECURITY: Same as for prefix rules, don't allow wildcard rules to match + // compound commands in prefix mode. e.g., Bash(cd *) must not match + // "cd /path && python3 evil.py" even though "cd *" pattern would match it. + if (isCompoundCommand.get(cmdToMatch)) { + return false + } + // In prefix mode (after splitting), wildcards can safely match subcommands + return matchWildcardPattern(bashRule.pattern, cmdToMatch) + } + }) + }) + .map(([, rule]) => rule) +} + +function matchingRulesForInput( + input: z.infer, + toolPermissionContext: ToolPermissionContext, + matchMode: 'exact' | 'prefix', + { skipCompoundCheck = false }: { skipCompoundCheck?: boolean } = {}, +) { + const denyRuleByContents = getRuleByContentsForTool( + toolPermissionContext, + BashTool, + 'deny', + ) + // SECURITY: Deny/ask rules use aggressive env var stripping so that + // `FOO=bar denied_command` still matches a deny rule for `denied_command`. + const matchingDenyRules = filterRulesByContentsMatchingInput( + input, + denyRuleByContents, + matchMode, + { stripAllEnvVars: true, skipCompoundCheck: true }, + ) + + const askRuleByContents = getRuleByContentsForTool( + toolPermissionContext, + BashTool, + 'ask', + ) + const matchingAskRules = filterRulesByContentsMatchingInput( + input, + askRuleByContents, + matchMode, + { stripAllEnvVars: true, skipCompoundCheck: true }, + ) + + const allowRuleByContents = getRuleByContentsForTool( + toolPermissionContext, + BashTool, + 'allow', + ) + const matchingAllowRules = filterRulesByContentsMatchingInput( + input, + allowRuleByContents, + matchMode, + { skipCompoundCheck }, + ) + + return { + matchingDenyRules, + matchingAskRules, + matchingAllowRules, + } +} + +/** + * Checks if the subcommand is an exact match for a permission rule + */ +export const bashToolCheckExactMatchPermission = ( + input: z.infer, + toolPermissionContext: ToolPermissionContext, +): PermissionResult => { + const command = input.command.trim() + const { matchingDenyRules, matchingAskRules, matchingAllowRules } = + matchingRulesForInput(input, toolPermissionContext, 'exact') + + // 1. Deny if exact command was denied + if (matchingDenyRules[0] !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${command} has been denied.`, + decisionReason: { + type: 'rule', + rule: matchingDenyRules[0], + }, + } + } + + // 2. Ask if exact command was in ask rules + if (matchingAskRules[0] !== undefined) { + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name), + decisionReason: { + type: 'rule', + rule: matchingAskRules[0], + }, + } + } + + // 3. Allow if exact command was allowed + if (matchingAllowRules[0] !== undefined) { + return { + behavior: 'allow', + updatedInput: input, + decisionReason: { + type: 'rule', + rule: matchingAllowRules[0], + }, + } + } + + // 4. Otherwise, passthrough + const decisionReason = { + type: 'other' as const, + reason: 'This command requires approval', + } + return { + behavior: 'passthrough', + message: createPermissionRequestMessage(BashTool.name, decisionReason), + decisionReason, + // Suggest exact match rule to user + // this may be overridden by prefix suggestions in `checkCommandAndSuggestRules()` + suggestions: suggestionForExactCommand(command), + } +} + +export const bashToolCheckPermission = ( + input: z.infer, + toolPermissionContext: ToolPermissionContext, + compoundCommandHasCd?: boolean, + astCommand?: SimpleCommand, +): PermissionResult => { + const command = input.command.trim() + + // 1. Check exact match first + const exactMatchResult = bashToolCheckExactMatchPermission( + input, + toolPermissionContext, + ) + + // 1a. Deny/ask if exact command has a rule + if ( + exactMatchResult.behavior === 'deny' || + exactMatchResult.behavior === 'ask' + ) { + return exactMatchResult + } + + // 2. Find all matching rules (prefix or exact) + // SECURITY FIX: Check Bash deny/ask rules BEFORE path constraints to prevent bypass + // via absolute paths outside the project directory (HackerOne report) + // When AST-parsed, the subcommand is already atomic — skip the legacy + // splitCommand re-check that misparses mid-word # as compound. + const { matchingDenyRules, matchingAskRules, matchingAllowRules } = + matchingRulesForInput(input, toolPermissionContext, 'prefix', { + skipCompoundCheck: astCommand !== undefined, + }) + + // 2a. Deny if command has a deny rule + if (matchingDenyRules[0] !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${command} has been denied.`, + decisionReason: { + type: 'rule', + rule: matchingDenyRules[0], + }, + } + } + + // 2b. Ask if command has an ask rule + if (matchingAskRules[0] !== undefined) { + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name), + decisionReason: { + type: 'rule', + rule: matchingAskRules[0], + }, + } + } + + // 3. Check path constraints + // This check comes after deny/ask rules so explicit rules take precedence. + // SECURITY: When AST-derived argv is available for this subcommand, pass + // it through so checkPathConstraints uses it directly instead of re-parsing + // with shell-quote (which has a single-quote backslash bug that causes + // parseCommandArguments to return [] and silently skip path validation). + const pathResult = checkPathConstraints( + input, + getCwd(), + toolPermissionContext, + compoundCommandHasCd, + astCommand?.redirects, + astCommand ? [astCommand] : undefined, + ) + if (pathResult.behavior !== 'passthrough') { + return pathResult + } + + // 4. Allow if command had an exact match allow + if (exactMatchResult.behavior === 'allow') { + return exactMatchResult + } + + // 5. Allow if command has an allow rule + if (matchingAllowRules[0] !== undefined) { + return { + behavior: 'allow', + updatedInput: input, + decisionReason: { + type: 'rule', + rule: matchingAllowRules[0], + }, + } + } + + // 5b. Check sed constraints (blocks dangerous sed operations before mode auto-allow) + const sedConstraintResult = checkSedConstraints(input, toolPermissionContext) + if (sedConstraintResult.behavior !== 'passthrough') { + return sedConstraintResult + } + + // 6. Check for mode-specific permission handling + const modeResult = checkPermissionMode(input, toolPermissionContext) + if (modeResult.behavior !== 'passthrough') { + return modeResult + } + + // 7. Check read-only rules + if (BashTool.isReadOnly(input)) { + return { + behavior: 'allow', + updatedInput: input, + decisionReason: { + type: 'other', + reason: 'Read-only command is allowed', + }, + } + } + + // 8. Passthrough since no rules match, will trigger permission prompt + const decisionReason = { + type: 'other' as const, + reason: 'This command requires approval', + } + return { + behavior: 'passthrough', + message: createPermissionRequestMessage(BashTool.name, decisionReason), + decisionReason, + // Suggest exact match rule to user + // this may be overridden by prefix suggestions in `checkCommandAndSuggestRules()` + suggestions: suggestionForExactCommand(command), + } +} + +/** + * Processes an individual subcommand and applies prefix checks & suggestions + */ +export async function checkCommandAndSuggestRules( + input: z.infer, + toolPermissionContext: ToolPermissionContext, + commandPrefixResult: CommandPrefixResult | null | undefined, + compoundCommandHasCd?: boolean, + astParseSucceeded?: boolean, +): Promise { + // 1. Check exact match first + const exactMatchResult = bashToolCheckExactMatchPermission( + input, + toolPermissionContext, + ) + if (exactMatchResult.behavior !== 'passthrough') { + return exactMatchResult + } + + // 2. Check the command prefix + const permissionResult = bashToolCheckPermission( + input, + toolPermissionContext, + compoundCommandHasCd, + ) + // 2a. Deny/ask if command was explictly denied/asked + if ( + permissionResult.behavior === 'deny' || + permissionResult.behavior === 'ask' + ) { + return permissionResult + } + + // 3. Ask for permission if command injection is detected. Skip when the + // AST parse already succeeded — tree-sitter has verified there are no + // hidden substitutions or structural tricks, so the legacy regex-based + // validators (backslash-escaped operators, etc.) would only add FPs. + if ( + !astParseSucceeded && + !isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_COMMAND_INJECTION_CHECK) + ) { + const safetyResult = await bashCommandIsSafeAsync(input.command) + + if (safetyResult.behavior !== 'passthrough') { + const decisionReason: PermissionDecisionReason = { + type: 'other' as const, + reason: + safetyResult.behavior === 'ask' && safetyResult.message + ? safetyResult.message + : 'This command contains patterns that could pose security risks and requires approval', + } + + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name, decisionReason), + decisionReason, + suggestions: [], // Don't suggest saving a potentially dangerous command + } + } + } + + // 4. Allow if command was allowed + if (permissionResult.behavior === 'allow') { + return permissionResult + } + + // 5. Suggest prefix if available, otherwise exact command + const suggestedUpdates = commandPrefixResult?.commandPrefix + ? suggestionForPrefix(commandPrefixResult.commandPrefix) + : suggestionForExactCommand(input.command) + + return { + ...permissionResult, + suggestions: suggestedUpdates, + } +} + +/** + * Checks if a command should be auto-allowed when sandboxed. + * Returns early if there are explicit deny/ask rules that should be respected. + * + * NOTE: This function should only be called when sandboxing and auto-allow are enabled. + * + * @param input - The bash tool input + * @param toolPermissionContext - The permission context + * @returns PermissionResult with: + * - deny/ask if explicit rule exists (exact or prefix) + * - allow if no explicit rules (sandbox auto-allow applies) + * - passthrough should not occur since we're in auto-allow mode + */ +function checkSandboxAutoAllow( + input: z.infer, + toolPermissionContext: ToolPermissionContext, +): PermissionResult { + const command = input.command.trim() + + // Check for explicit deny/ask rules on the full command (exact + prefix) + const { matchingDenyRules, matchingAskRules } = matchingRulesForInput( + input, + toolPermissionContext, + 'prefix', + ) + + // Return immediately if there's an explicit deny rule on the full command + if (matchingDenyRules[0] !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${command} has been denied.`, + decisionReason: { + type: 'rule', + rule: matchingDenyRules[0], + }, + } + } + + // SECURITY: For compound commands, check each subcommand against deny/ask + // rules. Prefix rules like Bash(rm:*) won't match the full compound command + // (e.g., "echo hello && rm -rf /" doesn't start with "rm"), so we must + // check each subcommand individually. + // IMPORTANT: Subcommand deny checks must run BEFORE full-command ask returns. + // Otherwise a wildcard ask rule matching the full command (e.g., Bash(*echo*)) + // would return 'ask' before a prefix deny rule on a subcommand (e.g., Bash(rm:*)) + // gets checked, downgrading a deny to an ask. + const subcommands = splitCommand(command) + if (subcommands.length > 1) { + let firstAskRule: PermissionRule | undefined + for (const sub of subcommands) { + const subResult = matchingRulesForInput( + { command: sub }, + toolPermissionContext, + 'prefix', + ) + // Deny takes priority — return immediately + if (subResult.matchingDenyRules[0] !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${command} has been denied.`, + decisionReason: { + type: 'rule', + rule: subResult.matchingDenyRules[0], + }, + } + } + // Stash first ask match; don't return yet (deny across all subs takes priority) + firstAskRule ??= subResult.matchingAskRules[0] + } + if (firstAskRule) { + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name), + decisionReason: { + type: 'rule', + rule: firstAskRule, + }, + } + } + } + + // Full-command ask check (after all deny sources have been exhausted) + if (matchingAskRules[0] !== undefined) { + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name), + decisionReason: { + type: 'rule', + rule: matchingAskRules[0], + }, + } + } + // No explicit rules, so auto-allow with sandbox + + return { + behavior: 'allow', + updatedInput: input, + decisionReason: { + type: 'other', + reason: 'Auto-allowed with sandbox (autoAllowBashIfSandboxed enabled)', + }, + } +} + +/** + * Filter out `cd ${cwd}` prefix subcommands, keeping astCommands aligned. + * Extracted to keep bashToolHasPermission under Bun's feature() DCE + * complexity threshold — inlining this breaks pendingClassifierCheck + * attachment in ~10 classifier tests. + */ +function filterCdCwdSubcommands( + rawSubcommands: string[], + astCommands: SimpleCommand[] | undefined, + cwd: string, + cwdMingw: string, +): { subcommands: string[]; astCommandsByIdx: (SimpleCommand | undefined)[] } { + const subcommands: string[] = [] + const astCommandsByIdx: (SimpleCommand | undefined)[] = [] + for (let i = 0; i < rawSubcommands.length; i++) { + const cmd = rawSubcommands[i]! + if (cmd === `cd ${cwd}` || cmd === `cd ${cwdMingw}`) continue + subcommands.push(cmd) + astCommandsByIdx.push(astCommands?.[i]) + } + return { subcommands, astCommandsByIdx } +} + +/** + * Early-exit deny enforcement for the AST too-complex and checkSemantics + * paths. Returns the exact-match result if non-passthrough (deny/ask/allow), + * then checks prefix/wildcard deny rules. Returns null if neither matched, + * meaning the caller should fall through to ask. Extracted to keep + * bashToolHasPermission under Bun's feature() DCE complexity threshold. + */ +function checkEarlyExitDeny( + input: z.infer, + toolPermissionContext: ToolPermissionContext, +): PermissionResult | null { + const exactMatchResult = bashToolCheckExactMatchPermission( + input, + toolPermissionContext, + ) + if (exactMatchResult.behavior !== 'passthrough') { + return exactMatchResult + } + const denyMatch = matchingRulesForInput( + input, + toolPermissionContext, + 'prefix', + ).matchingDenyRules[0] + if (denyMatch !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${input.command} has been denied.`, + decisionReason: { type: 'rule', rule: denyMatch }, + } + } + return null +} + +/** + * checkSemantics-path deny enforcement. Calls checkEarlyExitDeny (exact-match + * + full-command prefix deny), then checks each individual SimpleCommand .text + * span against prefix deny rules. The per-subcommand check is needed because + * filterRulesByContentsMatchingInput has a compound-command guard + * (splitCommand().length > 1 → prefix rules return false) that defeats + * `Bash(eval:*)` matching against a full pipeline like `echo foo | eval rm`. + * Each SimpleCommand span is a single command, so the guard doesn't fire. + * + * Separate helper (not folded into checkEarlyExitDeny or inlined at the call + * site) because bashToolHasPermission is tight against Bun's feature() DCE + * complexity threshold — adding even ~5 lines there breaks + * feature('BASH_CLASSIFIER') evaluation and drops pendingClassifierCheck. + */ +function checkSemanticsDeny( + input: z.infer, + toolPermissionContext: ToolPermissionContext, + commands: readonly { text: string }[], +): PermissionResult | null { + const fullCmd = checkEarlyExitDeny(input, toolPermissionContext) + if (fullCmd !== null) return fullCmd + for (const cmd of commands) { + const subDeny = matchingRulesForInput( + { ...input, command: cmd.text }, + toolPermissionContext, + 'prefix', + ).matchingDenyRules[0] + if (subDeny !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${input.command} has been denied.`, + decisionReason: { type: 'rule', rule: subDeny }, + } + } + } + return null +} + +/** + * Builds the pending classifier check metadata if classifier is enabled and has allow descriptions. + * Returns undefined if classifier is disabled, in auto mode, or no allow descriptions exist. + */ +function buildPendingClassifierCheck( + command: string, + toolPermissionContext: ToolPermissionContext, +): { command: string; cwd: string; descriptions: string[] } | undefined { + if (!isClassifierPermissionsEnabled()) { + return undefined + } + // Skip in auto mode - auto mode classifier handles all permission decisions + if (feature('TRANSCRIPT_CLASSIFIER') && toolPermissionContext.mode === 'auto') + return undefined + if (toolPermissionContext.mode === 'bypassPermissions') return undefined + + const allowDescriptions = getBashPromptAllowDescriptions( + toolPermissionContext, + ) + if (allowDescriptions.length === 0) return undefined + + return { + command, + cwd: getCwd(), + descriptions: allowDescriptions, + } +} + +const speculativeChecks = new Map>() + +/** + * Start a speculative bash allow classifier check early, so it runs in + * parallel with pre-tool hooks, deny/ask classifiers, and permission dialog setup. + * The result can be consumed later by executeAsyncClassifierCheck via + * consumeSpeculativeClassifierCheck. + */ +export function peekSpeculativeClassifierCheck( + command: string, +): Promise | undefined { + return speculativeChecks.get(command) +} + +export function startSpeculativeClassifierCheck( + command: string, + toolPermissionContext: ToolPermissionContext, + signal: AbortSignal, + isNonInteractiveSession: boolean, +): boolean { + // Same guards as buildPendingClassifierCheck + if (!isClassifierPermissionsEnabled()) return false + if (feature('TRANSCRIPT_CLASSIFIER') && toolPermissionContext.mode === 'auto') + return false + if (toolPermissionContext.mode === 'bypassPermissions') return false + const allowDescriptions = getBashPromptAllowDescriptions( + toolPermissionContext, + ) + if (allowDescriptions.length === 0) return false + + const cwd = getCwd() + const promise = classifyBashCommand( + command, + cwd, + allowDescriptions, + 'allow', + signal, + isNonInteractiveSession, + ) + // Prevent unhandled rejection if the signal aborts before this promise is consumed. + // The original promise (which may reject) is still stored in the Map for consumers to await. + promise.catch(() => {}) + speculativeChecks.set(command, promise) + return true +} + +/** + * Consume a speculative classifier check result for the given command. + * Returns the promise if one exists (and removes it from the map), or undefined. + */ +export function consumeSpeculativeClassifierCheck( + command: string, +): Promise | undefined { + const promise = speculativeChecks.get(command) + if (promise) { + speculativeChecks.delete(command) + } + return promise +} + +export function clearSpeculativeChecks(): void { + speculativeChecks.clear() +} + +/** + * Await a pending classifier check and return a PermissionDecisionReason if + * high-confidence allow, or undefined otherwise. + * + * Used by swarm agents (both tmux and in-process) to gate permission + * forwarding: run the classifier first, and only escalate to the leader + * if the classifier doesn't auto-approve. + */ +export async function awaitClassifierAutoApproval( + pendingCheck: PendingClassifierCheck, + signal: AbortSignal, + isNonInteractiveSession: boolean, +): Promise { + const { command, cwd, descriptions } = pendingCheck + const speculativeResult = consumeSpeculativeClassifierCheck(command) + const classifierResult = speculativeResult + ? await speculativeResult + : await classifyBashCommand( + command, + cwd, + descriptions, + 'allow', + signal, + isNonInteractiveSession, + ) + + logClassifierResultForAnts(command, 'allow', descriptions, classifierResult) + + if ( + feature('BASH_CLASSIFIER') && + classifierResult.matches && + classifierResult.confidence === 'high' + ) { + return { + type: 'classifier', + classifier: 'bash_allow', + reason: `Allowed by prompt rule: "${classifierResult.matchedDescription}"`, + } + } + return undefined +} + +type AsyncClassifierCheckCallbacks = { + shouldContinue: () => boolean + onAllow: (decisionReason: PermissionDecisionReason) => void + onComplete?: () => void +} + +/** + * Execute the bash allow classifier check asynchronously. + * This runs in the background while the permission prompt is shown. + * If the classifier allows with high confidence and the user hasn't interacted, auto-approves. + * + * @param pendingCheck - Classifier check metadata from bashToolHasPermission + * @param signal - Abort signal + * @param isNonInteractiveSession - Whether this is a non-interactive session + * @param callbacks - Callbacks to check if we should continue and handle approval + */ +export async function executeAsyncClassifierCheck( + pendingCheck: { command: string; cwd: string; descriptions: string[] }, + signal: AbortSignal, + isNonInteractiveSession: boolean, + callbacks: AsyncClassifierCheckCallbacks, +): Promise { + const { command, cwd, descriptions } = pendingCheck + const speculativeResult = consumeSpeculativeClassifierCheck(command) + + let classifierResult: ClassifierResult + try { + classifierResult = speculativeResult + ? await speculativeResult + : await classifyBashCommand( + command, + cwd, + descriptions, + 'allow', + signal, + isNonInteractiveSession, + ) + } catch (error: unknown) { + // When the coordinator session is cancelled, the abort signal fires and the + // classifier API call rejects with APIUserAbortError. This is expected and + // should not surface as an unhandled promise rejection. + if (error instanceof APIUserAbortError || error instanceof AbortError) { + callbacks.onComplete?.() + return + } + callbacks.onComplete?.() + throw error + } + + logClassifierResultForAnts(command, 'allow', descriptions, classifierResult) + + // Don't auto-approve if user already made a decision or has interacted + // with the permission dialog (e.g., arrow keys, tab, typing) + if (!callbacks.shouldContinue()) return + + if ( + feature('BASH_CLASSIFIER') && + classifierResult.matches && + classifierResult.confidence === 'high' + ) { + callbacks.onAllow({ + type: 'classifier', + classifier: 'bash_allow', + reason: `Allowed by prompt rule: "${classifierResult.matchedDescription}"`, + }) + } else { + // No match — notify so the checking indicator is cleared + callbacks.onComplete?.() + } +} + +/** + * The main implementation to check if we need to ask for user permission to call BashTool with a given input + */ +export async function bashToolHasPermission( + input: z.infer, + context: ToolUseContext, + getCommandSubcommandPrefixFn = getCommandSubcommandPrefix, +): Promise { + let appState = context.getAppState() + + // 0. AST-based security parse. This replaces both tryParseShellCommand + // (the shell-quote pre-check) and the bashCommandIsSafe misparsing gate. + // tree-sitter produces either a clean SimpleCommand[] (quotes resolved, + // no hidden substitutions) or 'too-complex' — which is exactly the signal + // we need to decide whether splitCommand's output can be trusted. + // + // When tree-sitter WASM is unavailable OR the injection check is disabled + // via env var, we fall back to the old path (legacy gate at ~1370 runs). + const injectionCheckDisabled = isEnvTruthy( + process.env.CLAUDE_CODE_DISABLE_COMMAND_INJECTION_CHECK, + ) + // GrowthBook killswitch for shadow mode — when off, skip the native parse + // entirely. Computed once; feature() must stay inline in the ternary below. + const shadowEnabled = feature('TREE_SITTER_BASH_SHADOW') + ? getFeatureValue_CACHED_MAY_BE_STALE('tengu_birch_trellis', true) + : false + // Parse once here; the resulting AST feeds both parseForSecurityFromAst + // and bashToolCheckCommandOperatorPermissions. + let astRoot = injectionCheckDisabled + ? null + : feature('TREE_SITTER_BASH_SHADOW') && !shadowEnabled + ? null + : await parseCommandRaw(input.command) + let astResult: ParseForSecurityResult = astRoot + ? parseForSecurityFromAst(input.command, astRoot) + : { kind: 'parse-unavailable' } + let astSubcommands: string[] | null = null + let astRedirects: Redirect[] | undefined + let astCommands: SimpleCommand[] | undefined + let shadowLegacySubs: string[] | undefined + + // Shadow-test tree-sitter: record its verdict, then force parse-unavailable + // so the legacy path stays authoritative. parseCommand stays gated on + // TREE_SITTER_BASH (not SHADOW) so legacy internals remain pure regex. + // One event per bash call captures both divergence AND unavailability + // reasons; module-load failures are separately covered by the + // session-scoped tengu_tree_sitter_load event. + if (feature('TREE_SITTER_BASH_SHADOW')) { + const available = astResult.kind !== 'parse-unavailable' + let tooComplex = false + let semanticFail = false + let subsDiffer = false + if (available) { + tooComplex = astResult.kind === 'too-complex' + semanticFail = + astResult.kind === 'simple' && !checkSemantics(astResult.commands).ok + const tsSubs = + astResult.kind === 'simple' + ? astResult.commands.map(c => c.text) + : undefined + const legacySubs = splitCommand(input.command) + shadowLegacySubs = legacySubs + subsDiffer = + tsSubs !== undefined && + (tsSubs.length !== legacySubs.length || + tsSubs.some((s, i) => s !== legacySubs[i])) + } + logEvent('tengu_tree_sitter_shadow', { + available, + astTooComplex: tooComplex, + astSemanticFail: semanticFail, + subsDiffer, + injectionCheckDisabled, + killswitchOff: !shadowEnabled, + cmdOverLength: input.command.length > 10000, + }) + // Always force legacy — shadow mode is observational only. + astResult = { kind: 'parse-unavailable' } + astRoot = null + } + + if (astResult.kind === 'too-complex') { + // Parse succeeded but found structure we can't statically analyze + // (command substitution, expansion, control flow, parser differential). + // Respect exact-match deny/ask/allow, then prefix/wildcard deny. Only + // fall through to ask if no deny matched — don't downgrade deny to ask. + const earlyExit = checkEarlyExitDeny(input, appState.toolPermissionContext) + if (earlyExit !== null) return earlyExit + const decisionReason: PermissionDecisionReason = { + type: 'other' as const, + reason: astResult.reason, + } + logEvent('tengu_bash_ast_too_complex', { + nodeTypeId: nodeTypeId(astResult.nodeType), + }) + return { + behavior: 'ask', + decisionReason, + message: createPermissionRequestMessage(BashTool.name, decisionReason), + suggestions: [], + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + + if (astResult.kind === 'simple') { + // Clean parse: check semantic-level concerns (zsh builtins, eval, etc.) + // that tokenize fine but are dangerous by name. + const sem = checkSemantics(astResult.commands) + if (!sem.ok) { + // Same deny-rule enforcement as the too-complex path: a user with + // `Bash(eval:*)` deny expects `eval "rm"` blocked, not downgraded. + const earlyExit = checkSemanticsDeny( + input, + appState.toolPermissionContext, + astResult.commands, + ) + if (earlyExit !== null) return earlyExit + const decisionReason: PermissionDecisionReason = { + type: 'other' as const, + reason: sem.reason, + } + return { + behavior: 'ask', + decisionReason, + message: createPermissionRequestMessage(BashTool.name, decisionReason), + suggestions: [], + } + } + // Stash the tokenized subcommands for use below. Downstream code (rule + // matching, path extraction, cd detection) still operates on strings, so + // we pass the original source span for each SimpleCommand. Downstream + // processing (stripSafeWrappers, parseCommandArguments) re-tokenizes + // these spans — that re-tokenization has known bugs (stripCommentLines + // mishandles newlines inside quotes), but checkSemantics already caught + // any argv element containing a newline, so those bugs can't bite here. + // Migrating downstream to operate on argv directly is a later commit. + astSubcommands = astResult.commands.map(c => c.text) + astRedirects = astResult.commands.flatMap(c => c.redirects) + astCommands = astResult.commands + } + + // Legacy shell-quote pre-check. Only reached on 'parse-unavailable' + // (tree-sitter not loaded OR TREE_SITTER_BASH feature gated off). Falls + // through to the full legacy path below. + if (astResult.kind === 'parse-unavailable') { + logForDebugging( + 'bashToolHasPermission: tree-sitter unavailable, using legacy shell-quote path', + ) + const parseResult = tryParseShellCommand(input.command) + if (!parseResult.success) { + const decisionReason = { + type: 'other' as const, + reason: `Command contains malformed syntax that cannot be parsed: ${parseResult.error}`, + } + return { + behavior: 'ask', + decisionReason, + message: createPermissionRequestMessage(BashTool.name, decisionReason), + } + } + } + + // Check sandbox auto-allow (which respects explicit deny/ask rules) + // Only call this if sandboxing and auto-allow are both enabled + if ( + SandboxManager.isSandboxingEnabled() && + SandboxManager.isAutoAllowBashIfSandboxedEnabled() && + shouldUseSandbox(input) + ) { + const sandboxAutoAllowResult = checkSandboxAutoAllow( + input, + appState.toolPermissionContext, + ) + if (sandboxAutoAllowResult.behavior !== 'passthrough') { + return sandboxAutoAllowResult + } + } + + // Check exact match first + const exactMatchResult = bashToolCheckExactMatchPermission( + input, + appState.toolPermissionContext, + ) + + // Exact command was denied + if (exactMatchResult.behavior === 'deny') { + return exactMatchResult + } + + // Check Bash prompt deny and ask rules in parallel (both use Haiku). + // Deny takes precedence over ask, and both take precedence over allow rules. + // Skip when in auto mode - auto mode classifier handles all permission decisions + if ( + isClassifierPermissionsEnabled() && + !( + feature('TRANSCRIPT_CLASSIFIER') && + appState.toolPermissionContext.mode === 'auto' + ) + ) { + const denyDescriptions = getBashPromptDenyDescriptions( + appState.toolPermissionContext, + ) + const askDescriptions = getBashPromptAskDescriptions( + appState.toolPermissionContext, + ) + const hasDeny = denyDescriptions.length > 0 + const hasAsk = askDescriptions.length > 0 + + if (hasDeny || hasAsk) { + const [denyResult, askResult] = await Promise.all([ + hasDeny + ? classifyBashCommand( + input.command, + getCwd(), + denyDescriptions, + 'deny', + context.abortController.signal, + context.options.isNonInteractiveSession, + ) + : null, + hasAsk + ? classifyBashCommand( + input.command, + getCwd(), + askDescriptions, + 'ask', + context.abortController.signal, + context.options.isNonInteractiveSession, + ) + : null, + ]) + + if (context.abortController.signal.aborted) { + throw new AbortError() + } + + if (denyResult) { + logClassifierResultForAnts( + input.command, + 'deny', + denyDescriptions, + denyResult, + ) + } + if (askResult) { + logClassifierResultForAnts( + input.command, + 'ask', + askDescriptions, + askResult, + ) + } + + // Deny takes precedence + if (denyResult?.matches && denyResult.confidence === 'high') { + return { + behavior: 'deny', + message: `Denied by Bash prompt rule: "${denyResult.matchedDescription}"`, + decisionReason: { + type: 'other', + reason: `Denied by Bash prompt rule: "${denyResult.matchedDescription}"`, + }, + } + } + + if (askResult?.matches && askResult.confidence === 'high') { + // Skip the Haiku call — the UI computes the prefix locally + // and lets the user edit it. Still call the injected function + // when tests override it. + let suggestions: PermissionUpdate[] + if (getCommandSubcommandPrefixFn === getCommandSubcommandPrefix) { + suggestions = suggestionForExactCommand(input.command) + } else { + const commandPrefixResult = await getCommandSubcommandPrefixFn( + input.command, + context.abortController.signal, + context.options.isNonInteractiveSession, + ) + if (context.abortController.signal.aborted) { + throw new AbortError() + } + suggestions = commandPrefixResult?.commandPrefix + ? suggestionForPrefix(commandPrefixResult.commandPrefix) + : suggestionForExactCommand(input.command) + } + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name), + decisionReason: { + type: 'other', + reason: `Required by Bash prompt rule: "${askResult.matchedDescription}"`, + }, + suggestions, + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + } + } + + // Check for non-subcommand Bash operators like `>`, `|`, etc. + // This must happen before dangerous path checks so that piped commands + // are handled by the operator logic (which generates "multiple operations" messages) + const commandOperatorResult = await checkCommandOperatorPermissions( + input, + (i: z.infer) => + bashToolHasPermission(i, context, getCommandSubcommandPrefixFn), + { isNormalizedCdCommand, isNormalizedGitCommand }, + astRoot, + ) + if (commandOperatorResult.behavior !== 'passthrough') { + // SECURITY FIX: When pipe segment processing returns 'allow', we must still validate + // the ORIGINAL command. The pipe segment processing strips redirections before + // checking each segment, so commands like: + // echo 'x' | xargs printf '%s' >> /tmp/file + // would have both segments allowed (echo and xargs printf) but the >> redirection + // would bypass validation. We must check: + // 1. Path constraints for output redirections + // 2. Command safety for dangerous patterns (backticks, etc.) in redirect targets + if (commandOperatorResult.behavior === 'allow') { + // Check for dangerous patterns (backticks, $(), etc.) in the original command + // This catches cases like: echo x | xargs echo > `pwd`/evil.txt + // where the backtick is in the redirect target (stripped from segments) + // Gate on AST: when astSubcommands is non-null, tree-sitter already + // validated structure (backticks/$() in redirect targets would have + // returned too-complex). Matches gating at ~1481, ~1706, ~1755. + // Avoids FP: `find -exec {} \; | grep x` tripping on backslash-;. + // bashCommandIsSafe runs the full legacy regex battery (~20 patterns) — + // only call it when we'll actually use the result. + const safetyResult = + astSubcommands === null + ? await bashCommandIsSafeAsync(input.command) + : null + if ( + safetyResult !== null && + safetyResult.behavior !== 'passthrough' && + safetyResult.behavior !== 'allow' + ) { + // Attach pending classifier check - may auto-approve before user responds + appState = context.getAppState() + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name, { + type: 'other', + reason: + safetyResult.message ?? + 'Command contains patterns that require approval', + }), + decisionReason: { + type: 'other', + reason: + safetyResult.message ?? + 'Command contains patterns that require approval', + }, + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + + appState = context.getAppState() + // SECURITY: Compute compoundCommandHasCd from the full command, NOT + // hardcode false. The pipe-handling path previously passed `false` here, + // disabling the cd+redirect check at pathValidation.ts:821. Appending + // `| echo done` to `cd .claude && echo x > settings.json` routed through + // this path with compoundCommandHasCd=false, letting the redirect write + // to .claude/settings.json without the cd+redirect block firing. + const pathResult = checkPathConstraints( + input, + getCwd(), + appState.toolPermissionContext, + commandHasAnyCd(input.command), + astRedirects, + astCommands, + ) + if (pathResult.behavior !== 'passthrough') { + return pathResult + } + } + + // When pipe segments return 'ask' (individual segments not allowed by rules), + // attach pending classifier check - may auto-approve before user responds. + if (commandOperatorResult.behavior === 'ask') { + appState = context.getAppState() + return { + ...commandOperatorResult, + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + + return commandOperatorResult + } + + // SECURITY: Legacy misparsing gate. Only runs when the tree-sitter module + // is not loaded. Timeout/abort is fail-closed via too-complex (returned + // early above), not routed here. When the AST parse succeeded, + // astSubcommands is non-null and we've already validated structure; this + // block is skipped entirely. The AST's 'too-complex' result subsumes + // everything isBashSecurityCheckForMisparsing covered — both answer the + // same question: "can splitCommand be trusted on this input?" + if ( + astSubcommands === null && + !isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_COMMAND_INJECTION_CHECK) + ) { + const originalCommandSafetyResult = await bashCommandIsSafeAsync( + input.command, + ) + if ( + originalCommandSafetyResult.behavior === 'ask' && + originalCommandSafetyResult.isBashSecurityCheckForMisparsing + ) { + // Compound commands with safe heredoc patterns ($(cat <<'EOF'...EOF)) + // trigger the $() check on the unsplit command. Strip the safe heredocs + // and re-check the remainder — if other misparsing patterns exist + // (e.g. backslash-escaped operators), they must still block. + const remainder = stripSafeHeredocSubstitutions(input.command) + const remainderResult = + remainder !== null ? await bashCommandIsSafeAsync(remainder) : null + if ( + remainder === null || + (remainderResult?.behavior === 'ask' && + remainderResult.isBashSecurityCheckForMisparsing) + ) { + // Allow if the exact command has an explicit allow permission — the user + // made a conscious choice to permit this specific command. + appState = context.getAppState() + const exactMatchResult = bashToolCheckExactMatchPermission( + input, + appState.toolPermissionContext, + ) + if (exactMatchResult.behavior === 'allow') { + return exactMatchResult + } + // Attach pending classifier check - may auto-approve before user responds + const decisionReason: PermissionDecisionReason = { + type: 'other' as const, + reason: originalCommandSafetyResult.message, + } + return { + behavior: 'ask', + message: createPermissionRequestMessage( + BashTool.name, + decisionReason, + ), + decisionReason, + suggestions: [], // Don't suggest saving a potentially dangerous command + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + } + } + + // Split into subcommands. Prefer the AST-extracted spans; fall back to + // splitCommand only when tree-sitter was unavailable. The cd-cwd filter + // strips the `cd ${cwd}` prefix that models like to prepend. + const cwd = getCwd() + const cwdMingw = + getPlatform() === 'windows' ? windowsPathToPosixPath(cwd) : cwd + const rawSubcommands = + astSubcommands ?? shadowLegacySubs ?? splitCommand(input.command) + const { subcommands, astCommandsByIdx } = filterCdCwdSubcommands( + rawSubcommands, + astCommands, + cwd, + cwdMingw, + ) + + // CC-643: Cap subcommand fanout. Only the legacy splitCommand path can + // explode — the AST path returns a bounded list (astSubcommands !== null) + // or short-circuits to 'too-complex' for structures it can't represent. + if ( + astSubcommands === null && + subcommands.length > MAX_SUBCOMMANDS_FOR_SECURITY_CHECK + ) { + logForDebugging( + `bashPermissions: ${subcommands.length} subcommands exceeds cap (${MAX_SUBCOMMANDS_FOR_SECURITY_CHECK}) — returning ask`, + { level: 'debug' }, + ) + const decisionReason = { + type: 'other' as const, + reason: `Command splits into ${subcommands.length} subcommands, too many to safety-check individually`, + } + return { + behavior: 'ask', + message: createPermissionRequestMessage(BashTool.name, decisionReason), + decisionReason, + } + } + + // Ask if there are multiple `cd` commands + const cdCommands = subcommands.filter(subCommand => + isNormalizedCdCommand(subCommand), + ) + if (cdCommands.length > 1) { + const decisionReason = { + type: 'other' as const, + reason: + 'Multiple directory changes in one command require approval for clarity', + } + return { + behavior: 'ask', + decisionReason, + message: createPermissionRequestMessage(BashTool.name, decisionReason), + } + } + + // Track if compound command contains cd for security validation + // This prevents bypassing path checks via: cd .claude/ && mv test.txt settings.json + const compoundCommandHasCd = cdCommands.length > 0 + + // SECURITY: Block compound commands that have both cd AND git + // This prevents sandbox escape via: cd /malicious/dir && git status + // where the malicious directory contains a bare git repo with core.fsmonitor. + // This check must happen HERE (before subcommand-level permission checks) + // because bashToolCheckPermission checks each subcommand independently via + // BashTool.isReadOnly(), which would re-derive compoundCommandHasCd=false + // from just "git status" alone, bypassing the readOnlyValidation.ts check. + if (compoundCommandHasCd) { + const hasGitCommand = subcommands.some(cmd => + isNormalizedGitCommand(cmd.trim()), + ) + if (hasGitCommand) { + const decisionReason = { + type: 'other' as const, + reason: + 'Compound commands with cd and git require approval to prevent bare repository attacks', + } + return { + behavior: 'ask', + decisionReason, + message: createPermissionRequestMessage(BashTool.name, decisionReason), + } + } + } + + appState = context.getAppState() // re-compute the latest in case the user hit shift+tab + + // SECURITY FIX: Check Bash deny/ask rules BEFORE path constraints + // This ensures that explicit deny rules like Bash(ls:*) take precedence over + // path constraint checks that return 'ask' for paths outside the project. + // Without this ordering, absolute paths outside the project (e.g., ls /home) + // would bypass deny rules because checkPathConstraints would return 'ask' first. + // + // Note: bashToolCheckPermission calls checkPathConstraints internally, which handles + // output redirection validation on each subcommand. However, since splitCommand strips + // redirections before we get here, we MUST validate output redirections on the ORIGINAL + // command AFTER checking deny rules but BEFORE returning results. + const subcommandPermissionDecisions = subcommands.map((command, i) => + bashToolCheckPermission( + { command }, + appState.toolPermissionContext, + compoundCommandHasCd, + astCommandsByIdx[i], + ), + ) + + // Deny if any subcommands are denied + const deniedSubresult = subcommandPermissionDecisions.find( + _ => _.behavior === 'deny', + ) + if (deniedSubresult !== undefined) { + return { + behavior: 'deny', + message: `Permission to use ${BashTool.name} with command ${input.command} has been denied.`, + decisionReason: { + type: 'subcommandResults', + reasons: new Map( + subcommandPermissionDecisions.map((result, i) => [ + subcommands[i]!, + result, + ]), + ), + }, + } + } + + // Validate output redirections on the ORIGINAL command (before splitCommand stripped them) + // This must happen AFTER checking deny rules but BEFORE returning results. + // Output redirections like "> /etc/passwd" are stripped by splitCommand, so the per-subcommand + // checkPathConstraints calls won't see them. We validate them here on the original input. + // SECURITY: When AST data is available, pass AST-derived redirects so + // checkPathConstraints uses them directly instead of re-parsing with + // shell-quote (which has a known single-quote backslash misparsing bug + // that can silently hide redirect operators). + const pathResult = checkPathConstraints( + input, + getCwd(), + appState.toolPermissionContext, + compoundCommandHasCd, + astRedirects, + astCommands, + ) + if (pathResult.behavior === 'deny') { + return pathResult + } + + const askSubresult = subcommandPermissionDecisions.find( + _ => _.behavior === 'ask', + ) + const nonAllowCount = count( + subcommandPermissionDecisions, + _ => _.behavior !== 'allow', + ) + + // SECURITY (GH#28784): Only short-circuit on a path-constraint 'ask' when no + // subcommand independently produced an 'ask'. checkPathConstraints re-runs the + // path-command loop on the full input, so `cd && python3 foo.py` + // produces an ask with ONLY a Read(/**) suggestion — the UI renders it as + // "Yes, allow reading from /" and picking that option silently approves + // python3. When a subcommand has its own ask (e.g. the cd subcommand's own + // path-constraint ask), fall through: either the askSubresult short-circuit + // below fires (single non-allow subcommand) or the merge flow collects Bash + // rule suggestions for every non-allow subcommand. The per-subcommand + // checkPathConstraints call inside bashToolCheckPermission already captures + // the Read rule for the cd target in that path. + // + // When no subcommand asked (all allow, or all passthrough like `printf > file`), + // pathResult IS the only ask — return it so redirection checks surface. + if (pathResult.behavior === 'ask' && askSubresult === undefined) { + return pathResult + } + + // Ask if any subcommands require approval (e.g., ls/cd outside boundaries). + // Only short-circuit when exactly ONE subcommand needs approval — if multiple + // do (e.g. cd-outside-project ask + python3 passthrough), fall through to the + // merge flow so the prompt surfaces Bash rule suggestions for all of them + // instead of only the first ask's Read rule (GH#28784). + if (askSubresult !== undefined && nonAllowCount === 1) { + return { + ...askSubresult, + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + + // Allow if exact command was allowed + if (exactMatchResult.behavior === 'allow') { + return exactMatchResult + } + + // If all subcommands are allowed via exact or prefix match, allow the + // command — but only if no command injection is possible. When the AST + // parse succeeded, each subcommand is already known-safe (no hidden + // substitutions, no structural tricks); the per-subcommand re-check is + // redundant. When on the legacy path, re-run bashCommandIsSafeAsync per sub. + let hasPossibleCommandInjection = false + if ( + astSubcommands === null && + !isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_COMMAND_INJECTION_CHECK) + ) { + // CC-643: Batch divergence telemetry into a single logEvent. The per-sub + // logEvent was the hot-path syscall driver (each call → /proc/self/stat + // via process.memoryUsage()). Aggregate count preserves the signal. + let divergenceCount = 0 + const onDivergence = () => { + divergenceCount++ + } + const results = await Promise.all( + subcommands.map(c => bashCommandIsSafeAsync(c, onDivergence)), + ) + hasPossibleCommandInjection = results.some( + r => r.behavior !== 'passthrough', + ) + if (divergenceCount > 0) { + logEvent('tengu_tree_sitter_security_divergence', { + quoteContextDivergence: true, + count: divergenceCount, + }) + } + } + if ( + subcommandPermissionDecisions.every(_ => _.behavior === 'allow') && + !hasPossibleCommandInjection + ) { + return { + behavior: 'allow', + updatedInput: input, + decisionReason: { + type: 'subcommandResults', + reasons: new Map( + subcommandPermissionDecisions.map((result, i) => [ + subcommands[i]!, + result, + ]), + ), + }, + } + } + + // Query Haiku for command prefixes + // Skip the Haiku call — the UI computes the prefix locally and + // lets the user edit it. Still call when a custom fn is injected (tests). + let commandSubcommandPrefix: Awaited< + ReturnType + > = null + if (getCommandSubcommandPrefixFn !== getCommandSubcommandPrefix) { + commandSubcommandPrefix = await getCommandSubcommandPrefixFn( + input.command, + context.abortController.signal, + context.options.isNonInteractiveSession, + ) + if (context.abortController.signal.aborted) { + throw new AbortError() + } + } + + // If there is only one command, no need to process subcommands + appState = context.getAppState() // re-compute the latest in case the user hit shift+tab + if (subcommands.length === 1) { + const result = await checkCommandAndSuggestRules( + { command: subcommands[0]! }, + appState.toolPermissionContext, + commandSubcommandPrefix, + compoundCommandHasCd, + astSubcommands !== null, + ) + // If command wasn't allowed, attach pending classifier check. + // At this point, 'ask' can only come from bashCommandIsSafe (security check inside + // checkCommandAndSuggestRules), NOT from explicit ask rules - those were already + // filtered out at step 13 (askSubresult check). The classifier can bypass security. + if (result.behavior === 'ask' || result.behavior === 'passthrough') { + return { + ...result, + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } + } + return result + } + + // Check subcommand permission results + const subcommandResults: Map = new Map() + for (const subcommand of subcommands) { + subcommandResults.set( + subcommand, + await checkCommandAndSuggestRules( + { + // Pass through input params like `sandbox` + ...input, + command: subcommand, + }, + appState.toolPermissionContext, + commandSubcommandPrefix?.subcommandPrefixes.get(subcommand), + compoundCommandHasCd, + astSubcommands !== null, + ), + ) + } + + // Allow if all subcommands are allowed + // Note that this is different than 6b because we are checking the command injection results. + if ( + subcommands.every(subcommand => { + const permissionResult = subcommandResults.get(subcommand) + return permissionResult?.behavior === 'allow' + }) + ) { + // Keep subcommandResults as PermissionResult for decisionReason + return { + behavior: 'allow', + updatedInput: input, + decisionReason: { + type: 'subcommandResults', + reasons: subcommandResults, + }, + } + } + + // Otherwise, ask for permission + const collectedRules: Map = new Map() + + for (const [subcommand, permissionResult] of subcommandResults) { + if ( + permissionResult.behavior === 'ask' || + permissionResult.behavior === 'passthrough' + ) { + const updates = + 'suggestions' in permissionResult + ? permissionResult.suggestions + : undefined + + const rules = extractRules(updates) + for (const rule of rules) { + // Use string representation as key for deduplication + const ruleKey = permissionRuleValueToString(rule) + collectedRules.set(ruleKey, rule) + } + + // GH#28784 follow-up: security-check asks (compound-cd+write, process + // substitution, etc.) carry no suggestions. In a compound command like + // `cd ~/out && rm -rf x`, that means only cd's Read rule gets collected + // and the UI labels the prompt "Yes, allow reading from /" — never + // mentioning rm. Synthesize a Bash(exact) rule so the UI shows the + // chained command. Skip explicit ask rules (decisionReason.type 'rule') + // where the user deliberately wants to review each time. + if ( + permissionResult.behavior === 'ask' && + rules.length === 0 && + permissionResult.decisionReason?.type !== 'rule' + ) { + for (const rule of extractRules( + suggestionForExactCommand(subcommand), + )) { + const ruleKey = permissionRuleValueToString(rule) + collectedRules.set(ruleKey, rule) + } + } + // Note: We only collect rules, not other update types like mode changes + // This is appropriate for bash subcommands which primarily need rule suggestions + } + } + + const decisionReason = { + type: 'subcommandResults' as const, + reasons: subcommandResults, + } + + // GH#11380: Cap at MAX_SUGGESTED_RULES_FOR_COMPOUND. Map preserves insertion + // order (subcommand order), so slicing keeps the leftmost N. + const cappedRules = Array.from(collectedRules.values()).slice( + 0, + MAX_SUGGESTED_RULES_FOR_COMPOUND, + ) + const suggestedUpdates: PermissionUpdate[] | undefined = + cappedRules.length > 0 + ? [ + { + type: 'addRules', + rules: cappedRules, + behavior: 'allow', + destination: 'localSettings', + }, + ] + : undefined + + // Attach pending classifier check - may auto-approve before user responds. + // Behavior is 'ask' if any subcommand was 'ask' (e.g., path constraint or ask + // rule) — before the GH#28784 fix, ask subresults always short-circuited above + // so this path only saw 'passthrough' subcommands and hardcoded that. + return { + behavior: askSubresult !== undefined ? 'ask' : 'passthrough', + message: createPermissionRequestMessage(BashTool.name, decisionReason), + decisionReason, + suggestions: suggestedUpdates, + ...(feature('BASH_CLASSIFIER') + ? { + pendingClassifierCheck: buildPendingClassifierCheck( + input.command, + appState.toolPermissionContext, + ), + } + : {}), + } +} + +/** + * Checks if a subcommand is a git command after normalizing away safe wrappers + * (env vars, timeout, etc.) and shell quotes. + * + * SECURITY: Must normalize before matching to prevent bypasses like: + * 'git' status — shell quotes hide the command from a naive regex + * NO_COLOR=1 git status — env var prefix hides the command + */ +export function isNormalizedGitCommand(command: string): boolean { + // Fast path: catch the most common case before any parsing + if (command.startsWith('git ') || command === 'git') { + return true + } + const stripped = stripSafeWrappers(command) + const parsed = tryParseShellCommand(stripped) + if (parsed.success && parsed.tokens.length > 0) { + // Direct git command + if (parsed.tokens[0] === 'git') { + return true + } + // "xargs git ..." — xargs runs git in the current directory, + // so it must be treated as a git command for cd+git security checks. + // This matches the xargs prefix handling in filterRulesByContentsMatchingInput. + if (parsed.tokens[0] === 'xargs' && parsed.tokens.includes('git')) { + return true + } + return false + } + return /^git(?:\s|$)/.test(stripped) +} + +/** + * Checks if a subcommand is a cd command after normalizing away safe wrappers + * (env vars, timeout, etc.) and shell quotes. + * + * SECURITY: Must normalize before matching to prevent bypasses like: + * FORCE_COLOR=1 cd sub — env var prefix hides the cd from a naive /^cd / regex + * This mirrors isNormalizedGitCommand to ensure symmetric normalization. + * + * Also matches pushd/popd — they change cwd just like cd, so + * pushd /tmp/bare-repo && git status + * must trigger the same cd+git guard. Mirrors PowerShell's + * DIRECTORY_CHANGE_ALIASES (src/utils/powershell/parser.ts). + */ +export function isNormalizedCdCommand(command: string): boolean { + const stripped = stripSafeWrappers(command) + const parsed = tryParseShellCommand(stripped) + if (parsed.success && parsed.tokens.length > 0) { + const cmd = parsed.tokens[0] + return cmd === 'cd' || cmd === 'pushd' || cmd === 'popd' + } + return /^(?:cd|pushd|popd)(?:\s|$)/.test(stripped) +} + +/** + * Checks if a compound command contains any cd command, + * using normalized detection that handles env var prefixes and shell quotes. + */ +export function commandHasAnyCd(command: string): boolean { + return splitCommand(command).some(subcmd => + isNormalizedCdCommand(subcmd.trim()), + ) +} diff --git a/_all-in-one-upload/cc-haha/src/tools/BashTool/commentLabel.ts b/_all-in-one-upload/cc-haha/src/tools/BashTool/commentLabel.ts new file mode 100644 index 0000000..dd36df8 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/BashTool/commentLabel.ts @@ -0,0 +1,13 @@ +/** + * If the first line of a bash command is a `# comment` (not a `#!` shebang), + * return the comment text stripped of the `#` prefix. Otherwise undefined. + * + * Under fullscreen mode this is the non-verbose tool-use label AND the + * collapse-group ⎿ hint — it's what Claude wrote for the human to read. + */ +export function extractBashCommentLabel(command: string): string | undefined { + const nl = command.indexOf('\n') + const firstLine = (nl === -1 ? command : command.slice(0, nl)).trim() + if (!firstLine.startsWith('#') || firstLine.startsWith('#!')) return undefined + return firstLine.replace(/^#+\s*/, '') || undefined +} diff --git a/_all-in-one-upload/cc-haha/src/tools/EnterPlanModeTool/UI.tsx b/_all-in-one-upload/cc-haha/src/tools/EnterPlanModeTool/UI.tsx new file mode 100644 index 0000000..1958fe3 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/EnterPlanModeTool/UI.tsx @@ -0,0 +1,33 @@ +import * as React from 'react'; +import { BLACK_CIRCLE } from 'src/constants/figures.js'; +import { getModeColor } from 'src/utils/permissions/PermissionMode.js'; +import { Box, Text } from '../../ink.js'; +import type { ToolProgressData } from '../../Tool.js'; +import type { ProgressMessage } from '../../types/message.js'; +import type { ThemeName } from '../../utils/theme.js'; +import type { Output } from './EnterPlanModeTool.js'; +export function renderToolUseMessage(): React.ReactNode { + return null; +} +export function renderToolResultMessage(_output: Output, _progressMessagesForMessage: ProgressMessage[], _options: { + theme: ThemeName; +}): React.ReactNode { + return + + {BLACK_CIRCLE} + Entered plan mode + + + + Claude is now exploring and designing an implementation approach. + + + ; +} +export function renderToolUseRejectedMessage(): React.ReactNode { + return + {BLACK_CIRCLE} + User declined to enter plan mode + ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsIkJMQUNLX0NJUkNMRSIsImdldE1vZGVDb2xvciIsIkJveCIsIlRleHQiLCJUb29sUHJvZ3Jlc3NEYXRhIiwiUHJvZ3Jlc3NNZXNzYWdlIiwiVGhlbWVOYW1lIiwiT3V0cHV0IiwicmVuZGVyVG9vbFVzZU1lc3NhZ2UiLCJSZWFjdE5vZGUiLCJyZW5kZXJUb29sUmVzdWx0TWVzc2FnZSIsIl9vdXRwdXQiLCJfcHJvZ3Jlc3NNZXNzYWdlc0Zvck1lc3NhZ2UiLCJfb3B0aW9ucyIsInRoZW1lIiwicmVuZGVyVG9vbFVzZVJlamVjdGVkTWVzc2FnZSJdLCJzb3VyY2VzIjpbIlVJLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgKiBhcyBSZWFjdCBmcm9tICdyZWFjdCdcbmltcG9ydCB7IEJMQUNLX0NJUkNMRSB9IGZyb20gJ3NyYy9jb25zdGFudHMvZmlndXJlcy5qcydcbmltcG9ydCB7IGdldE1vZGVDb2xvciB9IGZyb20gJ3NyYy91dGlscy9wZXJtaXNzaW9ucy9QZXJtaXNzaW9uTW9kZS5qcydcbmltcG9ydCB7IEJveCwgVGV4dCB9IGZyb20gJy4uLy4uL2luay5qcydcbmltcG9ydCB0eXBlIHsgVG9vbFByb2dyZXNzRGF0YSB9IGZyb20gJy4uLy4uL1Rvb2wuanMnXG5pbXBvcnQgdHlwZSB7IFByb2dyZXNzTWVzc2FnZSB9IGZyb20gJy4uLy4uL3R5cGV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sx b/_all-in-one-upload/cc-haha/src/tools/MCPTool/UI.tsx new file mode 100644 index 0000000..bc33ffe --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/MCPTool/UI.tsx @@ -0,0 +1,403 @@ +import { c as _c } from "react/compiler-runtime"; +import { feature } from 'bun:bundle'; +import figures from 'figures'; +import * as React from 'react'; +import type { z } from 'zod/v4'; +import { ProgressBar } from '../../components/design-system/ProgressBar.js'; +import { MessageResponse } from '../../components/MessageResponse.js'; +import { linkifyUrlsInText, OutputLine } from '../../components/shell/OutputLine.js'; +import { stringWidth } from '../../ink/stringWidth.js'; +import { Ansi, Box, Text } from '../../ink.js'; +import type { ToolProgressData } from '../../Tool.js'; +import type { ProgressMessage } from '../../types/message.js'; +import type { MCPProgress } from '../../types/tools.js'; +import { formatNumber } from '../../utils/format.js'; +import { createHyperlink } from '../../utils/hyperlink.js'; +import { getContentSizeEstimate, type MCPToolResult } from '../../utils/mcpValidation.js'; +import { jsonParse, jsonStringify } from '../../utils/slowOperations.js'; +import type { inputSchema } from './MCPTool.js'; + +// Threshold for displaying warning about large MCP responses +const MCP_OUTPUT_WARNING_THRESHOLD_TOKENS = 10_000; + +// In non-verbose mode, truncate individual input values to keep the header +// compact. Matches BashTool's philosophy of showing enough to identify the +// call without dumping the entire payload inline. +const MAX_INPUT_VALUE_CHARS = 80; + +// Max number of top-level keys before we fall back to raw JSON display. +// Beyond this a flat k:v list is more noise than help. +const MAX_FLAT_JSON_KEYS = 12; + +// Don't attempt flat-object parsing for large blobs. +const MAX_FLAT_JSON_CHARS = 5_000; + +// Don't attempt to parse JSON blobs larger than this (perf safety). +const MAX_JSON_PARSE_CHARS = 200_000; + +// A string value is "dominant text payload" if it has newlines or is +// long enough that inline display would be worse than unwrapping. +const UNWRAP_MIN_STRING_LEN = 200; +export function renderToolUseMessage(input: z.infer>, { + verbose +}: { + verbose: boolean; +}): React.ReactNode { + if (Object.keys(input).length === 0) { + return ''; + } + return Object.entries(input).map(([key, value]) => { + let rendered = jsonStringify(value); + if (feature('MCP_RICH_OUTPUT') && !verbose && rendered.length > MAX_INPUT_VALUE_CHARS) { + rendered = rendered.slice(0, MAX_INPUT_VALUE_CHARS).trimEnd() + '…'; + } + return `${key}: ${rendered}`; + }).join(', '); +} +export function renderToolUseProgressMessage(progressMessagesForMessage: ProgressMessage[]): React.ReactNode { + const lastProgress = progressMessagesForMessage.at(-1); + if (!lastProgress?.data) { + return + Running… + ; + } + const { + progress, + total, + progressMessage + } = lastProgress.data; + if (progress === undefined) { + return + Running… + ; + } + if (total !== undefined && total > 0) { + const ratio = Math.min(1, Math.max(0, progress / total)); + const percentage = Math.round(ratio * 100); + return + + {progressMessage && {progressMessage}} + + + {percentage}% + + + ; + } + return + {progressMessage ?? `Processing… ${progress}`} + ; +} +export function renderToolResultMessage(output: string | MCPToolResult, _progressMessagesForMessage: ProgressMessage[], { + verbose, + input +}: { + verbose: boolean; + input?: unknown; +}): React.ReactNode { + const mcpOutput = output as MCPToolResult; + if (!verbose) { + const slackSend = trySlackSendCompact(mcpOutput, input); + if (slackSend !== null) { + return + + Sent a message to{' '} + {createHyperlink(slackSend.url, slackSend.channel)} + + ; + } + } + const estimatedTokens = getContentSizeEstimate(mcpOutput); + const showWarning = estimatedTokens > MCP_OUTPUT_WARNING_THRESHOLD_TOKENS; + const warningMessage = showWarning ? `${figures.warning} Large MCP response (~${formatNumber(estimatedTokens)} tokens), this can fill up context quickly` : null; + let contentElement: React.ReactNode; + if (Array.isArray(mcpOutput)) { + const contentBlocks = mcpOutput.map((item, i) => { + if (item.type === 'image') { + return + + [Image] + + ; + } + // For text blocks and any other block types, extract text if available + const textContent = item.type === 'text' && 'text' in item && item.text !== null && item.text !== undefined ? String(item.text) : ''; + return feature('MCP_RICH_OUTPUT') ? : ; + }); + + // Wrap array content in a column layout + contentElement = + {contentBlocks} + ; + } else if (!mcpOutput) { + contentElement = + + (No content) + + ; + } else { + contentElement = feature('MCP_RICH_OUTPUT') ? : ; + } + if (warningMessage) { + return + + {warningMessage} + + {contentElement} + ; + } + return contentElement; +} + +/** + * Render MCP text output. Tries three strategies in order: + * 1. If JSON wraps a single dominant text payload (e.g. slack's + * {"messages":"line1\nline2..."}), unwrap and let OutputLine truncate. + * 2. If JSON is a small flat-ish object, render as aligned key: value. + * 3. Otherwise fall through to OutputLine (pretty-print + truncate). + */ +function MCPTextOutput(t0) { + const $ = _c(18); + const { + content, + verbose + } = t0; + let t1; + if ($[0] !== content || $[1] !== verbose) { + t1 = Symbol.for("react.early_return_sentinel"); + bb0: { + const unwrapped = tryUnwrapTextPayload(content); + if (unwrapped !== null) { + const t2 = unwrapped.extras.length > 0 && {unwrapped.extras.map(_temp).join(" \xB7 ")}; + let t3; + if ($[3] !== unwrapped || $[4] !== verbose) { + t3 = ; + $[3] = unwrapped; + $[4] = verbose; + $[5] = t3; + } else { + t3 = $[5]; + } + let t4; + if ($[6] !== t2 || $[7] !== t3) { + t4 = {t2}{t3}; + $[6] = t2; + $[7] = t3; + $[8] = t4; + } else { + t4 = $[8]; + } + t1 = t4; + break bb0; + } + } + $[0] = content; + $[1] = verbose; + $[2] = t1; + } else { + t1 = $[2]; + } + if (t1 !== Symbol.for("react.early_return_sentinel")) { + return t1; + } + let t2; + if ($[9] !== content) { + t2 = Symbol.for("react.early_return_sentinel"); + bb1: { + const flat = tryFlattenJson(content); + if (flat !== null) { + const maxKeyWidth = Math.max(...flat.map(_temp2)); + let t3; + if ($[11] !== maxKeyWidth) { + t3 = (t4, i) => { + const [key, value] = t4; + return {key.padEnd(maxKeyWidth)}: {linkifyUrlsInText(value)}; + }; + $[11] = maxKeyWidth; + $[12] = t3; + } else { + t3 = $[12]; + } + const t4 = {flat.map(t3)}; + let t5; + if ($[13] !== t4) { + t5 = {t4}; + $[13] = t4; + $[14] = t5; + } else { + t5 = $[14]; + } + t2 = t5; + break bb1; + } + } + $[9] = content; + $[10] = t2; + } else { + t2 = $[10]; + } + if (t2 !== Symbol.for("react.early_return_sentinel")) { + return t2; + } + let t3; + if ($[15] !== content || $[16] !== verbose) { + t3 = ; + $[15] = content; + $[16] = verbose; + $[17] = t3; + } else { + t3 = $[17]; + } + return t3; +} + +/** + * Parse content as a JSON object and return its entries. Null if content + * doesn't parse, isn't an object, is too large, or has 0/too-many keys. + */ +function _temp2(t0) { + const [k_0] = t0; + return stringWidth(k_0); +} +function _temp(t0) { + const [k, v] = t0; + return `${k}: ${v}`; +} +function parseJsonEntries(content: string, { + maxChars, + maxKeys +}: { + maxChars: number; + maxKeys: number; +}): [string, unknown][] | null { + const trimmed = content.trim(); + if (trimmed.length === 0 || trimmed.length > maxChars || trimmed[0] !== '{') { + return null; + } + let parsed: unknown; + try { + parsed = jsonParse(trimmed); + } catch { + return null; + } + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { + return null; + } + const entries = Object.entries(parsed); + if (entries.length === 0 || entries.length > maxKeys) { + return null; + } + return entries; +} + +/** + * If content parses as a JSON object where every value is a scalar or a + * small nested object, flatten it to [key, displayValue] pairs. Nested + * objects get one-line JSON. Returns null if content doesn't qualify. + */ +export function tryFlattenJson(content: string): [string, string][] | null { + const entries = parseJsonEntries(content, { + maxChars: MAX_FLAT_JSON_CHARS, + maxKeys: MAX_FLAT_JSON_KEYS + }); + if (entries === null) return null; + const result: [string, string][] = []; + for (const [key, value] of entries) { + if (typeof value === 'string') { + result.push([key, value]); + } else if (value === null || typeof value === 'number' || typeof value === 'boolean') { + result.push([key, String(value)]); + } else if (typeof value === 'object') { + const compact = jsonStringify(value); + if (compact.length > 120) return null; + result.push([key, compact]); + } else { + return null; + } + } + return result; +} + +/** + * If content is a JSON object where one key holds a dominant string payload + * (multiline or long) and all siblings are small scalars, unwrap it. This + * handles the common MCP pattern of {"messages":"line1\nline2..."} where + * pretty-printing keeps \n escaped but we want real line breaks + truncation. + */ +export function tryUnwrapTextPayload(content: string): { + body: string; + extras: [string, string][]; +} | null { + const entries = parseJsonEntries(content, { + maxChars: MAX_JSON_PARSE_CHARS, + maxKeys: 4 + }); + if (entries === null) return null; + // Find the one dominant string payload. Trim first: a trailing \n on a + // short sibling (e.g. pagination hints) shouldn't make it "dominant". + let body: string | null = null; + const extras: [string, string][] = []; + for (const [key, value] of entries) { + if (typeof value === 'string') { + const t = value.trimEnd(); + const isDominant = t.length > UNWRAP_MIN_STRING_LEN || t.includes('\n') && t.length > 50; + if (isDominant) { + if (body !== null) return null; // two big strings — ambiguous + body = t; + continue; + } + if (t.length > 150) return null; + extras.push([key, t.replace(/\s+/g, ' ')]); + } else if (value === null || typeof value === 'number' || typeof value === 'boolean') { + extras.push([key, String(value)]); + } else { + return null; // nested object/array — use flat or pretty-print path + } + } + if (body === null) return null; + return { + body, + extras + }; +} +const SLACK_ARCHIVES_RE = /^https:\/\/[a-z0-9-]+\.slack\.com\/archives\/([A-Z0-9]+)\/p\d+$/; + +/** + * Detect a Slack send-message result and return a compact {channel, url} pair. + * Matches both hosted (claude.ai Slack) and community MCP server shapes — + * both return `message_link` in the result. The channel label prefers the + * tool input (may be a name like "#foo" or an ID like "C09EVDAN1NK") and + * falls back to the ID parsed from the archives URL. + */ +export function trySlackSendCompact(output: string | MCPToolResult, input: unknown): { + channel: string; + url: string; +} | null { + let text: unknown = output; + if (Array.isArray(output)) { + const block = output.find(b => b.type === 'text'); + text = block && 'text' in block ? block.text : undefined; + } + if (typeof text !== 'string' || !text.includes('"message_link"')) { + return null; + } + const entries = parseJsonEntries(text, { + maxChars: 2000, + maxKeys: 6 + }); + const url = entries?.find(([k]) => k === 'message_link')?.[1]; + if (typeof url !== 'string') return null; + const m = SLACK_ARCHIVES_RE.exec(url); + if (!m) return null; + const inp = input as { + channel_id?: unknown; + channel?: unknown; + } | undefined; + const raw = inp?.channel_id ?? inp?.channel ?? m[1]; + const label = typeof raw === 'string' && raw ? raw : 'slack'; + return { + channel: label.startsWith('#') ? label : `#${label}`, + url + }; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJmZ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DQUFDNEQsQ0FBQyxFQUFFLE9BQU8sSUFBSTtFQUVuQixNQUFNRSxHQUFHLEdBQUduRyxLQUFLLElBQUk7SUFBRW9HLFVBQVUsQ0FBQyxFQUFFLE9BQU87SUFBRTlELE9BQU8sQ0FBQyxFQUFFLE9BQU87RUFBQyxDQUFDLEdBQUcsU0FBUztFQUM1RSxNQUFNK0QsR0FBRyxHQUFHRixHQUFHLEVBQUVDLFVBQVUsSUFBSUQsR0FBRyxFQUFFN0QsT0FBTyxJQUFJMkQsQ0FBQyxDQUFDLENBQUMsQ0FBQztFQUNuRCxNQUFNSyxLQUFLLEdBQUcsT0FBT0QsR0FBRyxLQUFLLFFBQVEsSUFBSUEsR0FBRyxHQUFHQSxHQUFHLEdBQUcsT0FBTztFQUM1RCxPQUFPO0lBQUUvRCxPQUFPLEVBQUVnRSxLQUFLLENBQUNDLFVBQVUsQ0FBQyxHQUFHLENBQUMsR0FBR0QsS0FBSyxHQUFHLElBQUlBLEtBQUssRUFBRTtJQUFFakU7RUFBSSxDQUFDO0FBQ3RFIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/tools/NotebookEditTool/UI.tsx b/_all-in-one-upload/cc-haha/src/tools/NotebookEditTool/UI.tsx new file mode 100644 index 0000000..017bbfd --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/NotebookEditTool/UI.tsx @@ -0,0 +1,93 @@ +import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'; +import * as React from 'react'; +import type { Message, ProgressMessage } from 'src/types/message.js'; +import { extractTag } from 'src/utils/messages.js'; +import type { ThemeName } from 'src/utils/theme.js'; +import type { z } from 'zod/v4'; +import { FallbackToolUseErrorMessage } from '../../components/FallbackToolUseErrorMessage.js'; +import { FilePathLink } from '../../components/FilePathLink.js'; +import { HighlightedCode } from '../../components/HighlightedCode.js'; +import { MessageResponse } from '../../components/MessageResponse.js'; +import { NotebookEditToolUseRejectedMessage } from '../../components/NotebookEditToolUseRejectedMessage.js'; +import { Box, Text } from '../../ink.js'; +import type { Tools } from '../../Tool.js'; +import { getDisplayPath } from '../../utils/file.js'; +import type { inputSchema, Output } from './NotebookEditTool.js'; +export function getToolUseSummary(input: Partial>> | undefined): string | null { + if (!input?.notebook_path) { + return null; + } + return getDisplayPath(input.notebook_path); +} +export function renderToolUseMessage({ + notebook_path, + cell_id, + new_source, + cell_type, + edit_mode +}: Partial>>, { + verbose +}: { + verbose: boolean; +}): React.ReactNode { + if (!notebook_path || !new_source || !cell_type) { + return null; + } + const displayPath = verbose ? notebook_path : getDisplayPath(notebook_path); + if (verbose) { + return <> + {displayPath} + {`@${cell_id}, content: ${new_source.slice(0, 30)}…, cell_type: ${cell_type}, edit_mode: ${edit_mode ?? 'replace'}`} + ; + } + return <> + {displayPath} + {`@${cell_id}`} + ; +} +export function renderToolUseRejectedMessage(input: z.infer>, { + verbose +}: { + columns?: number; + messages?: Message[]; + progressMessagesForMessage?: ProgressMessage[]; + theme?: ThemeName; + tools?: Tools; + verbose: boolean; +}): React.ReactNode { + return ; +} +export function renderToolUseErrorMessage(result: ToolResultBlockParam['content'], { + verbose +}: { + verbose: boolean; +}): React.ReactNode { + if (!verbose && typeof result === 'string' && extractTag(result, 'tool_use_error')) { + return + Error editing notebook + ; + } + return ; +} +export function renderToolResultMessage({ + cell_id, + new_source, + error +}: Output): React.ReactNode { + if (error) { + return + {error} + ; + } + return + + + Updated cell {cell_id}: + + + + + + ; +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJUb29sUmVzdWx0QmxvY2tQYXJhbSIsIlJlYWN0IiwiTWVzc2FnZSIsIlByb2dyZXNzTWVzc2FnZSIsImV4dHJhY3RUYWciLCJUaGVtZU5hbWUiLCJ6IiwiRmFsbGJhY2tUb29sVXNlRXJyb3JNZXNzYWdlIiwiRmlsZVBhdGhMaW5rIiwiSGlnaGxpZ2h0ZWRDb2RlIiwi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LENBQUMsQ0FBQ2pCLE9BQU8sQ0FBQyxFQUFFLElBQUksQ0FBQztBQUNsRCxRQUFRLEVBQUUsSUFBSTtBQUNkLFFBQVEsQ0FBQyxHQUFHLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQyxDQUFDO0FBQzNCLFVBQVUsQ0FBQyxlQUFlLENBQUMsSUFBSSxDQUFDLENBQUNDLFVBQVUsQ0FBQyxDQUFDLFFBQVEsQ0FBQyxhQUFhO0FBQ25FLFFBQVEsRUFBRSxHQUFHO0FBQ2IsTUFBTSxFQUFFLEdBQUc7QUFDWCxJQUFJLEVBQUUsZUFBZSxDQUFDO0FBRXRCIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/tools/OverflowTestTool/OverflowTestTool.ts b/_all-in-one-upload/cc-haha/src/tools/OverflowTestTool/OverflowTestTool.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/OverflowTestTool/OverflowTestTool.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/clmTypes.ts b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/clmTypes.ts new file mode 100644 index 0000000..a615563 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/clmTypes.ts @@ -0,0 +1,211 @@ +/** + * PowerShell Constrained Language Mode allowed types. + * + * Microsoft's CLM restricts .NET type usage to this allowlist when PS runs + * under AppLocker/WDAC system lockdown. Any type NOT in this set is considered + * unsafe for untrusted code execution. + * + * We invert this: type literals not in this set → ask. One canonical check + * replaces enumerating individual dangerous types (named pipes, reflection, + * process spawning, P/Invoke marshaling, etc.). Microsoft maintains the list. + * + * Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes + * + * Normalization: entries stored lowercase, short AND full names where both + * exist (PS resolves type accelerators like [int] → System.Int32 at runtime; + * we match against what the AST emits, which is the literal text). + */ +export const CLM_ALLOWED_TYPES: ReadonlySet = new Set( + [ + // Type accelerators (short names as they appear in AST TypeName.Name) + // SECURITY: 'adsi' and 'adsisearcher' REMOVED. Both are Active Directory + // Service Interface types that perform NETWORK BINDS when cast: + // [adsi]'LDAP://evil.com/...' → connects to LDAP server + // [adsisearcher]'(objectClass=user)' → binds to AD and queries + // Microsoft's CLM allows these because it's for Windows admins in trusted + // domains; we block them since the target isn't validated. + 'alias', + 'allowemptycollection', + 'allowemptystring', + 'allownull', + 'argumentcompleter', + 'argumentcompletions', + 'array', + 'bigint', + 'bool', + 'byte', + 'char', + 'cimclass', + 'cimconverter', + 'ciminstance', + // 'cimsession' REMOVED — see wmi/adsi comment below + 'cimtype', + 'cmdletbinding', + 'cultureinfo', + 'datetime', + 'decimal', + 'double', + 'dsclocalconfigurationmanager', + 'dscproperty', + 'dscresource', + 'experimentaction', + 'experimental', + 'experimentalfeature', + 'float', + 'guid', + 'hashtable', + 'int', + 'int16', + 'int32', + 'int64', + 'ipaddress', + 'ipendpoint', + 'long', + 'mailaddress', + 'norunspaceaffinity', + 'nullstring', + 'objectsecurity', + 'ordered', + 'outputtype', + 'parameter', + 'physicaladdress', + 'pscredential', + 'pscustomobject', + 'psdefaultvalue', + 'pslistmodifier', + 'psobject', + 'psprimitivedictionary', + 'pstypenameattribute', + 'ref', + 'regex', + 'sbyte', + 'securestring', + 'semver', + 'short', + 'single', + 'string', + 'supportswildcards', + 'switch', + 'timespan', + 'uint', + 'uint16', + 'uint32', + 'uint64', + 'ulong', + 'uri', + 'ushort', + 'validatecount', + 'validatedrive', + 'validatelength', + 'validatenotnull', + 'validatenotnullorempty', + 'validatenotnullorwhitespace', + 'validatepattern', + 'validaterange', + 'validatescript', + 'validateset', + 'validatetrusteddata', + 'validateuserdrive', + 'version', + 'void', + 'wildcardpattern', + // SECURITY: 'wmi', 'wmiclass', 'wmisearcher', 'cimsession' REMOVED. + // WMI type casts perform WMI queries which can target remote computers + // (network request) and access dangerous classes like Win32_Process. + // cimsession creates a CIM session (network connection to remote host). + // [wmi]'\\evil-host\root\cimv2:Win32_Process.Handle="1"' → remote WMI + // [wmisearcher]'SELECT * FROM Win32_Process' → runs WQL query + // Same rationale as adsi/adsisearcher removal above. + 'x500distinguishedname', + 'x509certificate', + 'xml', + // Full names for accelerators that resolve to System.* (AST may emit either) + 'system.array', + 'system.boolean', + 'system.byte', + 'system.char', + 'system.datetime', + 'system.decimal', + 'system.double', + 'system.guid', + 'system.int16', + 'system.int32', + 'system.int64', + 'system.numerics.biginteger', + 'system.sbyte', + 'system.single', + 'system.string', + 'system.timespan', + 'system.uint16', + 'system.uint32', + 'system.uint64', + 'system.uri', + 'system.version', + 'system.void', + 'system.collections.hashtable', + 'system.text.regularexpressions.regex', + 'system.globalization.cultureinfo', + 'system.net.ipaddress', + 'system.net.ipendpoint', + 'system.net.mail.mailaddress', + 'system.net.networkinformation.physicaladdress', + 'system.security.securestring', + 'system.security.cryptography.x509certificates.x509certificate', + 'system.security.cryptography.x509certificates.x500distinguishedname', + 'system.xml.xmldocument', + // System.Management.Automation.* — FQ equivalents of PS-specific accelerators + 'system.management.automation.pscredential', + 'system.management.automation.pscustomobject', + 'system.management.automation.pslistmodifier', + 'system.management.automation.psobject', + 'system.management.automation.psprimitivedictionary', + 'system.management.automation.psreference', + 'system.management.automation.semanticversion', + 'system.management.automation.switchparameter', + 'system.management.automation.wildcardpattern', + 'system.management.automation.language.nullstring', + // Microsoft.Management.Infrastructure.* — FQ equivalents of CIM accelerators + // SECURITY: cimsession FQ REMOVED — same network-bind hazard as short name + // (creates a CIM session to a remote host). + 'microsoft.management.infrastructure.cimclass', + 'microsoft.management.infrastructure.cimconverter', + 'microsoft.management.infrastructure.ciminstance', + 'microsoft.management.infrastructure.cimtype', + // FQ equivalents of remaining short-name accelerators + // SECURITY: DirectoryEntry/DirectorySearcher/ManagementObject/ + // ManagementClass/ManagementObjectSearcher FQ REMOVED — same network-bind + // hazard as short names adsi/adsisearcher/wmi/wmiclass/wmisearcher + // (LDAP bind, remote WMI). See short-name removal comments above. + 'system.collections.specialized.ordereddictionary', + 'system.security.accesscontrol.objectsecurity', + // Arrays of allowed types are allowed (e.g. [string[]]) + // normalizeTypeName strips [] before lookup, so store the base name + 'object', + 'system.object', + // ModuleSpecification — full qualified name + 'microsoft.powershell.commands.modulespecification', + ].map(t => t.toLowerCase()), +) + +/** + * Normalize a type name from AST TypeName.FullName or TypeName.Name. + * Handles array suffix ([]) and generic brackets. + */ +export function normalizeTypeName(name: string): string { + // Strip array suffix: "String[]" → "string" (arrays of allowed types are allowed) + // Strip generic args: "List[int]" → "list" (conservative — the generic wrapper + // might be unsafe even if the type arg is safe, so we check the outer type) + return name + .toLowerCase() + .replace(/\[\]$/, '') + .replace(/\[.*\]$/, '') + .trim() +} + +/** + * True if typeName (from AST) is in Microsoft's CLM allowlist. + * Types NOT in this set trigger ask — they access system APIs CLM blocks. + */ +export function isClmAllowedType(typeName: string): boolean { + return CLM_ALLOWED_TYPES.has(normalizeTypeName(typeName)) +} diff --git a/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/commandSemantics.ts b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/commandSemantics.ts new file mode 100644 index 0000000..2fd66fc --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/commandSemantics.ts @@ -0,0 +1,142 @@ +/** + * Command semantics configuration for interpreting exit codes in PowerShell. + * + * PowerShell-native cmdlets do NOT need exit-code semantics: + * - Select-String (grep equivalent) exits 0 on no-match (returns $null) + * - Compare-Object (diff equivalent) exits 0 regardless + * - Test-Path exits 0 regardless (returns bool via pipeline) + * Native cmdlets signal failure via terminating errors ($?), not exit codes. + * + * However, EXTERNAL executables invoked from PowerShell DO set $LASTEXITCODE, + * and many use non-zero codes to convey information rather than failure: + * - grep.exe / rg.exe (Git for Windows, scoop, etc.): 1 = no match + * - findstr.exe (Windows native): 1 = no match + * - robocopy.exe (Windows native): 0-7 = success, 8+ = error (notorious!) + * + * Without this module, PowerShellTool throws ShellError on any non-zero exit, + * so `robocopy` reporting "files copied successfully" (exit 1) shows as an error. + */ + +export type CommandSemantic = ( + exitCode: number, + stdout: string, + stderr: string, +) => { + isError: boolean + message?: string +} + +/** + * Default semantic: treat only 0 as success, everything else as error + */ +const DEFAULT_SEMANTIC: CommandSemantic = (exitCode, _stdout, _stderr) => ({ + isError: exitCode !== 0, + message: + exitCode !== 0 ? `Command failed with exit code ${exitCode}` : undefined, +}) + +/** + * grep / ripgrep: 0 = matches found, 1 = no matches, 2+ = error + */ +const GREP_SEMANTIC: CommandSemantic = (exitCode, _stdout, _stderr) => ({ + isError: exitCode >= 2, + message: exitCode === 1 ? 'No matches found' : undefined, +}) + +/** + * Command-specific semantics for external executables. + * Keys are lowercase command names WITHOUT .exe suffix. + * + * Deliberately omitted: + * - 'diff': Ambiguous. Windows PowerShell 5.1 aliases `diff` → Compare-Object + * (exit 0 on differ), but PS Core / Git for Windows may resolve to diff.exe + * (exit 1 on differ). Cannot reliably interpret. + * - 'fc': Ambiguous. PowerShell aliases `fc` → Format-Custom (a native cmdlet), + * but `fc.exe` is the Windows file compare utility (exit 1 = files differ). + * Same aliasing problem as `diff`. + * - 'find': Ambiguous. Windows find.exe (text search) vs Unix find.exe + * (file search via Git for Windows) have different semantics. + * - 'test', '[': Not PowerShell constructs. + * - 'select-string', 'compare-object', 'test-path': Native cmdlets exit 0. + */ +const COMMAND_SEMANTICS: Map = new Map([ + // External grep/ripgrep (Git for Windows, scoop, choco) + ['grep', GREP_SEMANTIC], + ['rg', GREP_SEMANTIC], + + // findstr.exe: Windows native text search + // 0 = match found, 1 = no match, 2 = error + ['findstr', GREP_SEMANTIC], + + // robocopy.exe: Windows native robust file copy + // Exit codes are a BITFIELD — 0-7 are success, 8+ indicates at least one failure: + // 0 = no files copied, no mismatch, no failures (already in sync) + // 1 = files copied successfully + // 2 = extra files/dirs detected (no copy) + // 4 = mismatched files/dirs detected + // 8 = some files/dirs could not be copied (copy errors) + // 16 = serious error (robocopy did not copy any files) + // This is the single most common "CI failed but nothing's wrong" Windows gotcha. + [ + 'robocopy', + (exitCode, _stdout, _stderr) => ({ + isError: exitCode >= 8, + message: + exitCode === 0 + ? 'No files copied (already in sync)' + : exitCode >= 1 && exitCode < 8 + ? exitCode & 1 + ? 'Files copied successfully' + : 'Robocopy completed (no errors)' + : undefined, + }), + ], +]) + +/** + * Extract the command name from a single pipeline segment. + * Strips leading `&` / `.` call operators and `.exe` suffix, lowercases. + */ +function extractBaseCommand(segment: string): string { + // Strip PowerShell call operators: & "cmd", . "cmd" + // (& and . at segment start followed by whitespace invoke the next token) + const stripped = segment.trim().replace(/^[&.]\s+/, '') + const firstToken = stripped.split(/\s+/)[0] || '' + // Strip surrounding quotes if command was invoked as & "grep.exe" + const unquoted = firstToken.replace(/^["']|["']$/g, '') + // Strip path: C:\bin\grep.exe → grep.exe, .\rg.exe → rg.exe + const basename = unquoted.split(/[\\/]/).pop() || unquoted + // Strip .exe suffix (Windows is case-insensitive) + return basename.toLowerCase().replace(/\.exe$/, '') +} + +/** + * Extract the primary command from a PowerShell command line. + * Takes the LAST pipeline segment since that determines the exit code. + * + * Heuristic split on `;` and `|` — may get it wrong for quoted strings or + * complex constructs. Do NOT depend on this for security; it's only used + * for exit-code interpretation (false negatives just fall back to default). + */ +function heuristicallyExtractBaseCommand(command: string): string { + const segments = command.split(/[;|]/).filter(s => s.trim()) + const last = segments[segments.length - 1] || command + return extractBaseCommand(last) +} + +/** + * Interpret command result based on semantic rules + */ +export function interpretCommandResult( + command: string, + exitCode: number, + stdout: string, + stderr: string, +): { + isError: boolean + message?: string +} { + const baseCommand = heuristicallyExtractBaseCommand(command) + const semantic = COMMAND_SEMANTICS.get(baseCommand) ?? DEFAULT_SEMANTIC + return semantic(exitCode, stdout, stderr) +} diff --git a/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/destructiveCommandWarning.ts b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/destructiveCommandWarning.ts new file mode 100644 index 0000000..14f436d --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/destructiveCommandWarning.ts @@ -0,0 +1,109 @@ +/** + * Detects potentially destructive PowerShell commands and returns a warning + * string for display in the permission dialog. This is purely informational + * -- it doesn't affect permission logic or auto-approval. + */ + +type DestructivePattern = { + pattern: RegExp + warning: string +} + +const DESTRUCTIVE_PATTERNS: DestructivePattern[] = [ + // Remove-Item with -Recurse and/or -Force (and common aliases) + // Anchored to statement start (^, |, ;, &, newline, {, () so `git rm --force` + // doesn't match — \b would match `rm` after any word boundary. The `{(` + // chars catch scriptblock/group bodies: `{ rm -Force ./x }`. The stopper + // adds only `}` (NOT `)`) — `}` ends a block so flags after it belong to a + // different statement (`if {rm} else {... -Force}`), but `)` closes a path + // grouping and flags after it are still this command's flags: + // `Remove-Item (Join-Path $r "tmp") -Recurse -Force` must still warn. + { + pattern: + /(?:^|[|;&\n({])\s*(Remove-Item|rm|del|rd|rmdir|ri)\b[^|;&\n}]*-Recurse\b[^|;&\n}]*-Force\b/i, + warning: 'Note: may recursively force-remove files', + }, + { + pattern: + /(?:^|[|;&\n({])\s*(Remove-Item|rm|del|rd|rmdir|ri)\b[^|;&\n}]*-Force\b[^|;&\n}]*-Recurse\b/i, + warning: 'Note: may recursively force-remove files', + }, + { + pattern: + /(?:^|[|;&\n({])\s*(Remove-Item|rm|del|rd|rmdir|ri)\b[^|;&\n}]*-Recurse\b/i, + warning: 'Note: may recursively remove files', + }, + { + pattern: + /(?:^|[|;&\n({])\s*(Remove-Item|rm|del|rd|rmdir|ri)\b[^|;&\n}]*-Force\b/i, + warning: 'Note: may force-remove files', + }, + + // Clear-Content on broad paths + { + pattern: /\bClear-Content\b[^|;&\n]*\*/i, + warning: 'Note: may clear content of multiple files', + }, + + // Format-Volume and Clear-Disk + { + pattern: /\bFormat-Volume\b/i, + warning: 'Note: may format a disk volume', + }, + { + pattern: /\bClear-Disk\b/i, + warning: 'Note: may clear a disk', + }, + + // Git destructive operations (same as BashTool) + { + pattern: /\bgit\s+reset\s+--hard\b/i, + warning: 'Note: may discard uncommitted changes', + }, + { + pattern: /\bgit\s+push\b[^|;&\n]*\s+(--force|--force-with-lease|-f)\b/i, + warning: 'Note: may overwrite remote history', + }, + { + pattern: + /\bgit\s+clean\b(?![^|;&\n]*(?:-[a-zA-Z]*n|--dry-run))[^|;&\n]*-[a-zA-Z]*f/i, + warning: 'Note: may permanently delete untracked files', + }, + { + pattern: /\bgit\s+stash\s+(drop|clear)\b/i, + warning: 'Note: may permanently remove stashed changes', + }, + + // Database operations + { + pattern: /\b(DROP|TRUNCATE)\s+(TABLE|DATABASE|SCHEMA)\b/i, + warning: 'Note: may drop or truncate database objects', + }, + + // System operations + { + pattern: /\bStop-Computer\b/i, + warning: 'Note: will shut down the computer', + }, + { + pattern: /\bRestart-Computer\b/i, + warning: 'Note: will restart the computer', + }, + { + pattern: /\bClear-RecycleBin\b/i, + warning: 'Note: permanently deletes recycled files', + }, +] + +/** + * Checks if a PowerShell command matches known destructive patterns. + * Returns a human-readable warning string, or null if no destructive pattern is detected. + */ +export function getDestructiveCommandWarning(command: string): string | null { + for (const { pattern, warning } of DESTRUCTIVE_PATTERNS) { + if (pattern.test(command)) { + return warning + } + } + return null +} diff --git a/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/powershellSecurity.ts b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/powershellSecurity.ts new file mode 100644 index 0000000..8439f86 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/PowerShellTool/powershellSecurity.ts @@ -0,0 +1,1090 @@ +/** + * PowerShell-specific security analysis for command validation. + * + * Detects dangerous patterns: code injection, download cradles, privilege + * escalation, dynamic command names, COM objects, etc. + * + * All checks are AST-based. If parsing failed (valid=false), none of the + * individual checks match and powershellCommandIsSafe returns 'ask'. + */ + +import { + DANGEROUS_SCRIPT_BLOCK_CMDLETS, + FILEPATH_EXECUTION_CMDLETS, + MODULE_LOADING_CMDLETS, +} from '../../utils/powershell/dangerousCmdlets.js' +import type { + ParsedCommandElement, + ParsedPowerShellCommand, +} from '../../utils/powershell/parser.js' +import { + COMMON_ALIASES, + commandHasArgAbbreviation, + deriveSecurityFlags, + getAllCommands, + getVariablesByScope, + hasCommandNamed, +} from '../../utils/powershell/parser.js' +import { isClmAllowedType } from './clmTypes.js' + +type PowerShellSecurityResult = { + behavior: 'passthrough' | 'ask' | 'allow' + message?: string +} + +const POWERSHELL_EXECUTABLES = new Set([ + 'pwsh', + 'pwsh.exe', + 'powershell', + 'powershell.exe', +]) + +/** + * Extracts the base executable name from a command, handling full paths + * like /usr/bin/pwsh, C:\Windows\...\powershell.exe, or .\pwsh. + */ +function isPowerShellExecutable(name: string): boolean { + const lower = name.toLowerCase() + if (POWERSHELL_EXECUTABLES.has(lower)) { + return true + } + // Extract basename from paths (both / and \ separators) + const lastSep = Math.max(lower.lastIndexOf('/'), lower.lastIndexOf('\\')) + if (lastSep >= 0) { + return POWERSHELL_EXECUTABLES.has(lower.slice(lastSep + 1)) + } + return false +} + +/** + * Alternative parameter-prefix characters that PowerShell accepts as equivalent + * to ASCII hyphen-minus (U+002D). PowerShell's tokenizer (SpecialCharacters.IsDash) + * and powershell.exe's CommandLineParameterParser both accept all four dash + * characters plus Windows PowerShell 5.1's `/` parameter delimiter. + * Extent.Text preserves the raw character; transformCommandAst uses ce.text for + * CommandParameterAst elements, so these reach us unchanged. + */ +const PS_ALT_PARAM_PREFIXES = new Set([ + '/', // Windows PowerShell 5.1 (powershell.exe, not pwsh 7+) + '\u2013', // en-dash + '\u2014', // em-dash + '\u2015', // horizontal bar +]) + +/** + * Wrapper around commandHasArgAbbreviation that also matches alternative + * parameter prefixes (`/`, en-dash, em-dash, horizontal-bar). PowerShell's + * tokenizer (SpecialCharacters.IsDash) accepts these for both powershell.exe + * args AND cmdlet parameters, so use this for ALL PS param checks — not just + * pwsh.exe invocations. Previously checkComObject/checkStartProcess/ + * checkDangerousFilePathExecution/checkForEachMemberName used bare + * commandHasArgAbbreviation, so `Start-Process foo –Verb RunAs` bypassed. + */ +function psExeHasParamAbbreviation( + cmd: ParsedCommandElement, + fullParam: string, + minPrefix: string, +): boolean { + if (commandHasArgAbbreviation(cmd, fullParam, minPrefix)) { + return true + } + // Normalize alternative prefixes to `-` and re-check. Build a synthetic cmd + // with normalized args; commandHasArgAbbreviation handles colon-value split. + const normalized: ParsedCommandElement = { + ...cmd, + args: cmd.args.map(a => + a.length > 0 && PS_ALT_PARAM_PREFIXES.has(a[0]!) ? '-' + a.slice(1) : a, + ), + } + return commandHasArgAbbreviation(normalized, fullParam, minPrefix) +} + +/** + * Checks if a PowerShell command uses Invoke-Expression or its alias (iex). + * These are equivalent to eval and can execute arbitrary code. + */ +function checkInvokeExpression( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (hasCommandNamed(parsed, 'Invoke-Expression')) { + return { + behavior: 'ask', + message: + 'Command uses Invoke-Expression which can execute arbitrary code', + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for dynamic command invocation where the command name itself is an + * expression that cannot be statically resolved. + * + * PoCs: + * & ${function:Invoke-Expression} 'payload' — VariableExpressionAst + * & ('iex','x')[0] 'payload' — IndexExpressionAst → 'Other' + * & ('i'+'ex') 'payload' — BinaryExpressionAst → 'Other' + * + * In all cases cmd.name is the literal extent text (e.g. "('iex','x')[0]"), + * which doesn't match hasCommandNamed('Invoke-Expression'). At runtime + * PowerShell evaluates the expression to a command name and invokes it. + * + * Legitimate command names are ALWAYS StringConstantExpressionAst (mapped to + * 'StringConstant'): `Get-Process`, `git`, `ls`. Any other element type in + * name position is dynamic. Rather than denylisting dynamic types (fragile — + * mapElementType's default case maps unknown AST types to 'Other', which a + * `=== 'Variable'` check misses), we allowlist 'StringConstant'. + * + * elementTypes[0] is the command-name element (transformCommandAst pushes it + * first, before arg elements). The `!== undefined` guard preserves fail-open + * when elementTypes is absent (parse-detail unavailable — if parsing failed + * entirely, valid=false already returns 'ask' earlier in the chain). + */ +function checkDynamicCommandName( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + if (cmd.elementType !== 'CommandAst') { + continue + } + const nameElementType = cmd.elementTypes?.[0] + if (nameElementType !== undefined && nameElementType !== 'StringConstant') { + return { + behavior: 'ask', + message: + 'Command name is a dynamic expression which cannot be statically validated', + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for encoded command parameters which obscure intent. + * These are commonly used in malware to bypass security tools. + */ +function checkEncodedCommand( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + if (isPowerShellExecutable(cmd.name)) { + if (psExeHasParamAbbreviation(cmd, '-encodedcommand', '-e')) { + return { + behavior: 'ask', + message: 'Command uses encoded parameters which obscure intent', + } + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for PowerShell re-invocation (nested pwsh/powershell process). + * + * Any PowerShell executable in command position is flagged — not just + * -Command/-File. Bare `pwsh` receiving stdin (`Get-Content x | pwsh`) or + * a positional script path executes arbitrary code with none of the explicit + * flags present. Same unvalidatable-nested-process reasoning as + * checkStartProcess vector 2: we cannot statically analyze what the child + * process will run. + */ +function checkPwshCommandOrFile( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + if (isPowerShellExecutable(cmd.name)) { + return { + behavior: 'ask', + message: + 'Command spawns a nested PowerShell process which cannot be validated', + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for download cradle patterns - common malware techniques + * that download and execute remote code. + * + * Per-statement: catches piped cradles (`IWR ... | IEX`). + * Cross-statement: catches split cradles (`$r = IWR ...; IEX $r.Content`). + * The cross-statement case is already blocked by checkInvokeExpression (which + * scans all statements), but this check improves the warning message. + */ +const DOWNLOADER_NAMES = new Set([ + 'invoke-webrequest', + 'iwr', + 'invoke-restmethod', + 'irm', + 'new-object', + 'start-bitstransfer', // MITRE T1197 +]) + +function isDownloader(name: string): boolean { + return DOWNLOADER_NAMES.has(name.toLowerCase()) +} + +function isIex(name: string): boolean { + const lower = name.toLowerCase() + return lower === 'invoke-expression' || lower === 'iex' +} + +function checkDownloadCradles( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + // Per-statement: piped cradle (IWR ... | IEX) + for (const statement of parsed.statements) { + const cmds = statement.commands + if (cmds.length < 2) { + continue + } + const hasDownloader = cmds.some(cmd => isDownloader(cmd.name)) + const hasIex = cmds.some(cmd => isIex(cmd.name)) + if (hasDownloader && hasIex) { + return { + behavior: 'ask', + message: 'Command downloads and executes remote code', + } + } + } + + // Cross-statement: split cradle ($r = IWR ...; IEX $r.Content). + // No new false positives: if IEX is present, checkInvokeExpression already asks. + const all = getAllCommands(parsed) + if (all.some(c => isDownloader(c.name)) && all.some(c => isIex(c.name))) { + return { + behavior: 'ask', + message: 'Command downloads and executes remote code', + } + } + + return { behavior: 'passthrough' } +} + +/** + * Checks for standalone download utilities — LOLBAS tools commonly used to + * fetch payloads. Unlike checkDownloadCradles (which requires download + IEX + * in-pipeline), this flags the download operation itself. + * + * Start-BitsTransfer: always a file transfer (MITRE T1197). + * certutil -urlcache: classic LOLBAS download. Only flagged with -urlcache; + * bare `certutil` has many legitimate cert-management uses. + * bitsadmin /transfer: legacy BITS download (pre-PowerShell). + */ +function checkDownloadUtilities( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + // Start-BitsTransfer is purpose-built for file transfer — no safe variant. + if (lower === 'start-bitstransfer') { + return { + behavior: 'ask', + message: 'Command downloads files via BITS transfer', + } + } + // certutil / certutil.exe — only when -urlcache is present. certutil has + // many non-download uses (cert store queries, encoding, etc.). + // certutil.exe accepts both -urlcache and /urlcache per standard Windows + // utility convention — check both forms (bitsadmin below does the same). + if (lower === 'certutil' || lower === 'certutil.exe') { + const hasUrlcache = cmd.args.some(a => { + const la = a.toLowerCase() + return la === '-urlcache' || la === '/urlcache' + }) + if (hasUrlcache) { + return { + behavior: 'ask', + message: 'Command uses certutil to download from a URL', + } + } + } + // bitsadmin /transfer — legacy BITS CLI, same threat as Start-BitsTransfer. + if (lower === 'bitsadmin' || lower === 'bitsadmin.exe') { + if (cmd.args.some(a => a.toLowerCase() === '/transfer')) { + return { + behavior: 'ask', + message: 'Command downloads files via BITS transfer', + } + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for Add-Type usage which compiles and loads .NET code at runtime. + * This can be used to execute arbitrary compiled code. + */ +function checkAddType( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (hasCommandNamed(parsed, 'Add-Type')) { + return { + behavior: 'ask', + message: 'Command compiles and loads .NET code', + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for New-Object -ComObject. COM objects like WScript.Shell, + * Shell.Application, MMC20.Application, Schedule.Service, Msxml2.XMLHTTP + * have their own execution/download capabilities — no IEX required. + * + * We can't enumerate all dangerous ProgIDs, so flag any -ComObject. Object + * creation alone is inert, but the prompt should warn the user that COM + * instantiation is an execution primitive. Method invocation on the result + * (.Run(), .Exec()) is separately caught by checkMemberInvocations. + */ +function checkComObject( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + if (cmd.name.toLowerCase() !== 'new-object') { + continue + } + // -ComObject min abbrev is -com (New-Object params: -TypeName, -ComObject, + // -ArgumentList, -Property, -Strict; -co is ambiguous in PS5.1 due to + // common params like -Confirm, so use -com). + if (psExeHasParamAbbreviation(cmd, '-comobject', '-com')) { + return { + behavior: 'ask', + message: + 'Command instantiates a COM object which may have execution capabilities', + } + } + // SECURITY: checkTypeLiterals only sees [bracket] syntax from + // parsed.typeLiterals. `New-Object System.Net.WebClient` passes the type + // as a STRING ARG (StringConstantExpressionAst), not a TypeExpressionAst, + // so CLM never fires. Extract -TypeName (named, colon-bound, or + // positional-0) and run through isClmAllowedType. Closes attackVectors D4. + let typeName: string | undefined + for (let i = 0; i < cmd.args.length; i++) { + const a = cmd.args[i]! + const lower = a.toLowerCase() + // -TypeName abbrev: -t is unambiguous (no other New-Object -t* params). + // Handle colon-bound form first: -TypeName:Foo.Bar + if (lower.startsWith('-t') && lower.includes(':')) { + const colonIdx = a.indexOf(':') + const paramPart = lower.slice(0, colonIdx) + if ('-typename'.startsWith(paramPart)) { + typeName = a.slice(colonIdx + 1) + break + } + } + // Space-separated form: -TypeName Foo.Bar + if ( + lower.startsWith('-t') && + '-typename'.startsWith(lower) && + cmd.args[i + 1] !== undefined + ) { + typeName = cmd.args[i + 1] + break + } + } + // Positional-0 binds to -TypeName (NetParameterSet default). Named params + // (-Strict, -ArgumentList, -Property, -ComObject) may appear before the + // positional TypeName, so scan past them to find the first non-consumed arg. + if (typeName === undefined) { + // New-Object named params that consume a following value argument + const VALUE_PARAMS = new Set(['-argumentlist', '-comobject', '-property']) + // Switch params (no value argument) + const SWITCH_PARAMS = new Set(['-strict']) + for (let i = 0; i < cmd.args.length; i++) { + const a = cmd.args[i]! + if (a.startsWith('-')) { + const lower = a.toLowerCase() + // Skip -TypeName variants (already handled by named-param loop above) + if (lower.startsWith('-t') && '-typename'.startsWith(lower)) { + i++ // skip value + continue + } + // Colon-bound form: -Param:Value (single token, no skip needed) + if (lower.includes(':')) continue + if (SWITCH_PARAMS.has(lower)) continue + if (VALUE_PARAMS.has(lower)) { + i++ // skip value + continue + } + // Unknown param — skip conservatively + continue + } + // First non-dash arg is the positional TypeName + typeName = a + break + } + } + if (typeName !== undefined && !isClmAllowedType(typeName)) { + return { + behavior: 'ask', + message: `New-Object instantiates .NET type '${typeName}' outside the ConstrainedLanguage allowlist`, + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for DANGEROUS_SCRIPT_BLOCK_CMDLETS invoked with -FilePath (or + * -LiteralPath). These run a script file — arbitrary code execution with no + * ScriptBlockAst in the tree. + * + * checkScriptBlockInjection only fires when hasScriptBlocks is true. With + * -FilePath there is no ScriptBlockAst, so DANGEROUS_SCRIPT_BLOCK_CMDLETS is + * never consulted. This check closes that gap for the -FilePath vector. + * + * Cmdlets in DANGEROUS_SCRIPT_BLOCK_CMDLETS that accept -FilePath: + * Invoke-Command -FilePath (icm alias via COMMON_ALIASES) + * Start-Job -FilePath, -LiteralPath + * Start-ThreadJob -FilePath + * Register-ScheduledJob -FilePath + * The *-PSSession and Register-*Event entries do not accept -FilePath. + * + * -f is unambiguous for -FilePath on all four (no other -f* params). + * -l is unambiguous for -LiteralPath on Start-Job; harmless no-op on the + * others (no -l* params to collide with). + */ + +function checkDangerousFilePathExecution( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + const resolved = COMMON_ALIASES[lower]?.toLowerCase() ?? lower + if (!FILEPATH_EXECUTION_CMDLETS.has(resolved)) { + continue + } + if ( + psExeHasParamAbbreviation(cmd, '-filepath', '-f') || + psExeHasParamAbbreviation(cmd, '-literalpath', '-l') + ) { + return { + behavior: 'ask', + message: `${cmd.name} -FilePath executes an arbitrary script file`, + } + } + // Positional binding: `Start-Job script.ps1` binds position-0 to + // -FilePath via FilePathParameterSet resolution (ScriptBlock args select + // ScriptBlockParameterSet instead). Same pattern as checkForEachMemberName: + // any non-dash StringConstant is a potential -FilePath. Over-flagging + // (e.g., `Start-Job -Name foo` where `foo` is StringConstant) is fail-safe. + for (let i = 0; i < cmd.args.length; i++) { + const argType = cmd.elementTypes?.[i + 1] + const arg = cmd.args[i] + if (argType === 'StringConstant' && arg && !arg.startsWith('-')) { + return { + behavior: 'ask', + message: `${cmd.name} with positional string argument binds to -FilePath and executes a script file`, + } + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for ForEach-Object -MemberName. Invokes a method by string name on + * every piped object — semantically equivalent to `| % { $_.Method() }` but + * without any ScriptBlockAst or InvokeMemberExpressionAst in the tree. + * + * PoC: `Get-Process | ForEach-Object -MemberName Kill` → kills all processes. + * checkScriptBlockInjection misses it (no script block); checkMemberInvocations + * misses it (no .Method() syntax). Aliases `%` and `foreach` resolve via + * COMMON_ALIASES. + */ +function checkForEachMemberName( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + const resolved = COMMON_ALIASES[lower]?.toLowerCase() ?? lower + if (resolved !== 'foreach-object') { + continue + } + // ForEach-Object params starting with -m: only -MemberName. -m is unambiguous. + if (psExeHasParamAbbreviation(cmd, '-membername', '-m')) { + return { + behavior: 'ask', + message: + 'ForEach-Object -MemberName invokes methods by string name which cannot be validated', + } + } + // PS7+: `ForEach-Object Kill` binds a positional string arg to + // -MemberName via MemberSet parameter-set resolution (ScriptBlock args + // select ScriptBlockSet instead). Scan ALL args — `-Verbose Kill` or + // `-ErrorAction Stop Kill` still binds Kill positionally. Any non-dash + // StringConstant is a potential -MemberName; over-flagging is fail-safe. + for (let i = 0; i < cmd.args.length; i++) { + const argType = cmd.elementTypes?.[i + 1] + const arg = cmd.args[i] + if (argType === 'StringConstant' && arg && !arg.startsWith('-')) { + return { + behavior: 'ask', + message: + 'ForEach-Object with positional string argument binds to -MemberName and invokes methods by name', + } + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Checks for dangerous Start-Process patterns. + * + * Two vectors: + * 1. `-Verb RunAs` — privilege escalation (UAC prompt). + * 2. Launching a PowerShell executable — nested invocation. + * `Start-Process pwsh -ArgumentList "-e "` evades + * checkEncodedCommand/checkPwshCommandOrFile because cmd.name is + * `Start-Process`, not `pwsh`. The `-e` lives inside the -ArgumentList + * string value and is never parsed as a param on the outer command. + * Rather than parse -ArgumentList contents (fragile — it's an opaque + * string or array), flag any Start-Process whose target is a PS + * executable: the nested invocation is unvalidatable by construction. + */ +function checkStartProcess( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + if (lower !== 'start-process' && lower !== 'saps' && lower !== 'start') { + continue + } + // Vector 1: -Verb RunAs (space or colon syntax). + // Space syntax: psExeHasParamAbbreviation finds -Verb/-v, then scan args + // for a bare 'runas' token. + if ( + psExeHasParamAbbreviation(cmd, '-Verb', '-v') && + cmd.args.some(a => a.toLowerCase() === 'runas') + ) { + return { + behavior: 'ask', + message: 'Command requests elevated privileges', + } + } + // Colon syntax — two layers: + // (a) Structural: PR #23554 added children[] for colon-bound param args. + // children[i] = [{type, text}] for the bound value. Check if any + // -v*-prefixed param has a child whose text normalizes (strip + // quotes/backtick/whitespace) to 'runas'. Robust against arbitrary + // quoting the regex can't anticipate. + // (b) Regex fallback: for parsed output without children[] or as + // defense-in-depth. -Verb:'RunAs', -Verb:"RunAs", -Verb:`runas all + // bypassed the old /...:runas$/ pattern because the quote/tick broke + // the match. + if (cmd.children) { + for (let i = 0; i < cmd.args.length; i++) { + // Strip backticks before matching param name (bug #14): -V`erb:RunAs + const argClean = cmd.args[i]!.replace(/`/g, '') + if (!/^[-\u2013\u2014\u2015/]v[a-z]*:/i.test(argClean)) continue + const kids = cmd.children[i] + if (!kids) continue + for (const child of kids) { + if (child.text.replace(/['"`\s]/g, '').toLowerCase() === 'runas') { + return { + behavior: 'ask', + message: 'Command requests elevated privileges', + } + } + } + } + } + if ( + cmd.args.some(a => { + // Strip backticks before matching (bug #14 / review nit #2) + const clean = a.replace(/`/g, '') + return /^[-\u2013\u2014\u2015/]v[a-z]*:['"` ]*runas['"` ]*$/i.test( + clean, + ) + }) + ) { + return { + behavior: 'ask', + message: 'Command requests elevated privileges', + } + } + // Vector 2: Start-Process targeting a PowerShell executable. + // Target is either the first positional arg or the value after -FilePath. + // Scan all args — any PS-executable token present is treated as the launch + // target. Known false-positive: path-valued params (-WorkingDirectory, + // -RedirectStandard*) whose basename is pwsh/powershell — + // isPowerShellExecutable extracts basenames from paths, so + // `-WorkingDirectory C:\projects\pwsh` triggers. Accepted trade-off: + // Start-Process is not in CMDLET_ALLOWLIST (always prompts regardless), + // result is ask not reject, and correctly parsing Start-Process parameter + // binding is fragile. Strip quotes the parser may have preserved. + for (const arg of cmd.args) { + const stripped = arg.replace(/^['"]|['"]$/g, '') + if (isPowerShellExecutable(stripped)) { + return { + behavior: 'ask', + message: + 'Start-Process launches a nested PowerShell process which cannot be validated', + } + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Cmdlets where script blocks are safe (filtering/output cmdlets). + * Script blocks piped to these are just predicates or projections, not arbitrary execution. + */ +const SAFE_SCRIPT_BLOCK_CMDLETS = new Set([ + 'where-object', + 'sort-object', + 'select-object', + 'group-object', + 'format-table', + 'format-list', + 'format-wide', + 'format-custom', + // NOT foreach-object — its block is arbitrary script, not a predicate. + // getAllCommands recurses so commands inside the block ARE checked, but + // non-command AST nodes (AssignmentStatementAst etc.) are invisible to it. + // See powershellPermissions.ts step-5 hasScriptBlocks guard. +]) + +/** + * Checks for script block injection patterns where script blocks + * appear in suspicious contexts that could execute arbitrary code. + * + * Script blocks used with safe filtering/output cmdlets (Where-Object, + * Sort-Object, Select-Object, Group-Object) are allowed. + * Script blocks used with dangerous cmdlets (Invoke-Command, Invoke-Expression, + * Start-Job, etc.) are flagged. + */ +function checkScriptBlockInjection( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + const security = deriveSecurityFlags(parsed) + if (!security.hasScriptBlocks) { + return { behavior: 'passthrough' } + } + + // Check all commands in the parsed result. If any command is in the + // dangerous set, flag it. If all commands with script blocks are in + // the safe set (or the allowlist), allow it. + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + if (DANGEROUS_SCRIPT_BLOCK_CMDLETS.has(lower)) { + return { + behavior: 'ask', + message: + 'Command contains script block with dangerous cmdlet that may execute arbitrary code', + } + } + } + + // Check if all commands are either safe script block consumers or don't use script blocks + const allCommandsSafe = getAllCommands(parsed).every(cmd => { + const lower = cmd.name.toLowerCase() + // Safe filtering/output cmdlets + if (SAFE_SCRIPT_BLOCK_CMDLETS.has(lower)) { + return true + } + // Resolve aliases + const alias = COMMON_ALIASES[lower] + if (alias && SAFE_SCRIPT_BLOCK_CMDLETS.has(alias.toLowerCase())) { + return true + } + // Unknown command with script blocks present — flag as potentially dangerous + return false + }) + + if (allCommandsSafe) { + return { behavior: 'passthrough' } + } + + return { + behavior: 'ask', + message: 'Command contains script block that may execute arbitrary code', + } +} + +/** + * AST-only check: Detects subexpressions $() which can hide command execution. + */ +function checkSubExpressions( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (deriveSecurityFlags(parsed).hasSubExpressions) { + return { + behavior: 'ask', + message: 'Command contains subexpressions $()', + } + } + return { behavior: 'passthrough' } +} + +/** + * AST-only check: Detects expandable strings (double-quoted) with embedded + * expressions like "$env:PATH" or "$(dangerous-command)". These can hide + * command execution or variable interpolation inside string literals. + */ +function checkExpandableStrings( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (deriveSecurityFlags(parsed).hasExpandableStrings) { + return { + behavior: 'ask', + message: 'Command contains expandable strings with embedded expressions', + } + } + return { behavior: 'passthrough' } +} + +/** + * AST-only check: Detects splatting (@variable) which can obscure arguments. + */ +function checkSplatting( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (deriveSecurityFlags(parsed).hasSplatting) { + return { + behavior: 'ask', + message: 'Command uses splatting (@variable)', + } + } + return { behavior: 'passthrough' } +} + +/** + * AST-only check: Detects stop-parsing token (--%) which prevents further parsing. + */ +function checkStopParsing( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (deriveSecurityFlags(parsed).hasStopParsing) { + return { + behavior: 'ask', + message: 'Command uses stop-parsing token (--%)', + } + } + return { behavior: 'passthrough' } +} + +/** + * AST-only check: Detects .NET method invocations which can access system APIs. + */ +function checkMemberInvocations( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + if (deriveSecurityFlags(parsed).hasMemberInvocations) { + return { + behavior: 'ask', + message: 'Command invokes .NET methods', + } + } + return { behavior: 'passthrough' } +} + +/** + * AST-only check: type literals outside Microsoft's ConstrainedLanguage + * allowlist. CLM blocks all .NET type access except ~90 primitives/attributes + * Microsoft considers safe for untrusted code. We trust that list as the + * "safe" boundary — anything outside it (Reflection.Assembly, IO.Pipes, + * Diagnostics.Process, InteropServices.Marshal, etc.) can access system APIs + * that compromise the permission model. + * + * Runs AFTER checkMemberInvocations: that broadly flags any ::Method / .Method() + * call; this check is the more specific "which types" signal. Both fire on + * [Reflection.Assembly]::Load; CLM gives the precise message. Pure type casts + * like [int]$x have no member invocation and only hit this check. + */ +function checkTypeLiterals( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const t of parsed.typeLiterals ?? []) { + if (!isClmAllowedType(t)) { + return { + behavior: 'ask', + message: `Command uses .NET type [${t}] outside the ConstrainedLanguage allowlist`, + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Invoke-Item (alias ii) opens a file with its default handler (ShellExecute + * on Windows, open/xdg-open on Unix). On an .exe/.ps1/.bat/.cmd this is RCE. + * Bug 008: ii is in no blocklist; passthrough prompt doesn't explain the + * exec hazard. Always ask — there is no safe variant (even opening .txt may + * invoke a user-configured handler that accepts arguments). + */ +function checkInvokeItem( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + if (lower === 'invoke-item' || lower === 'ii') { + return { + behavior: 'ask', + message: + 'Invoke-Item opens files with the default handler (ShellExecute). On executable files this runs arbitrary code.', + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Scheduled-task persistence primitives. Register-ScheduledJob was blocked + * (DANGEROUS_SCRIPT_BLOCK_CMDLETS); the newer Register-ScheduledTask cmdlet + * and legacy schtasks.exe /create were not. Persistence that survives the + * session with no explanatory prompt. + */ +const SCHEDULED_TASK_CMDLETS = new Set([ + 'register-scheduledtask', + 'new-scheduledtask', + 'new-scheduledtaskaction', + 'set-scheduledtask', +]) + +function checkScheduledTask( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + if (SCHEDULED_TASK_CMDLETS.has(lower)) { + return { + behavior: 'ask', + message: `${cmd.name} creates or modifies a scheduled task (persistence primitive)`, + } + } + if (lower === 'schtasks' || lower === 'schtasks.exe') { + if ( + cmd.args.some(a => { + const la = a.toLowerCase() + return ( + la === '/create' || + la === '/change' || + la === '-create' || + la === '-change' + ) + }) + ) { + return { + behavior: 'ask', + message: + 'schtasks with create/change modifies scheduled tasks (persistence primitive)', + } + } + } + } + return { behavior: 'passthrough' } +} + +/** + * AST-only check: Detects environment variable manipulation via Set-Item/New-Item on env: scope. + */ +const ENV_WRITE_CMDLETS = new Set([ + 'set-item', + 'si', + 'new-item', + 'ni', + 'remove-item', + 'ri', + 'del', + 'rm', + 'rd', + 'rmdir', + 'erase', + 'clear-item', + 'cli', + 'set-content', + // 'sc' omitted — collides with sc.exe on PS Core 7+, see COMMON_ALIASES note + 'add-content', + 'ac', +]) + +function checkEnvVarManipulation( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + const envVars = getVariablesByScope(parsed, 'env') + if (envVars.length === 0) { + return { behavior: 'passthrough' } + } + // Check if any command is a write cmdlet + for (const cmd of getAllCommands(parsed)) { + if (ENV_WRITE_CMDLETS.has(cmd.name.toLowerCase())) { + return { + behavior: 'ask', + message: 'Command modifies environment variables', + } + } + } + // Also flag if there are assignments involving env vars + if (deriveSecurityFlags(parsed).hasAssignments && envVars.length > 0) { + return { + behavior: 'ask', + message: 'Command modifies environment variables', + } + } + return { behavior: 'passthrough' } +} + +/** + * Module-loading cmdlets execute a .psm1's top-level script body (Import-Module) + * or download from arbitrary repositories (Install-Module, Save-Module). A + * wildcard allow rule like `Import-Module:*` would let an attacker-supplied + * .psm1 execute with the user's privileges — same risk as Invoke-Expression. + * + * NEVER_SUGGEST (dangerousCmdlets.ts) derives from this list so the UI + * never offers these as wildcard suggestions, but users can still manually + * write allow rules. This check ensures the permission engine independently + * gates these cmdlets. + */ + +function checkModuleLoading( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + if (MODULE_LOADING_CMDLETS.has(lower)) { + return { + behavior: 'ask', + message: + 'Command loads, installs, or downloads a PowerShell module or script, which can execute arbitrary code', + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Set-Alias/New-Alias can hijack future command resolution: after + * `Set-Alias Get-Content Invoke-Expression`, any later `Get-Content $x` + * executes arbitrary code. Set-Variable/New-Variable can poison + * `$PSDefaultParameterValues` (e.g., `Set-Variable PSDefaultParameterValues + * @{'*:Path'='/etc/passwd'}`) which alters every subsequent cmdlet's behavior. + * Neither effect can be validated statically — we'd need to track all future + * command resolutions in the session. Always ask. + */ +const RUNTIME_STATE_CMDLETS = new Set([ + 'set-alias', + 'sal', + 'new-alias', + 'nal', + 'set-variable', + 'sv', + 'new-variable', + 'nv', +]) + +function checkRuntimeStateManipulation( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + // Strip module qualifier: `Microsoft.PowerShell.Utility\Set-Alias` → `set-alias` + const raw = cmd.name.toLowerCase() + const lower = raw.includes('\\') + ? raw.slice(raw.lastIndexOf('\\') + 1) + : raw + if (RUNTIME_STATE_CMDLETS.has(lower)) { + return { + behavior: 'ask', + message: + 'Command creates or modifies an alias or variable that can affect future command resolution', + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Invoke-WmiMethod / Invoke-CimMethod are Start-Process equivalents via WMI. + * `Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList "cmd /c ..."` + * spawns an arbitrary process, bypassing checkStartProcess entirely. No narrow + * safe usage exists — -Class and -MethodName accept arbitrary strings, so + * gating on Win32_Process specifically would miss -Class $x or other process- + * spawning WMI classes. Returns ask on any invocation. (security finding #34) + */ +const WMI_SPAWN_CMDLETS = new Set([ + 'invoke-wmimethod', + 'iwmi', + 'invoke-cimmethod', +]) + +function checkWmiProcessSpawn( + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + for (const cmd of getAllCommands(parsed)) { + const lower = cmd.name.toLowerCase() + if (WMI_SPAWN_CMDLETS.has(lower)) { + return { + behavior: 'ask', + message: `${cmd.name} can spawn arbitrary processes via WMI/CIM (Win32_Process Create)`, + } + } + } + return { behavior: 'passthrough' } +} + +/** + * Main entry point for PowerShell security validation. + * Checks a PowerShell command against known dangerous patterns. + * + * All checks are AST-based. If the AST parse failed (parsed.valid === false), + * none of the individual checks will match and we return 'ask' as a safe default. + * + * @param command - The PowerShell command to validate (unused, kept for API compat) + * @param parsed - Parsed AST from PowerShell's native parser (required) + * @returns Security result indicating whether the command is safe + */ +export function powershellCommandIsSafe( + _command: string, + parsed: ParsedPowerShellCommand, +): PowerShellSecurityResult { + // If the AST parse failed, we cannot determine safety -- ask the user + if (!parsed.valid) { + return { + behavior: 'ask', + message: 'Could not parse command for security analysis', + } + } + + const validators = [ + checkInvokeExpression, + checkDynamicCommandName, + checkEncodedCommand, + checkPwshCommandOrFile, + checkDownloadCradles, + checkDownloadUtilities, + checkAddType, + checkComObject, + checkDangerousFilePathExecution, + checkInvokeItem, + checkScheduledTask, + checkForEachMemberName, + checkStartProcess, + checkScriptBlockInjection, + checkSubExpressions, + checkExpandableStrings, + checkSplatting, + checkStopParsing, + checkMemberInvocations, + checkTypeLiterals, + checkEnvVarManipulation, + checkModuleLoading, + checkRuntimeStateManipulation, + checkWmiProcessSpawn, + ] + + for (const validator of validators) { + const result = validator(parsed) + if (result.behavior === 'ask') { + return result + } + } + + // All checks passed + return { behavior: 'passthrough' } +} diff --git a/_all-in-one-upload/cc-haha/src/tools/SubscribePRTool/SubscribePRTool.ts b/_all-in-one-upload/cc-haha/src/tools/SubscribePRTool/SubscribePRTool.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/SubscribePRTool/SubscribePRTool.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/tools/TaskUpdateTool/constants.ts b/_all-in-one-upload/cc-haha/src/tools/TaskUpdateTool/constants.ts new file mode 100644 index 0000000..62b4709 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/TaskUpdateTool/constants.ts @@ -0,0 +1 @@ +export const TASK_UPDATE_TOOL_NAME = 'TaskUpdate' diff --git a/_all-in-one-upload/cc-haha/src/tools/ToolSearchTool/ToolSearchTool.ts b/_all-in-one-upload/cc-haha/src/tools/ToolSearchTool/ToolSearchTool.ts new file mode 100644 index 0000000..ee43b66 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/tools/ToolSearchTool/ToolSearchTool.ts @@ -0,0 +1,471 @@ +import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs' +import memoize from 'lodash-es/memoize.js' +import { z } from 'zod/v4' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../../services/analytics/index.js' +import { + buildTool, + findToolByName, + type Tool, + type ToolDef, + type Tools, +} from '../../Tool.js' +import { logForDebugging } from '../../utils/debug.js' +import { lazySchema } from '../../utils/lazySchema.js' +import { escapeRegExp } from '../../utils/stringUtils.js' +import { isToolSearchEnabledOptimistic } from '../../utils/toolSearch.js' +import { getPrompt, isDeferredTool, TOOL_SEARCH_TOOL_NAME } from './prompt.js' + +export const inputSchema = lazySchema(() => + z.object({ + query: z + .string() + .describe( + 'Query to find deferred tools. Use "select:" for direct selection, or keywords to search.', + ), + max_results: z + .number() + .optional() + .default(5) + .describe('Maximum number of results to return (default: 5)'), + }), +) +type InputSchema = ReturnType + +export const outputSchema = lazySchema(() => + z.object({ + matches: z.array(z.string()), + query: z.string(), + total_deferred_tools: z.number(), + pending_mcp_servers: z.array(z.string()).optional(), + }), +) +type OutputSchema = ReturnType + +export type Output = z.infer + +// Track deferred tool names to detect when cache should be cleared +let cachedDeferredToolNames: string | null = null + +/** + * Get a cache key representing the current set of deferred tools. + */ +function getDeferredToolsCacheKey(deferredTools: Tools): string { + return deferredTools + .map(t => t.name) + .sort() + .join(',') +} + +/** + * Get tool description, memoized by tool name. + * Used for keyword search scoring. + */ +const getToolDescriptionMemoized = memoize( + async (toolName: string, tools: Tools): Promise => { + const tool = findToolByName(tools, toolName) + if (!tool) { + return '' + } + return tool.prompt({ + getToolPermissionContext: async () => ({ + mode: 'default' as const, + additionalWorkingDirectories: new Map(), + alwaysAllowRules: {}, + alwaysDenyRules: {}, + alwaysAskRules: {}, + isBypassPermissionsModeAvailable: false, + }), + tools, + agents: [], + }) + }, + (toolName: string) => toolName, +) + +/** + * Invalidate the description cache if deferred tools have changed. + */ +function maybeInvalidateCache(deferredTools: Tools): void { + const currentKey = getDeferredToolsCacheKey(deferredTools) + if (cachedDeferredToolNames !== currentKey) { + logForDebugging( + `ToolSearchTool: cache invalidated - deferred tools changed`, + ) + getToolDescriptionMemoized.cache.clear?.() + cachedDeferredToolNames = currentKey + } +} + +export function clearToolSearchDescriptionCache(): void { + getToolDescriptionMemoized.cache.clear?.() + cachedDeferredToolNames = null +} + +/** + * Build the search result output structure. + */ +function buildSearchResult( + matches: string[], + query: string, + totalDeferredTools: number, + pendingMcpServers?: string[], +): { data: Output } { + return { + data: { + matches, + query, + total_deferred_tools: totalDeferredTools, + ...(pendingMcpServers && pendingMcpServers.length > 0 + ? { pending_mcp_servers: pendingMcpServers } + : {}), + }, + } +} + +/** + * Parse tool name into searchable parts. + * Handles both MCP tools (mcp__server__action) and regular tools (CamelCase). + */ +function parseToolName(name: string): { + parts: string[] + full: string + isMcp: boolean +} { + // Check if it's an MCP tool + if (name.startsWith('mcp__')) { + const withoutPrefix = name.replace(/^mcp__/, '').toLowerCase() + const parts = withoutPrefix.split('__').flatMap(p => p.split('_')) + return { + parts: parts.filter(Boolean), + full: withoutPrefix.replace(/__/g, ' ').replace(/_/g, ' '), + isMcp: true, + } + } + + // Regular tool - split by CamelCase and underscores + const parts = name + .replace(/([a-z])([A-Z])/g, '$1 $2') // CamelCase to spaces + .replace(/_/g, ' ') + .toLowerCase() + .split(/\s+/) + .filter(Boolean) + + return { + parts, + full: parts.join(' '), + isMcp: false, + } +} + +/** + * Pre-compile word-boundary regexes for all search terms. + * Called once per search instead of tools×terms×2 times. + */ +function compileTermPatterns(terms: string[]): Map { + const patterns = new Map() + for (const term of terms) { + if (!patterns.has(term)) { + patterns.set(term, new RegExp(`\\b${escapeRegExp(term)}\\b`)) + } + } + return patterns +} + +/** + * Keyword-based search over tool names and descriptions. + * Handles both MCP tools (mcp__server__action) and regular tools (CamelCase). + * + * The model typically queries with: + * - Server names when it knows the integration (e.g., "slack", "github") + * - Action words when looking for functionality (e.g., "read", "list", "create") + * - Tool-specific terms (e.g., "notebook", "shell", "kill") + */ +async function searchToolsWithKeywords( + query: string, + deferredTools: Tools, + tools: Tools, + maxResults: number, +): Promise { + const queryLower = query.toLowerCase().trim() + + // Fast path: if query matches a tool name exactly, return it directly. + // Handles models using a bare tool name instead of select: prefix (seen + // from subagents/post-compaction). Checks deferred first, then falls back + // to the full tool set — selecting an already-loaded tool is a harmless + // no-op that lets the model proceed without retry churn. + const exactMatch = + deferredTools.find(t => t.name.toLowerCase() === queryLower) ?? + tools.find(t => t.name.toLowerCase() === queryLower) + if (exactMatch) { + return [exactMatch.name] + } + + // If query looks like an MCP tool prefix (mcp__server), find matching tools. + // Handles models searching by server name with mcp__ prefix. + if (queryLower.startsWith('mcp__') && queryLower.length > 5) { + const prefixMatches = deferredTools + .filter(t => t.name.toLowerCase().startsWith(queryLower)) + .slice(0, maxResults) + .map(t => t.name) + if (prefixMatches.length > 0) { + return prefixMatches + } + } + + const queryTerms = queryLower.split(/\s+/).filter(term => term.length > 0) + + // Partition into required (+prefixed) and optional terms + const requiredTerms: string[] = [] + const optionalTerms: string[] = [] + for (const term of queryTerms) { + if (term.startsWith('+') && term.length > 1) { + requiredTerms.push(term.slice(1)) + } else { + optionalTerms.push(term) + } + } + + const allScoringTerms = + requiredTerms.length > 0 ? [...requiredTerms, ...optionalTerms] : queryTerms + const termPatterns = compileTermPatterns(allScoringTerms) + + // Pre-filter to tools matching ALL required terms in name or description + let candidateTools = deferredTools + if (requiredTerms.length > 0) { + const matches = await Promise.all( + deferredTools.map(async tool => { + const parsed = parseToolName(tool.name) + const description = await getToolDescriptionMemoized(tool.name, tools) + const descNormalized = description.toLowerCase() + const hintNormalized = tool.searchHint?.toLowerCase() ?? '' + const matchesAll = requiredTerms.every(term => { + const pattern = termPatterns.get(term)! + return ( + parsed.parts.includes(term) || + parsed.parts.some(part => part.includes(term)) || + pattern.test(descNormalized) || + (hintNormalized && pattern.test(hintNormalized)) + ) + }) + return matchesAll ? tool : null + }), + ) + candidateTools = matches.filter((t): t is Tool => t !== null) + } + + const scored = await Promise.all( + candidateTools.map(async tool => { + const parsed = parseToolName(tool.name) + const description = await getToolDescriptionMemoized(tool.name, tools) + const descNormalized = description.toLowerCase() + const hintNormalized = tool.searchHint?.toLowerCase() ?? '' + + let score = 0 + for (const term of allScoringTerms) { + const pattern = termPatterns.get(term)! + + // Exact part match (high weight for MCP server names, tool name parts) + if (parsed.parts.includes(term)) { + score += parsed.isMcp ? 12 : 10 + } else if (parsed.parts.some(part => part.includes(term))) { + score += parsed.isMcp ? 6 : 5 + } + + // Full name fallback (for edge cases) + if (parsed.full.includes(term) && score === 0) { + score += 3 + } + + // searchHint match — curated capability phrase, higher signal than prompt + if (hintNormalized && pattern.test(hintNormalized)) { + score += 4 + } + + // Description match - use word boundary to avoid false positives + if (pattern.test(descNormalized)) { + score += 2 + } + } + + return { name: tool.name, score } + }), + ) + + return scored + .filter(item => item.score > 0) + .sort((a, b) => b.score - a.score) + .slice(0, maxResults) + .map(item => item.name) +} + +export const ToolSearchTool = buildTool({ + isEnabled() { + return isToolSearchEnabledOptimistic() + }, + isConcurrencySafe() { + return true + }, + isReadOnly() { + return true + }, + name: TOOL_SEARCH_TOOL_NAME, + maxResultSizeChars: 100_000, + async description() { + return getPrompt() + }, + async prompt() { + return getPrompt() + }, + get inputSchema(): InputSchema { + return inputSchema() + }, + get outputSchema(): OutputSchema { + return outputSchema() + }, + async call(input, { options: { tools }, getAppState }) { + const { query, max_results = 5 } = input + + const deferredTools = tools.filter(isDeferredTool) + maybeInvalidateCache(deferredTools) + + // Check for MCP servers still connecting + function getPendingServerNames(): string[] | undefined { + const appState = getAppState() + const pending = appState.mcp.clients.filter(c => c.type === 'pending') + return pending.length > 0 ? pending.map(s => s.name) : undefined + } + + // Helper to log search outcome + function logSearchOutcome( + matches: string[], + queryType: 'select' | 'keyword', + ): void { + logEvent('tengu_tool_search_outcome', { + query: + query as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryType: + queryType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + matchCount: matches.length, + totalDeferredTools: deferredTools.length, + maxResults: max_results, + hasMatches: matches.length > 0, + }) + } + + // Check for select: prefix — direct tool selection. + // Supports comma-separated multi-select: `select:A,B,C`. + // If a name isn't in the deferred set but IS in the full tool set, + // we still return it — the tool is already loaded, so "selecting" it + // is a harmless no-op that lets the model proceed without retry churn. + const selectMatch = query.match(/^select:(.+)$/i) + if (selectMatch) { + const requested = selectMatch[1]! + .split(',') + .map(s => s.trim()) + .filter(Boolean) + + const found: string[] = [] + const missing: string[] = [] + for (const toolName of requested) { + const tool = + findToolByName(deferredTools, toolName) ?? + findToolByName(tools, toolName) + if (tool) { + if (!found.includes(tool.name)) found.push(tool.name) + } else { + missing.push(toolName) + } + } + + if (found.length === 0) { + logForDebugging( + `ToolSearchTool: select failed — none found: ${missing.join(', ')}`, + ) + logSearchOutcome([], 'select') + const pendingServers = getPendingServerNames() + return buildSearchResult( + [], + query, + deferredTools.length, + pendingServers, + ) + } + + if (missing.length > 0) { + logForDebugging( + `ToolSearchTool: partial select — found: ${found.join(', ')}, missing: ${missing.join(', ')}`, + ) + } else { + logForDebugging(`ToolSearchTool: selected ${found.join(', ')}`) + } + logSearchOutcome(found, 'select') + return buildSearchResult(found, query, deferredTools.length) + } + + // Keyword search + const matches = await searchToolsWithKeywords( + query, + deferredTools, + tools, + max_results, + ) + + logForDebugging( + `ToolSearchTool: keyword search for "${query}", found ${matches.length} matches`, + ) + + logSearchOutcome(matches, 'keyword') + + // Include pending server info when search finds no matches + if (matches.length === 0) { + const pendingServers = getPendingServerNames() + return buildSearchResult( + matches, + query, + deferredTools.length, + pendingServers, + ) + } + + return buildSearchResult(matches, query, deferredTools.length) + }, + renderToolUseMessage() { + return null + }, + userFacingName: () => '', + /** + * Returns a tool_result with tool_reference blocks. + * This format works on 1P/Foundry. Bedrock/Vertex may not support + * client-side tool_reference expansion yet. + */ + mapToolResultToToolResultBlockParam( + content: Output, + toolUseID: string, + ): ToolResultBlockParam { + if (content.matches.length === 0) { + let text = 'No matching deferred tools found' + if ( + content.pending_mcp_servers && + content.pending_mcp_servers.length > 0 + ) { + text += `. Some MCP servers are still connecting: ${content.pending_mcp_servers.join(', ')}. Their tools will become available shortly — try searching again.` + } + return { + type: 'tool_result', + tool_use_id: toolUseID, + content: text, + } + } + return { + type: 'tool_result', + tool_use_id: toolUseID, + content: content.matches.map(name => ({ + type: 'tool_reference' as const, + tool_name: name, + })), + } as unknown as ToolResultBlockParam + }, +} satisfies ToolDef) diff --git a/_all-in-one-upload/cc-haha/src/types/statusLine.ts b/_all-in-one-upload/cc-haha/src/types/statusLine.ts new file mode 100644 index 0000000..c170c49 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/types/statusLine.ts @@ -0,0 +1,34 @@ +// @generated stub from scan-missing-imports +// 该文件自动生成,对应 ant-internal 的 feature() gated 模块。 +// 所有外部 build 的代码路径在 DCE 后都不会真的执行这里的代码,这只是 +// bun build resolver 的占位符。 +const __target = function noop() {} +const __handler: ProxyHandler = { + get(_t, prop) { + if (prop === '__esModule') return true + if (prop === 'default') return new Proxy(__target, __handler) + if (prop === Symbol.toPrimitive) return () => undefined + if (prop === Symbol.iterator) return function* () {} + if (prop === Symbol.asyncIterator) return async function* () {} + if (prop === 'then') return undefined + return new Proxy(__target, __handler) + }, + apply() { + return new Proxy(__target, __handler) + }, + construct() { + return new Proxy(__target, __handler) + }, +} +const stub: any = new Proxy(__target, __handler) +export default stub +export const __stubMissing = true +// 兼容常见的命名导出 —— 没列在这里的也会通过 default Proxy 兜底 +export const createCachedMCState = stub +export const isCachedMicrocompactEnabled = stub +export const isModelSupportedForCacheEditing = stub +export const getCachedMCConfig = stub +export const markToolsSentToAPI = stub +export const resetCachedMCState = stub +export const checkProtectedNamespace = stub +export const getCoordinatorUserContext = stub diff --git a/_all-in-one-upload/cc-haha/src/utils/advisor.ts b/_all-in-one-upload/cc-haha/src/utils/advisor.ts new file mode 100644 index 0000000..54a2dfb --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/advisor.ts @@ -0,0 +1,145 @@ +import type { BetaUsage } from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../services/analytics/growthbook.js' +import { shouldIncludeFirstPartyOnlyBetas } from './betas.js' +import { isEnvTruthy } from './envUtils.js' +import { getInitialSettings } from './settings/settings.js' + +// The SDK does not yet have types for advisor blocks. +// TODO(hackyon): Migrate to the real anthropic SDK types when this feature ships publicly +export type AdvisorServerToolUseBlock = { + type: 'server_tool_use' + id: string + name: 'advisor' + input: { [key: string]: unknown } +} + +export type AdvisorToolResultBlock = { + type: 'advisor_tool_result' + tool_use_id: string + content: + | { + type: 'advisor_result' + text: string + } + | { + type: 'advisor_redacted_result' + encrypted_content: string + } + | { + type: 'advisor_tool_result_error' + error_code: string + } +} + +export type AdvisorBlock = AdvisorServerToolUseBlock | AdvisorToolResultBlock + +export function isAdvisorBlock(param: { + type: string + name?: string +}): param is AdvisorBlock { + return ( + param.type === 'advisor_tool_result' || + (param.type === 'server_tool_use' && param.name === 'advisor') + ) +} + +type AdvisorConfig = { + enabled?: boolean + canUserConfigure?: boolean + baseModel?: string + advisorModel?: string +} + +function getAdvisorConfig(): AdvisorConfig { + return getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_sage_compass', + {}, + ) +} + +export function isAdvisorEnabled(): boolean { + if (isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_ADVISOR_TOOL)) { + return false + } + // The advisor beta header is first-party only (Bedrock/Vertex 400 on it). + if (!shouldIncludeFirstPartyOnlyBetas()) { + return false + } + return getAdvisorConfig().enabled ?? false +} + +export function canUserConfigureAdvisor(): boolean { + return isAdvisorEnabled() && (getAdvisorConfig().canUserConfigure ?? false) +} + +export function getExperimentAdvisorModels(): + | { baseModel: string; advisorModel: string } + | undefined { + const config = getAdvisorConfig() + return isAdvisorEnabled() && + !canUserConfigureAdvisor() && + config.baseModel && + config.advisorModel + ? { baseModel: config.baseModel, advisorModel: config.advisorModel } + : undefined +} + +// @[MODEL LAUNCH]: Add the new model if it supports the advisor tool. +// Checks whether the main loop model supports calling the advisor tool. +export function modelSupportsAdvisor(model: string): boolean { + const m = model.toLowerCase() + return ( + m.includes('opus-4-6') || + m.includes('sonnet-4-6') || + process.env.USER_TYPE === 'ant' + ) +} + +// @[MODEL LAUNCH]: Add the new model if it can serve as an advisor model. +export function isValidAdvisorModel(model: string): boolean { + const m = model.toLowerCase() + return ( + m.includes('opus-4-6') || + m.includes('sonnet-4-6') || + process.env.USER_TYPE === 'ant' + ) +} + +export function getInitialAdvisorSetting(): string | undefined { + if (!isAdvisorEnabled()) { + return undefined + } + return getInitialSettings().advisorModel +} + +export function getAdvisorUsage( + usage: BetaUsage, +): Array { + const iterations = usage.iterations as + | Array<{ type: string }> + | null + | undefined + if (!iterations) { + return [] + } + return iterations.filter( + it => it.type === 'advisor_message', + ) as unknown as Array +} + +export const ADVISOR_TOOL_INSTRUCTIONS = `# Advisor Tool + +You have access to an \`advisor\` tool backed by a stronger reviewer model. It takes NO parameters -- when you call it, your entire conversation history is automatically forwarded. The advisor sees the task, every tool call you've made, every result you've seen. + +Call advisor BEFORE substantive work -- before writing code, before committing to an interpretation, before building on an assumption. If the task requires orientation first (finding files, reading code, seeing what's there), do that, then call advisor. Orientation is not substantive work. Writing, editing, and declaring an answer are. + +Also call advisor: +- When you believe the task is complete. BEFORE this call, make your deliverable durable: write the file, stage the change, save the result. The advisor call takes time; if the session ends during it, a durable result persists and an unwritten one doesn't. +- When stuck -- errors recurring, approach not converging, results that don't fit. +- When considering a change of approach. + +On tasks longer than a few steps, call advisor at least once before committing to an approach and once before declaring done. On short reactive tasks where the next action is dictated by tool output you just read, you don't need to keep calling -- the advisor adds most of its value on the first call, before the approach crystallizes. + +Give the advice serious weight. If you follow a step and it fails empirically, or you have primary-source evidence that contradicts a specific claim (the file says X, the code does Y), adapt. A passing self-test is not evidence the advice is wrong -- it's evidence your test doesn't check what the advice is checking. + +If you've already retrieved data pointing one way and the advisor points another: don't silently switch. Surface the conflict in one more advisor call -- "I found X, you suggest Y, which constraint breaks the tie?" The advisor saw your evidence but may have underweighted it; a reconcile call is cheaper than committing to the wrong branch.` diff --git a/_all-in-one-upload/cc-haha/src/utils/attachments.ts b/_all-in-one-upload/cc-haha/src/utils/attachments.ts new file mode 100644 index 0000000..556defe --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/attachments.ts @@ -0,0 +1,3993 @@ +// biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered +import { + logEvent, + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, +} from 'src/services/analytics/index.js' +import { + toolMatchesName, + type Tools, + type ToolUseContext, + type ToolPermissionContext, +} from '../Tool.js' +import { + FileReadTool, + MaxFileReadTokenExceededError, + type Output as FileReadToolOutput, + readImageWithTokenBudget, +} from '../tools/FileReadTool/FileReadTool.js' +import { FileTooLargeError, readFileInRange } from './readFileInRange.js' +import { expandPath } from './path.js' +import { countCharInString } from './stringUtils.js' +import { count, uniq } from './array.js' +import { getFsImplementation } from './fsOperations.js' +import { readdir, stat } from 'fs/promises' +import type { IDESelection } from '../hooks/useIdeSelection.js' +import { TODO_WRITE_TOOL_NAME } from '../tools/TodoWriteTool/constants.js' +import { TASK_CREATE_TOOL_NAME } from '../tools/TaskCreateTool/constants.js' +import { TASK_UPDATE_TOOL_NAME } from '../tools/TaskUpdateTool/constants.js' +import { BASH_TOOL_NAME } from '../tools/BashTool/toolName.js' +import { SKILL_TOOL_NAME } from '../tools/SkillTool/constants.js' +import type { TodoList } from './todo/types.js' +import { + type Task, + listTasks, + getTaskListId, + isTodoV2Enabled, +} from './tasks.js' +import { getPlanFilePath, getPlan } from './plans.js' +import { getConnectedIdeName } from './ide.js' +import { + filterInjectedMemoryFiles, + getManagedAndUserConditionalRules, + getMemoryFiles, + getMemoryFilesForNestedDirectory, + getConditionalRulesForCwdLevelDirectory, + type MemoryFileInfo, +} from './claudemd.js' +import { dirname, parse, relative, resolve } from 'path' +import { getCwd } from 'src/utils/cwd.js' +import { getViewedTeammateTask } from '../state/selectors.js' +import { logError } from './log.js' +import { logAntError } from './debug.js' +import { isENOENT, toError } from './errors.js' +import type { DiagnosticFile } from '../services/diagnosticTracking.js' +import { diagnosticTracker } from '../services/diagnosticTracking.js' +import type { + AttachmentMessage, + Message, + MessageOrigin, +} from 'src/types/message.js' +import { + type QueuedCommand, + getImagePasteIds, + isValidImagePaste, +} from 'src/types/textInputTypes.js' +import { randomUUID, type UUID } from 'crypto' +import { getSettings_DEPRECATED } from './settings/settings.js' +import { getSnippetForTwoFileDiff } from 'src/tools/FileEditTool/utils.js' +import type { + ContentBlockParam, + ImageBlockParam, + Base64ImageSource, +} from '@anthropic-ai/sdk/resources/messages.mjs' +import { maybeResizeAndDownsampleImageBlock } from './imageResizer.js' +import type { PastedContent } from './config.js' +import { getGlobalConfig } from './config.js' +import { + getDefaultSonnetModel, + getDefaultHaikuModel, + getDefaultOpusModel, +} from './model/model.js' +import type { ReadResourceResult } from '@modelcontextprotocol/sdk/types.js' +import { getSkillToolCommands, getMcpSkillCommands } from '../commands.js' +import type { Command } from '../types/command.js' +import uniqBy from 'lodash-es/uniqBy.js' +import { getProjectRoot } from '../bootstrap/state.js' +import { formatCommandsWithinBudget } from '../tools/SkillTool/prompt.js' +import { getContextWindowForModel } from './context.js' +import type { DiscoverySignal } from '../services/skillSearch/signals.js' +// Conditional require for DCE. All skill-search string literals that would +// otherwise leak into external builds live inside these modules. The only +// surfaces in THIS file are: the maybe() call (gated via spread below) and +// the skill_listing suppression check (uses the same skillSearchModules null +// check). The type-only DiscoverySignal import above is erased at compile time. +/* eslint-disable @typescript-eslint/no-require-imports */ +const skillSearchModules = feature('EXPERIMENTAL_SKILL_SEARCH') + ? { + featureCheck: + require('../services/skillSearch/featureCheck.js') as typeof import('../services/skillSearch/featureCheck.js'), + prefetch: + require('../services/skillSearch/prefetch.js') as typeof import('../services/skillSearch/prefetch.js'), + } + : null +const autoModeStateModule = feature('TRANSCRIPT_CLASSIFIER') + ? (require('./permissions/autoModeState.js') as typeof import('./permissions/autoModeState.js')) + : null +/* eslint-enable @typescript-eslint/no-require-imports */ +import { + MAX_LINES_TO_READ, + FILE_READ_TOOL_NAME, +} from 'src/tools/FileReadTool/prompt.js' +import { getDefaultFileReadingLimits } from 'src/tools/FileReadTool/limits.js' +import { cacheKeys, type FileStateCache } from './fileStateCache.js' +import { + createAbortController, + createChildAbortController, +} from './abortController.js' +import { isAbortError } from './errors.js' +import { + getFileModificationTimeAsync, + isFileWithinReadSizeLimit, +} from './file.js' +import type { AgentDefinition } from '../tools/AgentTool/loadAgentsDir.js' +import { filterAgentsByMcpRequirements } from '../tools/AgentTool/loadAgentsDir.js' +import { AGENT_TOOL_NAME } from '../tools/AgentTool/constants.js' +import { + formatAgentLine, + shouldInjectAgentListInMessages, +} from '../tools/AgentTool/prompt.js' +import { filterDeniedAgents } from './permissions/permissions.js' +import { getSubscriptionType } from './auth.js' +import { mcpInfoFromString } from '../services/mcp/mcpStringUtils.js' +import { + matchingRuleForInput, + pathInAllowedWorkingPath, +} from './permissions/filesystem.js' +import { + generateTaskAttachments, + applyTaskOffsetsAndEvictions, +} from './task/framework.js' +import { getTaskOutputPath } from './task/diskOutput.js' +import { drainPendingMessages } from '../tasks/LocalAgentTask/LocalAgentTask.js' +import type { TaskType, TaskStatus } from '../Task.js' +import { + getOriginalCwd, + getSessionId, + getSdkBetas, + getTotalCostUSD, + getTotalOutputTokens, + getCurrentTurnTokenBudget, + getTurnOutputTokens, + hasExitedPlanModeInSession, + setHasExitedPlanMode, + needsPlanModeExitAttachment, + setNeedsPlanModeExitAttachment, + needsAutoModeExitAttachment, + setNeedsAutoModeExitAttachment, + getLastEmittedDate, + setLastEmittedDate, + getKairosActive, +} from '../bootstrap/state.js' +import type { QuerySource } from '../constants/querySource.js' +import { + getDeferredToolsDelta, + isDeferredToolsDeltaEnabled, + isToolSearchEnabledOptimistic, + isToolSearchToolAvailable, + modelSupportsToolReference, + type DeferredToolsDeltaScanContext, +} from './toolSearch.js' +import { + getMcpInstructionsDelta, + isMcpInstructionsDeltaEnabled, + type ClientSideInstruction, +} from './mcpInstructionsDelta.js' +import { CLAUDE_IN_CHROME_MCP_SERVER_NAME } from './claudeInChrome/common.js' +import { CHROME_TOOL_SEARCH_INSTRUCTIONS } from './claudeInChrome/prompt.js' +import type { MCPServerConnection } from '../services/mcp/types.js' +import type { + HookEvent, + SyncHookJSONOutput, +} from 'src/entrypoints/agentSdkTypes.js' +import { + checkForAsyncHookResponses, + removeDeliveredAsyncHooks, +} from './hooks/AsyncHookRegistry.js' +import { + checkForLSPDiagnostics, + clearAllLSPDiagnostics, +} from '../services/lsp/LSPDiagnosticRegistry.js' +import { logForDebugging } from './debug.js' +import { + extractTextContent, + getUserMessageText, + isThinkingMessage, +} from './messages.js' +import { isHumanTurn } from './messagePredicates.js' +import { isEnvTruthy, getClaudeConfigHomeDir } from './envUtils.js' +import { feature } from 'bun:bundle' +/* eslint-disable @typescript-eslint/no-require-imports */ +const BRIEF_TOOL_NAME: string | null = + feature('KAIROS') || feature('KAIROS_BRIEF') + ? ( + require('../tools/BriefTool/prompt.js') as typeof import('../tools/BriefTool/prompt.js') + ).BRIEF_TOOL_NAME + : null +const sessionTranscriptModule = feature('KAIROS') + ? (require('../services/sessionTranscript/sessionTranscript.js') as typeof import('../services/sessionTranscript/sessionTranscript.js')) + : null +/* eslint-enable @typescript-eslint/no-require-imports */ +import { hasUltrathinkKeyword, isUltrathinkEnabled } from './thinking.js' +import { + tokenCountFromLastAPIResponse, + tokenCountWithEstimation, +} from './tokens.js' +import { + getEffectiveContextWindowSize, + isAutoCompactEnabled, +} from '../services/compact/autoCompact.js' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../services/analytics/growthbook.js' +import { + hasInstructionsLoadedHook, + executeInstructionsLoadedHooks, + type HookBlockingError, + type InstructionsMemoryType, +} from './hooks.js' +import { jsonStringify } from './slowOperations.js' +import { isPDFExtension } from './pdfUtils.js' +import { getLocalISODate } from '../constants/common.js' +import { getPDFPageCount } from './pdf.js' +import { PDF_AT_MENTION_INLINE_THRESHOLD } from '../constants/apiLimits.js' +import { isAgentSwarmsEnabled } from './agentSwarmsEnabled.js' +import { findRelevantMemories } from '../memdir/findRelevantMemories.js' +import { memoryAge, memoryFreshnessText } from '../memdir/memoryAge.js' +import { getAutoMemPath, isAutoMemoryEnabled } from '../memdir/paths.js' +import { getAgentMemoryDir } from '../tools/AgentTool/agentMemory.js' +import { + readUnreadMessages, + markMessagesAsReadByPredicate, + isShutdownApproved, + isStructuredProtocolMessage, + isIdleNotification, +} from './teammateMailbox.js' +import { + getAgentName, + getAgentId, + getTeamName, + isTeamLead, +} from './teammate.js' +import { isInProcessTeammate } from './teammateContext.js' +import { removeTeammateFromTeamFile } from './swarm/teamHelpers.js' +import { unassignTeammateTasks } from './tasks.js' +import { getCompanionIntroAttachment } from '../buddy/prompt.js' + +export const TODO_REMINDER_CONFIG = { + TURNS_SINCE_WRITE: 10, + TURNS_BETWEEN_REMINDERS: 10, +} as const + +export const PLAN_MODE_ATTACHMENT_CONFIG = { + TURNS_BETWEEN_ATTACHMENTS: 5, + FULL_REMINDER_EVERY_N_ATTACHMENTS: 5, +} as const + +export const AUTO_MODE_ATTACHMENT_CONFIG = { + TURNS_BETWEEN_ATTACHMENTS: 5, + FULL_REMINDER_EVERY_N_ATTACHMENTS: 5, +} as const + +const MAX_MEMORY_LINES = 200 +// Line cap alone doesn't bound size (200 × 500-char lines = 100KB). The +// surfacer injects up to 5 files per turn via , bypassing +// the per-message tool-result budget, so a tight per-file byte cap keeps +// aggregate injection bounded (5 × 4KB = 20KB/turn). Enforced via +// readFileInRange's truncateOnByteLimit option. Truncation means the +// most-relevant memory still surfaces: the frontmatter + opening context +// is usually what matters. +const MAX_MEMORY_BYTES = 4096 + +export const RELEVANT_MEMORIES_CONFIG = { + // Per-turn cap (5 × 4KB = 20KB) bounds a single injection, but over a + // long session the selector keeps surfacing distinct files — ~26K tokens/ + // session observed in prod. Cap the cumulative bytes: once hit, stop + // prefetching entirely. Budget is ~3 full injections; after that the + // most-relevant memories are already in context. Scanning messages + // (rather than tracking in toolUseContext) means compact naturally + // resets the counter — old attachments are gone from context, so + // re-surfacing is valid. + MAX_SESSION_BYTES: 60 * 1024, +} as const + +export const VERIFY_PLAN_REMINDER_CONFIG = { + TURNS_BETWEEN_REMINDERS: 10, +} as const + +export type FileAttachment = { + type: 'file' + filename: string + content: FileReadToolOutput + /** + * Whether the file was truncated due to size limits + */ + truncated?: boolean + /** Path relative to CWD at creation time, for stable display */ + displayPath: string +} + +export type CompactFileReferenceAttachment = { + type: 'compact_file_reference' + filename: string + /** Path relative to CWD at creation time, for stable display */ + displayPath: string +} + +export type PDFReferenceAttachment = { + type: 'pdf_reference' + filename: string + pageCount: number + fileSize: number + /** Path relative to CWD at creation time, for stable display */ + displayPath: string +} + +export type AlreadyReadFileAttachment = { + type: 'already_read_file' + filename: string + content: FileReadToolOutput + /** + * Whether the file was truncated due to size limits + */ + truncated?: boolean + /** Path relative to CWD at creation time, for stable display */ + displayPath: string +} + +export type AgentMentionAttachment = { + type: 'agent_mention' + agentType: string +} + +export type AsyncHookResponseAttachment = { + type: 'async_hook_response' + processId: string + hookName: string + hookEvent: HookEvent | 'StatusLine' | 'FileSuggestion' + toolName?: string + response: SyncHookJSONOutput + stdout: string + stderr: string + exitCode?: number +} + +export type HookAttachment = + | HookCancelledAttachment + | { + type: 'hook_blocking_error' + blockingError: HookBlockingError + hookName: string + toolUseID: string + hookEvent: HookEvent + } + | HookNonBlockingErrorAttachment + | HookErrorDuringExecutionAttachment + | { + type: 'hook_stopped_continuation' + message: string + hookName: string + toolUseID: string + hookEvent: HookEvent + } + | HookSuccessAttachment + | { + type: 'hook_additional_context' + content: string[] + hookName: string + toolUseID: string + hookEvent: HookEvent + } + | HookSystemMessageAttachment + | HookPermissionDecisionAttachment + +export type HookPermissionDecisionAttachment = { + type: 'hook_permission_decision' + decision: 'allow' | 'deny' + toolUseID: string + hookEvent: HookEvent +} + +export type HookSystemMessageAttachment = { + type: 'hook_system_message' + content: string + hookName: string + toolUseID: string + hookEvent: HookEvent +} + +export type HookCancelledAttachment = { + type: 'hook_cancelled' + hookName: string + toolUseID: string + hookEvent: HookEvent + command?: string + durationMs?: number +} + +export type HookErrorDuringExecutionAttachment = { + type: 'hook_error_during_execution' + content: string + hookName: string + toolUseID: string + hookEvent: HookEvent + command?: string + durationMs?: number +} + +export type HookSuccessAttachment = { + type: 'hook_success' + content: string + hookName: string + toolUseID: string + hookEvent: HookEvent + stdout?: string + stderr?: string + exitCode?: number + command?: string + durationMs?: number +} + +export type HookNonBlockingErrorAttachment = { + type: 'hook_non_blocking_error' + hookName: string + stderr: string + stdout: string + exitCode: number + toolUseID: string + hookEvent: HookEvent + command?: string + durationMs?: number +} + +export type Attachment = + /** + * User at-mentioned the file + */ + | FileAttachment + | CompactFileReferenceAttachment + | PDFReferenceAttachment + | AlreadyReadFileAttachment + /** + * An at-mentioned file was edited + */ + | { + type: 'edited_text_file' + filename: string + snippet: string + } + | { + type: 'edited_image_file' + filename: string + content: FileReadToolOutput + } + | { + type: 'directory' + path: string + content: string + /** Path relative to CWD at creation time, for stable display */ + displayPath: string + } + | { + type: 'selected_lines_in_ide' + ideName: string + lineStart: number + lineEnd: number + filename: string + content: string + /** Path relative to CWD at creation time, for stable display */ + displayPath: string + } + | { + type: 'opened_file_in_ide' + filename: string + } + | { + type: 'todo_reminder' + content: TodoList + itemCount: number + } + | { + type: 'task_reminder' + content: Task[] + itemCount: number + } + | { + type: 'nested_memory' + path: string + content: MemoryFileInfo + /** Path relative to CWD at creation time, for stable display */ + displayPath: string + } + | { + type: 'relevant_memories' + memories: { + path: string + content: string + mtimeMs: number + /** + * Pre-computed header string (age + path prefix). Computed once + * at attachment-creation time so the rendered bytes are stable + * across turns — recomputing memoryAge(mtimeMs) at render time + * calls Date.now(), so "saved 3 days ago" becomes "saved 4 days + * ago" across turns → different bytes → prompt cache bust. + * Optional for backward compat with resumed sessions; render + * path falls back to recomputing if missing. + */ + header?: string + /** + * lineCount when the file was truncated by readMemoriesForSurfacing, + * else undefined. Threaded to the readFileState write so + * getChangedFiles skips truncated memories (partial content would + * yield a misleading diff). + */ + limit?: number + }[] + } + | { + type: 'dynamic_skill' + skillDir: string + skillNames: string[] + /** Path relative to CWD at creation time, for stable display */ + displayPath: string + } + | { + type: 'skill_listing' + content: string + skillCount: number + isInitial: boolean + } + | { + type: 'skill_discovery' + skills: { name: string; description: string; shortId?: string }[] + signal: DiscoverySignal + source: 'native' | 'aki' | 'both' + } + | { + type: 'queued_command' + prompt: string | Array + source_uuid?: UUID + imagePasteIds?: number[] + /** Original queue mode — 'prompt' for user messages, 'task-notification' for system events */ + commandMode?: string + /** Provenance carried from QueuedCommand so mid-turn drains preserve it */ + origin?: MessageOrigin + /** Carried from QueuedCommand.isMeta — distinguishes human-typed from system-injected */ + isMeta?: boolean + } + | { + type: 'output_style' + style: string + } + | { + type: 'diagnostics' + files: DiagnosticFile[] + isNew: boolean + } + | { + type: 'plan_mode' + reminderType: 'full' | 'sparse' + isSubAgent?: boolean + planFilePath: string + planExists: boolean + } + | { + type: 'plan_mode_reentry' + planFilePath: string + } + | { + type: 'plan_mode_exit' + planFilePath: string + planExists: boolean + } + | { + type: 'auto_mode' + reminderType: 'full' | 'sparse' + } + | { + type: 'auto_mode_exit' + } + | { + type: 'critical_system_reminder' + content: string + } + | { + type: 'plan_file_reference' + planFilePath: string + planContent: string + } + | { + type: 'mcp_resource' + server: string + uri: string + name: string + description?: string + content: ReadResourceResult + } + | { + type: 'command_permissions' + allowedTools: string[] + model?: string + } + | AgentMentionAttachment + | { + type: 'task_status' + taskId: string + taskType: TaskType + status: TaskStatus + description: string + deltaSummary: string | null + outputFilePath?: string + } + | AsyncHookResponseAttachment + | { + type: 'token_usage' + used: number + total: number + remaining: number + } + | { + type: 'budget_usd' + used: number + total: number + remaining: number + } + | { + type: 'output_token_usage' + turn: number + session: number + budget: number | null + } + | { + type: 'structured_output' + data: unknown + } + | TeammateMailboxAttachment + | TeamContextAttachment + | HookAttachment + | { + type: 'invoked_skills' + skills: Array<{ + name: string + path: string + content: string + }> + } + | { + type: 'verify_plan_reminder' + } + | { + type: 'max_turns_reached' + maxTurns: number + turnCount: number + } + | { + type: 'current_session_memory' + content: string + path: string + tokenCount: number + } + | { + type: 'teammate_shutdown_batch' + count: number + } + | { + type: 'compaction_reminder' + } + | { + type: 'context_efficiency' + } + | { + type: 'date_change' + newDate: string + } + | { + type: 'ultrathink_effort' + level: 'high' + } + | { + type: 'deferred_tools_delta' + addedNames: string[] + addedLines: string[] + removedNames: string[] + } + | { + type: 'agent_listing_delta' + addedTypes: string[] + addedLines: string[] + removedTypes: string[] + /** True when this is the first announcement in the conversation */ + isInitial: boolean + /** Whether to include the "launch multiple agents concurrently" note (non-pro subscriptions) */ + showConcurrencyNote: boolean + } + | { + type: 'mcp_instructions_delta' + addedNames: string[] + addedBlocks: string[] + removedNames: string[] + } + | { + type: 'companion_intro' + name: string + species: string + } + | { + type: 'bagel_console' + errorCount: number + warningCount: number + sample: string + } + +export type TeammateMailboxAttachment = { + type: 'teammate_mailbox' + messages: Array<{ + from: string + text: string + timestamp: string + color?: string + summary?: string + }> +} + +export type TeamContextAttachment = { + type: 'team_context' + agentId: string + agentName: string + teamName: string + teamConfigPath: string + taskListPath: string +} + +/** + * This is janky + * TODO: Generate attachments when we create messages + */ +export async function getAttachments( + input: string | null, + toolUseContext: ToolUseContext, + ideSelection: IDESelection | null, + queuedCommands: QueuedCommand[], + messages?: Message[], + querySource?: QuerySource, + options?: { skipSkillDiscovery?: boolean }, +): Promise { + if ( + isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_ATTACHMENTS) || + isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE) + ) { + // query.ts:removeFromQueue dequeues these unconditionally after + // getAttachmentMessages runs — returning [] here silently drops them. + // Coworker runs with --bare and depends on task-notification for + // mid-tool-call notifications from Local*Task/Remote*Task. + return getQueuedCommandAttachments(queuedCommands) + } + + // This will slow down submissions + // TODO: Compute attachments as the user types, not here (though we use this + // function for slash command prompts too) + const abortController = createAbortController() + const timeoutId = setTimeout(ac => ac.abort(), 1000, abortController) + const context = { ...toolUseContext, abortController } + + const isMainThread = !toolUseContext.agentId + + // Attachments which are added in response to on user input + const userInputAttachments = input + ? [ + maybe('at_mentioned_files', () => + processAtMentionedFiles(input, context), + ), + maybe('mcp_resources', () => + processMcpResourceAttachments(input, context), + ), + maybe('agent_mentions', () => + Promise.resolve( + processAgentMentions( + input, + toolUseContext.options.agentDefinitions.activeAgents, + ), + ), + ), + // Skill discovery on turn 0 (user input as signal). Inter-turn + // discovery runs via startSkillDiscoveryPrefetch in query.ts, + // gated on write-pivot detection — see skillSearch/prefetch.ts. + // feature() here lets DCE drop the 'skill_discovery' string (and the + // function it calls) from external builds. + // + // skipSkillDiscovery gates out the SKILL.md-expansion path + // (getMessagesForPromptSlashCommand). When a skill is invoked, its + // SKILL.md content is passed as `input` here to extract @-mentions — + // but that content is NOT user intent and must not trigger discovery. + // Without this gate, a 110KB SKILL.md fires ~3.3s of chunked AKI + // queries on every skill invocation (session 13a9afae). + ...(feature('EXPERIMENTAL_SKILL_SEARCH') && + skillSearchModules && + !options?.skipSkillDiscovery + ? [ + maybe('skill_discovery', () => + skillSearchModules.prefetch.getTurnZeroSkillDiscovery( + input, + messages ?? [], + context, + ), + ), + ] + : []), + ] + : [] + + // Process user input attachments first (includes @mentioned files) + // This ensures files are added to nestedMemoryAttachmentTriggers before nested_memory processes them + const userAttachmentResults = await Promise.all(userInputAttachments) + + // Thread-safe attachments available in sub-agents + // NOTE: These must be created AFTER userInputAttachments completes to ensure + // nestedMemoryAttachmentTriggers is populated before getNestedMemoryAttachments runs + const allThreadAttachments = [ + // queuedCommands is already agent-scoped by the drain gate in query.ts — + // main thread gets agentId===undefined, subagents get their own agentId. + // Must run for all threads or subagent notifications drain into the void + // (removed from queue by removeFromQueue but never attached). + maybe('queued_commands', () => getQueuedCommandAttachments(queuedCommands)), + maybe('date_change', () => + Promise.resolve(getDateChangeAttachments(messages)), + ), + maybe('ultrathink_effort', () => + Promise.resolve(getUltrathinkEffortAttachment(input)), + ), + maybe('deferred_tools_delta', () => + Promise.resolve( + getDeferredToolsDeltaAttachment( + toolUseContext.options.tools, + toolUseContext.options.mainLoopModel, + messages, + { + callSite: isMainThread + ? 'attachments_main' + : 'attachments_subagent', + querySource, + }, + ), + ), + ), + maybe('agent_listing_delta', () => + Promise.resolve(getAgentListingDeltaAttachment(toolUseContext, messages)), + ), + maybe('mcp_instructions_delta', () => + Promise.resolve( + getMcpInstructionsDeltaAttachment( + toolUseContext.options.mcpClients, + toolUseContext.options.tools, + toolUseContext.options.mainLoopModel, + messages, + ), + ), + ), + maybe('companion_intro', () => + Promise.resolve(getCompanionIntroAttachment(messages)), + ), + maybe('changed_files', () => getChangedFiles(context)), + maybe('nested_memory', () => getNestedMemoryAttachments(context)), + // relevant_memories moved to async prefetch (startRelevantMemoryPrefetch) + maybe('dynamic_skill', () => getDynamicSkillAttachments(context)), + maybe('skill_listing', () => getSkillListingAttachments(context)), + // Inter-turn skill discovery now runs via startSkillDiscoveryPrefetch + // (query.ts, concurrent with the main turn). The blocking call that + // previously lived here was the assistant_turn signal — 97% of those + // Haiku calls found nothing in prod. Prefetch + await-at-collection + // replaces it; see src/services/skillSearch/prefetch.ts. + maybe('plan_mode', () => getPlanModeAttachments(messages, toolUseContext)), + maybe('plan_mode_exit', () => getPlanModeExitAttachment(toolUseContext)), + ...(feature('TRANSCRIPT_CLASSIFIER') + ? [ + maybe('auto_mode', () => + getAutoModeAttachments(messages, toolUseContext), + ), + maybe('auto_mode_exit', () => + getAutoModeExitAttachment(toolUseContext), + ), + ] + : []), + maybe('todo_reminders', () => + isTodoV2Enabled() + ? getTaskReminderAttachments(messages, toolUseContext) + : getTodoReminderAttachments(messages, toolUseContext), + ), + ...(isAgentSwarmsEnabled() + ? [ + // Skip teammate mailbox for the session_memory forked agent. + // It shares AppState.teamContext with the leader, so isTeamLead resolves + // true and it reads+marks-as-read the leader's DMs as ephemeral attachments, + // silently stealing messages that should be delivered as permanent turns. + ...(querySource === 'session_memory' + ? [] + : [ + maybe('teammate_mailbox', async () => + getTeammateMailboxAttachments(toolUseContext), + ), + ]), + maybe('team_context', async () => + getTeamContextAttachment(messages ?? []), + ), + ] + : []), + maybe('agent_pending_messages', async () => + getAgentPendingMessageAttachments(toolUseContext), + ), + maybe('critical_system_reminder', () => + Promise.resolve(getCriticalSystemReminderAttachment(toolUseContext)), + ), + ...(feature('COMPACTION_REMINDERS') + ? [ + maybe('compaction_reminder', () => + Promise.resolve( + getCompactionReminderAttachment( + messages ?? [], + toolUseContext.options.mainLoopModel, + ), + ), + ), + ] + : []), + ...(feature('HISTORY_SNIP') + ? [ + maybe('context_efficiency', () => + Promise.resolve(getContextEfficiencyAttachment(messages ?? [])), + ), + ] + : []), + ] + + // Attachments which are semantically only for the main conversation or don't have concurrency-safe implementations + const mainThreadAttachments = isMainThread + ? [ + maybe('ide_selection', async () => + getSelectedLinesFromIDE(ideSelection, toolUseContext), + ), + maybe('ide_opened_file', async () => + getOpenedFileFromIDE(ideSelection, toolUseContext), + ), + maybe('output_style', async () => + Promise.resolve(getOutputStyleAttachment()), + ), + maybe('diagnostics', async () => + getDiagnosticAttachments(toolUseContext), + ), + maybe('lsp_diagnostics', async () => + getLSPDiagnosticAttachments(toolUseContext), + ), + maybe('unified_tasks', async () => + getUnifiedTaskAttachments(toolUseContext), + ), + maybe('async_hook_responses', async () => + getAsyncHookResponseAttachments(), + ), + maybe('token_usage', async () => + Promise.resolve( + getTokenUsageAttachment( + messages ?? [], + toolUseContext.options.mainLoopModel, + ), + ), + ), + maybe('budget_usd', async () => + Promise.resolve( + getMaxBudgetUsdAttachment(toolUseContext.options.maxBudgetUsd), + ), + ), + maybe('output_token_usage', async () => + Promise.resolve(getOutputTokenUsageAttachment()), + ), + maybe('verify_plan_reminder', async () => + getVerifyPlanReminderAttachment(messages, toolUseContext), + ), + ] + : [] + + // Process thread and main thread attachments in parallel (no dependencies between them) + const [threadAttachmentResults, mainThreadAttachmentResults] = + await Promise.all([ + Promise.all(allThreadAttachments), + Promise.all(mainThreadAttachments), + ]) + + clearTimeout(timeoutId) + // Defensive: a getter leaking [undefined] crashes .map(a => a.type) below. + return [ + ...userAttachmentResults.flat(), + ...threadAttachmentResults.flat(), + ...mainThreadAttachmentResults.flat(), + ].filter(a => a !== undefined && a !== null) +} + +async function maybe(label: string, f: () => Promise): Promise { + const startTime = Date.now() + try { + const result = await f() + const duration = Date.now() - startTime + // Log only 5% of events to reduce volume + if (Math.random() < 0.05) { + // jsonStringify(undefined) returns undefined, so .length would throw + const attachmentSizeBytes = result + .filter(a => a !== undefined && a !== null) + .reduce((total, attachment) => { + return total + jsonStringify(attachment).length + }, 0) + logEvent('tengu_attachment_compute_duration', { + label, + duration_ms: duration, + attachment_size_bytes: attachmentSizeBytes, + attachment_count: result.length, + } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS) + } + return result + } catch (e) { + const duration = Date.now() - startTime + // Log only 5% of events to reduce volume + if (Math.random() < 0.05) { + logEvent('tengu_attachment_compute_duration', { + label, + duration_ms: duration, + error: true, + } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS) + } + logError(e) + // For Ant users, log the full error to help with debugging + logAntError(`Attachment error in ${label}`, e) + + return [] + } +} + +const INLINE_NOTIFICATION_MODES = new Set(['prompt', 'task-notification']) + +export async function getQueuedCommandAttachments( + queuedCommands: QueuedCommand[], +): Promise { + if (!queuedCommands) { + return [] + } + // Include both 'prompt' and 'task-notification' commands as attachments. + // During proactive agentic loops, task-notification commands would otherwise + // stay in the queue permanently (useQueueProcessor can't run while a query + // is active), causing hasPendingNotifications() to return true and Sleep to + // wake immediately with 0ms duration in an infinite loop. + const filtered = queuedCommands.filter(_ => + INLINE_NOTIFICATION_MODES.has(_.mode), + ) + return Promise.all( + filtered.map(async _ => { + const imageBlocks = await buildImageContentBlocks(_.pastedContents) + let prompt: string | Array = _.value + if (imageBlocks.length > 0) { + // Build content block array with text + images so the model sees them + const textValue = + typeof _.value === 'string' + ? _.value + : extractTextContent(_.value, '\n') + prompt = [{ type: 'text' as const, text: textValue }, ...imageBlocks] + } + return { + type: 'queued_command' as const, + prompt, + source_uuid: _.uuid, + imagePasteIds: getImagePasteIds(_.pastedContents), + commandMode: _.mode, + origin: _.origin, + isMeta: _.isMeta, + } + }), + ) +} + +export function getAgentPendingMessageAttachments( + toolUseContext: ToolUseContext, +): Attachment[] { + const agentId = toolUseContext.agentId + if (!agentId) return [] + const drained = drainPendingMessages( + agentId, + toolUseContext.getAppState, + toolUseContext.setAppStateForTasks ?? toolUseContext.setAppState, + ) + return drained.map(msg => ({ + type: 'queued_command' as const, + prompt: msg, + origin: { kind: 'coordinator' as const }, + isMeta: true, + })) +} + +async function buildImageContentBlocks( + pastedContents: Record | undefined, +): Promise { + if (!pastedContents) { + return [] + } + const imageContents = Object.values(pastedContents).filter(isValidImagePaste) + if (imageContents.length === 0) { + return [] + } + const results = await Promise.all( + imageContents.map(async img => { + const imageBlock: ImageBlockParam = { + type: 'image', + source: { + type: 'base64', + media_type: (img.mediaType || + 'image/png') as Base64ImageSource['media_type'], + data: img.content, + }, + } + const resized = await maybeResizeAndDownsampleImageBlock(imageBlock) + return resized.block + }), + ) + return results +} + +function getPlanModeAttachmentTurnCount(messages: Message[]): { + turnCount: number + foundPlanModeAttachment: boolean +} { + let turnsSinceLastAttachment = 0 + let foundPlanModeAttachment = false + + // Iterate backwards to find most recent plan_mode attachment. + // Count HUMAN turns (non-meta, non-tool-result user messages), not assistant + // messages — the tool loop in query.ts calls getAttachmentMessages on every + // tool round, so counting assistant messages would fire the reminder every + // 5 tool calls instead of every 5 human turns. + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + + if ( + message?.type === 'user' && + !message.isMeta && + !hasToolResultContent(message.message.content) + ) { + turnsSinceLastAttachment++ + } else if ( + message?.type === 'attachment' && + (message.attachment.type === 'plan_mode' || + message.attachment.type === 'plan_mode_reentry') + ) { + foundPlanModeAttachment = true + break + } + } + + return { turnCount: turnsSinceLastAttachment, foundPlanModeAttachment } +} + +/** + * Count plan_mode attachments since the last plan_mode_exit (or from start if no exit). + * This ensures the full/sparse cycle resets when re-entering plan mode. + */ +function countPlanModeAttachmentsSinceLastExit(messages: Message[]): number { + let count = 0 + // Iterate backwards - if we hit a plan_mode_exit, stop counting + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + if (message?.type === 'attachment') { + if (message.attachment.type === 'plan_mode_exit') { + break // Stop counting at the last exit + } + if (message.attachment.type === 'plan_mode') { + count++ + } + } + } + return count +} + +async function getPlanModeAttachments( + messages: Message[] | undefined, + toolUseContext: ToolUseContext, +): Promise { + const appState = toolUseContext.getAppState() + const permissionContext = appState.toolPermissionContext + if (permissionContext.mode !== 'plan') { + return [] + } + + // Check if we should attach based on turn count (except for first turn) + if (messages && messages.length > 0) { + const { turnCount, foundPlanModeAttachment } = + getPlanModeAttachmentTurnCount(messages) + // Only throttle if we've already sent a plan_mode attachment before + // On first turn in plan mode, always attach + if ( + foundPlanModeAttachment && + turnCount < PLAN_MODE_ATTACHMENT_CONFIG.TURNS_BETWEEN_ATTACHMENTS + ) { + return [] + } + } + + const planFilePath = getPlanFilePath(toolUseContext.agentId) + const existingPlan = getPlan(toolUseContext.agentId) + + const attachments: Attachment[] = [] + + // Check for re-entry: flag is set AND plan file exists + if (hasExitedPlanModeInSession() && existingPlan !== null) { + attachments.push({ type: 'plan_mode_reentry', planFilePath }) + setHasExitedPlanMode(false) // Clear flag - one-time guidance + } + + // Determine if this should be a full or sparse reminder + // Full reminder on 1st, 6th, 11th... (every Nth attachment) + const attachmentCount = + countPlanModeAttachmentsSinceLastExit(messages ?? []) + 1 + const reminderType: 'full' | 'sparse' = + attachmentCount % + PLAN_MODE_ATTACHMENT_CONFIG.FULL_REMINDER_EVERY_N_ATTACHMENTS === + 1 + ? 'full' + : 'sparse' + + // Always add the main plan_mode attachment + attachments.push({ + type: 'plan_mode', + reminderType, + isSubAgent: !!toolUseContext.agentId, + planFilePath, + planExists: existingPlan !== null, + }) + + return attachments +} + +/** + * Returns a plan_mode_exit attachment if we just exited plan mode. + * This is a one-time notification to tell the model it's no longer in plan mode. + */ +async function getPlanModeExitAttachment( + toolUseContext: ToolUseContext, +): Promise { + // Only trigger if the flag is set (we just exited plan mode) + if (!needsPlanModeExitAttachment()) { + return [] + } + + const appState = toolUseContext.getAppState() + if (appState.toolPermissionContext.mode === 'plan') { + setNeedsPlanModeExitAttachment(false) + return [] + } + + // Clear the flag - this is a one-time notification + setNeedsPlanModeExitAttachment(false) + + const planFilePath = getPlanFilePath(toolUseContext.agentId) + const planExists = getPlan(toolUseContext.agentId) !== null + + // Note: skill discovery does NOT fire on plan exit. By the time the plan is + // written, it's too late — the model should have had relevant skills WHILE + // planning. The user_message signal already fires on the request that + // triggers planning ("plan how to deploy this"), which is the right moment. + return [{ type: 'plan_mode_exit', planFilePath, planExists }] +} + +function getAutoModeAttachmentTurnCount(messages: Message[]): { + turnCount: number + foundAutoModeAttachment: boolean +} { + let turnsSinceLastAttachment = 0 + let foundAutoModeAttachment = false + + // Iterate backwards to find most recent auto_mode attachment. + // Count HUMAN turns (non-meta, non-tool-result user messages), not assistant + // messages — the tool loop in query.ts calls getAttachmentMessages on every + // tool round, so a single human turn with 100 tool calls would fire ~20 + // reminders if we counted assistant messages. Auto mode's target use case is + // long agentic sessions, where this accumulated 60-105× per session. + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + + if ( + message?.type === 'user' && + !message.isMeta && + !hasToolResultContent(message.message.content) + ) { + turnsSinceLastAttachment++ + } else if ( + message?.type === 'attachment' && + message.attachment.type === 'auto_mode' + ) { + foundAutoModeAttachment = true + break + } else if ( + message?.type === 'attachment' && + message.attachment.type === 'auto_mode_exit' + ) { + // Exit resets the throttle — treat as if no prior attachment exists + break + } + } + + return { turnCount: turnsSinceLastAttachment, foundAutoModeAttachment } +} + +/** + * Count auto_mode attachments since the last auto_mode_exit (or from start if no exit). + * This ensures the full/sparse cycle resets when re-entering auto mode. + */ +function countAutoModeAttachmentsSinceLastExit(messages: Message[]): number { + let count = 0 + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + if (message?.type === 'attachment') { + if (message.attachment.type === 'auto_mode_exit') { + break + } + if (message.attachment.type === 'auto_mode') { + count++ + } + } + } + return count +} + +async function getAutoModeAttachments( + messages: Message[] | undefined, + toolUseContext: ToolUseContext, +): Promise { + const appState = toolUseContext.getAppState() + const permissionContext = appState.toolPermissionContext + const inAuto = permissionContext.mode === 'auto' + const inPlanWithAuto = + permissionContext.mode === 'plan' && + (autoModeStateModule?.isAutoModeActive() ?? false) + if (!inAuto && !inPlanWithAuto) { + return [] + } + + // Check if we should attach based on turn count (except for first turn) + if (messages && messages.length > 0) { + const { turnCount, foundAutoModeAttachment } = + getAutoModeAttachmentTurnCount(messages) + // Only throttle if we've already sent an auto_mode attachment before + // On first turn in auto mode, always attach + if ( + foundAutoModeAttachment && + turnCount < AUTO_MODE_ATTACHMENT_CONFIG.TURNS_BETWEEN_ATTACHMENTS + ) { + return [] + } + } + + // Determine if this should be a full or sparse reminder + const attachmentCount = + countAutoModeAttachmentsSinceLastExit(messages ?? []) + 1 + const reminderType: 'full' | 'sparse' = + attachmentCount % + AUTO_MODE_ATTACHMENT_CONFIG.FULL_REMINDER_EVERY_N_ATTACHMENTS === + 1 + ? 'full' + : 'sparse' + + return [{ type: 'auto_mode', reminderType }] +} + +/** + * Returns an auto_mode_exit attachment if we just exited auto mode. + * This is a one-time notification to tell the model it's no longer in auto mode. + */ +async function getAutoModeExitAttachment( + toolUseContext: ToolUseContext, +): Promise { + if (!needsAutoModeExitAttachment()) { + return [] + } + + const appState = toolUseContext.getAppState() + // Suppress when auto is still active — covers both mode==='auto' and + // plan-with-auto-active (where mode==='plan' but classifier runs). + if ( + appState.toolPermissionContext.mode === 'auto' || + (autoModeStateModule?.isAutoModeActive() ?? false) + ) { + setNeedsAutoModeExitAttachment(false) + return [] + } + + setNeedsAutoModeExitAttachment(false) + return [{ type: 'auto_mode_exit' }] +} + +/** + * Detects when the local date has changed since the last turn (user coding + * past midnight) and emits an attachment to notify the model. + * + * The date_change attachment is appended at the tail of the conversation, + * so the model learns the new date without mutating the cached prefix. + * messages[0] (from getUserContext → prependUserContext) intentionally + * keeps the stale date — clearing that cache would regenerate the prefix + * and turn the entire conversation into cache_creation on the next turn + * (~920K effective tokens per midnight crossing per overnight session). + * + * Exported for testing — regression guard for the cache-clear removal. + */ +export function getDateChangeAttachments( + messages: Message[] | undefined, +): Attachment[] { + const currentDate = getLocalISODate() + const lastDate = getLastEmittedDate() + + if (lastDate === null) { + // First turn — just record, no attachment needed + setLastEmittedDate(currentDate) + return [] + } + + if (currentDate === lastDate) { + return [] + } + + setLastEmittedDate(currentDate) + + // Assistant mode: flush yesterday's transcript to the per-day file so + // the /dream skill (1–5am local) finds it even if no compaction fires + // today. Fire-and-forget; writeSessionTranscriptSegment buckets by + // message timestamp so a multi-day gap flushes each day correctly. + if (feature('KAIROS')) { + if (getKairosActive() && messages !== undefined) { + sessionTranscriptModule?.flushOnDateChange(messages, currentDate) + } + } + + return [{ type: 'date_change', newDate: currentDate }] +} + +function getUltrathinkEffortAttachment(input: string | null): Attachment[] { + if (!isUltrathinkEnabled() || !input || !hasUltrathinkKeyword(input)) { + return [] + } + logEvent('tengu_ultrathink', {}) + return [{ type: 'ultrathink_effort', level: 'high' }] +} + +// Exported for compact.ts — the gate must be identical at both call sites. +export function getDeferredToolsDeltaAttachment( + tools: Tools, + model: string, + messages: Message[] | undefined, + scanContext?: DeferredToolsDeltaScanContext, +): Attachment[] { + if (!isDeferredToolsDeltaEnabled()) return [] + // These three checks mirror the sync parts of isToolSearchEnabled — + // the attachment text says "available via ToolSearch", so ToolSearch + // has to actually be in the request. The async auto-threshold check + // is not replicated (would double-fire tengu_tool_search_mode_decision); + // in tst-auto below-threshold the attachment can fire while ToolSearch + // is filtered out, but that's a narrow case and the tools announced + // are directly callable anyway. + if (!isToolSearchEnabledOptimistic()) return [] + if (!modelSupportsToolReference(model)) return [] + if (!isToolSearchToolAvailable(tools)) return [] + const delta = getDeferredToolsDelta(tools, messages ?? [], scanContext) + if (!delta) return [] + return [{ type: 'deferred_tools_delta', ...delta }] +} + +/** + * Diff the current filtered agent pool against what's already been announced + * in this conversation (reconstructed from prior agent_listing_delta + * attachments). Returns [] if nothing changed or the gate is off. + * + * The agent list was embedded in AgentTool's description, causing ~10.2% of + * fleet cache_creation: MCP async connect, /reload-plugins, or + * permission-mode change → description changes → full tool-schema cache bust. + * Moving the list here keeps the tool description static. + * + * Exported for compact.ts — re-announces the full set after compaction eats + * prior deltas. + */ +export function getAgentListingDeltaAttachment( + toolUseContext: ToolUseContext, + messages: Message[] | undefined, +): Attachment[] { + if (!shouldInjectAgentListInMessages()) return [] + + // Skip if AgentTool isn't in the pool — the listing would be unactionable. + if ( + !toolUseContext.options.tools.some(t => toolMatchesName(t, AGENT_TOOL_NAME)) + ) { + return [] + } + + const { activeAgents, allowedAgentTypes } = + toolUseContext.options.agentDefinitions + + // Mirror AgentTool.prompt()'s filtering: MCP requirements → deny rules → + // allowedAgentTypes restriction. Keep this in sync with AgentTool.tsx. + const mcpServers = new Set() + for (const tool of toolUseContext.options.tools) { + const info = mcpInfoFromString(tool.name) + if (info) mcpServers.add(info.serverName) + } + const permissionContext = toolUseContext.getAppState().toolPermissionContext + let filtered = filterDeniedAgents( + filterAgentsByMcpRequirements(activeAgents, [...mcpServers]), + permissionContext, + AGENT_TOOL_NAME, + ) + if (allowedAgentTypes) { + filtered = filtered.filter(a => allowedAgentTypes.includes(a.agentType)) + } + + // Reconstruct announced set from prior deltas in the transcript. + const announced = new Set() + for (const msg of messages ?? []) { + if (msg.type !== 'attachment') continue + if (msg.attachment.type !== 'agent_listing_delta') continue + for (const t of msg.attachment.addedTypes) announced.add(t) + for (const t of msg.attachment.removedTypes) announced.delete(t) + } + + const currentTypes = new Set(filtered.map(a => a.agentType)) + const added = filtered.filter(a => !announced.has(a.agentType)) + const removed: string[] = [] + for (const t of announced) { + if (!currentTypes.has(t)) removed.push(t) + } + + if (added.length === 0 && removed.length === 0) return [] + + // Sort for deterministic output — agent load order is nondeterministic + // (plugin load races, MCP async connect). + added.sort((a, b) => a.agentType.localeCompare(b.agentType)) + removed.sort() + + return [ + { + type: 'agent_listing_delta', + addedTypes: added.map(a => a.agentType), + addedLines: added.map(formatAgentLine), + removedTypes: removed, + isInitial: announced.size === 0, + showConcurrencyNote: getSubscriptionType() !== 'pro', + }, + ] +} + +// Exported for compact.ts / reactiveCompact.ts — single source of truth for the gate. +export function getMcpInstructionsDeltaAttachment( + mcpClients: MCPServerConnection[], + tools: Tools, + model: string, + messages: Message[] | undefined, +): Attachment[] { + if (!isMcpInstructionsDeltaEnabled()) return [] + + // The chrome ToolSearch hint is client-authored and ToolSearch-conditional; + // actual server `instructions` are unconditional. Decide the chrome part + // here, pass it into the pure diff as a synthesized entry. + const clientSide: ClientSideInstruction[] = [] + if ( + isToolSearchEnabledOptimistic() && + modelSupportsToolReference(model) && + isToolSearchToolAvailable(tools) + ) { + clientSide.push({ + serverName: CLAUDE_IN_CHROME_MCP_SERVER_NAME, + block: CHROME_TOOL_SEARCH_INSTRUCTIONS, + }) + } + + const delta = getMcpInstructionsDelta(mcpClients, messages ?? [], clientSide) + if (!delta) return [] + return [{ type: 'mcp_instructions_delta', ...delta }] +} + +function getCriticalSystemReminderAttachment( + toolUseContext: ToolUseContext, +): Attachment[] { + const reminder = toolUseContext.criticalSystemReminder_EXPERIMENTAL + if (!reminder) { + return [] + } + return [{ type: 'critical_system_reminder', content: reminder }] +} + +function getOutputStyleAttachment(): Attachment[] { + const settings = getSettings_DEPRECATED() + const outputStyle = settings?.outputStyle || 'default' + + // Only show for non-default styles + if (outputStyle === 'default') { + return [] + } + + return [ + { + type: 'output_style', + style: outputStyle, + }, + ] +} + +async function getSelectedLinesFromIDE( + ideSelection: IDESelection | null, + toolUseContext: ToolUseContext, +): Promise { + const ideName = getConnectedIdeName(toolUseContext.options.mcpClients) + if ( + !ideName || + ideSelection?.lineStart === undefined || + !ideSelection.text || + !ideSelection.filePath + ) { + return [] + } + + const appState = toolUseContext.getAppState() + if (isFileReadDenied(ideSelection.filePath, appState.toolPermissionContext)) { + return [] + } + + return [ + { + type: 'selected_lines_in_ide', + ideName, + lineStart: ideSelection.lineStart, + lineEnd: ideSelection.lineStart + ideSelection.lineCount - 1, + filename: ideSelection.filePath, + content: ideSelection.text, + displayPath: relative(getCwd(), ideSelection.filePath), + }, + ] +} + +/** + * Computes the directories to process for nested memory file loading. + * Returns two lists: + * - nestedDirs: Directories between CWD and targetPath (processed for CLAUDE.md + all rules) + * - cwdLevelDirs: Directories from root to CWD (processed for conditional rules only) + * + * @param targetPath The target file path + * @param originalCwd The original current working directory + * @returns Object with nestedDirs and cwdLevelDirs arrays, both ordered from parent to child + */ +export function getDirectoriesToProcess( + targetPath: string, + originalCwd: string, +): { nestedDirs: string[]; cwdLevelDirs: string[] } { + // Build list of directories from original CWD to targetPath's directory + const targetDir = dirname(resolve(targetPath)) + const nestedDirs: string[] = [] + let currentDir = targetDir + + // Walk up from target directory to original CWD + while (currentDir !== originalCwd && currentDir !== parse(currentDir).root) { + if (currentDir.startsWith(originalCwd)) { + nestedDirs.push(currentDir) + } + currentDir = dirname(currentDir) + } + + // Reverse to get order from CWD down to target + nestedDirs.reverse() + + // Build list of directories from root to CWD (for conditional rules only) + const cwdLevelDirs: string[] = [] + currentDir = originalCwd + + while (currentDir !== parse(currentDir).root) { + cwdLevelDirs.push(currentDir) + currentDir = dirname(currentDir) + } + + // Reverse to get order from root to CWD + cwdLevelDirs.reverse() + + return { nestedDirs, cwdLevelDirs } +} + +/** + * Converts memory files to attachments, filtering out already-loaded files. + * + * @param memoryFiles The memory files to convert + * @param toolUseContext The tool use context (for tracking loaded files) + * @returns Array of nested memory attachments + */ +function isInstructionsMemoryType( + type: MemoryFileInfo['type'], +): type is InstructionsMemoryType { + return ( + type === 'User' || + type === 'Project' || + type === 'Local' || + type === 'Managed' + ) +} + +/** Exported for testing — regression guard for LRU-eviction re-injection. */ +export function memoryFilesToAttachments( + memoryFiles: MemoryFileInfo[], + toolUseContext: ToolUseContext, + triggerFilePath?: string, +): Attachment[] { + const attachments: Attachment[] = [] + const shouldFireHook = hasInstructionsLoadedHook() + + for (const memoryFile of memoryFiles) { + // Dedup: loadedNestedMemoryPaths is a non-evicting Set; readFileState + // is a 100-entry LRU that drops entries in busy sessions, so relying + // on it alone re-injects the same CLAUDE.md on every eviction cycle. + if (toolUseContext.loadedNestedMemoryPaths?.has(memoryFile.path)) { + continue + } + if (!toolUseContext.readFileState.has(memoryFile.path)) { + attachments.push({ + type: 'nested_memory', + path: memoryFile.path, + content: memoryFile, + displayPath: relative(getCwd(), memoryFile.path), + }) + toolUseContext.loadedNestedMemoryPaths?.add(memoryFile.path) + + // Mark as loaded in readFileState — this provides cross-function and + // cross-turn dedup via the .has() check above. + // + // When the injected content doesn't match disk (stripped HTML comments, + // stripped frontmatter, truncated MEMORY.md), cache the RAW disk bytes + // with `isPartialView: true`. Edit/Write see the flag and require a real + // Read first; getChangedFiles sees real content + undefined offset/limit + // so mid-session change detection still works. + toolUseContext.readFileState.set(memoryFile.path, { + content: memoryFile.contentDiffersFromDisk + ? (memoryFile.rawContent ?? memoryFile.content) + : memoryFile.content, + timestamp: Date.now(), + offset: undefined, + limit: undefined, + isPartialView: memoryFile.contentDiffersFromDisk, + }) + + + // Fire InstructionsLoaded hook for audit/observability (fire-and-forget) + if (shouldFireHook && isInstructionsMemoryType(memoryFile.type)) { + const loadReason = memoryFile.globs + ? 'path_glob_match' + : memoryFile.parent + ? 'include' + : 'nested_traversal' + void executeInstructionsLoadedHooks( + memoryFile.path, + memoryFile.type, + loadReason, + { + globs: memoryFile.globs, + triggerFilePath, + parentFilePath: memoryFile.parent, + }, + ) + } + } + } + + return attachments +} + +/** + * Loads nested memory files for a given file path and returns them as attachments. + * This function performs directory traversal to find CLAUDE.md files and conditional rules + * that apply to the target file path. + * + * Processing order (must be preserved): + * 1. Managed/User conditional rules matching targetPath + * 2. Nested directories (CWD → target): CLAUDE.md + unconditional + conditional rules + * 3. CWD-level directories (root → CWD): conditional rules only + * + * @param filePath The file path to get nested memory files for + * @param toolUseContext The tool use context + * @param appState The app state containing tool permission context + * @returns Array of nested memory attachments + */ +async function getNestedMemoryAttachmentsForFile( + filePath: string, + toolUseContext: ToolUseContext, + appState: { toolPermissionContext: ToolPermissionContext }, +): Promise { + const attachments: Attachment[] = [] + + try { + // Early return if path is not in allowed working path + if (!pathInAllowedWorkingPath(filePath, appState.toolPermissionContext)) { + return attachments + } + + const processedPaths = new Set() + const originalCwd = getOriginalCwd() + + // Phase 1: Process Managed and User conditional rules + const managedUserRules = await getManagedAndUserConditionalRules( + filePath, + processedPaths, + ) + attachments.push( + ...memoryFilesToAttachments(managedUserRules, toolUseContext, filePath), + ) + + // Phase 2: Get directories to process + const { nestedDirs, cwdLevelDirs } = getDirectoriesToProcess( + filePath, + originalCwd, + ) + + const skipProjectLevel = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_paper_halyard', + false, + ) + + // Phase 3: Process nested directories (CWD → target) + // Each directory gets: CLAUDE.md + unconditional rules + conditional rules + for (const dir of nestedDirs) { + const memoryFiles = ( + await getMemoryFilesForNestedDirectory(dir, filePath, processedPaths) + ).filter( + f => !skipProjectLevel || (f.type !== 'Project' && f.type !== 'Local'), + ) + attachments.push( + ...memoryFilesToAttachments(memoryFiles, toolUseContext, filePath), + ) + } + + // Phase 4: Process CWD-level directories (root → CWD) + // Only conditional rules (unconditional rules are already loaded eagerly) + for (const dir of cwdLevelDirs) { + const conditionalRules = ( + await getConditionalRulesForCwdLevelDirectory( + dir, + filePath, + processedPaths, + ) + ).filter( + f => !skipProjectLevel || (f.type !== 'Project' && f.type !== 'Local'), + ) + attachments.push( + ...memoryFilesToAttachments(conditionalRules, toolUseContext, filePath), + ) + } + } catch (error) { + logError(error) + } + + return attachments +} + +async function getOpenedFileFromIDE( + ideSelection: IDESelection | null, + toolUseContext: ToolUseContext, +): Promise { + if (!ideSelection?.filePath || ideSelection.text) { + return [] + } + + const appState = toolUseContext.getAppState() + if (isFileReadDenied(ideSelection.filePath, appState.toolPermissionContext)) { + return [] + } + + // Get nested memory files + const nestedMemoryAttachments = await getNestedMemoryAttachmentsForFile( + ideSelection.filePath, + toolUseContext, + appState, + ) + + // Return nested memory attachments followed by the opened file attachment + return [ + ...nestedMemoryAttachments, + { + type: 'opened_file_in_ide', + filename: ideSelection.filePath, + }, + ] +} + +async function processAtMentionedFiles( + input: string, + toolUseContext: ToolUseContext, +): Promise { + const files = extractAtMentionedFiles(input) + if (files.length === 0) return [] + + const appState = toolUseContext.getAppState() + const results = await Promise.all( + files.map(async file => { + try { + const { filename, lineStart, lineEnd } = parseAtMentionedFileLines(file) + const absoluteFilename = expandPath(filename) + + if ( + isFileReadDenied(absoluteFilename, appState.toolPermissionContext) + ) { + return null + } + + // Check if it's a directory + try { + const stats = await stat(absoluteFilename) + if (stats.isDirectory()) { + try { + const entries = await readdir(absoluteFilename, { + withFileTypes: true, + }) + const MAX_DIR_ENTRIES = 1000 + const truncated = entries.length > MAX_DIR_ENTRIES + const names = entries.slice(0, MAX_DIR_ENTRIES).map(e => e.name) + if (truncated) { + names.push( + `\u2026 and ${entries.length - MAX_DIR_ENTRIES} more entries`, + ) + } + const stdout = names.join('\n') + logEvent('tengu_at_mention_extracting_directory_success', {}) + + return { + type: 'directory' as const, + path: absoluteFilename, + content: stdout, + displayPath: relative(getCwd(), absoluteFilename), + } + } catch { + return null + } + } + } catch { + // If stat fails, continue with file logic + } + + return await generateFileAttachment( + absoluteFilename, + toolUseContext, + 'tengu_at_mention_extracting_filename_success', + 'tengu_at_mention_extracting_filename_error', + 'at-mention', + { + offset: lineStart, + limit: lineEnd && lineStart ? lineEnd - lineStart + 1 : undefined, + }, + ) + } catch { + logEvent('tengu_at_mention_extracting_filename_error', {}) + } + }), + ) + return results.filter(Boolean) as Attachment[] +} + +function processAgentMentions( + input: string, + agents: AgentDefinition[], +): Attachment[] { + const agentMentions = extractAgentMentions(input) + if (agentMentions.length === 0) return [] + + const results = agentMentions.map(mention => { + const agentType = mention.replace('agent-', '') + const agentDef = agents.find(def => def.agentType === agentType) + + if (!agentDef) { + logEvent('tengu_at_mention_agent_not_found', {}) + return null + } + + logEvent('tengu_at_mention_agent_success', {}) + + return { + type: 'agent_mention' as const, + agentType: agentDef.agentType, + } + }) + + return results.filter( + (result): result is NonNullable => result !== null, + ) +} + +async function processMcpResourceAttachments( + input: string, + toolUseContext: ToolUseContext, +): Promise { + const resourceMentions = extractMcpResourceMentions(input) + if (resourceMentions.length === 0) return [] + + const mcpClients = toolUseContext.options.mcpClients || [] + + const results = await Promise.all( + resourceMentions.map(async mention => { + try { + const [serverName, ...uriParts] = mention.split(':') + const uri = uriParts.join(':') // Rejoin in case URI contains colons + + if (!serverName || !uri) { + logEvent('tengu_at_mention_mcp_resource_error', {}) + return null + } + + // Find the MCP client + const client = mcpClients.find(c => c.name === serverName) + if (!client || client.type !== 'connected') { + logEvent('tengu_at_mention_mcp_resource_error', {}) + return null + } + + // Find the resource in available resources to get its metadata + const serverResources = + toolUseContext.options.mcpResources?.[serverName] || [] + const resourceInfo = serverResources.find(r => r.uri === uri) + if (!resourceInfo) { + logEvent('tengu_at_mention_mcp_resource_error', {}) + return null + } + + try { + const result = await client.client.readResource({ + uri, + }) + + logEvent('tengu_at_mention_mcp_resource_success', {}) + + return { + type: 'mcp_resource' as const, + server: serverName, + uri, + name: resourceInfo.name || uri, + description: resourceInfo.description, + content: result, + } + } catch (error) { + logEvent('tengu_at_mention_mcp_resource_error', {}) + logError(error) + return null + } + } catch { + logEvent('tengu_at_mention_mcp_resource_error', {}) + return null + } + }), + ) + + return results.filter( + (result): result is NonNullable => result !== null, + ) as Attachment[] +} + +export async function getChangedFiles( + toolUseContext: ToolUseContext, +): Promise { + const filePaths = cacheKeys(toolUseContext.readFileState) + if (filePaths.length === 0) return [] + + const appState = toolUseContext.getAppState() + const results = await Promise.all( + filePaths.map(async filePath => { + const fileState = toolUseContext.readFileState.get(filePath) + if (!fileState) return null + + // TODO: Implement offset/limit support for changed files + if (fileState.offset !== undefined || fileState.limit !== undefined) { + return null + } + + const normalizedPath = expandPath(filePath) + + // Check if file has a deny rule configured + if (isFileReadDenied(normalizedPath, appState.toolPermissionContext)) { + return null + } + + try { + const mtime = await getFileModificationTimeAsync(normalizedPath) + if (mtime <= fileState.timestamp) { + return null + } + + const fileInput = { file_path: normalizedPath } + + // Validate file path is valid + const isValid = await FileReadTool.validateInput( + fileInput, + toolUseContext, + ) + if (!isValid.result) { + return null + } + + const result = await FileReadTool.call(fileInput, toolUseContext) + // Extract only the changed section + if (result.data.type === 'text') { + const snippet = getSnippetForTwoFileDiff( + fileState.content, + result.data.file.content, + ) + + // File was touched but not modified + if (snippet === '') { + return null + } + + return { + type: 'edited_text_file' as const, + filename: normalizedPath, + snippet, + } + } + + // For non-text files (images), apply the same token limit logic as FileReadTool + if (result.data.type === 'image') { + try { + const data = await readImageWithTokenBudget(normalizedPath) + return { + type: 'edited_image_file' as const, + filename: normalizedPath, + content: data, + } + } catch (compressionError) { + logError(compressionError) + logEvent('tengu_watched_file_compression_failed', { + file: normalizedPath, + } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS) + return null + } + } + + // notebook / pdf / parts — no diff representation; explicitly + // null so the map callback has no implicit-undefined path. + return null + } catch (err) { + // Evict ONLY on ENOENT (file truly deleted). Transient stat + // failures — atomic-save races (editor writes tmp→rename and + // stat hits the gap), EACCES churn, network-FS hiccups — must + // NOT evict, or the next Edit fails code-6 even though the + // file still exists and the model just read it. VS Code + // auto-save/format-on-save hits this race especially often. + // See regression analysis on PR #18525. + if (isENOENT(err)) { + toolUseContext.readFileState.delete(filePath) + } + return null + } + }), + ) + return results.filter(result => result != null) as Attachment[] +} + +/** + * Processes paths that need nested memory attachments and checks for nested CLAUDE.md files + * Uses nestedMemoryAttachmentTriggers field from ToolUseContext + */ +async function getNestedMemoryAttachments( + toolUseContext: ToolUseContext, +): Promise { + // Check triggers first — getAppState() waits for a React render cycle, + // and the common case is an empty trigger set. + if ( + !toolUseContext.nestedMemoryAttachmentTriggers || + toolUseContext.nestedMemoryAttachmentTriggers.size === 0 + ) { + return [] + } + + const appState = toolUseContext.getAppState() + const attachments: Attachment[] = [] + + for (const filePath of toolUseContext.nestedMemoryAttachmentTriggers) { + const nestedAttachments = await getNestedMemoryAttachmentsForFile( + filePath, + toolUseContext, + appState, + ) + attachments.push(...nestedAttachments) + } + + toolUseContext.nestedMemoryAttachmentTriggers.clear() + + return attachments +} + +async function getRelevantMemoryAttachments( + input: string, + agents: AgentDefinition[], + readFileState: FileStateCache, + recentTools: readonly string[], + signal: AbortSignal, + alreadySurfaced: ReadonlySet, +): Promise { + // If an agent is @-mentioned, search only its memory dir (isolation). + // Otherwise search the auto-memory dir. + const memoryDirs = extractAgentMentions(input).flatMap(mention => { + const agentType = mention.replace('agent-', '') + const agentDef = agents.find(def => def.agentType === agentType) + return agentDef?.memory + ? [getAgentMemoryDir(agentType, agentDef.memory)] + : [] + }) + const dirs = memoryDirs.length > 0 ? memoryDirs : [getAutoMemPath()] + + const allResults = await Promise.all( + dirs.map(dir => + findRelevantMemories( + input, + dir, + signal, + recentTools, + alreadySurfaced, + ).catch(() => []), + ), + ) + // alreadySurfaced is filtered inside the selector so Sonnet spends its + // 5-slot budget on fresh candidates; readFileState catches files the + // model read via FileReadTool. The redundant alreadySurfaced check here + // is a belt-and-suspenders guard (multi-dir results may re-introduce a + // path the selector filtered in a different dir). + const selected = allResults + .flat() + .filter(m => !readFileState.has(m.path) && !alreadySurfaced.has(m.path)) + .slice(0, 5) + + const memories = await readMemoriesForSurfacing(selected, signal) + + if (memories.length === 0) { + return [] + } + return [{ type: 'relevant_memories' as const, memories }] +} + +/** + * Scan messages for past relevant_memories attachments. Returns both the + * set of surfaced paths (for selector de-dup) and cumulative byte count + * (for session-total throttle). Scanning messages rather than tracking + * in toolUseContext means compact naturally resets both — old attachments + * are gone from the compacted transcript, so re-surfacing is valid again. + */ +export function collectSurfacedMemories(messages: ReadonlyArray): { + paths: Set + totalBytes: number +} { + const paths = new Set() + let totalBytes = 0 + for (const m of messages) { + if (m.type === 'attachment' && m.attachment.type === 'relevant_memories') { + for (const mem of m.attachment.memories) { + paths.add(mem.path) + totalBytes += mem.content.length + } + } + } + return { paths, totalBytes } +} + +/** + * Reads a set of relevance-ranked memory files for injection as + * attachments. Enforces both MAX_MEMORY_LINES and + * MAX_MEMORY_BYTES via readFileInRange's truncateOnByteLimit option. + * Truncation surfaces partial + * content with a note rather than dropping the file — findRelevantMemories + * already picked this as most-relevant, so the frontmatter + opening context + * is worth surfacing even if later lines are cut. + * + * Exported for direct testing without mocking the ranker + GB gates. + */ +export async function readMemoriesForSurfacing( + selected: ReadonlyArray<{ path: string; mtimeMs: number }>, + signal?: AbortSignal, +): Promise< + Array<{ + path: string + content: string + mtimeMs: number + header: string + limit?: number + }> +> { + const results = await Promise.all( + selected.map(async ({ path: filePath, mtimeMs }) => { + try { + const result = await readFileInRange( + filePath, + 0, + MAX_MEMORY_LINES, + MAX_MEMORY_BYTES, + signal, + { truncateOnByteLimit: true }, + ) + const truncated = + result.totalLines > MAX_MEMORY_LINES || result.truncatedByBytes + const content = truncated + ? result.content + + `\n\n> This memory file was truncated (${result.truncatedByBytes ? `${MAX_MEMORY_BYTES} byte limit` : `first ${MAX_MEMORY_LINES} lines`}). Use the ${FILE_READ_TOOL_NAME} tool to view the complete file at: ${filePath}` + : result.content + return { + path: filePath, + content, + mtimeMs, + header: memoryHeader(filePath, mtimeMs), + limit: truncated ? result.lineCount : undefined, + } + } catch { + return null + } + }), + ) + return results.filter(r => r !== null) +} + +/** + * Header string for a relevant-memory block. Exported so messages.ts + * can fall back for resumed sessions where the stored header is missing. + */ +export function memoryHeader(path: string, mtimeMs: number): string { + const staleness = memoryFreshnessText(mtimeMs) + return staleness + ? `${staleness}\n\nMemory: ${path}:` + : `Memory (saved ${memoryAge(mtimeMs)}): ${path}:` +} + +/** + * A memory relevance-selector prefetch handle. The promise is started once + * per user turn and runs while the main model streams and tools execute. + * At the collect point (post-tools), the caller reads settledAt to + * consume-if-ready or skip-and-retry-next-iteration — the prefetch never + * blocks the turn. + * + * Disposable: query.ts binds with `using`, so [Symbol.dispose] fires on all + * generator exit paths (return, throw, .return() closure) — aborting the + * in-flight request and emitting terminal telemetry without instrumenting + * each of the ~13 return sites inside the while loop. + */ +export type MemoryPrefetch = { + promise: Promise + /** Set by promise.finally(). null until the promise settles. */ + settledAt: number | null + /** Set by the collect point in query.ts. -1 until consumed. */ + consumedOnIteration: number + [Symbol.dispose](): void +} + +/** + * Starts the relevant memory search as an async prefetch. + * Extracts the last real user prompt from messages (skipping isMeta system + * injections) and kicks off a non-blocking search. Returns a Disposable + * handle with settlement tracking. Bound with `using` in query.ts. + */ +export function startRelevantMemoryPrefetch( + messages: ReadonlyArray, + toolUseContext: ToolUseContext, +): MemoryPrefetch | undefined { + if ( + !isAutoMemoryEnabled() || + !getFeatureValue_CACHED_MAY_BE_STALE('tengu_moth_copse', false) + ) { + return undefined + } + + const lastUserMessage = messages.findLast(m => m.type === 'user' && !m.isMeta) + if (!lastUserMessage) { + return undefined + } + + const input = getUserMessageText(lastUserMessage) + // Single-word prompts lack enough context for meaningful term extraction + if (!input || !/\s/.test(input.trim())) { + return undefined + } + + const surfaced = collectSurfacedMemories(messages) + if (surfaced.totalBytes >= RELEVANT_MEMORIES_CONFIG.MAX_SESSION_BYTES) { + return undefined + } + + // Chained to the turn-level abort so user Escape cancels the sideQuery + // immediately, not just on [Symbol.dispose] when queryLoop exits. + const controller = createChildAbortController(toolUseContext.abortController) + const firedAt = Date.now() + const promise = getRelevantMemoryAttachments( + input, + toolUseContext.options.agentDefinitions.activeAgents, + toolUseContext.readFileState, + collectRecentSuccessfulTools(messages, lastUserMessage), + controller.signal, + surfaced.paths, + ).catch(e => { + if (!isAbortError(e)) { + logError(e) + } + return [] + }) + + const handle: MemoryPrefetch = { + promise, + settledAt: null, + consumedOnIteration: -1, + [Symbol.dispose]() { + controller.abort() + logEvent('tengu_memdir_prefetch_collected', { + hidden_by_first_iteration: + handle.settledAt !== null && handle.consumedOnIteration === 0, + consumed_on_iteration: handle.consumedOnIteration, + latency_ms: (handle.settledAt ?? Date.now()) - firedAt, + }) + }, + } + void promise.finally(() => { + handle.settledAt = Date.now() + }) + return handle +} + +type ToolResultBlock = { + type: 'tool_result' + tool_use_id: string + is_error?: boolean +} + +function isToolResultBlock(b: unknown): b is ToolResultBlock { + return ( + typeof b === 'object' && + b !== null && + (b as ToolResultBlock).type === 'tool_result' && + typeof (b as ToolResultBlock).tool_use_id === 'string' + ) +} + +/** + * Check whether a user message's content contains tool_result blocks. + * This is more reliable than checking `toolUseResult === undefined` because + * sub-agent tool result messages explicitly set `toolUseResult` to `undefined` + * when `preserveToolUseResults` is false (the default for Explore agents). + */ +function hasToolResultContent(content: unknown): boolean { + return Array.isArray(content) && content.some(isToolResultBlock) +} + +/** + * Tools that succeeded (and never errored) since the previous real turn + * boundary. The memory selector uses this to suppress docs about tools + * that are working — surfacing reference material for a tool the model + * is already calling successfully is noise. + * + * Any error → tool excluded (model is struggling, docs stay available). + * No result yet → also excluded (outcome unknown). + * + * tool_use lives in assistant content; tool_result in user content + * (toolUseResult set, isMeta undefined). Both are within the scan window. + * Backward scan sees results before uses so we collect both by id and + * resolve after. + */ +export function collectRecentSuccessfulTools( + messages: ReadonlyArray, + lastUserMessage: Message, +): readonly string[] { + const useIdToName = new Map() + const resultByUseId = new Map() + for (let i = messages.length - 1; i >= 0; i--) { + const m = messages[i] + if (!m) continue + if (isHumanTurn(m) && m !== lastUserMessage) break + if (m.type === 'assistant' && typeof m.message.content !== 'string') { + for (const block of m.message.content) { + if (block.type === 'tool_use') useIdToName.set(block.id, block.name) + } + } else if ( + m.type === 'user' && + 'message' in m && + Array.isArray(m.message.content) + ) { + for (const block of m.message.content) { + if (isToolResultBlock(block)) { + resultByUseId.set(block.tool_use_id, block.is_error === true) + } + } + } + } + const failed = new Set() + const succeeded = new Set() + for (const [id, name] of useIdToName) { + const errored = resultByUseId.get(id) + if (errored === undefined) continue + if (errored) { + failed.add(name) + } else { + succeeded.add(name) + } + } + return [...succeeded].filter(t => !failed.has(t)) +} + + +/** + * Filters prefetched memory attachments to exclude memories the model already + * has in context via FileRead/Write/Edit tool calls (any iteration this turn) + * or a previous turn's memory surfacing — both tracked in the cumulative + * readFileState. Survivors are then marked in readFileState so subsequent + * turns won't re-surface them. + * + * The mark-after-filter ordering is load-bearing: readMemoriesForSurfacing + * used to write to readFileState during the prefetch, which meant the filter + * saw every prefetch-selected path as "already in context" and dropped them + * all (self-referential filter). Deferring the write to here, after the + * filter runs, breaks that cycle while still deduping against tool calls + * from any iteration. + */ +export function filterDuplicateMemoryAttachments( + attachments: Attachment[], + readFileState: FileStateCache, +): Attachment[] { + return attachments + .map(attachment => { + if (attachment.type !== 'relevant_memories') return attachment + const filtered = attachment.memories.filter( + m => !readFileState.has(m.path), + ) + for (const m of filtered) { + readFileState.set(m.path, { + content: m.content, + timestamp: m.mtimeMs, + offset: undefined, + limit: m.limit, + }) + } + return filtered.length > 0 ? { ...attachment, memories: filtered } : null + }) + .filter((a): a is Attachment => a !== null) +} + +/** + * Processes skill directories that were discovered during file operations. + * Uses dynamicSkillDirTriggers field from ToolUseContext + */ +async function getDynamicSkillAttachments( + toolUseContext: ToolUseContext, +): Promise { + const attachments: Attachment[] = [] + + if ( + toolUseContext.dynamicSkillDirTriggers && + toolUseContext.dynamicSkillDirTriggers.size > 0 + ) { + // Parallelize: readdir all skill dirs concurrently + const perDirResults = await Promise.all( + Array.from(toolUseContext.dynamicSkillDirTriggers).map(async skillDir => { + try { + const entries = await readdir(skillDir, { withFileTypes: true }) + const candidates = entries + .filter(e => e.isDirectory() || e.isSymbolicLink()) + .map(e => e.name) + // Parallelize: stat all SKILL.md candidates concurrently + const checked = await Promise.all( + candidates.map(async name => { + try { + await stat(resolve(skillDir, name, 'SKILL.md')) + return name + } catch { + return null // SKILL.md doesn't exist, skip this entry + } + }), + ) + return { + skillDir, + skillNames: checked.filter((n): n is string => n !== null), + } + } catch { + // Ignore errors reading skill directories (e.g., directory doesn't exist) + return { skillDir, skillNames: [] } + } + }), + ) + + for (const { skillDir, skillNames } of perDirResults) { + if (skillNames.length > 0) { + attachments.push({ + type: 'dynamic_skill', + skillDir, + skillNames, + displayPath: relative(getCwd(), skillDir), + }) + } + } + + toolUseContext.dynamicSkillDirTriggers.clear() + } + + return attachments +} + +// Track which skills have been sent to avoid re-sending. Keyed by agentId +// (empty string = main thread) so subagents get their own turn-0 listing — +// without per-agent scoping, the main thread populating this Set would cause +// every subagent's filterToBundledAndMcp result to dedup to empty. +const sentSkillNames = new Map>() + +// Called when the skill set genuinely changes (plugin reload, skill file +// change on disk) so new skills get announced. NOT called on compact — +// post-compact re-injection costs ~4K tokens/event for marginal benefit. +export function resetSentSkillNames(): void { + sentSkillNames.clear() + suppressNext = false +} + +/** + * Suppress the next skill-listing injection. Called by conversationRecovery + * on --resume when a skill_listing attachment already exists in the + * transcript. + * + * `sentSkillNames` is module-scope — process-local. Each `claude -p` spawn + * starts with an empty Map, so without this every resume re-injects the + * full ~600-token listing even though it's already in the conversation from + * the prior process. Shows up on every --resume; particularly loud for + * daemons that respawn frequently. + * + * Trade-off: skills added between sessions won't be announced until the + * next non-resume session. Acceptable — skill_listing was never meant to + * cover cross-process deltas, and the agent can still call them (they're + * in the Skill tool's runtime registry regardless). + */ +export function suppressNextSkillListing(): void { + suppressNext = true +} +let suppressNext = false + +// When skill-search is enabled and the filtered (bundled + MCP) listing exceeds +// this count, fall back to bundled-only. Protects MCP-heavy users (100+ servers) +// from truncation while keeping the turn-0 guarantee for typical setups. +const FILTERED_LISTING_MAX = 30 + +/** + * Filter skills to bundled (Anthropic-curated) + MCP (user-connected) only. + * Used when skill-search is enabled to resolve the turn-0 gap for subagents: + * these sources are small, intent-signaled, and won't hit the truncation budget. + * User/project/plugin skills (the long tail — 200+) go through discovery instead. + * + * Falls back to bundled-only if bundled+mcp exceeds FILTERED_LISTING_MAX. + */ +export function filterToBundledAndMcp(commands: Command[]): Command[] { + const filtered = commands.filter( + cmd => cmd.loadedFrom === 'bundled' || cmd.loadedFrom === 'mcp', + ) + if (filtered.length > FILTERED_LISTING_MAX) { + return filtered.filter(cmd => cmd.loadedFrom === 'bundled') + } + return filtered +} + +async function getSkillListingAttachments( + toolUseContext: ToolUseContext, +): Promise { + if (process.env.NODE_ENV === 'test') { + return [] + } + + // Skip skill listing for agents that don't have the Skill tool — they can't use skills directly. + if ( + !toolUseContext.options.tools.some(t => toolMatchesName(t, SKILL_TOOL_NAME)) + ) { + return [] + } + + const cwd = getProjectRoot() + const localCommands = await getSkillToolCommands(cwd) + const mcpSkills = getMcpSkillCommands( + toolUseContext.getAppState().mcp.commands, + ) + let allCommands = + mcpSkills.length > 0 + ? uniqBy([...localCommands, ...mcpSkills], 'name') + : localCommands + + // When skill search is active, filter to bundled + MCP instead of full + // suppression. Resolves the turn-0 gap: main thread gets turn-0 discovery + // via getTurnZeroSkillDiscovery (blocking), but subagents use the async + // subagent_spawn signal (collected post-tools, visible turn 1). Bundled + + // MCP are small and intent-signaled; user/project/plugin skills go through + // discovery. feature() first for DCE — the property-access string leaks + // otherwise even with ?. on null. + if ( + feature('EXPERIMENTAL_SKILL_SEARCH') && + skillSearchModules?.featureCheck.isSkillSearchEnabled() + ) { + allCommands = filterToBundledAndMcp(allCommands) + } + + const agentKey = toolUseContext.agentId ?? '' + let sent = sentSkillNames.get(agentKey) + if (!sent) { + sent = new Set() + sentSkillNames.set(agentKey, sent) + } + + // Resume path: prior process already injected a listing; it's in the + // transcript. Mark everything current as sent so only post-resume deltas + // (skills loaded later via /reload-plugins etc) get announced. + if (suppressNext) { + suppressNext = false + for (const cmd of allCommands) { + sent.add(cmd.name) + } + return [] + } + + // Find skills we haven't sent yet + const newSkills = allCommands.filter(cmd => !sent.has(cmd.name)) + + if (newSkills.length === 0) { + return [] + } + + // If no skills have been sent yet, this is the initial batch + const isInitial = sent.size === 0 + + // Mark as sent + for (const cmd of newSkills) { + sent.add(cmd.name) + } + + logForDebugging( + `Sending ${newSkills.length} skills via attachment (${isInitial ? 'initial' : 'dynamic'}, ${sent.size} total sent)`, + ) + + // Format within budget using existing logic + const contextWindowTokens = getContextWindowForModel( + toolUseContext.options.mainLoopModel, + getSdkBetas(), + ) + const content = formatCommandsWithinBudget(newSkills, contextWindowTokens) + + return [ + { + type: 'skill_listing', + content, + skillCount: newSkills.length, + isInitial, + }, + ] +} + +// getSkillDiscoveryAttachment moved to skillSearch/prefetch.ts as +// getTurnZeroSkillDiscovery — keeps the 'skill_discovery' string literal inside +// a feature-gated module so it doesn't leak into external builds. + +export function extractAtMentionedFiles(content: string): string[] { + // Extract filenames mentioned with @ symbol, including line range syntax: @file.txt#L10-20 + // Also supports quoted paths for files with spaces: @"my/file with spaces.txt" + // Example: "foo bar @baz moo" would extract "baz" + // Example: 'check @"my file.txt" please' would extract "my file.txt" + + // Two patterns: quoted paths and regular paths + const quotedAtMentionRegex = /(^|\s)@"([^"]+)"/g + const regularAtMentionRegex = /(^|\s)@([^\s]+)\b/g + + const quotedMatches: string[] = [] + const regularMatches: string[] = [] + + // Extract quoted mentions first (skip agent mentions like @"code-reviewer (agent)") + let match + while ((match = quotedAtMentionRegex.exec(content)) !== null) { + if (match[2] && !match[2].endsWith(' (agent)')) { + quotedMatches.push(match[2]) // The content inside quotes + } + } + + // Extract regular mentions + const regularMatchArray = content.match(regularAtMentionRegex) || [] + regularMatchArray.forEach(match => { + const filename = match.slice(match.indexOf('@') + 1) + // Don't include if it starts with a quote (already handled as quoted) + if (!filename.startsWith('"')) { + regularMatches.push(filename) + } + }) + + // Combine and deduplicate + return uniq([...quotedMatches, ...regularMatches]) +} + +export function extractMcpResourceMentions(content: string): string[] { + // Extract MCP resources mentioned with @ symbol in format @server:uri + // Example: "@server1:resource/path" would extract "server1:resource/path" + const atMentionRegex = /(^|\s)@([^\s]+:[^\s]+)\b/g + const matches = content.match(atMentionRegex) || [] + + // Remove the prefix (everything before @) from each match + return uniq(matches.map(match => match.slice(match.indexOf('@') + 1))) +} + +export function extractAgentMentions(content: string): string[] { + // Extract agent mentions in two formats: + // 1. @agent- (legacy/manual typing) + // Example: "@agent-code-elegance-refiner" → "agent-code-elegance-refiner" + // 2. @" (agent)" (from autocomplete selection) + // Example: '@"code-reviewer (agent)"' → "code-reviewer" + // Supports colons, dots, and at-signs for plugin-scoped agents like "@agent-asana:project-status-updater" + const results: string[] = [] + + // Match quoted format: @" (agent)" + const quotedAgentRegex = /(^|\s)@"([\w:.@-]+) \(agent\)"/g + let match + while ((match = quotedAgentRegex.exec(content)) !== null) { + if (match[2]) { + results.push(match[2]) + } + } + + // Match unquoted format: @agent- + const unquotedAgentRegex = /(^|\s)@(agent-[\w:.@-]+)/g + const unquotedMatches = content.match(unquotedAgentRegex) || [] + for (const m of unquotedMatches) { + results.push(m.slice(m.indexOf('@') + 1)) + } + + return uniq(results) +} + +interface AtMentionedFileLines { + filename: string + lineStart?: number + lineEnd?: number +} + +export function parseAtMentionedFileLines( + mention: string, +): AtMentionedFileLines { + // Parse mentions like "file.txt#L10-20", "file.txt#heading", or just "file.txt" + // Supports line ranges (#L10, #L10-20) and strips non-line-range fragments (#heading) + const match = mention.match(/^([^#]+)(?:#L(\d+)(?:-(\d+))?)?(?:#[^#]*)?$/) + + if (!match) { + return { filename: mention } + } + + const [, filename, lineStartStr, lineEndStr] = match + const lineStart = lineStartStr ? parseInt(lineStartStr, 10) : undefined + const lineEnd = lineEndStr ? parseInt(lineEndStr, 10) : lineStart + + return { filename: filename ?? mention, lineStart, lineEnd } +} + +async function getDiagnosticAttachments( + toolUseContext: ToolUseContext, +): Promise { + // Diagnostics are only useful if the agent has the Bash tool to act on them + if ( + !toolUseContext.options.tools.some(t => toolMatchesName(t, BASH_TOOL_NAME)) + ) { + return [] + } + + // Get new diagnostics from the tracker (IDE diagnostics via MCP) + const newDiagnostics = await diagnosticTracker.getNewDiagnostics() + if (newDiagnostics.length === 0) { + return [] + } + + return [ + { + type: 'diagnostics', + files: newDiagnostics, + isNew: true, + }, + ] +} + +/** + * Get LSP diagnostic attachments from passive LSP servers. + * Follows the AsyncHookRegistry pattern for consistent async attachment delivery. + */ +async function getLSPDiagnosticAttachments( + toolUseContext: ToolUseContext, +): Promise { + // LSP diagnostics are only useful if the agent has the Bash tool to act on them + if ( + !toolUseContext.options.tools.some(t => toolMatchesName(t, BASH_TOOL_NAME)) + ) { + return [] + } + + logForDebugging('LSP Diagnostics: getLSPDiagnosticAttachments called') + + try { + const diagnosticSets = checkForLSPDiagnostics() + + if (diagnosticSets.length === 0) { + return [] + } + + logForDebugging( + `LSP Diagnostics: Found ${diagnosticSets.length} pending diagnostic set(s)`, + ) + + // Convert each diagnostic set to an attachment + const attachments: Attachment[] = diagnosticSets.map(({ files }) => ({ + type: 'diagnostics' as const, + files, + isNew: true, + })) + + // Clear delivered diagnostics from registry to prevent memory leak + // Follows same pattern as removeDeliveredAsyncHooks + if (diagnosticSets.length > 0) { + clearAllLSPDiagnostics() + logForDebugging( + `LSP Diagnostics: Cleared ${diagnosticSets.length} delivered diagnostic(s) from registry`, + ) + } + + logForDebugging( + `LSP Diagnostics: Returning ${attachments.length} diagnostic attachment(s)`, + ) + + return attachments + } catch (error) { + const err = toError(error) + logError( + new Error(`Failed to get LSP diagnostic attachments: ${err.message}`), + ) + // Return empty array to allow other attachments to proceed + return [] + } +} + +export async function* getAttachmentMessages( + input: string | null, + toolUseContext: ToolUseContext, + ideSelection: IDESelection | null, + queuedCommands: QueuedCommand[], + messages?: Message[], + querySource?: QuerySource, + options?: { skipSkillDiscovery?: boolean }, +): AsyncGenerator { + // TODO: Compute this upstream + const attachments = await getAttachments( + input, + toolUseContext, + ideSelection, + queuedCommands, + messages, + querySource, + options, + ) + + if (attachments.length === 0) { + return + } + + logEvent('tengu_attachments', { + attachment_types: attachments.map( + _ => _.type, + ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }) + + for (const attachment of attachments) { + yield createAttachmentMessage(attachment) + } +} + +/** + * Generates a file attachment by reading a file with proper validation and truncation. + * This is the core file reading logic shared between @-mentioned files and post-compact restoration. + * + * @param filename The absolute path to the file to read + * @param toolUseContext The tool use context for calling FileReadTool + * @param options Optional configuration for file reading + * @returns A new_file attachment or null if the file couldn't be read + */ +/** + * Check if a PDF file should be represented as a lightweight reference + * instead of being inlined. Returns a PDFReferenceAttachment for large PDFs + * (more than PDF_AT_MENTION_INLINE_THRESHOLD pages), or null otherwise. + */ +export async function tryGetPDFReference( + filename: string, +): Promise { + const ext = parse(filename).ext.toLowerCase() + if (!isPDFExtension(ext)) { + return null + } + try { + const [stats, pageCount] = await Promise.all([ + getFsImplementation().stat(filename), + getPDFPageCount(filename), + ]) + // Use page count if available, otherwise fall back to size heuristic (~100KB per page) + const effectivePageCount = pageCount ?? Math.ceil(stats.size / (100 * 1024)) + if (effectivePageCount > PDF_AT_MENTION_INLINE_THRESHOLD) { + logEvent('tengu_pdf_reference_attachment', { + pageCount: effectivePageCount, + fileSize: stats.size, + hadPdfinfo: pageCount !== null, + } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS) + return { + type: 'pdf_reference', + filename, + pageCount: effectivePageCount, + fileSize: stats.size, + displayPath: relative(getCwd(), filename), + } + } + } catch { + // If we can't stat the file, return null to proceed with normal reading + } + return null +} + +export async function generateFileAttachment( + filename: string, + toolUseContext: ToolUseContext, + successEventName: string, + errorEventName: string, + mode: 'compact' | 'at-mention', + options?: { + offset?: number + limit?: number + }, +): Promise< + | FileAttachment + | CompactFileReferenceAttachment + | PDFReferenceAttachment + | AlreadyReadFileAttachment + | null +> { + const { offset, limit } = options ?? {} + + // Check if file has a deny rule configured + const appState = toolUseContext.getAppState() + if (isFileReadDenied(filename, appState.toolPermissionContext)) { + return null + } + + // Check file size before attempting to read (skip for PDFs — they have their own size/page handling below) + if ( + mode === 'at-mention' && + !isFileWithinReadSizeLimit( + filename, + getDefaultFileReadingLimits().maxSizeBytes, + ) + ) { + const ext = parse(filename).ext.toLowerCase() + if (!isPDFExtension(ext)) { + try { + const stats = await getFsImplementation().stat(filename) + logEvent('tengu_attachment_file_too_large', { + size_bytes: stats.size, + mode, + } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS) + return null + } catch { + // If we can't stat the file, proceed with normal reading (will fail later if file doesn't exist) + } + } + } + + // For large PDFs on @ mention, return a lightweight reference instead of inlining + if (mode === 'at-mention') { + const pdfRef = await tryGetPDFReference(filename) + if (pdfRef) { + return pdfRef + } + } + + // Check if file is already in context with latest version + const existingFileState = toolUseContext.readFileState.get(filename) + if (existingFileState && mode === 'at-mention') { + try { + // Check if the file has been modified since we last read it + const mtimeMs = await getFileModificationTimeAsync(filename) + + // Handle timestamp format inconsistency: + // - FileReadTool stores Date.now() (current time when read) + // - FileEdit/WriteTools store mtimeMs (file modification time) + // + // If timestamp > mtimeMs, it was stored by FileReadTool using Date.now() + // In this case, we should not use the optimization since we can't reliably + // compare modification times. Only use optimization when timestamp <= mtimeMs, + // indicating it was stored by FileEdit/WriteTool with actual mtimeMs. + + if ( + existingFileState.timestamp <= mtimeMs && + mtimeMs === existingFileState.timestamp + ) { + // File hasn't been modified, return already_read_file attachment + // This tells the system the file is already in context and doesn't need to be sent to API + logEvent(successEventName, {}) + return { + type: 'already_read_file', + filename, + displayPath: relative(getCwd(), filename), + content: { + type: 'text', + file: { + filePath: filename, + content: existingFileState.content, + numLines: countCharInString(existingFileState.content, '\n') + 1, + startLine: offset ?? 1, + totalLines: + countCharInString(existingFileState.content, '\n') + 1, + }, + }, + } + } + } catch { + // If we can't stat the file, proceed with normal reading + } + } + + try { + const fileInput = { + file_path: filename, + offset, + limit, + } + + async function readTruncatedFile(): Promise< + | FileAttachment + | CompactFileReferenceAttachment + | AlreadyReadFileAttachment + | null + > { + if (mode === 'compact') { + return { + type: 'compact_file_reference', + filename, + displayPath: relative(getCwd(), filename), + } + } + + // Check deny rules before reading truncated file + const appState = toolUseContext.getAppState() + if (isFileReadDenied(filename, appState.toolPermissionContext)) { + return null + } + + try { + // Read only the first MAX_LINES_TO_READ lines for files that are too large + const truncatedInput = { + file_path: filename, + offset: offset ?? 1, + limit: MAX_LINES_TO_READ, + } + const result = await FileReadTool.call(truncatedInput, toolUseContext) + logEvent(successEventName, {}) + + return { + type: 'file' as const, + filename, + content: result.data, + truncated: true, + displayPath: relative(getCwd(), filename), + } + } catch { + logEvent(errorEventName, {}) + return null + } + } + + // Validate file path is valid + const isValid = await FileReadTool.validateInput(fileInput, toolUseContext) + if (!isValid.result) { + return null + } + + try { + const result = await FileReadTool.call(fileInput, toolUseContext) + logEvent(successEventName, {}) + return { + type: 'file', + filename, + content: result.data, + displayPath: relative(getCwd(), filename), + } + } catch (error) { + if ( + error instanceof MaxFileReadTokenExceededError || + error instanceof FileTooLargeError + ) { + return await readTruncatedFile() + } + throw error + } + } catch { + logEvent(errorEventName, {}) + return null + } +} + +export function createAttachmentMessage( + attachment: Attachment, +): AttachmentMessage { + return { + attachment, + type: 'attachment', + uuid: randomUUID(), + timestamp: new Date().toISOString(), + } +} + +function getTodoReminderTurnCounts(messages: Message[]): { + turnsSinceLastTodoWrite: number + turnsSinceLastReminder: number +} { + let lastTodoWriteIndex = -1 + let lastReminderIndex = -1 + let assistantTurnsSinceWrite = 0 + let assistantTurnsSinceReminder = 0 + + // Iterate backwards to find most recent events + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + + if (message?.type === 'assistant') { + if (isThinkingMessage(message)) { + // Skip thinking messages + continue + } + + // Check for TodoWrite usage BEFORE incrementing counter + // (we don't want to count the TodoWrite message itself as "1 turn since write") + if ( + lastTodoWriteIndex === -1 && + 'message' in message && + Array.isArray(message.message?.content) && + message.message.content.some( + block => block.type === 'tool_use' && block.name === 'TodoWrite', + ) + ) { + lastTodoWriteIndex = i + } + + // Count assistant turns before finding events + if (lastTodoWriteIndex === -1) assistantTurnsSinceWrite++ + if (lastReminderIndex === -1) assistantTurnsSinceReminder++ + } else if ( + lastReminderIndex === -1 && + message?.type === 'attachment' && + message.attachment.type === 'todo_reminder' + ) { + lastReminderIndex = i + } + + if (lastTodoWriteIndex !== -1 && lastReminderIndex !== -1) { + break + } + } + + return { + turnsSinceLastTodoWrite: assistantTurnsSinceWrite, + turnsSinceLastReminder: assistantTurnsSinceReminder, + } +} + +async function getTodoReminderAttachments( + messages: Message[] | undefined, + toolUseContext: ToolUseContext, +): Promise { + // Skip if TodoWrite tool is not available + if ( + !toolUseContext.options.tools.some(t => + toolMatchesName(t, TODO_WRITE_TOOL_NAME), + ) + ) { + return [] + } + + // When SendUserMessage is in the toolkit, it's the primary communication + // channel and the model is always told to use it (#20467). TodoWrite + // becomes a side channel — nudging the model about it conflicts with the + // brief workflow. The tool itself stays available; this only gates the + // "you haven't used it in a while" nag. + if ( + BRIEF_TOOL_NAME && + toolUseContext.options.tools.some(t => toolMatchesName(t, BRIEF_TOOL_NAME)) + ) { + return [] + } + + // Skip if no messages provided + if (!messages || messages.length === 0) { + return [] + } + + const { turnsSinceLastTodoWrite, turnsSinceLastReminder } = + getTodoReminderTurnCounts(messages) + + // Check if we should show a reminder + if ( + turnsSinceLastTodoWrite >= TODO_REMINDER_CONFIG.TURNS_SINCE_WRITE && + turnsSinceLastReminder >= TODO_REMINDER_CONFIG.TURNS_BETWEEN_REMINDERS + ) { + const todoKey = toolUseContext.agentId ?? getSessionId() + const appState = toolUseContext.getAppState() + const todos = appState.todos[todoKey] ?? [] + return [ + { + type: 'todo_reminder', + content: todos, + itemCount: todos.length, + }, + ] + } + + return [] +} + +function getTaskReminderTurnCounts(messages: Message[]): { + turnsSinceLastTaskManagement: number + turnsSinceLastReminder: number +} { + let lastTaskManagementIndex = -1 + let lastReminderIndex = -1 + let assistantTurnsSinceTaskManagement = 0 + let assistantTurnsSinceReminder = 0 + + // Iterate backwards to find most recent events + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + + if (message?.type === 'assistant') { + if (isThinkingMessage(message)) { + // Skip thinking messages + continue + } + + // Check for TaskCreate or TaskUpdate usage BEFORE incrementing counter + if ( + lastTaskManagementIndex === -1 && + 'message' in message && + Array.isArray(message.message?.content) && + message.message.content.some( + block => + block.type === 'tool_use' && + (block.name === TASK_CREATE_TOOL_NAME || + block.name === TASK_UPDATE_TOOL_NAME), + ) + ) { + lastTaskManagementIndex = i + } + + // Count assistant turns before finding events + if (lastTaskManagementIndex === -1) assistantTurnsSinceTaskManagement++ + if (lastReminderIndex === -1) assistantTurnsSinceReminder++ + } else if ( + lastReminderIndex === -1 && + message?.type === 'attachment' && + message.attachment.type === 'task_reminder' + ) { + lastReminderIndex = i + } + + if (lastTaskManagementIndex !== -1 && lastReminderIndex !== -1) { + break + } + } + + return { + turnsSinceLastTaskManagement: assistantTurnsSinceTaskManagement, + turnsSinceLastReminder: assistantTurnsSinceReminder, + } +} + +async function getTaskReminderAttachments( + messages: Message[] | undefined, + toolUseContext: ToolUseContext, +): Promise { + if (!isTodoV2Enabled()) { + return [] + } + + // Skip for ant users + if (process.env.USER_TYPE === 'ant') { + return [] + } + + // When SendUserMessage is in the toolkit, it's the primary communication + // channel and the model is always told to use it (#20467). TaskUpdate + // becomes a side channel — nudging the model about it conflicts with the + // brief workflow. The tool itself stays available; this only gates the nag. + if ( + BRIEF_TOOL_NAME && + toolUseContext.options.tools.some(t => toolMatchesName(t, BRIEF_TOOL_NAME)) + ) { + return [] + } + + // Skip if TaskUpdate tool is not available + if ( + !toolUseContext.options.tools.some(t => + toolMatchesName(t, TASK_UPDATE_TOOL_NAME), + ) + ) { + return [] + } + + // Skip if no messages provided + if (!messages || messages.length === 0) { + return [] + } + + const { turnsSinceLastTaskManagement, turnsSinceLastReminder } = + getTaskReminderTurnCounts(messages) + + // Check if we should show a reminder + if ( + turnsSinceLastTaskManagement >= TODO_REMINDER_CONFIG.TURNS_SINCE_WRITE && + turnsSinceLastReminder >= TODO_REMINDER_CONFIG.TURNS_BETWEEN_REMINDERS + ) { + const tasks = await listTasks(getTaskListId()) + return [ + { + type: 'task_reminder', + content: tasks, + itemCount: tasks.length, + }, + ] + } + + return [] +} + +/** + * Get attachments for all unified tasks using the Task framework. + * Replaces the old getBackgroundShellAttachments, getBackgroundRemoteSessionAttachments, + * and getAsyncAgentAttachments functions. + */ +async function getUnifiedTaskAttachments( + toolUseContext: ToolUseContext, +): Promise { + const appState = toolUseContext.getAppState() + const { attachments, updatedTaskOffsets, evictedTaskIds } = + await generateTaskAttachments(appState) + + applyTaskOffsetsAndEvictions( + toolUseContext.setAppState, + updatedTaskOffsets, + evictedTaskIds, + ) + + // Convert TaskAttachment to Attachment format + return attachments.map(taskAttachment => ({ + type: 'task_status' as const, + taskId: taskAttachment.taskId, + taskType: taskAttachment.taskType, + status: taskAttachment.status, + description: taskAttachment.description, + deltaSummary: taskAttachment.deltaSummary, + outputFilePath: getTaskOutputPath(taskAttachment.taskId), + })) +} + +async function getAsyncHookResponseAttachments(): Promise { + const responses = await checkForAsyncHookResponses() + + if (responses.length === 0) { + return [] + } + + logForDebugging( + `Hooks: getAsyncHookResponseAttachments found ${responses.length} responses`, + ) + + const attachments = responses.map( + ({ + processId, + response, + hookName, + hookEvent, + toolName, + pluginId, + stdout, + stderr, + exitCode, + }) => { + logForDebugging( + `Hooks: Creating attachment for ${processId} (${hookName}): ${jsonStringify(response)}`, + ) + return { + type: 'async_hook_response' as const, + processId, + hookName, + hookEvent, + toolName, + response, + stdout, + stderr, + exitCode, + } + }, + ) + + // Remove delivered hooks from registry to prevent re-processing + if (responses.length > 0) { + const processIds = responses.map(r => r.processId) + removeDeliveredAsyncHooks(processIds) + logForDebugging( + `Hooks: Removed ${processIds.length} delivered hooks from registry`, + ) + } + + logForDebugging( + `Hooks: getAsyncHookResponseAttachments found ${attachments.length} attachments`, + ) + + return attachments +} + +/** + * Get teammate mailbox attachments for agent swarm communication + * Teammates are independent Claude Code sessions running in parallel (swarms), + * not parent-child subagent relationships. + * + * This function checks two sources for messages: + * 1. File-based mailbox (for messages that arrived between polls) + * 2. AppState.inbox (for messages queued mid-turn by useInboxPoller) + * + * Messages from AppState.inbox are delivered mid-turn as attachments, + * allowing teammates to receive messages without waiting for the turn to end. + */ +async function getTeammateMailboxAttachments( + toolUseContext: ToolUseContext, +): Promise { + if (!isAgentSwarmsEnabled()) { + return [] + } + if (process.env.USER_TYPE !== 'ant') { + return [] + } + + // Get AppState early to check for team lead status + const appState = toolUseContext.getAppState() + + // Use agent name from helper (checks AsyncLocalStorage, then dynamicTeamContext) + const envAgentName = getAgentName() + + // Get team name (checks AsyncLocalStorage, dynamicTeamContext, then AppState) + const teamName = getTeamName(appState.teamContext) + + // Check if we're the team lead (uses shared logic from swarm utils) + const teamLeadStatus = isTeamLead(appState.teamContext) + + // Check if viewing a teammate's transcript (for in-process teammates) + const viewedTeammate = getViewedTeammateTask(appState) + + // Resolve agent name based on who we're VIEWING: + // - If viewing a teammate, use THEIR name (to read from their mailbox) + // - Otherwise use env var if set, or leader's name if we're the team lead + let agentName = viewedTeammate?.identity.agentName ?? envAgentName + if (!agentName && teamLeadStatus && appState.teamContext) { + const leadAgentId = appState.teamContext.leadAgentId + // Look up the lead's name from agents map (not the UUID) + agentName = appState.teamContext.teammates[leadAgentId]?.name || 'team-lead' + } + + logForDebugging( + `[SwarmMailbox] getTeammateMailboxAttachments called: envAgentName=${envAgentName}, isTeamLead=${teamLeadStatus}, resolved agentName=${agentName}, teamName=${teamName}`, + ) + + // Only check inbox if running as an agent in a swarm or team lead + if (!agentName) { + logForDebugging( + `[SwarmMailbox] Not checking inbox - not in a swarm or team lead`, + ) + return [] + } + + logForDebugging( + `[SwarmMailbox] Checking inbox for agent="${agentName}" team="${teamName || 'default'}"`, + ) + + // Check mailbox for unread messages (routes to in-process or file-based) + // Filter out structured protocol messages (permission requests/responses, shutdown + // messages, etc.) — these must be left unread for useInboxPoller to route to their + // proper handlers (workerPermissions queue, sandbox queue, etc.). Without filtering, + // attachment generation races with InboxPoller: whichever reads first marks all + // messages as read, and if attachments wins, protocol messages get bundled as raw + // LLM context text instead of being routed to their UI handlers. + const allUnreadMessages = await readUnreadMessages(agentName, teamName) + const unreadMessages = allUnreadMessages.filter( + m => !isStructuredProtocolMessage(m.text), + ) + logForDebugging( + `[MailboxBridge] Found ${allUnreadMessages.length} unread message(s) for "${agentName}" (${allUnreadMessages.length - unreadMessages.length} structured protocol messages filtered out)`, + ) + + // Also check AppState.inbox for pending messages (queued mid-turn by useInboxPoller) + // IMPORTANT: appState.inbox contains messages FROM teammates TO the leader. + // Only show these when viewing the leader's transcript (not a teammate's). + // When viewing a teammate, their messages come from the file-based mailbox above. + // In-process teammates share AppState with the leader — appState.inbox contains + // the LEADER's queued messages, not the teammate's. Skip it to prevent leakage + // (including self-echo from broadcasts). Teammates receive messages exclusively + // through their file-based mailbox + waitForNextPromptOrShutdown. + // Note: viewedTeammate was already computed above for agentName resolution + const pendingInboxMessages = + viewedTeammate || isInProcessTeammate() + ? [] // Viewing teammate or running as in-process teammate - don't show leader's inbox + : appState.inbox.messages.filter(m => m.status === 'pending') + logForDebugging( + `[SwarmMailbox] Found ${pendingInboxMessages.length} pending message(s) in AppState.inbox`, + ) + + // Combine both sources of messages WITH DEDUPLICATION + // The same message could exist in both file mailbox and AppState.inbox due to race conditions: + // 1. getTeammateMailboxAttachments reads file -> finds message M + // 2. InboxPoller reads same file -> queues M in AppState.inbox + // 3. getTeammateMailboxAttachments reads AppState -> finds M again + // We deduplicate using from+timestamp+text prefix as the key + const seen = new Set() + let allMessages: Array<{ + from: string + text: string + timestamp: string + color?: string + summary?: string + }> = [] + + for (const m of [...unreadMessages, ...pendingInboxMessages]) { + const key = `${m.from}|${m.timestamp}|${m.text.slice(0, 100)}` + if (!seen.has(key)) { + seen.add(key) + allMessages.push({ + from: m.from, + text: m.text, + timestamp: m.timestamp, + color: m.color, + summary: m.summary, + }) + } + } + + // Collapse multiple idle notifications per agent — keep only the latest. + // Single pass to parse, then filter without re-parsing. + const idleAgentByIndex = new Map() + const latestIdleByAgent = new Map() + for (let i = 0; i < allMessages.length; i++) { + const idle = isIdleNotification(allMessages[i]!.text) + if (idle) { + idleAgentByIndex.set(i, idle.from) + latestIdleByAgent.set(idle.from, i) + } + } + if (idleAgentByIndex.size > latestIdleByAgent.size) { + const beforeCount = allMessages.length + allMessages = allMessages.filter((_m, i) => { + const agent = idleAgentByIndex.get(i) + if (agent === undefined) return true + return latestIdleByAgent.get(agent) === i + }) + logForDebugging( + `[SwarmMailbox] Collapsed ${beforeCount - allMessages.length} duplicate idle notification(s)`, + ) + } + + if (allMessages.length === 0) { + logForDebugging(`[SwarmMailbox] No messages to deliver, returning empty`) + return [] + } + + logForDebugging( + `[SwarmMailbox] Returning ${allMessages.length} message(s) as attachment for "${agentName}" (${unreadMessages.length} from file, ${pendingInboxMessages.length} from AppState, after dedup)`, + ) + + // Build the attachment BEFORE marking messages as processed + // This prevents message loss if any operation below fails + const attachment: Attachment[] = [ + { + type: 'teammate_mailbox', + messages: allMessages, + }, + ] + + // Mark only non-structured mailbox messages as read after attachment is built. + // Structured protocol messages stay unread for useInboxPoller to handle. + if (unreadMessages.length > 0) { + await markMessagesAsReadByPredicate( + agentName, + m => !isStructuredProtocolMessage(m.text), + teamName, + ) + logForDebugging( + `[MailboxBridge] marked ${unreadMessages.length} non-structured message(s) as read for agent="${agentName}" team="${teamName || 'default'}"`, + ) + } + + // Process shutdown_approved messages - remove teammates from team file + // This mirrors what useInboxPoller does in interactive mode (lines 546-606) + // In -p mode, useInboxPoller doesn't run, so we must handle this here + if (teamLeadStatus && teamName) { + for (const m of allMessages) { + const shutdownApproval = isShutdownApproved(m.text) + if (shutdownApproval) { + const teammateToRemove = shutdownApproval.from + logForDebugging( + `[SwarmMailbox] Processing shutdown_approved from ${teammateToRemove}`, + ) + + // Find the teammate ID by name + const teammateId = appState.teamContext?.teammates + ? Object.entries(appState.teamContext.teammates).find( + ([, t]) => t.name === teammateToRemove, + )?.[0] + : undefined + + if (teammateId) { + // Remove from team file + removeTeammateFromTeamFile(teamName, { + agentId: teammateId, + name: teammateToRemove, + }) + logForDebugging( + `[SwarmMailbox] Removed ${teammateToRemove} from team file`, + ) + + // Unassign tasks owned by this teammate + await unassignTeammateTasks( + teamName, + teammateId, + teammateToRemove, + 'shutdown', + ) + + // Remove from teamContext in AppState + toolUseContext.setAppState(prev => { + if (!prev.teamContext?.teammates) return prev + if (!(teammateId in prev.teamContext.teammates)) return prev + const { [teammateId]: _, ...remainingTeammates } = + prev.teamContext.teammates + return { + ...prev, + teamContext: { + ...prev.teamContext, + teammates: remainingTeammates, + }, + } + }) + } + } + } + } + + // Mark AppState inbox messages as processed LAST, after attachment is built + // This ensures messages aren't lost if earlier operations fail + if (pendingInboxMessages.length > 0) { + const pendingIds = new Set(pendingInboxMessages.map(m => m.id)) + toolUseContext.setAppState(prev => ({ + ...prev, + inbox: { + messages: prev.inbox.messages.map(m => + pendingIds.has(m.id) ? { ...m, status: 'processed' as const } : m, + ), + }, + })) + } + + return attachment +} + +/** + * Get team context attachment for teammates in a swarm. + * Only injected on the first turn to provide team coordination instructions. + */ +function getTeamContextAttachment(messages: Message[]): Attachment[] { + const teamName = getTeamName() + const agentId = getAgentId() + const agentName = getAgentName() + + // Only inject for teammates (not team lead or non-team sessions) + if (!teamName || !agentId) { + return [] + } + + // Only inject on first turn - check if there are no assistant messages yet + const hasAssistantMessage = messages.some(m => m.type === 'assistant') + if (hasAssistantMessage) { + return [] + } + + const configDir = getClaudeConfigHomeDir() + const teamConfigPath = `${configDir}/teams/${teamName}/config.json` + const taskListPath = `${configDir}/tasks/${teamName}/` + + return [ + { + type: 'team_context', + agentId, + agentName: agentName || agentId, + teamName, + teamConfigPath, + taskListPath, + }, + ] +} + +function getTokenUsageAttachment( + messages: Message[], + model: string, +): Attachment[] { + if (!isEnvTruthy(process.env.CLAUDE_CODE_ENABLE_TOKEN_USAGE_ATTACHMENT)) { + return [] + } + + const contextWindow = getEffectiveContextWindowSize(model) + const usedTokens = tokenCountFromLastAPIResponse(messages) + + return [ + { + type: 'token_usage', + used: usedTokens, + total: contextWindow, + remaining: contextWindow - usedTokens, + }, + ] +} + +function getOutputTokenUsageAttachment(): Attachment[] { + if (feature('TOKEN_BUDGET')) { + const budget = getCurrentTurnTokenBudget() + if (budget === null || budget <= 0) { + return [] + } + return [ + { + type: 'output_token_usage', + turn: getTurnOutputTokens(), + session: getTotalOutputTokens(), + budget, + }, + ] + } + return [] +} + +function getMaxBudgetUsdAttachment(maxBudgetUsd?: number): Attachment[] { + if (maxBudgetUsd === undefined) { + return [] + } + + const usedCost = getTotalCostUSD() + const remainingBudget = maxBudgetUsd - usedCost + + return [ + { + type: 'budget_usd', + used: usedCost, + total: maxBudgetUsd, + remaining: remainingBudget, + }, + ] +} + +/** + * Count human turns since plan mode exit (plan_mode_exit attachment). + * Returns 0 if no plan_mode_exit attachment found. + * + * tool_result messages are type:'user' without isMeta, so filter by + * toolUseResult to avoid counting them — otherwise the 10-turn reminder + * interval fires every ~10 tool calls instead of ~10 human turns. + */ +export function getVerifyPlanReminderTurnCount(messages: Message[]): number { + let turnCount = 0 + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages[i] + if (message && isHumanTurn(message)) { + turnCount++ + } + // Stop counting at plan_mode_exit attachment (marks when implementation started) + if ( + message?.type === 'attachment' && + message.attachment.type === 'plan_mode_exit' + ) { + return turnCount + } + } + // No plan_mode_exit found + return 0 +} + +/** + * Get verify plan reminder attachment if the model hasn't called VerifyPlanExecution yet. + */ +async function getVerifyPlanReminderAttachment( + messages: Message[] | undefined, + toolUseContext: ToolUseContext, +): Promise { + if ( + process.env.USER_TYPE !== 'ant' || + !isEnvTruthy(process.env.CLAUDE_CODE_VERIFY_PLAN) + ) { + return [] + } + + const appState = toolUseContext.getAppState() + const pending = appState.pendingPlanVerification + + // Only remind if plan exists and verification not started or completed + if ( + !pending || + pending.verificationStarted || + pending.verificationCompleted + ) { + return [] + } + + // Only remind every N turns + if (messages && messages.length > 0) { + const turnCount = getVerifyPlanReminderTurnCount(messages) + if ( + turnCount === 0 || + turnCount % VERIFY_PLAN_REMINDER_CONFIG.TURNS_BETWEEN_REMINDERS !== 0 + ) { + return [] + } + } + + return [{ type: 'verify_plan_reminder' }] +} + +export function getCompactionReminderAttachment( + messages: Message[], + model: string, +): Attachment[] { + if (!getFeatureValue_CACHED_MAY_BE_STALE('tengu_marble_fox', false)) { + return [] + } + + if (!isAutoCompactEnabled()) { + return [] + } + + const contextWindow = getContextWindowForModel(model, getSdkBetas()) + if (contextWindow < 1_000_000) { + return [] + } + + const effectiveWindow = getEffectiveContextWindowSize(model) + const usedTokens = tokenCountWithEstimation(messages) + if (usedTokens < effectiveWindow * 0.25) { + return [] + } + + return [{ type: 'compaction_reminder' }] +} + +/** + * Context-efficiency nudge. Injected after every N tokens of growth without + * a snip. Pacing is handled entirely by shouldNudgeForSnips — the 10k + * interval resets on prior nudges, snip markers, snip boundaries, and + * compact boundaries. + */ +export function getContextEfficiencyAttachment( + messages: Message[], +): Attachment[] { + if (!feature('HISTORY_SNIP')) { + return [] + } + // Gate must match SnipTool.isEnabled() — don't nudge toward a tool that + // isn't in the tool list. Lazy require keeps this file snip-string-free. + const { isSnipRuntimeEnabled, shouldNudgeForSnips } = + // eslint-disable-next-line @typescript-eslint/no-require-imports + require('../services/compact/snipCompact.js') as typeof import('../services/compact/snipCompact.js') + if (!isSnipRuntimeEnabled()) { + return [] + } + + if (!shouldNudgeForSnips(messages)) { + return [] + } + + return [{ type: 'context_efficiency' }] +} + + +function isFileReadDenied( + filePath: string, + toolPermissionContext: ToolPermissionContext, +): boolean { + const denyRule = matchingRuleForInput( + filePath, + toolPermissionContext, + 'read', + 'deny', + ) + return denyRule !== null +} diff --git a/_all-in-one-upload/cc-haha/src/utils/codeIndexing.ts b/_all-in-one-upload/cc-haha/src/utils/codeIndexing.ts new file mode 100644 index 0000000..8bf076d --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/codeIndexing.ts @@ -0,0 +1,206 @@ +/** + * Utility functions for detecting code indexing tool usage. + * + * Tracks usage of common code indexing solutions like Sourcegraph, Cody, etc. + * both via CLI commands and MCP server integrations. + */ + +/** + * Known code indexing tool identifiers. + * These are the normalized names used in analytics events. + */ +export type CodeIndexingTool = + // Code search engines + | 'sourcegraph' + | 'hound' + | 'seagoat' + | 'bloop' + | 'gitloop' + // AI coding assistants with indexing + | 'cody' + | 'aider' + | 'continue' + | 'github-copilot' + | 'cursor' + | 'tabby' + | 'codeium' + | 'tabnine' + | 'augment' + | 'windsurf' + | 'aide' + | 'pieces' + | 'qodo' + | 'amazon-q' + | 'gemini' + // MCP code indexing servers + | 'claude-context' + | 'code-index-mcp' + | 'local-code-search' + | 'autodev-codebase' + // Context providers + | 'openctx' + +/** + * Mapping of CLI command prefixes to code indexing tools. + * The key is the command name (first word of the command). + */ +const CLI_COMMAND_MAPPING: Record = { + // Sourcegraph ecosystem + src: 'sourcegraph', + cody: 'cody', + // AI coding assistants + aider: 'aider', + tabby: 'tabby', + tabnine: 'tabnine', + augment: 'augment', + pieces: 'pieces', + qodo: 'qodo', + aide: 'aide', + // Code search tools + hound: 'hound', + seagoat: 'seagoat', + bloop: 'bloop', + gitloop: 'gitloop', + // Cloud provider AI assistants + q: 'amazon-q', + gemini: 'gemini', +} + +/** + * Mapping of MCP server name patterns to code indexing tools. + * Patterns are matched case-insensitively against the server name. + */ +const MCP_SERVER_PATTERNS: Array<{ + pattern: RegExp + tool: CodeIndexingTool +}> = [ + // Sourcegraph ecosystem + { pattern: /^sourcegraph$/i, tool: 'sourcegraph' }, + { pattern: /^cody$/i, tool: 'cody' }, + { pattern: /^openctx$/i, tool: 'openctx' }, + // AI coding assistants + { pattern: /^aider$/i, tool: 'aider' }, + { pattern: /^continue$/i, tool: 'continue' }, + { pattern: /^github[-_]?copilot$/i, tool: 'github-copilot' }, + { pattern: /^copilot$/i, tool: 'github-copilot' }, + { pattern: /^cursor$/i, tool: 'cursor' }, + { pattern: /^tabby$/i, tool: 'tabby' }, + { pattern: /^codeium$/i, tool: 'codeium' }, + { pattern: /^tabnine$/i, tool: 'tabnine' }, + { pattern: /^augment[-_]?code$/i, tool: 'augment' }, + { pattern: /^augment$/i, tool: 'augment' }, + { pattern: /^windsurf$/i, tool: 'windsurf' }, + { pattern: /^aide$/i, tool: 'aide' }, + { pattern: /^codestory$/i, tool: 'aide' }, + { pattern: /^pieces$/i, tool: 'pieces' }, + { pattern: /^qodo$/i, tool: 'qodo' }, + { pattern: /^amazon[-_]?q$/i, tool: 'amazon-q' }, + { pattern: /^gemini[-_]?code[-_]?assist$/i, tool: 'gemini' }, + { pattern: /^gemini$/i, tool: 'gemini' }, + // Code search tools + { pattern: /^hound$/i, tool: 'hound' }, + { pattern: /^seagoat$/i, tool: 'seagoat' }, + { pattern: /^bloop$/i, tool: 'bloop' }, + { pattern: /^gitloop$/i, tool: 'gitloop' }, + // MCP code indexing servers + { pattern: /^claude[-_]?context$/i, tool: 'claude-context' }, + { pattern: /^code[-_]?index[-_]?mcp$/i, tool: 'code-index-mcp' }, + { pattern: /^code[-_]?index$/i, tool: 'code-index-mcp' }, + { pattern: /^local[-_]?code[-_]?search$/i, tool: 'local-code-search' }, + { pattern: /^codebase$/i, tool: 'autodev-codebase' }, + { pattern: /^autodev[-_]?codebase$/i, tool: 'autodev-codebase' }, + { pattern: /^code[-_]?context$/i, tool: 'claude-context' }, +] + +/** + * Detects if a bash command is using a code indexing CLI tool. + * + * @param command - The full bash command string + * @returns The code indexing tool identifier, or undefined if not a code indexing command + * + * @example + * detectCodeIndexingFromCommand('src search "pattern"') // returns 'sourcegraph' + * detectCodeIndexingFromCommand('cody chat --message "help"') // returns 'cody' + * detectCodeIndexingFromCommand('ls -la') // returns undefined + */ +export function detectCodeIndexingFromCommand( + command: string, +): CodeIndexingTool | undefined { + // Extract the first word (command name) + const trimmed = command.trim() + const firstWord = trimmed.split(/\s+/)[0]?.toLowerCase() + + if (!firstWord) { + return undefined + } + + // Check for npx/bunx prefixed commands + if (firstWord === 'npx' || firstWord === 'bunx') { + const secondWord = trimmed.split(/\s+/)[1]?.toLowerCase() + if (secondWord && secondWord in CLI_COMMAND_MAPPING) { + return CLI_COMMAND_MAPPING[secondWord] + } + } + + return CLI_COMMAND_MAPPING[firstWord] +} + +/** + * Detects if an MCP tool is from a code indexing server. + * + * @param toolName - The MCP tool name (format: mcp__serverName__toolName) + * @returns The code indexing tool identifier, or undefined if not a code indexing tool + * + * @example + * detectCodeIndexingFromMcpTool('mcp__sourcegraph__search') // returns 'sourcegraph' + * detectCodeIndexingFromMcpTool('mcp__cody__chat') // returns 'cody' + * detectCodeIndexingFromMcpTool('mcp__filesystem__read') // returns undefined + */ +export function detectCodeIndexingFromMcpTool( + toolName: string, +): CodeIndexingTool | undefined { + // MCP tool names follow the format: mcp__serverName__toolName + if (!toolName.startsWith('mcp__')) { + return undefined + } + + const parts = toolName.split('__') + if (parts.length < 3) { + return undefined + } + + const serverName = parts[1] + if (!serverName) { + return undefined + } + + for (const { pattern, tool } of MCP_SERVER_PATTERNS) { + if (pattern.test(serverName)) { + return tool + } + } + + return undefined +} + +/** + * Detects if an MCP server name corresponds to a code indexing tool. + * + * @param serverName - The MCP server name + * @returns The code indexing tool identifier, or undefined if not a code indexing server + * + * @example + * detectCodeIndexingFromMcpServerName('sourcegraph') // returns 'sourcegraph' + * detectCodeIndexingFromMcpServerName('filesystem') // returns undefined + */ +export function detectCodeIndexingFromMcpServerName( + serverName: string, +): CodeIndexingTool | undefined { + for (const { pattern, tool } of MCP_SERVER_PATTERNS) { + if (pattern.test(serverName)) { + return tool + } + } + + return undefined +} diff --git a/_all-in-one-upload/cc-haha/src/utils/configConstants.ts b/_all-in-one-upload/cc-haha/src/utils/configConstants.ts new file mode 100644 index 0000000..3d1e6af --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/configConstants.ts @@ -0,0 +1,21 @@ +// These constants are in a separate file to avoid circular dependency issues. +// Do NOT add imports to this file - it must remain dependency-free. + +export const NOTIFICATION_CHANNELS = [ + 'auto', + 'iterm2', + 'iterm2_with_bell', + 'terminal_bell', + 'kitty', + 'ghostty', + 'notifications_disabled', +] as const + +// Valid editor modes (excludes deprecated 'emacs' which is auto-migrated to 'normal') +export const EDITOR_MODES = ['normal', 'vim'] as const + +// Valid teammate modes for spawning +// 'tmux' = traditional tmux-based teammates +// 'in-process' = in-process teammates running in same process +// 'auto' = automatically choose based on context (default) +export const TEAMMATE_MODES = ['auto', 'tmux', 'in-process'] as const diff --git a/_all-in-one-upload/cc-haha/src/utils/context.ts b/_all-in-one-upload/cc-haha/src/utils/context.ts new file mode 100644 index 0000000..d9714de --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/context.ts @@ -0,0 +1,221 @@ +// biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered +import { CONTEXT_1M_BETA_HEADER } from '../constants/betas.js' +import { getGlobalConfig } from './config.js' +import { isEnvTruthy } from './envUtils.js' +import { getCanonicalName } from './model/model.js' +import { getModelCapability } from './model/modelCapabilities.js' + +// Model context window size (200k tokens for all models right now) +export const MODEL_CONTEXT_WINDOW_DEFAULT = 200_000 + +// Maximum output tokens for compact operations +export const COMPACT_MAX_OUTPUT_TOKENS = 20_000 + +// Default max output tokens +const MAX_OUTPUT_TOKENS_DEFAULT = 32_000 +const MAX_OUTPUT_TOKENS_UPPER_LIMIT = 64_000 + +// Capped default for slot-reservation optimization. BQ p99 output = 4,911 +// tokens, so 32k/64k defaults over-reserve 8-16× slot capacity. With the cap +// enabled, <1% of requests hit the limit; those get one clean retry at 64k +// (see query.ts max_output_tokens_escalate). Cap is applied in +// claude.ts:getMaxOutputTokensForModel to avoid the growthbook→betas→context +// import cycle. +export const CAPPED_DEFAULT_MAX_TOKENS = 8_000 +export const ESCALATED_MAX_TOKENS = 64_000 + +/** + * Check if 1M context is disabled via environment variable. + * Used by C4E admins to disable 1M context for HIPAA compliance. + */ +export function is1mContextDisabled(): boolean { + return isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_1M_CONTEXT) +} + +export function has1mContext(model: string): boolean { + if (is1mContextDisabled()) { + return false + } + return /\[1m\]/i.test(model) +} + +// @[MODEL LAUNCH]: Update this pattern if the new model supports 1M context +export function modelSupports1M(model: string): boolean { + if (is1mContextDisabled()) { + return false + } + const canonical = getCanonicalName(model) + return canonical.includes('claude-sonnet-4') || canonical.includes('opus-4-6') +} + +export function getContextWindowForModel( + model: string, + betas?: string[], +): number { + // Allow override via environment variable (ant-only) + // This takes precedence over all other context window resolution, including 1M detection, + // so users can cap the effective context window for local decisions (auto-compact, etc.) + // while still using a 1M-capable endpoint. + if ( + process.env.USER_TYPE === 'ant' && + process.env.CLAUDE_CODE_MAX_CONTEXT_TOKENS + ) { + const override = parseInt(process.env.CLAUDE_CODE_MAX_CONTEXT_TOKENS, 10) + if (!isNaN(override) && override > 0) { + return override + } + } + + // [1m] suffix — explicit client-side opt-in, respected over all detection + if (has1mContext(model)) { + return 1_000_000 + } + + const cap = getModelCapability(model) + if (cap?.max_input_tokens && cap.max_input_tokens >= 100_000) { + if ( + cap.max_input_tokens > MODEL_CONTEXT_WINDOW_DEFAULT && + is1mContextDisabled() + ) { + return MODEL_CONTEXT_WINDOW_DEFAULT + } + return cap.max_input_tokens + } + + if (betas?.includes(CONTEXT_1M_BETA_HEADER) && modelSupports1M(model)) { + return 1_000_000 + } + if (getSonnet1mExpTreatmentEnabled(model)) { + return 1_000_000 + } + if (process.env.USER_TYPE === 'ant') { + const antModel = resolveAntModel(model) + if (antModel?.contextWindow) { + return antModel.contextWindow + } + } + return MODEL_CONTEXT_WINDOW_DEFAULT +} + +export function getSonnet1mExpTreatmentEnabled(model: string): boolean { + if (is1mContextDisabled()) { + return false + } + // Only applies to sonnet 4.6 without an explicit [1m] suffix + if (has1mContext(model)) { + return false + } + if (!getCanonicalName(model).includes('sonnet-4-6')) { + return false + } + return getGlobalConfig().clientDataCache?.['coral_reef_sonnet'] === 'true' +} + +/** + * Calculate context window usage percentage from token usage data. + * Returns used and remaining percentages, or null values if no usage data. + */ +export function calculateContextPercentages( + currentUsage: { + input_tokens: number + cache_creation_input_tokens: number + cache_read_input_tokens: number + } | null, + contextWindowSize: number, +): { used: number | null; remaining: number | null } { + if (!currentUsage) { + return { used: null, remaining: null } + } + + const totalInputTokens = + currentUsage.input_tokens + + currentUsage.cache_creation_input_tokens + + currentUsage.cache_read_input_tokens + + const usedPercentage = Math.round( + (totalInputTokens / contextWindowSize) * 100, + ) + const clampedUsed = Math.min(100, Math.max(0, usedPercentage)) + + return { + used: clampedUsed, + remaining: 100 - clampedUsed, + } +} + +/** + * Returns the model's default and upper limit for max output tokens. + */ +export function getModelMaxOutputTokens(model: string): { + default: number + upperLimit: number +} { + let defaultTokens: number + let upperLimit: number + + if (process.env.USER_TYPE === 'ant') { + const antModel = resolveAntModel(model.toLowerCase()) + if (antModel) { + defaultTokens = antModel.defaultMaxTokens ?? MAX_OUTPUT_TOKENS_DEFAULT + upperLimit = antModel.upperMaxTokensLimit ?? MAX_OUTPUT_TOKENS_UPPER_LIMIT + return { default: defaultTokens, upperLimit } + } + } + + const m = getCanonicalName(model) + + if (m.includes('opus-4-6')) { + defaultTokens = 64_000 + upperLimit = 128_000 + } else if (m.includes('sonnet-4-6')) { + defaultTokens = 32_000 + upperLimit = 128_000 + } else if ( + m.includes('opus-4-5') || + m.includes('sonnet-4') || + m.includes('haiku-4') + ) { + defaultTokens = 32_000 + upperLimit = 64_000 + } else if (m.includes('opus-4-1') || m.includes('opus-4')) { + defaultTokens = 32_000 + upperLimit = 32_000 + } else if (m.includes('claude-3-opus')) { + defaultTokens = 4_096 + upperLimit = 4_096 + } else if (m.includes('claude-3-sonnet')) { + defaultTokens = 8_192 + upperLimit = 8_192 + } else if (m.includes('claude-3-haiku')) { + defaultTokens = 4_096 + upperLimit = 4_096 + } else if (m.includes('3-5-sonnet') || m.includes('3-5-haiku')) { + defaultTokens = 8_192 + upperLimit = 8_192 + } else if (m.includes('3-7-sonnet')) { + defaultTokens = 32_000 + upperLimit = 64_000 + } else { + defaultTokens = MAX_OUTPUT_TOKENS_DEFAULT + upperLimit = MAX_OUTPUT_TOKENS_UPPER_LIMIT + } + + const cap = getModelCapability(model) + if (cap?.max_tokens && cap.max_tokens >= 4_096) { + upperLimit = cap.max_tokens + defaultTokens = Math.min(defaultTokens, upperLimit) + } + + return { default: defaultTokens, upperLimit } +} + +/** + * Returns the max thinking budget tokens for a given model. The max + * thinking tokens should be strictly less than the max output tokens. + * + * Deprecated since newer models use adaptive thinking rather than a + * strict thinking token budget. + */ +export function getMaxThinkingTokensForModel(model: string): number { + return getModelMaxOutputTokens(model).upperLimit - 1 +} diff --git a/_all-in-one-upload/cc-haha/src/utils/contextAnalysis.ts b/_all-in-one-upload/cc-haha/src/utils/contextAnalysis.ts new file mode 100644 index 0000000..2801d37 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/contextAnalysis.ts @@ -0,0 +1,272 @@ +import type { BetaContentBlock } from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs' +import type { + ContentBlock, + ContentBlockParam, +} from '@anthropic-ai/sdk/resources/index.mjs' +import { roughTokenCountEstimation as countTokens } from '../services/tokenEstimation.js' +import type { + AssistantMessage, + Message, + UserMessage, +} from '../types/message.js' +import { normalizeMessagesForAPI } from './messages.js' +import { jsonStringify } from './slowOperations.js' + +type TokenStats = { + toolRequests: Map + toolResults: Map + humanMessages: number + assistantMessages: number + localCommandOutputs: number + other: number + attachments: Map + duplicateFileReads: Map + total: number +} + +export function analyzeContext(messages: Message[]): TokenStats { + const stats: TokenStats = { + toolRequests: new Map(), + toolResults: new Map(), + humanMessages: 0, + assistantMessages: 0, + localCommandOutputs: 0, + other: 0, + attachments: new Map(), + duplicateFileReads: new Map(), + total: 0, + } + + const toolIdsToToolNames = new Map() + const readToolIdToFilePath = new Map() + const fileReadStats = new Map< + string, + { count: number; totalTokens: number } + >() + + messages.forEach(msg => { + if (msg.type === 'attachment') { + const type = msg.attachment.type || 'unknown' + stats.attachments.set(type, (stats.attachments.get(type) || 0) + 1) + } + }) + + const normalizedMessages = normalizeMessagesForAPI(messages) + normalizedMessages.forEach(msg => { + const { content } = msg.message + + // Not sure if this path is still used, but adding as a fallback + if (typeof content === 'string') { + const tokens = countTokens(content) + stats.total += tokens + // Check if this is a local command output + if (msg.type === 'user' && content.includes('local-command-stdout')) { + stats.localCommandOutputs += tokens + } else { + stats[msg.type === 'user' ? 'humanMessages' : 'assistantMessages'] += + tokens + } + } else { + content.forEach(block => + processBlock( + block, + msg, + stats, + toolIdsToToolNames, + readToolIdToFilePath, + fileReadStats, + ), + ) + } + }) + + // Calculate duplicate file reads + fileReadStats.forEach((data, path) => { + if (data.count > 1) { + const averageTokensPerRead = Math.floor(data.totalTokens / data.count) + const duplicateTokens = averageTokensPerRead * (data.count - 1) + + stats.duplicateFileReads.set(path, { + count: data.count, + tokens: duplicateTokens, + }) + } + }) + + return stats +} + +function processBlock( + block: ContentBlockParam | ContentBlock | BetaContentBlock, + message: UserMessage | AssistantMessage, + stats: TokenStats, + toolIds: Map, + readToolPaths: Map, + fileReads: Map, +): void { + const tokens = countTokens(jsonStringify(block)) + stats.total += tokens + + switch (block.type) { + case 'text': + // Check if this is a local command output + if ( + message.type === 'user' && + 'text' in block && + block.text.includes('local-command-stdout') + ) { + stats.localCommandOutputs += tokens + } else { + stats[ + message.type === 'user' ? 'humanMessages' : 'assistantMessages' + ] += tokens + } + break + + case 'tool_use': { + if ('name' in block && 'id' in block) { + const toolName = block.name || 'unknown' + increment(stats.toolRequests, toolName, tokens) + toolIds.set(block.id, toolName) + + // Track Read tool file paths + if ( + toolName === 'Read' && + 'input' in block && + block.input && + typeof block.input === 'object' && + 'file_path' in block.input + ) { + const path = String( + (block.input as Record).file_path, + ) + readToolPaths.set(block.id, path) + } + } + break + } + + case 'tool_result': { + if ('tool_use_id' in block) { + const toolName = toolIds.get(block.tool_use_id) || 'unknown' + increment(stats.toolResults, toolName, tokens) + + // Track file read tokens + if (toolName === 'Read') { + const path = readToolPaths.get(block.tool_use_id) + if (path) { + const current = fileReads.get(path) || { count: 0, totalTokens: 0 } + fileReads.set(path, { + count: current.count + 1, + totalTokens: current.totalTokens + tokens, + }) + } + } + } + break + } + + case 'image': + case 'server_tool_use': + case 'web_search_tool_result': + case 'search_result': + case 'document': + case 'thinking': + case 'redacted_thinking': + case 'code_execution_tool_result': + case 'mcp_tool_use': + case 'mcp_tool_result': + case 'container_upload': + case 'web_fetch_tool_result': + case 'bash_code_execution_tool_result': + case 'text_editor_code_execution_tool_result': + case 'tool_search_tool_result': + case 'compaction': + // Don't care about these for now.. + stats['other'] += tokens + break + } +} + +function increment(map: Map, key: string, value: number): void { + map.set(key, (map.get(key) || 0) + value) +} + +export function tokenStatsToStatsigMetrics( + stats: TokenStats, +): Record { + const metrics: Record = { + total_tokens: stats.total, + human_message_tokens: stats.humanMessages, + assistant_message_tokens: stats.assistantMessages, + local_command_output_tokens: stats.localCommandOutputs, + other_tokens: stats.other, + } + + stats.attachments.forEach((count, type) => { + metrics[`attachment_${type}_count`] = count + }) + + stats.toolRequests.forEach((tokens, tool) => { + metrics[`tool_request_${tool}_tokens`] = tokens + }) + + stats.toolResults.forEach((tokens, tool) => { + metrics[`tool_result_${tool}_tokens`] = tokens + }) + + const duplicateTotal = [...stats.duplicateFileReads.values()].reduce( + (sum, d) => sum + d.tokens, + 0, + ) + + metrics.duplicate_read_tokens = duplicateTotal + metrics.duplicate_read_file_count = stats.duplicateFileReads.size + + if (stats.total > 0) { + metrics.human_message_percent = Math.round( + (stats.humanMessages / stats.total) * 100, + ) + metrics.assistant_message_percent = Math.round( + (stats.assistantMessages / stats.total) * 100, + ) + metrics.local_command_output_percent = Math.round( + (stats.localCommandOutputs / stats.total) * 100, + ) + metrics.duplicate_read_percent = Math.round( + (duplicateTotal / stats.total) * 100, + ) + + const toolRequestTotal = [...stats.toolRequests.values()].reduce( + (sum, v) => sum + v, + 0, + ) + const toolResultTotal = [...stats.toolResults.values()].reduce( + (sum, v) => sum + v, + 0, + ) + + metrics.tool_request_percent = Math.round( + (toolRequestTotal / stats.total) * 100, + ) + metrics.tool_result_percent = Math.round( + (toolResultTotal / stats.total) * 100, + ) + + // Add individual tool request percentages + stats.toolRequests.forEach((tokens, tool) => { + metrics[`tool_request_${tool}_percent`] = Math.round( + (tokens / stats.total) * 100, + ) + }) + + // Add individual tool result percentages + stats.toolResults.forEach((tokens, tool) => { + metrics[`tool_result_${tool}_percent`] = Math.round( + (tokens / stats.total) * 100, + ) + }) + } + + return metrics +} diff --git a/_all-in-one-upload/cc-haha/src/utils/conversationRecovery.ts b/_all-in-one-upload/cc-haha/src/utils/conversationRecovery.ts new file mode 100644 index 0000000..af5ea23 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/conversationRecovery.ts @@ -0,0 +1,597 @@ +import { feature } from 'bun:bundle' +import type { UUID } from 'crypto' +import { relative } from 'path' +import { getCwd } from 'src/utils/cwd.js' +import { addInvokedSkill } from '../bootstrap/state.js' +import { asSessionId } from '../types/ids.js' +import type { + AttributionSnapshotMessage, + ContextCollapseCommitEntry, + ContextCollapseSnapshotEntry, + LogOption, + PersistedWorktreeSession, + SerializedMessage, +} from '../types/logs.js' +import type { + Message, + NormalizedMessage, + NormalizedUserMessage, +} from '../types/message.js' +import { PERMISSION_MODES } from '../types/permissions.js' +import { suppressNextSkillListing } from './attachments.js' +import { + copyFileHistoryForResume, + type FileHistorySnapshot, +} from './fileHistory.js' +import { logError } from './log.js' +import { + createAssistantMessage, + createUserMessage, + filterOrphanedThinkingOnlyMessages, + filterUnresolvedToolUses, + filterWhitespaceOnlyAssistantMessages, + isToolUseResultMessage, + NO_RESPONSE_REQUESTED, + normalizeMessages, +} from './messages.js' +import { copyPlanForResume } from './plans.js' +import { processSessionStartHooks } from './sessionStart.js' +import { + buildConversationChain, + checkResumeConsistency, + getLastSessionLog, + getSessionIdFromLog, + isLiteLog, + loadFullLog, + loadMessageLogs, + loadTranscriptFile, + removeExtraFields, +} from './sessionStorage.js' +import type { ContentReplacementRecord } from './toolResultStorage.js' + +// Dead code elimination: ant-only tool names are conditionally required so +// their strings don't leak into external builds. Static imports always bundle. +/* eslint-disable @typescript-eslint/no-require-imports */ +const BRIEF_TOOL_NAME: string | null = + feature('KAIROS') || feature('KAIROS_BRIEF') + ? ( + require('../tools/BriefTool/prompt.js') as typeof import('../tools/BriefTool/prompt.js') + ).BRIEF_TOOL_NAME + : null +const LEGACY_BRIEF_TOOL_NAME: string | null = + feature('KAIROS') || feature('KAIROS_BRIEF') + ? ( + require('../tools/BriefTool/prompt.js') as typeof import('../tools/BriefTool/prompt.js') + ).LEGACY_BRIEF_TOOL_NAME + : null +const SEND_USER_FILE_TOOL_NAME: string | null = feature('KAIROS') + ? ( + require('../tools/SendUserFileTool/prompt.js') as typeof import('../tools/SendUserFileTool/prompt.js') + ).SEND_USER_FILE_TOOL_NAME + : null +/* eslint-enable @typescript-eslint/no-require-imports */ + +/** + * Transforms legacy attachment types to current types for backward compatibility + */ +function migrateLegacyAttachmentTypes(message: Message): Message { + if (message.type !== 'attachment') { + return message + } + + const attachment = message.attachment as { + type: string + [key: string]: unknown + } // Handle legacy types not in current type system + + // Transform legacy attachment types + if (attachment.type === 'new_file') { + return { + ...message, + attachment: { + ...attachment, + type: 'file', + displayPath: relative(getCwd(), attachment.filename as string), + }, + } as SerializedMessage // Cast entire message since we know the structure is correct + } + + if (attachment.type === 'new_directory') { + return { + ...message, + attachment: { + ...attachment, + type: 'directory', + displayPath: relative(getCwd(), attachment.path as string), + }, + } as SerializedMessage // Cast entire message since we know the structure is correct + } + + // Backfill displayPath for attachments from old sessions + if (!('displayPath' in attachment)) { + const path = + 'filename' in attachment + ? (attachment.filename as string) + : 'path' in attachment + ? (attachment.path as string) + : 'skillDir' in attachment + ? (attachment.skillDir as string) + : undefined + if (path) { + return { + ...message, + attachment: { + ...attachment, + displayPath: relative(getCwd(), path), + }, + } as Message + } + } + + return message +} + +export type TeleportRemoteResponse = { + log: Message[] + branch?: string +} + +export type TurnInterruptionState = + | { kind: 'none' } + | { kind: 'interrupted_prompt'; message: NormalizedUserMessage } + +export type DeserializeResult = { + messages: Message[] + turnInterruptionState: TurnInterruptionState +} + +/** + * Deserializes messages from a log file into the format expected by the REPL. + * Filters unresolved tool uses, orphaned thinking messages, and appends a + * synthetic assistant sentinel when the last message is from the user. + * @internal Exported for testing - use loadConversationForResume instead + */ +export function deserializeMessages(serializedMessages: Message[]): Message[] { + return deserializeMessagesWithInterruptDetection(serializedMessages).messages +} + +/** + * Like deserializeMessages, but also detects whether the session was + * interrupted mid-turn. Used by the SDK resume path to auto-continue + * interrupted turns after a gateway-triggered restart. + * @internal Exported for testing + */ +export function deserializeMessagesWithInterruptDetection( + serializedMessages: Message[], +): DeserializeResult { + try { + // Transform legacy attachment types before processing + const migratedMessages = serializedMessages.map( + migrateLegacyAttachmentTypes, + ) + + // Strip invalid permissionMode values from deserialized user messages. + // The field is unvalidated JSON from disk and may contain modes from a different build. + const validModes = new Set(PERMISSION_MODES) + for (const msg of migratedMessages) { + if ( + msg.type === 'user' && + msg.permissionMode !== undefined && + !validModes.has(msg.permissionMode) + ) { + msg.permissionMode = undefined + } + } + + // Filter out unresolved tool uses and any synthetic messages that follow them + const filteredToolUses = filterUnresolvedToolUses( + migratedMessages, + ) as NormalizedMessage[] + + // Filter out orphaned thinking-only assistant messages that can cause API errors + // during resume. These occur when streaming yields separate messages per content + // block and interleaved user messages prevent proper merging by message.id. + const filteredThinking = filterOrphanedThinkingOnlyMessages( + filteredToolUses, + ) as NormalizedMessage[] + + // Filter out assistant messages with only whitespace text content. + // This can happen when model outputs "\n\n" before thinking, user cancels mid-stream. + const filteredMessages = filterWhitespaceOnlyAssistantMessages( + filteredThinking, + ) as NormalizedMessage[] + + const internalState = detectTurnInterruption(filteredMessages) + + // Transform mid-turn interruptions into interrupted_prompt by appending + // a synthetic continuation message. This unifies both interruption kinds + // so the consumer only needs to handle interrupted_prompt. + let turnInterruptionState: TurnInterruptionState + if (internalState.kind === 'interrupted_turn') { + const [continuationMessage] = normalizeMessages([ + createUserMessage({ + content: 'Continue from where you left off.', + isMeta: true, + }), + ]) + filteredMessages.push(continuationMessage!) + turnInterruptionState = { + kind: 'interrupted_prompt', + message: continuationMessage!, + } + } else { + turnInterruptionState = internalState + } + + // Append a synthetic assistant sentinel after the last user message so + // the conversation is API-valid if no resume action is taken. Skip past + // trailing system/progress messages and insert right after the user + // message so removeInterruptedMessage's splice(idx, 2) removes the + // correct pair. + const lastRelevantIdx = filteredMessages.findLastIndex( + m => m.type !== 'system' && m.type !== 'progress', + ) + if ( + lastRelevantIdx !== -1 && + filteredMessages[lastRelevantIdx]!.type === 'user' + ) { + filteredMessages.splice( + lastRelevantIdx + 1, + 0, + createAssistantMessage({ + content: NO_RESPONSE_REQUESTED, + }) as NormalizedMessage, + ) + } + + return { messages: filteredMessages, turnInterruptionState } + } catch (error) { + logError(error as Error) + throw error + } +} + +/** + * Internal 3-way result from detection, before transforming interrupted_turn + * into interrupted_prompt with a synthetic continuation message. + */ +type InternalInterruptionState = + | TurnInterruptionState + | { kind: 'interrupted_turn' } + +/** + * Determines whether the conversation was interrupted mid-turn based on the + * last message after filtering. An assistant as last message (after filtering + * unresolved tool_uses) is treated as a completed turn because stop_reason is + * always null on persisted messages in the streaming path. + * + * System and progress messages are skipped when finding the last turn-relevant + * message — they are bookkeeping artifacts that should not mask a genuine + * interruption. Attachments are kept as part of the turn. + */ +function detectTurnInterruption( + messages: NormalizedMessage[], +): InternalInterruptionState { + if (messages.length === 0) { + return { kind: 'none' } + } + + // Find the last turn-relevant message, skipping system/progress and + // synthetic API error assistants. Error assistants are already filtered + // before API send (normalizeMessagesForAPI) — skipping them here lets + // auto-resume fire after retry exhaustion instead of reading the error as + // a completed turn. + const lastMessageIdx = messages.findLastIndex( + m => + m.type !== 'system' && + m.type !== 'progress' && + !(m.type === 'assistant' && m.isApiErrorMessage), + ) + const lastMessage = + lastMessageIdx !== -1 ? messages[lastMessageIdx] : undefined + + if (!lastMessage) { + return { kind: 'none' } + } + + if (lastMessage.type === 'assistant') { + // In the streaming path, stop_reason is always null on persisted messages + // because messages are recorded at content_block_stop time, before + // message_delta delivers the stop_reason. After filterUnresolvedToolUses + // has removed assistant messages with unmatched tool_uses, an assistant as + // the last message means the turn most likely completed normally. + return { kind: 'none' } + } + + if (lastMessage.type === 'user') { + if (lastMessage.isMeta || lastMessage.isCompactSummary) { + return { kind: 'none' } + } + if (isToolUseResultMessage(lastMessage)) { + // Brief mode (#20467) drops the trailing assistant text block, so a + // completed brief-mode turn legitimately ends on SendUserMessage's + // tool_result. Without this check, resume misclassifies every + // brief-mode session as interrupted mid-turn and injects a phantom + // "Continue from where you left off." before the user's real next + // prompt. Look back one step for the originating tool_use. + if (isTerminalToolResult(lastMessage, messages, lastMessageIdx)) { + return { kind: 'none' } + } + return { kind: 'interrupted_turn' } + } + // Plain text user prompt — CC hadn't started responding + return { kind: 'interrupted_prompt', message: lastMessage } + } + + if (lastMessage.type === 'attachment') { + // Attachments are part of the user turn — the user provided context but + // the assistant never responded. + return { kind: 'interrupted_turn' } + } + + return { kind: 'none' } +} + +/** + * Is this tool_result the output of a tool that legitimately terminates a + * turn? SendUserMessage is the canonical case: in brief mode, calling it is + * the turn's final act — there is no follow-up assistant text (#20467 + * removed it). A transcript ending here means the turn COMPLETED, not that + * it was killed mid-tool. + * + * Walks back to find the assistant tool_use that this result belongs to and + * checks its name. The matching tool_use is typically the immediately + * preceding relevant message (filterUnresolvedToolUses has already dropped + * unpaired ones), but we walk just in case system/progress noise is + * interleaved. + */ +function isTerminalToolResult( + result: NormalizedUserMessage, + messages: NormalizedMessage[], + resultIdx: number, +): boolean { + const content = result.message.content + if (!Array.isArray(content)) return false + const block = content[0] + if (block?.type !== 'tool_result') return false + const toolUseId = block.tool_use_id + + for (let i = resultIdx - 1; i >= 0; i--) { + const msg = messages[i]! + if (msg.type !== 'assistant') continue + for (const b of msg.message.content) { + if (b.type === 'tool_use' && b.id === toolUseId) { + return ( + b.name === BRIEF_TOOL_NAME || + b.name === LEGACY_BRIEF_TOOL_NAME || + b.name === SEND_USER_FILE_TOOL_NAME + ) + } + } + } + return false +} + +/** + * Restores skill state from invoked_skills attachments in messages. + * This ensures that skills are preserved across resume after compaction. + * Without this, if another compaction happens after resume, the skills would be lost + * because STATE.invokedSkills would be empty. + * @internal Exported for testing - use loadConversationForResume instead + */ +export function restoreSkillStateFromMessages(messages: Message[]): void { + for (const message of messages) { + if (message.type !== 'attachment') { + continue + } + if (message.attachment.type === 'invoked_skills') { + for (const skill of message.attachment.skills) { + if (skill.name && skill.path && skill.content) { + // Resume only happens for the main session, so agentId is null + addInvokedSkill(skill.name, skill.path, skill.content, null) + } + } + } + // A prior process already injected the skills-available reminder — it's + // in the transcript the model is about to see. sentSkillNames is + // process-local, so without this every resume re-announces the same + // ~600 tokens. Fire-once latch; consumed on the first attachment pass. + if (message.attachment.type === 'skill_listing') { + suppressNextSkillListing() + } + } +} + +/** + * Chain-walk a transcript jsonl by path. Same sequence loadFullLog + * runs internally — loadTranscriptFile → find newest non-sidechain + * leaf → buildConversationChain → removeExtraFields — just starting + * from an arbitrary path instead of the sid-derived one. + * + * leafUuids is populated by loadTranscriptFile as "uuids that no + * other message's parentUuid points at" — the chain tips. There can + * be several (sidechains, orphans); newest non-sidechain is the main + * conversation's end. + */ +export async function loadMessagesFromJsonlPath(path: string): Promise<{ + messages: SerializedMessage[] + sessionId: UUID | undefined +}> { + const { messages: byUuid, leafUuids } = await loadTranscriptFile(path) + let tip: (typeof byUuid extends Map ? T : never) | null = null + let tipTs = 0 + for (const m of byUuid.values()) { + if (m.isSidechain || !leafUuids.has(m.uuid)) continue + const ts = new Date(m.timestamp).getTime() + if (ts > tipTs) { + tipTs = ts + tip = m + } + } + if (!tip) return { messages: [], sessionId: undefined } + const chain = buildConversationChain(byUuid, tip) + return { + messages: removeExtraFields(chain), + // Leaf's sessionId — forked sessions copy chain[0] from the source + // transcript, so the root retains the source session's ID. Matches + // loadFullLog's mostRecentLeaf.sessionId. + sessionId: tip.sessionId as UUID | undefined, + } +} + +/** + * Loads a conversation for resume from various sources. + * This is the centralized function for loading and deserializing conversations. + * + * @param source - The source to load from: + * - undefined: load most recent conversation + * - string: session ID to load + * - LogOption: already loaded conversation + * @param sourceJsonlFile - Alternate: path to a transcript jsonl. + * Used when --resume receives a .jsonl path (cli/print.ts routes + * on suffix), typically for cross-directory resume where the + * transcript lives outside the current project dir. + * @returns Object containing the deserialized messages and the original log, or null if not found + */ +export async function loadConversationForResume( + source: string | LogOption | undefined, + sourceJsonlFile: string | undefined, +): Promise<{ + messages: Message[] + turnInterruptionState: TurnInterruptionState + fileHistorySnapshots?: FileHistorySnapshot[] + attributionSnapshots?: AttributionSnapshotMessage[] + contentReplacements?: ContentReplacementRecord[] + contextCollapseCommits?: ContextCollapseCommitEntry[] + contextCollapseSnapshot?: ContextCollapseSnapshotEntry + sessionId: UUID | undefined + // Session metadata for restoring agent context + agentName?: string + agentColor?: string + agentSetting?: string + customTitle?: string + tag?: string + mode?: 'coordinator' | 'normal' + worktreeSession?: PersistedWorktreeSession | null + prNumber?: number + prUrl?: string + prRepository?: string + // Full path to the session file (for cross-directory resume) + fullPath?: string +} | null> { + try { + let log: LogOption | null = null + let messages: Message[] | null = null + let sessionId: UUID | undefined + + if (source === undefined) { + // --continue: most recent session, skipping live --bg/daemon sessions + // that are actively writing their own transcript. + const logsPromise = loadMessageLogs() + let skip = new Set() + if (feature('BG_SESSIONS')) { + try { + const { listAllLiveSessions } = await import('./udsClient.js') + const live = await listAllLiveSessions() + skip = new Set( + live.flatMap(s => + s.kind && s.kind !== 'interactive' && s.sessionId + ? [s.sessionId] + : [], + ), + ) + } catch { + // UDS unavailable — treat all sessions as continuable + } + } + const logs = await logsPromise + log = + logs.find(l => { + const id = getSessionIdFromLog(l) + return !id || !skip.has(id) + }) ?? null + } else if (sourceJsonlFile) { + // --resume with a .jsonl path (cli/print.ts routes on suffix). + // Same chain walk as the sid branch below — only the starting + // path differs. + const loaded = await loadMessagesFromJsonlPath(sourceJsonlFile) + messages = loaded.messages + sessionId = loaded.sessionId + } else if (typeof source === 'string') { + // Load specific session by ID + log = await getLastSessionLog(source as UUID) + sessionId = source as UUID + } else { + // Already have a LogOption + log = source + } + + if (!log && !messages) { + return null + } + + if (log) { + // Load full messages for lite logs + if (isLiteLog(log)) { + log = await loadFullLog(log) + } + + // Determine sessionId first so we can pass it to copy functions + if (!sessionId) { + sessionId = getSessionIdFromLog(log) as UUID + } + // Pass the original session ID to ensure the plan slug is associated with + // the session we're resuming, not the temporary session ID before resume + if (sessionId) { + await copyPlanForResume(log, asSessionId(sessionId)) + } + + // Copy file history for resume + void copyFileHistoryForResume(log) + + messages = log.messages + checkResumeConsistency(messages) + } + + // Restore skill state from invoked_skills attachments before deserialization. + // This ensures skills survive multiple compaction cycles after resume. + restoreSkillStateFromMessages(messages!) + + // Deserialize messages to handle unresolved tool uses and ensure proper format + const deserialized = deserializeMessagesWithInterruptDetection(messages!) + messages = deserialized.messages + + // Process session start hooks for resume + const hookMessages = await processSessionStartHooks('resume', { sessionId }) + + // Append hook messages to the conversation + messages.push(...hookMessages) + + return { + messages, + turnInterruptionState: deserialized.turnInterruptionState, + fileHistorySnapshots: log?.fileHistorySnapshots, + attributionSnapshots: log?.attributionSnapshots, + contentReplacements: log?.contentReplacements, + contextCollapseCommits: log?.contextCollapseCommits, + contextCollapseSnapshot: log?.contextCollapseSnapshot, + sessionId, + // Include session metadata for restoring agent context on resume + agentName: log?.agentName, + agentColor: log?.agentColor, + agentSetting: log?.agentSetting, + customTitle: log?.customTitle, + tag: log?.tag, + mode: log?.mode, + worktreeSession: log?.worktreeSession, + prNumber: log?.prNumber, + prUrl: log?.prUrl, + prRepository: log?.prRepository, + // Include full path for cross-directory resume + fullPath: log?.fullPath, + } + } catch (error) { + logError(error as Error) + throw error + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/crypto.ts b/_all-in-one-upload/cc-haha/src/utils/crypto.ts new file mode 100644 index 0000000..a97fe05 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/crypto.ts @@ -0,0 +1,13 @@ +// Indirection point for the package.json "browser" field. When bun builds +// browser-sdk.js with --target browser, this file is swapped for +// crypto.browser.ts — avoiding a ~500KB crypto-browserify polyfill that Bun +// would otherwise inline for `import ... from 'crypto'`. Node/bun builds use +// this file unchanged. +// +// NOTE: `export { randomUUID } from 'crypto'` (re-export syntax) breaks under +// bun-internal's bytecode compilation — the generated bytecode shows the +// import but the binding doesn't link (`ReferenceError: randomUUID is not +// defined`). The explicit import-then-export below produces a correct live +// binding. See integration-tests-ant-native failure on PR #20957/#21178. +import { randomUUID } from 'crypto' +export { randomUUID } diff --git a/_all-in-one-upload/cc-haha/src/utils/debug.ts b/_all-in-one-upload/cc-haha/src/utils/debug.ts new file mode 100644 index 0000000..220da2b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/debug.ts @@ -0,0 +1,268 @@ +import { appendFile, mkdir, symlink, unlink } from 'fs/promises' +import memoize from 'lodash-es/memoize.js' +import { dirname, join } from 'path' +import { getSessionId } from 'src/bootstrap/state.js' + +import { type BufferedWriter, createBufferedWriter } from './bufferedWriter.js' +import { registerCleanup } from './cleanupRegistry.js' +import { + type DebugFilter, + parseDebugFilter, + shouldShowDebugMessage, +} from './debugFilter.js' +import { getClaudeConfigHomeDir, isEnvTruthy } from './envUtils.js' +import { getFsImplementation } from './fsOperations.js' +import { writeToStderr } from './process.js' +import { jsonStringify } from './slowOperations.js' + +export type DebugLogLevel = 'verbose' | 'debug' | 'info' | 'warn' | 'error' + +const LEVEL_ORDER: Record = { + verbose: 0, + debug: 1, + info: 2, + warn: 3, + error: 4, +} + +/** + * Minimum log level to include in debug output. Defaults to 'debug', which + * filters out 'verbose' messages. Set CLAUDE_CODE_DEBUG_LOG_LEVEL=verbose to + * include high-volume diagnostics (e.g. full statusLine command, shell, cwd, + * stdout/stderr) that would otherwise drown out useful debug output. + */ +export const getMinDebugLogLevel = memoize((): DebugLogLevel => { + const raw = process.env.CLAUDE_CODE_DEBUG_LOG_LEVEL?.toLowerCase().trim() + if (raw && Object.hasOwn(LEVEL_ORDER, raw)) { + return raw as DebugLogLevel + } + return 'debug' +}) + +let runtimeDebugEnabled = false + +export const isDebugMode = memoize((): boolean => { + return ( + runtimeDebugEnabled || + isEnvTruthy(process.env.DEBUG) || + isEnvTruthy(process.env.DEBUG_SDK) || + process.argv.includes('--debug') || + process.argv.includes('-d') || + isDebugToStdErr() || + // Also check for --debug=pattern syntax + process.argv.some(arg => arg.startsWith('--debug=')) || + // --debug-file implicitly enables debug mode + getDebugFilePath() !== null + ) +}) + +/** + * Enables debug logging mid-session (e.g. via /debug). Non-ants don't write + * debug logs by default, so this lets them start capturing without restarting + * with --debug. Returns true if logging was already active. + */ +export function enableDebugLogging(): boolean { + const wasActive = isDebugMode() || process.env.USER_TYPE === 'ant' + runtimeDebugEnabled = true + isDebugMode.cache.clear?.() + return wasActive +} + +// Extract and parse debug filter from command line arguments +// Exported for testing purposes +export const getDebugFilter = memoize((): DebugFilter | null => { + // Look for --debug=pattern in argv + const debugArg = process.argv.find(arg => arg.startsWith('--debug=')) + if (!debugArg) { + return null + } + + // Extract the pattern after the equals sign + const filterPattern = debugArg.substring('--debug='.length) + return parseDebugFilter(filterPattern) +}) + +export const isDebugToStdErr = memoize((): boolean => { + return ( + process.argv.includes('--debug-to-stderr') || process.argv.includes('-d2e') + ) +}) + +export const getDebugFilePath = memoize((): string | null => { + for (let i = 0; i < process.argv.length; i++) { + const arg = process.argv[i]! + if (arg.startsWith('--debug-file=')) { + return arg.substring('--debug-file='.length) + } + if (arg === '--debug-file' && i + 1 < process.argv.length) { + return process.argv[i + 1]! + } + } + return null +}) + +function shouldLogDebugMessage(message: string): boolean { + if (process.env.NODE_ENV === 'test' && !isDebugToStdErr()) { + return false + } + + // Non-ants only write debug logs when debug mode is active (via --debug at + // startup or /debug mid-session). Ants always log for /share, bug reports. + if (process.env.USER_TYPE !== 'ant' && !isDebugMode()) { + return false + } + + if ( + typeof process === 'undefined' || + typeof process.versions === 'undefined' || + typeof process.versions.node === 'undefined' + ) { + return false + } + + const filter = getDebugFilter() + return shouldShowDebugMessage(message, filter) +} + +let hasFormattedOutput = false +export function setHasFormattedOutput(value: boolean): void { + hasFormattedOutput = value +} +export function getHasFormattedOutput(): boolean { + return hasFormattedOutput +} + +let debugWriter: BufferedWriter | null = null +let pendingWrite: Promise = Promise.resolve() + +// Module-level so .bind captures only its explicit args, not the +// writeFn closure's parent scope (Jarred, #22257). +async function appendAsync( + needMkdir: boolean, + dir: string, + path: string, + content: string, +): Promise { + if (needMkdir) { + await mkdir(dir, { recursive: true }).catch(() => {}) + } + await appendFile(path, content) + void updateLatestDebugLogSymlink() +} + +function noop(): void {} + +function getDebugWriter(): BufferedWriter { + if (!debugWriter) { + let ensuredDir: string | null = null + debugWriter = createBufferedWriter({ + writeFn: content => { + const path = getDebugLogPath() + const dir = dirname(path) + const needMkdir = ensuredDir !== dir + ensuredDir = dir + if (isDebugMode()) { + // immediateMode: must stay sync. Async writes are lost on direct + // process.exit() and keep the event loop alive in beforeExit + // handlers (infinite loop with Perfetto tracing). See #22257. + if (needMkdir) { + try { + getFsImplementation().mkdirSync(dir) + } catch { + // Directory already exists + } + } + getFsImplementation().appendFileSync(path, content) + void updateLatestDebugLogSymlink() + return + } + // Buffered path (ants without --debug): flushes ~1/sec so chain + // depth stays ~1. .bind over a closure so only the bound args are + // retained, not this scope. + pendingWrite = pendingWrite + .then(appendAsync.bind(null, needMkdir, dir, path, content)) + .catch(noop) + }, + flushIntervalMs: 1000, + maxBufferSize: 100, + immediateMode: isDebugMode(), + }) + registerCleanup(async () => { + debugWriter?.dispose() + await pendingWrite + }) + } + return debugWriter +} + +export async function flushDebugLogs(): Promise { + debugWriter?.flush() + await pendingWrite +} + +export function logForDebugging( + message: string, + { level }: { level: DebugLogLevel } = { + level: 'debug', + }, +): void { + if (LEVEL_ORDER[level] < LEVEL_ORDER[getMinDebugLogLevel()]) { + return + } + if (!shouldLogDebugMessage(message)) { + return + } + + // Multiline messages break the jsonl output format, so make any multiline messages JSON. + if (hasFormattedOutput && message.includes('\n')) { + message = jsonStringify(message) + } + const timestamp = new Date().toISOString() + const output = `${timestamp} [${level.toUpperCase()}] ${message.trim()}\n` + if (isDebugToStdErr()) { + writeToStderr(output) + return + } + + getDebugWriter().write(output) +} + +export function getDebugLogPath(): string { + return ( + getDebugFilePath() ?? + process.env.CLAUDE_CODE_DEBUG_LOGS_DIR ?? + join(getClaudeConfigHomeDir(), 'debug', `${getSessionId()}.txt`) + ) +} + +/** + * Updates the latest debug log symlink to point to the current debug log file. + * Creates or updates a symlink at ~/.claude/debug/latest + */ +const updateLatestDebugLogSymlink = memoize(async (): Promise => { + try { + const debugLogPath = getDebugLogPath() + const debugLogsDir = dirname(debugLogPath) + const latestSymlinkPath = join(debugLogsDir, 'latest') + + await unlink(latestSymlinkPath).catch(() => {}) + await symlink(debugLogPath, latestSymlinkPath) + } catch { + // Silently fail if symlink creation fails + } +}) + +/** + * Logs errors for Ants only, always visible in production. + */ +export function logAntError(context: string, error: unknown): void { + if (process.env.USER_TYPE !== 'ant') { + return + } + + if (error instanceof Error && error.stack) { + logForDebugging(`[ANT-ONLY] ${context} stack trace:\n${error.stack}`, { + level: 'error', + }) + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/directMemberMessage.ts b/_all-in-one-upload/cc-haha/src/utils/directMemberMessage.ts new file mode 100644 index 0000000..9429601 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/directMemberMessage.ts @@ -0,0 +1,69 @@ +import type { AppState } from '../state/AppState.js' + +/** + * Parse `@agent-name message` syntax for direct team member messaging. + */ +export function parseDirectMemberMessage(input: string): { + recipientName: string + message: string +} | null { + const match = input.match(/^@([\w-]+)\s+(.+)$/s) + if (!match) return null + + const [, recipientName, message] = match + if (!recipientName || !message) return null + + const trimmedMessage = message.trim() + if (!trimmedMessage) return null + + return { recipientName, message: trimmedMessage } +} + +export type DirectMessageResult = + | { success: true; recipientName: string } + | { + success: false + error: 'no_team_context' | 'unknown_recipient' + recipientName?: string + } + +type WriteToMailboxFn = ( + recipientName: string, + message: { from: string; text: string; timestamp: string }, + teamName: string, +) => Promise + +/** + * Send a direct message to a team member, bypassing the model. + */ +export async function sendDirectMemberMessage( + recipientName: string, + message: string, + teamContext: AppState['teamContext'], + writeToMailbox?: WriteToMailboxFn, +): Promise { + if (!teamContext || !writeToMailbox) { + return { success: false, error: 'no_team_context' } + } + + // Find team member by name + const member = Object.values(teamContext.teammates ?? {}).find( + t => t.name === recipientName, + ) + + if (!member) { + return { success: false, error: 'unknown_recipient', recipientName } + } + + await writeToMailbox( + recipientName, + { + from: 'user', + text: message, + timestamp: new Date().toISOString(), + }, + teamContext.teamName, + ) + + return { success: true, recipientName } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/errors.ts b/_all-in-one-upload/cc-haha/src/utils/errors.ts new file mode 100644 index 0000000..6a7f46e --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/errors.ts @@ -0,0 +1,238 @@ +import { APIUserAbortError } from '@anthropic-ai/sdk' + +export class ClaudeError extends Error { + constructor(message: string) { + super(message) + this.name = this.constructor.name + } +} + +export class MalformedCommandError extends Error {} + +export class AbortError extends Error { + constructor(message?: string) { + super(message) + this.name = 'AbortError' + } +} + +/** + * True iff `e` is any of the abort-shaped errors the codebase encounters: + * our AbortError class, a DOMException from AbortController.abort() + * (.name === 'AbortError'), or the SDK's APIUserAbortError. The SDK class + * is checked via instanceof because minified builds mangle class names — + * constructor.name becomes something like 'nJT' and the SDK never sets + * this.name, so string matching silently fails in production. + */ +export function isAbortError(e: unknown): boolean { + return ( + e instanceof AbortError || + e instanceof APIUserAbortError || + (e instanceof Error && e.name === 'AbortError') + ) +} + +/** + * Custom error class for configuration file parsing errors + * Includes the file path and the default configuration that should be used + */ +export class ConfigParseError extends Error { + filePath: string + defaultConfig: unknown + + constructor(message: string, filePath: string, defaultConfig: unknown) { + super(message) + this.name = 'ConfigParseError' + this.filePath = filePath + this.defaultConfig = defaultConfig + } +} + +export class ShellError extends Error { + constructor( + public readonly stdout: string, + public readonly stderr: string, + public readonly code: number, + public readonly interrupted: boolean, + ) { + super('Shell command failed') + this.name = 'ShellError' + } +} + +export class TeleportOperationError extends Error { + constructor( + message: string, + public readonly formattedMessage: string, + ) { + super(message) + this.name = 'TeleportOperationError' + } +} + +/** + * Error with a message that is safe to log to telemetry. + * Use the long name to confirm you've verified the message contains no + * sensitive data (file paths, URLs, code snippets). + * + * Single-arg: same message for user and telemetry + * Two-arg: different messages (e.g., full message has file path, telemetry doesn't) + * + * @example + * // Same message for both + * throw new TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS( + * 'MCP server "slack" connection timed out' + * ) + * + * // Different messages + * throw new TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS( + * `MCP tool timed out after ${ms}ms`, // Full message for logs/user + * 'MCP tool timed out' // Telemetry message + * ) + */ +export class TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS extends Error { + readonly telemetryMessage: string + + constructor(message: string, telemetryMessage?: string) { + super(message) + this.name = 'TelemetrySafeError' + this.telemetryMessage = telemetryMessage ?? message + } +} + +export function hasExactErrorMessage(error: unknown, message: string): boolean { + return error instanceof Error && error.message === message +} + +/** + * Normalize an unknown value into an Error. + * Use at catch-site boundaries when you need an Error instance. + */ +export function toError(e: unknown): Error { + return e instanceof Error ? e : new Error(String(e)) +} + +/** + * Extract a string message from an unknown error-like value. + * Use when you only need the message (e.g., for logging or display). + */ +export function errorMessage(e: unknown): string { + return e instanceof Error ? e.message : String(e) +} + +/** + * Extract the errno code (e.g., 'ENOENT', 'EACCES') from a caught error. + * Returns undefined if the error has no code or is not an ErrnoException. + * Replaces the `(e as NodeJS.ErrnoException).code` cast pattern. + */ +export function getErrnoCode(e: unknown): string | undefined { + if (e && typeof e === 'object' && 'code' in e && typeof e.code === 'string') { + return e.code + } + return undefined +} + +/** + * True if the error is ENOENT (file or directory does not exist). + * Replaces `(e as NodeJS.ErrnoException).code === 'ENOENT'`. + */ +export function isENOENT(e: unknown): boolean { + return getErrnoCode(e) === 'ENOENT' +} + +/** + * Extract the errno path (the filesystem path that triggered the error) + * from a caught error. Returns undefined if the error has no path. + * Replaces the `(e as NodeJS.ErrnoException).path` cast pattern. + */ +export function getErrnoPath(e: unknown): string | undefined { + if (e && typeof e === 'object' && 'path' in e && typeof e.path === 'string') { + return e.path + } + return undefined +} + +/** + * Extract error message + top N stack frames from an unknown error. + * Use when the error flows to the model as a tool_result — full stack + * traces are ~500-2000 chars of mostly-irrelevant internal frames and + * waste context tokens. Keep the full stack in debug logs instead. + */ +export function shortErrorStack(e: unknown, maxFrames = 5): string { + if (!(e instanceof Error)) return String(e) + if (!e.stack) return e.message + // V8/Bun stack format: "Name: message\n at frame1\n at frame2..." + // First line is the message; subsequent " at " lines are frames. + const lines = e.stack.split('\n') + const header = lines[0] ?? e.message + const frames = lines.slice(1).filter(l => l.trim().startsWith('at ')) + if (frames.length <= maxFrames) return e.stack + return [header, ...frames.slice(0, maxFrames)].join('\n') +} + +/** + * True if the error means the path is missing, inaccessible, or + * structurally unreachable — use in catch blocks after fs operations to + * distinguish expected "nothing there / no access" from unexpected errors. + * + * Covers: + * ENOENT — path does not exist + * EACCES — permission denied + * EPERM — operation not permitted + * ENOTDIR — a path component is not a directory (e.g. a file named + * `.claude` exists where a directory is expected) + * ELOOP — too many symlink levels (circular symlinks) + */ +export function isFsInaccessible(e: unknown): e is NodeJS.ErrnoException { + const code = getErrnoCode(e) + return ( + code === 'ENOENT' || + code === 'EACCES' || + code === 'EPERM' || + code === 'ENOTDIR' || + code === 'ELOOP' + ) +} + +export type AxiosErrorKind = + | 'auth' // 401/403 — caller typically sets skipRetry + | 'timeout' // ECONNABORTED + | 'network' // ECONNREFUSED/ENOTFOUND + | 'http' // other axios error (may have status) + | 'other' // not an axios error + +/** + * Classify a caught error from an axios request into one of a few buckets. + * Replaces the ~20-line isAxiosError → 401/403 → ECONNABORTED → ECONNREFUSED + * chain duplicated across sync-style services (settingsSync, policyLimits, + * remoteManagedSettings, teamMemorySync). + * + * Checks the `.isAxiosError` marker property directly (same as + * axios.isAxiosError()) to keep this module dependency-free. + */ +export function classifyAxiosError(e: unknown): { + kind: AxiosErrorKind + status?: number + message: string +} { + const message = errorMessage(e) + if ( + !e || + typeof e !== 'object' || + !('isAxiosError' in e) || + !e.isAxiosError + ) { + return { kind: 'other', message } + } + const err = e as { + response?: { status?: number } + code?: string + } + const status = err.response?.status + if (status === 401 || status === 403) return { kind: 'auth', status, message } + if (err.code === 'ECONNABORTED') return { kind: 'timeout', status, message } + if (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') { + return { kind: 'network', status, message } + } + return { kind: 'http', status, message } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/exampleCommands.ts b/_all-in-one-upload/cc-haha/src/utils/exampleCommands.ts new file mode 100644 index 0000000..3aecc67 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/exampleCommands.ts @@ -0,0 +1,184 @@ +import memoize from 'lodash-es/memoize.js' +import sample from 'lodash-es/sample.js' +import { getCwd } from '../utils/cwd.js' +import { getCurrentProjectConfig, saveCurrentProjectConfig } from './config.js' +import { env } from './env.js' +import { execFileNoThrowWithCwd } from './execFileNoThrow.js' +import { getIsGit, gitExe } from './git.js' +import { logError } from './log.js' +import { getGitEmail } from './user.js' + +// Patterns that mark a file as non-core (auto-generated, dependency, or config). +// Used to filter example-command filename suggestions deterministically +// instead of shelling out to Haiku. +const NON_CORE_PATTERNS = [ + // lock / dependency manifests + /(?:^|\/)(?:package-lock\.json|yarn\.lock|bun\.lock|bun\.lockb|pnpm-lock\.yaml|Pipfile\.lock|poetry\.lock|Cargo\.lock|Gemfile\.lock|go\.sum|composer\.lock|uv\.lock)$/, + // generated / build artifacts + /\.generated\./, + /(?:^|\/)(?:dist|build|out|target|node_modules|\.next|__pycache__)\//, + /\.(?:min\.js|min\.css|map|pyc|pyo)$/, + // data / docs / config extensions (not "write a test for" material) + /\.(?:json|ya?ml|toml|xml|ini|cfg|conf|env|lock|txt|md|mdx|rst|csv|log|svg)$/i, + // configuration / metadata + /(?:^|\/)\.?(?:eslintrc|prettierrc|babelrc|editorconfig|gitignore|gitattributes|dockerignore|npmrc)/, + /(?:^|\/)(?:tsconfig|jsconfig|biome|vitest\.config|jest\.config|webpack\.config|vite\.config|rollup\.config)\.[a-z]+$/, + /(?:^|\/)\.(?:github|vscode|idea|claude)\//, + // docs / changelogs (not "how does X work" material) + /(?:^|\/)(?:CHANGELOG|LICENSE|CONTRIBUTING|CODEOWNERS|README)(?:\.[a-z]+)?$/i, +] + +function isCoreFile(path: string): boolean { + return !NON_CORE_PATTERNS.some(p => p.test(path)) +} + +/** + * Counts occurrences of items in an array and returns the top N items + * sorted by count in descending order, formatted as a string. + */ +export function countAndSortItems(items: string[], topN: number = 20): string { + const counts = new Map() + for (const item of items) { + counts.set(item, (counts.get(item) || 0) + 1) + } + return Array.from(counts.entries()) + .sort((a, b) => b[1] - a[1]) + .slice(0, topN) + .map(([item, count]) => `${count.toString().padStart(6)} ${item}`) + .join('\n') +} + +/** + * Picks up to `want` basenames from a frequency-sorted list of paths, + * skipping non-core files and spreading across different directories. + * Returns empty array if fewer than `want` core files are available. + */ +export function pickDiverseCoreFiles( + sortedPaths: string[], + want: number, +): string[] { + const picked: string[] = [] + const seenBasenames = new Set() + const dirTally = new Map() + + // Greedy: on each pass allow +1 file per directory. Keeps the + // top-5 from collapsing into a single hot folder while still + // letting a dominant folder contribute multiple files if the + // repo is narrow. + for (let cap = 1; picked.length < want && cap <= want; cap++) { + for (const p of sortedPaths) { + if (picked.length >= want) break + if (!isCoreFile(p)) continue + const lastSep = Math.max(p.lastIndexOf('/'), p.lastIndexOf('\\')) + const base = lastSep >= 0 ? p.slice(lastSep + 1) : p + if (!base || seenBasenames.has(base)) continue + const dir = lastSep >= 0 ? p.slice(0, lastSep) : '.' + if ((dirTally.get(dir) ?? 0) >= cap) continue + picked.push(base) + seenBasenames.add(base) + dirTally.set(dir, (dirTally.get(dir) ?? 0) + 1) + } + } + + return picked.length >= want ? picked : [] +} + +async function getFrequentlyModifiedFiles(): Promise { + if (process.env.NODE_ENV === 'test') return [] + if (env.platform === 'win32') return [] + if (!(await getIsGit())) return [] + + try { + // Collect frequently-modified files, preferring the user's own commits. + const userEmail = await getGitEmail() + + const logArgs = [ + 'log', + '-n', + '1000', + '--pretty=format:', + '--name-only', + '--diff-filter=M', + ] + + const counts = new Map() + const tallyInto = (stdout: string) => { + for (const line of stdout.split('\n')) { + const f = line.trim() + if (f) counts.set(f, (counts.get(f) ?? 0) + 1) + } + } + + if (userEmail) { + const { stdout } = await execFileNoThrowWithCwd( + 'git', + [...logArgs, `--author=${userEmail}`], + { cwd: getCwd() }, + ) + tallyInto(stdout) + } + + // Fall back to all authors if the user's own history is thin. + if (counts.size < 10) { + const { stdout } = await execFileNoThrowWithCwd(gitExe(), logArgs, { + cwd: getCwd(), + }) + tallyInto(stdout) + } + + const sorted = Array.from(counts.entries()) + .sort((a, b) => b[1] - a[1]) + .map(([p]) => p) + + return pickDiverseCoreFiles(sorted, 5) + } catch (err) { + logError(err as Error) + return [] + } +} + +const ONE_WEEK_IN_MS = 7 * 24 * 60 * 60 * 1000 + +export const getExampleCommandFromCache = memoize(() => { + const projectConfig = getCurrentProjectConfig() + const frequentFile = projectConfig.exampleFiles?.length + ? sample(projectConfig.exampleFiles) + : '' + + const commands = [ + 'fix lint errors', + 'fix typecheck errors', + `how does ${frequentFile} work?`, + `refactor ${frequentFile}`, + 'how do I log an error?', + `edit ${frequentFile} to...`, + `write a test for ${frequentFile}`, + 'create a util logging.py that...', + ] + + return `Try "${sample(commands)}"` +}) + +export const refreshExampleCommands = memoize(async (): Promise => { + const projectConfig = getCurrentProjectConfig() + const now = Date.now() + const lastGenerated = projectConfig.exampleFilesGeneratedAt ?? 0 + + // Regenerate examples if they're over a week old + if (now - lastGenerated > ONE_WEEK_IN_MS) { + projectConfig.exampleFiles = [] + } + + // If no example files cached, kickstart fetch in background + if (!projectConfig.exampleFiles?.length) { + void getFrequentlyModifiedFiles().then(files => { + if (files.length) { + saveCurrentProjectConfig(current => ({ + ...current, + exampleFiles: files, + exampleFilesGeneratedAt: Date.now(), + })) + } + }) + } +}) diff --git a/_all-in-one-upload/cc-haha/src/utils/forkedAgent.ts b/_all-in-one-upload/cc-haha/src/utils/forkedAgent.ts new file mode 100644 index 0000000..eff9595 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/forkedAgent.ts @@ -0,0 +1,689 @@ +/** + * Helper for running forked agent query loops with usage tracking. + * + * This utility ensures forked agents: + * 1. Share identical cache-critical params with the parent to guarantee prompt cache hits + * 2. Track full usage metrics across the entire query loop + * 3. Log metrics via the tengu_fork_agent_query event when complete + * 4. Isolate mutable state to prevent interference with the main agent loop + */ + +import type { UUID } from 'crypto' +import { randomUUID } from 'crypto' +import type { PromptCommand } from '../commands.js' +import type { QuerySource } from '../constants/querySource.js' +import type { CanUseToolFn } from '../hooks/useCanUseTool.js' +import { query } from '../query.js' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../services/analytics/index.js' +import { accumulateUsage, updateUsage } from '../services/api/claude.js' +import { EMPTY_USAGE, type NonNullableUsage } from '../services/api/logging.js' +import type { ToolUseContext } from '../Tool.js' +import type { AgentDefinition } from '../tools/AgentTool/loadAgentsDir.js' +import type { AgentId } from '../types/ids.js' +import type { Message } from '../types/message.js' +import { createChildAbortController } from './abortController.js' +import { logForDebugging } from './debug.js' +import { cloneFileStateCache } from './fileStateCache.js' +import type { REPLHookContext } from './hooks/postSamplingHooks.js' +import { + createUserMessage, + extractTextContent, + getLastAssistantMessage, +} from './messages.js' +import { createDenialTrackingState } from './permissions/denialTracking.js' +import { parseToolListFromCLI } from './permissions/permissionSetup.js' +import { recordSidechainTranscript } from './sessionStorage.js' +import type { SystemPrompt } from './systemPromptType.js' +import { + type ContentReplacementState, + cloneContentReplacementState, +} from './toolResultStorage.js' +import { createAgentId } from './uuid.js' + +/** + * Parameters that must be identical between the fork and parent API requests + * to share the parent's prompt cache. The Anthropic API cache key is composed of: + * system prompt, tools, model, messages (prefix), and thinking config. + * + * CacheSafeParams carries the first five. Thinking config is derived from the + * inherited toolUseContext.options.thinkingConfig — but can be inadvertently + * changed if the fork sets maxOutputTokens, which clamps budget_tokens in + * claude.ts (but only for older models that do not use adaptive thinking). + * See the maxOutputTokens doc on ForkedAgentParams. + */ +export type CacheSafeParams = { + /** System prompt - must match parent for cache hits */ + systemPrompt: SystemPrompt + /** User context - prepended to messages, affects cache */ + userContext: { [k: string]: string } + /** System context - appended to system prompt, affects cache */ + systemContext: { [k: string]: string } + /** Tool use context containing tools, model, and other options */ + toolUseContext: ToolUseContext + /** Parent context messages for prompt cache sharing */ + forkContextMessages: Message[] +} + +// Slot written by handleStopHooks after each turn so post-turn forks +// (promptSuggestion, postTurnSummary, /btw) can share the main loop's +// prompt cache without each caller threading params through. +let lastCacheSafeParams: CacheSafeParams | null = null + +export function saveCacheSafeParams(params: CacheSafeParams | null): void { + lastCacheSafeParams = params +} + +export function getLastCacheSafeParams(): CacheSafeParams | null { + return lastCacheSafeParams +} + +export type ForkedAgentParams = { + /** Messages to start the forked query loop with */ + promptMessages: Message[] + /** Cache-safe parameters that must match the parent query */ + cacheSafeParams: CacheSafeParams + /** Permission check function for the forked agent */ + canUseTool: CanUseToolFn + /** Source identifier for tracking */ + querySource: QuerySource + /** Label for analytics (e.g., 'session_memory', 'supervisor') */ + forkLabel: string + /** Optional overrides for the subagent context (e.g., readFileState from setup phase) */ + overrides?: SubagentContextOverrides + /** + * Optional cap on output tokens. CAUTION: setting this changes both max_tokens + * AND budget_tokens (via clamping in claude.ts). If the fork uses cacheSafeParams + * to share the parent's prompt cache, a different budget_tokens will invalidate + * the cache — thinking config is part of the cache key. Only set this when cache + * sharing is not a goal (e.g., compact summaries). + */ + maxOutputTokens?: number + /** Optional cap on number of turns (API round-trips) */ + maxTurns?: number + /** Optional callback invoked for each message as it arrives (for streaming UI) */ + onMessage?: (message: Message) => void + /** Skip sidechain transcript recording (e.g., for ephemeral work like speculation) */ + skipTranscript?: boolean + /** Skip writing new prompt cache entries on the last message. For + * fire-and-forget forks where no future request will read from this prefix. */ + skipCacheWrite?: boolean +} + +export type ForkedAgentResult = { + /** All messages yielded during the query loop */ + messages: Message[] + /** Accumulated usage across all API calls in the loop */ + totalUsage: NonNullableUsage +} + +/** + * Creates CacheSafeParams from REPLHookContext. + * Use this helper when forking from a post-sampling hook context. + * + * To override specific fields (e.g., toolUseContext with cloned file state), + * spread the result and override: `{ ...createCacheSafeParams(context), toolUseContext: clonedContext }` + * + * @param context - The REPLHookContext from the post-sampling hook + */ +export function createCacheSafeParams( + context: REPLHookContext, +): CacheSafeParams { + return { + systemPrompt: context.systemPrompt, + userContext: context.userContext, + systemContext: context.systemContext, + toolUseContext: context.toolUseContext, + forkContextMessages: context.messages, + } +} + +/** + * Creates a modified getAppState that adds allowed tools to the permission context. + * This is used by forked skill/command execution to grant tool permissions. + */ +export function createGetAppStateWithAllowedTools( + baseGetAppState: ToolUseContext['getAppState'], + allowedTools: string[], +): ToolUseContext['getAppState'] { + if (allowedTools.length === 0) return baseGetAppState + return () => { + const appState = baseGetAppState() + return { + ...appState, + toolPermissionContext: { + ...appState.toolPermissionContext, + alwaysAllowRules: { + ...appState.toolPermissionContext.alwaysAllowRules, + command: [ + ...new Set([ + ...(appState.toolPermissionContext.alwaysAllowRules.command || + []), + ...allowedTools, + ]), + ], + }, + }, + } + } +} + +/** + * Result from preparing a forked command context. + */ +export type PreparedForkedContext = { + /** Skill content with args replaced */ + skillContent: string + /** Modified getAppState with allowed tools */ + modifiedGetAppState: ToolUseContext['getAppState'] + /** The general-purpose agent to use */ + baseAgent: AgentDefinition + /** Initial prompt messages */ + promptMessages: Message[] +} + +/** + * Prepares the context for executing a forked command/skill. + * This handles the common setup that both SkillTool and slash commands need. + */ +export async function prepareForkedCommandContext( + command: PromptCommand, + args: string, + context: ToolUseContext, +): Promise { + // Get skill content with $ARGUMENTS replaced + const skillPrompt = await command.getPromptForCommand(args, context) + const skillContent = skillPrompt + .map(block => (block.type === 'text' ? block.text : '')) + .join('\n') + + // Parse and prepare allowed tools + const allowedTools = parseToolListFromCLI(command.allowedTools ?? []) + + // Create modified context with allowed tools + const modifiedGetAppState = createGetAppStateWithAllowedTools( + context.getAppState, + allowedTools, + ) + + // Use command.agent if specified, otherwise 'general-purpose' + const agentTypeName = command.agent ?? 'general-purpose' + const agents = context.options.agentDefinitions.activeAgents + const baseAgent = + agents.find(a => a.agentType === agentTypeName) ?? + agents.find(a => a.agentType === 'general-purpose') ?? + agents[0] + + if (!baseAgent) { + throw new Error('No agent available for forked execution') + } + + // Prepare prompt messages + const promptMessages = [createUserMessage({ content: skillContent })] + + return { + skillContent, + modifiedGetAppState, + baseAgent, + promptMessages, + } +} + +/** + * Extracts result text from agent messages. + */ +export function extractResultText( + agentMessages: Message[], + defaultText = 'Execution completed', +): string { + const lastAssistantMessage = getLastAssistantMessage(agentMessages) + if (!lastAssistantMessage) return defaultText + + const textContent = extractTextContent( + lastAssistantMessage.message.content, + '\n', + ) + + return textContent || defaultText +} + +/** + * Options for creating a subagent context. + * + * By default, all mutable state is isolated to prevent interference with the parent. + * Use these options to: + * - Override specific fields (e.g., custom options, agentId, messages) + * - Explicitly opt-in to sharing specific callbacks (for interactive subagents) + */ +export type SubagentContextOverrides = { + /** Override the options object (e.g., custom tools, model) */ + options?: ToolUseContext['options'] + /** Override the agentId (for subagents with their own ID) */ + agentId?: AgentId + /** Override the agentType (for subagents with a specific type) */ + agentType?: string + /** Override the messages array */ + messages?: Message[] + /** Override the readFileState (e.g., fresh cache instead of clone) */ + readFileState?: ToolUseContext['readFileState'] + /** Override the abortController */ + abortController?: AbortController + /** Override the getAppState function */ + getAppState?: ToolUseContext['getAppState'] + + /** + * Explicit opt-in to share parent's setAppState callback. + * Use for interactive subagents that need to update shared state. + * @default false (isolated no-op) + */ + shareSetAppState?: boolean + /** + * Explicit opt-in to share parent's setResponseLength callback. + * Use for subagents that contribute to parent's response metrics. + * @default false (isolated no-op) + */ + shareSetResponseLength?: boolean + /** + * Explicit opt-in to share parent's abortController. + * Use for interactive subagents that should abort with parent. + * Note: Only applies if abortController override is not provided. + * @default false (new controller linked to parent) + */ + shareAbortController?: boolean + /** Critical system reminder to re-inject at every user turn */ + criticalSystemReminder_EXPERIMENTAL?: string + /** When true, canUseTool must always be called even when hooks auto-approve. + * Used by speculation for overlay file path rewriting. */ + requireCanUseTool?: boolean + /** Override replacement state — used by resumeAgentBackground to thread + * state reconstructed from the resumed sidechain so the same results + * are re-replaced (prompt cache stability). */ + contentReplacementState?: ContentReplacementState +} + +/** + * Creates an isolated ToolUseContext for subagents. + * + * By default, ALL mutable state is isolated to prevent interference: + * - readFileState: cloned from parent + * - abortController: new controller linked to parent (parent abort propagates) + * - getAppState: wrapped to set shouldAvoidPermissionPrompts + * - All mutation callbacks (setAppState, etc.): no-op + * - Fresh collections: nestedMemoryAttachmentTriggers, toolDecisions + * + * Callers can: + * - Override specific fields via the overrides parameter + * - Explicitly opt-in to sharing specific callbacks (shareSetAppState, etc.) + * + * @param parentContext - The parent's ToolUseContext to create subagent context from + * @param overrides - Optional overrides and sharing options + * + * @example + * // Full isolation (for background agents like session memory) + * const ctx = createSubagentContext(parentContext) + * + * @example + * // Custom options and agentId (for AgentTool async agents) + * const ctx = createSubagentContext(parentContext, { + * options: customOptions, + * agentId: newAgentId, + * messages: initialMessages, + * }) + * + * @example + * // Interactive subagent that shares some state + * const ctx = createSubagentContext(parentContext, { + * options: customOptions, + * agentId: newAgentId, + * shareSetAppState: true, + * shareSetResponseLength: true, + * shareAbortController: true, + * }) + */ +export function createSubagentContext( + parentContext: ToolUseContext, + overrides?: SubagentContextOverrides, +): ToolUseContext { + // Determine abortController: explicit override > share parent's > new child + const abortController = + overrides?.abortController ?? + (overrides?.shareAbortController + ? parentContext.abortController + : createChildAbortController(parentContext.abortController)) + + // Determine getAppState - wrap to set shouldAvoidPermissionPrompts unless sharing abortController + // (if sharing abortController, it's an interactive agent that CAN show UI) + const getAppState: ToolUseContext['getAppState'] = overrides?.getAppState + ? overrides.getAppState + : overrides?.shareAbortController + ? parentContext.getAppState + : () => { + const state = parentContext.getAppState() + if (state.toolPermissionContext.shouldAvoidPermissionPrompts) { + return state + } + return { + ...state, + toolPermissionContext: { + ...state.toolPermissionContext, + shouldAvoidPermissionPrompts: true, + }, + } + } + + return { + // Mutable state - cloned by default to maintain isolation + // Clone overrides.readFileState if provided, otherwise clone from parent + readFileState: cloneFileStateCache( + overrides?.readFileState ?? parentContext.readFileState, + ), + nestedMemoryAttachmentTriggers: new Set(), + loadedNestedMemoryPaths: new Set(), + dynamicSkillDirTriggers: new Set(), + // Per-subagent: tracks skills surfaced by discovery for was_discovered telemetry (SkillTool.ts:116) + discoveredSkillNames: new Set(), + toolDecisions: undefined, + // Budget decisions: override > clone of parent > undefined (feature off). + // + // Clone by default (not fresh): cache-sharing forks process parent + // messages containing parent tool_use_ids. A fresh state would see + // them as unseen and make divergent replacement decisions → wire + // prefix differs → cache miss. A clone makes identical decisions → + // cache hit. For non-forking subagents the parent UUIDs never match + // — clone is a harmless no-op. + // + // Override: AgentTool resume (reconstructed from sidechain records) + // and inProcessRunner (per-teammate persistent loop state). + contentReplacementState: + overrides?.contentReplacementState ?? + (parentContext.contentReplacementState + ? cloneContentReplacementState(parentContext.contentReplacementState) + : undefined), + + // AbortController + abortController, + + // AppState access + getAppState, + setAppState: overrides?.shareSetAppState + ? parentContext.setAppState + : () => {}, + // Task registration/kill must always reach the root store, even when + // setAppState is a no-op — otherwise async agents' background bash tasks + // are never registered and never killed (PPID=1 zombie). + setAppStateForTasks: + parentContext.setAppStateForTasks ?? parentContext.setAppState, + // Async subagents whose setAppState is a no-op need local denial tracking + // so the denial counter actually accumulates across retries. + localDenialTracking: overrides?.shareSetAppState + ? parentContext.localDenialTracking + : createDenialTrackingState(), + + // Mutation callbacks - no-op by default + setInProgressToolUseIDs: () => {}, + setResponseLength: overrides?.shareSetResponseLength + ? parentContext.setResponseLength + : () => {}, + pushApiMetricsEntry: overrides?.shareSetResponseLength + ? parentContext.pushApiMetricsEntry + : undefined, + updateFileHistoryState: () => {}, + // Attribution is scoped and functional (prev => next) — safe to share even + // when setAppState is stubbed. Concurrent calls compose via React's state queue. + updateAttributionState: parentContext.updateAttributionState, + + // UI callbacks - undefined for subagents (can't control parent UI) + addNotification: undefined, + setToolJSX: undefined, + setStreamMode: undefined, + setSDKStatus: undefined, + openMessageSelector: undefined, + + // Fields that can be overridden or copied from parent + options: overrides?.options ?? parentContext.options, + messages: overrides?.messages ?? parentContext.messages, + // Generate new agentId for subagents (each subagent should have its own ID) + agentId: overrides?.agentId ?? createAgentId(), + agentType: overrides?.agentType, + + // Create new query tracking chain for subagent with incremented depth + queryTracking: { + chainId: randomUUID(), + depth: (parentContext.queryTracking?.depth ?? -1) + 1, + }, + fileReadingLimits: parentContext.fileReadingLimits, + userModified: parentContext.userModified, + criticalSystemReminder_EXPERIMENTAL: + overrides?.criticalSystemReminder_EXPERIMENTAL, + requireCanUseTool: overrides?.requireCanUseTool, + } +} + +/** + * Runs a forked agent query loop and tracks cache hit metrics. + * + * This function: + * 1. Uses identical cache-safe params from parent to enable prompt caching + * 2. Accumulates usage across all query iterations + * 3. Logs tengu_fork_agent_query with full usage when complete + * + * @example + * ```typescript + * const result = await runForkedAgent({ + * promptMessages: [createUserMessage({ content: userPrompt })], + * cacheSafeParams: { + * systemPrompt, + * userContext, + * systemContext, + * toolUseContext: clonedToolUseContext, + * forkContextMessages: messages, + * }, + * canUseTool, + * querySource: 'session_memory', + * forkLabel: 'session_memory', + * }) + * ``` + */ +export async function runForkedAgent({ + promptMessages, + cacheSafeParams, + canUseTool, + querySource, + forkLabel, + overrides, + maxOutputTokens, + maxTurns, + onMessage, + skipTranscript, + skipCacheWrite, +}: ForkedAgentParams): Promise { + const startTime = Date.now() + const outputMessages: Message[] = [] + let totalUsage: NonNullableUsage = { ...EMPTY_USAGE } + + const { + systemPrompt, + userContext, + systemContext, + toolUseContext, + forkContextMessages, + } = cacheSafeParams + + // Create isolated context to prevent mutation of parent state + const isolatedToolUseContext = createSubagentContext( + toolUseContext, + overrides, + ) + + // Do NOT filterIncompleteToolCalls here — it drops the whole assistant on + // partial tool batches, orphaning the paired results (API 400). Dangling + // tool_uses are repaired downstream by ensureToolResultPairing in claude.ts, + // same as the main thread — identical post-repair prefix keeps the cache hit. + const initialMessages: Message[] = [...forkContextMessages, ...promptMessages] + + // Generate agent ID and record initial messages for transcript + // When skipTranscript is set, skip agent ID creation and all transcript I/O + const agentId = skipTranscript ? undefined : createAgentId(forkLabel) + let lastRecordedUuid: UUID | null = null + if (agentId) { + await recordSidechainTranscript(initialMessages, agentId).catch(err => + logForDebugging( + `Forked agent [${forkLabel}] failed to record initial transcript: ${err}`, + ), + ) + // Track the last recorded message UUID for parent chain continuity + lastRecordedUuid = + initialMessages.length > 0 + ? initialMessages[initialMessages.length - 1]!.uuid + : null + } + + // Run the query loop with isolated context (cache-safe params preserved) + try { + for await (const message of query({ + messages: initialMessages, + systemPrompt, + userContext, + systemContext, + canUseTool, + toolUseContext: isolatedToolUseContext, + querySource, + maxOutputTokensOverride: maxOutputTokens, + maxTurns, + skipCacheWrite, + })) { + // Extract real usage from message_delta stream events (final usage per API call) + if (message.type === 'stream_event') { + if ( + 'event' in message && + message.event?.type === 'message_delta' && + message.event.usage + ) { + const turnUsage = updateUsage({ ...EMPTY_USAGE }, message.event.usage) + totalUsage = accumulateUsage(totalUsage, turnUsage) + } + continue + } + if (message.type === 'stream_request_start') { + continue + } + + logForDebugging( + `Forked agent [${forkLabel}] received message: type=${message.type}`, + ) + + outputMessages.push(message as Message) + onMessage?.(message as Message) + + // Record transcript for recordable message types (same pattern as runAgent.ts) + const msg = message as Message + if ( + agentId && + (msg.type === 'assistant' || + msg.type === 'user' || + msg.type === 'progress') + ) { + await recordSidechainTranscript([msg], agentId, lastRecordedUuid).catch( + err => + logForDebugging( + `Forked agent [${forkLabel}] failed to record transcript: ${err}`, + ), + ) + if (msg.type !== 'progress') { + lastRecordedUuid = msg.uuid + } + } + } + } finally { + // Release cloned file state cache memory (same pattern as runAgent.ts) + isolatedToolUseContext.readFileState.clear() + // Release the cloned fork context messages + initialMessages.length = 0 + } + + logForDebugging( + `Forked agent [${forkLabel}] finished: ${outputMessages.length} messages, types=[${outputMessages.map(m => m.type).join(', ')}], totalUsage: input=${totalUsage.input_tokens} output=${totalUsage.output_tokens} cacheRead=${totalUsage.cache_read_input_tokens} cacheCreate=${totalUsage.cache_creation_input_tokens}`, + ) + + const durationMs = Date.now() - startTime + + // Log the fork query metrics with full NonNullableUsage + logForkAgentQueryEvent({ + forkLabel, + querySource, + durationMs, + messageCount: outputMessages.length, + totalUsage, + queryTracking: toolUseContext.queryTracking, + }) + + return { + messages: outputMessages, + totalUsage, + } +} + +/** + * Logs the tengu_fork_agent_query event with full NonNullableUsage fields. + */ +function logForkAgentQueryEvent({ + forkLabel, + querySource, + durationMs, + messageCount, + totalUsage, + queryTracking, +}: { + forkLabel: string + querySource: QuerySource + durationMs: number + messageCount: number + totalUsage: NonNullableUsage + queryTracking?: { chainId: string; depth: number } +}): void { + // Calculate cache hit rate + const totalInputTokens = + totalUsage.input_tokens + + totalUsage.cache_creation_input_tokens + + totalUsage.cache_read_input_tokens + const cacheHitRate = + totalInputTokens > 0 + ? totalUsage.cache_read_input_tokens / totalInputTokens + : 0 + + logEvent('tengu_fork_agent_query', { + // Metadata + forkLabel: + forkLabel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + querySource: + querySource as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + durationMs, + messageCount, + + // NonNullableUsage fields + inputTokens: totalUsage.input_tokens, + outputTokens: totalUsage.output_tokens, + cacheReadInputTokens: totalUsage.cache_read_input_tokens, + cacheCreationInputTokens: totalUsage.cache_creation_input_tokens, + serviceTier: + totalUsage.service_tier as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + cacheCreationEphemeral1hTokens: + totalUsage.cache_creation.ephemeral_1h_input_tokens, + cacheCreationEphemeral5mTokens: + totalUsage.cache_creation.ephemeral_5m_input_tokens, + + // Derived metrics + cacheHitRate, + + // Query tracking + ...(queryTracking + ? { + queryChainId: + queryTracking.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + queryDepth: queryTracking.depth, + } + : {}), + }) +} diff --git a/_all-in-one-upload/cc-haha/src/utils/generatedFiles.ts b/_all-in-one-upload/cc-haha/src/utils/generatedFiles.ts new file mode 100644 index 0000000..76d1412 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/generatedFiles.ts @@ -0,0 +1,136 @@ +import { basename, extname, posix, sep } from 'path' + +/** + * File patterns that should be excluded from attribution. + * Based on GitHub Linguist vendored patterns and common generated file patterns. + */ + +// Exact file name matches (case-insensitive) +const EXCLUDED_FILENAMES = new Set([ + 'package-lock.json', + 'yarn.lock', + 'pnpm-lock.yaml', + 'bun.lockb', + 'bun.lock', + 'composer.lock', + 'gemfile.lock', + 'cargo.lock', + 'poetry.lock', + 'pipfile.lock', + 'shrinkwrap.json', + 'npm-shrinkwrap.json', +]) + +// File extension patterns (case-insensitive) +const EXCLUDED_EXTENSIONS = new Set([ + '.lock', + '.min.js', + '.min.css', + '.min.html', + '.bundle.js', + '.bundle.css', + '.generated.ts', + '.generated.js', + '.d.ts', // TypeScript declaration files +]) + +// Directory patterns that indicate generated/vendored content +const EXCLUDED_DIRECTORIES = [ + '/dist/', + '/build/', + '/out/', + '/output/', + '/node_modules/', + '/vendor/', + '/vendored/', + '/third_party/', + '/third-party/', + '/external/', + '/.next/', + '/.nuxt/', + '/.svelte-kit/', + '/coverage/', + '/__pycache__/', + '/.tox/', + '/venv/', + '/.venv/', + '/target/release/', + '/target/debug/', +] + +// Filename patterns using regex for more complex matching +const EXCLUDED_FILENAME_PATTERNS = [ + /^.*\.min\.[a-z]+$/i, // *.min.* + /^.*-min\.[a-z]+$/i, // *-min.* + /^.*\.bundle\.[a-z]+$/i, // *.bundle.* + /^.*\.generated\.[a-z]+$/i, // *.generated.* + /^.*\.gen\.[a-z]+$/i, // *.gen.* + /^.*\.auto\.[a-z]+$/i, // *.auto.* + /^.*_generated\.[a-z]+$/i, // *_generated.* + /^.*_gen\.[a-z]+$/i, // *_gen.* + /^.*\.pb\.(go|js|ts|py|rb)$/i, // Protocol buffer generated files + /^.*_pb2?\.py$/i, // Python protobuf files + /^.*\.pb\.h$/i, // C++ protobuf headers + /^.*\.grpc\.[a-z]+$/i, // gRPC generated files + /^.*\.swagger\.[a-z]+$/i, // Swagger generated files + /^.*\.openapi\.[a-z]+$/i, // OpenAPI generated files +] + +/** + * Check if a file should be excluded from attribution based on Linguist-style rules. + * + * @param filePath - Relative file path from repository root + * @returns true if the file should be excluded from attribution + */ +export function isGeneratedFile(filePath: string): boolean { + // Normalize path separators for consistent pattern matching (patterns use posix-style /) + const normalizedPath = + posix.sep + filePath.split(sep).join(posix.sep).replace(/^\/+/, '') + const fileName = basename(filePath).toLowerCase() + const ext = extname(filePath).toLowerCase() + + // Check exact filename matches + if (EXCLUDED_FILENAMES.has(fileName)) { + return true + } + + // Check extension matches + if (EXCLUDED_EXTENSIONS.has(ext)) { + return true + } + + // Check for compound extensions like .min.js + const parts = fileName.split('.') + if (parts.length > 2) { + const compoundExt = '.' + parts.slice(-2).join('.') + if (EXCLUDED_EXTENSIONS.has(compoundExt)) { + return true + } + } + + // Check directory patterns + for (const dir of EXCLUDED_DIRECTORIES) { + if (normalizedPath.includes(dir)) { + return true + } + } + + // Check filename patterns + for (const pattern of EXCLUDED_FILENAME_PATTERNS) { + if (pattern.test(fileName)) { + return true + } + } + + return false +} + +/** + * Filter a list of files to exclude generated files. + * + * @param files - Array of file paths + * @returns Array of files that are not generated + */ +export function filterGeneratedFiles(files: string[]): string[] { + return files.filter(file => !isGeneratedFile(file)) +} diff --git a/_all-in-one-upload/cc-haha/src/utils/gitSettings.ts b/_all-in-one-upload/cc-haha/src/utils/gitSettings.ts new file mode 100644 index 0000000..f142995 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/gitSettings.ts @@ -0,0 +1,18 @@ +// Git-related behaviors that depend on user settings. +// +// This lives outside git.ts because git.ts is in the vscode extension's +// dep graph and must stay free of settings.ts, which transitively pulls +// @opentelemetry/api + undici (forbidden in vscode). It's also a cycle: +// settings.ts → git/gitignore.ts → git.ts, so git.ts → settings.ts loops. +// +// If you're tempted to add `import settings` to git.ts — don't. Put it here. + +import { isEnvDefinedFalsy, isEnvTruthy } from './envUtils.js' +import { getInitialSettings } from './settings/settings.js' + +export function shouldIncludeGitInstructions(): boolean { + const envVal = process.env.CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS + if (isEnvTruthy(envVal)) return false + if (isEnvDefinedFalsy(envVal)) return true + return getInitialSettings().includeGitInstructions ?? true +} diff --git a/_all-in-one-upload/cc-haha/src/utils/headlessProfiler.ts b/_all-in-one-upload/cc-haha/src/utils/headlessProfiler.ts new file mode 100644 index 0000000..50aadaf --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/headlessProfiler.ts @@ -0,0 +1,178 @@ +/** + * Headless mode profiling utility for measuring per-turn latency in -p (print) mode. + * + * Tracks key timing phases per turn: + * - Time to system message output (turn 0 only) + * - Time to first query started + * - Time to first API response (TTFT) + * + * Uses Node.js built-in performance hooks API for standard timing measurement. + * Sampled logging: 100% of ant users, 5% of external users. + * + * Set CLAUDE_CODE_PROFILE_STARTUP=1 for detailed logging output. + */ + +import { getIsNonInteractiveSession } from '../bootstrap/state.js' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../services/analytics/index.js' +import { logForDebugging } from './debug.js' +import { isEnvTruthy } from './envUtils.js' +import { getPerformance } from './profilerBase.js' +import { jsonStringify } from './slowOperations.js' + +// Detailed profiling mode - same env var as startupProfiler +// eslint-disable-next-line custom-rules/no-process-env-top-level +const DETAILED_PROFILING = isEnvTruthy(process.env.CLAUDE_CODE_PROFILE_STARTUP) + +// Sampling for Statsig logging: 100% ant, 5% external +// Decision made once at module load - non-sampled users pay no profiling cost +const STATSIG_SAMPLE_RATE = 0.05 +// eslint-disable-next-line custom-rules/no-process-env-top-level +const STATSIG_LOGGING_SAMPLED = + process.env.USER_TYPE === 'ant' || Math.random() < STATSIG_SAMPLE_RATE + +// Enable profiling if either detailed mode OR sampled for Statsig +const SHOULD_PROFILE = DETAILED_PROFILING || STATSIG_LOGGING_SAMPLED + +// Use a unique prefix to avoid conflicts with other profiler marks +const MARK_PREFIX = 'headless_' + +// Track current turn number (auto-incremented by headlessProfilerStartTurn) +let currentTurnNumber = -1 + +/** + * Clear all headless profiler marks from performance timeline + */ +function clearHeadlessMarks(): void { + const perf = getPerformance() + const allMarks = perf.getEntriesByType('mark') + for (const mark of allMarks) { + if (mark.name.startsWith(MARK_PREFIX)) { + perf.clearMarks(mark.name) + } + } +} + +/** + * Start a new turn for profiling. Clears previous marks, increments turn number, + * and records turn_start. Call this at the beginning of each user message processing. + */ +export function headlessProfilerStartTurn(): void { + // Only profile in headless/non-interactive mode + if (!getIsNonInteractiveSession()) return + // Only profile if enabled + if (!SHOULD_PROFILE) return + + currentTurnNumber++ + clearHeadlessMarks() + + const perf = getPerformance() + perf.mark(`${MARK_PREFIX}turn_start`) + + if (DETAILED_PROFILING) { + logForDebugging(`[headlessProfiler] Started turn ${currentTurnNumber}`) + } +} + +/** + * Record a checkpoint with the given name. + * Only records if in headless mode and profiling is enabled. + */ +export function headlessProfilerCheckpoint(name: string): void { + // Only profile in headless/non-interactive mode + if (!getIsNonInteractiveSession()) return + // Only profile if enabled + if (!SHOULD_PROFILE) return + + const perf = getPerformance() + perf.mark(`${MARK_PREFIX}${name}`) + + if (DETAILED_PROFILING) { + logForDebugging( + `[headlessProfiler] Checkpoint: ${name} at ${perf.now().toFixed(1)}ms`, + ) + } +} + +/** + * Log headless latency metrics for the current turn to Statsig. + * Call this at the end of each turn (before processing next user message). + */ +export function logHeadlessProfilerTurn(): void { + // Only log in headless mode + if (!getIsNonInteractiveSession()) return + // Only log if enabled + if (!SHOULD_PROFILE) return + + const perf = getPerformance() + const allMarks = perf.getEntriesByType('mark') + + // Filter to only our headless marks + const marks = allMarks.filter(mark => mark.name.startsWith(MARK_PREFIX)) + if (marks.length === 0) return + + // Build checkpoint lookup (strip prefix for easier access) + const checkpointTimes = new Map() + for (const mark of marks) { + const name = mark.name.slice(MARK_PREFIX.length) + checkpointTimes.set(name, mark.startTime) + } + + const turnStart = checkpointTimes.get('turn_start') + if (turnStart === undefined) return + + // Compute phase durations relative to turn_start + const metadata: Record = { + turn_number: currentTurnNumber, + } + + // Time to system message from process start (only meaningful for turn 0) + // Use absolute time since perf_hooks startTime is relative to process start + const systemMessageTime = checkpointTimes.get('system_message_yielded') + if (systemMessageTime !== undefined && currentTurnNumber === 0) { + metadata.time_to_system_message_ms = Math.round(systemMessageTime) + } + + // Time to query start + const queryStartTime = checkpointTimes.get('query_started') + if (queryStartTime !== undefined) { + metadata.time_to_query_start_ms = Math.round(queryStartTime - turnStart) + } + + // Time to first response (first chunk from API) + const firstChunkTime = checkpointTimes.get('first_chunk') + if (firstChunkTime !== undefined) { + metadata.time_to_first_response_ms = Math.round(firstChunkTime - turnStart) + } + + // Query overhead (time between query start and API request sent) + const apiRequestTime = checkpointTimes.get('api_request_sent') + if (queryStartTime !== undefined && apiRequestTime !== undefined) { + metadata.query_overhead_ms = Math.round(apiRequestTime - queryStartTime) + } + + // Add checkpoint count for debugging + metadata.checkpoint_count = marks.length + + // Add entrypoint for segmentation (sdk-ts, sdk-py, sdk-cli, or undefined) + if (process.env.CLAUDE_CODE_ENTRYPOINT) { + metadata.entrypoint = process.env.CLAUDE_CODE_ENTRYPOINT + } + + // Log to Statsig if sampled + if (STATSIG_LOGGING_SAMPLED) { + logEvent( + 'tengu_headless_latency', + metadata as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ) + } + + // Log detailed output if CLAUDE_CODE_PROFILE_STARTUP=1 + if (DETAILED_PROFILING) { + logForDebugging( + `[headlessProfiler] Turn ${currentTurnNumber} metrics: ${jsonStringify(metadata)}`, + ) + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/hooks/fileChangedWatcher.ts b/_all-in-one-upload/cc-haha/src/utils/hooks/fileChangedWatcher.ts new file mode 100644 index 0000000..3d5f771 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/hooks/fileChangedWatcher.ts @@ -0,0 +1,191 @@ +import chokidar, { type FSWatcher } from 'chokidar' +import { isAbsolute, join } from 'path' +import { registerCleanup } from '../cleanupRegistry.js' +import { logForDebugging } from '../debug.js' +import { errorMessage } from '../errors.js' +import { + executeCwdChangedHooks, + executeFileChangedHooks, + type HookOutsideReplResult, +} from '../hooks.js' +import { clearCwdEnvFiles } from '../sessionEnvironment.js' +import { getHooksConfigFromSnapshot } from './hooksConfigSnapshot.js' + +let watcher: FSWatcher | null = null +let currentCwd: string +let dynamicWatchPaths: string[] = [] +let dynamicWatchPathsSorted: string[] = [] +let initialized = false +let hasEnvHooks = false +let notifyCallback: ((text: string, isError: boolean) => void) | null = null + +export function setEnvHookNotifier( + cb: ((text: string, isError: boolean) => void) | null, +): void { + notifyCallback = cb +} + +export function initializeFileChangedWatcher(cwd: string): void { + if (initialized) return + initialized = true + currentCwd = cwd + + const config = getHooksConfigFromSnapshot() + hasEnvHooks = + (config?.CwdChanged?.length ?? 0) > 0 || + (config?.FileChanged?.length ?? 0) > 0 + + if (hasEnvHooks) { + registerCleanup(async () => dispose()) + } + + const paths = resolveWatchPaths(config) + if (paths.length === 0) return + + startWatching(paths) +} + +function resolveWatchPaths( + config?: ReturnType, +): string[] { + const matchers = (config ?? getHooksConfigFromSnapshot())?.FileChanged ?? [] + + // Matcher field: filenames to watch in cwd, pipe-separated (e.g. ".envrc|.env") + const staticPaths: string[] = [] + for (const m of matchers) { + if (!m.matcher) continue + for (const name of m.matcher.split('|').map(s => s.trim())) { + if (!name) continue + staticPaths.push(isAbsolute(name) ? name : join(currentCwd, name)) + } + } + + // Combine static matcher paths with dynamic paths from hook output + return [...new Set([...staticPaths, ...dynamicWatchPaths])] +} + +function startWatching(paths: string[]): void { + logForDebugging(`FileChanged: watching ${paths.length} paths`) + watcher = chokidar.watch(paths, { + persistent: true, + ignoreInitial: true, + awaitWriteFinish: { stabilityThreshold: 500, pollInterval: 200 }, + ignorePermissionErrors: true, + }) + watcher.on('change', p => handleFileEvent(p, 'change')) + watcher.on('add', p => handleFileEvent(p, 'add')) + watcher.on('unlink', p => handleFileEvent(p, 'unlink')) +} + +function handleFileEvent( + path: string, + event: 'change' | 'add' | 'unlink', +): void { + logForDebugging(`FileChanged: ${event} ${path}`) + void executeFileChangedHooks(path, event) + .then(({ results, watchPaths, systemMessages }) => { + if (watchPaths.length > 0) { + updateWatchPaths(watchPaths) + } + for (const msg of systemMessages) { + notifyCallback?.(msg, false) + } + for (const r of results) { + if (!r.succeeded && r.output) { + notifyCallback?.(r.output, true) + } + } + }) + .catch(e => { + const msg = errorMessage(e) + logForDebugging(`FileChanged hook failed: ${msg}`, { + level: 'error', + }) + notifyCallback?.(msg, true) + }) +} + +export function updateWatchPaths(paths: string[]): void { + if (!initialized) return + const sorted = paths.slice().sort() + if ( + sorted.length === dynamicWatchPathsSorted.length && + sorted.every((p, i) => p === dynamicWatchPathsSorted[i]) + ) { + return + } + dynamicWatchPaths = paths + dynamicWatchPathsSorted = sorted + restartWatching() +} + +function restartWatching(): void { + if (watcher) { + void watcher.close() + watcher = null + } + const paths = resolveWatchPaths() + if (paths.length > 0) { + startWatching(paths) + } +} + +export async function onCwdChangedForHooks( + oldCwd: string, + newCwd: string, +): Promise { + if (oldCwd === newCwd) return + + // Re-evaluate from the current snapshot so mid-session hook changes are picked up + const config = getHooksConfigFromSnapshot() + const currentHasEnvHooks = + (config?.CwdChanged?.length ?? 0) > 0 || + (config?.FileChanged?.length ?? 0) > 0 + if (!currentHasEnvHooks) return + currentCwd = newCwd + + await clearCwdEnvFiles() + const hookResult = await executeCwdChangedHooks(oldCwd, newCwd).catch(e => { + const msg = errorMessage(e) + logForDebugging(`CwdChanged hook failed: ${msg}`, { + level: 'error', + }) + notifyCallback?.(msg, true) + return { + results: [] as HookOutsideReplResult[], + watchPaths: [] as string[], + systemMessages: [] as string[], + } + }) + dynamicWatchPaths = hookResult.watchPaths + dynamicWatchPathsSorted = hookResult.watchPaths.slice().sort() + for (const msg of hookResult.systemMessages) { + notifyCallback?.(msg, false) + } + for (const r of hookResult.results) { + if (!r.succeeded && r.output) { + notifyCallback?.(r.output, true) + } + } + + // Re-resolve matcher paths against the new cwd + if (initialized) { + restartWatching() + } +} + +function dispose(): void { + if (watcher) { + void watcher.close() + watcher = null + } + dynamicWatchPaths = [] + dynamicWatchPathsSorted = [] + initialized = false + hasEnvHooks = false + notifyCallback = null +} + +export function resetFileChangedWatcherForTesting(): void { + dispose() +} diff --git a/_all-in-one-upload/cc-haha/src/utils/hooks/hooksConfigManager.ts b/_all-in-one-upload/cc-haha/src/utils/hooks/hooksConfigManager.ts new file mode 100644 index 0000000..c7c5a0d --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/hooks/hooksConfigManager.ts @@ -0,0 +1,400 @@ +import memoize from 'lodash-es/memoize.js' +import type { HookEvent } from 'src/entrypoints/agentSdkTypes.js' +import { getRegisteredHooks } from '../../bootstrap/state.js' +import type { AppState } from '../../state/AppState.js' +import { + getAllHooks, + type IndividualHookConfig, + sortMatchersByPriority, +} from './hooksSettings.js' + +export type MatcherMetadata = { + fieldToMatch: string + values: string[] +} + +export type HookEventMetadata = { + summary: string + description: string + matcherMetadata?: MatcherMetadata +} + +// Hook event metadata configuration. +// Resolver uses sorted-joined string key so that callers passing a fresh +// toolNames array each render (e.g. HooksConfigMenu) hit the cache instead +// of leaking a new entry per call. +export const getHookEventMetadata = memoize( + function (toolNames: string[]): Record { + return { + PreToolUse: { + summary: 'Before tool execution', + description: + 'Input to command is JSON of tool call arguments.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and block tool call\nOther exit codes - show stderr to user only but continue with tool call', + matcherMetadata: { + fieldToMatch: 'tool_name', + values: toolNames, + }, + }, + PostToolUse: { + summary: 'After tool execution', + description: + 'Input to command is JSON with fields "inputs" (tool call arguments) and "response" (tool call response).\nExit code 0 - stdout shown in transcript mode (ctrl+o)\nExit code 2 - show stderr to model immediately\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'tool_name', + values: toolNames, + }, + }, + PostToolUseFailure: { + summary: 'After tool execution fails', + description: + 'Input to command is JSON with tool_name, tool_input, tool_use_id, error, error_type, is_interrupt, and is_timeout.\nExit code 0 - stdout shown in transcript mode (ctrl+o)\nExit code 2 - show stderr to model immediately\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'tool_name', + values: toolNames, + }, + }, + PermissionDenied: { + summary: 'After auto mode classifier denies a tool call', + description: + 'Input to command is JSON with tool_name, tool_input, tool_use_id, and reason.\nReturn {"hookSpecificOutput":{"hookEventName":"PermissionDenied","retry":true}} to tell the model it may retry.\nExit code 0 - stdout shown in transcript mode (ctrl+o)\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'tool_name', + values: toolNames, + }, + }, + Notification: { + summary: 'When notifications are sent', + description: + 'Input to command is JSON with notification message and type.\nExit code 0 - stdout/stderr not shown\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'notification_type', + values: [ + 'permission_prompt', + 'idle_prompt', + 'auth_success', + 'elicitation_dialog', + 'elicitation_complete', + 'elicitation_response', + ], + }, + }, + UserPromptSubmit: { + summary: 'When the user submits a prompt', + description: + 'Input to command is JSON with original user prompt text.\nExit code 0 - stdout shown to Claude\nExit code 2 - block processing, erase original prompt, and show stderr to user only\nOther exit codes - show stderr to user only', + }, + SessionStart: { + summary: 'When a new session is started', + description: + 'Input to command is JSON with session start source.\nExit code 0 - stdout shown to Claude\nBlocking errors are ignored\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'source', + values: ['startup', 'resume', 'clear', 'compact'], + }, + }, + Stop: { + summary: 'Right before Claude concludes its response', + description: + 'Exit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and continue conversation\nOther exit codes - show stderr to user only', + }, + StopFailure: { + summary: 'When the turn ends due to an API error', + description: + 'Fires instead of Stop when an API error (rate limit, auth failure, etc.) ended the turn. Fire-and-forget — hook output and exit codes are ignored.', + matcherMetadata: { + fieldToMatch: 'error', + values: [ + 'rate_limit', + 'authentication_failed', + 'billing_error', + 'invalid_request', + 'server_error', + 'max_output_tokens', + 'unknown', + ], + }, + }, + SubagentStart: { + summary: 'When a subagent (Agent tool call) is started', + description: + 'Input to command is JSON with agent_id and agent_type.\nExit code 0 - stdout shown to subagent\nBlocking errors are ignored\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'agent_type', + values: [], // Will be populated with available agent types + }, + }, + SubagentStop: { + summary: + 'Right before a subagent (Agent tool call) concludes its response', + description: + 'Input to command is JSON with agent_id, agent_type, and agent_transcript_path.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to subagent and continue having it run\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'agent_type', + values: [], // Will be populated with available agent types + }, + }, + PreCompact: { + summary: 'Before conversation compaction', + description: + 'Input to command is JSON with compaction details.\nExit code 0 - stdout appended as custom compact instructions\nExit code 2 - block compaction\nOther exit codes - show stderr to user only but continue with compaction', + matcherMetadata: { + fieldToMatch: 'trigger', + values: ['manual', 'auto'], + }, + }, + PostCompact: { + summary: 'After conversation compaction', + description: + 'Input to command is JSON with compaction details and the summary.\nExit code 0 - stdout shown to user\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'trigger', + values: ['manual', 'auto'], + }, + }, + SessionEnd: { + summary: 'When a session is ending', + description: + 'Input to command is JSON with session end reason.\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'reason', + values: ['clear', 'logout', 'prompt_input_exit', 'other'], + }, + }, + PermissionRequest: { + summary: 'When a permission dialog is displayed', + description: + 'Input to command is JSON with tool_name, tool_input, and tool_use_id.\nOutput JSON with hookSpecificOutput containing decision to allow or deny.\nExit code 0 - use hook decision if provided\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'tool_name', + values: toolNames, + }, + }, + Setup: { + summary: 'Repo setup hooks for init and maintenance', + description: + 'Input to command is JSON with trigger (init or maintenance).\nExit code 0 - stdout shown to Claude\nBlocking errors are ignored\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'trigger', + values: ['init', 'maintenance'], + }, + }, + TeammateIdle: { + summary: 'When a teammate is about to go idle', + description: + 'Input to command is JSON with teammate_name and team_name.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to teammate and prevent idle (teammate continues working)\nOther exit codes - show stderr to user only', + }, + TaskCreated: { + summary: 'When a task is being created', + description: + 'Input to command is JSON with task_id, task_subject, task_description, teammate_name, and team_name.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and prevent task creation\nOther exit codes - show stderr to user only', + }, + TaskCompleted: { + summary: 'When a task is being marked as completed', + description: + 'Input to command is JSON with task_id, task_subject, task_description, teammate_name, and team_name.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and prevent task completion\nOther exit codes - show stderr to user only', + }, + Elicitation: { + summary: 'When an MCP server requests user input (elicitation)', + description: + 'Input to command is JSON with mcp_server_name, message, and requested_schema.\nOutput JSON with hookSpecificOutput containing action (accept/decline/cancel) and optional content.\nExit code 0 - use hook response if provided\nExit code 2 - deny the elicitation\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'mcp_server_name', + values: [], + }, + }, + ElicitationResult: { + summary: 'After a user responds to an MCP elicitation', + description: + 'Input to command is JSON with mcp_server_name, action, content, mode, and elicitation_id.\nOutput JSON with hookSpecificOutput containing optional action and content to override the response.\nExit code 0 - use hook response if provided\nExit code 2 - block the response (action becomes decline)\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'mcp_server_name', + values: [], + }, + }, + ConfigChange: { + summary: 'When configuration files change during a session', + description: + 'Input to command is JSON with source (user_settings, project_settings, local_settings, policy_settings, skills) and file_path.\nExit code 0 - allow the change\nExit code 2 - block the change from being applied to the session\nOther exit codes - show stderr to user only', + matcherMetadata: { + fieldToMatch: 'source', + values: [ + 'user_settings', + 'project_settings', + 'local_settings', + 'policy_settings', + 'skills', + ], + }, + }, + InstructionsLoaded: { + summary: 'When an instruction file (CLAUDE.md or rule) is loaded', + description: + 'Input to command is JSON with file_path, memory_type (User, Project, Local, Managed), load_reason (session_start, nested_traversal, path_glob_match, include, compact), globs (optional — the paths: frontmatter patterns that matched), trigger_file_path (optional — the file Claude touched that caused the load), and parent_file_path (optional — the file that @-included this one).\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only\nThis hook is observability-only and does not support blocking.', + matcherMetadata: { + fieldToMatch: 'load_reason', + values: [ + 'session_start', + 'nested_traversal', + 'path_glob_match', + 'include', + 'compact', + ], + }, + }, + WorktreeCreate: { + summary: 'Create an isolated worktree for VCS-agnostic isolation', + description: + 'Input to command is JSON with name (suggested worktree slug).\nStdout should contain the absolute path to the created worktree directory.\nExit code 0 - worktree created successfully\nOther exit codes - worktree creation failed', + }, + WorktreeRemove: { + summary: 'Remove a previously created worktree', + description: + 'Input to command is JSON with worktree_path (absolute path to worktree).\nExit code 0 - worktree removed successfully\nOther exit codes - show stderr to user only', + }, + CwdChanged: { + summary: 'After the working directory changes', + description: + 'Input to command is JSON with old_cwd and new_cwd.\nCLAUDE_ENV_FILE is set — write bash exports there to apply env to subsequent BashTool commands.\nHook output can include hookSpecificOutput.watchPaths (array of absolute paths) to register with the FileChanged watcher.\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only', + }, + FileChanged: { + summary: 'When a watched file changes', + description: + 'Input to command is JSON with file_path and event (change, add, unlink).\nCLAUDE_ENV_FILE is set — write bash exports there to apply env to subsequent BashTool commands.\nThe matcher field specifies filenames to watch in the current directory (e.g. ".envrc|.env").\nHook output can include hookSpecificOutput.watchPaths (array of absolute paths) to dynamically update the watch list.\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only', + }, + } + }, + toolNames => toolNames.slice().sort().join(','), +) + +// Group hooks by event and matcher +export function groupHooksByEventAndMatcher( + appState: AppState, + toolNames: string[], +): Record> { + const grouped: Record> = { + PreToolUse: {}, + PostToolUse: {}, + PostToolUseFailure: {}, + PermissionDenied: {}, + Notification: {}, + UserPromptSubmit: {}, + SessionStart: {}, + SessionEnd: {}, + Stop: {}, + StopFailure: {}, + SubagentStart: {}, + SubagentStop: {}, + PreCompact: {}, + PostCompact: {}, + PermissionRequest: {}, + Setup: {}, + TeammateIdle: {}, + TaskCreated: {}, + TaskCompleted: {}, + Elicitation: {}, + ElicitationResult: {}, + ConfigChange: {}, + WorktreeCreate: {}, + WorktreeRemove: {}, + InstructionsLoaded: {}, + CwdChanged: {}, + FileChanged: {}, + } + + const metadata = getHookEventMetadata(toolNames) + + // Include hooks from settings files + getAllHooks(appState).forEach(hook => { + const eventGroup = grouped[hook.event] + if (eventGroup) { + // For events without matchers, use empty string as key + const matcherKey = + metadata[hook.event].matcherMetadata !== undefined + ? hook.matcher || '' + : '' + if (!eventGroup[matcherKey]) { + eventGroup[matcherKey] = [] + } + eventGroup[matcherKey].push(hook) + } + }) + + // Include registered hooks (e.g., plugin hooks) + const registeredHooks = getRegisteredHooks() + if (registeredHooks) { + for (const [event, matchers] of Object.entries(registeredHooks)) { + const hookEvent = event as HookEvent + const eventGroup = grouped[hookEvent] + if (!eventGroup) continue + + for (const matcher of matchers) { + const matcherKey = matcher.matcher || '' + + // Only PluginHookMatcher has pluginRoot; HookCallbackMatcher (internal + // callbacks like attributionHooks, sessionFileAccessHooks) does not. + if ('pluginRoot' in matcher) { + eventGroup[matcherKey] ??= [] + for (const hook of matcher.hooks) { + eventGroup[matcherKey].push({ + event: hookEvent, + config: hook, + matcher: matcher.matcher, + source: 'pluginHook', + pluginName: matcher.pluginId, + }) + } + } else if (process.env.USER_TYPE === 'ant') { + eventGroup[matcherKey] ??= [] + for (const _hook of matcher.hooks) { + eventGroup[matcherKey].push({ + event: hookEvent, + config: { + type: 'command', + command: '[ANT-ONLY] Built-in Hook', + }, + matcher: matcher.matcher, + source: 'builtinHook', + }) + } + } + } + } + } + + return grouped +} + +// Get sorted matchers for a specific event +export function getSortedMatchersForEvent( + hooksByEventAndMatcher: Record< + HookEvent, + Record + >, + event: HookEvent, +): string[] { + const matchers = Object.keys(hooksByEventAndMatcher[event] || {}) + return sortMatchersByPriority(matchers, hooksByEventAndMatcher, event) +} + +// Get hooks for a specific event and matcher +export function getHooksForMatcher( + hooksByEventAndMatcher: Record< + HookEvent, + Record + >, + event: HookEvent, + matcher: string | null, +): IndividualHookConfig[] { + // For events without matchers, hooks are stored with empty string as key + // because the record keys must be strings. + const matcherKey = matcher ?? '' + return hooksByEventAndMatcher[event]?.[matcherKey] ?? [] +} + +// Get metadata for a specific event's matcher +export function getMatcherMetadata( + event: HookEvent, + toolNames: string[], +): MatcherMetadata | undefined { + return getHookEventMetadata(toolNames)[event].matcherMetadata +} diff --git a/_all-in-one-upload/cc-haha/src/utils/iTermBackup.ts b/_all-in-one-upload/cc-haha/src/utils/iTermBackup.ts new file mode 100644 index 0000000..10c6bbf --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/iTermBackup.ts @@ -0,0 +1,73 @@ +import { copyFile, stat } from 'fs/promises' +import { homedir } from 'os' +import { join } from 'path' +import { getGlobalConfig, saveGlobalConfig } from './config.js' +import { logError } from './log.js' + +export function markITerm2SetupComplete(): void { + saveGlobalConfig(current => ({ + ...current, + iterm2SetupInProgress: false, + })) +} + +function getIterm2RecoveryInfo(): { + inProgress: boolean + backupPath: string | null +} { + const config = getGlobalConfig() + return { + inProgress: config.iterm2SetupInProgress ?? false, + backupPath: config.iterm2BackupPath || null, + } +} + +function getITerm2PlistPath(): string { + return join( + homedir(), + 'Library', + 'Preferences', + 'com.googlecode.iterm2.plist', + ) +} + +type RestoreResult = + | { + status: 'restored' | 'no_backup' + } + | { + status: 'failed' + backupPath: string + } + +export async function checkAndRestoreITerm2Backup(): Promise { + const { inProgress, backupPath } = getIterm2RecoveryInfo() + if (!inProgress) { + return { status: 'no_backup' } + } + + if (!backupPath) { + markITerm2SetupComplete() + return { status: 'no_backup' } + } + + try { + await stat(backupPath) + } catch { + markITerm2SetupComplete() + return { status: 'no_backup' } + } + + try { + await copyFile(backupPath, getITerm2PlistPath()) + + markITerm2SetupComplete() + return { status: 'restored' } + } catch (restoreError) { + logError( + new Error(`Failed to restore iTerm2 settings with: ${restoreError}`), + ) + markITerm2SetupComplete() + return { status: 'failed', backupPath } + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/model/deprecation.ts b/_all-in-one-upload/cc-haha/src/utils/model/deprecation.ts new file mode 100644 index 0000000..a8b0ee2 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/model/deprecation.ts @@ -0,0 +1,101 @@ +/** + * Model deprecation utilities + * + * Contains information about deprecated models and their retirement dates. + */ + +import { type APIProvider, getAPIProvider } from './providers.js' + +type DeprecatedModelInfo = { + isDeprecated: true + modelName: string + retirementDate: string +} + +type NotDeprecatedInfo = { + isDeprecated: false +} + +type DeprecationInfo = DeprecatedModelInfo | NotDeprecatedInfo + +type DeprecationEntry = { + /** Human-readable model name */ + modelName: string + /** Retirement dates by provider (null = not deprecated for that provider) */ + retirementDates: Record +} + +/** + * Deprecated models and their retirement dates by provider. + * Keys are substrings to match in model IDs (case-insensitive). + * To add a new deprecated model, add an entry to this object. + */ +const DEPRECATED_MODELS: Record = { + 'claude-3-opus': { + modelName: 'Claude 3 Opus', + retirementDates: { + firstParty: 'January 5, 2026', + bedrock: 'January 15, 2026', + vertex: 'January 5, 2026', + foundry: 'January 5, 2026', + }, + }, + 'claude-3-7-sonnet': { + modelName: 'Claude 3.7 Sonnet', + retirementDates: { + firstParty: 'February 19, 2026', + bedrock: 'April 28, 2026', + vertex: 'May 11, 2026', + foundry: 'February 19, 2026', + }, + }, + 'claude-3-5-haiku': { + modelName: 'Claude 3.5 Haiku', + retirementDates: { + firstParty: 'February 19, 2026', + bedrock: null, + vertex: null, + foundry: null, + }, + }, +} + +/** + * Check if a model is deprecated and get its deprecation info + */ +function getDeprecatedModelInfo(modelId: string): DeprecationInfo { + const lowercaseModelId = modelId.toLowerCase() + const provider = getAPIProvider() + + for (const [key, value] of Object.entries(DEPRECATED_MODELS)) { + const retirementDate = value.retirementDates[provider] + if (!lowercaseModelId.includes(key) || !retirementDate) { + continue + } + return { + isDeprecated: true, + modelName: value.modelName, + retirementDate, + } + } + + return { isDeprecated: false } +} + +/** + * Get a deprecation warning message for a model, or null if not deprecated + */ +export function getModelDeprecationWarning( + modelId: string | null, +): string | null { + if (!modelId) { + return null + } + + const info = getDeprecatedModelInfo(modelId) + if (!info.isDeprecated) { + return null + } + + return `⚠ ${info.modelName} will be retired on ${info.retirementDate}. Consider switching to a newer model.` +} diff --git a/_all-in-one-upload/cc-haha/src/utils/model/modelAllowlist.ts b/_all-in-one-upload/cc-haha/src/utils/model/modelAllowlist.ts new file mode 100644 index 0000000..b3f8647 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/model/modelAllowlist.ts @@ -0,0 +1,170 @@ +import { getSettings_DEPRECATED } from '../settings/settings.js' +import { isModelAlias, isModelFamilyAlias } from './aliases.js' +import { parseUserSpecifiedModel } from './model.js' +import { resolveOverriddenModel } from './modelStrings.js' + +/** + * Check if a model belongs to a given family by checking if its name + * (or resolved name) contains the family identifier. + */ +function modelBelongsToFamily(model: string, family: string): boolean { + if (model.includes(family)) { + return true + } + // Resolve aliases like "best" → "claude-opus-4-7" to check family membership + if (isModelAlias(model)) { + const resolved = parseUserSpecifiedModel(model).toLowerCase() + return resolved.includes(family) + } + return false +} + +/** + * Check if a model name starts with a prefix at a segment boundary. + * The prefix must match up to the end of the name or a "-" separator. + * e.g. "claude-opus-4-5" matches "claude-opus-4-5-20251101" but not "claude-opus-4-50". + */ +function prefixMatchesModel(modelName: string, prefix: string): boolean { + if (!modelName.startsWith(prefix)) { + return false + } + return modelName.length === prefix.length || modelName[prefix.length] === '-' +} + +/** + * Check if a model matches a version-prefix entry in the allowlist. + * Supports shorthand like "opus-4-5" (mapped to "claude-opus-4-5") and + * full prefixes like "claude-opus-4-5". Resolves input aliases before matching. + */ +function modelMatchesVersionPrefix(model: string, entry: string): boolean { + // Resolve the input model to a full name if it's an alias + const resolvedModel = isModelAlias(model) + ? parseUserSpecifiedModel(model).toLowerCase() + : model + + // Try the entry as-is (e.g. "claude-opus-4-5") + if (prefixMatchesModel(resolvedModel, entry)) { + return true + } + // Try with "claude-" prefix (e.g. "opus-4-5" → "claude-opus-4-5") + if ( + !entry.startsWith('claude-') && + prefixMatchesModel(resolvedModel, `claude-${entry}`) + ) { + return true + } + return false +} + +/** + * Check if a family alias is narrowed by more specific entries in the allowlist. + * When the allowlist contains both "opus" and "opus-4-5", the specific entry + * takes precedence — "opus" alone would be a wildcard, but "opus-4-5" narrows + * it to only that version. + */ +function familyHasSpecificEntries( + family: string, + allowlist: string[], +): boolean { + for (const entry of allowlist) { + if (isModelFamilyAlias(entry)) { + continue + } + // Check if entry is a version-qualified variant of this family + // e.g., "opus-4-5" or "claude-opus-4-5-20251101" for the "opus" family + // Must match at a segment boundary (followed by '-' or end) to avoid + // false positives like "opusplan" matching "opus" + const idx = entry.indexOf(family) + if (idx === -1) { + continue + } + const afterFamily = idx + family.length + if (afterFamily === entry.length || entry[afterFamily] === '-') { + return true + } + } + return false +} + +/** + * Check if a model is allowed by the availableModels allowlist in settings. + * If availableModels is not set, all models are allowed. + * + * Matching tiers: + * 1. Family aliases ("opus", "sonnet", "haiku") — wildcard for the entire family, + * UNLESS more specific entries for that family also exist (e.g., "opus-4-5"). + * In that case, the family wildcard is ignored and only the specific entries apply. + * 2. Version prefixes ("opus-4-5", "claude-opus-4-5") — any build of that version + * 3. Full model IDs ("claude-opus-4-5-20251101") — exact match only + */ +export function isModelAllowed(model: string): boolean { + const settings = getSettings_DEPRECATED() || {} + const { availableModels } = settings + if (!availableModels) { + return true // No restrictions + } + if (availableModels.length === 0) { + return false // Empty allowlist blocks all user-specified models + } + + const resolvedModel = resolveOverriddenModel(model) + const normalizedModel = resolvedModel.trim().toLowerCase() + const normalizedAllowlist = availableModels.map(m => m.trim().toLowerCase()) + + // Direct match (alias-to-alias or full-name-to-full-name) + // Skip family aliases that have been narrowed by specific entries — + // e.g., "opus" in ["opus", "opus-4-5"] should NOT directly match, + // because the admin intends to restrict to opus 4.5 only. + if (normalizedAllowlist.includes(normalizedModel)) { + if ( + !isModelFamilyAlias(normalizedModel) || + !familyHasSpecificEntries(normalizedModel, normalizedAllowlist) + ) { + return true + } + } + + // Family-level aliases in the allowlist match any model in that family, + // but only if no more specific entries exist for that family. + // e.g., ["opus"] allows all opus, but ["opus", "opus-4-5"] only allows opus 4.5. + for (const entry of normalizedAllowlist) { + if ( + isModelFamilyAlias(entry) && + !familyHasSpecificEntries(entry, normalizedAllowlist) && + modelBelongsToFamily(normalizedModel, entry) + ) { + return true + } + } + + // For non-family entries, do bidirectional alias resolution + // If model is an alias, resolve it and check if the resolved name is in the list + if (isModelAlias(normalizedModel)) { + const resolved = parseUserSpecifiedModel(normalizedModel).toLowerCase() + if (normalizedAllowlist.includes(resolved)) { + return true + } + } + + // If any non-family alias in the allowlist resolves to the input model + for (const entry of normalizedAllowlist) { + if (!isModelFamilyAlias(entry) && isModelAlias(entry)) { + const resolved = parseUserSpecifiedModel(entry).toLowerCase() + if (resolved === normalizedModel) { + return true + } + } + } + + // Version-prefix matching: "opus-4-5" or "claude-opus-4-5" matches + // "claude-opus-4-5-20251101" at a segment boundary + for (const entry of normalizedAllowlist) { + if (!isModelFamilyAlias(entry) && !isModelAlias(entry)) { + if (modelMatchesVersionPrefix(normalizedModel, entry)) { + return true + } + } + } + + return false +} diff --git a/_all-in-one-upload/cc-haha/src/utils/model/modelOptions.ts b/_all-in-one-upload/cc-haha/src/utils/model/modelOptions.ts new file mode 100644 index 0000000..80e2878 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/model/modelOptions.ts @@ -0,0 +1,540 @@ +// biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered +import { getInitialMainLoopModel } from '../../bootstrap/state.js' +import { + isClaudeAISubscriber, + isMaxSubscriber, + isTeamPremiumSubscriber, +} from '../auth.js' +import { getModelStrings } from './modelStrings.js' +import { + COST_TIER_3_15, + COST_HAIKU_35, + COST_HAIKU_45, + formatModelPricing, +} from '../modelCost.js' +import { getSettings_DEPRECATED } from '../settings/settings.js' +import { checkOpus1mAccess, checkSonnet1mAccess } from './check1mAccess.js' +import { getAPIProvider } from './providers.js' +import { isModelAllowed } from './modelAllowlist.js' +import { + getCanonicalName, + getClaudeAiUserDefaultModelDescription, + getDefaultSonnetModel, + getDefaultOpusModel, + getDefaultHaikuModel, + getDefaultMainLoopModelSetting, + getMarketingNameForModel, + getUserSpecifiedModelSetting, + isOpus1mMergeEnabled, + getOpus46PricingSuffix, + renderDefaultModelSetting, + type ModelSetting, +} from './model.js' +import { has1mContext } from '../context.js' +import { getGlobalConfig } from '../config.js' + +// @[MODEL LAUNCH]: Update all the available and default model option strings below. + +export type ModelOption = { + value: ModelSetting + label: string + description: string + descriptionForModel?: string +} + +export function getDefaultOptionForUser(fastMode = false): ModelOption { + if (process.env.USER_TYPE === 'ant') { + const currentModel = renderDefaultModelSetting( + getDefaultMainLoopModelSetting(), + ) + return { + value: null, + label: 'Default (recommended)', + description: `Use the default model for Ants (currently ${currentModel})`, + descriptionForModel: `Default model (currently ${currentModel})`, + } + } + + // Subscribers + if (isClaudeAISubscriber()) { + return { + value: null, + label: 'Default (recommended)', + description: getClaudeAiUserDefaultModelDescription(fastMode), + } + } + + // PAYG + const is3P = getAPIProvider() !== 'firstParty' + return { + value: null, + label: 'Default (recommended)', + description: `Use the default model (currently ${renderDefaultModelSetting(getDefaultMainLoopModelSetting())})${is3P ? '' : ` · ${formatModelPricing(COST_TIER_3_15)}`}`, + } +} + +function getCustomSonnetOption(): ModelOption | undefined { + const is3P = getAPIProvider() !== 'firstParty' + const customSonnetModel = process.env.ANTHROPIC_DEFAULT_SONNET_MODEL + // When a 3P user has a custom sonnet model string, show it directly + if (is3P && customSonnetModel) { + const is1m = has1mContext(customSonnetModel) + return { + value: 'sonnet', + label: + process.env.ANTHROPIC_DEFAULT_SONNET_MODEL_NAME ?? customSonnetModel, + description: + process.env.ANTHROPIC_DEFAULT_SONNET_MODEL_DESCRIPTION ?? + `Custom Sonnet model${is1m ? ' (1M context)' : ''}`, + descriptionForModel: `${process.env.ANTHROPIC_DEFAULT_SONNET_MODEL_DESCRIPTION ?? `Custom Sonnet model${is1m ? ' with 1M context' : ''}`} (${customSonnetModel})`, + } + } +} + +// @[MODEL LAUNCH]: Update or add model option functions (getSonnetXXOption, getOpusXXOption, etc.) +// with the new model's label and description. These appear in the /model picker. +function getSonnet46Option(): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: is3P ? getModelStrings().sonnet46 : 'sonnet', + label: 'Sonnet', + description: `Sonnet 4.6 · Best for everyday tasks${is3P ? '' : ` · ${formatModelPricing(COST_TIER_3_15)}`}`, + descriptionForModel: + 'Sonnet 4.6 - best for everyday tasks. Generally recommended for most coding tasks', + } +} + +function getCustomOpusOption(): ModelOption | undefined { + const is3P = getAPIProvider() !== 'firstParty' + const customOpusModel = process.env.ANTHROPIC_DEFAULT_OPUS_MODEL + // When a 3P user has a custom opus model string, show it directly + if (is3P && customOpusModel) { + const is1m = has1mContext(customOpusModel) + return { + value: 'opus', + label: process.env.ANTHROPIC_DEFAULT_OPUS_MODEL_NAME ?? customOpusModel, + description: + process.env.ANTHROPIC_DEFAULT_OPUS_MODEL_DESCRIPTION ?? + `Custom Opus model${is1m ? ' (1M context)' : ''}`, + descriptionForModel: `${process.env.ANTHROPIC_DEFAULT_OPUS_MODEL_DESCRIPTION ?? `Custom Opus model${is1m ? ' with 1M context' : ''}`} (${customOpusModel})`, + } + } +} + +function getOpus41Option(): ModelOption { + return { + value: 'opus', + label: 'Opus 4.1', + description: `Opus 4.1 · Legacy`, + descriptionForModel: 'Opus 4.1 - legacy version', + } +} + +function getOpus46Option(fastMode = false): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: is3P ? getModelStrings().opus46 : 'opus', + label: 'Opus', + description: `Opus 4.7 · Most capable for complex work${getOpus46PricingSuffix(fastMode)}`, + descriptionForModel: 'Opus 4.7 - most capable for complex work', + } +} + +export function getSonnet46_1MOption(): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: is3P ? getModelStrings().sonnet46 + '[1m]' : 'sonnet[1m]', + label: 'Sonnet (1M context)', + description: `Sonnet 4.6 for long sessions${is3P ? '' : ` · ${formatModelPricing(COST_TIER_3_15)}`}`, + descriptionForModel: + 'Sonnet 4.6 with 1M context window - for long sessions with large codebases', + } +} + +export function getOpus46_1MOption(fastMode = false): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: is3P ? getModelStrings().opus46 + '[1m]' : 'opus[1m]', + label: 'Opus (1M context)', + description: `Opus 4.7 for long sessions${getOpus46PricingSuffix(fastMode)}`, + descriptionForModel: + 'Opus 4.7 with 1M context window - for long sessions with large codebases', + } +} + +function getCustomHaikuOption(): ModelOption | undefined { + const is3P = getAPIProvider() !== 'firstParty' + const customHaikuModel = process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL + // When a 3P user has a custom haiku model string, show it directly + if (is3P && customHaikuModel) { + return { + value: 'haiku', + label: process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME ?? customHaikuModel, + description: + process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL_DESCRIPTION ?? + 'Custom Haiku model', + descriptionForModel: `${process.env.ANTHROPIC_DEFAULT_HAIKU_MODEL_DESCRIPTION ?? 'Custom Haiku model'} (${customHaikuModel})`, + } + } +} + +function getHaiku45Option(): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: 'haiku', + label: 'Haiku', + description: `Haiku 4.5 · Fastest for quick answers${is3P ? '' : ` · ${formatModelPricing(COST_HAIKU_45)}`}`, + descriptionForModel: + 'Haiku 4.5 - fastest for quick answers. Lower cost but less capable than Sonnet 4.6.', + } +} + +function getHaiku35Option(): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: 'haiku', + label: 'Haiku', + description: `Haiku 3.5 for simple tasks${is3P ? '' : ` · ${formatModelPricing(COST_HAIKU_35)}`}`, + descriptionForModel: + 'Haiku 3.5 - faster and lower cost, but less capable than Sonnet. Use for simple tasks.', + } +} + +function getHaikuOption(): ModelOption { + // Return correct Haiku option based on provider + const haikuModel = getDefaultHaikuModel() + return haikuModel === getModelStrings().haiku45 + ? getHaiku45Option() + : getHaiku35Option() +} + +function getMaxOpusOption(fastMode = false): ModelOption { + return { + value: 'opus', + label: 'Opus', + description: `Opus 4.7 · Most capable for complex work${fastMode ? getOpus46PricingSuffix(true) : ''}`, + } +} + +export function getMaxSonnet46_1MOption(): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + const billingInfo = isClaudeAISubscriber() ? ' · Billed as extra usage' : '' + return { + value: 'sonnet[1m]', + label: 'Sonnet (1M context)', + description: `Sonnet 4.6 with 1M context${billingInfo}${is3P ? '' : ` · ${formatModelPricing(COST_TIER_3_15)}`}`, + } +} + +export function getMaxOpus46_1MOption(fastMode = false): ModelOption { + const billingInfo = isClaudeAISubscriber() ? ' · Billed as extra usage' : '' + return { + value: 'opus[1m]', + label: 'Opus (1M context)', + description: `Opus 4.7 with 1M context${billingInfo}${getOpus46PricingSuffix(fastMode)}`, + } +} + +function getMergedOpus1MOption(fastMode = false): ModelOption { + const is3P = getAPIProvider() !== 'firstParty' + return { + value: is3P ? getModelStrings().opus46 + '[1m]' : 'opus[1m]', + label: 'Opus (1M context)', + description: `Opus 4.7 with 1M context · Most capable for complex work${!is3P && fastMode ? getOpus46PricingSuffix(fastMode) : ''}`, + descriptionForModel: + 'Opus 4.7 with 1M context - most capable for complex work', + } +} + +const MaxSonnet46Option: ModelOption = { + value: 'sonnet', + label: 'Sonnet', + description: 'Sonnet 4.6 · Best for everyday tasks', +} + +const MaxHaiku45Option: ModelOption = { + value: 'haiku', + label: 'Haiku', + description: 'Haiku 4.5 · Fastest for quick answers', +} + +function getOpusPlanOption(): ModelOption { + return { + value: 'opusplan', + label: 'Opus Plan Mode', + description: 'Use Opus 4.7 in plan mode, Sonnet 4.6 otherwise', + } +} + +// @[MODEL LAUNCH]: Update the model picker lists below to include/reorder options for the new model. +// Each user tier (ant, Max/Team Premium, Pro/Team Standard/Enterprise, PAYG 1P, PAYG 3P) has its own list. +function getModelOptionsBase(fastMode = false): ModelOption[] { + if (process.env.USER_TYPE === 'ant') { + // Build options from antModels config + const antModelOptions: ModelOption[] = getAntModels().map(m => ({ + value: m.alias, + label: m.label, + description: m.description ?? `[ANT-ONLY] ${m.label} (${m.model})`, + })) + + return [ + getDefaultOptionForUser(), + ...antModelOptions, + getMergedOpus1MOption(fastMode), + getSonnet46Option(), + getSonnet46_1MOption(), + getHaiku45Option(), + ] + } + + if (isClaudeAISubscriber()) { + if (isMaxSubscriber() || isTeamPremiumSubscriber()) { + // Max and Team Premium users: Opus is default, show Sonnet as alternative + const premiumOptions = [getDefaultOptionForUser(fastMode)] + if (!isOpus1mMergeEnabled() && checkOpus1mAccess()) { + premiumOptions.push(getMaxOpus46_1MOption(fastMode)) + } + + premiumOptions.push(MaxSonnet46Option) + if (checkSonnet1mAccess()) { + premiumOptions.push(getMaxSonnet46_1MOption()) + } + + premiumOptions.push(MaxHaiku45Option) + return premiumOptions + } + + // Pro/Team Standard/Enterprise users: Sonnet is default, show Opus as alternative + const standardOptions = [getDefaultOptionForUser(fastMode)] + if (checkSonnet1mAccess()) { + standardOptions.push(getMaxSonnet46_1MOption()) + } + + if (isOpus1mMergeEnabled()) { + standardOptions.push(getMergedOpus1MOption(fastMode)) + } else { + standardOptions.push(getMaxOpusOption(fastMode)) + if (checkOpus1mAccess()) { + standardOptions.push(getMaxOpus46_1MOption(fastMode)) + } + } + + standardOptions.push(MaxHaiku45Option) + return standardOptions + } + + // PAYG 1P API: Default (Sonnet) + Sonnet 1M + Opus 4.7 + Opus 1M + Haiku + if (getAPIProvider() === 'firstParty') { + const payg1POptions = [getDefaultOptionForUser(fastMode)] + if (checkSonnet1mAccess()) { + payg1POptions.push(getSonnet46_1MOption()) + } + if (isOpus1mMergeEnabled()) { + payg1POptions.push(getMergedOpus1MOption(fastMode)) + } else { + payg1POptions.push(getOpus46Option(fastMode)) + if (checkOpus1mAccess()) { + payg1POptions.push(getOpus46_1MOption(fastMode)) + } + } + payg1POptions.push(getHaiku45Option()) + return payg1POptions + } + + // PAYG 3P: Default (Sonnet 4.5) + Sonnet (3P custom) or Sonnet 4.6/1M + Opus (3P custom) or Opus 4.1/Opus 4.7/Opus1M + Haiku + Opus 4.1 + const payg3pOptions = [getDefaultOptionForUser(fastMode)] + + const customSonnet = getCustomSonnetOption() + if (customSonnet !== undefined) { + payg3pOptions.push(customSonnet) + } else { + // Add Sonnet 4.6 since Sonnet 4.5 is the default + payg3pOptions.push(getSonnet46Option()) + if (checkSonnet1mAccess()) { + payg3pOptions.push(getSonnet46_1MOption()) + } + } + + const customOpus = getCustomOpusOption() + if (customOpus !== undefined) { + payg3pOptions.push(customOpus) + } else { + // Add Opus 4.1, Opus 4.7 and Opus 4.7 1M + payg3pOptions.push(getOpus41Option()) // This is the default opus + payg3pOptions.push(getOpus46Option(fastMode)) + if (checkOpus1mAccess()) { + payg3pOptions.push(getOpus46_1MOption(fastMode)) + } + } + const customHaiku = getCustomHaikuOption() + if (customHaiku !== undefined) { + payg3pOptions.push(customHaiku) + } else { + payg3pOptions.push(getHaikuOption()) + } + return payg3pOptions +} + +// @[MODEL LAUNCH]: Add the new model ID to the appropriate family pattern below +// so the "newer version available" hint works correctly. +/** + * Map a full model name to its family alias and the marketing name of the + * version the alias currently resolves to. Used to detect when a user has + * a specific older version pinned and a newer one is available. + */ +function getModelFamilyInfo( + model: string, +): { alias: string; currentVersionName: string } | null { + const canonical = getCanonicalName(model) + + // Sonnet family + if ( + canonical.includes('claude-sonnet-4-6') || + canonical.includes('claude-sonnet-4-5') || + canonical.includes('claude-sonnet-4-') || + canonical.includes('claude-3-7-sonnet') || + canonical.includes('claude-3-5-sonnet') + ) { + const currentName = getMarketingNameForModel(getDefaultSonnetModel()) + if (currentName) { + return { alias: 'Sonnet', currentVersionName: currentName } + } + } + + // Opus family + if (canonical.includes('claude-opus-4')) { + const currentName = getMarketingNameForModel(getDefaultOpusModel()) + if (currentName) { + return { alias: 'Opus', currentVersionName: currentName } + } + } + + // Haiku family + if ( + canonical.includes('claude-haiku') || + canonical.includes('claude-3-5-haiku') + ) { + const currentName = getMarketingNameForModel(getDefaultHaikuModel()) + if (currentName) { + return { alias: 'Haiku', currentVersionName: currentName } + } + } + + return null +} + +/** + * Returns a ModelOption for a known Anthropic model with a human-readable + * label, and an upgrade hint if a newer version is available via the alias. + * Returns null if the model is not recognized. + */ +function getKnownModelOption(model: string): ModelOption | null { + const marketingName = getMarketingNameForModel(model) + if (!marketingName) return null + + const familyInfo = getModelFamilyInfo(model) + if (!familyInfo) { + return { + value: model, + label: marketingName, + description: model, + } + } + + // Check if the alias currently resolves to a different (newer) version + if (marketingName !== familyInfo.currentVersionName) { + return { + value: model, + label: marketingName, + description: `Newer version available · select ${familyInfo.alias} for ${familyInfo.currentVersionName}`, + } + } + + // Same version as the alias — just show the friendly name + return { + value: model, + label: marketingName, + description: model, + } +} + +export function getModelOptions(fastMode = false): ModelOption[] { + const options = getModelOptionsBase(fastMode) + + // Add the custom model from the ANTHROPIC_CUSTOM_MODEL_OPTION env var + const envCustomModel = process.env.ANTHROPIC_CUSTOM_MODEL_OPTION + if ( + envCustomModel && + !options.some(existing => existing.value === envCustomModel) + ) { + options.push({ + value: envCustomModel, + label: process.env.ANTHROPIC_CUSTOM_MODEL_OPTION_NAME ?? envCustomModel, + description: + process.env.ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION ?? + `Custom model (${envCustomModel})`, + }) + } + + // Append additional model options fetched during bootstrap + for (const opt of getGlobalConfig().additionalModelOptionsCache ?? []) { + if (!options.some(existing => existing.value === opt.value)) { + options.push(opt) + } + } + + // Add custom model from either the current model value or the initial one + // if it is not already in the options. + let customModel: ModelSetting = null + const currentMainLoopModel = getUserSpecifiedModelSetting() + const initialMainLoopModel = getInitialMainLoopModel() + if (currentMainLoopModel !== undefined && currentMainLoopModel !== null) { + customModel = currentMainLoopModel + } else if (initialMainLoopModel !== null) { + customModel = initialMainLoopModel + } + if (customModel === null || options.some(opt => opt.value === customModel)) { + return filterModelOptionsByAllowlist(options) + } else if (customModel === 'opusplan') { + return filterModelOptionsByAllowlist([...options, getOpusPlanOption()]) + } else if (customModel === 'opus' && getAPIProvider() === 'firstParty') { + return filterModelOptionsByAllowlist([ + ...options, + getMaxOpusOption(fastMode), + ]) + } else if (customModel === 'opus[1m]' && getAPIProvider() === 'firstParty') { + return filterModelOptionsByAllowlist([ + ...options, + getMergedOpus1MOption(fastMode), + ]) + } else { + // Try to show a human-readable label for known Anthropic models, with an + // upgrade hint if the alias now resolves to a newer version. + const knownOption = getKnownModelOption(customModel) + if (knownOption) { + options.push(knownOption) + } else { + options.push({ + value: customModel, + label: customModel, + description: 'Custom model', + }) + } + return filterModelOptionsByAllowlist(options) + } +} + +/** + * Filter model options by the availableModels allowlist. + * Always preserves the "Default" option (value: null). + */ +function filterModelOptionsByAllowlist(options: ModelOption[]): ModelOption[] { + const settings = getSettings_DEPRECATED() || {} + if (!settings.availableModels) { + return options // No restrictions + } + return options.filter( + opt => + opt.value === null || (opt.value !== null && isModelAllowed(opt.value)), + ) +} diff --git a/_all-in-one-upload/cc-haha/src/utils/modifiers.ts b/_all-in-one-upload/cc-haha/src/utils/modifiers.ts new file mode 100644 index 0000000..02d8c52 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/modifiers.ts @@ -0,0 +1,40 @@ +export type ModifierKey = 'shift' | 'command' | 'control' | 'option' + +let prewarmed = false + +/** + * Pre-warm the native module by loading it in advance. + * Call this early to avoid delay on first use. + */ +export function prewarmModifiers(): void { + if (prewarmed || process.platform !== 'darwin') { + return + } + prewarmed = true + // Load module in background + try { + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { prewarm } = require('modifiers-napi') as { prewarm: () => void } + prewarm() + } catch { + // Ignore errors during prewarm + } +} + +/** + * Check if a specific modifier key is currently pressed (synchronous). + */ +export function isModifierPressed(modifier: ModifierKey): boolean { + if (process.platform !== 'darwin') { + return false + } + try { + // Dynamic import to avoid loading native module at top level + const { isModifierPressed: nativeIsModifierPressed } = + // eslint-disable-next-line @typescript-eslint/no-require-imports + require('modifiers-napi') as { isModifierPressed: (m: string) => boolean } + return nativeIsModifierPressed(modifier) + } catch { + return false + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/nativeInstaller/download.ts b/_all-in-one-upload/cc-haha/src/utils/nativeInstaller/download.ts new file mode 100644 index 0000000..fa11e67 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/nativeInstaller/download.ts @@ -0,0 +1,523 @@ +/** + * Download functionality for native installer + * + * Handles downloading Claude binaries from various sources: + * - Artifactory NPM packages + * - GCS bucket + */ + +import { feature } from 'bun:bundle' +import axios from 'axios' +import { createHash } from 'crypto' +import { chmod, writeFile } from 'fs/promises' +import { join } from 'path' +import { logEvent } from 'src/services/analytics/index.js' +import type { ReleaseChannel } from '../config.js' +import { logForDebugging } from '../debug.js' +import { toError } from '../errors.js' +import { execFileNoThrowWithCwd } from '../execFileNoThrow.js' +import { getFsImplementation } from '../fsOperations.js' +import { logError } from '../log.js' +import { sleep } from '../sleep.js' +import { jsonStringify, writeFileSync_DEPRECATED } from '../slowOperations.js' +import { getBinaryName, getPlatform } from './installer.js' + +const GCS_BUCKET_URL = + 'https://storage.googleapis.com/claude-code-dist-86c565f3-f756-42ad-8dfa-d59b1c096819/claude-code-releases' +export const ARTIFACTORY_REGISTRY_URL = + 'https://artifactory.infra.ant.dev/artifactory/api/npm/npm-all/' + +export async function getLatestVersionFromArtifactory( + tag: string = 'latest', +): Promise { + const startTime = Date.now() + const { stdout, code, stderr } = await execFileNoThrowWithCwd( + 'npm', + [ + 'view', + `${MACRO.NATIVE_PACKAGE_URL}@${tag}`, + 'version', + '--prefer-online', + '--registry', + ARTIFACTORY_REGISTRY_URL, + ], + { + timeout: 30000, + preserveOutputOnError: true, + }, + ) + + const latencyMs = Date.now() - startTime + + if (code !== 0) { + logEvent('tengu_version_check_failure', { + latency_ms: latencyMs, + source_npm: true, + exit_code: code, + }) + const error = new Error(`npm view failed with code ${code}: ${stderr}`) + logError(error) + throw error + } + + logEvent('tengu_version_check_success', { + latency_ms: latencyMs, + source_npm: true, + }) + logForDebugging( + `npm view ${MACRO.NATIVE_PACKAGE_URL}@${tag} version: ${stdout}`, + ) + const latestVersion = stdout.trim() + return latestVersion +} + +export async function getLatestVersionFromBinaryRepo( + channel: ReleaseChannel = 'latest', + baseUrl: string, + authConfig?: { auth: { username: string; password: string } }, +): Promise { + const startTime = Date.now() + try { + const response = await axios.get(`${baseUrl}/${channel}`, { + timeout: 30000, + responseType: 'text', + ...authConfig, + }) + const latencyMs = Date.now() - startTime + logEvent('tengu_version_check_success', { + latency_ms: latencyMs, + }) + return response.data.trim() + } catch (error) { + const latencyMs = Date.now() - startTime + const errorMessage = error instanceof Error ? error.message : String(error) + let httpStatus: number | undefined + if (axios.isAxiosError(error) && error.response) { + httpStatus = error.response.status + } + + logEvent('tengu_version_check_failure', { + latency_ms: latencyMs, + http_status: httpStatus, + is_timeout: errorMessage.includes('timeout'), + }) + const fetchError = new Error( + `Failed to fetch version from ${baseUrl}/${channel}: ${errorMessage}`, + ) + logError(fetchError) + throw fetchError + } +} + +export async function getLatestVersion( + channelOrVersion: string, +): Promise { + // Direct version - match internal format too (e.g. 1.0.30-dev.shaf4937ce) + if (/^v?\d+\.\d+\.\d+(-\S+)?$/.test(channelOrVersion)) { + const normalized = channelOrVersion.startsWith('v') + ? channelOrVersion.slice(1) + : channelOrVersion + // 99.99.x is reserved for CI smoke-test fixtures on real GCS. + // feature() is false in all shipped builds — DCE collapses this to an + // unconditional throw. Only `bun --feature=ALLOW_TEST_VERSIONS` (the + // smoke test's source-level invocation) bypasses. + if (/^99\.99\./.test(normalized) && !feature('ALLOW_TEST_VERSIONS')) { + throw new Error( + `Version ${normalized} is not available for installation. Use 'stable' or 'latest'.`, + ) + } + return normalized + } + + // ReleaseChannel validation + const channel = channelOrVersion as ReleaseChannel + if (channel !== 'stable' && channel !== 'latest') { + throw new Error( + `Invalid channel: ${channelOrVersion}. Use 'stable' or 'latest'`, + ) + } + + // Route to appropriate source + if (process.env.USER_TYPE === 'ant') { + // Use Artifactory for ant users + const npmTag = channel === 'stable' ? 'stable' : 'latest' + return getLatestVersionFromArtifactory(npmTag) + } + + // Use GCS for external users + return getLatestVersionFromBinaryRepo(channel, GCS_BUCKET_URL) +} + +export async function downloadVersionFromArtifactory( + version: string, + stagingPath: string, +) { + const fs = getFsImplementation() + + // If we get here, we own the lock and can delete a partial download + await fs.rm(stagingPath, { recursive: true, force: true }) + + // Get the platform-specific package name + const platform = getPlatform() + const platformPackageName = `${MACRO.NATIVE_PACKAGE_URL}-${platform}` + + // Fetch integrity hash for the platform-specific package + logForDebugging( + `Fetching integrity hash for ${platformPackageName}@${version}`, + ) + const { + stdout: integrityOutput, + code, + stderr, + } = await execFileNoThrowWithCwd( + 'npm', + [ + 'view', + `${platformPackageName}@${version}`, + 'dist.integrity', + '--registry', + ARTIFACTORY_REGISTRY_URL, + ], + { + timeout: 30000, + preserveOutputOnError: true, + }, + ) + + if (code !== 0) { + throw new Error(`npm view integrity failed with code ${code}: ${stderr}`) + } + + const integrity = integrityOutput.trim() + if (!integrity) { + throw new Error( + `Failed to fetch integrity hash for ${platformPackageName}@${version}`, + ) + } + + logForDebugging(`Got integrity hash for ${platform}: ${integrity}`) + + // Create isolated npm project in staging + await fs.mkdir(stagingPath) + + const packageJson = { + name: 'claude-native-installer', + version: '0.0.1', + dependencies: { + [MACRO.NATIVE_PACKAGE_URL!]: version, + }, + } + + // Create package-lock.json with integrity verification for platform-specific package + const packageLock = { + name: 'claude-native-installer', + version: '0.0.1', + lockfileVersion: 3, + requires: true, + packages: { + '': { + name: 'claude-native-installer', + version: '0.0.1', + dependencies: { + [MACRO.NATIVE_PACKAGE_URL!]: version, + }, + }, + [`node_modules/${MACRO.NATIVE_PACKAGE_URL}`]: { + version: version, + optionalDependencies: { + [platformPackageName]: version, + }, + }, + [`node_modules/${platformPackageName}`]: { + version: version, + integrity: integrity, + }, + }, + } + + writeFileSync_DEPRECATED( + join(stagingPath, 'package.json'), + jsonStringify(packageJson, null, 2), + { encoding: 'utf8', flush: true }, + ) + + writeFileSync_DEPRECATED( + join(stagingPath, 'package-lock.json'), + jsonStringify(packageLock, null, 2), + { encoding: 'utf8', flush: true }, + ) + + // Install with npm - it will verify integrity from package-lock.json + // Use --prefer-online to force fresh metadata checks, helping with Artifactory replication delays + const result = await execFileNoThrowWithCwd( + 'npm', + ['ci', '--prefer-online', '--registry', ARTIFACTORY_REGISTRY_URL], + { + timeout: 60000, + preserveOutputOnError: true, + cwd: stagingPath, + }, + ) + + if (result.code !== 0) { + throw new Error(`npm ci failed with code ${result.code}: ${result.stderr}`) + } + + logForDebugging( + `Successfully downloaded and verified ${MACRO.NATIVE_PACKAGE_URL}@${version}`, + ) +} + +// Stall timeout: abort if no bytes received for this duration +const DEFAULT_STALL_TIMEOUT_MS = 60000 // 60 seconds +const MAX_DOWNLOAD_RETRIES = 3 + +function getStallTimeoutMs(): number { + return ( + Number(process.env.CLAUDE_CODE_STALL_TIMEOUT_MS_FOR_TESTING) || + DEFAULT_STALL_TIMEOUT_MS + ) +} + +class StallTimeoutError extends Error { + constructor() { + super('Download stalled: no data received for 60 seconds') + this.name = 'StallTimeoutError' + } +} + +/** + * Common logic for downloading and verifying a binary. + * Includes stall detection (aborts if no bytes for 60s) and retry logic. + */ +async function downloadAndVerifyBinary( + binaryUrl: string, + expectedChecksum: string, + binaryPath: string, + requestConfig: Record = {}, +) { + let lastError: Error | undefined + + for (let attempt = 1; attempt <= MAX_DOWNLOAD_RETRIES; attempt++) { + const controller = new AbortController() + let stallTimer: ReturnType | undefined + + const clearStallTimer = () => { + if (stallTimer) { + clearTimeout(stallTimer) + stallTimer = undefined + } + } + + const resetStallTimer = () => { + clearStallTimer() + stallTimer = setTimeout(c => c.abort(), getStallTimeoutMs(), controller) + } + + try { + // Start the stall timer before the request + resetStallTimer() + + const response = await axios.get(binaryUrl, { + timeout: 5 * 60000, // 5 minute total timeout + responseType: 'arraybuffer', + signal: controller.signal, + onDownloadProgress: () => { + // Reset stall timer on each chunk of data received + resetStallTimer() + }, + ...requestConfig, + }) + + clearStallTimer() + + // Verify checksum + const hash = createHash('sha256') + hash.update(response.data) + const actualChecksum = hash.digest('hex') + + if (actualChecksum !== expectedChecksum) { + throw new Error( + `Checksum mismatch: expected ${expectedChecksum}, got ${actualChecksum}`, + ) + } + + // Write binary to disk + await writeFile(binaryPath, Buffer.from(response.data)) + await chmod(binaryPath, 0o755) + + // Success - return early + return + } catch (error) { + clearStallTimer() + + // Check if this was a stall timeout (axios wraps abort signals in CanceledError) + const isStallTimeout = axios.isCancel(error) + + if (isStallTimeout) { + lastError = new StallTimeoutError() + } else { + lastError = toError(error) + } + + // Only retry on stall timeouts + if (isStallTimeout && attempt < MAX_DOWNLOAD_RETRIES) { + logForDebugging( + `Download stalled on attempt ${attempt}/${MAX_DOWNLOAD_RETRIES}, retrying...`, + ) + // Brief pause before retry to let network recover + await sleep(1000) + continue + } + + // Don't retry other errors (HTTP errors, checksum mismatches, etc.) + throw lastError + } + } + + // Should not reach here, but just in case + throw lastError ?? new Error('Download failed after all retries') +} + +export async function downloadVersionFromBinaryRepo( + version: string, + stagingPath: string, + baseUrl: string, + authConfig?: { + auth?: { username: string; password: string } + headers?: Record + }, +) { + const fs = getFsImplementation() + + // If we get here, we own the lock and can delete a partial download + await fs.rm(stagingPath, { recursive: true, force: true }) + + // Get platform + const platform = getPlatform() + const startTime = Date.now() + + // Log download attempt start + logEvent('tengu_binary_download_attempt', {}) + + // Fetch manifest to get checksum + let manifest + try { + const manifestResponse = await axios.get( + `${baseUrl}/${version}/manifest.json`, + { + timeout: 10000, + responseType: 'json', + ...authConfig, + }, + ) + manifest = manifestResponse.data + } catch (error) { + const latencyMs = Date.now() - startTime + const errorMessage = error instanceof Error ? error.message : String(error) + let httpStatus: number | undefined + if (axios.isAxiosError(error) && error.response) { + httpStatus = error.response.status + } + + logEvent('tengu_binary_manifest_fetch_failure', { + latency_ms: latencyMs, + http_status: httpStatus, + is_timeout: errorMessage.includes('timeout'), + }) + logError( + new Error( + `Failed to fetch manifest from ${baseUrl}/${version}/manifest.json: ${errorMessage}`, + ), + ) + throw error + } + + const platformInfo = manifest.platforms[platform] + + if (!platformInfo) { + logEvent('tengu_binary_platform_not_found', {}) + throw new Error( + `Platform ${platform} not found in manifest for version ${version}`, + ) + } + + const expectedChecksum = platformInfo.checksum + + // Both GCS and generic bucket use identical layout: ${baseUrl}/${version}/${platform}/${binaryName} + const binaryName = getBinaryName(platform) + const binaryUrl = `${baseUrl}/${version}/${platform}/${binaryName}` + + // Write to staging + await fs.mkdir(stagingPath) + const binaryPath = join(stagingPath, binaryName) + + try { + await downloadAndVerifyBinary( + binaryUrl, + expectedChecksum, + binaryPath, + authConfig || {}, + ) + const latencyMs = Date.now() - startTime + logEvent('tengu_binary_download_success', { + latency_ms: latencyMs, + }) + } catch (error) { + const latencyMs = Date.now() - startTime + const errorMessage = error instanceof Error ? error.message : String(error) + let httpStatus: number | undefined + if (axios.isAxiosError(error) && error.response) { + httpStatus = error.response.status + } + + logEvent('tengu_binary_download_failure', { + latency_ms: latencyMs, + http_status: httpStatus, + is_timeout: errorMessage.includes('timeout'), + is_checksum_mismatch: errorMessage.includes('Checksum mismatch'), + }) + logError( + new Error(`Failed to download binary from ${binaryUrl}: ${errorMessage}`), + ) + throw error + } +} + +export async function downloadVersion( + version: string, + stagingPath: string, +): Promise<'npm' | 'binary'> { + // Test-fixture versions route to the private sentinel bucket. DCE'd in all + // shipped builds — the string 'claude-code-ci-sentinel' and the gcloud call + // never exist in compiled binaries. Same gcloud-token pattern as + // remoteSkillLoader.ts:175-195. + if (feature('ALLOW_TEST_VERSIONS') && /^99\.99\./.test(version)) { + const { stdout } = await execFileNoThrowWithCwd('gcloud', [ + 'auth', + 'print-access-token', + ]) + await downloadVersionFromBinaryRepo( + version, + stagingPath, + 'https://storage.googleapis.com/claude-code-ci-sentinel', + { headers: { Authorization: `Bearer ${stdout.trim()}` } }, + ) + return 'binary' + } + + if (process.env.USER_TYPE === 'ant') { + // Use Artifactory for ant users + await downloadVersionFromArtifactory(version, stagingPath) + return 'npm' + } + + // Use GCS for external users + await downloadVersionFromBinaryRepo(version, stagingPath, GCS_BUCKET_URL) + return 'binary' +} + +// Exported for testing +export { StallTimeoutError, MAX_DOWNLOAD_RETRIES } +export const STALL_TIMEOUT_MS = DEFAULT_STALL_TIMEOUT_MS +export const _downloadAndVerifyBinaryForTesting = downloadAndVerifyBinary diff --git a/_all-in-one-upload/cc-haha/src/utils/pdf.ts b/_all-in-one-upload/cc-haha/src/utils/pdf.ts new file mode 100644 index 0000000..65dc506 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/pdf.ts @@ -0,0 +1,300 @@ +import { randomUUID } from 'crypto' +import { mkdir, readdir, readFile } from 'fs/promises' +import { join } from 'path' +import { + PDF_MAX_EXTRACT_SIZE, + PDF_TARGET_RAW_SIZE, +} from '../constants/apiLimits.js' +import { errorMessage } from './errors.js' +import { execFileNoThrow } from './execFileNoThrow.js' +import { formatFileSize } from './format.js' +import { getFsImplementation } from './fsOperations.js' +import { getToolResultsDir } from './toolResultStorage.js' + +export type PDFError = { + reason: + | 'empty' + | 'too_large' + | 'password_protected' + | 'corrupted' + | 'unknown' + | 'unavailable' + message: string +} + +export type PDFResult = + | { success: true; data: T } + | { success: false; error: PDFError } + +/** + * Read a PDF file and return it as base64-encoded data. + * @param filePath Path to the PDF file + * @returns Result containing PDF data or a structured error + */ +export async function readPDF(filePath: string): Promise< + PDFResult<{ + type: 'pdf' + file: { + filePath: string + base64: string + originalSize: number + } + }> +> { + try { + const fs = getFsImplementation() + const stats = await fs.stat(filePath) + const originalSize = stats.size + + // Check if file is empty + if (originalSize === 0) { + return { + success: false, + error: { reason: 'empty', message: `PDF file is empty: ${filePath}` }, + } + } + + // Check if PDF exceeds maximum size + // The API has a 32MB total request limit. After base64 encoding (~33% larger), + // a PDF must be under ~20MB raw to leave room for conversation context. + if (originalSize > PDF_TARGET_RAW_SIZE) { + return { + success: false, + error: { + reason: 'too_large', + message: `PDF file exceeds maximum allowed size of ${formatFileSize(PDF_TARGET_RAW_SIZE)}.`, + }, + } + } + + const fileBuffer = await readFile(filePath) + + // Validate PDF magic bytes — reject files that aren't actually PDFs + // (e.g., HTML files renamed to .pdf) before they enter conversation context. + // Once an invalid PDF document block is in the message history, every subsequent + // API call fails with 400 "The PDF specified was not valid" and the session + // becomes unrecoverable without /clear. + const header = fileBuffer.subarray(0, 5).toString('ascii') + if (!header.startsWith('%PDF-')) { + return { + success: false, + error: { + reason: 'corrupted', + message: `File is not a valid PDF (missing %PDF- header): ${filePath}`, + }, + } + } + + const base64 = fileBuffer.toString('base64') + + // Note: We cannot check page count here without parsing the PDF + // The API will enforce the 100-page limit and return an error if exceeded + + return { + success: true, + data: { + type: 'pdf', + file: { + filePath, + base64, + originalSize, + }, + }, + } + } catch (e: unknown) { + return { + success: false, + error: { + reason: 'unknown', + message: errorMessage(e), + }, + } + } +} + +/** + * Get the number of pages in a PDF file using `pdfinfo` (from poppler-utils). + * Returns `null` if pdfinfo is not available or if the page count cannot be determined. + */ +export async function getPDFPageCount( + filePath: string, +): Promise { + const { code, stdout } = await execFileNoThrow('pdfinfo', [filePath], { + timeout: 10_000, + useCwd: false, + }) + if (code !== 0) { + return null + } + const match = /^Pages:\s+(\d+)/m.exec(stdout) + if (!match) { + return null + } + const count = parseInt(match[1]!, 10) + return isNaN(count) ? null : count +} + +export type PDFExtractPagesResult = { + type: 'parts' + file: { + filePath: string + originalSize: number + count: number + outputDir: string + } +} + +let pdftoppmAvailable: boolean | undefined + +/** + * Reset the pdftoppm availability cache. Used by tests only. + */ +export function resetPdftoppmCache(): void { + pdftoppmAvailable = undefined +} + +/** + * Check whether the `pdftoppm` binary (from poppler-utils) is available. + * The result is cached for the lifetime of the process. + */ +export async function isPdftoppmAvailable(): Promise { + if (pdftoppmAvailable !== undefined) return pdftoppmAvailable + const { code, stderr } = await execFileNoThrow('pdftoppm', ['-v'], { + timeout: 5000, + useCwd: false, + }) + // pdftoppm prints version info to stderr and exits 0 (or sometimes 99 on older versions) + pdftoppmAvailable = code === 0 || stderr.length > 0 + return pdftoppmAvailable +} + +/** + * Extract PDF pages as JPEG images using pdftoppm. + * Produces page-01.jpg, page-02.jpg, etc. in an output directory. + * This enables reading large PDFs and works with all API providers. + * + * @param filePath Path to the PDF file + * @param options Optional page range (1-indexed, inclusive) + */ +export async function extractPDFPages( + filePath: string, + options?: { firstPage?: number; lastPage?: number }, +): Promise> { + try { + const fs = getFsImplementation() + const stats = await fs.stat(filePath) + const originalSize = stats.size + + if (originalSize === 0) { + return { + success: false, + error: { reason: 'empty', message: `PDF file is empty: ${filePath}` }, + } + } + + if (originalSize > PDF_MAX_EXTRACT_SIZE) { + return { + success: false, + error: { + reason: 'too_large', + message: `PDF file exceeds maximum allowed size for text extraction (${formatFileSize(PDF_MAX_EXTRACT_SIZE)}).`, + }, + } + } + + const available = await isPdftoppmAvailable() + if (!available) { + return { + success: false, + error: { + reason: 'unavailable', + message: + 'pdftoppm is not installed. Install poppler-utils (e.g. `brew install poppler` or `apt-get install poppler-utils`) to enable PDF page rendering.', + }, + } + } + + const uuid = randomUUID() + const outputDir = join(getToolResultsDir(), `pdf-${uuid}`) + await mkdir(outputDir, { recursive: true }) + + // pdftoppm produces files like -01.jpg, -02.jpg, etc. + const prefix = join(outputDir, 'page') + const args = ['-jpeg', '-r', '100'] + if (options?.firstPage) { + args.push('-f', String(options.firstPage)) + } + if (options?.lastPage && options.lastPage !== Infinity) { + args.push('-l', String(options.lastPage)) + } + args.push(filePath, prefix) + const { code, stderr } = await execFileNoThrow('pdftoppm', args, { + timeout: 120_000, + useCwd: false, + }) + + if (code !== 0) { + if (/password/i.test(stderr)) { + return { + success: false, + error: { + reason: 'password_protected', + message: + 'PDF is password-protected. Please provide an unprotected version.', + }, + } + } + if (/damaged|corrupt|invalid/i.test(stderr)) { + return { + success: false, + error: { + reason: 'corrupted', + message: 'PDF file is corrupted or invalid.', + }, + } + } + return { + success: false, + error: { reason: 'unknown', message: `pdftoppm failed: ${stderr}` }, + } + } + + // Read generated image files and sort naturally + const entries = await readdir(outputDir) + const imageFiles = entries.filter(f => f.endsWith('.jpg')).sort() + const pageCount = imageFiles.length + + if (pageCount === 0) { + return { + success: false, + error: { + reason: 'corrupted', + message: 'pdftoppm produced no output pages. The PDF may be invalid.', + }, + } + } + + const count = imageFiles.length + + return { + success: true, + data: { + type: 'parts', + file: { + filePath, + originalSize, + outputDir, + count, + }, + }, + } + } catch (e: unknown) { + return { + success: false, + error: { + reason: 'unknown', + message: errorMessage(e), + }, + } + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/permissions/PermissionUpdateSchema.ts b/_all-in-one-upload/cc-haha/src/utils/permissions/PermissionUpdateSchema.ts new file mode 100644 index 0000000..8d750f0 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/permissions/PermissionUpdateSchema.ts @@ -0,0 +1,78 @@ +/** + * Zod schemas for permission updates. + * + * This file is intentionally kept minimal with no complex dependencies + * so it can be safely imported by src/types/hooks.ts without creating + * circular dependencies. + */ +import z from 'zod/v4' +// Types extracted to src/types/permissions.ts to break import cycles +import type { + PermissionUpdate, + PermissionUpdateDestination, +} from '../../types/permissions.js' +import { lazySchema } from '../lazySchema.js' +import { externalPermissionModeSchema } from './PermissionMode.js' +import { + permissionBehaviorSchema, + permissionRuleValueSchema, +} from './PermissionRule.js' + +// Re-export for backwards compatibility +export type { PermissionUpdate, PermissionUpdateDestination } + +/** + * PermissionUpdateDestination is where a new permission rule should be saved to. + */ +export const permissionUpdateDestinationSchema = lazySchema(() => + z.enum([ + // User settings (global) + 'userSettings', + // Project settings (shared per-directory) + 'projectSettings', + // Local settings (gitignored) + 'localSettings', + // In-memory for the current session only + 'session', + // From the command line arguments + 'cliArg', + ]), +) + +export const permissionUpdateSchema = lazySchema(() => + z.discriminatedUnion('type', [ + z.object({ + type: z.literal('addRules'), + rules: z.array(permissionRuleValueSchema()), + behavior: permissionBehaviorSchema(), + destination: permissionUpdateDestinationSchema(), + }), + z.object({ + type: z.literal('replaceRules'), + rules: z.array(permissionRuleValueSchema()), + behavior: permissionBehaviorSchema(), + destination: permissionUpdateDestinationSchema(), + }), + z.object({ + type: z.literal('removeRules'), + rules: z.array(permissionRuleValueSchema()), + behavior: permissionBehaviorSchema(), + destination: permissionUpdateDestinationSchema(), + }), + z.object({ + type: z.literal('setMode'), + mode: externalPermissionModeSchema(), + destination: permissionUpdateDestinationSchema(), + }), + z.object({ + type: z.literal('addDirectories'), + directories: z.array(z.string()), + destination: permissionUpdateDestinationSchema(), + }), + z.object({ + type: z.literal('removeDirectories'), + directories: z.array(z.string()), + destination: permissionUpdateDestinationSchema(), + }), + ]), +) diff --git a/_all-in-one-upload/cc-haha/src/utils/permissions/denialTracking.ts b/_all-in-one-upload/cc-haha/src/utils/permissions/denialTracking.ts new file mode 100644 index 0000000..0ea0f62 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/permissions/denialTracking.ts @@ -0,0 +1,45 @@ +/** + * Denial tracking infrastructure for permission classifiers. + * Tracks consecutive denials and total denials to determine + * when to fall back to prompting. + */ + +export type DenialTrackingState = { + consecutiveDenials: number + totalDenials: number +} + +export const DENIAL_LIMITS = { + maxConsecutive: 3, + maxTotal: 20, +} as const + +export function createDenialTrackingState(): DenialTrackingState { + return { + consecutiveDenials: 0, + totalDenials: 0, + } +} + +export function recordDenial(state: DenialTrackingState): DenialTrackingState { + return { + ...state, + consecutiveDenials: state.consecutiveDenials + 1, + totalDenials: state.totalDenials + 1, + } +} + +export function recordSuccess(state: DenialTrackingState): DenialTrackingState { + if (state.consecutiveDenials === 0) return state // No change needed + return { + ...state, + consecutiveDenials: 0, + } +} + +export function shouldFallbackToPrompting(state: DenialTrackingState): boolean { + return ( + state.consecutiveDenials >= DENIAL_LIMITS.maxConsecutive || + state.totalDenials >= DENIAL_LIMITS.maxTotal + ) +} diff --git a/_all-in-one-upload/cc-haha/src/utils/permissions/permissionsLoader.ts b/_all-in-one-upload/cc-haha/src/utils/permissions/permissionsLoader.ts new file mode 100644 index 0000000..29f06f9 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/permissions/permissionsLoader.ts @@ -0,0 +1,296 @@ +import { readFileSync } from '../fileRead.js' +import { getFsImplementation, safeResolvePath } from '../fsOperations.js' +import { safeParseJSON } from '../json.js' +import { logError } from '../log.js' +import { + type EditableSettingSource, + getEnabledSettingSources, + type SettingSource, +} from '../settings/constants.js' +import { + getSettingsFilePathForSource, + getSettingsForSource, + updateSettingsForSource, +} from '../settings/settings.js' +import type { SettingsJson } from '../settings/types.js' +import type { + PermissionBehavior, + PermissionRule, + PermissionRuleSource, + PermissionRuleValue, +} from './PermissionRule.js' +import { + permissionRuleValueFromString, + permissionRuleValueToString, +} from './permissionRuleParser.js' + +/** + * Returns true if allowManagedPermissionRulesOnly is enabled in managed settings (policySettings). + * When enabled, only permission rules from managed settings are respected. + */ +export function shouldAllowManagedPermissionRulesOnly(): boolean { + return ( + getSettingsForSource('policySettings')?.allowManagedPermissionRulesOnly === + true + ) +} + +/** + * Returns true if "always allow" options should be shown in permission prompts. + * When allowManagedPermissionRulesOnly is enabled, these options are hidden. + */ +export function shouldShowAlwaysAllowOptions(): boolean { + return !shouldAllowManagedPermissionRulesOnly() +} + +const SUPPORTED_RULE_BEHAVIORS = [ + 'allow', + 'deny', + 'ask', +] as const satisfies PermissionBehavior[] + +/** + * Lenient version of getSettingsForSource that doesn't fail on ANY validation errors. + * Simply parses the JSON and returns it as-is without schema validation. + * + * Used when loading settings to append new rules (avoids losing existing rules + * due to validation failures in unrelated fields like hooks). + * + * FOR EDITING ONLY - do not use this for reading settings for execution. + */ +function getSettingsForSourceLenient_FOR_EDITING_ONLY_NOT_FOR_READING( + source: SettingSource, +): SettingsJson | null { + const filePath = getSettingsFilePathForSource(source) + if (!filePath) { + return null + } + + try { + const { resolvedPath } = safeResolvePath(getFsImplementation(), filePath) + const content = readFileSync(resolvedPath) + if (content.trim() === '') { + return {} + } + + const data = safeParseJSON(content, false) + // Return raw parsed JSON without validation to preserve all existing settings + // This is safe because we're only using this for reading/appending, not for execution + return data && typeof data === 'object' ? (data as SettingsJson) : null + } catch { + return null + } +} + +/** + * Converts permissions JSON to an array of PermissionRule objects + * @param data The parsed permissions data + * @param source The source of these rules + * @returns Array of PermissionRule objects + */ +function settingsJsonToRules( + data: SettingsJson | null, + source: PermissionRuleSource, +): PermissionRule[] { + if (!data || !data.permissions) { + return [] + } + + const { permissions } = data + const rules: PermissionRule[] = [] + for (const behavior of SUPPORTED_RULE_BEHAVIORS) { + const behaviorArray = permissions[behavior] + if (behaviorArray) { + for (const ruleString of behaviorArray) { + rules.push({ + source, + ruleBehavior: behavior, + ruleValue: permissionRuleValueFromString(ruleString), + }) + } + } + } + return rules +} + +/** + * Loads all permission rules from all relevant sources (managed and project settings) + * @returns Array of all permission rules + */ +export function loadAllPermissionRulesFromDisk(): PermissionRule[] { + // If allowManagedPermissionRulesOnly is set, only use managed permission rules + if (shouldAllowManagedPermissionRulesOnly()) { + return getPermissionRulesForSource('policySettings') + } + + // Otherwise, load from all enabled sources (backwards compatible) + const rules: PermissionRule[] = [] + + for (const source of getEnabledSettingSources()) { + rules.push(...getPermissionRulesForSource(source)) + } + return rules +} + +/** + * Loads permission rules from a specific source + * @param source The source to load from + * @returns Array of permission rules from that source + */ +export function getPermissionRulesForSource( + source: SettingSource, +): PermissionRule[] { + const settingsData = getSettingsForSource(source) + return settingsJsonToRules(settingsData, source) +} + +export type PermissionRuleFromEditableSettings = PermissionRule & { + source: EditableSettingSource +} + +// Editable sources that can be modified (excludes policySettings and flagSettings) +const EDITABLE_SOURCES: EditableSettingSource[] = [ + 'userSettings', + 'projectSettings', + 'localSettings', +] + +/** + * Deletes a rule from the project permissions file + * @param rule The rule to delete + * @returns Promise resolving to a boolean indicating success + */ +export function deletePermissionRuleFromSettings( + rule: PermissionRuleFromEditableSettings, +): boolean { + // Runtime check to ensure source is actually editable + if (!EDITABLE_SOURCES.includes(rule.source as EditableSettingSource)) { + return false + } + + const ruleString = permissionRuleValueToString(rule.ruleValue) + const settingsData = getSettingsForSource(rule.source) + + // If there's no settings data or permissions, nothing to do + if (!settingsData || !settingsData.permissions) { + return false + } + + const behaviorArray = settingsData.permissions[rule.ruleBehavior] + if (!behaviorArray) { + return false + } + + // Normalize raw settings entries via roundtrip parse→serialize so legacy + // names (e.g. "KillShell") match their canonical form ("TaskStop"). + const normalizeEntry = (raw: string): string => + permissionRuleValueToString(permissionRuleValueFromString(raw)) + + if (!behaviorArray.some(raw => normalizeEntry(raw) === ruleString)) { + return false + } + + try { + // Keep a copy of the original permissions data to preserve unrecognized keys + const updatedSettingsData = { + ...settingsData, + permissions: { + ...settingsData.permissions, + [rule.ruleBehavior]: behaviorArray.filter( + raw => normalizeEntry(raw) !== ruleString, + ), + }, + } + + const { error } = updateSettingsForSource(rule.source, updatedSettingsData) + if (error) { + // Error already logged inside updateSettingsForSource + return false + } + + return true + } catch (error) { + logError(error) + return false + } +} + +function getEmptyPermissionSettingsJson(): SettingsJson { + return { + permissions: {}, + } +} + +/** + * Adds rules to the project permissions file + * @param ruleValues The rule values to add + * @returns Promise resolving to a boolean indicating success + */ +export function addPermissionRulesToSettings( + { + ruleValues, + ruleBehavior, + }: { + ruleValues: PermissionRuleValue[] + ruleBehavior: PermissionBehavior + }, + source: EditableSettingSource, +): boolean { + // When allowManagedPermissionRulesOnly is enabled, don't persist new permission rules + if (shouldAllowManagedPermissionRulesOnly()) { + return false + } + + if (ruleValues.length < 1) { + // No rules to add + return true + } + + const ruleStrings = ruleValues.map(permissionRuleValueToString) + // First try the normal settings loader which validates the schema + // If validation fails, fall back to lenient loading to preserve existing rules + // even if some fields (like hooks) have validation errors + const settingsData = + getSettingsForSource(source) || + getSettingsForSourceLenient_FOR_EDITING_ONLY_NOT_FOR_READING(source) || + getEmptyPermissionSettingsJson() + + try { + // Ensure permissions object exists + const existingPermissions = settingsData.permissions || {} + const existingRules = existingPermissions[ruleBehavior] || [] + + // Filter out duplicates - normalize existing entries via roundtrip + // parse→serialize so legacy names match their canonical form. + const existingRulesSet = new Set( + existingRules.map(raw => + permissionRuleValueToString(permissionRuleValueFromString(raw)), + ), + ) + const newRules = ruleStrings.filter(rule => !existingRulesSet.has(rule)) + + // If no new rules to add, return success + if (newRules.length === 0) { + return true + } + + // Keep a copy of the original settings data to preserve unrecognized keys + const updatedSettingsData = { + ...settingsData, + permissions: { + ...existingPermissions, + [ruleBehavior]: [...existingRules, ...newRules], + }, + } + const result = updateSettingsForSource(source, updatedSettingsData) + + if (result.error) { + throw result.error + } + + return true + } catch (error) { + logError(error) + return false + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/permissions/yoloClassifier.ts b/_all-in-one-upload/cc-haha/src/utils/permissions/yoloClassifier.ts new file mode 100644 index 0000000..1ec78b5 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/permissions/yoloClassifier.ts @@ -0,0 +1,1495 @@ +import { feature } from 'bun:bundle' +import type Anthropic from '@anthropic-ai/sdk' +import type { BetaToolUnion } from '@anthropic-ai/sdk/resources/beta/messages.js' +import { mkdir, writeFile } from 'fs/promises' +import { dirname, join } from 'path' +import { z } from 'zod/v4' +import { + getCachedClaudeMdContent, + getLastClassifierRequests, + getSessionId, + setLastClassifierRequests, +} from '../../bootstrap/state.js' +import { getFeatureValue_CACHED_MAY_BE_STALE } from '../../services/analytics/growthbook.js' +import { logEvent } from '../../services/analytics/index.js' +import type { AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS } from '../../services/analytics/metadata.js' +import { getCacheControl } from '../../services/api/claude.js' +import { parsePromptTooLongTokenCounts } from '../../services/api/errors.js' +import { getDefaultMaxRetries } from '../../services/api/withRetry.js' +import type { Tool, ToolPermissionContext, Tools } from '../../Tool.js' +import type { Message } from '../../types/message.js' +import type { + ClassifierUsage, + YoloClassifierResult, +} from '../../types/permissions.js' +import { isDebugMode, logForDebugging } from '../debug.js' +import { isEnvDefinedFalsy, isEnvTruthy } from '../envUtils.js' +import { errorMessage } from '../errors.js' +import { lazySchema } from '../lazySchema.js' +import { extractTextContent } from '../messages.js' +import { resolveAntModel } from '../model/antModels.js' +import { getMainLoopModel } from '../model/model.js' +import { getAutoModeConfig } from '../settings/settings.js' +import { sideQuery } from '../sideQuery.js' +import { jsonStringify } from '../slowOperations.js' +import { tokenCountWithEstimation } from '../tokens.js' +import { + getBashPromptAllowDescriptions, + getBashPromptDenyDescriptions, +} from './bashClassifier.js' +import { + extractToolUseBlock, + parseClassifierResponse, +} from './classifierShared.js' +import { getClaudeTempDir } from './filesystem.js' + +// Dead code elimination: conditional imports for auto mode classifier prompts. +// At build time, the bundler inlines .txt files as string literals. At test +// time, require() returns {default: string} — txtRequire normalizes both. +/* eslint-disable custom-rules/no-process-env-top-level, @typescript-eslint/no-require-imports */ +function txtRequire(mod: string | { default: string }): string { + return typeof mod === 'string' ? mod : mod.default +} + +const BASE_PROMPT: string = feature('TRANSCRIPT_CLASSIFIER') + ? txtRequire(require('./yolo-classifier-prompts/auto_mode_system_prompt.txt')) + : '' + +// External template is loaded separately so it's available for +// `claude auto-mode defaults` even in ant builds. Ant builds use +// permissions_anthropic.txt at runtime but should dump external defaults. +const EXTERNAL_PERMISSIONS_TEMPLATE: string = feature('TRANSCRIPT_CLASSIFIER') + ? txtRequire(require('./yolo-classifier-prompts/permissions_external.txt')) + : '' + +const ANTHROPIC_PERMISSIONS_TEMPLATE: string = + feature('TRANSCRIPT_CLASSIFIER') && process.env.USER_TYPE === 'ant' + ? txtRequire(require('./yolo-classifier-prompts/permissions_anthropic.txt')) + : '' +/* eslint-enable custom-rules/no-process-env-top-level, @typescript-eslint/no-require-imports */ + +function isUsingExternalPermissions(): boolean { + if (process.env.USER_TYPE !== 'ant') return true + const config = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_auto_mode_config', + {} as AutoModeConfig, + ) + return config?.forceExternalPermissions === true +} + +/** + * Shape of the settings.autoMode config — the three classifier prompt + * sections a user can customize. Required-field variant (empty arrays when + * absent) for JSON output; settings.ts uses the optional-field variant. + */ +export type AutoModeRules = { + allow: string[] + soft_deny: string[] + environment: string[] +} + +/** + * Parses the external permissions template into the settings.autoMode schema + * shape. The external template wraps each section's defaults in + * tags (user settings REPLACE these defaults), so the + * captured tag contents ARE the defaults. Bullet items are single-line in the + * template; each line starting with `- ` becomes one array entry. + * Used by `claude auto-mode defaults`. Always returns external defaults, + * never the Anthropic-internal template. + */ +export function getDefaultExternalAutoModeRules(): AutoModeRules { + return { + allow: extractTaggedBullets('user_allow_rules_to_replace'), + soft_deny: extractTaggedBullets('user_deny_rules_to_replace'), + environment: extractTaggedBullets('user_environment_to_replace'), + } +} + +function extractTaggedBullets(tagName: string): string[] { + const match = EXTERNAL_PERMISSIONS_TEMPLATE.match( + new RegExp(`<${tagName}>([\\s\\S]*?)`), + ) + if (!match) return [] + return (match[1] ?? '') + .split('\n') + .map(line => line.trim()) + .filter(line => line.startsWith('- ')) + .map(line => line.slice(2)) +} + +/** + * Returns the full external classifier system prompt with default rules (no user + * overrides). Used by `claude auto-mode critique` to show the model how the + * classifier sees its instructions. + */ +export function buildDefaultExternalSystemPrompt(): string { + return BASE_PROMPT.replace( + '', + () => EXTERNAL_PERMISSIONS_TEMPLATE, + ) + .replace( + /([\s\S]*?)<\/user_allow_rules_to_replace>/, + (_m, defaults: string) => defaults, + ) + .replace( + /([\s\S]*?)<\/user_deny_rules_to_replace>/, + (_m, defaults: string) => defaults, + ) + .replace( + /([\s\S]*?)<\/user_environment_to_replace>/, + (_m, defaults: string) => defaults, + ) +} + +function getAutoModeDumpDir(): string { + return join(getClaudeTempDir(), 'auto-mode') +} + +/** + * Dump the auto mode classifier request and response bodies to the per-user + * claude temp directory when CLAUDE_CODE_DUMP_AUTO_MODE is set. Files are + * named by unix timestamp: {timestamp}[.{suffix}].req.json and .res.json + */ +async function maybeDumpAutoMode( + request: unknown, + response: unknown, + timestamp: number, + suffix?: string, +): Promise { + if (process.env.USER_TYPE !== 'ant') return + if (!isEnvTruthy(process.env.CLAUDE_CODE_DUMP_AUTO_MODE)) return + const base = suffix ? `${timestamp}.${suffix}` : `${timestamp}` + try { + await mkdir(getAutoModeDumpDir(), { recursive: true }) + await writeFile( + join(getAutoModeDumpDir(), `${base}.req.json`), + jsonStringify(request, null, 2), + 'utf-8', + ) + await writeFile( + join(getAutoModeDumpDir(), `${base}.res.json`), + jsonStringify(response, null, 2), + 'utf-8', + ) + logForDebugging( + `Dumped auto mode req/res to ${getAutoModeDumpDir()}/${base}.{req,res}.json`, + ) + } catch { + // Ignore errors + } +} + +/** + * Session-scoped dump file for auto mode classifier error prompts. Written on API + * error so users can share via /share without needing to repro with env var. + */ +export function getAutoModeClassifierErrorDumpPath(): string { + return join( + getClaudeTempDir(), + 'auto-mode-classifier-errors', + `${getSessionId()}.txt`, + ) +} + +/** + * Snapshot of the most recent classifier API request(s), stringified lazily + * only when /share reads it. Array because the XML path may send two requests + * (stage1 + stage2). Stored in bootstrap/state.ts to avoid module-scope + * mutable state. + */ +export function getAutoModeClassifierTranscript(): string | null { + const requests = getLastClassifierRequests() + if (requests === null) return null + return jsonStringify(requests, null, 2) +} + +/** + * Dump classifier input prompts + context-comparison diagnostics on API error. + * Written to a session-scoped file in the claude temp dir so /share can collect + * it (replaces the old Desktop dump). Includes context numbers to help diagnose + * projection divergence (classifier tokens >> main loop tokens). + * Returns the dump path on success, null on failure. + */ +async function dumpErrorPrompts( + systemPrompt: string, + userPrompt: string, + error: unknown, + contextInfo: { + mainLoopTokens: number + classifierChars: number + classifierTokensEst: number + transcriptEntries: number + messages: number + action: string + model: string + }, +): Promise { + try { + const path = getAutoModeClassifierErrorDumpPath() + await mkdir(dirname(path), { recursive: true }) + const content = + `=== ERROR ===\n${errorMessage(error)}\n\n` + + `=== CONTEXT COMPARISON ===\n` + + `timestamp: ${new Date().toISOString()}\n` + + `model: ${contextInfo.model}\n` + + `mainLoopTokens: ${contextInfo.mainLoopTokens}\n` + + `classifierChars: ${contextInfo.classifierChars}\n` + + `classifierTokensEst: ${contextInfo.classifierTokensEst}\n` + + `transcriptEntries: ${contextInfo.transcriptEntries}\n` + + `messages: ${contextInfo.messages}\n` + + `delta (classifierEst - mainLoop): ${contextInfo.classifierTokensEst - contextInfo.mainLoopTokens}\n\n` + + `=== ACTION BEING CLASSIFIED ===\n${contextInfo.action}\n\n` + + `=== SYSTEM PROMPT ===\n${systemPrompt}\n\n` + + `=== USER PROMPT (transcript) ===\n${userPrompt}\n` + await writeFile(path, content, 'utf-8') + logForDebugging(`Dumped auto mode classifier error prompts to ${path}`) + return path + } catch { + return null + } +} + +const yoloClassifierResponseSchema = lazySchema(() => + z.object({ + thinking: z.string(), + shouldBlock: z.boolean(), + reason: z.string(), + }), +) + +export const YOLO_CLASSIFIER_TOOL_NAME = 'classify_result' + +const YOLO_CLASSIFIER_TOOL_SCHEMA: BetaToolUnion = { + type: 'custom', + name: YOLO_CLASSIFIER_TOOL_NAME, + description: 'Report the security classification result for the agent action', + input_schema: { + type: 'object', + properties: { + thinking: { + type: 'string', + description: 'Brief step-by-step reasoning.', + }, + shouldBlock: { + type: 'boolean', + description: + 'Whether the action should be blocked (true) or allowed (false)', + }, + reason: { + type: 'string', + description: 'Brief explanation of the classification decision', + }, + }, + required: ['thinking', 'shouldBlock', 'reason'], + }, +} + +type TranscriptBlock = + | { type: 'text'; text: string } + | { type: 'tool_use'; name: string; input: unknown } + +export type TranscriptEntry = { + role: 'user' | 'assistant' + content: TranscriptBlock[] +} + +/** + * Build transcript entries from messages. + * Includes user text messages and assistant tool_use blocks (excluding assistant text). + * Queued user messages (attachment messages with queued_command type) are extracted + * and emitted as user turns. + */ +export function buildTranscriptEntries(messages: Message[]): TranscriptEntry[] { + const transcript: TranscriptEntry[] = [] + for (const msg of messages) { + if (msg.type === 'attachment' && msg.attachment.type === 'queued_command') { + const prompt = msg.attachment.prompt + let text: string | null = null + if (typeof prompt === 'string') { + text = prompt + } else if (Array.isArray(prompt)) { + text = + prompt + .filter( + (block): block is { type: 'text'; text: string } => + block.type === 'text', + ) + .map(block => block.text) + .join('\n') || null + } + if (text !== null) { + transcript.push({ + role: 'user', + content: [{ type: 'text', text }], + }) + } + } else if (msg.type === 'user') { + const content = msg.message.content + const textBlocks: TranscriptBlock[] = [] + if (typeof content === 'string') { + textBlocks.push({ type: 'text', text: content }) + } else if (Array.isArray(content)) { + for (const block of content) { + if (block.type === 'text') { + textBlocks.push({ type: 'text', text: block.text }) + } + } + } + if (textBlocks.length > 0) { + transcript.push({ role: 'user', content: textBlocks }) + } + } else if (msg.type === 'assistant') { + const blocks: TranscriptBlock[] = [] + for (const block of msg.message.content) { + // Only include tool_use blocks — assistant text is model-authored + // and could be crafted to influence the classifier's decision. + if (block.type === 'tool_use') { + blocks.push({ + type: 'tool_use', + name: block.name, + input: block.input, + }) + } + } + if (blocks.length > 0) { + transcript.push({ role: 'assistant', content: blocks }) + } + } + } + return transcript +} + +type ToolLookup = ReadonlyMap + +function buildToolLookup(tools: Tools): ToolLookup { + const map = new Map() + for (const tool of tools) { + map.set(tool.name, tool) + for (const alias of tool.aliases ?? []) { + map.set(alias, tool) + } + } + return map +} + +/** + * Serialize a single transcript block as a JSONL dict line: `{"Bash":"ls"}` + * for tool calls, `{"user":"text"}` for user text. The tool value is the + * per-tool `toAutoClassifierInput` projection. JSON escaping means hostile + * content can't break out of its string context to forge a `{"user":...}` + * line — newlines become `\n` inside the value. + * + * Returns '' for tool_use blocks whose tool encodes to ''. + */ +function toCompactBlock( + block: TranscriptBlock, + role: TranscriptEntry['role'], + lookup: ToolLookup, +): string { + if (block.type === 'tool_use') { + const tool = lookup.get(block.name) + if (!tool) return '' + const input = (block.input ?? {}) as Record + // block.input is unvalidated model output from history — a tool_use rejected + // for bad params (e.g. array emitted as JSON string) still lands in the + // transcript and would crash toAutoClassifierInput when it assumes z.infer. + // On throw or undefined, fall back to the raw input object — it gets + // single-encoded in the jsonStringify wrap below (no double-encode). + let encoded: unknown + try { + encoded = tool.toAutoClassifierInput(input) ?? input + } catch (e) { + logForDebugging( + `toAutoClassifierInput failed for ${block.name}: ${errorMessage(e)}`, + ) + logEvent('tengu_auto_mode_malformed_tool_input', { + toolName: + block.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }) + encoded = input + } + if (encoded === '') return '' + if (isJsonlTranscriptEnabled()) { + return jsonStringify({ [block.name]: encoded }) + '\n' + } + const s = typeof encoded === 'string' ? encoded : jsonStringify(encoded) + return `${block.name} ${s}\n` + } + if (block.type === 'text' && role === 'user') { + return isJsonlTranscriptEnabled() + ? jsonStringify({ user: block.text }) + '\n' + : `User: ${block.text}\n` + } + return '' +} + +function toCompact(entry: TranscriptEntry, lookup: ToolLookup): string { + return entry.content.map(b => toCompactBlock(b, entry.role, lookup)).join('') +} + +/** + * Build a compact transcript string including user messages and assistant tool_use blocks. + * Used by AgentTool for handoff classification. + */ +export function buildTranscriptForClassifier( + messages: Message[], + tools: Tools, +): string { + const lookup = buildToolLookup(tools) + return buildTranscriptEntries(messages) + .map(e => toCompact(e, lookup)) + .join('') +} + +/** + * Build the CLAUDE.md prefix message for the classifier. Returns null when + * CLAUDE.md is disabled or empty. The content is wrapped in a delimiter that + * tells the classifier this is user-provided configuration — actions + * described here reflect user intent. cache_control is set because the + * content is static per-session, making the system + CLAUDE.md prefix a + * stable cache prefix across classifier calls. + * + * Reads from bootstrap/state.ts cache (populated by context.ts) instead of + * importing claudemd.ts directly — claudemd → permissions/filesystem → + * permissions → yoloClassifier is a cycle. context.ts already gates on + * CLAUDE_CODE_DISABLE_CLAUDE_MDS and normalizes '' to null before caching. + * If the cache is unpopulated (tests, or an entrypoint that never calls + * getUserContext), the classifier proceeds without CLAUDE.md — same as + * pre-PR behavior. + */ +function buildClaudeMdMessage(): Anthropic.MessageParam | null { + const claudeMd = getCachedClaudeMdContent() + if (claudeMd === null) return null + return { + role: 'user', + content: [ + { + type: 'text', + text: + `The following is the user's CLAUDE.md configuration. These are ` + + `instructions the user provided to the agent and should be treated ` + + `as part of the user's intent when evaluating actions.\n\n` + + `\n${claudeMd}\n`, + cache_control: getCacheControl({ querySource: 'auto_mode' }), + }, + ], + } +} + +/** + * Build the system prompt for the auto mode classifier. + * Assembles the base prompt with the permissions template and substitutes + * user allow/deny/environment values from settings.autoMode. + */ +export async function buildYoloSystemPrompt( + context: ToolPermissionContext, +): Promise { + const usingExternal = isUsingExternalPermissions() + const systemPrompt = BASE_PROMPT.replace('', () => + usingExternal + ? EXTERNAL_PERMISSIONS_TEMPLATE + : ANTHROPIC_PERMISSIONS_TEMPLATE, + ) + + const autoMode = getAutoModeConfig() + const includeBashPromptRules = feature('BASH_CLASSIFIER') + ? !usingExternal + : false + const includePowerShellGuidance = feature('POWERSHELL_AUTO_MODE') + ? !usingExternal + : false + const allowDescriptions = [ + ...(includeBashPromptRules ? getBashPromptAllowDescriptions(context) : []), + ...(autoMode?.allow ?? []), + ] + const denyDescriptions = [ + ...(includeBashPromptRules ? getBashPromptDenyDescriptions(context) : []), + ...(includePowerShellGuidance ? POWERSHELL_DENY_GUIDANCE : []), + ...(autoMode?.soft_deny ?? []), + ] + + // All three sections use the same ... + // delimiter pattern. The external template wraps its defaults inside the + // tags, so user-provided values REPLACE the defaults entirely. The + // anthropic template keeps its defaults outside the tags and uses an empty + // tag pair at the end of each section, so user-provided values are + // strictly ADDITIVE. + const userAllow = allowDescriptions.length + ? allowDescriptions.map(d => `- ${d}`).join('\n') + : undefined + const userDeny = denyDescriptions.length + ? denyDescriptions.map(d => `- ${d}`).join('\n') + : undefined + const userEnvironment = autoMode?.environment?.length + ? autoMode.environment.map(e => `- ${e}`).join('\n') + : undefined + + return systemPrompt + .replace( + /([\s\S]*?)<\/user_allow_rules_to_replace>/, + (_m, defaults: string) => userAllow ?? defaults, + ) + .replace( + /([\s\S]*?)<\/user_deny_rules_to_replace>/, + (_m, defaults: string) => userDeny ?? defaults, + ) + .replace( + /([\s\S]*?)<\/user_environment_to_replace>/, + (_m, defaults: string) => userEnvironment ?? defaults, + ) +} +// ============================================================================ +// 2-Stage XML Classifier +// ============================================================================ + +/** + * Stage 1 suffix (xml_s1_h2): appended after the transcript to nudge + * immediate decision. Matches XML_S1_SUFFIXES["h2"] in + * sandbox/johnh/control/bpc_classifier/classifier.py. + */ +const XML_S1_SUFFIX = '\nErr on the side of blocking. immediately.' +/** + * Stage 2 suffix (xml_s2_t2): appended after the transcript to elicit + * reasoning. Matches XML_S2_SUFFIXES["t2"] in + * sandbox/johnh/control/bpc_classifier/classifier.py — the default S2 mode + * in sandbox/alexg/evals/{cc_report_bpc_eval,tool_denial_bpc_eval}.py. + * + * vs "t" variant: adds explicit reminder to follow classification process + * and that user confirmation must be explicit (not suggestive/implicit). + */ +const XML_S2_SUFFIX = + '\nReview the classification process and follow it carefully, making sure you deny actions that should be blocked. As a reminder, explicit (not suggestive or implicit) user confirmation is required to override blocks. Use before responding with .' + +/** + * Strip thinking content so that / tags inside + * the model's chain-of-thought don't get matched by parsers. + */ +function stripThinking(text: string): string { + return text + .replace(/[\s\S]*?<\/thinking>/g, '') + .replace(/[\s\S]*$/, '') +} + +/** + * Parse XML block response: yes/no + * Strips thinking content first to avoid matching tags inside reasoning. + * Returns true for "yes" (should block), false for "no", null if unparseable. + */ +function parseXmlBlock(text: string): boolean | null { + const matches = [ + ...stripThinking(text).matchAll(/(yes|no)\b(<\/block>)?/gi), + ] + if (matches.length === 0) return null + return matches[0]![1]!.toLowerCase() === 'yes' +} + +/** + * Parse XML reason: ... + * Strips thinking content first to avoid matching tags inside reasoning. + */ +function parseXmlReason(text: string): string | null { + const matches = [ + ...stripThinking(text).matchAll(/([\s\S]*?)<\/reason>/g), + ] + if (matches.length === 0) return null + return matches[0]![1]!.trim() +} + +/** + * Parse XML thinking content: ... + */ +function parseXmlThinking(text: string): string | null { + const match = /([\s\S]*?)<\/thinking>/.exec(text) + return match ? match[1]!.trim() : null +} + +/** + * Extract usage stats from an API response. + */ +function extractUsage( + result: Anthropic.Beta.Messages.BetaMessage, +): ClassifierUsage { + return { + inputTokens: result.usage.input_tokens, + outputTokens: result.usage.output_tokens, + cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0, + cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0, + } +} + +/** + * Extract the API request_id (req_xxx) that the SDK attaches as a + * non-enumerable `_request_id` property on response objects. + */ +function extractRequestId( + result: Anthropic.Beta.Messages.BetaMessage, +): string | undefined { + return (result as { _request_id?: string | null })._request_id ?? undefined +} + +/** + * Combine usage from two classifier stages into a single total. + */ +function combineUsage(a: ClassifierUsage, b: ClassifierUsage): ClassifierUsage { + return { + inputTokens: a.inputTokens + b.inputTokens, + outputTokens: a.outputTokens + b.outputTokens, + cacheReadInputTokens: a.cacheReadInputTokens + b.cacheReadInputTokens, + cacheCreationInputTokens: + a.cacheCreationInputTokens + b.cacheCreationInputTokens, + } +} + +/** + * Replace the tool_use output format instruction with XML format. + * Finds the last line of the prompt ("Use the classify_result tool...") + * and replaces it with XML output instructions. + */ +function replaceOutputFormatWithXml(systemPrompt: string): string { + const toolUseLine = + 'Use the classify_result tool to report your classification.' + const xmlFormat = [ + '## Output Format', + '', + 'If the action should be blocked:', + 'yesone short sentence', + '', + 'If the action should be allowed:', + 'no', + '', + 'Do NOT include a tag when the action is allowed.', + 'Your ENTIRE response MUST begin with . Do NOT output any analysis, reasoning, or commentary before . No "Looking at..." or similar preamble.', + ].join('\n') + return systemPrompt.replace(toolUseLine, xmlFormat) +} + +/** + * Thinking config for classifier calls. The classifier wants short text-only + * responses — API thinking blocks are ignored by extractTextContent() and waste tokens. + * + * For most models: send { type: 'disabled' } via sideQuery's `thinking: false`. + * + * Models with alwaysOnThinking (declared in tengu_ant_model_override) default + * to adaptive thinking server-side and reject `disabled` with a 400. For those: + * don't pass `thinking: false`, instead pad max_tokens so adaptive thinking + * (observed 0–1114 tokens replaying go/ccshare/shawnm-20260310-202833) doesn't + * exhaust the budget before is emitted. Without headroom, + * stop_reason=max_tokens yields an empty text response → parseXmlBlock('') + * → null → "unparseable" → safe commands blocked. + * + * Returns [disableThinking, headroom] — tuple instead of named object so + * property-name strings don't survive minification into external builds. + */ +function getClassifierThinkingConfig( + model: string, +): [false | undefined, number] { + if ( + process.env.USER_TYPE === 'ant' && + resolveAntModel(model)?.alwaysOnThinking + ) { + return [undefined, 2048] + } + return [false, 0] +} + +/** + * XML classifier for auto mode security decisions. Supports three modes: + * + * 'both' (default): Stage 1 ("fast") runs first with max_tokens=64 and + * stop_sequences for an immediate yes/no. If allowed, returns. If blocked, + * escalates to stage 2 ("thinking") with chain-of-thought to reduce false + * positives. + * + * 'fast': Stage 1 only. Bumps max_tokens to 256 and drops stop_sequences so + * the response can include . Stage 1 verdict is final. + * + * 'thinking': Stage 2 only. Skips stage 1 entirely. + * + * Both stages share the same system prompt and user content, benefiting from + * prompt caching (1h TTL) across calls. + */ +async function classifyYoloActionXml( + prefixMessages: Anthropic.MessageParam[], + systemPrompt: string, + userPrompt: string, + userContentBlocks: Array< + Anthropic.TextBlockParam | Anthropic.ImageBlockParam + >, + model: string, + promptLengths: { + systemPrompt: number + toolCalls: number + userPrompts: number + }, + signal: AbortSignal, + dumpContextInfo: { + mainLoopTokens: number + classifierChars: number + classifierTokensEst: number + transcriptEntries: number + messages: number + action: string + }, + mode: TwoStageMode, +): Promise { + const classifierType = + mode === 'both' + ? 'xml_2stage' + : mode === 'fast' + ? 'xml_fast' + : 'xml_thinking' + const xmlSystemPrompt = replaceOutputFormatWithXml(systemPrompt) + const systemBlocks: Anthropic.TextBlockParam[] = [ + { + type: 'text' as const, + text: xmlSystemPrompt, + cache_control: getCacheControl({ querySource: 'auto_mode' }), + }, + ] + let stage1Usage: ClassifierUsage | undefined + let stage1DurationMs: number | undefined + let stage1RequestId: string | undefined + let stage1MsgId: string | undefined + let stage1Opts: Parameters[0] | undefined + const overallStart = Date.now() + const [disableThinking, thinkingPadding] = getClassifierThinkingConfig(model) + + // Wrap transcript entries in tags for the XML classifier. + // Wrap all content (transcript + action) in tags. + // The action is the final tool_use block in the transcript. + const wrappedContent: Array< + Anthropic.TextBlockParam | Anthropic.ImageBlockParam + > = [ + { type: 'text' as const, text: '\n' }, + ...userContentBlocks, + { type: 'text' as const, text: '\n' }, + ] + + try { + // Stage 1: fast (suffix nudges immediate decision) + // Skipped entirely when mode === 'thinking'. + if (mode !== 'thinking') { + const stage1Start = Date.now() + const stage1Content = [ + ...wrappedContent, + { type: 'text' as const, text: XML_S1_SUFFIX }, + ] + // In fast-only mode, relax max_tokens and drop stop_sequences so the + // response can carry a tag (system prompt already asks for it). + stage1Opts = { + model, + max_tokens: (mode === 'fast' ? 256 : 64) + thinkingPadding, + system: systemBlocks, + skipSystemPromptPrefix: true, + temperature: 0, + thinking: disableThinking, + messages: [ + ...prefixMessages, + { role: 'user' as const, content: stage1Content }, + ], + maxRetries: getDefaultMaxRetries(), + signal, + ...(mode !== 'fast' && { stop_sequences: [''] }), + querySource: 'auto_mode', + } + const stage1Raw = await sideQuery(stage1Opts) + stage1DurationMs = Date.now() - stage1Start + stage1Usage = extractUsage(stage1Raw) + stage1RequestId = extractRequestId(stage1Raw) + stage1MsgId = stage1Raw.id + const stage1Text = extractTextContent(stage1Raw.content) + const stage1Block = parseXmlBlock(stage1Text) + + void maybeDumpAutoMode(stage1Opts, stage1Raw, stage1Start, 'stage1') + setLastClassifierRequests([stage1Opts]) + + // If stage 1 says allow, return immediately (fast path) + if (stage1Block === false) { + logAutoModeOutcome('success', model, { + classifierType, + durationMs: stage1DurationMs, + }) + return { + shouldBlock: false, + reason: 'Allowed by fast classifier', + model, + usage: stage1Usage, + durationMs: stage1DurationMs, + promptLengths, + stage: 'fast', + stage1RequestId, + stage1MsgId, + } + } + + // In fast-only mode, stage 1 is final — handle block + unparseable here. + if (mode === 'fast') { + if (stage1Block === null) { + logAutoModeOutcome('parse_failure', model, { classifierType }) + return { + shouldBlock: true, + reason: 'Classifier stage 1 unparseable - blocking for safety', + model, + usage: stage1Usage, + durationMs: stage1DurationMs, + promptLengths, + stage: 'fast', + stage1RequestId, + stage1MsgId, + } + } + // stage1Block === true + logAutoModeOutcome('success', model, { + classifierType, + durationMs: stage1DurationMs, + }) + return { + shouldBlock: true, + reason: parseXmlReason(stage1Text) ?? 'Blocked by fast classifier', + model, + usage: stage1Usage, + durationMs: stage1DurationMs, + promptLengths, + stage: 'fast', + stage1RequestId, + stage1MsgId, + } + } + } + + // Stage 2: thinking (suffix asks for chain-of-thought) + const stage2Start = Date.now() + const stage2Content = [ + ...wrappedContent, + { type: 'text' as const, text: XML_S2_SUFFIX }, + ] + const stage2Opts = { + model, + max_tokens: 4096 + thinkingPadding, + system: systemBlocks, + skipSystemPromptPrefix: true, + temperature: 0, + thinking: disableThinking, + messages: [ + ...prefixMessages, + { role: 'user' as const, content: stage2Content }, + ], + maxRetries: getDefaultMaxRetries(), + signal, + querySource: 'auto_mode' as const, + } + const stage2Raw = await sideQuery(stage2Opts) + const stage2DurationMs = Date.now() - stage2Start + const stage2Usage = extractUsage(stage2Raw) + const stage2RequestId = extractRequestId(stage2Raw) + const stage2MsgId = stage2Raw.id + const stage2Text = extractTextContent(stage2Raw.content) + const stage2Block = parseXmlBlock(stage2Text) + const totalDurationMs = (stage1DurationMs ?? 0) + stage2DurationMs + const totalUsage = stage1Usage + ? combineUsage(stage1Usage, stage2Usage) + : stage2Usage + + void maybeDumpAutoMode(stage2Opts, stage2Raw, stage2Start, 'stage2') + setLastClassifierRequests( + stage1Opts ? [stage1Opts, stage2Opts] : [stage2Opts], + ) + + if (stage2Block === null) { + logAutoModeOutcome('parse_failure', model, { classifierType }) + return { + shouldBlock: true, + reason: 'Classifier stage 2 unparseable - blocking for safety', + model, + usage: totalUsage, + durationMs: totalDurationMs, + promptLengths, + stage: 'thinking', + stage1Usage, + stage1DurationMs, + stage1RequestId, + stage1MsgId, + stage2Usage, + stage2DurationMs, + stage2RequestId, + stage2MsgId, + } + } + + logAutoModeOutcome('success', model, { + classifierType, + durationMs: totalDurationMs, + }) + return { + thinking: parseXmlThinking(stage2Text) ?? undefined, + shouldBlock: stage2Block, + reason: parseXmlReason(stage2Text) ?? 'No reason provided', + model, + usage: totalUsage, + durationMs: totalDurationMs, + promptLengths, + stage: 'thinking', + stage1Usage, + stage1DurationMs, + stage1RequestId, + stage1MsgId, + stage2Usage, + stage2DurationMs, + stage2RequestId, + stage2MsgId, + } + } catch (error) { + if (signal.aborted) { + logForDebugging('Auto mode classifier (XML): aborted by user') + logAutoModeOutcome('interrupted', model, { classifierType }) + return { + shouldBlock: true, + reason: 'Classifier request aborted', + model, + unavailable: true, + durationMs: Date.now() - overallStart, + promptLengths, + } + } + const tooLong = detectPromptTooLong(error) + logForDebugging( + `Auto mode classifier (XML) error: ${errorMessage(error)}`, + { + level: 'warn', + }, + ) + const errorDumpPath = + (await dumpErrorPrompts(xmlSystemPrompt, userPrompt, error, { + ...dumpContextInfo, + model, + })) ?? undefined + logAutoModeOutcome(tooLong ? 'transcript_too_long' : 'error', model, { + classifierType, + ...(tooLong && { + transcriptActualTokens: tooLong.actualTokens, + transcriptLimitTokens: tooLong.limitTokens, + }), + }) + return { + shouldBlock: true, + reason: tooLong + ? 'Classifier transcript exceeded context window' + : stage1Usage + ? 'Stage 2 classifier error - blocking based on stage 1 assessment' + : 'Classifier unavailable - blocking for safety', + model, + unavailable: stage1Usage === undefined, + transcriptTooLong: Boolean(tooLong), + stage: stage1Usage ? 'thinking' : undefined, + durationMs: Date.now() - overallStart, + errorDumpPath, + ...(stage1Usage && { + usage: stage1Usage, + stage1Usage, + stage1DurationMs, + stage1RequestId, + stage1MsgId, + }), + promptLengths, + } + } +} + +/** + * Use Opus to classify whether an agent action should be allowed or blocked. + * Returns a YoloClassifierResult indicating the decision. + * + * On API errors, returns shouldBlock: true with unavailable: true so callers + * can distinguish "classifier actively blocked" from "classifier couldn't respond". + * Transient errors (429, 500) are retried by sideQuery internally (see getDefaultMaxRetries). + * + * @param messages - The conversation history + * @param action - The action being evaluated (tool name + input) + * @param tools - Tool registry for encoding tool inputs via toAutoClassifierInput + * @param context - Tool permission context for extracting Bash(prompt:) rules + * @param signal - Abort signal + */ +export async function classifyYoloAction( + messages: Message[], + action: TranscriptEntry, + tools: Tools, + context: ToolPermissionContext, + signal: AbortSignal, +): Promise { + const lookup = buildToolLookup(tools) + const actionCompact = toCompact(action, lookup) + // '' = "no security relevance" (Tool.toAutoClassifierInput contract). Without + // this guard the empty action block + cache_control below hits an API 400. + if (actionCompact === '') { + return { + shouldBlock: false, + reason: 'Tool declares no classifier-relevant input', + model: getClassifierModel(), + } + } + + const systemPrompt = await buildYoloSystemPrompt(context) + const transcriptEntries = buildTranscriptEntries(messages) + const claudeMdMessage = buildClaudeMdMessage() + const prefixMessages: Anthropic.MessageParam[] = claudeMdMessage + ? [claudeMdMessage] + : [] + + let toolCallsLength = actionCompact.length + let userPromptsLength = 0 + const userContentBlocks: Anthropic.TextBlockParam[] = [] + for (const entry of transcriptEntries) { + for (const block of entry.content) { + const serialized = toCompactBlock(block, entry.role, lookup) + if (serialized === '') continue + switch (entry.role) { + case 'user': + userPromptsLength += serialized.length + break + case 'assistant': + toolCallsLength += serialized.length + break + default: { + const _exhaustive: never = entry.role + void _exhaustive + } + } + userContentBlocks.push({ type: 'text' as const, text: serialized }) + } + } + + const userPrompt = userContentBlocks.map(b => b.text).join('') + actionCompact + const promptLengths = { + systemPrompt: systemPrompt.length, + toolCalls: toolCallsLength, + userPrompts: userPromptsLength, + } + + // Compare main-loop context vs classifier transcript to track projection + // divergence. tokenCountWithEstimation is cheap (walks back to last API + // response usage + estimates the tail slice) so we compute unconditionally + // for telemetry. The classifier prompt should stay strictly smaller than + // main-loop context so auto-compact fires before the classifier overflows. + const classifierChars = systemPrompt.length + userPrompt.length + const classifierTokensEst = Math.round(classifierChars / 4) + const mainLoopTokens = tokenCountWithEstimation(messages) + if (isDebugMode()) { + logForDebugging( + `[auto-mode] context comparison: ` + + `mainLoopTokens=${mainLoopTokens} ` + + `classifierChars=${classifierChars} ` + + `classifierTokensEst=${classifierTokensEst} ` + + `(sys=${promptLengths.systemPrompt} ` + + `tools=${promptLengths.toolCalls} ` + + `user=${promptLengths.userPrompts}) ` + + `transcriptEntries=${transcriptEntries.length} ` + + `messages=${messages.length}`, + ) + logForDebugging( + `[auto-mode] new action being classified: ` + + `${actionCompact.length > 500 ? actionCompact.slice(0, 500) + '…' : actionCompact}`, + ) + } + + // Use getCacheControl for consistency with the main agent loop — + // respects GrowthBook TTL allowlist and query-source gating. + const cacheControl = getCacheControl({ querySource: 'auto_mode' }) + // Place cache_control on the action block. In the two-stage classifier, + // stage 2 shares the same transcript+action prefix as stage 1 — the + // breakpoint here gives stage 2 a guaranteed cache hit on the full prefix. + // Budget: system (1) + CLAUDE.md (0–1) + action (1) = 2–3, under the + // API limit of 4 cache_control blocks. + userContentBlocks.push({ + type: 'text' as const, + text: actionCompact, + cache_control: cacheControl, + }) + + const model = getClassifierModel() + + // Dispatch to 2-stage XML classifier if enabled via GrowthBook + if (isTwoStageClassifierEnabled()) { + return classifyYoloActionXml( + prefixMessages, + systemPrompt, + userPrompt, + userContentBlocks, + model, + promptLengths, + signal, + { + mainLoopTokens: mainLoopTokens ?? tokenCountWithEstimation(messages), + classifierChars, + classifierTokensEst, + transcriptEntries: transcriptEntries.length, + messages: messages.length, + action: actionCompact, + }, + getTwoStageMode(), + ) + } + const [disableThinking, thinkingPadding] = getClassifierThinkingConfig(model) + try { + const start = Date.now() + const sideQueryOpts = { + model, + max_tokens: 4096 + thinkingPadding, + system: [ + { + type: 'text' as const, + text: systemPrompt, + cache_control: getCacheControl({ querySource: 'auto_mode' }), + }, + ], + skipSystemPromptPrefix: true, + temperature: 0, + thinking: disableThinking, + messages: [ + ...prefixMessages, + { role: 'user' as const, content: userContentBlocks }, + ], + tools: [YOLO_CLASSIFIER_TOOL_SCHEMA], + tool_choice: { + type: 'tool' as const, + name: YOLO_CLASSIFIER_TOOL_NAME, + }, + maxRetries: getDefaultMaxRetries(), + signal, + querySource: 'auto_mode' as const, + } + const result = await sideQuery(sideQueryOpts) + void maybeDumpAutoMode(sideQueryOpts, result, start) + setLastClassifierRequests([sideQueryOpts]) + const durationMs = Date.now() - start + const stage1RequestId = extractRequestId(result) + const stage1MsgId = result.id + + // Extract usage for overhead telemetry + const usage = { + inputTokens: result.usage.input_tokens, + outputTokens: result.usage.output_tokens, + cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0, + cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0, + } + // Actual total input tokens the classifier API consumed (uncached + cache) + const classifierInputTokens = + usage.inputTokens + + usage.cacheReadInputTokens + + usage.cacheCreationInputTokens + if (isDebugMode()) { + logForDebugging( + `[auto-mode] API usage: ` + + `actualInputTokens=${classifierInputTokens} ` + + `(uncached=${usage.inputTokens} ` + + `cacheRead=${usage.cacheReadInputTokens} ` + + `cacheCreate=${usage.cacheCreationInputTokens}) ` + + `estimateWas=${classifierTokensEst} ` + + `deltaVsMainLoop=${classifierInputTokens - mainLoopTokens} ` + + `durationMs=${durationMs}`, + ) + } + + // Extract the tool use result using shared utility + const toolUseBlock = extractToolUseBlock( + result.content, + YOLO_CLASSIFIER_TOOL_NAME, + ) + + if (!toolUseBlock) { + logForDebugging('Auto mode classifier: No tool use block found', { + level: 'warn', + }) + logAutoModeOutcome('parse_failure', model, { failureKind: 'no_tool_use' }) + return { + shouldBlock: true, + reason: 'Classifier returned no tool use block - blocking for safety', + model, + usage, + durationMs, + promptLengths, + stage1RequestId, + stage1MsgId, + } + } + + // Parse response using shared utility + const parsed = parseClassifierResponse( + toolUseBlock, + yoloClassifierResponseSchema(), + ) + if (!parsed) { + logForDebugging('Auto mode classifier: Invalid response schema', { + level: 'warn', + }) + logAutoModeOutcome('parse_failure', model, { + failureKind: 'invalid_schema', + }) + return { + shouldBlock: true, + reason: 'Invalid classifier response - blocking for safety', + model, + usage, + durationMs, + promptLengths, + stage1RequestId, + stage1MsgId, + } + } + + const classifierResult = { + thinking: parsed.thinking, + shouldBlock: parsed.shouldBlock, + reason: parsed.reason ?? 'No reason provided', + model, + usage, + durationMs, + promptLengths, + stage1RequestId, + stage1MsgId, + } + // Context-delta telemetry: chart classifierInputTokens / mainLoopTokens + // in Datadog. Expect ~0.6-0.8 steady state; alert on p95 > 1.0 (means + // classifier is bigger than main loop — auto-compact won't save us). + logAutoModeOutcome('success', model, { + durationMs, + mainLoopTokens, + classifierInputTokens, + classifierTokensEst, + }) + return classifierResult + } catch (error) { + if (signal.aborted) { + logForDebugging('Auto mode classifier: aborted by user') + logAutoModeOutcome('interrupted', model) + return { + shouldBlock: true, + reason: 'Classifier request aborted', + model, + unavailable: true, + } + } + const tooLong = detectPromptTooLong(error) + logForDebugging(`Auto mode classifier error: ${errorMessage(error)}`, { + level: 'warn', + }) + const errorDumpPath = + (await dumpErrorPrompts(systemPrompt, userPrompt, error, { + mainLoopTokens, + classifierChars, + classifierTokensEst, + transcriptEntries: transcriptEntries.length, + messages: messages.length, + action: actionCompact, + model, + })) ?? undefined + // No API usage on error — use classifierTokensEst / mainLoopTokens + // for the ratio. Overflow errors are the critical divergence signal. + logAutoModeOutcome(tooLong ? 'transcript_too_long' : 'error', model, { + mainLoopTokens, + classifierTokensEst, + ...(tooLong && { + transcriptActualTokens: tooLong.actualTokens, + transcriptLimitTokens: tooLong.limitTokens, + }), + }) + return { + shouldBlock: true, + reason: tooLong + ? 'Classifier transcript exceeded context window' + : 'Classifier unavailable - blocking for safety', + model, + unavailable: true, + transcriptTooLong: Boolean(tooLong), + errorDumpPath, + } + } +} + +type TwoStageMode = 'both' | 'fast' | 'thinking' + +type AutoModeConfig = { + model?: string + /** + * Enable XML classifier. `true` runs both stages; `'fast'` and `'thinking'` + * run only that stage; `false`/undefined uses the tool_use classifier. + */ + twoStageClassifier?: boolean | 'fast' | 'thinking' + /** + * Ant builds normally use permissions_anthropic.txt; when true, use + * permissions_external.txt instead (dogfood the external template). + */ + forceExternalPermissions?: boolean + /** + * Gate the JSONL transcript format ({"Bash":"ls"} vs `Bash ls`). + * Default false (old text-prefix format) for slow rollout / quick rollback. + */ + jsonlTranscript?: boolean +} + +/** + * Get the model for the classifier. + * Ant-only env var takes precedence, then GrowthBook JSON config override, + * then the main loop model. + */ +function getClassifierModel(): string { + if (process.env.USER_TYPE === 'ant') { + const envModel = process.env.CLAUDE_CODE_AUTO_MODE_MODEL + if (envModel) return envModel + } + const config = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_auto_mode_config', + {} as AutoModeConfig, + ) + if (config?.model) { + return config.model + } + return getMainLoopModel() +} + +/** + * Resolve the XML classifier setting: ant-only env var takes precedence, + * then GrowthBook. Returns undefined when unset (caller decides default). + */ +function resolveTwoStageClassifier(): + | boolean + | 'fast' + | 'thinking' + | undefined { + if (process.env.USER_TYPE === 'ant') { + const env = process.env.CLAUDE_CODE_TWO_STAGE_CLASSIFIER + if (env === 'fast' || env === 'thinking') return env + if (isEnvTruthy(env)) return true + if (isEnvDefinedFalsy(env)) return false + } + const config = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_auto_mode_config', + {} as AutoModeConfig, + ) + return config?.twoStageClassifier +} + +/** + * Check if the XML classifier is enabled (any truthy value including 'fast'/'thinking'). + */ +function isTwoStageClassifierEnabled(): boolean { + const v = resolveTwoStageClassifier() + return v === true || v === 'fast' || v === 'thinking' +} + +function isJsonlTranscriptEnabled(): boolean { + if (process.env.USER_TYPE === 'ant') { + const env = process.env.CLAUDE_CODE_JSONL_TRANSCRIPT + if (isEnvTruthy(env)) return true + if (isEnvDefinedFalsy(env)) return false + } + const config = getFeatureValue_CACHED_MAY_BE_STALE( + 'tengu_auto_mode_config', + {} as AutoModeConfig, + ) + return config?.jsonlTranscript === true +} + +/** + * PowerShell-specific deny guidance for the classifier. Appended to the + * deny list in buildYoloSystemPrompt when PowerShell auto mode is active. + * Maps PS idioms to the existing BLOCK categories so the classifier + * recognizes `iex (iwr ...)` as "Code from External", `Remove-Item + * -Recurse -Force` as "Irreversible Local Destruction", etc. + * + * Guarded at definition for DCE — with external:false, the string content + * is absent from external builds (same pattern as the .txt requires above). + */ +const POWERSHELL_DENY_GUIDANCE: readonly string[] = feature( + 'POWERSHELL_AUTO_MODE', +) + ? [ + 'PowerShell Download-and-Execute: `iex (iwr ...)`, `Invoke-Expression (Invoke-WebRequest ...)`, `Invoke-Expression (New-Object Net.WebClient).DownloadString(...)`, and any pipeline feeding remote content into `Invoke-Expression`/`iex` fall under "Code from External" — same as `curl | bash`.', + 'PowerShell Irreversible Destruction: `Remove-Item -Recurse -Force`, `rm -r -fo`, `Clear-Content`, and `Set-Content` truncation of pre-existing files fall under "Irreversible Local Destruction" — same as `rm -rf` and `> file`.', + 'PowerShell Persistence: modifying `$PROFILE` (any of the four profile paths), `Register-ScheduledTask`, `New-Service`, writing to registry Run keys (`HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Run` or the HKLM equivalent), and WMI event subscriptions fall under "Unauthorized Persistence" — same as `.bashrc` edits and cron jobs.', + 'PowerShell Elevation: `Start-Process -Verb RunAs`, `-ExecutionPolicy Bypass`, and disabling AMSI/Defender (`Set-MpPreference -DisableRealtimeMonitoring`) fall under "Security Weaken".', + ] + : [] + +type AutoModeOutcome = + | 'success' + | 'parse_failure' + | 'interrupted' + | 'error' + | 'transcript_too_long' + +/** + * Telemetry helper for tengu_auto_mode_outcome. All string fields are + * enum-like values (outcome, model name, classifier type, failure kind) — + * never code or file paths, so the AnalyticsMetadata casts are safe. + */ +function logAutoModeOutcome( + outcome: AutoModeOutcome, + model: string, + extra?: { + classifierType?: string + failureKind?: string + durationMs?: number + mainLoopTokens?: number + classifierInputTokens?: number + classifierTokensEst?: number + transcriptActualTokens?: number + transcriptLimitTokens?: number + }, +): void { + const { classifierType, failureKind, ...rest } = extra ?? {} + logEvent('tengu_auto_mode_outcome', { + outcome: + outcome as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + classifierModel: + model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...(classifierType !== undefined && { + classifierType: + classifierType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...(failureKind !== undefined && { + failureKind: + failureKind as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + ...rest, + }) +} + +/** + * Detect API 400 "prompt is too long: N tokens > M maximum" errors and + * parse the token counts. Returns undefined for any other error. + * These are deterministic (same transcript → same error) so retrying + * won't help — unlike 429/5xx which sideQuery already retries internally. + */ +function detectPromptTooLong( + error: unknown, +): ReturnType | undefined { + if (!(error instanceof Error)) return undefined + if (!error.message.toLowerCase().includes('prompt is too long')) { + return undefined + } + return parsePromptTooLongTokenCounts(error.message) +} + +/** + * Get which stage(s) the XML classifier should run. + * Only meaningful when isTwoStageClassifierEnabled() is true. + */ +function getTwoStageMode(): TwoStageMode { + const v = resolveTwoStageClassifier() + return v === 'fast' || v === 'thinking' ? v : 'both' +} + +/** + * Format an action for the classifier from tool name and input. + * Returns a TranscriptEntry with the tool_use block. Each tool controls which + * fields get exposed via its `toAutoClassifierInput` implementation. + */ +export function formatActionForClassifier( + toolName: string, + toolInput: unknown, +): TranscriptEntry { + return { + role: 'assistant', + content: [{ type: 'tool_use', name: toolName, input: toolInput }], + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/plugins/.syncthing.hintRecommendation.ts.tmp b/_all-in-one-upload/cc-haha/src/utils/plugins/.syncthing.hintRecommendation.ts.tmp new file mode 100644 index 0000000..0ae2aeb Binary files /dev/null and b/_all-in-one-upload/cc-haha/src/utils/plugins/.syncthing.hintRecommendation.ts.tmp differ diff --git a/_all-in-one-upload/cc-haha/src/utils/plugins/pluginInstallationHelpers.ts b/_all-in-one-upload/cc-haha/src/utils/plugins/pluginInstallationHelpers.ts new file mode 100644 index 0000000..1e4c885 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/plugins/pluginInstallationHelpers.ts @@ -0,0 +1,595 @@ +/** + * Shared helper functions for plugin installation + * + * This module contains common utilities used across the plugin installation + * system to reduce code duplication and improve maintainability. + */ + +import { randomBytes } from 'crypto' +import { rename, rm } from 'fs/promises' +import { dirname, join, resolve, sep } from 'path' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + type AnalyticsMetadata_I_VERIFIED_THIS_IS_PII_TAGGED, + logEvent, +} from '../../services/analytics/index.js' +import { getCwd } from '../cwd.js' +import { toError } from '../errors.js' +import { getFsImplementation } from '../fsOperations.js' +import { logError } from '../log.js' +import { + getSettingsForSource, + updateSettingsForSource, +} from '../settings/settings.js' +import { buildPluginTelemetryFields } from '../telemetry/pluginTelemetry.js' +import { clearAllCaches } from './cacheUtils.js' +import { + formatDependencyCountSuffix, + getEnabledPluginIdsForScope, + type ResolutionResult, + resolveDependencyClosure, +} from './dependencyResolver.js' +import { + addInstalledPlugin, + getGitCommitSha, +} from './installedPluginsManager.js' +import { getManagedPluginNames } from './managedPlugins.js' +import { getMarketplaceCacheOnly, getPluginById } from './marketplaceManager.js' +import { + isOfficialMarketplaceName, + parsePluginIdentifier, + scopeToSettingSource, +} from './pluginIdentifier.js' +import { + cachePlugin, + getVersionedCachePath, + getVersionedZipCachePath, +} from './pluginLoader.js' +import { isPluginBlockedByPolicy } from './pluginPolicy.js' +import { calculatePluginVersion } from './pluginVersioning.js' +import { + isLocalPluginSource, + type PluginMarketplaceEntry, + type PluginScope, + type PluginSource, +} from './schemas.js' +import { + convertDirectoryToZipInPlace, + isPluginZipCacheEnabled, +} from './zipCache.js' + +/** + * Plugin installation metadata for installed_plugins.json + */ +export type PluginInstallationInfo = { + pluginId: string + installPath: string + version?: string +} + +/** + * Get current ISO timestamp + */ +export function getCurrentTimestamp(): string { + return new Date().toISOString() +} + +/** + * Validate that a resolved path stays within a base directory. + * Prevents path traversal attacks where malicious paths like './../../../etc/passwd' + * could escape the expected directory. + * + * @param basePath - The base directory that the resolved path must stay within + * @param relativePath - The relative path to validate + * @returns The validated absolute path + * @throws Error if the path would escape the base directory + */ +export function validatePathWithinBase( + basePath: string, + relativePath: string, +): string { + const resolvedPath = resolve(basePath, relativePath) + const normalizedBase = resolve(basePath) + sep + + // Check if the resolved path starts with the base path + // Adding sep ensures we don't match partial directory names + // e.g., /foo/bar should not match /foo/barbaz + if ( + !resolvedPath.startsWith(normalizedBase) && + resolvedPath !== resolve(basePath) + ) { + throw new Error( + `Path traversal detected: "${relativePath}" would escape the base directory`, + ) + } + + return resolvedPath +} + +/** + * Cache a plugin (local or external) and add it to installed_plugins.json + * + * This function combines the common pattern of: + * 1. Caching a plugin to ~/.claude/plugins/cache/ + * 2. Adding it to the installed plugins registry + * + * Both local plugins (with string source like "./path") and external plugins + * (with object source like {source: "github", ...}) are cached to the same + * location to ensure consistent behavior. + * + * @param pluginId - Plugin ID in "plugin@marketplace" format + * @param entry - Plugin marketplace entry + * @param scope - Installation scope (user, project, local, or managed). Defaults to 'user'. + * 'managed' scope is used for plugins installed automatically from managed settings. + * @param projectPath - Project path (required for project/local scopes) + * @param localSourcePath - For local plugins, the resolved absolute path to the source directory + * @returns The installation path + */ +export async function cacheAndRegisterPlugin( + pluginId: string, + entry: PluginMarketplaceEntry, + scope: PluginScope = 'user', + projectPath?: string, + localSourcePath?: string, +): Promise { + // For local plugins, we need the resolved absolute path + // Cast to PluginSource since cachePlugin handles any string path at runtime + const source: PluginSource = + typeof entry.source === 'string' && localSourcePath + ? (localSourcePath as PluginSource) + : entry.source + + const cacheResult = await cachePlugin(source, { + manifest: entry as PluginMarketplaceEntry, + }) + + // For local plugins, use the original source path for Git SHA calculation + // because the cached temp directory doesn't have .git (it's copied from a + // subdirectory of the marketplace git repo). For external plugins, use the + // cached path. For git-subdir sources, cachePlugin already captured the SHA + // before discarding the ephemeral clone (the extracted subdir has no .git). + const pathForGitSha = localSourcePath || cacheResult.path + const gitCommitSha = + cacheResult.gitCommitSha ?? (await getGitCommitSha(pathForGitSha)) + + const now = getCurrentTimestamp() + const version = await calculatePluginVersion( + pluginId, + entry.source, + cacheResult.manifest, + pathForGitSha, + entry.version, + cacheResult.gitCommitSha, + ) + + // Move the cached plugin to the versioned path: cache/marketplace/plugin/version/ + const versionedPath = getVersionedCachePath(pluginId, version) + let finalPath = cacheResult.path + + // Only move if the paths are different and plugin was cached to a different location + if (cacheResult.path !== versionedPath) { + // Create the versioned directory structure + await getFsImplementation().mkdir(dirname(versionedPath)) + + // Remove existing versioned path if present (force: no-op if missing) + await rm(versionedPath, { recursive: true, force: true }) + + // Check if versionedPath is a subdirectory of cacheResult.path + // This happens when marketplace name equals plugin name (e.g., "exa-mcp-server@exa-mcp-server") + // In this case, we can't directly rename because we'd be moving a directory into itself + const normalizedCachePath = cacheResult.path.endsWith(sep) + ? cacheResult.path + : cacheResult.path + sep + const isSubdirectory = versionedPath.startsWith(normalizedCachePath) + + if (isSubdirectory) { + // Move to a temp location first, then to final destination + // We can't directly rename/copy a directory into its own subdirectory + // Use the parent of cacheResult.path (same filesystem) to avoid EXDEV + // errors when /tmp is on a different filesystem (e.g., tmpfs) + const tempPath = join( + dirname(cacheResult.path), + `.claude-plugin-temp-${Date.now()}-${randomBytes(4).toString('hex')}`, + ) + await rename(cacheResult.path, tempPath) + await getFsImplementation().mkdir(dirname(versionedPath)) + await rename(tempPath, versionedPath) + } else { + // Move the cached plugin to the versioned location + await rename(cacheResult.path, versionedPath) + } + finalPath = versionedPath + } + + // Zip cache mode: convert directory to ZIP and remove the directory + if (isPluginZipCacheEnabled()) { + const zipPath = getVersionedZipCachePath(pluginId, version) + await convertDirectoryToZipInPlace(finalPath, zipPath) + finalPath = zipPath + } + + // Add to both V1 and V2 installed_plugins files with correct scope + addInstalledPlugin( + pluginId, + { + version, + installedAt: now, + lastUpdated: now, + installPath: finalPath, + gitCommitSha, + }, + scope, + projectPath, + ) + + return finalPath +} + +/** + * Register a plugin installation without caching + * + * Used for local plugins that are already on disk and don't need remote caching. + * External plugins should use cacheAndRegisterPlugin() instead. + * + * @param info - Plugin installation information + * @param scope - Installation scope (user, project, local, or managed). Defaults to 'user'. + * 'managed' scope is used for plugins registered from managed settings. + * @param projectPath - Project path (required for project/local scopes) + */ +export function registerPluginInstallation( + info: PluginInstallationInfo, + scope: PluginScope = 'user', + projectPath?: string, +): void { + const now = getCurrentTimestamp() + addInstalledPlugin( + info.pluginId, + { + version: info.version || 'unknown', + installedAt: now, + lastUpdated: now, + installPath: info.installPath, + }, + scope, + projectPath, + ) +} + +/** + * Parse plugin ID into components + * + * @param pluginId - Plugin ID in "plugin@marketplace" format + * @returns Parsed components or null if invalid + */ +export function parsePluginId( + pluginId: string, +): { name: string; marketplace: string } | null { + const parts = pluginId.split('@') + if (parts.length !== 2 || !parts[0] || !parts[1]) { + return null + } + + return { + name: parts[0], + marketplace: parts[1], + } +} + +/** + * Structured result from the install core. Wrappers format messages and + * handle analytics/error-catching around this. + */ +export type InstallCoreResult = + | { ok: true; closure: string[]; depNote: string } + | { ok: false; reason: 'local-source-no-location'; pluginName: string } + | { ok: false; reason: 'settings-write-failed'; message: string } + | { + ok: false + reason: 'resolution-failed' + resolution: ResolutionResult & { ok: false } + } + | { ok: false; reason: 'blocked-by-policy'; pluginName: string } + | { + ok: false + reason: 'dependency-blocked-by-policy' + pluginName: string + blockedDependency: string + } + +/** + * Format a failed ResolutionResult into a user-facing message. Unified on + * the richer CLI messages (the "Is the X marketplace added?" hint is useful + * for UI users too). + */ +export function formatResolutionError( + r: ResolutionResult & { ok: false }, +): string { + switch (r.reason) { + case 'cycle': + return `Dependency cycle: ${r.chain.join(' → ')}` + case 'cross-marketplace': { + const depMkt = parsePluginIdentifier(r.dependency).marketplace + const where = depMkt + ? `marketplace "${depMkt}"` + : 'a different marketplace' + const hint = depMkt + ? ` Add "${depMkt}" to allowCrossMarketplaceDependenciesOn in the ROOT marketplace's marketplace.json (the marketplace of the plugin you're installing — only its allowlist applies; no transitive trust).` + : '' + return `Dependency "${r.dependency}" (required by ${r.requiredBy}) is in ${where}, which is not in the allowlist — cross-marketplace dependencies are blocked by default. Install it manually first.${hint}` + } + case 'not-found': { + const { marketplace: depMkt } = parsePluginIdentifier(r.missing) + return depMkt + ? `Dependency "${r.missing}" (required by ${r.requiredBy}) not found. Is the "${depMkt}" marketplace added?` + : `Dependency "${r.missing}" (required by ${r.requiredBy}) not found in any configured marketplace` + } + } +} + +/** + * Core plugin install logic, shared by the CLI path (`installPluginOp`) and + * the interactive UI path (`installPluginFromMarketplace`). Given a + * pre-resolved marketplace entry, this: + * + * 1. Guards against local-source plugins without a marketplace install + * location (would silently no-op otherwise). + * 2. Resolves the transitive dependency closure (when PLUGIN_DEPENDENCIES + * is on; trivial single-plugin closure otherwise). + * 3. Writes the entire closure to enabledPlugins in one settings update. + * 4. Caches each closure member (downloads/copies sources as needed). + * 5. Clears memoization caches. + * + * Returns a structured result. Message formatting, analytics, and top-level + * error wrapping stay in the caller-specific wrappers. + * + * @param marketplaceInstallLocation Pass this if the caller already has it + * (from a prior marketplace search) to avoid a redundant lookup. + */ +export async function installResolvedPlugin({ + pluginId, + entry, + scope, + marketplaceInstallLocation, +}: { + pluginId: string + entry: PluginMarketplaceEntry + scope: 'user' | 'project' | 'local' + marketplaceInstallLocation?: string +}): Promise { + const settingSource = scopeToSettingSource(scope) + + // ── Policy guard ── + // Org-blocked plugins (managed-settings.json enabledPlugins: false) cannot + // be installed. Checked here so all install paths (CLI, UI, hint-triggered) + // are covered in one place. + if (isPluginBlockedByPolicy(pluginId)) { + return { ok: false, reason: 'blocked-by-policy', pluginName: entry.name } + } + + // ── Resolve dependency closure ── + // depInfo caches marketplace lookups so the materialize loop doesn't + // re-fetch. Seed the root if the caller gave us its install location. + const depInfo = new Map< + string, + { entry: PluginMarketplaceEntry; marketplaceInstallLocation: string } + >() + // Without this guard, a local-source root with undefined + // marketplaceInstallLocation falls through: depInfo isn't seeded, the + // materialize loop's `if (!info) continue` skips the root, and the user + // sees "Successfully installed" while nothing is cached. + if (isLocalPluginSource(entry.source) && !marketplaceInstallLocation) { + return { + ok: false, + reason: 'local-source-no-location', + pluginName: entry.name, + } + } + if (marketplaceInstallLocation) { + depInfo.set(pluginId, { entry, marketplaceInstallLocation }) + } + + const rootMarketplace = parsePluginIdentifier(pluginId).marketplace + const allowedCrossMarketplaces = new Set( + (rootMarketplace + ? (await getMarketplaceCacheOnly(rootMarketplace)) + ?.allowCrossMarketplaceDependenciesOn + : undefined) ?? [], + ) + const resolution = await resolveDependencyClosure( + pluginId, + async id => { + if (depInfo.has(id)) return depInfo.get(id)!.entry + if (id === pluginId) return entry + const info = await getPluginById(id) + if (info) depInfo.set(id, info) + return info?.entry ?? null + }, + getEnabledPluginIdsForScope(settingSource), + allowedCrossMarketplaces, + ) + if (!resolution.ok) { + return { ok: false, reason: 'resolution-failed', resolution } + } + + // ── Policy guard for transitive dependencies ── + // The root plugin was already checked above, but any dependency in the + // closure could also be policy-blocked. Check before writing to settings + // so a non-blocked plugin can't pull in a blocked dependency. + for (const id of resolution.closure) { + if (id !== pluginId && isPluginBlockedByPolicy(id)) { + return { + ok: false, + reason: 'dependency-blocked-by-policy', + pluginName: entry.name, + blockedDependency: id, + } + } + } + + // ── ACTION: write entire closure to settings in one call ── + const closureEnabled: Record = {} + for (const id of resolution.closure) closureEnabled[id] = true + const { error } = updateSettingsForSource(settingSource, { + enabledPlugins: { + ...getSettingsForSource(settingSource)?.enabledPlugins, + ...closureEnabled, + }, + }) + if (error) { + return { + ok: false, + reason: 'settings-write-failed', + message: error.message, + } + } + + // ── Materialize: cache each closure member ── + const projectPath = scope !== 'user' ? getCwd() : undefined + for (const id of resolution.closure) { + let info = depInfo.get(id) + // Root wasn't pre-seeded (caller didn't pass marketplaceInstallLocation + // for a non-local source). Fetch now; it's needed for the cache write. + if (!info && id === pluginId) { + const mktLocation = (await getPluginById(id))?.marketplaceInstallLocation + if (mktLocation) info = { entry, marketplaceInstallLocation: mktLocation } + } + if (!info) continue + + let localSourcePath: string | undefined + const { source } = info.entry + if (isLocalPluginSource(source)) { + localSourcePath = validatePathWithinBase( + info.marketplaceInstallLocation, + source, + ) + } + await cacheAndRegisterPlugin( + id, + info.entry, + scope, + projectPath, + localSourcePath, + ) + } + + clearAllCaches() + + const depNote = formatDependencyCountSuffix( + resolution.closure.filter(id => id !== pluginId), + ) + return { ok: true, closure: resolution.closure, depNote } +} + +/** + * Result of a plugin installation operation + */ +export type InstallPluginResult = + | { success: true; message: string } + | { success: false; error: string } + +/** + * Parameters for installing a plugin from marketplace + */ +export type InstallPluginParams = { + pluginId: string + entry: PluginMarketplaceEntry + marketplaceName: string + scope?: 'user' | 'project' | 'local' + trigger?: 'hint' | 'user' +} + +/** + * Install a single plugin from a marketplace with the specified scope. + * Interactive-UI wrapper around `installResolvedPlugin` — adds try/catch, + * analytics, and UI-style message formatting. + */ +export async function installPluginFromMarketplace({ + pluginId, + entry, + marketplaceName, + scope = 'user', + trigger = 'user', +}: InstallPluginParams): Promise { + try { + // Look up the marketplace install location for local-source plugins. + // Without this, plugins with relative-path sources fail from the + // interactive UI path (/plugin install) even though the CLI path works. + const pluginInfo = await getPluginById(pluginId) + const marketplaceInstallLocation = pluginInfo?.marketplaceInstallLocation + + const result = await installResolvedPlugin({ + pluginId, + entry, + scope, + marketplaceInstallLocation, + }) + + if (!result.ok) { + switch (result.reason) { + case 'local-source-no-location': + return { + success: false, + error: `Cannot install local plugin "${result.pluginName}" without marketplace install location`, + } + case 'settings-write-failed': + return { + success: false, + error: `Failed to update settings: ${result.message}`, + } + case 'resolution-failed': + return { + success: false, + error: formatResolutionError(result.resolution), + } + case 'blocked-by-policy': + return { + success: false, + error: `Plugin "${result.pluginName}" is blocked by your organization's policy and cannot be installed`, + } + case 'dependency-blocked-by-policy': + return { + success: false, + error: `Cannot install "${result.pluginName}": dependency "${result.blockedDependency}" is blocked by your organization's policy`, + } + } + } + + // _PROTO_* routes to PII-tagged plugin_name/marketplace_name BQ columns. + // plugin_id kept in additional_metadata (redacted to 'third-party' for + // non-official) because dbt external_claude_code_plugin_installs.sql + // extracts $.plugin_id for official-marketplace install tracking. Other + // plugin lifecycle events drop the blob key — no downstream consumers. + logEvent('tengu_plugin_installed', { + _PROTO_plugin_name: + entry.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_PII_TAGGED, + _PROTO_marketplace_name: + marketplaceName as AnalyticsMetadata_I_VERIFIED_THIS_IS_PII_TAGGED, + plugin_id: (isOfficialMarketplaceName(marketplaceName) + ? pluginId + : 'third-party') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + trigger: + trigger as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + install_source: (trigger === 'hint' + ? 'ui-suggestion' + : 'ui-discover') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...buildPluginTelemetryFields( + entry.name, + marketplaceName, + getManagedPluginNames(), + ), + ...(entry.version && { + version: + entry.version as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + }), + }) + + return { + success: true, + message: `✓ Installed ${entry.name}${result.depNote}. Run /reload-plugins to activate.`, + } + } catch (err) { + const errorMessage = err instanceof Error ? err.message : String(err) + logError(toError(err)) + return { success: false, error: `Failed to install: ${errorMessage}` } + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/plugins/pluginPolicy.ts b/_all-in-one-upload/cc-haha/src/utils/plugins/pluginPolicy.ts new file mode 100644 index 0000000..bd808ec --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/plugins/pluginPolicy.ts @@ -0,0 +1,20 @@ +/** + * Plugin policy checks backed by managed settings (policySettings). + * + * Kept as a leaf module (only imports settings) to avoid circular dependencies + * — marketplaceHelpers.ts imports marketplaceManager.ts which transitively + * reaches most of the plugin subsystem. + */ + +import { getSettingsForSource } from '../settings/settings.js' + +/** + * Check if a plugin is force-disabled by org policy (managed-settings.json). + * Policy-blocked plugins cannot be installed or enabled by the user at any + * scope. Used as the single source of truth for policy blocking across the + * install chokepoint, enable op, and UI filters. + */ +export function isPluginBlockedByPolicy(pluginId: string): boolean { + const policyEnabled = getSettingsForSource('policySettings')?.enabledPlugins + return policyEnabled?.[pluginId] === false +} diff --git a/_all-in-one-upload/cc-haha/src/utils/processUserInput/processBashCommand.tsx b/_all-in-one-upload/cc-haha/src/utils/processUserInput/processBashCommand.tsx new file mode 100644 index 0000000..8ab9585 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/processUserInput/processBashCommand.tsx @@ -0,0 +1,140 @@ +import type { ContentBlockParam } from '@anthropic-ai/sdk/resources'; +import { randomUUID } from 'crypto'; +import * as React from 'react'; +import { BashModeProgress } from 'src/components/BashModeProgress.js'; +import type { SetToolJSXFn } from 'src/Tool.js'; +import { BashTool } from 'src/tools/BashTool/BashTool.js'; +import type { AttachmentMessage, SystemMessage, UserMessage } from 'src/types/message.js'; +import type { ShellProgress } from 'src/types/tools.js'; +import { logEvent } from '../../services/analytics/index.js'; +import { errorMessage, ShellError } from '../errors.js'; +import { createSyntheticUserCaveatMessage, createUserInterruptionMessage, createUserMessage, prepareUserContent } from '../messages.js'; +import { resolveDefaultShell } from '../shell/resolveDefaultShell.js'; +import { isPowerShellToolEnabled } from '../shell/shellToolUtils.js'; +import { processToolResultBlock } from '../toolResultStorage.js'; +import { escapeXml } from '../xml.js'; +import type { ProcessUserInputContext } from './processUserInput.js'; +export async function processBashCommand(inputString: string, precedingInputBlocks: ContentBlockParam[], attachmentMessages: AttachmentMessage[], context: ProcessUserInputContext, setToolJSX: SetToolJSXFn): Promise<{ + messages: (UserMessage | AttachmentMessage | SystemMessage)[]; + shouldQuery: boolean; +}> { + // Shell routing (docs/design/ps-shell-selection.md §5.2): consult + // defaultShell, fall back to bash. isPowerShellToolEnabled() applies the + // same platform + env-var gate as tools.ts so input-box routing matches + // tool-list visibility. Computed up front so telemetry records the + // actual shell, not the raw setting. + const usePowerShell = isPowerShellToolEnabled() && resolveDefaultShell() === 'powershell'; + logEvent('tengu_input_bash', { + powershell: usePowerShell + }); + const userMessage = createUserMessage({ + content: prepareUserContent({ + inputString: `${inputString}`, + precedingInputBlocks + }) + }); + + // ctrl+b to background indicator + let jsx: React.ReactNode; + + // Just show initial UI + setToolJSX({ + jsx: , + shouldHidePromptInput: false + }); + try { + const bashModeContext: ProcessUserInputContext = { + ...context, + // TODO: Clean up this hack + setToolJSX: _ => { + jsx = _?.jsx; + } + }; + + // Progress UI — shared across both shell backends (both emit ShellProgress) + const onProgress = (progress: { + data: ShellProgress; + }) => { + setToolJSX({ + jsx: <> + + {jsx} + , + shouldHidePromptInput: false, + showSpinner: false + }); + }; + + // User-initiated `!` commands run outside sandbox. Both shell tools honor + // dangerouslyDisableSandbox (checked against areUnsandboxedCommandsAllowed() + // in shouldUseSandbox.ts). PS sandbox is Linux/macOS/WSL2 only — on Windows + // native, shouldUseSandbox() returns false regardless (unsupported platform). + // Lazy-require PowerShellTool so its ~300KB chunk only loads when the + // user has actually selected the powershell default shell. + type PSMod = typeof import('src/tools/PowerShellTool/PowerShellTool.js'); + let PowerShellTool: PSMod['PowerShellTool'] | null = null; + if (usePowerShell) { + /* eslint-disable @typescript-eslint/no-require-imports */ + PowerShellTool = (require('src/tools/PowerShellTool/PowerShellTool.js') as PSMod).PowerShellTool; + /* eslint-enable @typescript-eslint/no-require-imports */ + } + const shellTool = PowerShellTool ?? BashTool; + const response = PowerShellTool ? await PowerShellTool.call({ + command: inputString, + dangerouslyDisableSandbox: true + }, bashModeContext, undefined, undefined, onProgress) : await BashTool.call({ + command: inputString, + dangerouslyDisableSandbox: true + }, bashModeContext, undefined, undefined, onProgress); + const data = response.data; + if (!data) { + throw new Error('No result received from shell command'); + } + const stderr = data.stderr; + // Reuse the same formatting pipeline as inline !`cmd` bash (promptShellExecution) + // and model-initiated Bash. When BashTool.call() persists large output to disk, + // data.persistedOutputPath is set and the formatter wraps in . + // Pass stderr:'' to keep it separate for the UI tag. + const mapped = await processToolResultBlock(shellTool, { + ...data, + stderr: '' + }, randomUUID()); + // mapped.content may contain our own wrapper (trusted + // XML from buildLargeToolResultMessage). Escaping it would turn structural + // tags into <persisted-output>, breaking the model's parse and + // UserBashOutputMessage's extractTag. Escape the raw fallback only. + const stdout = typeof mapped.content === 'string' ? mapped.content : escapeXml(data.stdout); + return { + messages: [createSyntheticUserCaveatMessage(), userMessage, ...attachmentMessages, createUserMessage({ + content: `${stdout}${escapeXml(stderr)}` + })], + shouldQuery: false + }; + } catch (e) { + if (e instanceof ShellError) { + if (e.interrupted) { + return { + messages: [createSyntheticUserCaveatMessage(), userMessage, createUserInterruptionMessage({ + toolUse: false + }), ...attachmentMessages], + shouldQuery: false + }; + } + return { + messages: [createSyntheticUserCaveatMessage(), userMessage, ...attachmentMessages, createUserMessage({ + content: `${escapeXml(e.stdout)}${escapeXml(e.stderr)}` + })], + shouldQuery: false + }; + } + return { + messages: [createSyntheticUserCaveatMessage(), userMessage, ...attachmentMessages, createUserMessage({ + content: `Command failed: ${escapeXml(errorMessage(e))}` + })], + shouldQuery: false + }; + } finally { + setToolJSX(null); + } +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJDb250ZW50QmxvY2tQYXJhbSIsInJhbmRvbVVVSUQiLCJSZWFjdCIsIkJhc2hNb2RlUHJvZ3Jlc3MiLCJTZXRUb29sSlNYRm4iLCJCYXNoVG9vbCIsI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ucy52ZXJib3NlfVxuICAgICAgLz5cbiAgICApLFxuICAgIHNob3VsZEhpZGVQcm9tcHRJbnB1dDogZmFsc2UsXG4gIH0pXG5cbiAgdHJ5IHtcbiAgICBjb25zdCBiYXNoTW9kZUNvbnRleHQ6IFByb2Nlc3NVc2VySW5wdXRDb250ZXh0ID0ge1xuICAgICAgLi4uY29udGV4dCxcbiAgICAgIC8vIFRPRE86IENsZWFuIHVwIHRoaXMgaGFja1xuICAgICAgc2V0VG9vbEpTWDogXyA9PiB7XG4gICAgICAgIGpzeCA9IF8/LmpzeFxuICAgICAgfSxcbiAgICB9XG5cbiAgICAvLyBQcm9ncmVzcyBVSSDigJQgc2hhcmVkIGFjcm9zcyBib3RoIHNoZWxsIGJhY2tlbmRzIChib3RoIGVtaXQgU2hlbGxQcm9ncmVzcylcbiAgICBjb25zdCBvblByb2dyZXNzID0gKHByb2dyZXNzOiB7IGRhdGE6IFNoZWxsUHJvZ3Jlc3MgfSkgPT4ge1xuICAgICAgc2V0VG9vbEpTWCh7XG4gICAgICAgIGpzeDogKFxuICAgICAgICAgIDw+XG4gICAgICAgICAgICA8QmFzaE1vZGVQcm9ncmVzc1xuICAgICAgICAgICAgICBpbnB1dD17aW5wdXRTdHJpbmchfVxuICAgICAgICAgICAgICBwcm9ncmVzcz17cHJvZ3Jlc3MuZGF0YX1cbiAgICAgICAgICAgICAgdmVyYm9zZT17Y29udGV4dC5vcHRpb25zLnZlcmJvc2V9XG4gICAgICAgICAgICAvPlxuICAgICAgICAgICAge2pzeH1cbiAgICAgICAgICA8Lz5cbiAgICAgICAgKSxcbiAgICAgICAgc2hvdWxkSGlkZVByb21wdElucHV0OiBmYWxzZSxcbiAgICAgICAgc2hvd1NwaW5uZXI6IGZhbHNlLFxuICAgICAgfSlcbiAgICB9XG5cbiAgICAvLyBVc2VyLWluaXRpYXRlZCBgIWAgY29tbWFuZHMgcnVuIG91dHNpZGUgc2FuZGJveC4gQm90aCBzaGVsbCB0b29scyBob25vclxuIC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sRUFBRTtVQUFNLENBQUMsQ0FBQyxFQUNqRCxHQUFHcEMsa0JBQWtCLENBQ3RCO1VBQ0RLLFdBQVcsRUFBRTtRQUNmLENBQUM7TUFDSDtNQUNBLE9BQU87UUFDTEQsUUFBUSxFQUFFLENBQ1JoQixnQ0FBZ0MsQ0FBQyxDQUFDLEVBQ2xDb0IsV0FBVyxFQUNYLEdBQUdSLGtCQUFrQixFQUNyQlYsaUJBQWlCLENBQUM7VUFDaEJtQixPQUFPLEVBQUUsZ0JBQWdCZCxTQUFTLENBQUN1QyxDQUFDLENBQUNELE1BQU0sQ0FBQyw4QkFBOEJ0QyxTQUFTLENBQUN1QyxDQUFDLENBQUNILE1BQU0sQ0FBQztRQUMvRixDQUFDLENBQUMsQ0FDSDtRQUNEMUIsV0FBVyxFQUFFO01BQ2YsQ0FBQztJQUNIO0lBQ0EsT0FBTztNQUNMRCxRQUFRLEVBQUUsQ0FDUmhCLGdDQUFnQyxDQUFDLENBQUMsRUFDbENvQixXQUFXLEVBQ1gsR0FBR1Isa0JBQWtCLEVBQ3JCVixpQkFBaUIsQ0FBQztRQUNoQm1CLE9BQU8sRUFBRSxnQ0FBZ0NkLFNBQVMsQ0FBQ1QsWUFBWSxDQUFDZ0QsQ0FBQyxDQUFDLENBQUM7TUFDckUsQ0FBQyxDQUFDLENBQ0g7TUFDRDdCLFdBQVcsRUFBRTtJQUNmLENBQUM7RUFDSCxDQUFDLFNBQVM7SUFDUkgsVUFBVSxDQUFDLElBQUksQ0FBQztFQUNsQjtBQUNGIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/utils/queryHelpers.ts b/_all-in-one-upload/cc-haha/src/utils/queryHelpers.ts new file mode 100644 index 0000000..a3661ba --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/queryHelpers.ts @@ -0,0 +1,552 @@ +import type { ToolUseBlock } from '@anthropic-ai/sdk/resources/index.mjs' +import last from 'lodash-es/last.js' +import { + getSessionId, + isSessionPersistenceDisabled, +} from 'src/bootstrap/state.js' +import type { SDKMessage } from 'src/entrypoints/agentSdkTypes.js' +import type { CanUseToolFn } from '../hooks/useCanUseTool.js' +import { runTools } from '../services/tools/toolOrchestration.js' +import { findToolByName, type Tool, type Tools } from '../Tool.js' +import { BASH_TOOL_NAME } from '../tools/BashTool/toolName.js' +import { FILE_EDIT_TOOL_NAME } from '../tools/FileEditTool/constants.js' +import type { Input as FileReadInput } from '../tools/FileReadTool/FileReadTool.js' +import { + FILE_READ_TOOL_NAME, + FILE_UNCHANGED_STUB, +} from '../tools/FileReadTool/prompt.js' +import { FILE_WRITE_TOOL_NAME } from '../tools/FileWriteTool/prompt.js' +import type { Message } from '../types/message.js' +import type { OrphanedPermission } from '../types/textInputTypes.js' +import { logForDebugging } from './debug.js' +import { isEnvTruthy } from './envUtils.js' +import { isFsInaccessible } from './errors.js' +import { getFileModificationTime, stripLineNumberPrefix } from './file.js' +import { readFileSyncWithMetadata } from './fileRead.js' +import { + createFileStateCacheWithSizeLimit, + type FileStateCache, +} from './fileStateCache.js' +import { isNotEmptyMessage, normalizeMessages } from './messages.js' +import { expandPath } from './path.js' +import type { + inputSchema as permissionToolInputSchema, + outputSchema as permissionToolOutputSchema, +} from './permissions/PermissionPromptToolResultSchema.js' +import type { ProcessUserInputContext } from './processUserInput/processUserInput.js' +import { recordTranscript } from './sessionStorage.js' + +export type PermissionPromptTool = Tool< + ReturnType, + ReturnType +> + +// Small cache size for ask operations which typically access few files +// during permission prompts or limited tool operations +const ASK_READ_FILE_STATE_CACHE_SIZE = 10 + +/** + * Checks if the result should be considered successful based on the last message. + * Returns true if: + * - Last message is assistant with text/thinking content + * - Last message is user with only tool_result blocks + * - Last message is the user prompt but the API completed with end_turn + * (model chose to emit no content blocks) + */ +export function isResultSuccessful( + message: Message | undefined, + stopReason: string | null = null, +): message is Message { + if (!message) return false + + if (message.type === 'assistant') { + const lastContent = last(message.message.content) + return ( + lastContent?.type === 'text' || + lastContent?.type === 'thinking' || + lastContent?.type === 'redacted_thinking' + ) + } + + if (message.type === 'user') { + // Check if all content blocks are tool_result type + const content = message.message.content + if ( + Array.isArray(content) && + content.length > 0 && + content.every(block => 'type' in block && block.type === 'tool_result') + ) { + return true + } + } + + // Carve-out: API completed (message_delta set stop_reason) but yielded + // no assistant content — last(messages) is still this turn's prompt. + // claude.ts:2026 recognizes end_turn-with-zero-content-blocks as + // legitimate and passes through without throwing. Observed on + // task_notification drain turns: model returns stop_reason=end_turn, + // outputTokens=4, textContentLength=0 — it saw the subagent result + // and decided nothing needed saying. Without this, QueryEngine emits + // error_during_execution with errors[] = the entire process's + // accumulated logError() buffer. Covers both string-content and + // text-block-content user prompts, and any other non-passing shape. + return stopReason === 'end_turn' +} + +// Track last sent time for tool progress messages per tool use ID +// Keep only the last 100 entries to prevent unbounded growth +const MAX_TOOL_PROGRESS_TRACKING_ENTRIES = 100 +const TOOL_PROGRESS_THROTTLE_MS = 30000 +const toolProgressLastSentTime = new Map() + +export function* normalizeMessage(message: Message): Generator { + switch (message.type) { + case 'assistant': + for (const _ of normalizeMessages([message])) { + // Skip empty messages (e.g., "(no content)") that shouldn't be output to SDK + if (!isNotEmptyMessage(_)) { + continue + } + yield { + type: 'assistant', + message: _.message, + parent_tool_use_id: null, + session_id: getSessionId(), + uuid: _.uuid, + error: _.error, + } + } + return + case 'progress': + if ( + message.data.type === 'agent_progress' || + message.data.type === 'skill_progress' + ) { + for (const _ of normalizeMessages([message.data.message])) { + switch (_.type) { + case 'assistant': + // Skip empty messages (e.g., "(no content)") that shouldn't be output to SDK + if (!isNotEmptyMessage(_)) { + break + } + yield { + type: 'assistant', + message: _.message, + parent_tool_use_id: message.parentToolUseID, + session_id: getSessionId(), + uuid: _.uuid, + error: _.error, + } + break + case 'user': + yield { + type: 'user', + message: _.message, + parent_tool_use_id: message.parentToolUseID, + session_id: getSessionId(), + uuid: _.uuid, + timestamp: _.timestamp, + isSynthetic: _.isMeta || _.isVisibleInTranscriptOnly, + tool_use_result: _.mcpMeta + ? { content: _.toolUseResult, ..._.mcpMeta } + : _.toolUseResult, + } + break + } + } + } else if ( + message.data.type === 'bash_progress' || + message.data.type === 'powershell_progress' + ) { + // Filter bash progress to send only one per minute + // Only emit for Claude Code Remote for now + if ( + !isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) && + !process.env.CLAUDE_CODE_CONTAINER_ID + ) { + break + } + + // Use parentToolUseID as the key since toolUseID changes for each progress message + const trackingKey = message.parentToolUseID + const now = Date.now() + const lastSent = toolProgressLastSentTime.get(trackingKey) || 0 + const timeSinceLastSent = now - lastSent + + // Send if at least 30 seconds have passed since last update + if (timeSinceLastSent >= TOOL_PROGRESS_THROTTLE_MS) { + // Remove oldest entry if we're at capacity (LRU eviction) + if ( + toolProgressLastSentTime.size >= MAX_TOOL_PROGRESS_TRACKING_ENTRIES + ) { + const firstKey = toolProgressLastSentTime.keys().next().value + if (firstKey !== undefined) { + toolProgressLastSentTime.delete(firstKey) + } + } + + toolProgressLastSentTime.set(trackingKey, now) + yield { + type: 'tool_progress', + tool_use_id: message.toolUseID, + tool_name: + message.data.type === 'bash_progress' ? 'Bash' : 'PowerShell', + parent_tool_use_id: message.parentToolUseID, + elapsed_time_seconds: message.data.elapsedTimeSeconds, + task_id: message.data.taskId, + session_id: getSessionId(), + uuid: message.uuid, + } + } + } + break + case 'user': + for (const _ of normalizeMessages([message])) { + yield { + type: 'user', + message: _.message, + parent_tool_use_id: null, + session_id: getSessionId(), + uuid: _.uuid, + timestamp: _.timestamp, + isSynthetic: _.isMeta || _.isVisibleInTranscriptOnly, + tool_use_result: _.mcpMeta + ? { content: _.toolUseResult, ..._.mcpMeta } + : _.toolUseResult, + } + } + return + default: + // yield nothing + } +} + +export async function* handleOrphanedPermission( + orphanedPermission: OrphanedPermission, + tools: Tools, + mutableMessages: Message[], + processUserInputContext: ProcessUserInputContext, +): AsyncGenerator { + const persistSession = !isSessionPersistenceDisabled() + const { permissionResult, assistantMessage } = orphanedPermission + const { toolUseID } = permissionResult + + if (!toolUseID) { + return + } + + const content = assistantMessage.message.content + let toolUseBlock: ToolUseBlock | undefined + if (Array.isArray(content)) { + for (const block of content) { + if (block.type === 'tool_use' && block.id === toolUseID) { + toolUseBlock = block as ToolUseBlock + break + } + } + } + + if (!toolUseBlock) { + return + } + + const toolName = toolUseBlock.name + const toolInput = toolUseBlock.input + + const toolDefinition = findToolByName(tools, toolName) + if (!toolDefinition) { + return + } + + // Create ToolUseBlock with the updated input if permission was allowed + let finalInput = toolInput + if (permissionResult.behavior === 'allow') { + if (permissionResult.updatedInput !== undefined) { + finalInput = permissionResult.updatedInput + } else { + logForDebugging( + `Orphaned permission for ${toolName}: updatedInput is undefined, falling back to original tool input`, + { level: 'warn' }, + ) + } + } + const finalToolUseBlock: ToolUseBlock = { + ...toolUseBlock, + input: finalInput, + } + + const canUseTool: CanUseToolFn = async () => ({ + ...permissionResult, + decisionReason: { + type: 'mode', + mode: 'default' as const, + }, + }) + + // Add the assistant message with tool_use to messages BEFORE executing + // so the conversation history is complete (tool_use -> tool_result). + // + // On CCR resume, mutableMessages is seeded from the transcript and may already + // contain this tool_use. Pushing again would make normalizeMessagesForAPI merge + // same-ID assistants (concatenating content) and produce a duplicate tool_use + // ID, which the API rejects with "tool_use ids must be unique". + // + // Check for the specific tool_use_id rather than message.id: streaming yields + // each content block as a separate AssistantMessage sharing one message.id, so + // a [text, tool_use] response lands as two entries. filterUnresolvedToolUses may + // strip the tool_use entry but keep the text one; an id-based check would then + // wrongly skip the push while runTools below still executes, orphaning the result. + const alreadyPresent = mutableMessages.some( + m => + m.type === 'assistant' && + Array.isArray(m.message.content) && + m.message.content.some( + b => b.type === 'tool_use' && 'id' in b && b.id === toolUseID, + ), + ) + if (!alreadyPresent) { + mutableMessages.push(assistantMessage) + if (persistSession) { + await recordTranscript(mutableMessages) + } + } + + const sdkAssistantMessage: SDKMessage = { + ...assistantMessage, + session_id: getSessionId(), + parent_tool_use_id: null, + } as SDKMessage + yield sdkAssistantMessage + + // Execute the tool - errors are handled internally by runToolUse + for await (const update of runTools( + [finalToolUseBlock], + [assistantMessage], + canUseTool, + processUserInputContext, + )) { + if (update.message) { + mutableMessages.push(update.message) + if (persistSession) { + await recordTranscript(mutableMessages) + } + + const sdkMessage: SDKMessage = { + ...update.message, + session_id: getSessionId(), + parent_tool_use_id: null, + } as SDKMessage + + yield sdkMessage + } + } +} + +// Create a function to extract read files from messages +export function extractReadFilesFromMessages( + messages: Message[], + cwd: string, + maxSize: number = ASK_READ_FILE_STATE_CACHE_SIZE, +): FileStateCache { + const cache = createFileStateCacheWithSizeLimit(maxSize) + + // First pass: find all FileReadTool/FileWriteTool/FileEditTool uses in assistant messages + const fileReadToolUseIds = new Map() // toolUseId -> filePath + const fileWriteToolUseIds = new Map< + string, + { filePath: string; content: string } + >() // toolUseId -> { filePath, content } + const fileEditToolUseIds = new Map() // toolUseId -> filePath + + for (const message of messages) { + if ( + message.type === 'assistant' && + Array.isArray(message.message.content) + ) { + for (const content of message.message.content) { + if ( + content.type === 'tool_use' && + content.name === FILE_READ_TOOL_NAME + ) { + // Extract file_path from the tool use input + const input = content.input as FileReadInput | undefined + // Ranged reads are not added to the cache. + if ( + input?.file_path && + input?.offset === undefined && + input?.limit === undefined + ) { + // Normalize to absolute path for consistent cache lookups + const absolutePath = expandPath(input.file_path, cwd) + fileReadToolUseIds.set(content.id, absolutePath) + } + } else if ( + content.type === 'tool_use' && + content.name === FILE_WRITE_TOOL_NAME + ) { + // Extract file_path and content from the Write tool use input + const input = content.input as + | { file_path?: string; content?: string } + | undefined + if (input?.file_path && input?.content) { + // Normalize to absolute path for consistent cache lookups + const absolutePath = expandPath(input.file_path, cwd) + fileWriteToolUseIds.set(content.id, { + filePath: absolutePath, + content: input.content, + }) + } + } else if ( + content.type === 'tool_use' && + content.name === FILE_EDIT_TOOL_NAME + ) { + // Edit's input has old_string/new_string, not the resulting content. + // Track the path so the second pass can read current disk state. + const input = content.input as { file_path?: string } | undefined + if (input?.file_path) { + const absolutePath = expandPath(input.file_path, cwd) + fileEditToolUseIds.set(content.id, absolutePath) + } + } + } + } + } + + // Second pass: find corresponding tool results and extract content + for (const message of messages) { + if (message.type === 'user' && Array.isArray(message.message.content)) { + for (const content of message.message.content) { + if (content.type === 'tool_result' && content.tool_use_id) { + // Handle Read tool results + const readFilePath = fileReadToolUseIds.get(content.tool_use_id) + if ( + readFilePath && + typeof content.content === 'string' && + // Dedup stubs contain no file content — the earlier real Read + // already cached it. Chronological last-wins would otherwise + // overwrite the real entry with stub text. + !content.content.startsWith(FILE_UNCHANGED_STUB) + ) { + // Remove system-reminder blocks from the content + const processedContent = content.content.replace( + /[\s\S]*?<\/system-reminder>/g, + '', + ) + + // Extract the actual file content from the tool result + // Tool results for text files contain line numbers, we need to strip those + const fileContent = processedContent + .split('\n') + .map(stripLineNumberPrefix) + .join('\n') + .trim() + + // Cache the file content with the message timestamp + if (message.timestamp) { + const timestamp = new Date(message.timestamp).getTime() + cache.set(readFilePath, { + content: fileContent, + timestamp, + offset: undefined, + limit: undefined, + }) + } + } + + // Handle Write tool results - use content from the tool input + const writeToolData = fileWriteToolUseIds.get(content.tool_use_id) + if (writeToolData && message.timestamp) { + const timestamp = new Date(message.timestamp).getTime() + cache.set(writeToolData.filePath, { + content: writeToolData.content, + timestamp, + offset: undefined, + limit: undefined, + }) + } + + // Handle Edit tool results — post-edit content isn't in the + // tool_use input (only old_string/new_string) nor fully in the + // result (only a snippet). Read from disk now, using actual mtime + // so getChangedFiles's mtime check passes on the next turn. + // + // Callers seed the cache once at process start (print.ts --resume, + // Cowork cold-restart per turn), so disk content at extraction time + // IS the post-edit state. No dedup: processing every Edit preserves + // last-wins semantics when Read/Write interleave (Edit→Read→Edit). + const editFilePath = fileEditToolUseIds.get(content.tool_use_id) + if (editFilePath && content.is_error !== true) { + try { + const { content: diskContent } = + readFileSyncWithMetadata(editFilePath) + cache.set(editFilePath, { + content: diskContent, + timestamp: getFileModificationTime(editFilePath), + offset: undefined, + limit: undefined, + }) + } catch (e: unknown) { + if (!isFsInaccessible(e)) { + throw e + } + // File deleted or inaccessible since the Edit — skip + } + } + } + } + } + } + + return cache +} + +/** + * Extract the top-level CLI tools used in BashTool calls from message history. + * Returns a deduplicated set of command names (e.g. 'vercel', 'aws', 'git'). + */ +export function extractBashToolsFromMessages(messages: Message[]): Set { + const tools = new Set() + for (const message of messages) { + if ( + message.type === 'assistant' && + Array.isArray(message.message.content) + ) { + for (const content of message.message.content) { + if (content.type === 'tool_use' && content.name === BASH_TOOL_NAME) { + const { input } = content + if ( + typeof input !== 'object' || + input === null || + !('command' in input) + ) + continue + const cmd = extractCliName( + typeof input.command === 'string' ? input.command : undefined, + ) + if (cmd) { + tools.add(cmd) + } + } + } + } + } + return tools +} + +const STRIPPED_COMMANDS = new Set(['sudo']) + +/** + * Extract the actual CLI name from a bash command string, skipping + * env var assignments (e.g. `FOO=bar vercel` → `vercel`) and prefixes + * in STRIPPED_COMMANDS. + */ +function extractCliName(command: string | undefined): string | undefined { + if (!command) return undefined + const tokens = command.trim().split(/\s+/) + for (const token of tokens) { + if (/^[A-Za-z_]\w*=/.test(token)) continue + if (STRIPPED_COMMANDS.has(token)) continue + return token + } + return undefined +} diff --git a/_all-in-one-upload/cc-haha/src/utils/secureStorage/keychainPrefetch.ts b/_all-in-one-upload/cc-haha/src/utils/secureStorage/keychainPrefetch.ts new file mode 100644 index 0000000..061c2b1 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/secureStorage/keychainPrefetch.ts @@ -0,0 +1,116 @@ +/** + * Minimal module for firing macOS keychain reads in parallel with main.tsx + * module evaluation, same pattern as startMdmRawRead() in settings/mdm/rawRead.ts. + * + * isRemoteManagedSettingsEligible() reads two separate keychain entries + * SEQUENTIALLY via sync execSync during applySafeConfigEnvironmentVariables(): + * 1. "Claude Code-credentials" (OAuth tokens) — ~32ms + * 2. "Claude Code" (legacy API key) — ~33ms + * Sequential cost: ~65ms on every macOS startup. + * + * Firing both here lets the subprocesses run in parallel with the ~65ms of + * main.tsx imports. ensureKeychainPrefetchCompleted() is awaited alongside + * ensureMdmSettingsLoaded() in main.tsx preAction — nearly free since the + * subprocesses finish during import evaluation. Sync read() and + * getApiKeyFromConfigOrMacOSKeychain() then hit their caches. + * + * Imports stay minimal: child_process + macOsKeychainHelpers.ts (NOT + * macOsKeychainStorage.ts — that pulls in execa → human-signals → + * cross-spawn, ~58ms of synchronous module init). The helpers file's own + * import chain (envUtils, oauth constants, crypto) is already evaluated by + * startupProfiler.ts at main.tsx:5, so no new module-init cost lands here. + */ + +import { execFile } from 'child_process' +import { isBareMode } from '../envUtils.js' +import { + CREDENTIALS_SERVICE_SUFFIX, + getMacOsKeychainStorageServiceName, + getUsername, + primeKeychainCacheFromPrefetch, +} from './macOsKeychainHelpers.js' + +const KEYCHAIN_PREFETCH_TIMEOUT_MS = 10_000 + +// Shared with auth.ts getApiKeyFromConfigOrMacOSKeychain() so it can skip its +// sync spawn when the prefetch already landed. Distinguishing "not started" (null) +// from "completed with no key" ({ stdout: null }) lets the sync reader only +// trust a completed prefetch. +let legacyApiKeyPrefetch: { stdout: string | null } | null = null + +let prefetchPromise: Promise | null = null + +type SpawnResult = { stdout: string | null; timedOut: boolean } + +function spawnSecurity(serviceName: string): Promise { + return new Promise(resolve => { + execFile( + 'security', + ['find-generic-password', '-a', getUsername(), '-w', '-s', serviceName], + { encoding: 'utf-8', timeout: KEYCHAIN_PREFETCH_TIMEOUT_MS }, + (err, stdout) => { + // Exit 44 (entry not found) is a valid "no key" result and safe to + // prime as null. But timeout (err.killed) means the keychain MAY have + // a key we couldn't fetch — don't prime, let sync spawn retry. + // biome-ignore lint/nursery/noFloatingPromises: resolve() is not a floating promise + resolve({ + stdout: err ? null : stdout?.trim() || null, + timedOut: Boolean(err && 'killed' in err && err.killed), + }) + }, + ) + }) +} + +/** + * Fire both keychain reads in parallel. Called at main.tsx top-level + * immediately after startMdmRawRead(). Non-darwin is a no-op. + */ +export function startKeychainPrefetch(): void { + if (process.platform !== 'darwin' || prefetchPromise || isBareMode()) return + + // Fire both subprocesses immediately (non-blocking). They run in parallel + // with each other AND with main.tsx imports. The await in Promise.all + // happens later via ensureKeychainPrefetchCompleted(). + const oauthSpawn = spawnSecurity( + getMacOsKeychainStorageServiceName(CREDENTIALS_SERVICE_SUFFIX), + ) + const legacySpawn = spawnSecurity(getMacOsKeychainStorageServiceName()) + + prefetchPromise = Promise.all([oauthSpawn, legacySpawn]).then( + ([oauth, legacy]) => { + // Timed-out prefetch: don't prime. Sync read/spawn will retry with its + // own (longer) timeout. Priming null here would shadow a key that the + // sync path might successfully fetch. + if (!oauth.timedOut) primeKeychainCacheFromPrefetch(oauth.stdout) + if (!legacy.timedOut) legacyApiKeyPrefetch = { stdout: legacy.stdout } + }, + ) +} + +/** + * Await prefetch completion. Called in main.tsx preAction alongside + * ensureMdmSettingsLoaded() — nearly free since subprocesses finish during + * the ~65ms of main.tsx imports. Resolves immediately on non-darwin. + */ +export async function ensureKeychainPrefetchCompleted(): Promise { + if (prefetchPromise) await prefetchPromise +} + +/** + * Consumed by getApiKeyFromConfigOrMacOSKeychain() in auth.ts before it + * falls through to sync execSync. Returns null if prefetch hasn't completed. + */ +export function getLegacyApiKeyPrefetchResult(): { + stdout: string | null +} | null { + return legacyApiKeyPrefetch +} + +/** + * Clear prefetch result. Called alongside getApiKeyFromConfigOrMacOSKeychain + * cache invalidation so a stale prefetch doesn't shadow a fresh write. + */ +export function clearLegacyApiKeyPrefetch(): void { + legacyApiKeyPrefetch = null +} diff --git a/_all-in-one-upload/cc-haha/src/utils/sessionFileAccessHooks.ts b/_all-in-one-upload/cc-haha/src/utils/sessionFileAccessHooks.ts new file mode 100644 index 0000000..08d4ed5 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/sessionFileAccessHooks.ts @@ -0,0 +1,250 @@ +/** + * Session file access analytics hooks. + * Tracks access to session memory and transcript files via Read, Grep, Glob tools. + * Also tracks memdir file access via Read, Grep, Glob, Edit, and Write tools. + */ +import { feature } from 'bun:bundle' +import { registerHookCallbacks } from '../bootstrap/state.js' +import type { HookInput, HookJSONOutput } from '../entrypoints/agentSdkTypes.js' +import { + type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + logEvent, +} from '../services/analytics/index.js' +import { FILE_EDIT_TOOL_NAME } from '../tools/FileEditTool/constants.js' +import { inputSchema as editInputSchema } from '../tools/FileEditTool/types.js' +import { FileReadTool } from '../tools/FileReadTool/FileReadTool.js' +import { FILE_READ_TOOL_NAME } from '../tools/FileReadTool/prompt.js' +import { FileWriteTool } from '../tools/FileWriteTool/FileWriteTool.js' +import { FILE_WRITE_TOOL_NAME } from '../tools/FileWriteTool/prompt.js' +import { GlobTool } from '../tools/GlobTool/GlobTool.js' +import { GLOB_TOOL_NAME } from '../tools/GlobTool/prompt.js' +import { GrepTool } from '../tools/GrepTool/GrepTool.js' +import { GREP_TOOL_NAME } from '../tools/GrepTool/prompt.js' +import type { HookCallback } from '../types/hooks.js' +import { + detectSessionFileType, + detectSessionPatternType, + isAutoMemFile, + memoryScopeForPath, +} from './memoryFileDetection.js' + +/* eslint-disable @typescript-eslint/no-require-imports */ +const teamMemPaths = feature('TEAMMEM') + ? (require('../memdir/teamMemPaths.js') as typeof import('../memdir/teamMemPaths.js')) + : null +const teamMemWatcher = feature('TEAMMEM') + ? (require('../services/teamMemorySync/watcher.js') as typeof import('../services/teamMemorySync/watcher.js')) + : null +const memoryShapeTelemetry = feature('MEMORY_SHAPE_TELEMETRY') + ? (require('../memdir/memoryShapeTelemetry.js') as typeof import('../memdir/memoryShapeTelemetry.js')) + : null + +/* eslint-enable @typescript-eslint/no-require-imports */ +import { getSubagentLogName } from './agentContext.js' + +/** + * Extract the file path from a tool input for memdir detection. + * Covers Read (file_path), Edit (file_path), and Write (file_path). + */ +function getFilePathFromInput( + toolName: string, + toolInput: unknown, +): string | null { + switch (toolName) { + case FILE_READ_TOOL_NAME: { + const parsed = FileReadTool.inputSchema.safeParse(toolInput) + return parsed.success ? parsed.data.file_path : null + } + case FILE_EDIT_TOOL_NAME: { + const parsed = editInputSchema().safeParse(toolInput) + return parsed.success ? parsed.data.file_path : null + } + case FILE_WRITE_TOOL_NAME: { + const parsed = FileWriteTool.inputSchema.safeParse(toolInput) + return parsed.success ? parsed.data.file_path : null + } + default: + return null + } +} + +/** + * Extract file type from tool input. + * Returns the detected session file type or null. + */ +function getSessionFileTypeFromInput( + toolName: string, + toolInput: unknown, +): 'session_memory' | 'session_transcript' | null { + switch (toolName) { + case FILE_READ_TOOL_NAME: { + const parsed = FileReadTool.inputSchema.safeParse(toolInput) + if (!parsed.success) return null + return detectSessionFileType(parsed.data.file_path) + } + case GREP_TOOL_NAME: { + const parsed = GrepTool.inputSchema.safeParse(toolInput) + if (!parsed.success) return null + // Check path if provided + if (parsed.data.path) { + const pathType = detectSessionFileType(parsed.data.path) + if (pathType) return pathType + } + // Check glob pattern + if (parsed.data.glob) { + const globType = detectSessionPatternType(parsed.data.glob) + if (globType) return globType + } + return null + } + case GLOB_TOOL_NAME: { + const parsed = GlobTool.inputSchema.safeParse(toolInput) + if (!parsed.success) return null + // Check path if provided + if (parsed.data.path) { + const pathType = detectSessionFileType(parsed.data.path) + if (pathType) return pathType + } + // Check pattern + const patternType = detectSessionPatternType(parsed.data.pattern) + if (patternType) return patternType + return null + } + default: + return null + } +} + +/** + * Check if a tool use constitutes a memory file access. + * Detects session memory (via Read/Grep/Glob) and memdir access (via Read/Edit/Write). + * Uses the same conditions as the PostToolUse session file access hooks. + */ +export function isMemoryFileAccess( + toolName: string, + toolInput: unknown, +): boolean { + if (getSessionFileTypeFromInput(toolName, toolInput) === 'session_memory') { + return true + } + + const filePath = getFilePathFromInput(toolName, toolInput) + if ( + filePath && + (isAutoMemFile(filePath) || + (feature('TEAMMEM') && teamMemPaths!.isTeamMemFile(filePath))) + ) { + return true + } + + return false +} + +/** + * PostToolUse callback to log session file access events. + */ +async function handleSessionFileAccess( + input: HookInput, + _toolUseID: string | null, + _signal: AbortSignal | undefined, +): Promise { + if (input.hook_event_name !== 'PostToolUse') return {} + + const fileType = getSessionFileTypeFromInput( + input.tool_name, + input.tool_input, + ) + + const subagentName = getSubagentLogName() + const subagentProps = subagentName ? { subagent_name: subagentName } : {} + + if (fileType === 'session_memory') { + logEvent('tengu_session_memory_accessed', { ...subagentProps }) + } else if (fileType === 'session_transcript') { + logEvent('tengu_transcript_accessed', { ...subagentProps }) + } + + // Memdir access tracking + const filePath = getFilePathFromInput(input.tool_name, input.tool_input) + if (filePath && isAutoMemFile(filePath)) { + logEvent('tengu_memdir_accessed', { + tool: input.tool_name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...subagentProps, + }) + + switch (input.tool_name) { + case FILE_READ_TOOL_NAME: + logEvent('tengu_memdir_file_read', { ...subagentProps }) + break + case FILE_EDIT_TOOL_NAME: + logEvent('tengu_memdir_file_edit', { ...subagentProps }) + break + case FILE_WRITE_TOOL_NAME: + logEvent('tengu_memdir_file_write', { ...subagentProps }) + break + } + } + + // Team memory access tracking + if (feature('TEAMMEM') && filePath && teamMemPaths!.isTeamMemFile(filePath)) { + logEvent('tengu_team_mem_accessed', { + tool: input.tool_name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, + ...subagentProps, + }) + + switch (input.tool_name) { + case FILE_READ_TOOL_NAME: + logEvent('tengu_team_mem_file_read', { ...subagentProps }) + break + case FILE_EDIT_TOOL_NAME: + logEvent('tengu_team_mem_file_edit', { ...subagentProps }) + teamMemWatcher?.notifyTeamMemoryWrite() + break + case FILE_WRITE_TOOL_NAME: + logEvent('tengu_team_mem_file_write', { ...subagentProps }) + teamMemWatcher?.notifyTeamMemoryWrite() + break + } + } + + if (feature('MEMORY_SHAPE_TELEMETRY') && filePath) { + const scope = memoryScopeForPath(filePath) + if ( + scope !== null && + (input.tool_name === FILE_EDIT_TOOL_NAME || + input.tool_name === FILE_WRITE_TOOL_NAME) + ) { + memoryShapeTelemetry!.logMemoryWriteShape( + input.tool_name, + input.tool_input, + filePath, + scope, + ) + } + } + + return {} +} + +/** + * Register session file access tracking hooks. + * Called during CLI initialization. + */ +export function registerSessionFileAccessHooks(): void { + const hook: HookCallback = { + type: 'callback', + callback: handleSessionFileAccess, + timeout: 1, // Very short timeout - just logging + internal: true, + } + + registerHookCallbacks({ + PostToolUse: [ + { matcher: FILE_READ_TOOL_NAME, hooks: [hook] }, + { matcher: GREP_TOOL_NAME, hooks: [hook] }, + { matcher: GLOB_TOOL_NAME, hooks: [hook] }, + { matcher: FILE_EDIT_TOOL_NAME, hooks: [hook] }, + { matcher: FILE_WRITE_TOOL_NAME, hooks: [hook] }, + ], + }) +} diff --git a/_all-in-one-upload/cc-haha/src/utils/settings/settings.ts b/_all-in-one-upload/cc-haha/src/utils/settings/settings.ts new file mode 100644 index 0000000..3bea04a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/settings/settings.ts @@ -0,0 +1,1015 @@ +import { feature } from 'bun:bundle' +import mergeWith from 'lodash-es/mergeWith.js' +import { dirname, join, resolve } from 'path' +import { z } from 'zod/v4' +import { + getFlagSettingsInline, + getFlagSettingsPath, + getOriginalCwd, + getUseCoworkPlugins, +} from '../../bootstrap/state.js' +import { getRemoteManagedSettingsSyncFromCache } from '../../services/remoteManagedSettings/syncCacheState.js' +import { uniq } from '../array.js' +import { logForDebugging } from '../debug.js' +import { logForDiagnosticsNoPII } from '../diagLogs.js' +import { getClaudeConfigHomeDir, isEnvTruthy } from '../envUtils.js' +import { getErrnoCode, isENOENT } from '../errors.js' +import { writeFileSyncAndFlush_DEPRECATED } from '../file.js' +import { readFileSync } from '../fileRead.js' +import { getFsImplementation, safeResolvePath } from '../fsOperations.js' +import { addFileGlobRuleToGitignore } from '../git/gitignore.js' +import { safeParseJSON } from '../json.js' +import { logError } from '../log.js' +import { getPlatform } from '../platform.js' +import { clone, jsonStringify } from '../slowOperations.js' +import { profileCheckpoint } from '../startupProfiler.js' +import { + type EditableSettingSource, + getEnabledSettingSources, + type SettingSource, +} from './constants.js' +import { markInternalWrite } from './internalWrites.js' +import { + getManagedFilePath, + getManagedSettingsDropInDir, +} from './managedPath.js' +import { getHkcuSettings, getMdmSettings } from './mdm/settings.js' +import { + getCachedParsedFile, + getCachedSettingsForSource, + getPluginSettingsBase, + getSessionSettingsCache, + resetSettingsCache, + setCachedParsedFile, + setCachedSettingsForSource, + setSessionSettingsCache, +} from './settingsCache.js' +import { type SettingsJson, SettingsSchema } from './types.js' +import { + filterInvalidPermissionRules, + formatZodError, + type SettingsWithErrors, + type ValidationError, +} from './validation.js' + +/** + * Get the path to the managed settings file based on the current platform + */ +function getManagedSettingsFilePath(): string { + return join(getManagedFilePath(), 'managed-settings.json') +} + +/** + * Load file-based managed settings: managed-settings.json + managed-settings.d/*.json. + * + * managed-settings.json is merged first (lowest precedence / base), then drop-in + * files are sorted alphabetically and merged on top (higher precedence, later + * files win). This matches the systemd/sudoers drop-in convention: the base + * file provides defaults, drop-ins customize. Separate teams can ship + * independent policy fragments (e.g. 10-otel.json, 20-security.json) without + * coordinating edits to a single admin-owned file. + * + * Exported for testing. + */ +export function loadManagedFileSettings(): { + settings: SettingsJson | null + errors: ValidationError[] +} { + const errors: ValidationError[] = [] + let merged: SettingsJson = {} + let found = false + + const { settings, errors: baseErrors } = parseSettingsFile( + getManagedSettingsFilePath(), + ) + errors.push(...baseErrors) + if (settings && Object.keys(settings).length > 0) { + merged = mergeWith(merged, settings, settingsMergeCustomizer) + found = true + } + + const dropInDir = getManagedSettingsDropInDir() + try { + const entries = getFsImplementation() + .readdirSync(dropInDir) + .filter( + d => + (d.isFile() || d.isSymbolicLink()) && + d.name.endsWith('.json') && + !d.name.startsWith('.'), + ) + .map(d => d.name) + .sort() + for (const name of entries) { + const { settings, errors: fileErrors } = parseSettingsFile( + join(dropInDir, name), + ) + errors.push(...fileErrors) + if (settings && Object.keys(settings).length > 0) { + merged = mergeWith(merged, settings, settingsMergeCustomizer) + found = true + } + } + } catch (e) { + const code = getErrnoCode(e) + if (code !== 'ENOENT' && code !== 'ENOTDIR') { + logError(e) + } + } + + return { settings: found ? merged : null, errors } +} + +/** + * Check which file-based managed settings sources are present. + * Used by /status to show "(file)", "(drop-ins)", or "(file + drop-ins)". + */ +export function getManagedFileSettingsPresence(): { + hasBase: boolean + hasDropIns: boolean +} { + const { settings: base } = parseSettingsFile(getManagedSettingsFilePath()) + const hasBase = !!base && Object.keys(base).length > 0 + + let hasDropIns = false + const dropInDir = getManagedSettingsDropInDir() + try { + hasDropIns = getFsImplementation() + .readdirSync(dropInDir) + .some( + d => + (d.isFile() || d.isSymbolicLink()) && + d.name.endsWith('.json') && + !d.name.startsWith('.'), + ) + } catch { + // dir doesn't exist + } + + return { hasBase, hasDropIns } +} + +/** + * Handles file system errors appropriately + * @param error The error to handle + * @param path The file path that caused the error + */ +function handleFileSystemError(error: unknown, path: string): void { + if ( + typeof error === 'object' && + error && + 'code' in error && + error.code === 'ENOENT' + ) { + logForDebugging( + `Broken symlink or missing file encountered for settings.json at path: ${path}`, + ) + } else { + logError(error) + } +} + +/** + * Parses a settings file into a structured format + * @param path The path to the permissions file + * @param source The source of the settings (optional, for error reporting) + * @returns Parsed settings data and validation errors + */ +export function parseSettingsFile(path: string): { + settings: SettingsJson | null + errors: ValidationError[] +} { + const cached = getCachedParsedFile(path) + if (cached) { + // Clone so callers (e.g. mergeWith in getSettingsForSourceUncached, + // updateSettingsForSource) can't mutate the cached entry. + return { + settings: cached.settings ? clone(cached.settings) : null, + errors: cached.errors, + } + } + const result = parseSettingsFileUncached(path) + setCachedParsedFile(path, result) + // Clone the first return too — the caller may mutate before + // another caller reads the same cache entry. + return { + settings: result.settings ? clone(result.settings) : null, + errors: result.errors, + } +} + +function parseSettingsFileUncached(path: string): { + settings: SettingsJson | null + errors: ValidationError[] +} { + try { + const { resolvedPath } = safeResolvePath(getFsImplementation(), path) + const content = readFileSync(resolvedPath) + + if (content.trim() === '') { + return { settings: {}, errors: [] } + } + + const data = safeParseJSON(content, false) + + // Filter invalid permission rules before schema validation so one bad + // rule doesn't cause the entire settings file to be rejected. + const ruleWarnings = filterInvalidPermissionRules(data, path) + + const result = SettingsSchema().safeParse(data) + + if (!result.success) { + const errors = formatZodError(result.error, path) + return { settings: null, errors: [...ruleWarnings, ...errors] } + } + + return { settings: result.data, errors: ruleWarnings } + } catch (error) { + handleFileSystemError(error, path) + return { settings: null, errors: [] } + } +} + +/** + * Get the absolute path to the associated file root for a given settings source + * (e.g. for $PROJ_DIR/.claude/settings.json, returns $PROJ_DIR) + * @param source The source of the settings + * @returns The root path of the settings file + */ +export function getSettingsRootPathForSource(source: SettingSource): string { + switch (source) { + case 'userSettings': + return resolve(getClaudeConfigHomeDir()) + case 'policySettings': + case 'projectSettings': + case 'localSettings': { + return resolve(getOriginalCwd()) + } + case 'flagSettings': { + const path = getFlagSettingsPath() + return path ? dirname(resolve(path)) : resolve(getOriginalCwd()) + } + } +} + +/** + * Get the user settings filename based on cowork mode. + * Returns 'cowork_settings.json' when in cowork mode, 'settings.json' otherwise. + * + * Priority: + * 1. Session state (set by CLI flag --cowork) + * 2. Environment variable CLAUDE_CODE_USE_COWORK_PLUGINS + * 3. Default: 'settings.json' + */ +function getUserSettingsFilePath(): string { + if ( + getUseCoworkPlugins() || + isEnvTruthy(process.env.CLAUDE_CODE_USE_COWORK_PLUGINS) + ) { + return 'cowork_settings.json' + } + return 'settings.json' +} + +export function getSettingsFilePathForSource( + source: SettingSource, +): string | undefined { + switch (source) { + case 'userSettings': + return join( + getSettingsRootPathForSource(source), + getUserSettingsFilePath(), + ) + case 'projectSettings': + case 'localSettings': { + return join( + getSettingsRootPathForSource(source), + getRelativeSettingsFilePathForSource(source), + ) + } + case 'policySettings': + return getManagedSettingsFilePath() + case 'flagSettings': { + return getFlagSettingsPath() + } + } +} + +export function getRelativeSettingsFilePathForSource( + source: 'projectSettings' | 'localSettings', +): string { + switch (source) { + case 'projectSettings': + return join('.claude', 'settings.json') + case 'localSettings': + return join('.claude', 'settings.local.json') + } +} + +export function getSettingsForSource( + source: SettingSource, +): SettingsJson | null { + const cached = getCachedSettingsForSource(source) + if (cached !== undefined) return cached + const result = getSettingsForSourceUncached(source) + setCachedSettingsForSource(source, result) + return result +} + +function getSettingsForSourceUncached( + source: SettingSource, +): SettingsJson | null { + // For policySettings: first source wins (remote > HKLM/plist > file > HKCU) + if (source === 'policySettings') { + const remoteSettings = getRemoteManagedSettingsSyncFromCache() + if (remoteSettings && Object.keys(remoteSettings).length > 0) { + return remoteSettings + } + + const mdmResult = getMdmSettings() + if (Object.keys(mdmResult.settings).length > 0) { + return mdmResult.settings + } + + const { settings: fileSettings } = loadManagedFileSettings() + if (fileSettings) { + return fileSettings + } + + const hkcu = getHkcuSettings() + if (Object.keys(hkcu.settings).length > 0) { + return hkcu.settings + } + + return null + } + + const settingsFilePath = getSettingsFilePathForSource(source) + const { settings: fileSettings } = settingsFilePath + ? parseSettingsFile(settingsFilePath) + : { settings: null } + + // For flagSettings, merge in any inline settings set via the SDK + if (source === 'flagSettings') { + const inlineSettings = getFlagSettingsInline() + if (inlineSettings) { + const parsed = SettingsSchema().safeParse(inlineSettings) + if (parsed.success) { + return mergeWith( + fileSettings || {}, + parsed.data, + settingsMergeCustomizer, + ) as SettingsJson + } + } + } + + return fileSettings +} + +/** + * Get the origin of the highest-priority active policy settings source. + * Uses "first source wins" — returns the first source that has content. + * Priority: remote > plist/hklm > file (managed-settings.json) > hkcu + */ +export function getPolicySettingsOrigin(): + | 'remote' + | 'plist' + | 'hklm' + | 'file' + | 'hkcu' + | null { + // 1. Remote (highest) + const remoteSettings = getRemoteManagedSettingsSyncFromCache() + if (remoteSettings && Object.keys(remoteSettings).length > 0) { + return 'remote' + } + + // 2. Admin-only MDM (HKLM / macOS plist) + const mdmResult = getMdmSettings() + if (Object.keys(mdmResult.settings).length > 0) { + return getPlatform() === 'macos' ? 'plist' : 'hklm' + } + + // 3. managed-settings.json + managed-settings.d/ (file-based, requires admin) + const { settings: fileSettings } = loadManagedFileSettings() + if (fileSettings) { + return 'file' + } + + // 4. HKCU (lowest — user-writable) + const hkcu = getHkcuSettings() + if (Object.keys(hkcu.settings).length > 0) { + return 'hkcu' + } + + return null +} + +/** + * Merges `settings` into the existing settings for `source` using lodash mergeWith. + * + * To delete a key from a record field (e.g. enabledPlugins, extraKnownMarketplaces), + * set it to `undefined` — do NOT use `delete`. mergeWith only detects deletion when + * the key is present with an explicit `undefined` value. + */ +export function updateSettingsForSource( + source: EditableSettingSource, + settings: SettingsJson, +): { error: Error | null } { + if ( + (source as unknown) === 'policySettings' || + (source as unknown) === 'flagSettings' + ) { + return { error: null } + } + + // Create the folder if needed + const filePath = getSettingsFilePathForSource(source) + if (!filePath) { + return { error: null } + } + + try { + getFsImplementation().mkdirSync(dirname(filePath)) + + // Try to get existing settings with validation. Bypass the per-source + // cache — mergeWith below mutates its target (including nested refs), + // and mutating the cached object would leak unpersisted state if the + // write fails before resetSettingsCache(). + let existingSettings = getSettingsForSourceUncached(source) + + // If validation failed, check if file exists with a JSON syntax error + if (!existingSettings) { + let content: string | null = null + try { + content = readFileSync(filePath) + } catch (e) { + if (!isENOENT(e)) { + throw e + } + // File doesn't exist — fall through to merge with empty settings + } + if (content !== null) { + const rawData = safeParseJSON(content) + if (rawData === null) { + // JSON syntax error - return validation error instead of overwriting + // safeParseJSON will already log the error, so we'll just return the error here + return { + error: new Error( + `Invalid JSON syntax in settings file at ${filePath}`, + ), + } + } + if (rawData && typeof rawData === 'object') { + existingSettings = rawData as SettingsJson + logForDebugging( + `Using raw settings from ${filePath} due to validation failure`, + ) + } + } + } + + const updatedSettings = mergeWith( + existingSettings || {}, + settings, + ( + _objValue: unknown, + srcValue: unknown, + key: string | number | symbol, + object: Record, + ) => { + // Handle undefined as deletion + if (srcValue === undefined && object && typeof key === 'string') { + delete object[key] + return undefined + } + // For arrays, always replace with the provided array + // This puts the responsibility on the caller to compute the desired final state + if (Array.isArray(srcValue)) { + return srcValue + } + // For non-arrays, let lodash handle the default merge behavior + return undefined + }, + ) + + // Mark this as an internal write before writing the file + markInternalWrite(filePath) + + writeFileSyncAndFlush_DEPRECATED( + filePath, + jsonStringify(updatedSettings, null, 2) + '\n', + ) + + // Invalidate the session cache since settings have been updated + resetSettingsCache() + + if (source === 'localSettings') { + // Okay to add to gitignore async without awaiting + void addFileGlobRuleToGitignore( + getRelativeSettingsFilePathForSource('localSettings'), + getOriginalCwd(), + ) + } + } catch (e) { + const error = new Error( + `Failed to read raw settings from ${filePath}: ${e}`, + ) + logError(error) + return { error } + } + + return { error: null } +} + +/** + * Custom merge function for arrays - concatenate and deduplicate + */ +function mergeArrays(targetArray: T[], sourceArray: T[]): T[] { + return uniq([...targetArray, ...sourceArray]) +} + +/** + * Custom merge function for lodash mergeWith when merging settings. + * Arrays are concatenated and deduplicated; other values use default lodash merge behavior. + * Exported for testing. + */ +export function settingsMergeCustomizer( + objValue: unknown, + srcValue: unknown, +): unknown { + if (Array.isArray(objValue) && Array.isArray(srcValue)) { + return mergeArrays(objValue, srcValue) + } + // Return undefined to let lodash handle default merge behavior + return undefined +} + +/** + * Get a list of setting keys from managed settings for logging purposes. + * For certain nested settings (permissions, sandbox, hooks), expands to show + * one level of nesting (e.g., "permissions.allow"). For other settings, + * returns only the top-level key. + * + * @param settings The settings object to extract keys from + * @returns Sorted array of key paths + */ +export function getManagedSettingsKeysForLogging( + settings: SettingsJson, +): string[] { + // Use .strip() to get only valid schema keys + const validSettings = SettingsSchema().strip().parse(settings) as Record< + string, + unknown + > + const keysToExpand = ['permissions', 'sandbox', 'hooks'] + const allKeys: string[] = [] + + // Define valid nested keys for each nested setting we expand + const validNestedKeys: Record> = { + permissions: new Set([ + 'allow', + 'deny', + 'ask', + 'defaultMode', + 'disableBypassPermissionsMode', + ...(feature('TRANSCRIPT_CLASSIFIER') ? ['disableAutoMode'] : []), + 'additionalDirectories', + ]), + sandbox: new Set([ + 'enabled', + 'failIfUnavailable', + 'allowUnsandboxedCommands', + 'network', + 'filesystem', + 'ignoreViolations', + 'excludedCommands', + 'autoAllowBashIfSandboxed', + 'enableWeakerNestedSandbox', + 'enableWeakerNetworkIsolation', + 'ripgrep', + ]), + // For hooks, we use z.record with enum keys, so we validate separately + hooks: new Set([ + 'PreToolUse', + 'PostToolUse', + 'Notification', + 'UserPromptSubmit', + 'SessionStart', + 'SessionEnd', + 'Stop', + 'SubagentStop', + 'PreCompact', + 'PostCompact', + 'TeammateIdle', + 'TaskCreated', + 'TaskCompleted', + ]), + } + + for (const key of Object.keys(validSettings)) { + if ( + keysToExpand.includes(key) && + validSettings[key] && + typeof validSettings[key] === 'object' + ) { + // Expand nested keys for these special settings (one level deep only) + const nestedObj = validSettings[key] as Record + const validKeys = validNestedKeys[key] + + if (validKeys) { + for (const nestedKey of Object.keys(nestedObj)) { + // Only include known valid nested keys + if (validKeys.has(nestedKey)) { + allKeys.push(`${key}.${nestedKey}`) + } + } + } + } else { + // For other settings, just use the top-level key + allKeys.push(key) + } + } + + return allKeys.sort() +} + +// Flag to prevent infinite recursion when loading settings +let isLoadingSettings = false + +/** + * Load settings from disk without using cache + * This is the original implementation that actually reads from files + */ +function loadSettingsFromDisk(): SettingsWithErrors { + // Prevent recursive calls to loadSettingsFromDisk + if (isLoadingSettings) { + return { settings: {}, errors: [] } + } + + const startTime = Date.now() + profileCheckpoint('loadSettingsFromDisk_start') + logForDiagnosticsNoPII('info', 'settings_load_started') + + isLoadingSettings = true + try { + // Start with plugin settings as the lowest priority base. + // All file-based sources (user, project, local, flag, policy) override these. + // Plugin settings only contain allowlisted keys (e.g., agent) that are valid SettingsJson fields. + const pluginSettings = getPluginSettingsBase() + let mergedSettings: SettingsJson = {} + if (pluginSettings) { + mergedSettings = mergeWith( + mergedSettings, + pluginSettings, + settingsMergeCustomizer, + ) + } + const allErrors: ValidationError[] = [] + const seenErrors = new Set() + const seenFiles = new Set() + + // Merge settings from each source in priority order with deep merging + for (const source of getEnabledSettingSources()) { + // policySettings: "first source wins" — use the highest-priority source + // that has content. Priority: remote > HKLM/plist > managed-settings.json > HKCU + if (source === 'policySettings') { + let policySettings: SettingsJson | null = null + const policyErrors: ValidationError[] = [] + + // 1. Remote (highest priority) + const remoteSettings = getRemoteManagedSettingsSyncFromCache() + if (remoteSettings && Object.keys(remoteSettings).length > 0) { + const result = SettingsSchema().safeParse(remoteSettings) + if (result.success) { + policySettings = result.data + } else { + // Remote exists but is invalid — surface errors even as we fall through + policyErrors.push( + ...formatZodError(result.error, 'remote managed settings'), + ) + } + } + + // 2. Admin-only MDM (HKLM / macOS plist) + if (!policySettings) { + const mdmResult = getMdmSettings() + if (Object.keys(mdmResult.settings).length > 0) { + policySettings = mdmResult.settings + } + policyErrors.push(...mdmResult.errors) + } + + // 3. managed-settings.json + managed-settings.d/ (file-based, requires admin) + if (!policySettings) { + const { settings, errors } = loadManagedFileSettings() + if (settings) { + policySettings = settings + } + policyErrors.push(...errors) + } + + // 4. HKCU (lowest — user-writable, only if nothing above exists) + if (!policySettings) { + const hkcu = getHkcuSettings() + if (Object.keys(hkcu.settings).length > 0) { + policySettings = hkcu.settings + } + policyErrors.push(...hkcu.errors) + } + + // Merge the winning policy source into the settings chain + if (policySettings) { + mergedSettings = mergeWith( + mergedSettings, + policySettings, + settingsMergeCustomizer, + ) + } + for (const error of policyErrors) { + const errorKey = `${error.file}:${error.path}:${error.message}` + if (!seenErrors.has(errorKey)) { + seenErrors.add(errorKey) + allErrors.push(error) + } + } + + continue + } + + const filePath = getSettingsFilePathForSource(source) + if (filePath) { + const resolvedPath = resolve(filePath) + + // Skip if we've already loaded this file from another source + if (!seenFiles.has(resolvedPath)) { + seenFiles.add(resolvedPath) + + const { settings, errors } = parseSettingsFile(filePath) + + // Add unique errors (deduplication) + for (const error of errors) { + const errorKey = `${error.file}:${error.path}:${error.message}` + if (!seenErrors.has(errorKey)) { + seenErrors.add(errorKey) + allErrors.push(error) + } + } + + if (settings) { + mergedSettings = mergeWith( + mergedSettings, + settings, + settingsMergeCustomizer, + ) + } + } + } + + // For flagSettings, also merge any inline settings set via the SDK + if (source === 'flagSettings') { + const inlineSettings = getFlagSettingsInline() + if (inlineSettings) { + const parsed = SettingsSchema().safeParse(inlineSettings) + if (parsed.success) { + mergedSettings = mergeWith( + mergedSettings, + parsed.data, + settingsMergeCustomizer, + ) + } + } + } + } + + logForDiagnosticsNoPII('info', 'settings_load_completed', { + duration_ms: Date.now() - startTime, + source_count: seenFiles.size, + error_count: allErrors.length, + }) + + return { settings: mergedSettings, errors: allErrors } + } finally { + isLoadingSettings = false + } +} + +/** + * Get merged settings from all sources in priority order + * Settings are merged from lowest to highest priority: + * userSettings -> projectSettings -> localSettings -> policySettings + * + * This function returns a snapshot of settings at the time of call. + * For React components, prefer using useSettings() hook for reactive updates + * when settings change on disk. + * + * Uses session-level caching to avoid repeated file I/O. + * Cache is invalidated when settings files change via resetSettingsCache(). + * + * @returns Merged settings from all available sources (always returns at least empty object) + */ +export function getInitialSettings(): SettingsJson { + const { settings } = getSettingsWithErrors() + return settings || {} +} + +/** + * @deprecated Use getInitialSettings() instead. This alias exists for backwards compatibility. + */ +export const getSettings_DEPRECATED = getInitialSettings + +export type SettingsWithSources = { + effective: SettingsJson + /** Ordered low-to-high priority — later entries override earlier ones. */ + sources: Array<{ source: SettingSource; settings: SettingsJson }> +} + +/** + * Get the effective merged settings alongside the raw per-source settings, + * in merge-priority order. Only includes sources that are enabled and have + * non-empty content. + * + * Always reads fresh from disk — resets the session cache so that `effective` + * and `sources` are consistent even if the change detector hasn't fired yet. + */ +export function getSettingsWithSources(): SettingsWithSources { + // Reset both caches so getSettingsForSource (per-source cache) and + // getInitialSettings (session cache) agree on the current disk state. + resetSettingsCache() + const sources: SettingsWithSources['sources'] = [] + for (const source of getEnabledSettingSources()) { + const settings = getSettingsForSource(source) + if (settings && Object.keys(settings).length > 0) { + sources.push({ source, settings }) + } + } + return { effective: getInitialSettings(), sources } +} + +/** + * Get merged settings and validation errors from all sources + * This function now uses session-level caching to avoid repeated file I/O. + * Settings changes require Claude Code restart, so cache is valid for entire session. + * @returns Merged settings and all validation errors encountered + */ +export function getSettingsWithErrors(): SettingsWithErrors { + // Use cached result if available + const cached = getSessionSettingsCache() + if (cached !== null) { + return cached + } + + // Load from disk and cache the result + const result = loadSettingsFromDisk() + profileCheckpoint('loadSettingsFromDisk_end') + setSessionSettingsCache(result) + return result +} + +/** + * Check if any raw settings file contains a specific key, regardless of validation. + * This is useful for detecting user intent even when settings validation fails. + * For example, if a user set cleanupPeriodDays but has validation errors elsewhere, + * we can detect they explicitly configured cleanup and skip cleanup rather than + * falling back to defaults. + */ +/** + * Returns true if any trusted settings source has accepted the bypass + * permissions mode dialog. projectSettings is intentionally excluded — + * a malicious project could otherwise auto-bypass the dialog (RCE risk). + */ +export function hasSkipDangerousModePermissionPrompt(): boolean { + return !!( + getSettingsForSource('userSettings')?.skipDangerousModePermissionPrompt || + getSettingsForSource('localSettings')?.skipDangerousModePermissionPrompt || + getSettingsForSource('flagSettings')?.skipDangerousModePermissionPrompt || + getSettingsForSource('policySettings')?.skipDangerousModePermissionPrompt + ) +} + +/** + * Returns true if any trusted settings source has accepted the auto + * mode opt-in dialog. projectSettings is intentionally excluded — + * a malicious project could otherwise auto-bypass the dialog (RCE risk). + */ +export function hasAutoModeOptIn(): boolean { + if (feature('TRANSCRIPT_CLASSIFIER')) { + const user = getSettingsForSource('userSettings')?.skipAutoPermissionPrompt + const local = + getSettingsForSource('localSettings')?.skipAutoPermissionPrompt + const flag = getSettingsForSource('flagSettings')?.skipAutoPermissionPrompt + const policy = + getSettingsForSource('policySettings')?.skipAutoPermissionPrompt + const result = !!(user || local || flag || policy) + logForDebugging( + `[auto-mode] hasAutoModeOptIn=${result} skipAutoPermissionPrompt: user=${user} local=${local} flag=${flag} policy=${policy}`, + ) + return result + } + return false +} + +/** + * Returns whether plan mode should use auto mode semantics. Default true + * (opt-out). Returns false if any trusted source explicitly sets false. + * projectSettings is excluded so a malicious project can't control this. + */ +export function getUseAutoModeDuringPlan(): boolean { + if (feature('TRANSCRIPT_CLASSIFIER')) { + return ( + getSettingsForSource('policySettings')?.useAutoModeDuringPlan !== false && + getSettingsForSource('flagSettings')?.useAutoModeDuringPlan !== false && + getSettingsForSource('userSettings')?.useAutoModeDuringPlan !== false && + getSettingsForSource('localSettings')?.useAutoModeDuringPlan !== false + ) + } + return true +} + +/** + * Returns the merged autoMode config from trusted settings sources. + * Only available when TRANSCRIPT_CLASSIFIER is active; returns undefined otherwise. + * projectSettings is intentionally excluded — a malicious project could + * otherwise inject classifier allow/deny rules (RCE risk). + */ +export function getAutoModeConfig(): + | { allow?: string[]; soft_deny?: string[]; environment?: string[] } + | undefined { + if (feature('TRANSCRIPT_CLASSIFIER')) { + const schema = z.object({ + allow: z.array(z.string()).optional(), + soft_deny: z.array(z.string()).optional(), + deny: z.array(z.string()).optional(), + environment: z.array(z.string()).optional(), + }) + + const allow: string[] = [] + const soft_deny: string[] = [] + const environment: string[] = [] + + for (const source of [ + 'userSettings', + 'localSettings', + 'flagSettings', + 'policySettings', + ] as const) { + const settings = getSettingsForSource(source) + if (!settings) continue + const result = schema.safeParse( + (settings as Record).autoMode, + ) + if (result.success) { + if (result.data.allow) allow.push(...result.data.allow) + if (result.data.soft_deny) soft_deny.push(...result.data.soft_deny) + if (process.env.USER_TYPE === 'ant') { + if (result.data.deny) soft_deny.push(...result.data.deny) + } + if (result.data.environment) + environment.push(...result.data.environment) + } + } + + if (allow.length > 0 || soft_deny.length > 0 || environment.length > 0) { + return { + ...(allow.length > 0 && { allow }), + ...(soft_deny.length > 0 && { soft_deny }), + ...(environment.length > 0 && { environment }), + } + } + } + return undefined +} + +export function rawSettingsContainsKey(key: string): boolean { + for (const source of getEnabledSettingSources()) { + // Skip policySettings - we only care about user-configured settings + if (source === 'policySettings') { + continue + } + + const filePath = getSettingsFilePathForSource(source) + if (!filePath) { + continue + } + + try { + const { resolvedPath } = safeResolvePath(getFsImplementation(), filePath) + const content = readFileSync(resolvedPath) + if (!content.trim()) { + continue + } + + const rawData = safeParseJSON(content, false) + if (rawData && typeof rawData === 'object' && key in rawData) { + return true + } + } catch (error) { + // File not found is expected - not all settings files exist + // Other errors (permissions, I/O) should be tracked + handleFileSystemError(error, filePath) + } + } + + return false +} diff --git a/_all-in-one-upload/cc-haha/src/utils/shell/powershellDetection.ts b/_all-in-one-upload/cc-haha/src/utils/shell/powershellDetection.ts new file mode 100644 index 0000000..acc3fb7 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/shell/powershellDetection.ts @@ -0,0 +1,107 @@ +import { realpath, stat } from 'fs/promises' +import { getPlatform } from '../platform.js' +import { which } from '../which.js' + +async function probePath(p: string): Promise { + try { + return (await stat(p)).isFile() ? p : null + } catch { + return null + } +} + +/** + * Attempts to find PowerShell on the system via PATH. + * Prefers pwsh (PowerShell Core 7+), falls back to powershell (5.1). + * + * On Linux, if PATH resolves to a snap launcher (/snap/…) — directly or + * via a symlink chain like /usr/bin/pwsh → /snap/bin/pwsh — probe known + * apt/rpm install locations instead: the snap launcher can hang in + * subprocesses while snapd initializes confinement, but the underlying + * binary at /opt/microsoft/powershell/7/pwsh is reliable. On + * Windows/macOS, PATH is sufficient. + */ +export async function findPowerShell(): Promise { + const pwshPath = await which('pwsh') + if (pwshPath) { + // Snap launcher hangs in subprocesses. Prefer the direct binary. + // Check both the resolved PATH entry and its symlink target: on + // some distros /usr/bin/pwsh is a symlink to /snap/bin/pwsh, which + // would bypass a naive startsWith('/snap/') on the which() result. + if (getPlatform() === 'linux') { + const resolved = await realpath(pwshPath).catch(() => pwshPath) + if (pwshPath.startsWith('/snap/') || resolved.startsWith('/snap/')) { + const direct = + (await probePath('/opt/microsoft/powershell/7/pwsh')) ?? + (await probePath('/usr/bin/pwsh')) + if (direct) { + const directResolved = await realpath(direct).catch(() => direct) + if ( + !direct.startsWith('/snap/') && + !directResolved.startsWith('/snap/') + ) { + return direct + } + } + } + } + return pwshPath + } + + const powershellPath = await which('powershell') + if (powershellPath) { + return powershellPath + } + + return null +} + +let cachedPowerShellPath: Promise | null = null + +/** + * Gets the cached PowerShell path. Returns a memoized promise that + * resolves to the PowerShell executable path or null. + */ +export function getCachedPowerShellPath(): Promise { + if (!cachedPowerShellPath) { + cachedPowerShellPath = findPowerShell() + } + return cachedPowerShellPath +} + +export type PowerShellEdition = 'core' | 'desktop' + +/** + * Infers the PowerShell edition from the binary name without spawning. + * - `pwsh` / `pwsh.exe` → 'core' (PowerShell 7+: supports `&&`, `||`, `?:`, `??`) + * - `powershell` / `powershell.exe` → 'desktop' (Windows PowerShell 5.1: + * no pipeline chain operators, stderr-sets-$? bug, UTF-16 default encoding) + * + * PowerShell 6 (also `pwsh`, no `&&`) has been EOL since 2020 and is not + * a realistic install target, so 'core' safely implies 7+ semantics. + * + * Used by the tool prompt to give version-appropriate syntax guidance so + * the model doesn't emit `cmd1 && cmd2` on 5.1 (parser error) or avoid + * `&&` on 7+ where it's the correct short-circuiting operator. + */ +export async function getPowerShellEdition(): Promise { + const p = await getCachedPowerShellPath() + if (!p) return null + // basename without extension, case-insensitive. Covers: + // C:\Program Files\PowerShell\7\pwsh.exe + // /opt/microsoft/powershell/7/pwsh + // C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe + const base = p + .split(/[/\\]/) + .pop()! + .toLowerCase() + .replace(/\.exe$/, '') + return base === 'pwsh' ? 'core' : 'desktop' +} + +/** + * Resets the cached PowerShell path. Only for testing. + */ +export function resetPowerShellCache(): void { + cachedPowerShellPath = null +} diff --git a/_all-in-one-upload/cc-haha/src/utils/sinks.ts b/_all-in-one-upload/cc-haha/src/utils/sinks.ts new file mode 100644 index 0000000..386ce6a --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/sinks.ts @@ -0,0 +1,16 @@ +import { initializeAnalyticsSink } from '../services/analytics/sink.js' +import { initializeErrorLogSink } from './errorLogSink.js' + +/** + * Attach error log and analytics sinks, draining any events queued before + * attachment. Both inits are idempotent. Called from setup() for the default + * command; other entrypoints (subcommands, daemon, bridge) call this directly + * since they bypass setup(). + * + * Leaf module — kept out of setup.ts to avoid the setup → commands → bridge + * → setup import cycle. + */ +export function initSinks(): void { + initializeErrorLogSink() + initializeAnalyticsSink() +} diff --git a/_all-in-one-upload/cc-haha/src/utils/staticRender.tsx b/_all-in-one-upload/cc-haha/src/utils/staticRender.tsx new file mode 100644 index 0000000..6a6917b --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/staticRender.tsx @@ -0,0 +1,116 @@ +import { c as _c } from "react/compiler-runtime"; +import * as React from 'react'; +import { useLayoutEffect } from 'react'; +import { PassThrough } from 'stream'; +import stripAnsi from 'strip-ansi'; +import { render, useApp } from '../ink.js'; + +// This is a workaround for the fact that Ink doesn't support multiple +// components in the same render tree. Instead of using a we just render +// the component to a string and then print it to stdout + +/** + * Wrapper component that exits after rendering. + * Uses useLayoutEffect to ensure we wait for React's commit phase to complete + * before exiting. This is more robust than process.nextTick() for React 19's + * async render cycle. + */ +function RenderOnceAndExit(t0) { + const $ = _c(5); + const { + children + } = t0; + const { + exit + } = useApp(); + let t1; + let t2; + if ($[0] !== exit) { + t1 = () => { + const timer = setTimeout(exit, 0); + return () => clearTimeout(timer); + }; + t2 = [exit]; + $[0] = exit; + $[1] = t1; + $[2] = t2; + } else { + t1 = $[1]; + t2 = $[2]; + } + useLayoutEffect(t1, t2); + let t3; + if ($[3] !== children) { + t3 = <>{children}; + $[3] = children; + $[4] = t3; + } else { + t3 = $[4]; + } + return t3; +} + +// DEC synchronized update markers used by terminals +const SYNC_START = '\x1B[?2026h'; +const SYNC_END = '\x1B[?2026l'; + +/** + * Extracts content from the first complete frame in Ink's output. + * Ink with non-TTY stdout outputs multiple frames, each wrapped in DEC synchronized + * update sequences ([?2026h ... [?2026l). We only want the first frame's content. + */ +function extractFirstFrame(output: string): string { + const startIndex = output.indexOf(SYNC_START); + if (startIndex === -1) return output; + const contentStart = startIndex + SYNC_START.length; + const endIndex = output.indexOf(SYNC_END, contentStart); + if (endIndex === -1) return output; + return output.slice(contentStart, endIndex); +} + +/** + * Renders a React node to a string with ANSI escape codes (for terminal output). + */ +export function renderToAnsiString(node: React.ReactNode, columns?: number): Promise { + return new Promise(async resolve => { + let output = ''; + + // Capture all writes. Set .columns so Ink (ink.tsx:~165) picks up a + // chosen width instead of PassThrough's undefined → 80 fallback — + // useful for rendering at terminal width for file dumps that should + // match what the user sees on screen. + const stream = new PassThrough(); + if (columns !== undefined) { + ; + (stream as unknown as { + columns: number; + }).columns = columns; + } + stream.on('data', chunk => { + output += chunk.toString(); + }); + + // Render the component wrapped in RenderOnceAndExit + // Non-TTY stdout (PassThrough) gives full-frame output instead of diffs + const instance = await render({node}, { + stdout: stream as unknown as NodeJS.WriteStream, + patchConsole: false + }); + + // Wait for the component to exit naturally + await instance.waitUntilExit(); + + // Extract only the first frame's content to avoid duplication + // (Ink outputs multiple frames in non-TTY mode) + await resolve(extractFirstFrame(output)); + }); +} + +/** + * Renders a React node to a plain text string (ANSI codes stripped). + */ +export async function renderToString(node: React.ReactNode, columns?: number): Promise { + const output = await renderToAnsiString(node, columns); + return stripAnsi(output); +} +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsInVzZUxheW91dEVmZmVjdCIsIlBhc3NUaHJvdWdoIiwic3RyaXBBbnNpIiwicmVuZGVyIiwidXNlQXBwIiwiUmVuZGVyT25jZUFuZEV4aXQiLCJ0MCIsIiQiLCJfYyIsImNoaWxkcmVuIiwiZXhpdCIsInQxIiwidDIiLCJ0aW1lciIsInNldFRpbWVvdXQiLCJjbGVhclRpbWVvdXQiLCJ0MyIsIlNZTkNfU1RBUlQiLCJTWU5DX0VORCIsImV4dHJhY3RGaXJzdEZyYW1lIiwib3V0cHV0Iiwic3RhcnRJbmRleCIsImluZGV4T2YiLCJjb250ZW50U3RhcnQiLCJsZW5ndGgiLCJlbmRJbmRleCIsInNsaWNlIiwicmVuZGVyVG9Bbn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QyxDQUFDOztJQUVGO0lBQ0E7SUFDQSxNQUFNQyxRQUFRLEdBQUcsTUFBTW5DLE1BQU0sQ0FDM0IsQ0FBQyxpQkFBaUIsQ0FBQyxDQUFDeUIsSUFBSSxDQUFDLEVBQUUsaUJBQWlCLENBQUMsRUFDN0M7TUFDRVcsTUFBTSxFQUFFTixNQUFNLElBQUksT0FBTyxJQUFJTyxNQUFNLENBQUNDLFdBQVc7TUFDL0NDLFlBQVksRUFBRTtJQUNoQixDQUNGLENBQUM7O0lBRUQ7SUFDQSxNQUFNSixRQUFRLENBQUNLLGFBQWEsQ0FBQyxDQUFDOztJQUU5QjtJQUNBO0lBQ0EsTUFBTVgsT0FBTyxDQUFDYixpQkFBaUIsQ0FBQ0MsTUFBTSxDQUFDLENBQUM7RUFDMUMsQ0FBQyxDQUFDO0FBQ0o7O0FBRUE7QUFDQTtBQUNBO0FBQ0EsT0FBTyxlQUFld0IsY0FBY0EsQ0FDbENoQixJQUFJLEVBQUU3QixLQUFLLENBQUM4QixTQUFTLEVBQ3JCQyxPQUFnQixDQUFSLEVBQUUsTUFBTSxDQUNqQixFQUFFQyxPQUFPLENBQUMsTUFBTSxDQUFDLENBQUM7RUFDakIsTUFBTVgsTUFBTSxHQUFHLE1BQU1PLGtCQUFrQixDQUFDQyxJQUFJLEVBQUVFLE9BQU8sQ0FBQztFQUN0RCxPQUFPNUIsU0FBUyxDQUFDa0IsTUFBTSxDQUFDO0FBQzFCIiwiaWdub3JlTGlzdCI6W119 \ No newline at end of file diff --git a/_all-in-one-upload/cc-haha/src/utils/subprocessEnv.ts b/_all-in-one-upload/cc-haha/src/utils/subprocessEnv.ts new file mode 100644 index 0000000..4eb62c9 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/subprocessEnv.ts @@ -0,0 +1,99 @@ +import { isEnvTruthy } from './envUtils.js' + +/** + * Env vars to strip from subprocess environments when running inside GitHub + * Actions. This prevents prompt-injection attacks from exfiltrating secrets + * via shell expansion (e.g., ${ANTHROPIC_API_KEY}) in Bash tool commands. + * + * The parent claude process keeps these vars (needed for API calls, lazy + * credential reads). Only child processes (bash, shell snapshot, MCP stdio, LSP, hooks) are scrubbed. + * + * GITHUB_TOKEN / GH_TOKEN are intentionally NOT scrubbed — wrapper scripts + * (gh.sh) need them to call the GitHub API. That token is job-scoped and + * expires when the workflow ends. + */ +const GHA_SUBPROCESS_SCRUB = [ + // Anthropic auth — claude re-reads these per-request, subprocesses don't need them + 'ANTHROPIC_API_KEY', + 'CLAUDE_CODE_OAUTH_TOKEN', + 'ANTHROPIC_AUTH_TOKEN', + 'ANTHROPIC_FOUNDRY_API_KEY', + 'ANTHROPIC_CUSTOM_HEADERS', + + // OTLP exporter headers — documented to carry Authorization=Bearer tokens + // for monitoring backends; read in-process by OTEL SDK, subprocesses never need them + 'OTEL_EXPORTER_OTLP_HEADERS', + 'OTEL_EXPORTER_OTLP_LOGS_HEADERS', + 'OTEL_EXPORTER_OTLP_METRICS_HEADERS', + 'OTEL_EXPORTER_OTLP_TRACES_HEADERS', + + // Cloud provider creds — same pattern (lazy SDK reads) + 'AWS_SECRET_ACCESS_KEY', + 'AWS_SESSION_TOKEN', + 'AWS_BEARER_TOKEN_BEDROCK', + 'GOOGLE_APPLICATION_CREDENTIALS', + 'AZURE_CLIENT_SECRET', + 'AZURE_CLIENT_CERTIFICATE_PATH', + + // GitHub Actions OIDC — consumed by the action's JS before claude spawns; + // leaking these allows minting an App installation token → repo takeover + 'ACTIONS_ID_TOKEN_REQUEST_TOKEN', + 'ACTIONS_ID_TOKEN_REQUEST_URL', + + // GitHub Actions artifact/cache API — cache poisoning → supply-chain pivot + 'ACTIONS_RUNTIME_TOKEN', + 'ACTIONS_RUNTIME_URL', + + // claude-code-action-specific duplicates — action JS consumes these during + // prepare, before spawning claude. ALL_INPUTS contains anthropic_api_key as JSON. + 'ALL_INPUTS', + 'OVERRIDE_GITHUB_TOKEN', + 'DEFAULT_WORKFLOW_TOKEN', + 'SSH_SIGNING_KEY', +] as const + +/** + * Returns a copy of process.env with sensitive secrets stripped, for use when + * spawning subprocesses (Bash tool, shell snapshot, MCP stdio servers, LSP + * servers, shell hooks). + * + * Gated on CLAUDE_CODE_SUBPROCESS_ENV_SCRUB. claude-code-action sets this + * automatically when `allowed_non_write_users` is configured — the flag that + * exposes a workflow to untrusted content (prompt injection surface). + */ +// Registered by init.ts after the upstreamproxy module is dynamically imported +// in CCR sessions. Stays undefined in non-CCR startups so we never pull in the +// upstreamproxy module graph (upstreamproxy.ts + relay.ts) via a static import. +let _getUpstreamProxyEnv: (() => Record) | undefined + +/** + * Called from init.ts to wire up the proxy env function after the upstreamproxy + * module has been lazily loaded. Must be called before any subprocess is spawned. + */ +export function registerUpstreamProxyEnvFn( + fn: () => Record, +): void { + _getUpstreamProxyEnv = fn +} + +export function subprocessEnv(): NodeJS.ProcessEnv { + // CCR upstreamproxy: inject HTTPS_PROXY + CA bundle vars so curl/gh/python + // in agent subprocesses route through the local relay. Returns {} when the + // proxy is disabled or not registered (non-CCR), so this is a no-op outside + // CCR containers. + const proxyEnv = _getUpstreamProxyEnv?.() ?? {} + + if (!isEnvTruthy(process.env.CLAUDE_CODE_SUBPROCESS_ENV_SCRUB)) { + return Object.keys(proxyEnv).length > 0 + ? { ...process.env, ...proxyEnv } + : process.env + } + const env = { ...process.env, ...proxyEnv } + for (const k of GHA_SUBPROCESS_SCRUB) { + delete env[k] + // GitHub Actions auto-creates INPUT_ for `with:` inputs, duplicating + // secrets like INPUT_ANTHROPIC_API_KEY. No-op for vars that aren't action inputs. + delete env[`INPUT_${k}`] + } + return env +} diff --git a/_all-in-one-upload/cc-haha/src/utils/swarm/backends/TmuxBackend.ts b/_all-in-one-upload/cc-haha/src/utils/swarm/backends/TmuxBackend.ts new file mode 100644 index 0000000..402afd8 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/swarm/backends/TmuxBackend.ts @@ -0,0 +1,764 @@ +import type { AgentColorName } from '../../../tools/AgentTool/agentColorManager.js' +import { logForDebugging } from '../../../utils/debug.js' +import { execFileNoThrow } from '../../../utils/execFileNoThrow.js' +import { logError } from '../../../utils/log.js' +import { count } from '../../array.js' +import { sleep } from '../../sleep.js' +import { + getSwarmSocketName, + HIDDEN_SESSION_NAME, + SWARM_SESSION_NAME, + SWARM_VIEW_WINDOW_NAME, + TMUX_COMMAND, +} from '../constants.js' +import { + getLeaderPaneId, + isInsideTmux as isInsideTmuxFromDetection, + isTmuxAvailable, +} from './detection.js' +import { registerTmuxBackend } from './registry.js' +import type { CreatePaneResult, PaneBackend, PaneId } from './types.js' + +// Track whether the first pane has been used for external swarm session +let firstPaneUsedForExternal = false + +// Cached leader window target (session:window format) to avoid repeated queries +let cachedLeaderWindowTarget: string | null = null + +// Lock mechanism to prevent race conditions when spawning teammates in parallel +let paneCreationLock: Promise = Promise.resolve() + +// Delay after pane creation to allow shell initialization (loading rc files, prompts, etc.) +// 200ms is enough for most shell configurations including slow ones like starship/oh-my-zsh +const PANE_SHELL_INIT_DELAY_MS = 200 + +function waitForPaneShellReady(): Promise { + return sleep(PANE_SHELL_INIT_DELAY_MS) +} + +/** + * Acquires a lock for pane creation, ensuring sequential execution. + * Returns a release function that must be called when done. + */ +function acquirePaneCreationLock(): Promise<() => void> { + let release: () => void + const newLock = new Promise(resolve => { + release = resolve + }) + + const previousLock = paneCreationLock + paneCreationLock = newLock + + return previousLock.then(() => release!) +} + +/** + * Gets the tmux color name for a given agent color. + * These are tmux's built-in color names that work with pane-border-style. + */ +function getTmuxColorName(color: AgentColorName): string { + const tmuxColors: Record = { + red: 'red', + blue: 'blue', + green: 'green', + yellow: 'yellow', + purple: 'magenta', + orange: 'colour208', + pink: 'colour205', + cyan: 'cyan', + } + return tmuxColors[color] +} + +/** + * Runs a tmux command in the user's original tmux session (no socket override). + * Use this for operations that interact with the user's tmux panes (split-pane with leader). + */ +function runTmuxInUserSession( + args: string[], +): Promise<{ stdout: string; stderr: string; code: number }> { + return execFileNoThrow(TMUX_COMMAND, args) +} + +/** + * Runs a tmux command in the external swarm socket. + * Use this for operations in the standalone swarm session (when user is not in tmux). + */ +function runTmuxInSwarm( + args: string[], +): Promise<{ stdout: string; stderr: string; code: number }> { + return execFileNoThrow(TMUX_COMMAND, ['-L', getSwarmSocketName(), ...args]) +} + +/** + * TmuxBackend implements PaneBackend using tmux for pane management. + * + * When running INSIDE tmux (leader is in tmux): + * - Splits the current window to add teammates alongside the leader + * - Leader stays on left (30%), teammates on right (70%) + * + * When running OUTSIDE tmux (leader is in regular terminal): + * - Creates a claude-swarm session with a swarm-view window + * - All teammates are equally distributed (no leader pane) + */ +export class TmuxBackend implements PaneBackend { + readonly type = 'tmux' as const + readonly displayName = 'tmux' + readonly supportsHideShow = true + + /** + * Checks if tmux is installed and available. + * Delegates to detection.ts for consistent detection logic. + */ + async isAvailable(): Promise { + return isTmuxAvailable() + } + + /** + * Checks if we're currently running inside a tmux session. + * Delegates to detection.ts for consistent detection logic. + */ + async isRunningInside(): Promise { + return isInsideTmuxFromDetection() + } + + /** + * Creates a new teammate pane in the swarm view. + * Uses a lock to prevent race conditions when multiple teammates are spawned in parallel. + */ + async createTeammatePaneInSwarmView( + name: string, + color: AgentColorName, + ): Promise { + const releaseLock = await acquirePaneCreationLock() + + try { + const insideTmux = await this.isRunningInside() + + if (insideTmux) { + return await this.createTeammatePaneWithLeader(name, color) + } + + return await this.createTeammatePaneExternal(name, color) + } finally { + releaseLock() + } + } + + /** + * Sends a command to a specific pane. + */ + async sendCommandToPane( + paneId: PaneId, + command: string, + useExternalSession = false, + ): Promise { + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + const result = await runTmux(['send-keys', '-t', paneId, command, 'Enter']) + + if (result.code !== 0) { + throw new Error( + `Failed to send command to pane ${paneId}: ${result.stderr}`, + ) + } + } + + /** + * Sets the border color for a specific pane. + */ + async setPaneBorderColor( + paneId: PaneId, + color: AgentColorName, + useExternalSession = false, + ): Promise { + const tmuxColor = getTmuxColorName(color) + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + + // Set pane-specific border style using pane options (requires tmux 3.2+) + await runTmux([ + 'select-pane', + '-t', + paneId, + '-P', + `bg=default,fg=${tmuxColor}`, + ]) + + await runTmux([ + 'set-option', + '-p', + '-t', + paneId, + 'pane-border-style', + `fg=${tmuxColor}`, + ]) + + await runTmux([ + 'set-option', + '-p', + '-t', + paneId, + 'pane-active-border-style', + `fg=${tmuxColor}`, + ]) + } + + /** + * Sets the title for a pane (shown in pane border if pane-border-status is set). + */ + async setPaneTitle( + paneId: PaneId, + name: string, + color: AgentColorName, + useExternalSession = false, + ): Promise { + const tmuxColor = getTmuxColorName(color) + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + + // Set the pane title + await runTmux(['select-pane', '-t', paneId, '-T', name]) + + // Enable pane border status with colored format + await runTmux([ + 'set-option', + '-p', + '-t', + paneId, + 'pane-border-format', + `#[fg=${tmuxColor},bold] #{pane_title} #[default]`, + ]) + } + + /** + * Enables pane border status for a window (shows pane titles). + */ + async enablePaneBorderStatus( + windowTarget?: string, + useExternalSession = false, + ): Promise { + const target = windowTarget || (await this.getCurrentWindowTarget()) + if (!target) { + return + } + + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + await runTmux([ + 'set-option', + '-w', + '-t', + target, + 'pane-border-status', + 'top', + ]) + } + + /** + * Rebalances panes to achieve the desired layout. + */ + async rebalancePanes( + windowTarget: string, + hasLeader: boolean, + ): Promise { + if (hasLeader) { + await this.rebalancePanesWithLeader(windowTarget) + } else { + await this.rebalancePanesTiled(windowTarget) + } + } + + /** + * Kills/closes a specific pane. + */ + async killPane(paneId: PaneId, useExternalSession = false): Promise { + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + const result = await runTmux(['kill-pane', '-t', paneId]) + return result.code === 0 + } + + /** + * Hides a pane by moving it to a detached hidden session. + * Creates the hidden session if it doesn't exist, then uses break-pane to move the pane there. + */ + async hidePane(paneId: PaneId, useExternalSession = false): Promise { + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + + // Create hidden session if it doesn't exist (detached, not visible) + await runTmux(['new-session', '-d', '-s', HIDDEN_SESSION_NAME]) + + // Move the pane to the hidden session + const result = await runTmux([ + 'break-pane', + '-d', + '-s', + paneId, + '-t', + `${HIDDEN_SESSION_NAME}:`, + ]) + + if (result.code === 0) { + logForDebugging(`[TmuxBackend] Hidden pane ${paneId}`) + } else { + logForDebugging( + `[TmuxBackend] Failed to hide pane ${paneId}: ${result.stderr}`, + ) + } + + return result.code === 0 + } + + /** + * Shows a previously hidden pane by joining it back into the target window. + * Uses `tmux join-pane` to move the pane back, then reapplies main-vertical layout + * with leader at 30%. + */ + async showPane( + paneId: PaneId, + targetWindowOrPane: string, + useExternalSession = false, + ): Promise { + const runTmux = useExternalSession ? runTmuxInSwarm : runTmuxInUserSession + + // join-pane -s: source pane to move + // -t: target window/pane to join into + // -h: join horizontally (side by side) + const result = await runTmux([ + 'join-pane', + '-h', + '-s', + paneId, + '-t', + targetWindowOrPane, + ]) + + if (result.code !== 0) { + logForDebugging( + `[TmuxBackend] Failed to show pane ${paneId}: ${result.stderr}`, + ) + return false + } + + logForDebugging( + `[TmuxBackend] Showed pane ${paneId} in ${targetWindowOrPane}`, + ) + + // Reapply main-vertical layout with leader at 30% + await runTmux(['select-layout', '-t', targetWindowOrPane, 'main-vertical']) + + // Get the first pane (leader) and resize to 30% + const panesResult = await runTmux([ + 'list-panes', + '-t', + targetWindowOrPane, + '-F', + '#{pane_id}', + ]) + + const panes = panesResult.stdout.trim().split('\n').filter(Boolean) + if (panes[0]) { + await runTmux(['resize-pane', '-t', panes[0], '-x', '30%']) + } + + return true + } + + // Private helper methods + + /** + * Gets the leader's pane ID. + * Uses the TMUX_PANE env var captured at module load to ensure we always + * get the leader's original pane, even if the user has switched panes. + */ + private async getCurrentPaneId(): Promise { + // Use the pane ID captured at startup (from TMUX_PANE env var) + const leaderPane = getLeaderPaneId() + if (leaderPane) { + return leaderPane + } + + // Fallback to dynamic query (shouldn't happen if we're inside tmux) + const result = await execFileNoThrow(TMUX_COMMAND, [ + 'display-message', + '-p', + '#{pane_id}', + ]) + + if (result.code !== 0) { + logForDebugging( + `[TmuxBackend] Failed to get current pane ID (exit ${result.code}): ${result.stderr}`, + ) + return null + } + + return result.stdout.trim() + } + + /** + * Gets the leader's window target (session:window format). + * Uses the leader's pane ID to query for its window, ensuring we get the + * correct window even if the user has switched to a different window. + * Caches the result since the leader's window won't change. + */ + private async getCurrentWindowTarget(): Promise { + // Return cached value if available + if (cachedLeaderWindowTarget) { + return cachedLeaderWindowTarget + } + + // Build the command - use -t to target the leader's pane specifically + const leaderPane = getLeaderPaneId() + const args = ['display-message'] + if (leaderPane) { + args.push('-t', leaderPane) + } + args.push('-p', '#{session_name}:#{window_index}') + + const result = await execFileNoThrow(TMUX_COMMAND, args) + + if (result.code !== 0) { + logForDebugging( + `[TmuxBackend] Failed to get current window target (exit ${result.code}): ${result.stderr}`, + ) + return null + } + + cachedLeaderWindowTarget = result.stdout.trim() + return cachedLeaderWindowTarget + } + + /** + * Gets the number of panes in a window. + */ + private async getCurrentWindowPaneCount( + windowTarget?: string, + useSwarmSocket = false, + ): Promise { + const target = windowTarget || (await this.getCurrentWindowTarget()) + if (!target) { + return null + } + + const args = ['list-panes', '-t', target, '-F', '#{pane_id}'] + const result = useSwarmSocket + ? await runTmuxInSwarm(args) + : await runTmuxInUserSession(args) + + if (result.code !== 0) { + logError( + new Error( + `[TmuxBackend] Failed to get pane count for ${target} (exit ${result.code}): ${result.stderr}`, + ), + ) + return null + } + + return count(result.stdout.trim().split('\n'), Boolean) + } + + /** + * Checks if a tmux session exists in the swarm socket. + */ + private async hasSessionInSwarm(sessionName: string): Promise { + const result = await runTmuxInSwarm(['has-session', '-t', sessionName]) + return result.code === 0 + } + + /** + * Creates the swarm session with a single window for teammates when running outside tmux. + */ + private async createExternalSwarmSession(): Promise<{ + windowTarget: string + paneId: string + }> { + const sessionExists = await this.hasSessionInSwarm(SWARM_SESSION_NAME) + + if (!sessionExists) { + const result = await runTmuxInSwarm([ + 'new-session', + '-d', + '-s', + SWARM_SESSION_NAME, + '-n', + SWARM_VIEW_WINDOW_NAME, + '-P', + '-F', + '#{pane_id}', + ]) + + if (result.code !== 0) { + throw new Error( + `Failed to create swarm session: ${result.stderr || 'Unknown error'}`, + ) + } + + const paneId = result.stdout.trim() + const windowTarget = `${SWARM_SESSION_NAME}:${SWARM_VIEW_WINDOW_NAME}` + + logForDebugging( + `[TmuxBackend] Created external swarm session with window ${windowTarget}, pane ${paneId}`, + ) + + return { windowTarget, paneId } + } + + // Session exists, check if swarm-view window exists + const listResult = await runTmuxInSwarm([ + 'list-windows', + '-t', + SWARM_SESSION_NAME, + '-F', + '#{window_name}', + ]) + + const windows = listResult.stdout.trim().split('\n').filter(Boolean) + const windowTarget = `${SWARM_SESSION_NAME}:${SWARM_VIEW_WINDOW_NAME}` + + if (windows.includes(SWARM_VIEW_WINDOW_NAME)) { + const paneResult = await runTmuxInSwarm([ + 'list-panes', + '-t', + windowTarget, + '-F', + '#{pane_id}', + ]) + + const panes = paneResult.stdout.trim().split('\n').filter(Boolean) + return { windowTarget, paneId: panes[0] || '' } + } + + // Create the swarm-view window + const createResult = await runTmuxInSwarm([ + 'new-window', + '-t', + SWARM_SESSION_NAME, + '-n', + SWARM_VIEW_WINDOW_NAME, + '-P', + '-F', + '#{pane_id}', + ]) + + if (createResult.code !== 0) { + throw new Error( + `Failed to create swarm-view window: ${createResult.stderr || 'Unknown error'}`, + ) + } + + return { windowTarget, paneId: createResult.stdout.trim() } + } + + /** + * Creates a teammate pane when running inside tmux (with leader). + */ + private async createTeammatePaneWithLeader( + teammateName: string, + teammateColor: AgentColorName, + ): Promise { + const currentPaneId = await this.getCurrentPaneId() + const windowTarget = await this.getCurrentWindowTarget() + + if (!currentPaneId || !windowTarget) { + throw new Error('Could not determine current tmux pane/window') + } + + const paneCount = await this.getCurrentWindowPaneCount(windowTarget) + if (paneCount === null) { + throw new Error('Could not determine pane count for current window') + } + const isFirstTeammate = paneCount === 1 + + let splitResult + if (isFirstTeammate) { + // First teammate: split horizontally from the leader pane + splitResult = await execFileNoThrow(TMUX_COMMAND, [ + 'split-window', + '-t', + currentPaneId, + '-h', + '-l', + '70%', + '-P', + '-F', + '#{pane_id}', + ]) + } else { + // Additional teammates: split from an existing teammate pane + const listResult = await execFileNoThrow(TMUX_COMMAND, [ + 'list-panes', + '-t', + windowTarget, + '-F', + '#{pane_id}', + ]) + + const panes = listResult.stdout.trim().split('\n').filter(Boolean) + const teammatePanes = panes.slice(1) + const teammateCount = teammatePanes.length + + const splitVertically = teammateCount % 2 === 1 + const targetPaneIndex = Math.floor((teammateCount - 1) / 2) + const targetPane = + teammatePanes[targetPaneIndex] || + teammatePanes[teammatePanes.length - 1] + + splitResult = await execFileNoThrow(TMUX_COMMAND, [ + 'split-window', + '-t', + targetPane!, + splitVertically ? '-v' : '-h', + '-P', + '-F', + '#{pane_id}', + ]) + } + + if (splitResult.code !== 0) { + throw new Error(`Failed to create teammate pane: ${splitResult.stderr}`) + } + + const paneId = splitResult.stdout.trim() + logForDebugging( + `[TmuxBackend] Created teammate pane for ${teammateName}: ${paneId}`, + ) + + await this.setPaneBorderColor(paneId, teammateColor) + await this.setPaneTitle(paneId, teammateName, teammateColor) + await this.rebalancePanesWithLeader(windowTarget) + + // Wait for shell to initialize before returning, so commands can be sent immediately + await waitForPaneShellReady() + + return { paneId, isFirstTeammate } + } + + /** + * Creates a teammate pane when running outside tmux (no leader in tmux). + */ + private async createTeammatePaneExternal( + teammateName: string, + teammateColor: AgentColorName, + ): Promise { + const { windowTarget, paneId: firstPaneId } = + await this.createExternalSwarmSession() + + const paneCount = await this.getCurrentWindowPaneCount(windowTarget, true) + if (paneCount === null) { + throw new Error('Could not determine pane count for swarm window') + } + const isFirstTeammate = !firstPaneUsedForExternal && paneCount === 1 + + let paneId: string + + if (isFirstTeammate) { + paneId = firstPaneId + firstPaneUsedForExternal = true + logForDebugging( + `[TmuxBackend] Using initial pane for first teammate ${teammateName}: ${paneId}`, + ) + + await this.enablePaneBorderStatus(windowTarget, true) + } else { + const listResult = await runTmuxInSwarm([ + 'list-panes', + '-t', + windowTarget, + '-F', + '#{pane_id}', + ]) + + const panes = listResult.stdout.trim().split('\n').filter(Boolean) + const teammateCount = panes.length + + const splitVertically = teammateCount % 2 === 1 + const targetPaneIndex = Math.floor((teammateCount - 1) / 2) + const targetPane = panes[targetPaneIndex] || panes[panes.length - 1] + + const splitResult = await runTmuxInSwarm([ + 'split-window', + '-t', + targetPane!, + splitVertically ? '-v' : '-h', + '-P', + '-F', + '#{pane_id}', + ]) + + if (splitResult.code !== 0) { + throw new Error(`Failed to create teammate pane: ${splitResult.stderr}`) + } + + paneId = splitResult.stdout.trim() + logForDebugging( + `[TmuxBackend] Created teammate pane for ${teammateName}: ${paneId}`, + ) + } + + await this.setPaneBorderColor(paneId, teammateColor, true) + await this.setPaneTitle(paneId, teammateName, teammateColor, true) + await this.rebalancePanesTiled(windowTarget) + + // Wait for shell to initialize before returning, so commands can be sent immediately + await waitForPaneShellReady() + + return { paneId, isFirstTeammate } + } + + /** + * Rebalances panes in a window with a leader. + */ + private async rebalancePanesWithLeader(windowTarget: string): Promise { + const listResult = await runTmuxInUserSession([ + 'list-panes', + '-t', + windowTarget, + '-F', + '#{pane_id}', + ]) + + const panes = listResult.stdout.trim().split('\n').filter(Boolean) + if (panes.length <= 2) { + return + } + + await runTmuxInUserSession([ + 'select-layout', + '-t', + windowTarget, + 'main-vertical', + ]) + + const leaderPane = panes[0] + await runTmuxInUserSession(['resize-pane', '-t', leaderPane!, '-x', '30%']) + + logForDebugging( + `[TmuxBackend] Rebalanced ${panes.length - 1} teammate panes with leader`, + ) + } + + /** + * Rebalances panes in a window without a leader (tiled layout). + */ + private async rebalancePanesTiled(windowTarget: string): Promise { + const listResult = await runTmuxInSwarm([ + 'list-panes', + '-t', + windowTarget, + '-F', + '#{pane_id}', + ]) + + const panes = listResult.stdout.trim().split('\n').filter(Boolean) + if (panes.length <= 1) { + return + } + + await runTmuxInSwarm(['select-layout', '-t', windowTarget, 'tiled']) + + logForDebugging( + `[TmuxBackend] Rebalanced ${panes.length} teammate panes with tiled layout`, + ) + } +} + +// Register the backend with the registry when this module is imported. +// This side effect is intentional - the registry needs backends to self-register to avoid circular dependencies. +// eslint-disable-next-line custom-rules/no-top-level-side-effects +registerTmuxBackend(TmuxBackend) diff --git a/_all-in-one-upload/cc-haha/src/utils/swarm/permissionSync.ts b/_all-in-one-upload/cc-haha/src/utils/swarm/permissionSync.ts new file mode 100644 index 0000000..001e7a2 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/swarm/permissionSync.ts @@ -0,0 +1,928 @@ +/** + * Synchronized Permission Prompts for Agent Swarms + * + * This module provides infrastructure for coordinating permission prompts across + * multiple agents in a swarm. When a worker agent needs permission for a tool use, + * it can forward the request to the team leader, who can then approve or deny it. + * + * The system uses the teammate mailbox for message passing: + * - Workers send permission requests to the leader's mailbox + * - Leaders send permission responses to the worker's mailbox + * + * Flow: + * 1. Worker agent encounters a permission prompt + * 2. Worker sends a permission_request message to the leader's mailbox + * 3. Leader polls for mailbox messages and detects permission requests + * 4. User approves/denies via the leader's UI + * 5. Leader sends a permission_response message to the worker's mailbox + * 6. Worker polls mailbox for responses and continues execution + */ + +import { mkdir, readdir, readFile, unlink, writeFile } from 'fs/promises' +import { join } from 'path' +import { z } from 'zod/v4' +import { logForDebugging } from '../debug.js' +import { getErrnoCode } from '../errors.js' +import { lazySchema } from '../lazySchema.js' +import * as lockfile from '../lockfile.js' +import { logError } from '../log.js' +import type { PermissionUpdate } from '../permissions/PermissionUpdateSchema.js' +import { jsonParse, jsonStringify } from '../slowOperations.js' +import { + getAgentId, + getAgentName, + getTeammateColor, + getTeamName, +} from '../teammate.js' +import { + createPermissionRequestMessage, + createPermissionResponseMessage, + createSandboxPermissionRequestMessage, + createSandboxPermissionResponseMessage, + writeToMailbox, +} from '../teammateMailbox.js' +import { getTeamDir, readTeamFileAsync } from './teamHelpers.js' + +/** + * Full request schema for a permission request from a worker to the leader + */ +export const SwarmPermissionRequestSchema = lazySchema(() => + z.object({ + /** Unique identifier for this request */ + id: z.string(), + /** Worker's CLAUDE_CODE_AGENT_ID */ + workerId: z.string(), + /** Worker's CLAUDE_CODE_AGENT_NAME */ + workerName: z.string(), + /** Worker's CLAUDE_CODE_AGENT_COLOR */ + workerColor: z.string().optional(), + /** Team name for routing */ + teamName: z.string(), + /** Tool name requiring permission (e.g., "Bash", "Edit") */ + toolName: z.string(), + /** Original toolUseID from worker's context */ + toolUseId: z.string(), + /** Human-readable description of the tool use */ + description: z.string(), + /** Serialized tool input */ + input: z.record(z.string(), z.unknown()), + /** Suggested permission rules from the permission result */ + permissionSuggestions: z.array(z.unknown()), + /** Status of the request */ + status: z.enum(['pending', 'approved', 'rejected']), + /** Who resolved the request */ + resolvedBy: z.enum(['worker', 'leader']).optional(), + /** Timestamp when resolved */ + resolvedAt: z.number().optional(), + /** Rejection feedback message */ + feedback: z.string().optional(), + /** Modified input if changed by resolver */ + updatedInput: z.record(z.string(), z.unknown()).optional(), + /** "Always allow" rules applied during resolution */ + permissionUpdates: z.array(z.unknown()).optional(), + /** Timestamp when request was created */ + createdAt: z.number(), + }), +) + +export type SwarmPermissionRequest = z.infer< + ReturnType +> + +/** + * Resolution data returned when leader/worker resolves a request + */ +export type PermissionResolution = { + /** Decision: approved or rejected */ + decision: 'approved' | 'rejected' + /** Who resolved it */ + resolvedBy: 'worker' | 'leader' + /** Optional feedback message if rejected */ + feedback?: string + /** Optional updated input if the resolver modified it */ + updatedInput?: Record + /** Permission updates to apply (e.g., "always allow" rules) */ + permissionUpdates?: PermissionUpdate[] +} + +/** + * Get the base directory for a team's permission requests + * Path: ~/.claude/teams/{teamName}/permissions/ + */ +export function getPermissionDir(teamName: string): string { + return join(getTeamDir(teamName), 'permissions') +} + +/** + * Get the pending directory for a team + */ +function getPendingDir(teamName: string): string { + return join(getPermissionDir(teamName), 'pending') +} + +/** + * Get the resolved directory for a team + */ +function getResolvedDir(teamName: string): string { + return join(getPermissionDir(teamName), 'resolved') +} + +/** + * Ensure the permissions directory structure exists (async) + */ +async function ensurePermissionDirsAsync(teamName: string): Promise { + const permDir = getPermissionDir(teamName) + const pendingDir = getPendingDir(teamName) + const resolvedDir = getResolvedDir(teamName) + + for (const dir of [permDir, pendingDir, resolvedDir]) { + await mkdir(dir, { recursive: true }) + } +} + +/** + * Get the path to a pending request file + */ +function getPendingRequestPath(teamName: string, requestId: string): string { + return join(getPendingDir(teamName), `${requestId}.json`) +} + +/** + * Get the path to a resolved request file + */ +function getResolvedRequestPath(teamName: string, requestId: string): string { + return join(getResolvedDir(teamName), `${requestId}.json`) +} + +/** + * Generate a unique request ID + */ +export function generateRequestId(): string { + return `perm-${Date.now()}-${Math.random().toString(36).substring(2, 9)}` +} + +/** + * Create a new SwarmPermissionRequest object + */ +export function createPermissionRequest(params: { + toolName: string + toolUseId: string + input: Record + description: string + permissionSuggestions?: unknown[] + teamName?: string + workerId?: string + workerName?: string + workerColor?: string +}): SwarmPermissionRequest { + const teamName = params.teamName || getTeamName() + const workerId = params.workerId || getAgentId() + const workerName = params.workerName || getAgentName() + const workerColor = params.workerColor || getTeammateColor() + + if (!teamName) { + throw new Error('Team name is required for permission requests') + } + if (!workerId) { + throw new Error('Worker ID is required for permission requests') + } + if (!workerName) { + throw new Error('Worker name is required for permission requests') + } + + return { + id: generateRequestId(), + workerId, + workerName, + workerColor, + teamName, + toolName: params.toolName, + toolUseId: params.toolUseId, + description: params.description, + input: params.input, + permissionSuggestions: params.permissionSuggestions || [], + status: 'pending', + createdAt: Date.now(), + } +} + +/** + * Write a permission request to the pending directory with file locking + * Called by worker agents when they need permission approval from the leader + * + * @returns The written request + */ +export async function writePermissionRequest( + request: SwarmPermissionRequest, +): Promise { + await ensurePermissionDirsAsync(request.teamName) + + const pendingPath = getPendingRequestPath(request.teamName, request.id) + const lockDir = getPendingDir(request.teamName) + + // Create a directory-level lock file for atomic writes + const lockFilePath = join(lockDir, '.lock') + await writeFile(lockFilePath, '', 'utf-8') + + let release: (() => Promise) | undefined + try { + release = await lockfile.lock(lockFilePath) + + // Write the request file + await writeFile(pendingPath, jsonStringify(request, null, 2), 'utf-8') + + logForDebugging( + `[PermissionSync] Wrote pending request ${request.id} from ${request.workerName} for ${request.toolName}`, + ) + + return request + } catch (error) { + logForDebugging( + `[PermissionSync] Failed to write permission request: ${error}`, + ) + logError(error) + throw error + } finally { + if (release) { + await release() + } + } +} + +/** + * Read all pending permission requests for a team + * Called by the team leader to see what requests need attention + */ +export async function readPendingPermissions( + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + logForDebugging('[PermissionSync] No team name available') + return [] + } + + const pendingDir = getPendingDir(team) + + let files: string[] + try { + files = await readdir(pendingDir) + } catch (e: unknown) { + const code = getErrnoCode(e) + if (code === 'ENOENT') { + return [] + } + logForDebugging(`[PermissionSync] Failed to read pending requests: ${e}`) + logError(e) + return [] + } + + const jsonFiles = files.filter(f => f.endsWith('.json') && f !== '.lock') + + const results = await Promise.all( + jsonFiles.map(async file => { + const filePath = join(pendingDir, file) + try { + const content = await readFile(filePath, 'utf-8') + const parsed = SwarmPermissionRequestSchema().safeParse( + jsonParse(content), + ) + if (parsed.success) { + return parsed.data + } + logForDebugging( + `[PermissionSync] Invalid request file ${file}: ${parsed.error.message}`, + ) + return null + } catch (err) { + logForDebugging( + `[PermissionSync] Failed to read request file ${file}: ${err}`, + ) + return null + } + }), + ) + + const requests = results.filter(r => r !== null) + + // Sort by creation time (oldest first) + requests.sort((a, b) => a.createdAt - b.createdAt) + + return requests +} + +/** + * Read a resolved permission request by ID + * Called by workers to check if their request has been resolved + * + * @returns The resolved request, or null if not yet resolved + */ +export async function readResolvedPermission( + requestId: string, + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + return null + } + + const resolvedPath = getResolvedRequestPath(team, requestId) + + try { + const content = await readFile(resolvedPath, 'utf-8') + const parsed = SwarmPermissionRequestSchema().safeParse(jsonParse(content)) + if (parsed.success) { + return parsed.data + } + logForDebugging( + `[PermissionSync] Invalid resolved request ${requestId}: ${parsed.error.message}`, + ) + return null + } catch (e: unknown) { + const code = getErrnoCode(e) + if (code === 'ENOENT') { + return null + } + logForDebugging( + `[PermissionSync] Failed to read resolved request ${requestId}: ${e}`, + ) + logError(e) + return null + } +} + +/** + * Resolve a permission request + * Called by the team leader (or worker in self-resolution cases) + * + * Writes the resolution to resolved/, removes from pending/ + */ +export async function resolvePermission( + requestId: string, + resolution: PermissionResolution, + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + logForDebugging('[PermissionSync] No team name available') + return false + } + + await ensurePermissionDirsAsync(team) + + const pendingPath = getPendingRequestPath(team, requestId) + const resolvedPath = getResolvedRequestPath(team, requestId) + const lockFilePath = join(getPendingDir(team), '.lock') + + await writeFile(lockFilePath, '', 'utf-8') + + let release: (() => Promise) | undefined + try { + release = await lockfile.lock(lockFilePath) + + // Read the pending request + let content: string + try { + content = await readFile(pendingPath, 'utf-8') + } catch (e: unknown) { + const code = getErrnoCode(e) + if (code === 'ENOENT') { + logForDebugging( + `[PermissionSync] Pending request not found: ${requestId}`, + ) + return false + } + throw e + } + + const parsed = SwarmPermissionRequestSchema().safeParse(jsonParse(content)) + if (!parsed.success) { + logForDebugging( + `[PermissionSync] Invalid pending request ${requestId}: ${parsed.error.message}`, + ) + return false + } + + const request = parsed.data + + // Update the request with resolution data + const resolvedRequest: SwarmPermissionRequest = { + ...request, + status: resolution.decision === 'approved' ? 'approved' : 'rejected', + resolvedBy: resolution.resolvedBy, + resolvedAt: Date.now(), + feedback: resolution.feedback, + updatedInput: resolution.updatedInput, + permissionUpdates: resolution.permissionUpdates, + } + + // Write to resolved directory + await writeFile( + resolvedPath, + jsonStringify(resolvedRequest, null, 2), + 'utf-8', + ) + + // Remove from pending directory + await unlink(pendingPath) + + logForDebugging( + `[PermissionSync] Resolved request ${requestId} with ${resolution.decision}`, + ) + + return true + } catch (error) { + logForDebugging(`[PermissionSync] Failed to resolve request: ${error}`) + logError(error) + return false + } finally { + if (release) { + await release() + } + } +} + +/** + * Clean up old resolved permission files + * Called periodically to prevent file accumulation + * + * @param teamName - Team name + * @param maxAgeMs - Maximum age in milliseconds (default: 1 hour) + */ +export async function cleanupOldResolutions( + teamName?: string, + maxAgeMs = 3600000, +): Promise { + const team = teamName || getTeamName() + if (!team) { + return 0 + } + + const resolvedDir = getResolvedDir(team) + + let files: string[] + try { + files = await readdir(resolvedDir) + } catch (e: unknown) { + const code = getErrnoCode(e) + if (code === 'ENOENT') { + return 0 + } + logForDebugging(`[PermissionSync] Failed to cleanup resolutions: ${e}`) + logError(e) + return 0 + } + + const now = Date.now() + const jsonFiles = files.filter(f => f.endsWith('.json')) + + const cleanupResults = await Promise.all( + jsonFiles.map(async file => { + const filePath = join(resolvedDir, file) + try { + const content = await readFile(filePath, 'utf-8') + const request = jsonParse(content) as SwarmPermissionRequest + + // Check if the resolution is old enough to clean up + // Use >= to handle edge case where maxAgeMs is 0 (clean up everything) + const resolvedAt = request.resolvedAt || request.createdAt + if (now - resolvedAt >= maxAgeMs) { + await unlink(filePath) + logForDebugging(`[PermissionSync] Cleaned up old resolution: ${file}`) + return 1 + } + return 0 + } catch { + // If we can't parse it, clean it up anyway + try { + await unlink(filePath) + return 1 + } catch { + // Ignore deletion errors + return 0 + } + } + }), + ) + + const cleanedCount = cleanupResults.reduce((sum, n) => sum + n, 0) + + if (cleanedCount > 0) { + logForDebugging( + `[PermissionSync] Cleaned up ${cleanedCount} old resolutions`, + ) + } + + return cleanedCount +} + +/** + * Legacy response type for worker polling + * Used for backward compatibility with worker integration code + */ +export type PermissionResponse = { + /** ID of the request this responds to */ + requestId: string + /** Decision: approved or denied */ + decision: 'approved' | 'denied' + /** Timestamp when response was created */ + timestamp: string + /** Optional feedback message if denied */ + feedback?: string + /** Optional updated input if the resolver modified it */ + updatedInput?: Record + /** Permission updates to apply (e.g., "always allow" rules) */ + permissionUpdates?: unknown[] +} + +/** + * Poll for a permission response (worker-side convenience function) + * Converts the resolved request into a simpler response format + * + * @returns The permission response, or null if not yet resolved + */ +export async function pollForResponse( + requestId: string, + _agentName?: string, + teamName?: string, +): Promise { + const resolved = await readResolvedPermission(requestId, teamName) + if (!resolved) { + return null + } + + return { + requestId: resolved.id, + decision: resolved.status === 'approved' ? 'approved' : 'denied', + timestamp: resolved.resolvedAt + ? new Date(resolved.resolvedAt).toISOString() + : new Date(resolved.createdAt).toISOString(), + feedback: resolved.feedback, + updatedInput: resolved.updatedInput, + permissionUpdates: resolved.permissionUpdates, + } +} + +/** + * Remove a worker's response after processing + * This is an alias for deleteResolvedPermission for backward compatibility + */ +export async function removeWorkerResponse( + requestId: string, + _agentName?: string, + teamName?: string, +): Promise { + await deleteResolvedPermission(requestId, teamName) +} + +/** + * Check if the current agent is a team leader + */ +export function isTeamLeader(teamName?: string): boolean { + const team = teamName || getTeamName() + if (!team) { + return false + } + + // Team leaders don't have an agent ID set, or their ID is 'team-lead' + const agentId = getAgentId() + + return !agentId || agentId === 'team-lead' +} + +/** + * Check if the current agent is a worker in a swarm + */ +export function isSwarmWorker(): boolean { + const teamName = getTeamName() + const agentId = getAgentId() + + return !!teamName && !!agentId && !isTeamLeader() +} + +/** + * Delete a resolved permission file + * Called after a worker has processed the resolution + */ +export async function deleteResolvedPermission( + requestId: string, + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + return false + } + + const resolvedPath = getResolvedRequestPath(team, requestId) + + try { + await unlink(resolvedPath) + logForDebugging( + `[PermissionSync] Deleted resolved permission: ${requestId}`, + ) + return true + } catch (e: unknown) { + const code = getErrnoCode(e) + if (code === 'ENOENT') { + return false + } + logForDebugging( + `[PermissionSync] Failed to delete resolved permission: ${e}`, + ) + logError(e) + return false + } +} + +/** + * Submit a permission request (alias for writePermissionRequest) + * Provided for backward compatibility with worker integration code + */ +export const submitPermissionRequest = writePermissionRequest + +// ============================================================================ +// Mailbox-Based Permission System +// ============================================================================ + +/** + * Get the leader's name from the team file + * This is needed to send permission requests to the leader's mailbox + */ +export async function getLeaderName(teamName?: string): Promise { + const team = teamName || getTeamName() + if (!team) { + return null + } + + const teamFile = await readTeamFileAsync(team) + if (!teamFile) { + logForDebugging(`[PermissionSync] Team file not found for team: ${team}`) + return null + } + + const leadMember = teamFile.members.find( + m => m.agentId === teamFile.leadAgentId, + ) + return leadMember?.name || 'team-lead' +} + +/** + * Send a permission request to the leader via mailbox. + * This is the new mailbox-based approach that replaces the file-based pending directory. + * + * @param request - The permission request to send + * @returns true if the message was sent successfully + */ +export async function sendPermissionRequestViaMailbox( + request: SwarmPermissionRequest, +): Promise { + const leaderName = await getLeaderName(request.teamName) + if (!leaderName) { + logForDebugging( + `[PermissionSync] Cannot send permission request: leader name not found`, + ) + return false + } + + try { + // Create the permission request message + const message = createPermissionRequestMessage({ + request_id: request.id, + agent_id: request.workerName, + tool_name: request.toolName, + tool_use_id: request.toolUseId, + description: request.description, + input: request.input, + permission_suggestions: request.permissionSuggestions, + }) + + // Send to leader's mailbox (routes to in-process or file-based based on recipient) + await writeToMailbox( + leaderName, + { + from: request.workerName, + text: jsonStringify(message), + timestamp: new Date().toISOString(), + color: request.workerColor, + }, + request.teamName, + ) + + logForDebugging( + `[PermissionSync] Sent permission request ${request.id} to leader ${leaderName} via mailbox`, + ) + return true + } catch (error) { + logForDebugging( + `[PermissionSync] Failed to send permission request via mailbox: ${error}`, + ) + logError(error) + return false + } +} + +/** + * Send a permission response to a worker via mailbox. + * This is the new mailbox-based approach that replaces the file-based resolved directory. + * + * @param workerName - The worker's name to send the response to + * @param resolution - The permission resolution + * @param requestId - The original request ID + * @param teamName - The team name + * @returns true if the message was sent successfully + */ +export async function sendPermissionResponseViaMailbox( + workerName: string, + resolution: PermissionResolution, + requestId: string, + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + logForDebugging( + `[PermissionSync] Cannot send permission response: team name not found`, + ) + return false + } + + try { + // Create the permission response message + const message = createPermissionResponseMessage({ + request_id: requestId, + subtype: resolution.decision === 'approved' ? 'success' : 'error', + error: resolution.feedback, + updated_input: resolution.updatedInput, + permission_updates: resolution.permissionUpdates, + }) + + // Get the sender name (leader's name) + const senderName = getAgentName() || 'team-lead' + + // Send to worker's mailbox (routes to in-process or file-based based on recipient) + await writeToMailbox( + workerName, + { + from: senderName, + text: jsonStringify(message), + timestamp: new Date().toISOString(), + }, + team, + ) + + logForDebugging( + `[PermissionSync] Sent permission response for ${requestId} to worker ${workerName} via mailbox`, + ) + return true + } catch (error) { + logForDebugging( + `[PermissionSync] Failed to send permission response via mailbox: ${error}`, + ) + logError(error) + return false + } +} + +// ============================================================================ +// Sandbox Permission Mailbox System +// ============================================================================ + +/** + * Generate a unique sandbox permission request ID + */ +export function generateSandboxRequestId(): string { + return `sandbox-${Date.now()}-${Math.random().toString(36).substring(2, 9)}` +} + +/** + * Send a sandbox permission request to the leader via mailbox. + * Called by workers when sandbox runtime needs network access approval. + * + * @param host - The host requesting network access + * @param requestId - Unique ID for this request + * @param teamName - Optional team name + * @returns true if the message was sent successfully + */ +export async function sendSandboxPermissionRequestViaMailbox( + host: string, + requestId: string, + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + logForDebugging( + `[PermissionSync] Cannot send sandbox permission request: team name not found`, + ) + return false + } + + const leaderName = await getLeaderName(team) + if (!leaderName) { + logForDebugging( + `[PermissionSync] Cannot send sandbox permission request: leader name not found`, + ) + return false + } + + const workerId = getAgentId() + const workerName = getAgentName() + const workerColor = getTeammateColor() + + if (!workerId || !workerName) { + logForDebugging( + `[PermissionSync] Cannot send sandbox permission request: worker ID or name not found`, + ) + return false + } + + try { + const message = createSandboxPermissionRequestMessage({ + requestId, + workerId, + workerName, + workerColor, + host, + }) + + // Send to leader's mailbox (routes to in-process or file-based based on recipient) + await writeToMailbox( + leaderName, + { + from: workerName, + text: jsonStringify(message), + timestamp: new Date().toISOString(), + color: workerColor, + }, + team, + ) + + logForDebugging( + `[PermissionSync] Sent sandbox permission request ${requestId} for host ${host} to leader ${leaderName} via mailbox`, + ) + return true + } catch (error) { + logForDebugging( + `[PermissionSync] Failed to send sandbox permission request via mailbox: ${error}`, + ) + logError(error) + return false + } +} + +/** + * Send a sandbox permission response to a worker via mailbox. + * Called by the leader when approving/denying a sandbox network access request. + * + * @param workerName - The worker's name to send the response to + * @param requestId - The original request ID + * @param host - The host that was approved/denied + * @param allow - Whether the connection is allowed + * @param teamName - Optional team name + * @returns true if the message was sent successfully + */ +export async function sendSandboxPermissionResponseViaMailbox( + workerName: string, + requestId: string, + host: string, + allow: boolean, + teamName?: string, +): Promise { + const team = teamName || getTeamName() + if (!team) { + logForDebugging( + `[PermissionSync] Cannot send sandbox permission response: team name not found`, + ) + return false + } + + try { + const message = createSandboxPermissionResponseMessage({ + requestId, + host, + allow, + }) + + const senderName = getAgentName() || 'team-lead' + + // Send to worker's mailbox (routes to in-process or file-based based on recipient) + await writeToMailbox( + workerName, + { + from: senderName, + text: jsonStringify(message), + timestamp: new Date().toISOString(), + }, + team, + ) + + logForDebugging( + `[PermissionSync] Sent sandbox permission response for ${requestId} (host: ${host}, allow: ${allow}) to worker ${workerName} via mailbox`, + ) + return true + } catch (error) { + logForDebugging( + `[PermissionSync] Failed to send sandbox permission response via mailbox: ${error}`, + ) + logError(error) + return false + } +} diff --git a/_all-in-one-upload/cc-haha/src/utils/windowsPaths.ts b/_all-in-one-upload/cc-haha/src/utils/windowsPaths.ts new file mode 100644 index 0000000..776c2e3 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/utils/windowsPaths.ts @@ -0,0 +1,199 @@ +import memoize from 'lodash-es/memoize.js' +import * as path from 'path' +import * as pathWin32 from 'path/win32' +import { getCwd } from './cwd.js' +import { logForDebugging } from './debug.js' +import { execSync_DEPRECATED } from './execSyncWrapper.js' +import { memoizeWithLRU } from './memoize.js' +import { getPlatform } from './platform.js' + +/** + * Check if a file or directory exists on Windows using the dir command + * @param path - The path to check + * @returns true if the path exists, false otherwise + */ +function checkPathExists(path: string): boolean { + try { + execSync_DEPRECATED(`dir "${path}"`, { stdio: 'pipe' }) + return true + } catch { + return false + } +} + +/** + * Find an executable using where.exe on Windows + * @param executable - The name of the executable to find + * @returns The path to the executable or null if not found + */ +function findExecutable(executable: string): string | null { + // For git, check common installation locations first + if (executable === 'git') { + const defaultLocations = [ + // check 64 bit before 32 bit + 'C:\\Program Files\\Git\\cmd\\git.exe', + 'C:\\Program Files (x86)\\Git\\cmd\\git.exe', + // intentionally don't look for C:\Program Files\Git\mingw64\bin\git.exe + // because that directory is the "raw" tools with no environment setup + ] + + for (const location of defaultLocations) { + if (checkPathExists(location)) { + return location + } + } + } + + // Fall back to where.exe + try { + const result = execSync_DEPRECATED(`where.exe ${executable}`, { + stdio: 'pipe', + encoding: 'utf8', + }).trim() + + // SECURITY: Filter out any results from the current directory + // to prevent executing malicious git.bat/cmd/exe files + const paths = result.split('\r\n').filter(Boolean) + const cwd = getCwd().toLowerCase() + + for (const candidatePath of paths) { + // Normalize and compare paths to ensure we're not in current directory + const normalizedPath = path.resolve(candidatePath).toLowerCase() + const pathDir = path.dirname(normalizedPath).toLowerCase() + + // Skip if the executable is in the current working directory + if (pathDir === cwd || normalizedPath.startsWith(cwd + path.sep)) { + logForDebugging( + `Skipping potentially malicious executable in current directory: ${candidatePath}`, + ) + continue + } + + // Return the first valid path that's not in the current directory + return candidatePath + } + + return null + } catch { + return null + } +} + +/** + * If Windows, set the SHELL environment variable to git-bash path. + * This is used by BashTool and Shell.ts for user shell commands. + * COMSPEC is left unchanged for system process execution. + */ +export function setShellIfWindows(): void { + if (getPlatform() === 'windows') { + const gitBashPath = tryFindGitBashPath() + if (!gitBashPath) { + logForDebugging( + 'Git Bash not found on Windows; leaving SHELL unset for lazy fallback handling', + { level: 'warn' }, + ) + return + } + process.env.SHELL = gitBashPath + logForDebugging(`Using bash path: "${gitBashPath}"`) + } +} + +/** + * Best-effort Git Bash resolution on Windows. + * + * Returns null when Git Bash is unavailable so callers can decide whether to + * fall back (for example, to PowerShell) instead of hard-exiting the process. + */ +export const tryFindGitBashPath = memoize((): string | null => { + if (process.env.CLAUDE_CODE_GIT_BASH_PATH) { + if (checkPathExists(process.env.CLAUDE_CODE_GIT_BASH_PATH)) { + return process.env.CLAUDE_CODE_GIT_BASH_PATH + } + return null + } + + const gitPath = findExecutable('git') + if (gitPath) { + const bashPath = pathWin32.join(gitPath, '..', '..', 'bin', 'bash.exe') + if (checkPathExists(bashPath)) { + return bashPath + } + } + + return null +}) + +/** + * Find the path where `bash.exe` included with git-bash exists, exiting the process if not found. + */ +export const findGitBashPath = memoize((): string => { + const gitBashPath = tryFindGitBashPath() + if (gitBashPath) { + return gitBashPath + } + + if (process.env.CLAUDE_CODE_GIT_BASH_PATH) { + // biome-ignore lint/suspicious/noConsole:: intentional console output + console.error( + `Claude Code was unable to find CLAUDE_CODE_GIT_BASH_PATH path "${process.env.CLAUDE_CODE_GIT_BASH_PATH}"`, + ) + // eslint-disable-next-line custom-rules/no-process-exit + process.exit(1) + } + + // biome-ignore lint/suspicious/noConsole:: intentional console output + console.error( + 'Claude Code on Windows requires git-bash (https://git-scm.com/downloads/win). If installed but not in PATH, set environment variable pointing to your bash.exe, similar to: CLAUDE_CODE_GIT_BASH_PATH=C:\\Program Files\\Git\\bin\\bash.exe', + ) + // eslint-disable-next-line custom-rules/no-process-exit + process.exit(1) +}) + +/** Convert a Windows path to a POSIX path using pure JS. */ +export const windowsPathToPosixPath = memoizeWithLRU( + (windowsPath: string): string => { + // Handle UNC paths: \\server\share -> //server/share + if (windowsPath.startsWith('\\\\')) { + return windowsPath.replace(/\\/g, '/') + } + // Handle drive letter paths: C:\Users\foo -> /c/Users/foo + const match = windowsPath.match(/^([A-Za-z]):[/\\]/) + if (match) { + const driveLetter = match[1]!.toLowerCase() + return '/' + driveLetter + windowsPath.slice(2).replace(/\\/g, '/') + } + // Already POSIX or relative — just flip slashes + return windowsPath.replace(/\\/g, '/') + }, + (p: string) => p, + 500, +) + +/** Convert a POSIX path to a Windows path using pure JS. */ +export const posixPathToWindowsPath = memoizeWithLRU( + (posixPath: string): string => { + // Handle UNC paths: //server/share -> \\server\share + if (posixPath.startsWith('//')) { + return posixPath.replace(/\//g, '\\') + } + // Handle /cygdrive/c/... format + const cygdriveMatch = posixPath.match(/^\/cygdrive\/([A-Za-z])(\/|$)/) + if (cygdriveMatch) { + const driveLetter = cygdriveMatch[1]!.toUpperCase() + const rest = posixPath.slice(('/cygdrive/' + cygdriveMatch[1]).length) + return driveLetter + ':' + (rest || '\\').replace(/\//g, '\\') + } + // Handle /c/... format (MSYS2/Git Bash) + const driveMatch = posixPath.match(/^\/([A-Za-z])(\/|$)/) + if (driveMatch) { + const driveLetter = driveMatch[1]!.toUpperCase() + const rest = posixPath.slice(2) + return driveLetter + ':' + (rest || '\\').replace(/\//g, '\\') + } + // Already Windows or relative — just flip slashes + return posixPath.replace(/\//g, '\\') + }, + (p: string) => p, + 500, +) diff --git a/_all-in-one-upload/cc-haha/src/vendor/computer-use-mcp/tools.ts b/_all-in-one-upload/cc-haha/src/vendor/computer-use-mcp/tools.ts new file mode 100644 index 0000000..c744a23 --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/vendor/computer-use-mcp/tools.ts @@ -0,0 +1,706 @@ +/** + * MCP tool schemas for the computer-use server. Mirrors + * claude-for-chrome-mcp/src/browserTools.ts in shape (plain `Tool`-shaped + * object literals, no zod). + * + * Coordinate descriptions are baked in at tool-list build time from the + * `chicago_coordinate_mode` gate. The model sees exactly ONE coordinate + * convention in the param descriptions and never learns the other exists. + * The host (`serverDef.ts`) reads the same frozen gate value for + * `scaleCoord` — both must agree or clicks land in the wrong space. + */ + +import type { Tool } from "@modelcontextprotocol/sdk/types.js"; + +import type { CoordinateMode } from "./types.js"; + +// See packages/desktop/computer-use-mcp/COORDINATES.md before touching any +// model-facing coordinate text. Chrome's browserTools.ts:143 is the reference +// phrasing — "pixels from the left edge", no geometry, no number to do math with. +const COORD_DESC: Record = { + pixels: { + x: "Horizontal pixel position read directly from the most recent screenshot image, measured from the left edge. The server handles all scaling.", + y: "Vertical pixel position read directly from the most recent screenshot image, measured from the top edge. The server handles all scaling.", + }, + normalized_0_100: { + x: "Horizontal position as a percentage of screen width, 0.0–100.0 (0 = left edge, 100 = right edge).", + y: "Vertical position as a percentage of screen height, 0.0–100.0 (0 = top edge, 100 = bottom edge).", + }, +}; + +const FRONTMOST_GATE_DESC = + "The frontmost application must be in the session allowlist at the time of this call, or this tool returns an error and does nothing."; + +/** + * Item schema for the `actions` array in `computer_batch`, `teach_step`, and + * `teach_batch`. All three dispatch through the same `dispatchAction` path + * with the same validation — keep this enum in sync with `BATCHABLE_ACTIONS` + * in toolCalls.ts. + */ +const BATCH_ACTION_ITEM_SCHEMA = { + type: "object", + properties: { + action: { + type: "string", + enum: [ + "key", + "type", + "mouse_move", + "left_click", + "left_click_drag", + "right_click", + "middle_click", + "double_click", + "triple_click", + "scroll", + "hold_key", + "screenshot", + "cursor_position", + "left_mouse_down", + "left_mouse_up", + "wait", + ], + description: "The action to perform.", + }, + coordinate: { + type: "array", + items: { type: "number" }, + minItems: 2, + maxItems: 2, + description: + "(x, y) for click/mouse_move/scroll/left_click_drag end point.", + }, + start_coordinate: { + type: "array", + items: { type: "number" }, + minItems: 2, + maxItems: 2, + description: + "(x, y) drag start — left_click_drag only. Omit to drag from current cursor.", + }, + text: { + type: "string", + description: + "For type: the text. For key/hold_key: the chord string. For click/scroll: modifier keys to hold.", + }, + scroll_direction: { + type: "string", + enum: ["up", "down", "left", "right"], + }, + scroll_amount: { type: "integer", minimum: 0, maximum: 100 }, + duration: { + type: "number", + description: "Seconds (0–100). For hold_key/wait.", + }, + repeat: { + type: "integer", + minimum: 1, + maximum: 100, + description: "For key: repeat count.", + }, + }, + required: ["action"], +}; + +/** + * Build the tool list. Parameterized by capabilities and coordinate mode so + * descriptions are honest and unambiguous (plan §1 — "Unfiltered + honest"). + * + * `coordinateMode` MUST match what the host passes to `scaleCoord` at tool- + * -call time. Both should read the same frozen-at-load gate constant. + * + * `installedAppNames` — optional pre-sanitized list of app display names to + * enumerate in the `request_access` description. The caller is responsible + * for sanitization (length cap, character allowlist, sort, count cap) — + * this function just splices the list into the description verbatim. Omit + * to fall back to the generic "display names or bundle IDs" wording. + */ +export function buildComputerUseTools( + caps: { + screenshotFiltering: "native" | "none"; + platform: "darwin" | "win32"; + /** Include request_teach_access + teach_step. Read once at server construction. */ + teachMode?: boolean; + }, + coordinateMode: CoordinateMode, + installedAppNames?: string[], +): Tool[] { + const coord = COORD_DESC[coordinateMode]; + + // Shared hint suffix for BOTH request_access and request_teach_access — + // they use the same resolveRequestedApps path, so the model should get + // the same enumeration for both. + const installedAppsHint = + installedAppNames && installedAppNames.length > 0 + ? ` Available applications on this machine: ${installedAppNames.join(", ")}.` + : ""; + + // [x, y]` tuple — param shape for all + // click/move/scroll tools. + const coordinateTuple = { + type: "array", + items: { type: "number" }, + minItems: 2, + maxItems: 2, + description: `(x, y): ${coord.x}`, + }; + // Modifier hold during click. Shared across all 5 click variants. + const clickModifierText = { + type: "string", + description: + 'Modifier keys to hold during the click (e.g. "shift", "ctrl+shift"). Supports the same syntax as the key tool.', + }; + + const screenshotDesc = + caps.screenshotFiltering === "native" + ? "Take a screenshot of the primary display. Applications not in the session allowlist are excluded at the compositor level — only granted apps and the desktop are visible." + : "Take a screenshot of the primary display. On this platform, screenshots are NOT filtered — all open windows are visible. Input actions targeting apps not in the session allowlist are rejected."; + + return [ + { + name: "request_access", + description: + "Request user permission to control a set of applications for this session. Must be called before any other tool in this server. " + + "The user sees a single dialog listing all requested apps and either allows the whole set or denies it. " + + "Call this again mid-session to add more apps; previously granted apps remain granted. " + + "Returns the granted apps, denied apps, and screenshot filtering capability.", + inputSchema: { + type: "object" as const, + properties: { + apps: { + type: "array", + items: { type: "string" }, + description: + "Application display names (e.g. \"Slack\", \"Calendar\") or bundle identifiers (e.g. \"com.tinyspeck.slackmacgap\"). Display names are resolved case-insensitively against installed apps." + + installedAppsHint, + }, + reason: { + type: "string", + description: + "One-sentence explanation shown to the user in the approval dialog. Explain the task, not the mechanism.", + }, + clipboardRead: { + type: "boolean", + description: + "Also request permission to read the user's clipboard (separate checkbox in the dialog).", + }, + clipboardWrite: { + type: "boolean", + description: + "Also request permission to write the user's clipboard. When granted, multi-line `type` calls use the clipboard fast path.", + }, + systemKeyCombos: { + type: "boolean", + description: + "Also request permission to send system-level key combos (quit app, switch app, lock screen). Without this, those specific combos are blocked.", + }, + }, + required: ["apps", "reason"], + }, + }, + + { + name: "screenshot", + description: + screenshotDesc + + " Returns an error if the allowlist is empty. The returned image is what subsequent click coordinates are relative to.", + inputSchema: { + type: "object" as const, + properties: { + save_to_disk: { + type: "boolean", + description: + "Save the image to disk so it can be attached to a message for the user. Returns the saved path in the tool result. Only set this when you intend to share the image — screenshots you're just looking at don't need saving.", + }, + }, + required: [], + }, + }, + + { + name: "zoom", + description: + "Take a higher-resolution screenshot of a specific region of the last full-screen screenshot. Use this liberally to inspect small text, button labels, or fine UI details that are hard to read in the downsampled full-screen image. " + + "IMPORTANT: Coordinates in subsequent click calls always refer to the full-screen screenshot, never the zoomed image. This tool is read-only for inspecting detail.", + inputSchema: { + type: "object" as const, + properties: { + region: { + type: "array", + items: { type: "integer" }, + minItems: 4, + maxItems: 4, + description: + "(x0, y0, x1, y1): Rectangle to zoom into, in the coordinate space of the most recent full-screen screenshot. x0,y0 = top-left, x1,y1 = bottom-right.", + }, + save_to_disk: { + type: "boolean", + description: + "Save the image to disk so it can be attached to a message for the user. Returns the saved path in the tool result. Only set this when you intend to share the image.", + }, + }, + required: ["region"], + }, + }, + + { + name: "left_click", + description: `Left-click at the given coordinates. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + text: clickModifierText, + }, + required: ["coordinate"], + }, + }, + + { + name: "double_click", + description: `Double-click at the given coordinates. Selects a word in most text editors. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + text: clickModifierText, + }, + required: ["coordinate"], + }, + }, + + { + name: "triple_click", + description: `Triple-click at the given coordinates. Selects a line in most text editors. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + text: clickModifierText, + }, + required: ["coordinate"], + }, + }, + + { + name: "right_click", + description: `Right-click at the given coordinates. Opens a context menu in most applications. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + text: clickModifierText, + }, + required: ["coordinate"], + }, + }, + + { + name: "middle_click", + description: `Middle-click (scroll-wheel click) at the given coordinates. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + text: clickModifierText, + }, + required: ["coordinate"], + }, + }, + + { + name: "type", + description: `Type text into whatever currently has keyboard focus. ${FRONTMOST_GATE_DESC} Newlines are supported. For keyboard shortcuts use \`key\` instead.`, + inputSchema: { + type: "object" as const, + properties: { + text: { type: "string", description: "Text to type." }, + }, + required: ["text"], + }, + }, + + { + name: "key", + description: + `Press a key or key combination (e.g. "return", "escape", "cmd+a", "ctrl+shift+tab"). ${FRONTMOST_GATE_DESC} ` + + "System-level combos (quit app, switch app, lock screen) require the `systemKeyCombos` grant — without it they return an error. All other combos work.", + inputSchema: { + type: "object" as const, + properties: { + text: { + type: "string", + description: 'Modifiers joined with "+", e.g. "cmd+shift+a".', + }, + repeat: { + type: "integer", + minimum: 1, + maximum: 100, + description: "Number of times to repeat the key press. Default is 1.", + }, + }, + required: ["text"], + }, + }, + + { + name: "scroll", + description: `Scroll at the given coordinates. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + scroll_direction: { + type: "string", + enum: ["up", "down", "left", "right"], + description: "Direction to scroll.", + }, + scroll_amount: { + type: "integer", + minimum: 0, + maximum: 100, + description: "Number of scroll ticks.", + }, + }, + required: ["coordinate", "scroll_direction", "scroll_amount"], + }, + }, + + { + name: "left_click_drag", + description: `Press, move to target, and release. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: { + ...coordinateTuple, + description: `(x, y) end point: ${coord.x}`, + }, + start_coordinate: { + ...coordinateTuple, + description: `(x, y) start point. If omitted, drags from the current cursor position. ${coord.x}`, + }, + }, + required: ["coordinate"], + }, + }, + + { + name: "mouse_move", + description: `Move the mouse cursor without clicking. Useful for triggering hover states. ${FRONTMOST_GATE_DESC}`, + inputSchema: { + type: "object" as const, + properties: { + coordinate: coordinateTuple, + }, + required: ["coordinate"], + }, + }, + + { + name: "open_application", + description: + "Bring an application to the front, launching it if necessary. The target application must already be in the session allowlist — call request_access first.", + inputSchema: { + type: "object" as const, + properties: { + app: { + type: "string", + description: + "Display name (e.g. \"Slack\") or bundle identifier (e.g. \"com.tinyspeck.slackmacgap\").", + }, + }, + required: ["app"], + }, + }, + + { + name: "switch_display", + description: + "Switch which monitor subsequent screenshots capture. Use this when the " + + "application you need is on a different monitor than the one shown. " + + "The screenshot tool tells you which monitor it captured and lists " + + "other attached monitors by name — pass one of those names here. " + + "After switching, call screenshot to see the new monitor. " + + 'Pass "auto" to return to automatic monitor selection.', + inputSchema: { + type: "object" as const, + properties: { + display: { + type: "string", + description: + 'Monitor name from the screenshot note (e.g. "Built-in Retina Display", ' + + '"LG UltraFine"), or "auto" to re-enable automatic selection.', + }, + }, + required: ["display"], + }, + }, + + { + name: "list_granted_applications", + description: + "List the applications currently in the session allowlist, plus the active grant flags and coordinate mode. No side effects.", + inputSchema: { + type: "object" as const, + properties: {}, + required: [], + }, + }, + + { + name: "read_clipboard", + description: + "Read the current clipboard contents as text. Requires the `clipboardRead` grant.", + inputSchema: { + type: "object" as const, + properties: {}, + required: [], + }, + }, + + { + name: "write_clipboard", + description: + "Write text to the clipboard. Requires the `clipboardWrite` grant.", + inputSchema: { + type: "object" as const, + properties: { + text: { type: "string" }, + }, + required: ["text"], + }, + }, + + { + name: "wait", + description: "Wait for a specified duration.", + inputSchema: { + type: "object" as const, + properties: { + duration: { + type: "number", + description: "Duration in seconds (0–100).", + }, + }, + required: ["duration"], + }, + }, + + { + name: "cursor_position", + description: + "Get the current mouse cursor position. Returns image-pixel coordinates relative to the most recent screenshot, or logical points if no screenshot has been taken.", + inputSchema: { + type: "object" as const, + properties: {}, + required: [], + }, + }, + + { + name: "hold_key", + description: + `Press and hold a key or key combination for the specified duration, then release. ${FRONTMOST_GATE_DESC} ` + + "System-level combos require the `systemKeyCombos` grant.", + inputSchema: { + type: "object" as const, + properties: { + text: { + type: "string", + description: 'Key or chord to hold, e.g. "space", "shift+down".', + }, + duration: { + type: "number", + description: "Duration in seconds (0–100).", + }, + }, + required: ["text", "duration"], + }, + }, + + { + name: "left_mouse_down", + description: + `Press the left mouse button at the current cursor position and leave it held. ${FRONTMOST_GATE_DESC} ` + + "Use mouse_move first to position the cursor. Call left_mouse_up to release. Errors if the button is already held.", + inputSchema: { + type: "object" as const, + properties: {}, + required: [], + }, + }, + + { + name: "left_mouse_up", + description: + `Release the left mouse button at the current cursor position. ${FRONTMOST_GATE_DESC} ` + + "Pairs with left_mouse_down. Safe to call even if the button is not currently held.", + inputSchema: { + type: "object" as const, + properties: {}, + required: [], + }, + }, + + { + name: "computer_batch", + description: + "Execute a sequence of actions in ONE tool call. Each individual tool call requires a model→API round trip (seconds); " + + "batching a predictable sequence eliminates all but one. Use this whenever you can predict the outcome of several actions ahead — " + + "e.g. click a field, type into it, press Return. Actions execute sequentially and stop on the first error. " + + `${FRONTMOST_GATE_DESC} The frontmost check runs before EACH action inside the batch — if an action opens a non-allowed app, the next action's gate fires and the batch stops there. ` + + "Mid-batch screenshot actions are allowed for inspection but coordinates in subsequent clicks always refer to the PRE-BATCH full-screen screenshot.", + inputSchema: { + type: "object" as const, + properties: { + actions: { + type: "array", + minItems: 1, + items: BATCH_ACTION_ITEM_SCHEMA, + description: + 'List of actions. Example: [{"action":"left_click","coordinate":[100,200]},{"action":"type","text":"hello"},{"action":"key","text":"Return"}]', + }, + }, + required: ["actions"], + }, + }, + + ...(caps.teachMode ? buildTeachTools(coord, installedAppsHint) : []), + ]; +} + +/** + * Teach-mode tools. Split out so the spread above stays a single expression; + * takes `coord` so `teach_step.anchor`'s description uses the same + * frozen coordinate-mode phrasing as click coords, and `installedAppsHint` + * so `request_teach_access.apps` gets the same enumeration as + * `request_access.apps` (same resolution path → same hint). + */ +function buildTeachTools( + coord: { x: string; y: string }, + installedAppsHint: string, +): Tool[] { + // Shared between teach_step (top-level) and teach_batch (inside steps[] + // items). Depends on coord, so it lives inside this factory. + const teachStepProperties = { + explanation: { + type: "string", + description: + "Tooltip body text. Explain what the user is looking at and why it matters. " + + "This is the ONLY place the user sees your words — be complete but concise.", + }, + next_preview: { + type: "string", + description: + "One line describing exactly what will happen when the user clicks Next. " + + 'Example: "Next: I\'ll click Create Bucket and type the name." ' + + "Shown below the explanation in a smaller font.", + }, + anchor: { + type: "array", + items: { type: "number" }, + minItems: 2, + maxItems: 2, + description: + `(x, y) — where the tooltip arrow points. ${coord.x} ` + + "Omit to center the tooltip with no arrow (for general-context steps).", + }, + actions: { + type: "array", + // Empty allowed — "read this, click Next" steps. + items: BATCH_ACTION_ITEM_SCHEMA, + description: + "Actions to execute when the user clicks Next. Same item schema as computer_batch.actions. " + + "Empty array is valid for purely explanatory steps. Actions run sequentially and stop on first error.", + }, + } as const; + + return [ + { + name: "request_teach_access", + description: + "Request permission to guide the user through a task step-by-step with on-screen tooltips. " + + "Use this INSTEAD OF request_access when the user wants to LEARN how to do something " + + '(phrases like "teach me", "walk me through", "show me how", "help me learn"). ' + + "On approval the main Claude window hides and a fullscreen tooltip overlay appears. " + + "You then call teach_step repeatedly; each call shows one tooltip and waits for the user to click Next. " + + "Same app-allowlist semantics as request_access, but no clipboard/system-key flags. " + + "Teach mode ends automatically when your turn ends.", + inputSchema: { + type: "object" as const, + properties: { + apps: { + type: "array", + items: { type: "string" }, + description: + 'Application display names (e.g. "Slack", "Calendar") or bundle identifiers. Resolved case-insensitively against installed apps.' + + installedAppsHint, + }, + reason: { + type: "string", + description: + 'What you will be teaching. Shown in the approval dialog as "Claude wants to guide you through {reason}". Keep it short and task-focused.', + }, + }, + required: ["apps", "reason"], + }, + }, + + { + name: "teach_step", + description: + "Show one guided-tour tooltip and wait for the user to click Next. On Next, execute the actions, " + + "take a fresh screenshot, and return both — you do NOT need a separate screenshot call between steps. " + + "The returned image shows the state after your actions ran; anchor the next teach_step against it. " + + "IMPORTANT — the user only sees the tooltip during teach mode. Put ALL narration in `explanation`. " + + "Text you emit outside teach_step calls is NOT visible until teach mode ends. " + + "Pack as many actions as possible into each step's `actions` array — the user waits through " + + "the whole round trip between clicks, so one step that fills a form beats five steps that fill one field each. " + + "Returns {exited:true} if the user clicks Exit — do not call teach_step again after that. " + + "Take an initial screenshot before your FIRST teach_step to anchor it.", + inputSchema: { + type: "object" as const, + properties: teachStepProperties, + required: ["explanation", "next_preview", "actions"], + }, + }, + + { + name: "teach_batch", + description: + "Queue multiple teach steps in one tool call. Parallels computer_batch: " + + "N steps → one model↔API round trip instead of N. Each step still shows a tooltip " + + "and waits for the user's Next click, but YOU aren't waiting for a round trip between steps. " + + "You can call teach_batch multiple times in one tour — treat each batch as one predictable " + + "SEGMENT (typically: all the steps on one page). The returned screenshot shows the state " + + "after the batch's final actions; anchor the NEXT teach_batch against it. " + + "WITHIN a batch, all anchors and click coordinates refer to the PRE-BATCH screenshot " + + "(same invariant as computer_batch) — for steps 2+ in a batch, either omit anchor " + + "(centered tooltip) or target elements you know won't have moved. " + + "Good pattern: batch 5 tooltips on page A (last step navigates) → read returned screenshot → " + + "batch 3 tooltips on page B → done. " + + "Returns {exited:true, stepsCompleted:N} if the user clicks Exit — do NOT call again after that; " + + "{stepsCompleted, stepFailed, ...} if an action errors mid-batch; " + + "otherwise {stepsCompleted, results:[...]} plus a final screenshot. " + + "Fall back to individual teach_step calls when you need to react to each intermediate screenshot.", + inputSchema: { + type: "object" as const, + properties: { + steps: { + type: "array", + minItems: 1, + items: { + type: "object", + properties: teachStepProperties, + required: ["explanation", "next_preview", "actions"], + }, + description: + "Ordered steps. Validated upfront — a typo in step 5 errors before any tooltip shows.", + }, + }, + required: ["steps"], + }, + }, + ]; +} diff --git a/_all-in-one-upload/cc-haha/src/vim/types.ts b/_all-in-one-upload/cc-haha/src/vim/types.ts new file mode 100644 index 0000000..6ce950d --- /dev/null +++ b/_all-in-one-upload/cc-haha/src/vim/types.ts @@ -0,0 +1,199 @@ +/** + * Vim Mode State Machine Types + * + * This file defines the complete state machine for vim input handling. + * The types ARE the documentation - reading them tells you how the system works. + * + * State Diagram: + * ``` + * VimState + * ┌──────────────────────────────┬──────────────────────────────────────┐ + * │ INSERT │ NORMAL │ + * │ (tracks insertedText) │ (CommandState machine) │ + * │ │ │ + * │ │ idle ──┬─[d/c/y]──► operator │ + * │ │ ├─[1-9]────► count │ + * │ │ ├─[fFtT]───► find │ + * │ │ ├─[g]──────► g │ + * │ │ ├─[r]──────► replace │ + * │ │ └─[><]─────► indent │ + * │ │ │ + * │ │ operator ─┬─[motion]──► execute │ + * │ │ ├─[0-9]────► operatorCount│ + * │ │ ├─[ia]─────► operatorTextObj + * │ │ └─[fFtT]───► operatorFind │ + * └──────────────────────────────┴──────────────────────────────────────┘ + * ``` + */ + +// ============================================================================ +// Core Types +// ============================================================================ + +export type Operator = 'delete' | 'change' | 'yank' + +export type FindType = 'f' | 'F' | 't' | 'T' + +export type TextObjScope = 'inner' | 'around' + +// ============================================================================ +// State Machine Types +// ============================================================================ + +/** + * Complete vim state. Mode determines what data is tracked. + * + * INSERT mode: Track text being typed (for dot-repeat) + * NORMAL mode: Track command being parsed (state machine) + */ +export type VimState = + | { mode: 'INSERT'; insertedText: string } + | { mode: 'NORMAL'; command: CommandState } + +/** + * Command state machine for NORMAL mode. + * + * Each state knows exactly what input it's waiting for. + * TypeScript ensures exhaustive handling in switches. + */ +export type CommandState = + | { type: 'idle' } + | { type: 'count'; digits: string } + | { type: 'operator'; op: Operator; count: number } + | { type: 'operatorCount'; op: Operator; count: number; digits: string } + | { type: 'operatorFind'; op: Operator; count: number; find: FindType } + | { + type: 'operatorTextObj' + op: Operator + count: number + scope: TextObjScope + } + | { type: 'find'; find: FindType; count: number } + | { type: 'g'; count: number } + | { type: 'operatorG'; op: Operator; count: number } + | { type: 'replace'; count: number } + | { type: 'indent'; dir: '>' | '<'; count: number } + +/** + * Persistent state that survives across commands. + * This is the "memory" of vim - what gets recalled for repeats and pastes. + */ +export type PersistentState = { + lastChange: RecordedChange | null + lastFind: { type: FindType; char: string } | null + register: string + registerIsLinewise: boolean +} + +/** + * Recorded change for dot-repeat. + * Captures everything needed to replay a command. + */ +export type RecordedChange = + | { type: 'insert'; text: string } + | { + type: 'operator' + op: Operator + motion: string + count: number + } + | { + type: 'operatorTextObj' + op: Operator + objType: string + scope: TextObjScope + count: number + } + | { + type: 'operatorFind' + op: Operator + find: FindType + char: string + count: number + } + | { type: 'replace'; char: string; count: number } + | { type: 'x'; count: number } + | { type: 'toggleCase'; count: number } + | { type: 'indent'; dir: '>' | '<'; count: number } + | { type: 'openLine'; direction: 'above' | 'below' } + | { type: 'join'; count: number } + +// ============================================================================ +// Key Groups - Named constants, no magic strings +// ============================================================================ + +export const OPERATORS = { + d: 'delete', + c: 'change', + y: 'yank', +} as const satisfies Record + +export function isOperatorKey(key: string): key is keyof typeof OPERATORS { + return key in OPERATORS +} + +export const SIMPLE_MOTIONS = new Set([ + 'h', + 'l', + 'j', + 'k', // Basic movement + 'w', + 'b', + 'e', + 'W', + 'B', + 'E', // Word motions + '0', + '^', + '$', // Line positions +]) + +export const FIND_KEYS = new Set(['f', 'F', 't', 'T']) + +export const TEXT_OBJ_SCOPES = { + i: 'inner', + a: 'around', +} as const satisfies Record + +export function isTextObjScopeKey( + key: string, +): key is keyof typeof TEXT_OBJ_SCOPES { + return key in TEXT_OBJ_SCOPES +} + +export const TEXT_OBJ_TYPES = new Set([ + 'w', + 'W', // Word/WORD + '"', + "'", + '`', // Quotes + '(', + ')', + 'b', // Parens + '[', + ']', // Brackets + '{', + '}', + 'B', // Braces + '<', + '>', // Angle brackets +]) + +export const MAX_VIM_COUNT = 10000 + +// ============================================================================ +// State Factories +// ============================================================================ + +export function createInitialVimState(): VimState { + return { mode: 'INSERT', insertedText: '' } +} + +export function createInitialPersistentState(): PersistentState { + return { + lastChange: null, + lastFind: null, + register: '', + registerIsLinewise: false, + } +} diff --git a/_all-in-one-upload/new-api/LICENSE b/_all-in-one-upload/new-api/LICENSE new file mode 100644 index 0000000..0ad25db --- /dev/null +++ b/_all-in-one-upload/new-api/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/_all-in-one-upload/new-api/bin/migration_v0.3-v0.4.sql b/_all-in-one-upload/new-api/bin/migration_v0.3-v0.4.sql new file mode 100644 index 0000000..e6103c2 --- /dev/null +++ b/_all-in-one-upload/new-api/bin/migration_v0.3-v0.4.sql @@ -0,0 +1,17 @@ +INSERT INTO abilities (`group`, model, channel_id, enabled) +SELECT c.`group`, m.model, c.id, 1 +FROM channels c +CROSS JOIN ( + SELECT 'gpt-3.5-turbo' AS model UNION ALL + SELECT 'gpt-3.5-turbo-0301' AS model UNION ALL + SELECT 'gpt-4' AS model UNION ALL + SELECT 'gpt-4-0314' AS model +) AS m +WHERE c.status = 1 + AND NOT EXISTS ( + SELECT 1 + FROM abilities a + WHERE a.`group` = c.`group` + AND a.model = m.model + AND a.channel_id = c.id +); diff --git a/_all-in-one-upload/new-api/common/endpoint_type.go b/_all-in-one-upload/new-api/common/endpoint_type.go new file mode 100644 index 0000000..a5e2ff8 --- /dev/null +++ b/_all-in-one-upload/new-api/common/endpoint_type.go @@ -0,0 +1,45 @@ +package common + +import "github.com/QuantumNous/new-api/constant" + +// GetEndpointTypesByChannelType 获取渠道最优先端点类型(所有的渠道都支持 OpenAI 端点) +func GetEndpointTypesByChannelType(channelType int, modelName string) []constant.EndpointType { + var endpointTypes []constant.EndpointType + switch channelType { + case constant.ChannelTypeJina: + endpointTypes = []constant.EndpointType{constant.EndpointTypeJinaRerank} + //case constant.ChannelTypeMidjourney, constant.ChannelTypeMidjourneyPlus: + // endpointTypes = []constant.EndpointType{constant.EndpointTypeMidjourney} + //case constant.ChannelTypeSunoAPI: + // endpointTypes = []constant.EndpointType{constant.EndpointTypeSuno} + //case constant.ChannelTypeKling: + // endpointTypes = []constant.EndpointType{constant.EndpointTypeKling} + //case constant.ChannelTypeJimeng: + // endpointTypes = []constant.EndpointType{constant.EndpointTypeJimeng} + case constant.ChannelTypeAws: + fallthrough + case constant.ChannelTypeAnthropic: + endpointTypes = []constant.EndpointType{constant.EndpointTypeAnthropic, constant.EndpointTypeOpenAI} + case constant.ChannelTypeVertexAi: + fallthrough + case constant.ChannelTypeGemini: + endpointTypes = []constant.EndpointType{constant.EndpointTypeGemini, constant.EndpointTypeOpenAI} + case constant.ChannelTypeOpenRouter: // OpenRouter 只支持 OpenAI 端点 + endpointTypes = []constant.EndpointType{constant.EndpointTypeOpenAI} + case constant.ChannelTypeXai: + endpointTypes = []constant.EndpointType{constant.EndpointTypeOpenAI, constant.EndpointTypeOpenAIResponse} + case constant.ChannelTypeSora: + endpointTypes = []constant.EndpointType{constant.EndpointTypeOpenAIVideo} + default: + if IsOpenAIResponseOnlyModel(modelName) { + endpointTypes = []constant.EndpointType{constant.EndpointTypeOpenAIResponse} + } else { + endpointTypes = []constant.EndpointType{constant.EndpointTypeOpenAI} + } + } + if IsImageGenerationModel(modelName) { + // add to first + endpointTypes = append([]constant.EndpointType{constant.EndpointTypeImageGeneration}, endpointTypes...) + } + return endpointTypes +} diff --git a/_all-in-one-upload/new-api/common/go-channel.go b/_all-in-one-upload/new-api/common/go-channel.go new file mode 100644 index 0000000..f9168fc --- /dev/null +++ b/_all-in-one-upload/new-api/common/go-channel.go @@ -0,0 +1,53 @@ +package common + +import ( + "time" +) + +func SafeSendBool(ch chan bool, value bool) (closed bool) { + defer func() { + // Recover from panic if one occured. A panic would mean the channel was closed. + if recover() != nil { + closed = true + } + }() + + // This will panic if the channel is closed. + ch <- value + + // If the code reaches here, then the channel was not closed. + return false +} + +func SafeSendString(ch chan string, value string) (closed bool) { + defer func() { + // Recover from panic if one occured. A panic would mean the channel was closed. + if recover() != nil { + closed = true + } + }() + + // This will panic if the channel is closed. + ch <- value + + // If the code reaches here, then the channel was not closed. + return false +} + +// SafeSendStringTimeout send, return true, else return false +func SafeSendStringTimeout(ch chan string, value string, timeout int) (closed bool) { + defer func() { + // Recover from panic if one occured. A panic would mean the channel was closed. + if recover() != nil { + closed = false + } + }() + + // This will panic if the channel is closed. + select { + case ch <- value: + return true + case <-time.After(time.Duration(timeout) * time.Second): + return false + } +} diff --git a/_all-in-one-upload/new-api/common/str.go b/_all-in-one-upload/new-api/common/str.go new file mode 100644 index 0000000..71391f7 --- /dev/null +++ b/_all-in-one-upload/new-api/common/str.go @@ -0,0 +1,254 @@ +package common + +import ( + "encoding/base64" + "encoding/json" + "net/url" + "regexp" + "strconv" + "strings" + "unsafe" + + "github.com/samber/lo" +) + +var ( + maskURLPattern = regexp.MustCompile(`(http|https)://[^\s/$.?#].[^\s]*`) + maskDomainPattern = regexp.MustCompile(`\b(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}\b`) + maskIPPattern = regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}\b`) + // maskApiKeyPattern matches patterns like 'api_key:xxx' or "api_key:xxx" to mask the API key value + maskApiKeyPattern = regexp.MustCompile(`(['"]?)api_key:([^\s'"]+)(['"]?)`) +) + +func GetStringIfEmpty(str string, defaultValue string) string { + if str == "" { + return defaultValue + } + return str +} + +func GetRandomString(length int) string { + if length <= 0 { + return "" + } + return lo.RandomString(length, lo.AlphanumericCharset) +} + +func MapToJsonStr(m map[string]interface{}) string { + bytes, err := json.Marshal(m) + if err != nil { + return "" + } + return string(bytes) +} + +func StrToMap(str string) (map[string]interface{}, error) { + m := make(map[string]interface{}) + err := Unmarshal([]byte(str), &m) + if err != nil { + return nil, err + } + return m, nil +} + +func StrToJsonArray(str string) ([]interface{}, error) { + var js []interface{} + err := json.Unmarshal([]byte(str), &js) + if err != nil { + return nil, err + } + return js, nil +} + +func IsJsonArray(str string) bool { + var js []interface{} + return json.Unmarshal([]byte(str), &js) == nil +} + +func IsJsonObject(str string) bool { + var js map[string]interface{} + return json.Unmarshal([]byte(str), &js) == nil +} + +func String2Int(str string) int { + num, err := strconv.Atoi(str) + if err != nil { + return 0 + } + return num +} + +func StringsContains(strs []string, str string) bool { + for _, s := range strs { + if s == str { + return true + } + } + return false +} + +// StringToByteSlice []byte only read, panic on append +func StringToByteSlice(s string) []byte { + tmp1 := (*[2]uintptr)(unsafe.Pointer(&s)) + tmp2 := [3]uintptr{tmp1[0], tmp1[1], tmp1[1]} + return *(*[]byte)(unsafe.Pointer(&tmp2)) +} + +func EncodeBase64(str string) string { + return base64.StdEncoding.EncodeToString([]byte(str)) +} + +func GetJsonString(data any) string { + if data == nil { + return "" + } + b, _ := json.Marshal(data) + return string(b) +} + +// NormalizeBillingPreference clamps the billing preference to valid values. +func NormalizeBillingPreference(pref string) string { + switch strings.TrimSpace(pref) { + case "subscription_first", "wallet_first", "subscription_only", "wallet_only": + return strings.TrimSpace(pref) + default: + return "subscription_first" + } +} + +// MaskEmail masks a user email to prevent PII leakage in logs +// Returns "***masked***" if email is empty, otherwise shows only the domain part +func MaskEmail(email string) string { + if email == "" { + return "***masked***" + } + + // Find the @ symbol + atIndex := strings.Index(email, "@") + if atIndex == -1 { + // No @ symbol found, return masked + return "***masked***" + } + + // Return only the domain part with @ symbol + return "***@" + email[atIndex+1:] +} + +// maskHostTail returns the tail parts of a domain/host that should be preserved. +// It keeps 2 parts for likely country-code TLDs (e.g., co.uk, com.cn), otherwise keeps only the TLD. +func maskHostTail(parts []string) []string { + if len(parts) < 2 { + return parts + } + lastPart := parts[len(parts)-1] + secondLastPart := parts[len(parts)-2] + if len(lastPart) == 2 && len(secondLastPart) <= 3 { + // Likely country code TLD like co.uk, com.cn + return []string{secondLastPart, lastPart} + } + return []string{lastPart} +} + +// maskHostForURL collapses subdomains and keeps only masked prefix + preserved tail. +// Example: api.openai.com -> ***.com, sub.domain.co.uk -> ***.co.uk +func maskHostForURL(host string) string { + parts := strings.Split(host, ".") + if len(parts) < 2 { + return "***" + } + tail := maskHostTail(parts) + return "***." + strings.Join(tail, ".") +} + +// maskHostForPlainDomain masks a plain domain and reflects subdomain depth with multiple ***. +// Example: openai.com -> ***.com, api.openai.com -> ***.***.com, sub.domain.co.uk -> ***.***.co.uk +func maskHostForPlainDomain(domain string) string { + parts := strings.Split(domain, ".") + if len(parts) < 2 { + return domain + } + tail := maskHostTail(parts) + numStars := len(parts) - len(tail) + if numStars < 1 { + numStars = 1 + } + stars := strings.TrimSuffix(strings.Repeat("***.", numStars), ".") + return stars + "." + strings.Join(tail, ".") +} + +// MaskSensitiveInfo masks sensitive information like URLs, IPs, and domain names in a string +// Example: +// http://example.com -> http://***.com +// https://api.test.org/v1/users/123?key=secret -> https://***.org/***/***/?key=*** +// https://sub.domain.co.uk/path/to/resource -> https://***.co.uk/***/*** +// 192.168.1.1 -> ***.***.***.*** +// openai.com -> ***.com +// www.openai.com -> ***.***.com +// api.openai.com -> ***.***.com +func MaskSensitiveInfo(str string) string { + // Mask URLs + str = maskURLPattern.ReplaceAllStringFunc(str, func(urlStr string) string { + u, err := url.Parse(urlStr) + if err != nil { + return urlStr + } + + host := u.Host + if host == "" { + return urlStr + } + + // Mask host with unified logic + maskedHost := maskHostForURL(host) + + result := u.Scheme + "://" + maskedHost + + // Mask path + if u.Path != "" && u.Path != "/" { + pathParts := strings.Split(strings.Trim(u.Path, "/"), "/") + maskedPathParts := make([]string, len(pathParts)) + for i := range pathParts { + if pathParts[i] != "" { + maskedPathParts[i] = "***" + } + } + if len(maskedPathParts) > 0 { + result += "/" + strings.Join(maskedPathParts, "/") + } + } else if u.Path == "/" { + result += "/" + } + + // Mask query parameters + if u.RawQuery != "" { + values, err := url.ParseQuery(u.RawQuery) + if err != nil { + // If can't parse query, just mask the whole query string + result += "?***" + } else { + maskedParams := make([]string, 0, len(values)) + for key := range values { + maskedParams = append(maskedParams, key+"=***") + } + if len(maskedParams) > 0 { + result += "?" + strings.Join(maskedParams, "&") + } + } + } + + return result + }) + + // Mask domain names without protocol (like openai.com, www.openai.com) + str = maskDomainPattern.ReplaceAllStringFunc(str, func(domain string) string { + return maskHostForPlainDomain(domain) + }) + + // Mask IP addresses + str = maskIPPattern.ReplaceAllString(str, "***.***.***.***") + + // Mask API keys (e.g., "api_key:AIzaSyAAAaUooTUni8AdaOkSRMda30n_Q4vrV70" -> "api_key:***") + str = maskApiKeyPattern.ReplaceAllString(str, "${1}api_key:***${3}") + + return str +} diff --git a/_all-in-one-upload/new-api/common/system_monitor_windows.go b/_all-in-one-upload/new-api/common/system_monitor_windows.go new file mode 100644 index 0000000..7db7667 --- /dev/null +++ b/_all-in-one-upload/new-api/common/system_monitor_windows.go @@ -0,0 +1,50 @@ +//go:build windows + +package common + +import ( + "os" + "syscall" + "unsafe" +) + +// GetDiskSpaceInfo 获取缓存目录所在磁盘的空间信息 (Windows) +func GetDiskSpaceInfo() DiskSpaceInfo { + cachePath := GetDiskCachePath() + if cachePath == "" { + cachePath = os.TempDir() + } + + info := DiskSpaceInfo{} + + kernel32 := syscall.NewLazyDLL("kernel32.dll") + getDiskFreeSpaceEx := kernel32.NewProc("GetDiskFreeSpaceExW") + + var freeBytesAvailable, totalBytes, totalFreeBytes uint64 + + pathPtr, err := syscall.UTF16PtrFromString(cachePath) + if err != nil { + return info + } + + ret, _, _ := getDiskFreeSpaceEx.Call( + uintptr(unsafe.Pointer(pathPtr)), + uintptr(unsafe.Pointer(&freeBytesAvailable)), + uintptr(unsafe.Pointer(&totalBytes)), + uintptr(unsafe.Pointer(&totalFreeBytes)), + ) + + if ret == 0 { + return info + } + + info.Total = totalBytes + info.Free = freeBytesAvailable + info.Used = totalBytes - totalFreeBytes + + if info.Total > 0 { + info.UsedPercent = float64(info.Used) / float64(info.Total) * 100 + } + + return info +} diff --git a/_all-in-one-upload/new-api/constant/context_key.go b/_all-in-one-upload/new-api/constant/context_key.go new file mode 100644 index 0000000..c28ad20 --- /dev/null +++ b/_all-in-one-upload/new-api/constant/context_key.go @@ -0,0 +1,69 @@ +package constant + +type ContextKey string + +const ( + ContextKeyTokenCountMeta ContextKey = "token_count_meta" + ContextKeyPromptTokens ContextKey = "prompt_tokens" + ContextKeyEstimatedTokens ContextKey = "estimated_tokens" + + ContextKeyOriginalModel ContextKey = "original_model" + ContextKeyRequestStartTime ContextKey = "request_start_time" + + /* token related keys */ + ContextKeyTokenUnlimited ContextKey = "token_unlimited_quota" + ContextKeyTokenKey ContextKey = "token_key" + ContextKeyTokenId ContextKey = "token_id" + ContextKeyTokenGroup ContextKey = "token_group" + ContextKeyTokenSpecificChannelId ContextKey = "specific_channel_id" + ContextKeyTokenModelLimitEnabled ContextKey = "token_model_limit_enabled" + ContextKeyTokenModelLimit ContextKey = "token_model_limit" + ContextKeyTokenCrossGroupRetry ContextKey = "token_cross_group_retry" + + /* channel related keys */ + ContextKeyChannelId ContextKey = "channel_id" + ContextKeyChannelName ContextKey = "channel_name" + ContextKeyChannelCreateTime ContextKey = "channel_create_time" + ContextKeyChannelBaseUrl ContextKey = "base_url" + ContextKeyChannelType ContextKey = "channel_type" + ContextKeyChannelSetting ContextKey = "channel_setting" + ContextKeyChannelOtherSetting ContextKey = "channel_other_setting" + ContextKeyChannelParamOverride ContextKey = "param_override" + ContextKeyChannelHeaderOverride ContextKey = "header_override" + ContextKeyChannelOrganization ContextKey = "channel_organization" + ContextKeyChannelAutoBan ContextKey = "auto_ban" + ContextKeyChannelModelMapping ContextKey = "model_mapping" + ContextKeyChannelStatusCodeMapping ContextKey = "status_code_mapping" + ContextKeyChannelIsMultiKey ContextKey = "channel_is_multi_key" + ContextKeyChannelMultiKeyIndex ContextKey = "channel_multi_key_index" + ContextKeyChannelKey ContextKey = "channel_key" + + ContextKeyAutoGroup ContextKey = "auto_group" + ContextKeyAutoGroupIndex ContextKey = "auto_group_index" + ContextKeyAutoGroupRetryIndex ContextKey = "auto_group_retry_index" + + /* user related keys */ + ContextKeyUserId ContextKey = "id" + ContextKeyUserSetting ContextKey = "user_setting" + ContextKeyUserQuota ContextKey = "user_quota" + ContextKeyUserStatus ContextKey = "user_status" + ContextKeyUserEmail ContextKey = "user_email" + ContextKeyUserGroup ContextKey = "user_group" + ContextKeyUsingGroup ContextKey = "group" + ContextKeyUserName ContextKey = "username" + + ContextKeyLocalCountTokens ContextKey = "local_count_tokens" + + ContextKeySystemPromptOverride ContextKey = "system_prompt_override" + + // ContextKeyFileSourcesToCleanup stores file sources that need cleanup when request ends + ContextKeyFileSourcesToCleanup ContextKey = "file_sources_to_cleanup" + + // ContextKeyAdminRejectReason stores an admin-only reject/block reason extracted from upstream responses. + // It is not returned to end users, but can be persisted into consume/error logs for debugging. + ContextKeyAdminRejectReason ContextKey = "admin_reject_reason" + + // ContextKeyLanguage stores the user's language preference for i18n + ContextKeyLanguage ContextKey = "language" + ContextKeyIsStream ContextKey = "is_stream" +) diff --git a/_all-in-one-upload/new-api/controller/channel_affinity_cache.go b/_all-in-one-upload/new-api/controller/channel_affinity_cache.go new file mode 100644 index 0000000..a72b04b --- /dev/null +++ b/_all-in-one-upload/new-api/controller/channel_affinity_cache.go @@ -0,0 +1,88 @@ +package controller + +import ( + "net/http" + "strings" + + "github.com/QuantumNous/new-api/service" + "github.com/gin-gonic/gin" +) + +func GetChannelAffinityCacheStats(c *gin.Context) { + stats := service.GetChannelAffinityCacheStats() + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": stats, + }) +} + +func ClearChannelAffinityCache(c *gin.Context) { + all := strings.TrimSpace(c.Query("all")) + ruleName := strings.TrimSpace(c.Query("rule_name")) + + if all == "true" { + deleted := service.ClearChannelAffinityCacheAll() + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": gin.H{ + "deleted": deleted, + }, + }) + return + } + + if ruleName == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "缺少参数:rule_name,或使用 all=true 清空全部", + }) + return + } + + deleted, err := service.ClearChannelAffinityCacheByRuleName(ruleName) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": gin.H{ + "deleted": deleted, + }, + }) +} + +func GetChannelAffinityUsageCacheStats(c *gin.Context) { + ruleName := strings.TrimSpace(c.Query("rule_name")) + usingGroup := strings.TrimSpace(c.Query("using_group")) + keyFp := strings.TrimSpace(c.Query("key_fp")) + + if ruleName == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "missing param: rule_name", + }) + return + } + if keyFp == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "missing param: key_fp", + }) + return + } + + stats := service.GetChannelAffinityUsageCacheStats(ruleName, usingGroup, keyFp) + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": stats, + }) +} diff --git a/_all-in-one-upload/new-api/controller/codex_oauth.go b/_all-in-one-upload/new-api/controller/codex_oauth.go new file mode 100644 index 0000000..de9743a --- /dev/null +++ b/_all-in-one-upload/new-api/controller/codex_oauth.go @@ -0,0 +1,247 @@ +package controller + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/model" + "github.com/QuantumNous/new-api/relay/channel/codex" + "github.com/QuantumNous/new-api/service" + + "github.com/gin-contrib/sessions" + "github.com/gin-gonic/gin" +) + +type codexOAuthCompleteRequest struct { + Input string `json:"input"` +} + +func codexOAuthSessionKey(channelID int, field string) string { + return fmt.Sprintf("codex_oauth_%s_%d", field, channelID) +} + +func parseCodexAuthorizationInput(input string) (code string, state string, err error) { + v := strings.TrimSpace(input) + if v == "" { + return "", "", errors.New("empty input") + } + if strings.Contains(v, "#") { + parts := strings.SplitN(v, "#", 2) + code = strings.TrimSpace(parts[0]) + state = strings.TrimSpace(parts[1]) + return code, state, nil + } + if strings.Contains(v, "code=") { + u, parseErr := url.Parse(v) + if parseErr == nil { + q := u.Query() + code = strings.TrimSpace(q.Get("code")) + state = strings.TrimSpace(q.Get("state")) + return code, state, nil + } + q, parseErr := url.ParseQuery(v) + if parseErr == nil { + code = strings.TrimSpace(q.Get("code")) + state = strings.TrimSpace(q.Get("state")) + return code, state, nil + } + } + + code = v + return code, "", nil +} + +func StartCodexOAuth(c *gin.Context) { + startCodexOAuthWithChannelID(c, 0) +} + +func StartCodexOAuthForChannel(c *gin.Context) { + channelID, err := strconv.Atoi(c.Param("id")) + if err != nil { + common.ApiError(c, fmt.Errorf("invalid channel id: %w", err)) + return + } + startCodexOAuthWithChannelID(c, channelID) +} + +func startCodexOAuthWithChannelID(c *gin.Context, channelID int) { + if channelID > 0 { + ch, err := model.GetChannelById(channelID, false) + if err != nil { + common.ApiError(c, err) + return + } + if ch == nil { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "channel not found"}) + return + } + if ch.Type != constant.ChannelTypeCodex { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "channel type is not Codex"}) + return + } + } + + flow, err := service.CreateCodexOAuthAuthorizationFlow() + if err != nil { + common.ApiError(c, err) + return + } + + session := sessions.Default(c) + session.Set(codexOAuthSessionKey(channelID, "state"), flow.State) + session.Set(codexOAuthSessionKey(channelID, "verifier"), flow.Verifier) + session.Set(codexOAuthSessionKey(channelID, "created_at"), time.Now().Unix()) + _ = session.Save() + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": gin.H{ + "authorize_url": flow.AuthorizeURL, + }, + }) +} + +func CompleteCodexOAuth(c *gin.Context) { + completeCodexOAuthWithChannelID(c, 0) +} + +func CompleteCodexOAuthForChannel(c *gin.Context) { + channelID, err := strconv.Atoi(c.Param("id")) + if err != nil { + common.ApiError(c, fmt.Errorf("invalid channel id: %w", err)) + return + } + completeCodexOAuthWithChannelID(c, channelID) +} + +func completeCodexOAuthWithChannelID(c *gin.Context, channelID int) { + req := codexOAuthCompleteRequest{} + if err := c.ShouldBindJSON(&req); err != nil { + common.ApiError(c, err) + return + } + + code, state, err := parseCodexAuthorizationInput(req.Input) + if err != nil { + common.SysError("failed to parse codex authorization input: " + err.Error()) + c.JSON(http.StatusOK, gin.H{"success": false, "message": "解析授权信息失败,请检查输入格式"}) + return + } + if strings.TrimSpace(code) == "" { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "missing authorization code"}) + return + } + if strings.TrimSpace(state) == "" { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "missing state in input"}) + return + } + + channelProxy := "" + if channelID > 0 { + ch, err := model.GetChannelById(channelID, false) + if err != nil { + common.ApiError(c, err) + return + } + if ch == nil { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "channel not found"}) + return + } + if ch.Type != constant.ChannelTypeCodex { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "channel type is not Codex"}) + return + } + channelProxy = ch.GetSetting().Proxy + } + + session := sessions.Default(c) + expectedState, _ := session.Get(codexOAuthSessionKey(channelID, "state")).(string) + verifier, _ := session.Get(codexOAuthSessionKey(channelID, "verifier")).(string) + if strings.TrimSpace(expectedState) == "" || strings.TrimSpace(verifier) == "" { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "oauth flow not started or session expired"}) + return + } + if state != expectedState { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "state mismatch"}) + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), 15*time.Second) + defer cancel() + + tokenRes, err := service.ExchangeCodexAuthorizationCodeWithProxy(ctx, code, verifier, channelProxy) + if err != nil { + common.SysError("failed to exchange codex authorization code: " + err.Error()) + c.JSON(http.StatusOK, gin.H{"success": false, "message": "授权码交换失败,请重试"}) + return + } + + accountID, ok := service.ExtractCodexAccountIDFromJWT(tokenRes.AccessToken) + if !ok { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "failed to extract account_id from access_token"}) + return + } + email, _ := service.ExtractEmailFromJWT(tokenRes.AccessToken) + + key := codex.OAuthKey{ + AccessToken: tokenRes.AccessToken, + RefreshToken: tokenRes.RefreshToken, + AccountID: accountID, + LastRefresh: time.Now().Format(time.RFC3339), + Expired: tokenRes.ExpiresAt.Format(time.RFC3339), + Email: email, + Type: "codex", + } + encoded, err := common.Marshal(key) + if err != nil { + common.ApiError(c, err) + return + } + + session.Delete(codexOAuthSessionKey(channelID, "state")) + session.Delete(codexOAuthSessionKey(channelID, "verifier")) + session.Delete(codexOAuthSessionKey(channelID, "created_at")) + _ = session.Save() + + if channelID > 0 { + if err := model.DB.Model(&model.Channel{}).Where("id = ?", channelID).Update("key", string(encoded)).Error; err != nil { + common.ApiError(c, err) + return + } + model.InitChannelCache() + service.ResetProxyClientCache() + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "saved", + "data": gin.H{ + "channel_id": channelID, + "account_id": accountID, + "email": email, + "expires_at": key.Expired, + "last_refresh": key.LastRefresh, + }, + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "generated", + "data": gin.H{ + "key": string(encoded), + "account_id": accountID, + "email": email, + "expires_at": key.Expired, + "last_refresh": key.LastRefresh, + }, + }) +} diff --git a/_all-in-one-upload/new-api/controller/custom_oauth.go b/_all-in-one-upload/new-api/controller/custom_oauth.go new file mode 100644 index 0000000..c21ec79 --- /dev/null +++ b/_all-in-one-upload/new-api/controller/custom_oauth.go @@ -0,0 +1,584 @@ +package controller + +import ( + "context" + "io" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/model" + "github.com/QuantumNous/new-api/oauth" + "github.com/gin-gonic/gin" +) + +// CustomOAuthProviderResponse is the response structure for custom OAuth providers +// It excludes sensitive fields like client_secret +type CustomOAuthProviderResponse struct { + Id int `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + Icon string `json:"icon"` + Enabled bool `json:"enabled"` + ClientId string `json:"client_id"` + AuthorizationEndpoint string `json:"authorization_endpoint"` + TokenEndpoint string `json:"token_endpoint"` + UserInfoEndpoint string `json:"user_info_endpoint"` + Scopes string `json:"scopes"` + UserIdField string `json:"user_id_field"` + UsernameField string `json:"username_field"` + DisplayNameField string `json:"display_name_field"` + EmailField string `json:"email_field"` + WellKnown string `json:"well_known"` + AuthStyle int `json:"auth_style"` + AccessPolicy string `json:"access_policy"` + AccessDeniedMessage string `json:"access_denied_message"` +} + +type UserOAuthBindingResponse struct { + ProviderId int `json:"provider_id"` + ProviderName string `json:"provider_name"` + ProviderSlug string `json:"provider_slug"` + ProviderIcon string `json:"provider_icon"` + ProviderUserId string `json:"provider_user_id"` +} + +func toCustomOAuthProviderResponse(p *model.CustomOAuthProvider) *CustomOAuthProviderResponse { + return &CustomOAuthProviderResponse{ + Id: p.Id, + Name: p.Name, + Slug: p.Slug, + Icon: p.Icon, + Enabled: p.Enabled, + ClientId: p.ClientId, + AuthorizationEndpoint: p.AuthorizationEndpoint, + TokenEndpoint: p.TokenEndpoint, + UserInfoEndpoint: p.UserInfoEndpoint, + Scopes: p.Scopes, + UserIdField: p.UserIdField, + UsernameField: p.UsernameField, + DisplayNameField: p.DisplayNameField, + EmailField: p.EmailField, + WellKnown: p.WellKnown, + AuthStyle: p.AuthStyle, + AccessPolicy: p.AccessPolicy, + AccessDeniedMessage: p.AccessDeniedMessage, + } +} + +// GetCustomOAuthProviders returns all custom OAuth providers +func GetCustomOAuthProviders(c *gin.Context) { + providers, err := model.GetAllCustomOAuthProviders() + if err != nil { + common.ApiError(c, err) + return + } + + response := make([]*CustomOAuthProviderResponse, len(providers)) + for i, p := range providers { + response[i] = toCustomOAuthProviderResponse(p) + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": response, + }) +} + +// GetCustomOAuthProvider returns a single custom OAuth provider by ID +func GetCustomOAuthProvider(c *gin.Context) { + idStr := c.Param("id") + id, err := strconv.Atoi(idStr) + if err != nil { + common.ApiErrorMsg(c, "无效的 ID") + return + } + + provider, err := model.GetCustomOAuthProviderById(id) + if err != nil { + common.ApiErrorMsg(c, "未找到该 OAuth 提供商") + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": toCustomOAuthProviderResponse(provider), + }) +} + +// CreateCustomOAuthProviderRequest is the request structure for creating a custom OAuth provider +type CreateCustomOAuthProviderRequest struct { + Name string `json:"name" binding:"required"` + Slug string `json:"slug" binding:"required"` + Icon string `json:"icon"` + Enabled bool `json:"enabled"` + ClientId string `json:"client_id" binding:"required"` + ClientSecret string `json:"client_secret" binding:"required"` + AuthorizationEndpoint string `json:"authorization_endpoint" binding:"required"` + TokenEndpoint string `json:"token_endpoint" binding:"required"` + UserInfoEndpoint string `json:"user_info_endpoint" binding:"required"` + Scopes string `json:"scopes"` + UserIdField string `json:"user_id_field"` + UsernameField string `json:"username_field"` + DisplayNameField string `json:"display_name_field"` + EmailField string `json:"email_field"` + WellKnown string `json:"well_known"` + AuthStyle int `json:"auth_style"` + AccessPolicy string `json:"access_policy"` + AccessDeniedMessage string `json:"access_denied_message"` +} + +type FetchCustomOAuthDiscoveryRequest struct { + WellKnownURL string `json:"well_known_url"` + IssuerURL string `json:"issuer_url"` +} + +// FetchCustomOAuthDiscovery fetches OIDC discovery document via backend (root-only route) +func FetchCustomOAuthDiscovery(c *gin.Context) { + var req FetchCustomOAuthDiscoveryRequest + if err := c.ShouldBindJSON(&req); err != nil { + common.ApiErrorMsg(c, "无效的请求参数: "+err.Error()) + return + } + + wellKnownURL := strings.TrimSpace(req.WellKnownURL) + issuerURL := strings.TrimSpace(req.IssuerURL) + + if wellKnownURL == "" && issuerURL == "" { + common.ApiErrorMsg(c, "请先填写 Discovery URL 或 Issuer URL") + return + } + + targetURL := wellKnownURL + if targetURL == "" { + targetURL = strings.TrimRight(issuerURL, "/") + "/.well-known/openid-configuration" + } + targetURL = strings.TrimSpace(targetURL) + + parsedURL, err := url.Parse(targetURL) + if err != nil || parsedURL.Host == "" || (parsedURL.Scheme != "http" && parsedURL.Scheme != "https") { + common.ApiErrorMsg(c, "Discovery URL 无效,仅支持 http/https") + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), 20*time.Second) + defer cancel() + + httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, targetURL, nil) + if err != nil { + common.ApiErrorMsg(c, "创建 Discovery 请求失败: "+err.Error()) + return + } + httpReq.Header.Set("Accept", "application/json") + + client := &http.Client{Timeout: 20 * time.Second} + resp, err := client.Do(httpReq) + if err != nil { + common.ApiErrorMsg(c, "获取 Discovery 配置失败: "+err.Error()) + return + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) + message := strings.TrimSpace(string(body)) + if message == "" { + message = resp.Status + } + common.ApiErrorMsg(c, "获取 Discovery 配置失败: "+message) + return + } + + var discovery map[string]any + if err = common.DecodeJson(resp.Body, &discovery); err != nil { + common.ApiErrorMsg(c, "解析 Discovery 配置失败: "+err.Error()) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": gin.H{ + "well_known_url": targetURL, + "discovery": discovery, + }, + }) +} + +// CreateCustomOAuthProvider creates a new custom OAuth provider +func CreateCustomOAuthProvider(c *gin.Context) { + var req CreateCustomOAuthProviderRequest + if err := c.ShouldBindJSON(&req); err != nil { + common.ApiErrorMsg(c, "无效的请求参数: "+err.Error()) + return + } + + // Check if slug is already taken + if model.IsSlugTaken(req.Slug, 0) { + common.ApiErrorMsg(c, "该 Slug 已被使用") + return + } + + // Check if slug conflicts with built-in providers + if oauth.IsProviderRegistered(req.Slug) && !oauth.IsCustomProvider(req.Slug) { + common.ApiErrorMsg(c, "该 Slug 与内置 OAuth 提供商冲突") + return + } + + provider := &model.CustomOAuthProvider{ + Name: req.Name, + Slug: req.Slug, + Icon: req.Icon, + Enabled: req.Enabled, + ClientId: req.ClientId, + ClientSecret: req.ClientSecret, + AuthorizationEndpoint: req.AuthorizationEndpoint, + TokenEndpoint: req.TokenEndpoint, + UserInfoEndpoint: req.UserInfoEndpoint, + Scopes: req.Scopes, + UserIdField: req.UserIdField, + UsernameField: req.UsernameField, + DisplayNameField: req.DisplayNameField, + EmailField: req.EmailField, + WellKnown: req.WellKnown, + AuthStyle: req.AuthStyle, + AccessPolicy: req.AccessPolicy, + AccessDeniedMessage: req.AccessDeniedMessage, + } + + if err := model.CreateCustomOAuthProvider(provider); err != nil { + common.ApiError(c, err) + return + } + + // Register the provider in the OAuth registry + oauth.RegisterOrUpdateCustomProvider(provider) + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "创建成功", + "data": toCustomOAuthProviderResponse(provider), + }) +} + +// UpdateCustomOAuthProviderRequest is the request structure for updating a custom OAuth provider +type UpdateCustomOAuthProviderRequest struct { + Name string `json:"name"` + Slug string `json:"slug"` + Icon *string `json:"icon"` // Optional: if nil, keep existing + Enabled *bool `json:"enabled"` // Optional: if nil, keep existing + ClientId string `json:"client_id"` + ClientSecret string `json:"client_secret"` // Optional: if empty, keep existing + AuthorizationEndpoint string `json:"authorization_endpoint"` + TokenEndpoint string `json:"token_endpoint"` + UserInfoEndpoint string `json:"user_info_endpoint"` + Scopes string `json:"scopes"` + UserIdField string `json:"user_id_field"` + UsernameField string `json:"username_field"` + DisplayNameField string `json:"display_name_field"` + EmailField string `json:"email_field"` + WellKnown *string `json:"well_known"` // Optional: if nil, keep existing + AuthStyle *int `json:"auth_style"` // Optional: if nil, keep existing + AccessPolicy *string `json:"access_policy"` // Optional: if nil, keep existing + AccessDeniedMessage *string `json:"access_denied_message"` // Optional: if nil, keep existing +} + +// UpdateCustomOAuthProvider updates an existing custom OAuth provider +func UpdateCustomOAuthProvider(c *gin.Context) { + idStr := c.Param("id") + id, err := strconv.Atoi(idStr) + if err != nil { + common.ApiErrorMsg(c, "无效的 ID") + return + } + + var req UpdateCustomOAuthProviderRequest + if err := c.ShouldBindJSON(&req); err != nil { + common.ApiErrorMsg(c, "无效的请求参数: "+err.Error()) + return + } + + // Get existing provider + provider, err := model.GetCustomOAuthProviderById(id) + if err != nil { + common.ApiErrorMsg(c, "未找到该 OAuth 提供商") + return + } + + oldSlug := provider.Slug + + // Check if new slug is taken by another provider + if req.Slug != "" && req.Slug != provider.Slug { + if model.IsSlugTaken(req.Slug, id) { + common.ApiErrorMsg(c, "该 Slug 已被使用") + return + } + // Check if slug conflicts with built-in providers + if oauth.IsProviderRegistered(req.Slug) && !oauth.IsCustomProvider(req.Slug) { + common.ApiErrorMsg(c, "该 Slug 与内置 OAuth 提供商冲突") + return + } + } + + // Update fields + if req.Name != "" { + provider.Name = req.Name + } + if req.Slug != "" { + provider.Slug = req.Slug + } + if req.Icon != nil { + provider.Icon = *req.Icon + } + if req.Enabled != nil { + provider.Enabled = *req.Enabled + } + if req.ClientId != "" { + provider.ClientId = req.ClientId + } + if req.ClientSecret != "" { + provider.ClientSecret = req.ClientSecret + } + if req.AuthorizationEndpoint != "" { + provider.AuthorizationEndpoint = req.AuthorizationEndpoint + } + if req.TokenEndpoint != "" { + provider.TokenEndpoint = req.TokenEndpoint + } + if req.UserInfoEndpoint != "" { + provider.UserInfoEndpoint = req.UserInfoEndpoint + } + if req.Scopes != "" { + provider.Scopes = req.Scopes + } + if req.UserIdField != "" { + provider.UserIdField = req.UserIdField + } + if req.UsernameField != "" { + provider.UsernameField = req.UsernameField + } + if req.DisplayNameField != "" { + provider.DisplayNameField = req.DisplayNameField + } + if req.EmailField != "" { + provider.EmailField = req.EmailField + } + if req.WellKnown != nil { + provider.WellKnown = *req.WellKnown + } + if req.AuthStyle != nil { + provider.AuthStyle = *req.AuthStyle + } + if req.AccessPolicy != nil { + provider.AccessPolicy = *req.AccessPolicy + } + if req.AccessDeniedMessage != nil { + provider.AccessDeniedMessage = *req.AccessDeniedMessage + } + + if err := model.UpdateCustomOAuthProvider(provider); err != nil { + common.ApiError(c, err) + return + } + + // Update the provider in the OAuth registry + if oldSlug != provider.Slug { + oauth.UnregisterCustomProvider(oldSlug) + } + oauth.RegisterOrUpdateCustomProvider(provider) + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "更新成功", + "data": toCustomOAuthProviderResponse(provider), + }) +} + +// DeleteCustomOAuthProvider deletes a custom OAuth provider +func DeleteCustomOAuthProvider(c *gin.Context) { + idStr := c.Param("id") + id, err := strconv.Atoi(idStr) + if err != nil { + common.ApiErrorMsg(c, "无效的 ID") + return + } + + // Get existing provider to get slug + provider, err := model.GetCustomOAuthProviderById(id) + if err != nil { + common.ApiErrorMsg(c, "未找到该 OAuth 提供商") + return + } + + // Check if there are any user bindings + count, err := model.GetBindingCountByProviderId(id) + if err != nil { + common.SysError("Failed to get binding count for provider " + strconv.Itoa(id) + ": " + err.Error()) + common.ApiErrorMsg(c, "检查用户绑定时发生错误,请稍后重试") + return + } + if count > 0 { + common.ApiErrorMsg(c, "该 OAuth 提供商还有用户绑定,无法删除。请先解除所有用户绑定。") + return + } + + if err := model.DeleteCustomOAuthProvider(id); err != nil { + common.ApiError(c, err) + return + } + + // Unregister the provider from the OAuth registry + oauth.UnregisterCustomProvider(provider.Slug) + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "删除成功", + }) +} + +func buildUserOAuthBindingsResponse(userId int) ([]UserOAuthBindingResponse, error) { + bindings, err := model.GetUserOAuthBindingsByUserId(userId) + if err != nil { + return nil, err + } + + response := make([]UserOAuthBindingResponse, 0, len(bindings)) + for _, binding := range bindings { + provider, err := model.GetCustomOAuthProviderById(binding.ProviderId) + if err != nil { + continue + } + response = append(response, UserOAuthBindingResponse{ + ProviderId: binding.ProviderId, + ProviderName: provider.Name, + ProviderSlug: provider.Slug, + ProviderIcon: provider.Icon, + ProviderUserId: binding.ProviderUserId, + }) + } + + return response, nil +} + +// GetUserOAuthBindings returns all OAuth bindings for the current user +func GetUserOAuthBindings(c *gin.Context) { + userId := c.GetInt("id") + if userId == 0 { + common.ApiErrorMsg(c, "未登录") + return + } + + response, err := buildUserOAuthBindingsResponse(userId) + if err != nil { + common.ApiError(c, err) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": response, + }) +} + +func GetUserOAuthBindingsByAdmin(c *gin.Context) { + userIdStr := c.Param("id") + userId, err := strconv.Atoi(userIdStr) + if err != nil { + common.ApiErrorMsg(c, "invalid user id") + return + } + + targetUser, err := model.GetUserById(userId, false) + if err != nil { + common.ApiError(c, err) + return + } + + myRole := c.GetInt("role") + if myRole <= targetUser.Role && myRole != common.RoleRootUser { + common.ApiErrorMsg(c, "no permission") + return + } + + response, err := buildUserOAuthBindingsResponse(userId) + if err != nil { + common.ApiError(c, err) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": response, + }) +} + +// UnbindCustomOAuth unbinds a custom OAuth provider from the current user +func UnbindCustomOAuth(c *gin.Context) { + userId := c.GetInt("id") + if userId == 0 { + common.ApiErrorMsg(c, "未登录") + return + } + + providerIdStr := c.Param("provider_id") + providerId, err := strconv.Atoi(providerIdStr) + if err != nil { + common.ApiErrorMsg(c, "无效的提供商 ID") + return + } + + if err := model.DeleteUserOAuthBinding(userId, providerId); err != nil { + common.ApiError(c, err) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "解绑成功", + }) +} + +func UnbindCustomOAuthByAdmin(c *gin.Context) { + userIdStr := c.Param("id") + userId, err := strconv.Atoi(userIdStr) + if err != nil { + common.ApiErrorMsg(c, "invalid user id") + return + } + + targetUser, err := model.GetUserById(userId, false) + if err != nil { + common.ApiError(c, err) + return + } + + myRole := c.GetInt("role") + if myRole <= targetUser.Role && myRole != common.RoleRootUser { + common.ApiErrorMsg(c, "no permission") + return + } + + providerIdStr := c.Param("provider_id") + providerId, err := strconv.Atoi(providerIdStr) + if err != nil { + common.ApiErrorMsg(c, "invalid provider id") + return + } + + if err := model.DeleteUserOAuthBinding(userId, providerId); err != nil { + common.ApiError(c, err) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "success", + }) +} diff --git a/_all-in-one-upload/new-api/controller/subscription_payment_epay.go b/_all-in-one-upload/new-api/controller/subscription_payment_epay.go new file mode 100644 index 0000000..2567654 --- /dev/null +++ b/_all-in-one-upload/new-api/controller/subscription_payment_epay.go @@ -0,0 +1,217 @@ +package controller + +import ( + "fmt" + "net/http" + "net/url" + "strconv" + "time" + + "github.com/Calcium-Ion/go-epay/epay" + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/model" + "github.com/QuantumNous/new-api/service" + "github.com/QuantumNous/new-api/setting/operation_setting" + "github.com/QuantumNous/new-api/setting/system_setting" + "github.com/gin-gonic/gin" + "github.com/samber/lo" +) + +type SubscriptionEpayPayRequest struct { + PlanId int `json:"plan_id"` + PaymentMethod string `json:"payment_method"` +} + +func SubscriptionRequestEpay(c *gin.Context) { + var req SubscriptionEpayPayRequest + if err := c.ShouldBindJSON(&req); err != nil || req.PlanId <= 0 { + common.ApiErrorMsg(c, "参数错误") + return + } + + plan, err := model.GetSubscriptionPlanById(req.PlanId) + if err != nil { + common.ApiError(c, err) + return + } + if !plan.Enabled { + common.ApiErrorMsg(c, "套餐未启用") + return + } + if plan.PriceAmount < 0.01 { + common.ApiErrorMsg(c, "套餐金额过低") + return + } + if !operation_setting.ContainsPayMethod(req.PaymentMethod) { + common.ApiErrorMsg(c, "支付方式不存在") + return + } + + userId := c.GetInt("id") + if plan.MaxPurchasePerUser > 0 { + count, err := model.CountUserSubscriptionsByPlan(userId, plan.Id) + if err != nil { + common.ApiError(c, err) + return + } + if count >= int64(plan.MaxPurchasePerUser) { + common.ApiErrorMsg(c, "已达到该套餐购买上限") + return + } + } + + callBackAddress := service.GetCallbackAddress() + returnUrl, err := url.Parse(callBackAddress + "/api/subscription/epay/return") + if err != nil { + common.ApiErrorMsg(c, "回调地址配置错误") + return + } + notifyUrl, err := url.Parse(callBackAddress + "/api/subscription/epay/notify") + if err != nil { + common.ApiErrorMsg(c, "回调地址配置错误") + return + } + + tradeNo := fmt.Sprintf("%s%d", common.GetRandomString(6), time.Now().Unix()) + tradeNo = fmt.Sprintf("SUBUSR%dNO%s", userId, tradeNo) + + client := GetEpayClient() + if client == nil { + common.ApiErrorMsg(c, "当前管理员未配置支付信息") + return + } + + order := &model.SubscriptionOrder{ + UserId: userId, + PlanId: plan.Id, + Money: plan.PriceAmount, + TradeNo: tradeNo, + PaymentMethod: req.PaymentMethod, + PaymentProvider: model.PaymentProviderEpay, + CreateTime: time.Now().Unix(), + Status: common.TopUpStatusPending, + } + if err := order.Insert(); err != nil { + common.ApiErrorMsg(c, "创建订单失败") + return + } + uri, params, err := client.Purchase(&epay.PurchaseArgs{ + Type: req.PaymentMethod, + ServiceTradeNo: tradeNo, + Name: fmt.Sprintf("SUB:%s", plan.Title), + Money: strconv.FormatFloat(plan.PriceAmount, 'f', 2, 64), + Device: epay.PC, + NotifyUrl: notifyUrl, + ReturnUrl: returnUrl, + }) + if err != nil { + _ = model.ExpireSubscriptionOrder(tradeNo, model.PaymentProviderEpay) + common.ApiErrorMsg(c, "拉起支付失败") + return + } + c.JSON(http.StatusOK, gin.H{"message": "success", "data": params, "url": uri}) +} + +func SubscriptionEpayNotify(c *gin.Context) { + var params map[string]string + + if c.Request.Method == "POST" { + // POST 请求:从 POST body 解析参数 + if err := c.Request.ParseForm(); err != nil { + _, _ = c.Writer.Write([]byte("fail")) + return + } + params = lo.Reduce(lo.Keys(c.Request.PostForm), func(r map[string]string, t string, i int) map[string]string { + r[t] = c.Request.PostForm.Get(t) + return r + }, map[string]string{}) + } else { + // GET 请求:从 URL Query 解析参数 + params = lo.Reduce(lo.Keys(c.Request.URL.Query()), func(r map[string]string, t string, i int) map[string]string { + r[t] = c.Request.URL.Query().Get(t) + return r + }, map[string]string{}) + } + + if len(params) == 0 { + _, _ = c.Writer.Write([]byte("fail")) + return + } + + client := GetEpayClient() + if client == nil { + _, _ = c.Writer.Write([]byte("fail")) + return + } + verifyInfo, err := client.Verify(params) + if err != nil || !verifyInfo.VerifyStatus { + _, _ = c.Writer.Write([]byte("fail")) + return + } + + if verifyInfo.TradeStatus != epay.StatusTradeSuccess { + _, _ = c.Writer.Write([]byte("fail")) + return + } + + LockOrder(verifyInfo.ServiceTradeNo) + defer UnlockOrder(verifyInfo.ServiceTradeNo) + + if err := model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, common.GetJsonString(verifyInfo), model.PaymentProviderEpay, verifyInfo.Type); err != nil { + _, _ = c.Writer.Write([]byte("fail")) + return + } + + _, _ = c.Writer.Write([]byte("success")) +} + +// SubscriptionEpayReturn handles browser return after payment. +// It verifies the payload and completes the order, then redirects to console. +func SubscriptionEpayReturn(c *gin.Context) { + var params map[string]string + + if c.Request.Method == "POST" { + // POST 请求:从 POST body 解析参数 + if err := c.Request.ParseForm(); err != nil { + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail") + return + } + params = lo.Reduce(lo.Keys(c.Request.PostForm), func(r map[string]string, t string, i int) map[string]string { + r[t] = c.Request.PostForm.Get(t) + return r + }, map[string]string{}) + } else { + // GET 请求:从 URL Query 解析参数 + params = lo.Reduce(lo.Keys(c.Request.URL.Query()), func(r map[string]string, t string, i int) map[string]string { + r[t] = c.Request.URL.Query().Get(t) + return r + }, map[string]string{}) + } + + if len(params) == 0 { + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail") + return + } + + client := GetEpayClient() + if client == nil { + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail") + return + } + verifyInfo, err := client.Verify(params) + if err != nil || !verifyInfo.VerifyStatus { + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail") + return + } + if verifyInfo.TradeStatus == epay.StatusTradeSuccess { + LockOrder(verifyInfo.ServiceTradeNo) + defer UnlockOrder(verifyInfo.ServiceTradeNo) + if err := model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, common.GetJsonString(verifyInfo), model.PaymentProviderEpay, verifyInfo.Type); err != nil { + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=fail") + return + } + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=success") + return + } + c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=pending") +} diff --git a/_all-in-one-upload/new-api/docker-compose.local.yml b/_all-in-one-upload/new-api/docker-compose.local.yml new file mode 100644 index 0000000..cbde737 --- /dev/null +++ b/_all-in-one-upload/new-api/docker-compose.local.yml @@ -0,0 +1,4 @@ +services: + new-api: + ports: + - "3001:3000" diff --git a/_all-in-one-upload/new-api/docs/images/aliyun.png b/_all-in-one-upload/new-api/docs/images/aliyun.png new file mode 100644 index 0000000..6266bfb Binary files /dev/null and b/_all-in-one-upload/new-api/docs/images/aliyun.png differ diff --git a/_all-in-one-upload/new-api/dto/video.go b/_all-in-one-upload/new-api/dto/video.go new file mode 100644 index 0000000..5b48146 --- /dev/null +++ b/_all-in-one-upload/new-api/dto/video.go @@ -0,0 +1,47 @@ +package dto + +type VideoRequest struct { + Model string `json:"model,omitempty" example:"kling-v1"` // Model/style ID + Prompt string `json:"prompt,omitempty" example:"宇航员站起身走了"` // Text prompt + Image string `json:"image,omitempty" example:"https://h2.inkwai.com/bs2/upload-ylab-stunt/se/ai_portal_queue_mmu_image_upscale_aiweb/3214b798-e1b4-4b00-b7af-72b5b0417420_raw_image_0.jpg"` // Image input (URL/Base64) + Duration float64 `json:"duration" example:"5.0"` // Video duration (seconds) + Width int `json:"width" example:"512"` // Video width + Height int `json:"height" example:"512"` // Video height + Fps int `json:"fps,omitempty" example:"30"` // Video frame rate + Seed int `json:"seed,omitempty" example:"20231234"` // Random seed + N int `json:"n,omitempty" example:"1"` // Number of videos to generate + ResponseFormat string `json:"response_format,omitempty" example:"url"` // Response format + User string `json:"user,omitempty" example:"user-1234"` // User identifier + Metadata map[string]any `json:"metadata,omitempty"` // Vendor-specific/custom params (e.g. negative_prompt, style, quality_level, etc.) +} + +// VideoResponse 视频生成提交任务后的响应 +type VideoResponse struct { + TaskId string `json:"task_id"` + Status string `json:"status"` +} + +// VideoTaskResponse 查询视频生成任务状态的响应 +type VideoTaskResponse struct { + TaskId string `json:"task_id" example:"abcd1234efgh"` // 任务ID + Status string `json:"status" example:"succeeded"` // 任务状态 + Url string `json:"url,omitempty"` // 视频资源URL(成功时) + Format string `json:"format,omitempty" example:"mp4"` // 视频格式 + Metadata *VideoTaskMetadata `json:"metadata,omitempty"` // 结果元数据 + Error *VideoTaskError `json:"error,omitempty"` // 错误信息(失败时) +} + +// VideoTaskMetadata 视频任务元数据 +type VideoTaskMetadata struct { + Duration float64 `json:"duration" example:"5.0"` // 实际生成的视频时长 + Fps int `json:"fps" example:"30"` // 实际帧率 + Width int `json:"width" example:"512"` // 实际宽度 + Height int `json:"height" example:"512"` // 实际高度 + Seed int `json:"seed" example:"20231234"` // 使用的随机种子 +} + +// VideoTaskError 视频任务错误信息 +type VideoTaskError struct { + Code int `json:"code"` + Message string `json:"message"` +} diff --git a/_all-in-one-upload/new-api/i18n/i18n.go b/_all-in-one-upload/new-api/i18n/i18n.go new file mode 100644 index 0000000..7ca8d2a --- /dev/null +++ b/_all-in-one-upload/new-api/i18n/i18n.go @@ -0,0 +1,231 @@ +package i18n + +import ( + "embed" + "strings" + "sync" + + "github.com/gin-gonic/gin" + "github.com/nicksnyder/go-i18n/v2/i18n" + "golang.org/x/text/language" + "gopkg.in/yaml.v3" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" +) + +const ( + LangZhCN = "zh-CN" + LangZhTW = "zh-TW" + LangEn = "en" + DefaultLang = LangEn // Fallback to English if language not supported +) + +//go:embed locales/*.yaml +var localeFS embed.FS + +var ( + bundle *i18n.Bundle + localizers = make(map[string]*i18n.Localizer) + mu sync.RWMutex + initOnce sync.Once +) + +// Init initializes the i18n bundle and loads all translation files +func Init() error { + var initErr error + initOnce.Do(func() { + bundle = i18n.NewBundle(language.Chinese) + bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal) + + // Load embedded translation files + files := []string{"locales/zh-CN.yaml", "locales/zh-TW.yaml", "locales/en.yaml"} + for _, file := range files { + _, err := bundle.LoadMessageFileFS(localeFS, file) + if err != nil { + initErr = err + return + } + } + + // Pre-create localizers for supported languages + localizers[LangZhCN] = i18n.NewLocalizer(bundle, LangZhCN) + localizers[LangZhTW] = i18n.NewLocalizer(bundle, LangZhTW) + localizers[LangEn] = i18n.NewLocalizer(bundle, LangEn) + + // Set the TranslateMessage function in common package + common.TranslateMessage = T + }) + return initErr +} + +// GetLocalizer returns a localizer for the specified language +func GetLocalizer(lang string) *i18n.Localizer { + lang = normalizeLang(lang) + + mu.RLock() + loc, ok := localizers[lang] + mu.RUnlock() + + if ok { + return loc + } + + // Create new localizer for unknown language (fallback to default) + mu.Lock() + defer mu.Unlock() + + // Double-check after acquiring write lock + if loc, ok = localizers[lang]; ok { + return loc + } + + loc = i18n.NewLocalizer(bundle, lang, DefaultLang) + localizers[lang] = loc + return loc +} + +// T translates a message key using the language from gin context +func T(c *gin.Context, key string, args ...map[string]any) string { + lang := GetLangFromContext(c) + return Translate(lang, key, args...) +} + +// Translate translates a message key for the specified language +func Translate(lang, key string, args ...map[string]any) string { + loc := GetLocalizer(lang) + + config := &i18n.LocalizeConfig{ + MessageID: key, + } + + if len(args) > 0 && args[0] != nil { + config.TemplateData = args[0] + } + + msg, err := loc.Localize(config) + if err != nil { + // Return key as fallback if translation not found + return key + } + return msg +} + +// userLangLoaderFunc is a function that loads user language from database/cache +// It's set by the model package to avoid circular imports +var userLangLoaderFunc func(userId int) string + +// SetUserLangLoader sets the function to load user language (called from model package) +func SetUserLangLoader(loader func(userId int) string) { + userLangLoaderFunc = loader +} + +// GetLangFromContext extracts the language setting from gin context +// It checks multiple sources in priority order: +// 1. User settings (ContextKeyUserSetting) - if already loaded (e.g., by TokenAuth) +// 2. Lazy load user language from cache/DB using user ID +// 3. Language set by middleware (ContextKeyLanguage) - from Accept-Language header +// 4. Default language (English) +func GetLangFromContext(c *gin.Context) string { + if c == nil { + return DefaultLang + } + + // 1. Try to get language from user settings (if already loaded by TokenAuth or other middleware) + if userSetting, ok := common.GetContextKeyType[dto.UserSetting](c, constant.ContextKeyUserSetting); ok { + if userSetting.Language != "" { + normalized := normalizeLang(userSetting.Language) + if IsSupported(normalized) { + return normalized + } + } + } + + // 2. Lazy load user language using user ID (for session-based auth where full settings aren't loaded) + if userLangLoaderFunc != nil { + if userId, exists := c.Get("id"); exists { + if uid, ok := userId.(int); ok && uid > 0 { + lang := userLangLoaderFunc(uid) + if lang != "" { + normalized := normalizeLang(lang) + if IsSupported(normalized) { + return normalized + } + } + } + } + } + + // 3. Try to get language from context (set by I18n middleware from Accept-Language) + if lang := c.GetString(string(constant.ContextKeyLanguage)); lang != "" { + normalized := normalizeLang(lang) + if IsSupported(normalized) { + return normalized + } + } + + // 4. Try Accept-Language header directly (fallback if middleware didn't run) + if acceptLang := c.GetHeader("Accept-Language"); acceptLang != "" { + lang := ParseAcceptLanguage(acceptLang) + if IsSupported(lang) { + return lang + } + } + + return DefaultLang +} + +// ParseAcceptLanguage parses the Accept-Language header and returns the preferred language +func ParseAcceptLanguage(header string) string { + if header == "" { + return DefaultLang + } + + // Simple parsing: take the first language tag + parts := strings.Split(header, ",") + if len(parts) == 0 { + return DefaultLang + } + + // Get the first language and remove quality value + firstLang := strings.TrimSpace(parts[0]) + if idx := strings.Index(firstLang, ";"); idx > 0 { + firstLang = firstLang[:idx] + } + + return normalizeLang(firstLang) +} + +// normalizeLang normalizes language code to supported format +func normalizeLang(lang string) string { + lang = strings.ToLower(strings.TrimSpace(lang)) + + // Handle common variations + switch { + case strings.HasPrefix(lang, "zh-tw"): + return LangZhTW + case strings.HasPrefix(lang, "zh"): + return LangZhCN + case strings.HasPrefix(lang, "en"): + return LangEn + default: + return DefaultLang + } +} + +// SupportedLanguages returns a list of supported language codes +func SupportedLanguages() []string { + return []string{LangZhCN, LangZhTW, LangEn} +} + +// IsSupported checks if a language code is supported +func IsSupported(lang string) bool { + lang = normalizeLang(lang) + for _, supported := range SupportedLanguages() { + if lang == supported { + return true + } + } + return false +} diff --git a/_all-in-one-upload/new-api/middleware/cache.go b/_all-in-one-upload/new-api/middleware/cache.go new file mode 100644 index 0000000..1a9dff8 --- /dev/null +++ b/_all-in-one-upload/new-api/middleware/cache.go @@ -0,0 +1,17 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" +) + +func Cache() func(c *gin.Context) { + return func(c *gin.Context) { + if c.Request.RequestURI == "/" { + c.Header("Cache-Control", "no-cache") + } else { + c.Header("Cache-Control", "max-age=604800") // one week + } + c.Header("Cache-Version", "b688f2fb5be447c25e5aa3bd063087a83db32a288bf6a4f35f2d8db310e40b14") + c.Next() + } +} diff --git a/_all-in-one-upload/new-api/middleware/distributor.go b/_all-in-one-upload/new-api/middleware/distributor.go new file mode 100644 index 0000000..2263fae --- /dev/null +++ b/_all-in-one-upload/new-api/middleware/distributor.go @@ -0,0 +1,435 @@ +package middleware + +import ( + "errors" + "fmt" + "net/http" + "slices" + "strconv" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/i18n" + "github.com/QuantumNous/new-api/model" + relayconstant "github.com/QuantumNous/new-api/relay/constant" + "github.com/QuantumNous/new-api/service" + "github.com/QuantumNous/new-api/setting/ratio_setting" + "github.com/QuantumNous/new-api/types" + + "github.com/gin-gonic/gin" +) + +type ModelRequest struct { + Model string `json:"model"` + Group string `json:"group,omitempty"` +} + +func Distribute() func(c *gin.Context) { + return func(c *gin.Context) { + var channel *model.Channel + channelId, ok := common.GetContextKey(c, constant.ContextKeyTokenSpecificChannelId) + modelRequest, shouldSelectChannel, err := getModelRequest(c) + if err != nil { + abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidRequest, map[string]any{"Error": err.Error()})) + return + } + if ok { + id, err := strconv.Atoi(channelId.(string)) + if err != nil { + abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidChannelId)) + return + } + channel, err = model.GetChannelById(id, true) + if err != nil { + abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidChannelId)) + return + } + if channel.Status != common.ChannelStatusEnabled { + abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorChannelDisabled)) + return + } + } else { + // Select a channel for the user + // check token model mapping + modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled) + if modelLimitEnable { + s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit) + if !ok { + // token model limit is empty, all models are not allowed + abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorTokenNoModelAccess)) + return + } + var tokenModelLimit map[string]bool + tokenModelLimit, ok = s.(map[string]bool) + if !ok { + tokenModelLimit = map[string]bool{} + } + matchName := ratio_setting.FormatMatchingModelName(modelRequest.Model) // match gpts & thinking-* + if _, ok := tokenModelLimit[matchName]; !ok { + abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorTokenModelForbidden, map[string]any{"Model": modelRequest.Model})) + return + } + } + + if shouldSelectChannel { + if modelRequest.Model == "" { + abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorModelNameRequired)) + return + } + var selectGroup string + usingGroup := common.GetContextKeyString(c, constant.ContextKeyUsingGroup) + // check path is /pg/chat/completions + if strings.HasPrefix(c.Request.URL.Path, "/pg/chat/completions") { + playgroundRequest := &dto.PlayGroundRequest{} + err = common.UnmarshalBodyReusable(c, playgroundRequest) + if err != nil { + abortWithOpenAiMessage(c, http.StatusBadRequest, i18n.T(c, i18n.MsgDistributorInvalidPlayground, map[string]any{"Error": err.Error()})) + return + } + if playgroundRequest.Group != "" { + if !service.GroupInUserUsableGroups(usingGroup, playgroundRequest.Group) && playgroundRequest.Group != usingGroup { + abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorGroupAccessDenied)) + return + } + usingGroup = playgroundRequest.Group + common.SetContextKey(c, constant.ContextKeyUsingGroup, usingGroup) + } + } + + if preferredChannelID, found := service.GetPreferredChannelByAffinity(c, modelRequest.Model, usingGroup); found { + preferred, err := model.CacheGetChannel(preferredChannelID) + if err == nil && preferred != nil { + if preferred.Status != common.ChannelStatusEnabled { + if service.ShouldSkipRetryAfterChannelAffinityFailure(c) { + abortWithOpenAiMessage(c, http.StatusForbidden, i18n.T(c, i18n.MsgDistributorAffinityChannelDisabled)) + return + } + } else if usingGroup == "auto" { + userGroup := common.GetContextKeyString(c, constant.ContextKeyUserGroup) + autoGroups := service.GetUserAutoGroup(userGroup) + for _, g := range autoGroups { + if model.IsChannelEnabledForGroupModel(g, modelRequest.Model, preferred.Id) { + selectGroup = g + common.SetContextKey(c, constant.ContextKeyAutoGroup, g) + channel = preferred + service.MarkChannelAffinityUsed(c, g, preferred.Id) + break + } + } + } else if model.IsChannelEnabledForGroupModel(usingGroup, modelRequest.Model, preferred.Id) { + channel = preferred + selectGroup = usingGroup + service.MarkChannelAffinityUsed(c, usingGroup, preferred.Id) + } + } + } + + if channel == nil { + channel, selectGroup, err = service.CacheGetRandomSatisfiedChannel(&service.RetryParam{ + Ctx: c, + ModelName: modelRequest.Model, + TokenGroup: usingGroup, + Retry: common.GetPointer(0), + }) + if err != nil { + showGroup := usingGroup + if usingGroup == "auto" { + showGroup = fmt.Sprintf("auto(%s)", selectGroup) + } + message := i18n.T(c, i18n.MsgDistributorGetChannelFailed, map[string]any{"Group": showGroup, "Model": modelRequest.Model, "Error": err.Error()}) + // 如果错误,但是渠道不为空,说明是数据库一致性问题 + //if channel != nil { + // common.SysError(fmt.Sprintf("渠道不存在:%d", channel.Id)) + // message = "数据库一致性已被破坏,请联系管理员" + //} + abortWithOpenAiMessage(c, http.StatusServiceUnavailable, message, types.ErrorCodeModelNotFound) + return + } + if channel == nil { + abortWithOpenAiMessage(c, http.StatusServiceUnavailable, i18n.T(c, i18n.MsgDistributorNoAvailableChannel, map[string]any{"Group": usingGroup, "Model": modelRequest.Model}), types.ErrorCodeModelNotFound) + return + } + } + } + } + common.SetContextKey(c, constant.ContextKeyRequestStartTime, time.Now()) + SetupContextForSelectedChannel(c, channel, modelRequest.Model) + c.Next() + if channel != nil && c.Writer != nil && c.Writer.Status() < http.StatusBadRequest { + service.RecordChannelAffinity(c, channel.Id) + } + } +} + +// getModelFromRequest 从请求中读取模型信息 +// 根据 Content-Type 自动处理: +// - application/json +// - application/x-www-form-urlencoded +// - multipart/form-data +func getModelFromRequest(c *gin.Context) (*ModelRequest, error) { + var modelRequest ModelRequest + err := common.UnmarshalBodyReusable(c, &modelRequest) + if err != nil { + return nil, errors.New(i18n.T(c, i18n.MsgDistributorInvalidRequest, map[string]any{"Error": err.Error()})) + } + return &modelRequest, nil +} + +func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { + var modelRequest ModelRequest + shouldSelectChannel := true + var err error + if strings.Contains(c.Request.URL.Path, "/mj/") { + relayMode := relayconstant.Path2RelayModeMidjourney(c.Request.URL.Path) + if relayMode == relayconstant.RelayModeMidjourneyTaskFetch || + relayMode == relayconstant.RelayModeMidjourneyTaskFetchByCondition || + relayMode == relayconstant.RelayModeMidjourneyNotify || + relayMode == relayconstant.RelayModeMidjourneyTaskImageSeed { + shouldSelectChannel = false + } else { + midjourneyRequest := dto.MidjourneyRequest{} + err = common.UnmarshalBodyReusable(c, &midjourneyRequest) + if err != nil { + return nil, false, errors.New(i18n.T(c, i18n.MsgDistributorInvalidMidjourney, map[string]any{"Error": err.Error()})) + } + midjourneyModel, mjErr, success := service.GetMjRequestModel(relayMode, &midjourneyRequest) + if mjErr != nil { + return nil, false, fmt.Errorf("%s", mjErr.Description) + } + if midjourneyModel == "" { + if !success { + return nil, false, fmt.Errorf("%s", i18n.T(c, i18n.MsgDistributorInvalidParseModel)) + } else { + // task fetch, task fetch by condition, notify + shouldSelectChannel = false + } + } + modelRequest.Model = midjourneyModel + } + c.Set("relay_mode", relayMode) + } else if strings.Contains(c.Request.URL.Path, "/suno/") { + relayMode := relayconstant.Path2RelaySuno(c.Request.Method, c.Request.URL.Path) + if relayMode == relayconstant.RelayModeSunoFetch || + relayMode == relayconstant.RelayModeSunoFetchByID { + shouldSelectChannel = false + } else { + modelName := service.CoverTaskActionToModelName(constant.TaskPlatformSuno, c.Param("action")) + modelRequest.Model = modelName + } + c.Set("platform", string(constant.TaskPlatformSuno)) + c.Set("relay_mode", relayMode) + } else if strings.Contains(c.Request.URL.Path, "/v1/videos/") && strings.HasSuffix(c.Request.URL.Path, "/remix") { + relayMode := relayconstant.RelayModeVideoSubmit + c.Set("relay_mode", relayMode) + shouldSelectChannel = false + } else if strings.Contains(c.Request.URL.Path, "/v1/videos") { + //curl https://api.openai.com/v1/videos \ + // -H "Authorization: Bearer $OPENAI_API_KEY" \ + // -F "model=sora-2" \ + // -F "prompt=A calico cat playing a piano on stage" + // -F input_reference="@image.jpg" + relayMode := relayconstant.RelayModeUnknown + if c.Request.Method == http.MethodPost { + relayMode = relayconstant.RelayModeVideoSubmit + req, err := getModelFromRequest(c) + if err != nil { + return nil, false, err + } + if req != nil { + modelRequest.Model = req.Model + } + } else if c.Request.Method == http.MethodGet { + relayMode = relayconstant.RelayModeVideoFetchByID + shouldSelectChannel = false + } + c.Set("relay_mode", relayMode) + } else if strings.Contains(c.Request.URL.Path, "/v1/video/generations") { + relayMode := relayconstant.RelayModeUnknown + if c.Request.Method == http.MethodPost { + req, err := getModelFromRequest(c) + if err != nil { + return nil, false, err + } + modelRequest.Model = req.Model + relayMode = relayconstant.RelayModeVideoSubmit + } else if c.Request.Method == http.MethodGet { + relayMode = relayconstant.RelayModeVideoFetchByID + shouldSelectChannel = false + } + if _, ok := c.Get("relay_mode"); !ok { + c.Set("relay_mode", relayMode) + } + } else if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") { + // Gemini API 路径处理: /v1beta/models/gemini-2.0-flash:generateContent + relayMode := relayconstant.RelayModeGemini + modelName := extractModelNameFromGeminiPath(c.Request.URL.Path) + if modelName != "" { + modelRequest.Model = modelName + } + c.Set("relay_mode", relayMode) + } else if !strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") && !strings.Contains(c.Request.Header.Get("Content-Type"), "multipart/form-data") { + req, err := getModelFromRequest(c) + if err != nil { + return nil, false, err + } + modelRequest.Model = req.Model + } + if strings.HasPrefix(c.Request.URL.Path, "/v1/realtime") { + //wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01 + modelRequest.Model = c.Query("model") + } + if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") { + if modelRequest.Model == "" { + modelRequest.Model = "text-moderation-stable" + } + } + if strings.HasSuffix(c.Request.URL.Path, "embeddings") { + if modelRequest.Model == "" { + modelRequest.Model = c.Param("model") + } + } + if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") { + modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "dall-e") + } else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") { + //modelRequest.Model = common.GetStringIfEmpty(c.PostForm("model"), "gpt-image-1") + contentType := c.ContentType() + if slices.Contains([]string{gin.MIMEPOSTForm, gin.MIMEMultipartPOSTForm}, contentType) { + req, err := getModelFromRequest(c) + if err == nil && req.Model != "" { + modelRequest.Model = req.Model + } + } + } + if strings.HasPrefix(c.Request.URL.Path, "/v1/audio") { + relayMode := relayconstant.RelayModeAudioSpeech + if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") { + + modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "tts-1") + } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") { + // 先尝试从请求读取 + if req, err := getModelFromRequest(c); err == nil && req.Model != "" { + modelRequest.Model = req.Model + } + modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1") + relayMode = relayconstant.RelayModeAudioTranslation + } else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") { + // 先尝试从请求读取 + if req, err := getModelFromRequest(c); err == nil && req.Model != "" { + modelRequest.Model = req.Model + } + modelRequest.Model = common.GetStringIfEmpty(modelRequest.Model, "whisper-1") + relayMode = relayconstant.RelayModeAudioTranscription + } + c.Set("relay_mode", relayMode) + } + if strings.HasPrefix(c.Request.URL.Path, "/pg/chat/completions") { + // playground chat completions + req, err := getModelFromRequest(c) + if err != nil { + return nil, false, err + } + modelRequest.Model = req.Model + modelRequest.Group = req.Group + common.SetContextKey(c, constant.ContextKeyTokenGroup, modelRequest.Group) + } + + if strings.HasPrefix(c.Request.URL.Path, "/v1/responses/compact") && modelRequest.Model != "" { + modelRequest.Model = ratio_setting.WithCompactModelSuffix(modelRequest.Model) + } + return &modelRequest, shouldSelectChannel, nil +} + +func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.NewAPIError { + c.Set("original_model", modelName) // for retry + if channel == nil { + return types.NewError(errors.New("channel is nil"), types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry()) + } + common.SetContextKey(c, constant.ContextKeyChannelId, channel.Id) + common.SetContextKey(c, constant.ContextKeyChannelName, channel.Name) + common.SetContextKey(c, constant.ContextKeyChannelType, channel.Type) + common.SetContextKey(c, constant.ContextKeyChannelCreateTime, channel.CreatedTime) + common.SetContextKey(c, constant.ContextKeyChannelSetting, channel.GetSetting()) + common.SetContextKey(c, constant.ContextKeyChannelOtherSetting, channel.GetOtherSettings()) + paramOverride := channel.GetParamOverride() + headerOverride := channel.GetHeaderOverride() + if mergedParam, applied := service.ApplyChannelAffinityOverrideTemplate(c, paramOverride); applied { + paramOverride = mergedParam + } + common.SetContextKey(c, constant.ContextKeyChannelParamOverride, paramOverride) + common.SetContextKey(c, constant.ContextKeyChannelHeaderOverride, headerOverride) + if nil != channel.OpenAIOrganization && *channel.OpenAIOrganization != "" { + common.SetContextKey(c, constant.ContextKeyChannelOrganization, *channel.OpenAIOrganization) + } + common.SetContextKey(c, constant.ContextKeyChannelAutoBan, channel.GetAutoBan()) + common.SetContextKey(c, constant.ContextKeyChannelModelMapping, channel.GetModelMapping()) + common.SetContextKey(c, constant.ContextKeyChannelStatusCodeMapping, channel.GetStatusCodeMapping()) + + key, index, newAPIError := channel.GetNextEnabledKey() + if newAPIError != nil { + return newAPIError + } + if channel.ChannelInfo.IsMultiKey { + common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, true) + common.SetContextKey(c, constant.ContextKeyChannelMultiKeyIndex, index) + } else { + // 必须设置为 false,否则在重试到单个 key 的时候会导致日志显示错误 + common.SetContextKey(c, constant.ContextKeyChannelIsMultiKey, false) + } + // c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", key)) + common.SetContextKey(c, constant.ContextKeyChannelKey, key) + common.SetContextKey(c, constant.ContextKeyChannelBaseUrl, channel.GetBaseURL()) + + common.SetContextKey(c, constant.ContextKeySystemPromptOverride, false) + + // TODO: api_version统一 + switch channel.Type { + case constant.ChannelTypeAzure: + c.Set("api_version", channel.Other) + case constant.ChannelTypeVertexAi: + c.Set("region", channel.Other) + case constant.ChannelTypeXunfei: + c.Set("api_version", channel.Other) + case constant.ChannelTypeGemini: + c.Set("api_version", channel.Other) + case constant.ChannelTypeAli: + c.Set("plugin", channel.Other) + case constant.ChannelCloudflare: + c.Set("api_version", channel.Other) + case constant.ChannelTypeMokaAI: + c.Set("api_version", channel.Other) + case constant.ChannelTypeCoze: + c.Set("bot_id", channel.Other) + } + return nil +} + +// extractModelNameFromGeminiPath 从 Gemini API URL 路径中提取模型名 +// 输入格式: /v1beta/models/gemini-2.0-flash:generateContent +// 输出: gemini-2.0-flash +func extractModelNameFromGeminiPath(path string) string { + // 查找 "/models/" 的位置 + modelsPrefix := "/models/" + modelsIndex := strings.Index(path, modelsPrefix) + if modelsIndex == -1 { + return "" + } + + // 从 "/models/" 之后开始提取 + startIndex := modelsIndex + len(modelsPrefix) + if startIndex >= len(path) { + return "" + } + + // 查找 ":" 的位置,模型名在 ":" 之前 + colonIndex := strings.Index(path[startIndex:], ":") + if colonIndex == -1 { + // 如果没有找到 ":",返回从 "/models/" 到路径结尾的部分 + return path[startIndex:] + } + + // 返回模型名部分 + return path[startIndex : startIndex+colonIndex] +} diff --git a/_all-in-one-upload/new-api/middleware/kling_adapter.go b/_all-in-one-upload/new-api/middleware/kling_adapter.go new file mode 100644 index 0000000..e200379 --- /dev/null +++ b/_all-in-one-upload/new-api/middleware/kling_adapter.go @@ -0,0 +1,52 @@ +package middleware + +import ( + "bytes" + "encoding/json" + "io" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + + "github.com/gin-gonic/gin" +) + +func KlingRequestConvert() func(c *gin.Context) { + return func(c *gin.Context) { + var originalReq map[string]interface{} + if err := common.UnmarshalBodyReusable(c, &originalReq); err != nil { + c.Next() + return + } + + // Support both model_name and model fields + model, _ := originalReq["model_name"].(string) + if model == "" { + model, _ = originalReq["model"].(string) + } + prompt, _ := originalReq["prompt"].(string) + + unifiedReq := map[string]interface{}{ + "model": model, + "prompt": prompt, + "metadata": originalReq, + } + + jsonData, err := json.Marshal(unifiedReq) + if err != nil { + c.Next() + return + } + + // Rewrite request body and path + c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonData)) + c.Request.URL.Path = "/v1/video/generations" + if image, ok := originalReq["image"]; !ok || image == "" { + c.Set("action", constant.TaskActionTextGenerate) + } + + // We have to reset the request body for the next handlers + c.Set(common.KeyRequestBody, jsonData) + c.Next() + } +} diff --git a/_all-in-one-upload/new-api/model/setup.go b/_all-in-one-upload/new-api/model/setup.go new file mode 100644 index 0000000..c4d7997 --- /dev/null +++ b/_all-in-one-upload/new-api/model/setup.go @@ -0,0 +1,16 @@ +package model + +type Setup struct { + ID uint `json:"id" gorm:"primaryKey"` + Version string `json:"version" gorm:"type:varchar(50);not null"` + InitializedAt int64 `json:"initialized_at" gorm:"type:bigint;not null"` +} + +func GetSetup() *Setup { + var setup Setup + err := DB.First(&setup).Error + if err != nil { + return nil + } + return &setup +} diff --git a/_all-in-one-upload/new-api/model/user_cache.go b/_all-in-one-upload/new-api/model/user_cache.go new file mode 100644 index 0000000..80d0264 --- /dev/null +++ b/_all-in-one-upload/new-api/model/user_cache.go @@ -0,0 +1,239 @@ +package model + +import ( + "fmt" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + + "github.com/gin-gonic/gin" + + "github.com/bytedance/gopkg/util/gopool" +) + +// UserBase struct remains the same as it represents the cached data structure +type UserBase struct { + Id int `json:"id"` + Group string `json:"group"` + Email string `json:"email"` + Quota int `json:"quota"` + Status int `json:"status"` + Username string `json:"username"` + Setting string `json:"setting"` +} + +func (user *UserBase) WriteContext(c *gin.Context) { + common.SetContextKey(c, constant.ContextKeyUserGroup, user.Group) + common.SetContextKey(c, constant.ContextKeyUserQuota, user.Quota) + common.SetContextKey(c, constant.ContextKeyUserStatus, user.Status) + common.SetContextKey(c, constant.ContextKeyUserEmail, user.Email) + common.SetContextKey(c, constant.ContextKeyUserName, user.Username) + common.SetContextKey(c, constant.ContextKeyUserSetting, user.GetSetting()) +} + +func (user *UserBase) GetSetting() dto.UserSetting { + setting := dto.UserSetting{} + if user.Setting != "" { + err := common.Unmarshal([]byte(user.Setting), &setting) + if err != nil { + common.SysLog("failed to unmarshal setting: " + err.Error()) + } + } + return setting +} + +// getUserCacheKey returns the key for user cache +func getUserCacheKey(userId int) string { + return fmt.Sprintf("user:%d", userId) +} + +// invalidateUserCache clears user cache +func invalidateUserCache(userId int) error { + if !common.RedisEnabled { + return nil + } + return common.RedisDelKey(getUserCacheKey(userId)) +} + +// InvalidateUserCache is the exported version of invalidateUserCache. +// 供 controller 等上层包在用户状态变更(如禁用、删除、角色变更)后主动清理缓存。 +func InvalidateUserCache(userId int) error { + return invalidateUserCache(userId) +} + +// updateUserCache updates all user cache fields using hash +func updateUserCache(user User) error { + if !common.RedisEnabled { + return nil + } + + return common.RedisHSetObj( + getUserCacheKey(user.Id), + user.ToBaseUser(), + time.Duration(common.RedisKeyCacheSeconds())*time.Second, + ) +} + +// GetUserCache gets complete user cache from hash +func GetUserCache(userId int) (userCache *UserBase, err error) { + var user *User + var fromDB bool + defer func() { + // Update Redis cache asynchronously on successful DB read + if shouldUpdateRedis(fromDB, err) && user != nil { + gopool.Go(func() { + if err := updateUserCache(*user); err != nil { + common.SysLog("failed to update user status cache: " + err.Error()) + } + }) + } + }() + + // Try getting from Redis first + userCache, err = cacheGetUserBase(userId) + if err == nil { + return userCache, nil + } + + // If Redis fails, get from DB + fromDB = true + user, err = GetUserById(userId, false) + if err != nil { + return nil, err // Return nil and error if DB lookup fails + } + + // Create cache object from user data + userCache = &UserBase{ + Id: user.Id, + Group: user.Group, + Quota: user.Quota, + Status: user.Status, + Username: user.Username, + Setting: user.Setting, + Email: user.Email, + } + + return userCache, nil +} + +func cacheGetUserBase(userId int) (*UserBase, error) { + if !common.RedisEnabled { + return nil, fmt.Errorf("redis is not enabled") + } + var userCache UserBase + // Try getting from Redis first + err := common.RedisHGetObj(getUserCacheKey(userId), &userCache) + if err != nil { + return nil, err + } + return &userCache, nil +} + +// Add atomic quota operations using hash fields +func cacheIncrUserQuota(userId int, delta int64) error { + if !common.RedisEnabled { + return nil + } + return common.RedisHIncrBy(getUserCacheKey(userId), "Quota", delta) +} + +func cacheDecrUserQuota(userId int, delta int64) error { + return cacheIncrUserQuota(userId, -delta) +} + +// Helper functions to get individual fields if needed +func getUserGroupCache(userId int) (string, error) { + cache, err := GetUserCache(userId) + if err != nil { + return "", err + } + return cache.Group, nil +} + +func getUserQuotaCache(userId int) (int, error) { + cache, err := GetUserCache(userId) + if err != nil { + return 0, err + } + return cache.Quota, nil +} + +func getUserStatusCache(userId int) (int, error) { + cache, err := GetUserCache(userId) + if err != nil { + return 0, err + } + return cache.Status, nil +} + +func getUserNameCache(userId int) (string, error) { + cache, err := GetUserCache(userId) + if err != nil { + return "", err + } + return cache.Username, nil +} + +func getUserSettingCache(userId int) (dto.UserSetting, error) { + cache, err := GetUserCache(userId) + if err != nil { + return dto.UserSetting{}, err + } + return cache.GetSetting(), nil +} + +// New functions for individual field updates +func updateUserStatusCache(userId int, status bool) error { + if !common.RedisEnabled { + return nil + } + statusInt := common.UserStatusEnabled + if !status { + statusInt = common.UserStatusDisabled + } + return common.RedisHSetField(getUserCacheKey(userId), "Status", fmt.Sprintf("%d", statusInt)) +} + +func updateUserQuotaCache(userId int, quota int) error { + if !common.RedisEnabled { + return nil + } + return common.RedisHSetField(getUserCacheKey(userId), "Quota", fmt.Sprintf("%d", quota)) +} + +func updateUserGroupCache(userId int, group string) error { + if !common.RedisEnabled { + return nil + } + return common.RedisHSetField(getUserCacheKey(userId), "Group", group) +} + +func UpdateUserGroupCache(userId int, group string) error { + return updateUserGroupCache(userId, group) +} + +func updateUserNameCache(userId int, username string) error { + if !common.RedisEnabled { + return nil + } + return common.RedisHSetField(getUserCacheKey(userId), "Username", username) +} + +func updateUserSettingCache(userId int, setting string) error { + if !common.RedisEnabled { + return nil + } + return common.RedisHSetField(getUserCacheKey(userId), "Setting", setting) +} + +// GetUserLanguage returns the user's language preference from cache +// Uses the existing GetUserCache mechanism for efficiency +func GetUserLanguage(userId int) string { + userCache, err := GetUserCache(userId) + if err != nil { + return "" + } + return userCache.GetSetting().Language +} diff --git a/_all-in-one-upload/new-api/pkg/billingexpr/billingexpr_test.go b/_all-in-one-upload/new-api/pkg/billingexpr/billingexpr_test.go new file mode 100644 index 0000000..5a5412f --- /dev/null +++ b/_all-in-one-upload/new-api/pkg/billingexpr/billingexpr_test.go @@ -0,0 +1,1094 @@ +package billingexpr_test + +import ( + "math" + "math/rand" + "testing" + + "github.com/QuantumNous/new-api/pkg/billingexpr" +) + +// --------------------------------------------------------------------------- +// Claude-style: fixed tiers, input > 200K changes both input & output price +// --------------------------------------------------------------------------- + +const claudeExpr = `p <= 200000 ? tier("standard", p * 1.5 + c * 7.5) : tier("long_context", p * 3.0 + c * 11.25)` + +func TestClaude_StandardTier(t *testing.T) { + cost, trace, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{P: 100000, C: 5000}) + if err != nil { + t.Fatal(err) + } + want := 100000*1.5 + 5000*7.5 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard") + } +} + +func TestClaude_LongContextTier(t *testing.T) { + cost, trace, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{P: 300000, C: 10000}) + if err != nil { + t.Fatal(err) + } + want := 300000*3.0 + 10000*11.25 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "long_context" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "long_context") + } +} + +func TestClaude_BoundaryExact(t *testing.T) { + cost, trace, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{P: 200000, C: 1000}) + if err != nil { + t.Fatal(err) + } + want := 200000*1.5 + 1000*7.5 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard") + } +} + +// --------------------------------------------------------------------------- +// GLM-style: multi-condition tiers with both input and output dimensions +// --------------------------------------------------------------------------- + +const glmExpr = ` +( + p < 32000 && c < 200 ? tier("tier1_short", (p)*2 + c*8) : + p < 32000 && c >= 200 ? tier("tier2_long_output", (p)*3 + c*14) : + tier("tier3_long_input", (p)*4 + c*16) +) / 1000000 +` + +func TestGLM_Tier1(t *testing.T) { + cost, trace, err := billingexpr.RunExpr(glmExpr, billingexpr.TokenParams{P: 15000, C: 100}) + if err != nil { + t.Fatal(err) + } + want := (15000.0*2 + 100.0*8) / 1000000 + if math.Abs(cost-want) > 1e-10 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "tier1_short" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "tier1_short") + } +} + +func TestGLM_Tier2(t *testing.T) { + cost, trace, err := billingexpr.RunExpr(glmExpr, billingexpr.TokenParams{P: 15000, C: 500}) + if err != nil { + t.Fatal(err) + } + want := (15000.0*3 + 500.0*14) / 1000000 + if math.Abs(cost-want) > 1e-10 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "tier2_long_output" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "tier2_long_output") + } +} + +func TestGLM_Tier3(t *testing.T) { + cost, trace, err := billingexpr.RunExpr(glmExpr, billingexpr.TokenParams{P: 50000, C: 100}) + if err != nil { + t.Fatal(err) + } + want := (50000.0*4 + 100.0*16) / 1000000 + if math.Abs(cost-want) > 1e-10 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "tier3_long_input" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "tier3_long_input") + } +} + +// --------------------------------------------------------------------------- +// Simple flat-rate (no tier() call) +// --------------------------------------------------------------------------- + +func TestSimpleExpr_NoTier(t *testing.T) { + cost, trace, err := billingexpr.RunExpr("p * 0.5 + c * 1.0", billingexpr.TokenParams{P: 1000, C: 500}) + if err != nil { + t.Fatal(err) + } + want := 1000*0.5 + 500*1.0 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "" { + t.Errorf("tier should be empty, got %q", trace.MatchedTier) + } +} + +// --------------------------------------------------------------------------- +// Math helper functions +// --------------------------------------------------------------------------- + +func TestMathHelpers(t *testing.T) { + cost, _, err := billingexpr.RunExpr("max(p, c) * 0.5 + min(p, c) * 0.1", billingexpr.TokenParams{P: 300, C: 500}) + if err != nil { + t.Fatal(err) + } + want := 500*0.5 + 300*0.1 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +func TestRequestProbeHelpers(t *testing.T) { + cost, _, err := billingexpr.RunExprWithRequest( + `p * 0.5 + c * 1.0 * (param("service_tier") == "fast" ? 2 : 1)`, + billingexpr.TokenParams{P: 1000, C: 500}, + billingexpr.RequestInput{ + Body: []byte(`{"service_tier":"fast"}`), + }, + ) + if err != nil { + t.Fatal(err) + } + want := 1000*0.5 + 500*1.0*2 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +func TestHeaderProbeHelper(t *testing.T) { + cost, _, err := billingexpr.RunExprWithRequest( + `p * 0.5 + c * 1.0 * (has(header("anthropic-beta"), "fast-mode") ? 2 : 1)`, + billingexpr.TokenParams{P: 1000, C: 500}, + billingexpr.RequestInput{ + Headers: map[string]string{ + "Anthropic-Beta": "fast-mode-2026-02-01", + }, + }, + ) + if err != nil { + t.Fatal(err) + } + want := 1000*0.5 + 500*1.0*2 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +func TestParamProbeNestedBool(t *testing.T) { + cost, _, err := billingexpr.RunExprWithRequest( + `p * (param("stream_options.fast_mode") == true ? 1.5 : 1.0)`, + billingexpr.TokenParams{P: 100}, + billingexpr.RequestInput{ + Body: []byte(`{"stream_options":{"fast_mode":true}}`), + }, + ) + if err != nil { + t.Fatal(err) + } + want := 150.0 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +func TestParamProbeArrayLength(t *testing.T) { + cost, _, err := billingexpr.RunExprWithRequest( + `p * (param("messages.#") > 20 ? 1.2 : 1.0)`, + billingexpr.TokenParams{P: 100}, + billingexpr.RequestInput{ + Body: []byte(`{"messages":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]}`), + }, + ) + if err != nil { + t.Fatal(err) + } + want := 120.0 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +func TestRequestProbeMissingFieldReturnsNil(t *testing.T) { + cost, _, err := billingexpr.RunExprWithRequest( + `param("missing.value") == nil ? 2 : 1`, + billingexpr.TokenParams{}, + billingexpr.RequestInput{ + Body: []byte(`{"service_tier":"standard"}`), + }, + ) + if err != nil { + t.Fatal(err) + } + if cost != 2 { + t.Errorf("cost = %f, want 2", cost) + } +} + +func TestRequestProbeMultipleRulesMultiply(t *testing.T) { + cost, _, err := billingexpr.RunExprWithRequest( + `(param("service_tier") == "fast" ? 2 : 1) * (has(header("anthropic-beta"), "fast-mode-2026-02-01") ? 2.5 : 1)`, + billingexpr.TokenParams{}, + billingexpr.RequestInput{ + Headers: map[string]string{ + "Anthropic-Beta": "fast-mode-2026-02-01", + }, + Body: []byte(`{"service_tier":"fast"}`), + }, + ) + if err != nil { + t.Fatal(err) + } + if math.Abs(cost-5) > 1e-6 { + t.Errorf("cost = %f, want 5", cost) + } +} + +func TestCeilFloor(t *testing.T) { + cost, _, err := billingexpr.RunExpr("ceil(p / 1000) * 0.5", billingexpr.TokenParams{P: 1500}) + if err != nil { + t.Fatal(err) + } + want := math.Ceil(1500.0/1000) * 0.5 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +// --------------------------------------------------------------------------- +// Zero tokens +// --------------------------------------------------------------------------- + +func TestZeroTokens(t *testing.T) { + cost, _, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{}) + if err != nil { + t.Fatal(err) + } + if cost != 0 { + t.Errorf("cost should be 0 for zero tokens, got %f", cost) + } +} + +// --------------------------------------------------------------------------- +// Rounding +// --------------------------------------------------------------------------- + +func TestQuotaRound(t *testing.T) { + tests := []struct { + in float64 + want int + }{ + {0, 0}, + {0.4, 0}, + {0.5, 1}, + {0.6, 1}, + {1.5, 2}, + {-0.5, -1}, + {-0.6, -1}, + {999.4999, 999}, + {999.5, 1000}, + {1e9 + 0.5, 1e9 + 1}, + } + for _, tt := range tests { + got := billingexpr.QuotaRound(tt.in) + if got != tt.want { + t.Errorf("QuotaRound(%f) = %d, want %d", tt.in, got, tt.want) + } + } +} + +// --------------------------------------------------------------------------- +// Settlement +// --------------------------------------------------------------------------- + +func TestComputeTieredQuota_Basic(t *testing.T) { + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: claudeExpr, + ExprHash: billingexpr.ExprHashString(claudeExpr), + GroupRatio: 1.0, + EstimatedPromptTokens: 100000, + EstimatedCompletionTokens: 5000, + EstimatedQuotaBeforeGroup: (100000*1.5 + 5000*7.5) / 1_000_000 * 500_000, + EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) / 1_000_000 * 500_000), + EstimatedTier: "standard", + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 300000, C: 10000}) + if err != nil { + t.Fatal(err) + } + + wantBefore := (300000*3.0 + 10000*11.25) / 1_000_000 * 500_000 + if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 { + t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore) + } + if result.MatchedTier != "long_context" { + t.Errorf("tier = %q, want %q", result.MatchedTier, "long_context") + } + if !result.CrossedTier { + t.Error("expected crossed_tier=true (estimated standard, actual long_context)") + } +} + +func TestComputeTieredQuota_SameTier(t *testing.T) { + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: claudeExpr, + ExprHash: billingexpr.ExprHashString(claudeExpr), + GroupRatio: 1.5, + EstimatedPromptTokens: 50000, + EstimatedCompletionTokens: 1000, + EstimatedQuotaBeforeGroup: (50000*1.5 + 1000*7.5) / 1_000_000 * 500_000, + EstimatedQuotaAfterGroup: billingexpr.QuotaRound((50000*1.5 + 1000*7.5) / 1_000_000 * 500_000 * 1.5), + EstimatedTier: "standard", + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 80000, C: 2000}) + if err != nil { + t.Fatal(err) + } + + wantBefore := (80000*1.5 + 2000*7.5) / 1_000_000 * 500_000 + wantAfter := billingexpr.QuotaRound(wantBefore * 1.5) + if result.ActualQuotaAfterGroup != wantAfter { + t.Errorf("after group: got %d, want %d", result.ActualQuotaAfterGroup, wantAfter) + } + if result.CrossedTier { + t.Error("expected crossed_tier=false (both standard)") + } +} + +// --------------------------------------------------------------------------- +// Compile errors +// --------------------------------------------------------------------------- + +func TestCompileError(t *testing.T) { + _, _, err := billingexpr.RunExpr("invalid +-+ syntax", billingexpr.TokenParams{}) + if err == nil { + t.Error("expected compile error") + } +} + +// --------------------------------------------------------------------------- +// Compile Cache +// --------------------------------------------------------------------------- + +func TestCompileCache_SameResult(t *testing.T) { + r1, _, err := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100}) + if err != nil { + t.Fatal(err) + } + r2, _, err := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100}) + if err != nil { + t.Fatal(err) + } + if r1 != r2 { + t.Errorf("cached and uncached results differ: %f != %f", r1, r2) + } +} + +func TestInvalidateCache(t *testing.T) { + billingexpr.InvalidateCache() + r1, _, _ := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100}) + billingexpr.InvalidateCache() + r2, _, _ := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100}) + if r1 != r2 { + t.Errorf("post-invalidate results differ: %f != %f", r1, r2) + } +} + +// --------------------------------------------------------------------------- +// Hash +// --------------------------------------------------------------------------- + +func TestExprHashString_Deterministic(t *testing.T) { + h1 := billingexpr.ExprHashString("p * 0.5") + h2 := billingexpr.ExprHashString("p * 0.5") + if h1 != h2 { + t.Error("hash should be deterministic") + } + h3 := billingexpr.ExprHashString("p * 0.6") + if h1 == h3 { + t.Error("different expressions should have different hashes") + } +} + +// --------------------------------------------------------------------------- +// Cache variables: present +// --------------------------------------------------------------------------- + +const claudeWithCacheExpr = `p <= 200000 ? tier("standard", p * 1.5 + c * 7.5 + cr * 0.15 + cc * 1.875) : tier("long_context", p * 3.0 + c * 11.25 + cr * 0.3 + cc * 3.75)` + +func TestCachePresent_StandardTier(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 50000, CC: 10000} + cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params) + if err != nil { + t.Fatal(err) + } + want := 100000*1.5 + 5000*7.5 + 50000*0.15 + 10000*1.875 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard") + } +} + +func TestCachePresent_LongContextTier(t *testing.T) { + params := billingexpr.TokenParams{P: 300000, C: 10000, CR: 100000, CC: 20000} + cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params) + if err != nil { + t.Fatal(err) + } + want := 300000*3.0 + 10000*11.25 + 100000*0.3 + 20000*3.75 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "long_context" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "long_context") + } +} + +// --------------------------------------------------------------------------- +// Cache variables: absent (all zero) — same expression still works +// --------------------------------------------------------------------------- + +func TestCacheAbsent_ZeroCacheTokens(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 5000} + cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params) + if err != nil { + t.Fatal(err) + } + want := 100000*1.5 + 5000*7.5 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f (cache terms should be 0)", cost, want) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard") + } +} + +// --------------------------------------------------------------------------- +// Mixed cache fields: cc and cc1h non-zero +// --------------------------------------------------------------------------- + +const claudeCacheSplitExpr = `tier("default", p * 1.5 + c * 7.5 + cr * 0.15 + cc * 2.0 + cc1h * 3.0)` + +func TestMixedCacheFields(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 10000, CC: 5000, CC1h: 2000} + cost, _, err := billingexpr.RunExpr(claudeCacheSplitExpr, params) + if err != nil { + t.Fatal(err) + } + want := 100000*1.5 + 5000*7.5 + 10000*0.15 + 5000*2.0 + 2000*3.0 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } +} + +func TestMixedCacheFields_AllCacheZero(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 5000} + cost, _, err := billingexpr.RunExpr(claudeCacheSplitExpr, params) + if err != nil { + t.Fatal(err) + } + want := 100000*1.5 + 5000*7.5 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f (all cache zero)", cost, want) + } +} + +// --------------------------------------------------------------------------- +// Backward compatibility: p+c only expressions still work with TokenParams +// --------------------------------------------------------------------------- + +func TestBackwardCompat_OldExprWithTokenParams(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 99999, CC: 88888} + cost, trace, err := billingexpr.RunExpr(claudeExpr, params) + if err != nil { + t.Fatal(err) + } + want := 100000*1.5 + 5000*7.5 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f (old expr ignores cache fields)", cost, want) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard") + } +} + +// --------------------------------------------------------------------------- +// Settlement with cache tokens +// --------------------------------------------------------------------------- + +func TestComputeTieredQuota_WithCache(t *testing.T) { + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: claudeWithCacheExpr, + ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr), + GroupRatio: 1.0, + EstimatedPromptTokens: 100000, + EstimatedCompletionTokens: 5000, + EstimatedQuotaBeforeGroup: (100000*1.5 + 5000*7.5) / 1_000_000 * 500_000, + EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) / 1_000_000 * 500_000), + EstimatedTier: "standard", + QuotaPerUnit: 500_000, + } + + params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 50000, CC: 10000} + result, err := billingexpr.ComputeTieredQuota(snap, params) + if err != nil { + t.Fatal(err) + } + + wantBefore := (100000*1.5 + 5000*7.5 + 50000*0.15 + 10000*1.875) / 1_000_000 * 500_000 + if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 { + t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore) + } + if result.MatchedTier != "standard" { + t.Errorf("tier = %q, want %q", result.MatchedTier, "standard") + } + if result.CrossedTier { + t.Error("expected crossed_tier=false (same tier)") + } +} + +func TestComputeTieredQuota_WithCacheCrossTier(t *testing.T) { + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: claudeWithCacheExpr, + ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr), + GroupRatio: 2.0, + EstimatedPromptTokens: 100000, + EstimatedCompletionTokens: 5000, + EstimatedQuotaBeforeGroup: (100000*1.5 + 5000*7.5) / 1_000_000 * 500_000, + EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) / 1_000_000 * 500_000 * 2.0), + EstimatedTier: "standard", + QuotaPerUnit: 500_000, + } + + params := billingexpr.TokenParams{P: 300000, C: 10000, CR: 50000, CC: 10000} + result, err := billingexpr.ComputeTieredQuota(snap, params) + if err != nil { + t.Fatal(err) + } + + wantBefore := (300000*3.0 + 10000*11.25 + 50000*0.3 + 10000*3.75) / 1_000_000 * 500_000 + wantAfter := billingexpr.QuotaRound(wantBefore * 2.0) + if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 { + t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore) + } + if result.ActualQuotaAfterGroup != wantAfter { + t.Errorf("after group: got %d, want %d", result.ActualQuotaAfterGroup, wantAfter) + } + if !result.CrossedTier { + t.Error("expected crossed_tier=true (estimated standard, actual long_context)") + } +} + +// --------------------------------------------------------------------------- +// Fuzz: random p/c/cache, verify non-negative result +// --------------------------------------------------------------------------- + +func TestFuzz_NonNegativeResults(t *testing.T) { + exprs := []string{ + claudeExpr, + claudeWithCacheExpr, + glmExpr, + "p * 0.5 + c * 1.0", + "max(p, c) * 0.1", + "p * 0.5 + cr * 0.1 + cc * 0.2", + } + + rng := rand.New(rand.NewSource(42)) + + for _, exprStr := range exprs { + for i := 0; i < 500; i++ { + params := billingexpr.TokenParams{ + P: math.Round(rng.Float64() * 1000000), + C: math.Round(rng.Float64() * 500000), + CR: math.Round(rng.Float64() * 200000), + CC: math.Round(rng.Float64() * 50000), + CC1h: math.Round(rng.Float64() * 10000), + } + + cost, _, err := billingexpr.RunExpr(exprStr, params) + if err != nil { + t.Fatalf("expr=%q params=%+v: %v", exprStr, params, err) + } + if cost < 0 { + t.Errorf("expr=%q params=%+v: negative cost %f", exprStr, params, cost) + } + } + } +} + +func TestFuzz_SettlementConsistency(t *testing.T) { + rng := rand.New(rand.NewSource(99)) + + for i := 0; i < 200; i++ { + estParams := billingexpr.TokenParams{ + P: math.Round(rng.Float64() * 500000), + C: math.Round(rng.Float64() * 100000), + CR: math.Round(rng.Float64() * 100000), + CC: math.Round(rng.Float64() * 30000), + } + actParams := billingexpr.TokenParams{ + P: math.Round(rng.Float64() * 500000), + C: math.Round(rng.Float64() * 100000), + CR: math.Round(rng.Float64() * 100000), + CC: math.Round(rng.Float64() * 30000), + } + groupRatio := 0.5 + rng.Float64()*2.0 + + estCost, estTrace, _ := billingexpr.RunExpr(claudeWithCacheExpr, estParams) + + const qpu = 500_000.0 + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: claudeWithCacheExpr, + ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr), + GroupRatio: groupRatio, + EstimatedPromptTokens: int(estParams.P), + EstimatedCompletionTokens: int(estParams.C), + EstimatedQuotaBeforeGroup: estCost / 1_000_000 * qpu, + EstimatedQuotaAfterGroup: billingexpr.QuotaRound(estCost / 1_000_000 * qpu * groupRatio), + EstimatedTier: estTrace.MatchedTier, + QuotaPerUnit: qpu, + } + + result, err := billingexpr.ComputeTieredQuota(snap, actParams) + if err != nil { + t.Fatalf("iter %d: %v", i, err) + } + + directCost, _, _ := billingexpr.RunExpr(claudeWithCacheExpr, actParams) + directQuota := billingexpr.QuotaRound(directCost / 1_000_000 * qpu * groupRatio) + + if result.ActualQuotaAfterGroup != directQuota { + t.Errorf("iter %d: settlement %d != direct %d", i, result.ActualQuotaAfterGroup, directQuota) + } + } +} + +// --------------------------------------------------------------------------- +// Settlement-level tests for ComputeTieredQuota +// --------------------------------------------------------------------------- + +func TestComputeTieredQuota_BasicSettlement(t *testing.T) { + exprStr := `tier("default", p + c)` + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 1.0, + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3000, C: 2000}) + if err != nil { + t.Fatal(err) + } + // exprOutput = 5000; quota = 5000 / 1M * 500K = 2500 + if math.Abs(result.ActualQuotaBeforeGroup-2500) > 1e-6 { + t.Errorf("before group = %f, want 2500", result.ActualQuotaBeforeGroup) + } + if result.ActualQuotaAfterGroup != 2500 { + t.Errorf("after group = %d, want 2500", result.ActualQuotaAfterGroup) + } + if result.MatchedTier != "default" { + t.Errorf("tier = %q, want default", result.MatchedTier) + } +} + +func TestComputeTieredQuota_WithGroupRatio(t *testing.T) { + exprStr := `tier("default", p + c)` + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 2.0, + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 1000, C: 500}) + if err != nil { + t.Fatal(err) + } + // exprOutput = 1500; quotaBeforeGroup = 750; afterGroup = round(750 * 2.0) = 1500 + if result.ActualQuotaAfterGroup != 1500 { + t.Errorf("after group = %d, want 1500", result.ActualQuotaAfterGroup) + } +} + +func TestComputeTieredQuota_ZeroTokens(t *testing.T) { + exprStr := `tier("default", p * 2 + c * 10)` + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 1.0, + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{}) + if err != nil { + t.Fatal(err) + } + if result.ActualQuotaAfterGroup != 0 { + t.Errorf("after group = %d, want 0", result.ActualQuotaAfterGroup) + } +} + +func TestComputeTieredQuota_RoundingEdge(t *testing.T) { + exprStr := `tier("default", p * 0.5)` // 3 * 0.5 = 1.5 (expr); 1.5 / 1M * 500K = 0.75; round(0.75) = 1 + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 1.0, + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3}) + if err != nil { + t.Fatal(err) + } + // 3 * 0.5 = 1.5 (expr); quota = 1.5 / 1M * 500K = 0.75; round(0.75) = 1 + if result.ActualQuotaAfterGroup != 1 { + t.Errorf("after group = %d, want 1 (round 0.75 up)", result.ActualQuotaAfterGroup) + } +} + +func TestComputeTieredQuota_RoundingEdgeDown(t *testing.T) { + exprStr := `tier("default", p * 0.4)` // 3 * 0.4 = 1.2 (expr); 1.2 / 1M * 500K = 0.6; round(0.6) = 1 + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 1.0, + QuotaPerUnit: 500_000, + } + + result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3}) + if err != nil { + t.Fatal(err) + } + // 3 * 0.4 = 1.2 (expr); quota = 1.2 / 1M * 500K = 0.6; round(0.6) = 1 + if result.ActualQuotaAfterGroup != 1 { + t.Errorf("after group = %d, want 1 (round 0.6 up)", result.ActualQuotaAfterGroup) + } +} + +func TestComputeTieredQuotaWithRequest_ProbeAffectsQuota(t *testing.T) { + exprStr := `param("fast") == true ? tier("fast", p * 4) : tier("normal", p * 2)` + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 1.0, + EstimatedTier: "normal", + QuotaPerUnit: 500_000, + } + + // Without request: normal tier + r1, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 1000}) + if err != nil { + t.Fatal(err) + } + // normal: p*2 = 2000; quota = 2000 / 1M * 500K = 1000 + if r1.ActualQuotaAfterGroup != 1000 { + t.Errorf("normal = %d, want 1000", r1.ActualQuotaAfterGroup) + } + + // With request: fast tier + r2, err := billingexpr.ComputeTieredQuotaWithRequest(snap, billingexpr.TokenParams{P: 1000}, billingexpr.RequestInput{ + Body: []byte(`{"fast":true}`), + }) + if err != nil { + t.Fatal(err) + } + // fast: p*4 = 4000; quota = 4000 / 1M * 500K = 2000 + if r2.ActualQuotaAfterGroup != 2000 { + t.Errorf("fast = %d, want 2000", r2.ActualQuotaAfterGroup) + } + if !r2.CrossedTier { + t.Error("expected CrossedTier = true when probe changes tier") + } +} + +func TestComputeTieredQuota_BoundaryTierCrossing(t *testing.T) { + exprStr := `p <= 100000 ? tier("small", p * 1) : tier("large", p * 2)` + snap := &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + ExprString: exprStr, + ExprHash: billingexpr.ExprHashString(exprStr), + GroupRatio: 1.0, + EstimatedTier: "small", + QuotaPerUnit: 500_000, + } + + // At boundary: small, p*1 = 100000; quota = 100000 / 1M * 500K = 50000 + r1, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 100000}) + if err != nil { + t.Fatal(err) + } + if r1.MatchedTier != "small" { + t.Errorf("at boundary: tier = %s, want small", r1.MatchedTier) + } + if r1.ActualQuotaAfterGroup != 50000 { + t.Errorf("at boundary: quota = %d, want 50000", r1.ActualQuotaAfterGroup) + } + + // Past boundary: large, p*2 = 200002; quota = 200002 / 1M * 500K = 100001 + r2, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 100001}) + if err != nil { + t.Fatal(err) + } + if r2.MatchedTier != "large" { + t.Errorf("past boundary: tier = %s, want large", r2.MatchedTier) + } + if r2.ActualQuotaAfterGroup != 100001 { + t.Errorf("past boundary: quota = %d, want 100001", r2.ActualQuotaAfterGroup) + } + if !r2.CrossedTier { + t.Error("expected CrossedTier = true") + } +} + +// --------------------------------------------------------------------------- +// Time function tests +// --------------------------------------------------------------------------- + +func TestTimeFunctions_ValidTimezone(t *testing.T) { + exprStr := `tier("default", p) * (hour("UTC") >= 0 ? 1 : 1)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100}) + if err != nil { + t.Fatal(err) + } + if cost != 100 { + t.Errorf("cost = %f, want 100", cost) + } +} + +func TestTimeFunctions_AllFunctionsCompile(t *testing.T) { + exprStr := `tier("default", p) * (hour("Asia/Shanghai") >= 0 ? 1 : 1) * (minute("UTC") >= 0 ? 1 : 1) * (weekday("UTC") >= 0 ? 1 : 1) * (month("UTC") >= 1 ? 1 : 1) * (day("UTC") >= 1 ? 1 : 1)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 500}) + if err != nil { + t.Fatal(err) + } + if cost != 500 { + t.Errorf("cost = %f, want 500", cost) + } +} + +func TestTimeFunctions_InvalidTimezone(t *testing.T) { + exprStr := `tier("default", p) * (hour("Invalid/Zone") >= 0 ? 1 : 2)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100}) + if err != nil { + t.Fatal(err) + } + // Invalid timezone falls back to UTC; hour is 0-23, so condition is always true + if cost != 100 { + t.Errorf("cost = %f, want 100 (fallback to UTC)", cost) + } +} + +func TestTimeFunctions_EmptyTimezone(t *testing.T) { + exprStr := `tier("default", p) * (hour("") >= 0 ? 1 : 2)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100}) + if err != nil { + t.Fatal(err) + } + if cost != 100 { + t.Errorf("cost = %f, want 100 (empty tz -> UTC)", cost) + } +} + +func TestTimeFunctions_NightDiscountPattern(t *testing.T) { + exprStr := `tier("default", p * 2 + c * 10) * (hour("UTC") >= 21 || hour("UTC") < 6 ? 0.5 : 1)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500}) + if err != nil { + t.Fatal(err) + } + // Base = 1000*2 + 500*10 = 7000; multiplier is either 0.5 or 1 depending on current UTC hour + if cost != 7000 && cost != 3500 { + t.Errorf("cost = %f, want 7000 or 3500", cost) + } +} + +func TestTimeFunctions_WeekdayRange(t *testing.T) { + exprStr := `tier("default", p) * (weekday("UTC") >= 0 && weekday("UTC") <= 6 ? 1 : 999)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100}) + if err != nil { + t.Fatal(err) + } + // weekday is always 0-6, so multiplier is always 1 + if cost != 100 { + t.Errorf("cost = %f, want 100", cost) + } +} + +func TestTimeFunctions_MonthDayPattern(t *testing.T) { + exprStr := `tier("default", p) * (month("Asia/Shanghai") == 1 && day("Asia/Shanghai") == 1 ? 0.5 : 1)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000}) + if err != nil { + t.Fatal(err) + } + // Either 1000 (not Jan 1) or 500 (Jan 1) — both are valid + if cost != 1000 && cost != 500 { + t.Errorf("cost = %f, want 1000 or 500", cost) + } +} + +// --------------------------------------------------------------------------- +// Image and audio token tests +// --------------------------------------------------------------------------- + +func TestImageTokenVariable(t *testing.T) { + exprStr := `tier("base", p * 2 + c * 10 + img * 5)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500, Img: 200}) + if err != nil { + t.Fatal(err) + } + // 1000*2 + 500*10 + 200*5 = 2000 + 5000 + 1000 = 8000 + if math.Abs(cost-8000) > 1e-6 { + t.Errorf("cost = %f, want 8000", cost) + } +} + +func TestAudioTokenVariables(t *testing.T) { + exprStr := `tier("base", p * 2 + c * 10 + ai * 50 + ao * 100)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500, AI: 100, AO: 50}) + if err != nil { + t.Fatal(err) + } + // 1000*2 + 500*10 + 100*50 + 50*100 = 2000 + 5000 + 5000 + 5000 = 17000 + if math.Abs(cost-17000) > 1e-6 { + t.Errorf("cost = %f, want 17000", cost) + } +} + +func TestImageAudioVariables(t *testing.T) { + exprStr := `tier("base", p * 1 + img * 3 + ai * 5 + ao * 10)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100, Img: 50, AI: 20, AO: 10}) + if err != nil { + t.Fatal(err) + } + // 100*1 + 50*3 + 20*5 + 10*10 = 100 + 150 + 100 + 100 = 450 + if math.Abs(cost-450) > 1e-6 { + t.Errorf("cost = %f, want 450", cost) + } +} + +func TestImageAudioZero(t *testing.T) { + exprStr := `tier("base", p * 2 + img * 5 + ai * 50 + ao * 100)` + cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000}) + if err != nil { + t.Fatal(err) + } + // img, ai, ao default to 0 + if math.Abs(cost-2000) > 1e-6 { + t.Errorf("cost = %f, want 2000", cost) + } +} + +// --------------------------------------------------------------------------- +// len variable tests — tier conditions based on context length +// --------------------------------------------------------------------------- + +const lenTieredExpr = `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6)` + +func TestLen_StandardTier(t *testing.T) { + params := billingexpr.TokenParams{P: 80000, C: 5000, Len: 100000, CR: 20000} + cost, trace, err := billingexpr.RunExpr(lenTieredExpr, params) + if err != nil { + t.Fatal(err) + } + want := 80000*3 + 5000*15 + 20000*0.3 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want standard", trace.MatchedTier) + } +} + +func TestLen_LongContextTier(t *testing.T) { + // p is low (cache subtracted), but len is high (full context) + params := billingexpr.TokenParams{P: 50000, C: 5000, Len: 300000, CR: 250000} + cost, trace, err := billingexpr.RunExpr(lenTieredExpr, params) + if err != nil { + t.Fatal(err) + } + want := 50000*6 + 5000*22.5 + 250000*0.6 + if math.Abs(cost-want) > 1e-6 { + t.Errorf("cost = %f, want %f", cost, want) + } + if trace.MatchedTier != "long_context" { + t.Errorf("tier = %q, want long_context (len=300000 > 200000)", trace.MatchedTier) + } +} + +func TestLen_BoundaryExact(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 1000, Len: 200000, CR: 100000} + _, trace, err := billingexpr.RunExpr(lenTieredExpr, params) + if err != nil { + t.Fatal(err) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want standard (len=200000 <= 200000)", trace.MatchedTier) + } +} + +func TestLen_BoundaryPlusOne(t *testing.T) { + params := billingexpr.TokenParams{P: 100000, C: 1000, Len: 200001, CR: 100001} + _, trace, err := billingexpr.RunExpr(lenTieredExpr, params) + if err != nil { + t.Fatal(err) + } + if trace.MatchedTier != "long_context" { + t.Errorf("tier = %q, want long_context (len=200001 > 200000)", trace.MatchedTier) + } +} + +func TestLen_ZeroDefaultsToZero(t *testing.T) { + // len defaults to 0 when not set + params := billingexpr.TokenParams{P: 1000, C: 500} + _, trace, err := billingexpr.RunExpr(lenTieredExpr, params) + if err != nil { + t.Fatal(err) + } + if trace.MatchedTier != "standard" { + t.Errorf("tier = %q, want standard (len=0 <= 200000)", trace.MatchedTier) + } +} + +// --------------------------------------------------------------------------- +// Benchmarks: compile vs cached execution +// --------------------------------------------------------------------------- + +const benchComplexExpr = `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6 + img * 3 + img_o * 30 + ai * 10 + ao * 40) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12 + img * 6 + img_o * 60 + ai * 20 + ao * 80)` + +func BenchmarkExprCompile(b *testing.B) { + for i := 0; i < b.N; i++ { + billingexpr.InvalidateCache() + billingexpr.CompileFromCache(benchComplexExpr) + } +} + +func BenchmarkExprRunCached(b *testing.B) { + billingexpr.CompileFromCache(benchComplexExpr) + params := billingexpr.TokenParams{P: 150000, C: 10000, Len: 188000, CR: 30000, CC: 5000, Img: 2000, AI: 1000, AO: 500} + b.ResetTimer() + for i := 0; i < b.N; i++ { + billingexpr.RunExpr(benchComplexExpr, params) + } +} diff --git a/_all-in-one-upload/new-api/pkg/ionet/jsonutil.go b/_all-in-one-upload/new-api/pkg/ionet/jsonutil.go new file mode 100644 index 0000000..0b3219c --- /dev/null +++ b/_all-in-one-upload/new-api/pkg/ionet/jsonutil.go @@ -0,0 +1,96 @@ +package ionet + +import ( + "encoding/json" + "strings" + "time" + + "github.com/samber/lo" +) + +// decodeWithFlexibleTimes unmarshals API responses while tolerating timestamp strings +// that omit timezone information by normalizing them to RFC3339Nano. +func decodeWithFlexibleTimes(data []byte, target interface{}) error { + var intermediate interface{} + if err := json.Unmarshal(data, &intermediate); err != nil { + return err + } + + normalized := normalizeTimeValues(intermediate) + reencoded, err := json.Marshal(normalized) + if err != nil { + return err + } + + return json.Unmarshal(reencoded, target) +} + +func decodeData[T any](data []byte, target *T) error { + var wrapper struct { + Data T `json:"data"` + } + if err := json.Unmarshal(data, &wrapper); err != nil { + return err + } + *target = wrapper.Data + return nil +} + +func decodeDataWithFlexibleTimes[T any](data []byte, target *T) error { + var wrapper struct { + Data T `json:"data"` + } + if err := decodeWithFlexibleTimes(data, &wrapper); err != nil { + return err + } + *target = wrapper.Data + return nil +} + +func normalizeTimeValues(value interface{}) interface{} { + switch v := value.(type) { + case map[string]interface{}: + return lo.MapValues(v, func(val interface{}, _ string) interface{} { + return normalizeTimeValues(val) + }) + case []interface{}: + return lo.Map(v, func(item interface{}, _ int) interface{} { + return normalizeTimeValues(item) + }) + case string: + if normalized, changed := normalizeTimeString(v); changed { + return normalized + } + return v + default: + return value + } +} + +func normalizeTimeString(input string) (string, bool) { + trimmed := strings.TrimSpace(input) + if trimmed == "" { + return input, false + } + + if _, err := time.Parse(time.RFC3339Nano, trimmed); err == nil { + return trimmed, trimmed != input + } + if _, err := time.Parse(time.RFC3339, trimmed); err == nil { + return trimmed, trimmed != input + } + + layouts := []string{ + "2006-01-02T15:04:05.999999999", + "2006-01-02T15:04:05.999999", + "2006-01-02T15:04:05", + } + + for _, layout := range layouts { + if parsed, err := time.Parse(layout, trimmed); err == nil { + return parsed.UTC().Format(time.RFC3339Nano), true + } + } + + return input, false +} diff --git a/_all-in-one-upload/new-api/relay/channel/ali/constants.go b/_all-in-one-upload/new-api/relay/channel/ali/constants.go new file mode 100644 index 0000000..df64439 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/ali/constants.go @@ -0,0 +1,14 @@ +package ali + +var ModelList = []string{ + "qwen-turbo", + "qwen-plus", + "qwen-max", + "qwen-max-longcontext", + "qwq-32b", + "qwen3-235b-a22b", + "text-embedding-v1", + "gte-rerank-v2", +} + +var ChannelName = "ali" diff --git a/_all-in-one-upload/new-api/relay/channel/gemini/relay-gemini.go b/_all-in-one-upload/new-api/relay/channel/gemini/relay-gemini.go new file mode 100644 index 0000000..355c75d --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/gemini/relay-gemini.go @@ -0,0 +1,1725 @@ +package gemini + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "strconv" + "strings" + "time" + "unicode/utf8" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/logger" + "github.com/QuantumNous/new-api/relay/channel/openai" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/relay/helper" + "github.com/QuantumNous/new-api/service" + "github.com/QuantumNous/new-api/setting/model_setting" + "github.com/QuantumNous/new-api/setting/reasoning" + "github.com/QuantumNous/new-api/types" + "github.com/gin-gonic/gin" + "github.com/samber/lo" +) + +// https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference?hl=zh-cn#blob +var geminiSupportedMimeTypes = map[string]bool{ + "application/pdf": true, + "audio/mpeg": true, + "audio/mp3": true, + "audio/wav": true, + "image/png": true, + "image/jpeg": true, + "image/jpg": true, // support old image/jpeg + "image/webp": true, + "image/heic": true, + "image/heif": true, + "text/plain": true, + "video/mov": true, + "video/mpeg": true, + "video/mp4": true, + "video/mpg": true, + "video/avi": true, + "video/wmv": true, + "video/mpegps": true, + "video/flv": true, +} + +const thoughtSignatureBypassValue = "context_engineering_is_the_way_to_go" + +// Gemini 允许的思考预算范围 +const ( + pro25MinBudget = 128 + pro25MaxBudget = 32768 + flash25MaxBudget = 24576 + flash25LiteMinBudget = 512 + flash25LiteMaxBudget = 24576 +) + +func isNew25ProModel(modelName string) bool { + return strings.HasPrefix(modelName, "gemini-2.5-pro") && + !strings.HasPrefix(modelName, "gemini-2.5-pro-preview-05-06") && + !strings.HasPrefix(modelName, "gemini-2.5-pro-preview-03-25") +} + +func is25FlashLiteModel(modelName string) bool { + return strings.HasPrefix(modelName, "gemini-2.5-flash-lite") +} + +// clampThinkingBudget 根据模型名称将预算限制在允许的范围内 +func clampThinkingBudget(modelName string, budget int) int { + isNew25Pro := isNew25ProModel(modelName) + is25FlashLite := is25FlashLiteModel(modelName) + + if is25FlashLite { + if budget < flash25LiteMinBudget { + return flash25LiteMinBudget + } + if budget > flash25LiteMaxBudget { + return flash25LiteMaxBudget + } + } else if isNew25Pro { + if budget < pro25MinBudget { + return pro25MinBudget + } + if budget > pro25MaxBudget { + return pro25MaxBudget + } + } else { // 其他模型 + if budget < 0 { + return 0 + } + if budget > flash25MaxBudget { + return flash25MaxBudget + } + } + return budget +} + +// "effort": "high" - Allocates a large portion of tokens for reasoning (approximately 80% of max_tokens) +// "effort": "medium" - Allocates a moderate portion of tokens (approximately 50% of max_tokens) +// "effort": "low" - Allocates a smaller portion of tokens (approximately 20% of max_tokens) +// "effort": "minimal" - Allocates a minimal portion of tokens (approximately 5% of max_tokens) +func clampThinkingBudgetByEffort(modelName string, effort string) int { + isNew25Pro := isNew25ProModel(modelName) + is25FlashLite := is25FlashLiteModel(modelName) + + maxBudget := 0 + if is25FlashLite { + maxBudget = flash25LiteMaxBudget + } + if isNew25Pro { + maxBudget = pro25MaxBudget + } else { + maxBudget = flash25MaxBudget + } + switch effort { + case "high": + maxBudget = maxBudget * 80 / 100 + case "medium": + maxBudget = maxBudget * 50 / 100 + case "low": + maxBudget = maxBudget * 20 / 100 + case "minimal": + maxBudget = maxBudget * 5 / 100 + } + return clampThinkingBudget(modelName, maxBudget) +} + +func ThinkingAdaptor(geminiRequest *dto.GeminiChatRequest, info *relaycommon.RelayInfo, oaiRequest ...dto.GeneralOpenAIRequest) { + if model_setting.GetGeminiSettings().ThinkingAdapterEnabled { + modelName := info.UpstreamModelName + isNew25Pro := strings.HasPrefix(modelName, "gemini-2.5-pro") && + !strings.HasPrefix(modelName, "gemini-2.5-pro-preview-05-06") && + !strings.HasPrefix(modelName, "gemini-2.5-pro-preview-03-25") + + if strings.Contains(modelName, "-thinking-") { + parts := strings.SplitN(modelName, "-thinking-", 2) + if len(parts) == 2 && parts[1] != "" { + if budgetTokens, err := strconv.Atoi(parts[1]); err == nil { + clampedBudget := clampThinkingBudget(modelName, budgetTokens) + geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{ + ThinkingBudget: common.GetPointer(clampedBudget), + IncludeThoughts: true, + } + } + } + } else if strings.HasSuffix(modelName, "-thinking") { + unsupportedModels := []string{ + "gemini-2.5-pro-preview-05-06", + "gemini-2.5-pro-preview-03-25", + } + isUnsupported := false + for _, unsupportedModel := range unsupportedModels { + if strings.HasPrefix(modelName, unsupportedModel) { + isUnsupported = true + break + } + } + + if isUnsupported { + geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{ + IncludeThoughts: true, + } + } else { + geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{ + IncludeThoughts: true, + } + if geminiRequest.GenerationConfig.MaxOutputTokens != nil && *geminiRequest.GenerationConfig.MaxOutputTokens > 0 { + budgetTokens := model_setting.GetGeminiSettings().ThinkingAdapterBudgetTokensPercentage * float64(*geminiRequest.GenerationConfig.MaxOutputTokens) + clampedBudget := clampThinkingBudget(modelName, int(budgetTokens)) + geminiRequest.GenerationConfig.ThinkingConfig.ThinkingBudget = common.GetPointer(clampedBudget) + } else { + if len(oaiRequest) > 0 { + // 如果有reasoningEffort参数,则根据其值设置思考预算 + geminiRequest.GenerationConfig.ThinkingConfig.ThinkingBudget = common.GetPointer(clampThinkingBudgetByEffort(modelName, oaiRequest[0].ReasoningEffort)) + } + } + } + } else if strings.HasSuffix(modelName, "-nothinking") { + if !isNew25Pro { + geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{ + ThinkingBudget: common.GetPointer(0), + } + } + } else if _, level, ok := reasoning.TrimEffortSuffix(info.UpstreamModelName); ok && level != "" { + geminiRequest.GenerationConfig.ThinkingConfig = &dto.GeminiThinkingConfig{ + IncludeThoughts: true, + ThinkingLevel: level, + } + info.ReasoningEffort = level + } + } +} + +// Setting safety to the lowest possible values since Gemini is already powerless enough +func CovertOpenAI2Gemini(c *gin.Context, textRequest dto.GeneralOpenAIRequest, info *relaycommon.RelayInfo) (*dto.GeminiChatRequest, error) { + + geminiRequest := dto.GeminiChatRequest{ + Contents: make([]dto.GeminiChatContent, 0, len(textRequest.Messages)), + GenerationConfig: dto.GeminiChatGenerationConfig{ + Temperature: textRequest.Temperature, + }, + } + + if textRequest.TopP != nil && *textRequest.TopP > 0 { + geminiRequest.GenerationConfig.TopP = common.GetPointer(*textRequest.TopP) + } + + if maxTokens := textRequest.GetMaxTokens(); maxTokens > 0 { + geminiRequest.GenerationConfig.MaxOutputTokens = common.GetPointer(maxTokens) + } + + if textRequest.Seed != nil && *textRequest.Seed != 0 { + geminiSeed := int64(lo.FromPtr(textRequest.Seed)) + geminiRequest.GenerationConfig.Seed = common.GetPointer(geminiSeed) + } + + attachThoughtSignature := (info.ChannelType == constant.ChannelTypeGemini || + info.ChannelType == constant.ChannelTypeVertexAi) && + model_setting.GetGeminiSettings().FunctionCallThoughtSignatureEnabled + + if model_setting.IsGeminiModelSupportImagine(info.UpstreamModelName) { + geminiRequest.GenerationConfig.ResponseModalities = []string{ + "TEXT", + "IMAGE", + } + } + if stopSequences := parseStopSequences(textRequest.Stop); len(stopSequences) > 0 { + // Gemini supports up to 5 stop sequences + if len(stopSequences) > 5 { + stopSequences = stopSequences[:5] + } + geminiRequest.GenerationConfig.StopSequences = stopSequences + } + + adaptorWithExtraBody := false + + // patch extra_body + if len(textRequest.ExtraBody) > 0 { + var extraBody map[string]interface{} + if err := common.Unmarshal(textRequest.ExtraBody, &extraBody); err != nil { + return nil, fmt.Errorf("invalid extra body: %w", err) + } + + // eg. {"google":{"thinking_config":{"thinking_budget":5324,"include_thoughts":true}}} + if googleBody, ok := extraBody["google"].(map[string]interface{}); ok { + if !strings.HasSuffix(info.UpstreamModelName, "-nothinking") { + adaptorWithExtraBody = true + // check error param name like thinkingConfig, should be thinking_config + if _, hasErrorParam := googleBody["thinkingConfig"]; hasErrorParam { + return nil, errors.New("extra_body.google.thinkingConfig is not supported, use extra_body.google.thinking_config instead") + } + + if thinkingConfig, ok := googleBody["thinking_config"].(map[string]interface{}); ok { + // check error param name like thinkingBudget, should be thinking_budget + if _, hasErrorParam := thinkingConfig["thinkingBudget"]; hasErrorParam { + return nil, errors.New("extra_body.google.thinking_config.thinkingBudget is not supported, use extra_body.google.thinking_config.thinking_budget instead") + } + var hasThinkingConfig bool + var tempThinkingConfig dto.GeminiThinkingConfig + + if thinkingBudget, exists := thinkingConfig["thinking_budget"]; exists { + switch v := thinkingBudget.(type) { + case float64: + budgetInt := int(v) + tempThinkingConfig.ThinkingBudget = common.GetPointer(budgetInt) + if budgetInt > 0 { + // 有正数预算 + tempThinkingConfig.IncludeThoughts = true + } else { + // 存在但为0或负数,禁用思考 + tempThinkingConfig.IncludeThoughts = false + } + hasThinkingConfig = true + default: + return nil, errors.New("extra_body.google.thinking_config.thinking_budget must be an integer") + } + } + + if includeThoughts, exists := thinkingConfig["include_thoughts"]; exists { + if v, ok := includeThoughts.(bool); ok { + tempThinkingConfig.IncludeThoughts = v + hasThinkingConfig = true + } else { + return nil, errors.New("extra_body.google.thinking_config.include_thoughts must be a boolean") + } + } + if thinkingLevel, exists := thinkingConfig["thinking_level"]; exists { + if v, ok := thinkingLevel.(string); ok { + tempThinkingConfig.ThinkingLevel = v + hasThinkingConfig = true + } else { + return nil, errors.New("extra_body.google.thinking_config.thinking_level must be a string") + } + } + + if hasThinkingConfig { + // 避免 panic: 仅在获得配置时分配,防止后续赋值时空指针 + if geminiRequest.GenerationConfig.ThinkingConfig == nil { + geminiRequest.GenerationConfig.ThinkingConfig = &tempThinkingConfig + } else { + // 如果已分配,则合并内容 + if tempThinkingConfig.ThinkingBudget != nil { + geminiRequest.GenerationConfig.ThinkingConfig.ThinkingBudget = tempThinkingConfig.ThinkingBudget + } + geminiRequest.GenerationConfig.ThinkingConfig.IncludeThoughts = tempThinkingConfig.IncludeThoughts + if tempThinkingConfig.ThinkingLevel != "" { + geminiRequest.GenerationConfig.ThinkingConfig.ThinkingLevel = tempThinkingConfig.ThinkingLevel + } + } + } + } + } + + // check error param name like imageConfig, should be image_config + if _, hasErrorParam := googleBody["imageConfig"]; hasErrorParam { + return nil, errors.New("extra_body.google.imageConfig is not supported, use extra_body.google.image_config instead") + } + + if imageConfig, ok := googleBody["image_config"].(map[string]interface{}); ok { + // check error param name like aspectRatio, should be aspect_ratio + if _, hasErrorParam := imageConfig["aspectRatio"]; hasErrorParam { + return nil, errors.New("extra_body.google.image_config.aspectRatio is not supported, use extra_body.google.image_config.aspect_ratio instead") + } + // check error param name like imageSize, should be image_size + if _, hasErrorParam := imageConfig["imageSize"]; hasErrorParam { + return nil, errors.New("extra_body.google.image_config.imageSize is not supported, use extra_body.google.image_config.image_size instead") + } + + // convert snake_case to camelCase for Gemini API + geminiImageConfig := make(map[string]interface{}) + if aspectRatio, ok := imageConfig["aspect_ratio"]; ok { + geminiImageConfig["aspectRatio"] = aspectRatio + } + if imageSize, ok := imageConfig["image_size"]; ok { + geminiImageConfig["imageSize"] = imageSize + } + + if len(geminiImageConfig) > 0 { + imageConfigBytes, err := common.Marshal(geminiImageConfig) + if err != nil { + return nil, fmt.Errorf("failed to marshal image_config: %w", err) + } + geminiRequest.GenerationConfig.ImageConfig = imageConfigBytes + } + } + } + } + + if !adaptorWithExtraBody { + ThinkingAdaptor(&geminiRequest, info, textRequest) + } + + safetySettings := make([]dto.GeminiChatSafetySettings, 0, len(SafetySettingList)) + for _, category := range SafetySettingList { + safetySettings = append(safetySettings, dto.GeminiChatSafetySettings{ + Category: category, + Threshold: model_setting.GetGeminiSafetySetting(category), + }) + } + geminiRequest.SafetySettings = safetySettings + + // openaiContent.FuncToToolCalls() + if textRequest.Tools != nil { + functions := make([]dto.FunctionRequest, 0, len(textRequest.Tools)) + googleSearch := false + codeExecution := false + urlContext := false + for _, tool := range textRequest.Tools { + if tool.Function.Name == "googleSearch" { + googleSearch = true + continue + } + if tool.Function.Name == "codeExecution" { + codeExecution = true + continue + } + if tool.Function.Name == "urlContext" { + urlContext = true + continue + } + if tool.Function.Parameters != nil { + + params, ok := tool.Function.Parameters.(map[string]interface{}) + if ok { + if props, hasProps := params["properties"].(map[string]interface{}); hasProps { + if len(props) == 0 { + tool.Function.Parameters = nil + } + } + } + } + // Clean the parameters before appending + cleanedParams := cleanFunctionParameters(tool.Function.Parameters) + tool.Function.Parameters = cleanedParams + functions = append(functions, tool.Function) + } + geminiTools := geminiRequest.GetTools() + if codeExecution { + geminiTools = append(geminiTools, dto.GeminiChatTool{ + CodeExecution: make(map[string]string), + }) + } + if googleSearch { + geminiTools = append(geminiTools, dto.GeminiChatTool{ + GoogleSearch: make(map[string]string), + }) + } + if urlContext { + geminiTools = append(geminiTools, dto.GeminiChatTool{ + URLContext: make(map[string]string), + }) + } + if len(functions) > 0 { + geminiTools = append(geminiTools, dto.GeminiChatTool{ + FunctionDeclarations: functions, + }) + } + geminiRequest.SetTools(geminiTools) + + // [NEW] Convert OpenAI tool_choice to Gemini toolConfig.functionCallingConfig + // Mapping: "auto" -> "AUTO", "none" -> "NONE", "required" -> "ANY" + // Object format: {"type": "function", "function": {"name": "xxx"}} -> "ANY" + allowedFunctionNames + if textRequest.ToolChoice != nil { + geminiRequest.ToolConfig = convertToolChoiceToGeminiConfig(textRequest.ToolChoice) + } + } + + if textRequest.ResponseFormat != nil && (textRequest.ResponseFormat.Type == "json_schema" || textRequest.ResponseFormat.Type == "json_object") { + geminiRequest.GenerationConfig.ResponseMimeType = "application/json" + + if len(textRequest.ResponseFormat.JsonSchema) > 0 { + // 先将json.RawMessage解析 + var jsonSchema dto.FormatJsonSchema + if err := common.Unmarshal(textRequest.ResponseFormat.JsonSchema, &jsonSchema); err == nil { + cleanedSchema := removeAdditionalPropertiesWithDepth(jsonSchema.Schema, 0) + geminiRequest.GenerationConfig.ResponseSchema = cleanedSchema + } + } + } + tool_call_ids := make(map[string]string) + var system_content []string + //shouldAddDummyModelMessage := false + for _, message := range textRequest.Messages { + if message.Role == "system" || message.Role == "developer" { + system_content = append(system_content, message.StringContent()) + continue + } else if message.Role == "tool" || message.Role == "function" { + if len(geminiRequest.Contents) == 0 || geminiRequest.Contents[len(geminiRequest.Contents)-1].Role == "model" { + geminiRequest.Contents = append(geminiRequest.Contents, dto.GeminiChatContent{ + Role: "user", + }) + } + var parts = &geminiRequest.Contents[len(geminiRequest.Contents)-1].Parts + name := "" + if message.Name != nil { + name = *message.Name + } else if val, exists := tool_call_ids[message.ToolCallId]; exists { + name = val + } + var contentMap map[string]interface{} + contentStr := message.StringContent() + + // 1. 尝试解析为 JSON 对象 + if err := json.Unmarshal([]byte(contentStr), &contentMap); err != nil { + // 2. 如果失败,尝试解析为 JSON 数组 + var contentSlice []interface{} + if err := json.Unmarshal([]byte(contentStr), &contentSlice); err == nil { + // 如果是数组,包装成对象 + contentMap = map[string]interface{}{"result": contentSlice} + } else { + // 3. 如果再次失败,作为纯文本处理 + contentMap = map[string]interface{}{"content": contentStr} + } + } + + functionResp := &dto.GeminiFunctionResponse{ + Name: name, + Response: contentMap, + } + + *parts = append(*parts, dto.GeminiPart{ + FunctionResponse: functionResp, + }) + continue + } + var parts []dto.GeminiPart + content := dto.GeminiChatContent{ + Role: message.Role, + } + shouldAttachThoughtSignature := attachThoughtSignature && (message.Role == "assistant" || message.Role == "model") + signatureAttached := false + // isToolCall := false + if message.ToolCalls != nil { + // message.Role = "model" + // isToolCall = true + for _, call := range message.ParseToolCalls() { + args := map[string]interface{}{} + if call.Function.Arguments != "" { + if json.Unmarshal([]byte(call.Function.Arguments), &args) != nil { + return nil, fmt.Errorf("invalid arguments for function %s, args: %s", call.Function.Name, call.Function.Arguments) + } + } + toolCall := dto.GeminiPart{ + FunctionCall: &dto.FunctionCall{ + FunctionName: call.Function.Name, + Arguments: args, + }, + } + if shouldAttachThoughtSignature && !signatureAttached && hasFunctionCallContent(toolCall.FunctionCall) && len(toolCall.ThoughtSignature) == 0 { + toolCall.ThoughtSignature = json.RawMessage(strconv.Quote(thoughtSignatureBypassValue)) + signatureAttached = true + } + parts = append(parts, toolCall) + tool_call_ids[call.ID] = call.Function.Name + } + } + + openaiContent := message.ParseContent() + for _, part := range openaiContent { + if part.Type == dto.ContentTypeText { + if part.Text == "" { + continue + } + // check markdown image ![image](data:image/jpeg;base64,xxxxxxxxxxxx) + // 使用字符串查找而非正则,避免大文本性能问题 + text := part.Text + hasMarkdownImage := false + for { + // 快速检查是否包含 markdown 图片标记 + startIdx := strings.Index(text, "![") + if startIdx == -1 { + break + } + // 找到 ]( + bracketIdx := strings.Index(text[startIdx:], "](data:") + if bracketIdx == -1 { + break + } + bracketIdx += startIdx + // 找到闭合的 ) + closeIdx := strings.Index(text[bracketIdx+2:], ")") + if closeIdx == -1 { + break + } + closeIdx += bracketIdx + 2 + + hasMarkdownImage = true + // 添加图片前的文本 + if startIdx > 0 { + textBefore := text[:startIdx] + if textBefore != "" { + parts = append(parts, dto.GeminiPart{ + Text: textBefore, + }) + } + } + // 提取 data URL (从 "](" 后面开始,到 ")" 之前) + dataUrl := text[bracketIdx+2 : closeIdx] + format, base64String, err := service.DecodeBase64FileData(dataUrl) + if err != nil { + return nil, fmt.Errorf("decode markdown base64 image data failed: %s", err.Error()) + } + imgPart := dto.GeminiPart{ + InlineData: &dto.GeminiInlineData{ + MimeType: format, + Data: base64String, + }, + } + if shouldAttachThoughtSignature { + imgPart.ThoughtSignature = json.RawMessage(strconv.Quote(thoughtSignatureBypassValue)) + } + parts = append(parts, imgPart) + // 继续处理剩余文本 + text = text[closeIdx+1:] + } + // 添加剩余文本或原始文本(如果没有找到 markdown 图片) + if !hasMarkdownImage { + parts = append(parts, dto.GeminiPart{ + Text: part.Text, + }) + } + } else { + source := part.ToFileSource() + if source == nil { + continue + } + base64Data, mimeType, err := service.GetBase64Data(c, source, "formatting image for Gemini") + if err != nil { + return nil, fmt.Errorf("get file data from '%s' failed: %w", source.GetIdentifier(), err) + } + + // 校验 MimeType 是否在 Gemini 支持的白名单中 + if _, ok := geminiSupportedMimeTypes[strings.ToLower(mimeType)]; !ok { + return nil, fmt.Errorf("mime type is not supported by Gemini: '%s', url: '%s', supported types are: %v", mimeType, source.GetIdentifier(), getSupportedMimeTypesList()) + } + + parts = append(parts, dto.GeminiPart{ + InlineData: &dto.GeminiInlineData{ + MimeType: mimeType, + Data: base64Data, + }, + }) + } + } + + // 如果需要附加签名但还没有附加(没有 tool_calls 或 tool_calls 为空), + // 则在第一个文本 part 上附加 thoughtSignature + if shouldAttachThoughtSignature && !signatureAttached && len(parts) > 0 { + for i := range parts { + if parts[i].Text != "" { + parts[i].ThoughtSignature = json.RawMessage(strconv.Quote(thoughtSignatureBypassValue)) + break + } + } + } + + content.Parts = parts + + // there's no assistant role in gemini and API shall vomit if Role is not user or model + if content.Role == "assistant" { + content.Role = "model" + } + if len(content.Parts) > 0 { + geminiRequest.Contents = append(geminiRequest.Contents, content) + } + } + + if len(system_content) > 0 { + geminiRequest.SystemInstructions = &dto.GeminiChatContent{ + Parts: []dto.GeminiPart{ + { + Text: strings.Join(system_content, "\n"), + }, + }, + } + } + + return &geminiRequest, nil +} + +// parseStopSequences 解析停止序列,支持字符串或字符串数组 +func parseStopSequences(stop any) []string { + if stop == nil { + return nil + } + + switch v := stop.(type) { + case string: + if v != "" { + return []string{v} + } + case []string: + return v + case []interface{}: + sequences := make([]string, 0, len(v)) + for _, item := range v { + if str, ok := item.(string); ok && str != "" { + sequences = append(sequences, str) + } + } + return sequences + } + return nil +} + +func hasFunctionCallContent(call *dto.FunctionCall) bool { + if call == nil { + return false + } + if strings.TrimSpace(call.FunctionName) != "" { + return true + } + + switch v := call.Arguments.(type) { + case nil: + return false + case string: + return strings.TrimSpace(v) != "" + case map[string]interface{}: + return len(v) > 0 + case []interface{}: + return len(v) > 0 + default: + return true + } +} + +// Helper function to get a list of supported MIME types for error messages +func getSupportedMimeTypesList() []string { + keys := make([]string, 0, len(geminiSupportedMimeTypes)) + for k := range geminiSupportedMimeTypes { + keys = append(keys, k) + } + return keys +} + +var geminiOpenAPISchemaAllowedFields = map[string]struct{}{ + "anyOf": {}, + "default": {}, + "description": {}, + "enum": {}, + "example": {}, + "format": {}, + "items": {}, + "maxItems": {}, + "maxLength": {}, + "maxProperties": {}, + "maximum": {}, + "minItems": {}, + "minLength": {}, + "minProperties": {}, + "minimum": {}, + "nullable": {}, + "pattern": {}, + "properties": {}, + "propertyOrdering": {}, + "required": {}, + "title": {}, + "type": {}, +} + +const geminiFunctionSchemaMaxDepth = 64 + +// cleanFunctionParameters recursively removes unsupported fields from Gemini function parameters. +func cleanFunctionParameters(params interface{}) interface{} { + return cleanFunctionParametersWithDepth(params, 0) +} + +func cleanFunctionParametersWithDepth(params interface{}, depth int) interface{} { + if params == nil { + return nil + } + + if depth >= geminiFunctionSchemaMaxDepth { + return cleanFunctionParametersShallow(params) + } + + switch v := params.(type) { + case map[string]interface{}: + // Keep only Gemini-supported OpenAPI schema subset fields (per official SDK Schema). + cleanedMap := make(map[string]interface{}, len(v)) + for k, val := range v { + if _, ok := geminiOpenAPISchemaAllowedFields[k]; ok { + cleanedMap[k] = val + } + } + + normalizeGeminiSchemaTypeAndNullable(cleanedMap) + + // Clean properties + if props, ok := cleanedMap["properties"].(map[string]interface{}); ok && props != nil { + cleanedProps := make(map[string]interface{}) + for propName, propValue := range props { + cleanedProps[propName] = cleanFunctionParametersWithDepth(propValue, depth+1) + } + cleanedMap["properties"] = cleanedProps + } + + // Recursively clean items in arrays + if items, ok := cleanedMap["items"].(map[string]interface{}); ok && items != nil { + cleanedMap["items"] = cleanFunctionParametersWithDepth(items, depth+1) + } + // OpenAPI tuple-style items is not supported by Gemini SDK Schema; keep first to avoid API rejection. + if itemsArray, ok := cleanedMap["items"].([]interface{}); ok && len(itemsArray) > 0 { + cleanedMap["items"] = cleanFunctionParametersWithDepth(itemsArray[0], depth+1) + } + + // Recursively clean anyOf + if nested, ok := cleanedMap["anyOf"].([]interface{}); ok && nested != nil { + cleanedNested := make([]interface{}, len(nested)) + for i, item := range nested { + cleanedNested[i] = cleanFunctionParametersWithDepth(item, depth+1) + } + cleanedMap["anyOf"] = cleanedNested + } + + return cleanedMap + + case []interface{}: + // Handle arrays of schemas + cleanedArray := make([]interface{}, len(v)) + for i, item := range v { + cleanedArray[i] = cleanFunctionParametersWithDepth(item, depth+1) + } + return cleanedArray + + default: + // Not a map or array, return as is (e.g., could be a primitive) + return params + } +} + +func cleanFunctionParametersShallow(params interface{}) interface{} { + switch v := params.(type) { + case map[string]interface{}: + cleanedMap := make(map[string]interface{}, len(v)) + for k, val := range v { + if _, ok := geminiOpenAPISchemaAllowedFields[k]; ok { + cleanedMap[k] = val + } + } + normalizeGeminiSchemaTypeAndNullable(cleanedMap) + // Stop recursion and avoid retaining huge nested structures. + delete(cleanedMap, "properties") + delete(cleanedMap, "items") + delete(cleanedMap, "anyOf") + return cleanedMap + case []interface{}: + // Prefer an empty list over deep recursion on attacker-controlled inputs. + return []interface{}{} + default: + return params + } +} + +func normalizeGeminiSchemaTypeAndNullable(schema map[string]interface{}) { + rawType, ok := schema["type"] + if !ok || rawType == nil { + return + } + + normalize := func(t string) (string, bool) { + switch strings.ToLower(strings.TrimSpace(t)) { + case "object": + return "OBJECT", false + case "array": + return "ARRAY", false + case "string": + return "STRING", false + case "integer": + return "INTEGER", false + case "number": + return "NUMBER", false + case "boolean": + return "BOOLEAN", false + case "null": + return "", true + default: + return t, false + } + } + + switch t := rawType.(type) { + case string: + normalized, isNull := normalize(t) + if isNull { + schema["nullable"] = true + delete(schema, "type") + return + } + schema["type"] = normalized + case []interface{}: + nullable := false + var chosen string + for _, item := range t { + if s, ok := item.(string); ok { + normalized, isNull := normalize(s) + if isNull { + nullable = true + continue + } + if chosen == "" { + chosen = normalized + } + } + } + if nullable { + schema["nullable"] = true + } + if chosen != "" { + schema["type"] = chosen + } else { + delete(schema, "type") + } + } +} + +func removeAdditionalPropertiesWithDepth(schema interface{}, depth int) interface{} { + if depth >= 5 { + return schema + } + + v, ok := schema.(map[string]interface{}) + if !ok || len(v) == 0 { + return schema + } + // 删除所有的title字段 + delete(v, "title") + delete(v, "$schema") + // 如果type不为object和array,则直接返回 + if typeVal, exists := v["type"]; !exists || (typeVal != "object" && typeVal != "array") { + return schema + } + switch v["type"] { + case "object": + delete(v, "additionalProperties") + // 处理 properties + if properties, ok := v["properties"].(map[string]interface{}); ok { + for key, value := range properties { + properties[key] = removeAdditionalPropertiesWithDepth(value, depth+1) + } + } + for _, field := range []string{"allOf", "anyOf", "oneOf"} { + if nested, ok := v[field].([]interface{}); ok { + for i, item := range nested { + nested[i] = removeAdditionalPropertiesWithDepth(item, depth+1) + } + } + } + case "array": + if items, ok := v["items"].(map[string]interface{}); ok { + v["items"] = removeAdditionalPropertiesWithDepth(items, depth+1) + } + } + + return v +} + +func unescapeString(s string) (string, error) { + var result []rune + escaped := false + i := 0 + + for i < len(s) { + r, size := utf8.DecodeRuneInString(s[i:]) // 正确解码UTF-8字符 + if r == utf8.RuneError { + return "", fmt.Errorf("invalid UTF-8 encoding") + } + + if escaped { + // 如果是转义符后的字符,检查其类型 + switch r { + case '"': + result = append(result, '"') + case '\\': + result = append(result, '\\') + case '/': + result = append(result, '/') + case 'b': + result = append(result, '\b') + case 'f': + result = append(result, '\f') + case 'n': + result = append(result, '\n') + case 'r': + result = append(result, '\r') + case 't': + result = append(result, '\t') + case '\'': + result = append(result, '\'') + default: + // 如果遇到一个非法的转义字符,直接按原样输出 + result = append(result, '\\', r) + } + escaped = false + } else { + if r == '\\' { + escaped = true // 记录反斜杠作为转义符 + } else { + result = append(result, r) + } + } + i += size // 移动到下一个字符 + } + + return string(result), nil +} +func unescapeMapOrSlice(data interface{}) interface{} { + switch v := data.(type) { + case map[string]interface{}: + for k, val := range v { + v[k] = unescapeMapOrSlice(val) + } + case []interface{}: + for i, val := range v { + v[i] = unescapeMapOrSlice(val) + } + case string: + if unescaped, err := unescapeString(v); err != nil { + return v + } else { + return unescaped + } + } + return data +} + +func getResponseToolCall(item *dto.GeminiPart) *dto.ToolCallResponse { + var argsBytes []byte + var err error + // 移除 unescapeMapOrSlice 调用,直接使用 json.Marshal + // JSON 序列化/反序列化已经正确处理了转义字符 + argsBytes, err = json.Marshal(item.FunctionCall.Arguments) + + if err != nil { + return nil + } + return &dto.ToolCallResponse{ + ID: fmt.Sprintf("call_%s", common.GetUUID()), + Type: "function", + Function: dto.FunctionResponse{ + Arguments: string(argsBytes), + Name: item.FunctionCall.FunctionName, + }, + } +} + +func buildUsageFromGeminiMetadata(metadata dto.GeminiUsageMetadata, fallbackPromptTokens int) dto.Usage { + promptTokens := metadata.PromptTokenCount + metadata.ToolUsePromptTokenCount + if promptTokens <= 0 && fallbackPromptTokens > 0 { + promptTokens = fallbackPromptTokens + } + + usage := dto.Usage{ + PromptTokens: promptTokens, + CompletionTokens: metadata.CandidatesTokenCount + metadata.ThoughtsTokenCount, + TotalTokens: metadata.TotalTokenCount, + } + usage.CompletionTokenDetails.ReasoningTokens = metadata.ThoughtsTokenCount + usage.PromptTokensDetails.CachedTokens = metadata.CachedContentTokenCount + + for _, detail := range metadata.PromptTokensDetails { + if detail.Modality == "AUDIO" { + usage.PromptTokensDetails.AudioTokens += detail.TokenCount + } else if detail.Modality == "TEXT" { + usage.PromptTokensDetails.TextTokens += detail.TokenCount + } + } + for _, detail := range metadata.ToolUsePromptTokensDetails { + if detail.Modality == "AUDIO" { + usage.PromptTokensDetails.AudioTokens += detail.TokenCount + } else if detail.Modality == "TEXT" { + usage.PromptTokensDetails.TextTokens += detail.TokenCount + } + } + for _, detail := range metadata.CandidatesTokensDetails { + switch detail.Modality { + case "IMAGE": + usage.CompletionTokenDetails.ImageTokens += detail.TokenCount + case "AUDIO": + usage.CompletionTokenDetails.AudioTokens += detail.TokenCount + case "TEXT": + usage.CompletionTokenDetails.TextTokens += detail.TokenCount + } + } + + if usage.TotalTokens > 0 && usage.CompletionTokens <= 0 { + usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens + } + + if usage.PromptTokens > 0 && usage.PromptTokensDetails.TextTokens == 0 && usage.PromptTokensDetails.AudioTokens == 0 { + usage.PromptTokensDetails.TextTokens = usage.PromptTokens + } + + return usage +} + +func responseGeminiChat2OpenAI(c *gin.Context, response *dto.GeminiChatResponse) *dto.OpenAITextResponse { + fullTextResponse := dto.OpenAITextResponse{ + Id: helper.GetResponseID(c), + Object: "chat.completion", + Created: common.GetTimestamp(), + Choices: make([]dto.OpenAITextResponseChoice, 0, len(response.Candidates)), + } + isToolCall := false + for _, candidate := range response.Candidates { + choice := dto.OpenAITextResponseChoice{ + Index: int(candidate.Index), + Message: dto.Message{ + Role: "assistant", + Content: "", + }, + FinishReason: constant.FinishReasonStop, + } + if len(candidate.Content.Parts) > 0 { + var texts []string + var toolCalls []dto.ToolCallResponse + for _, part := range candidate.Content.Parts { + if part.InlineData != nil { + // 媒体内容 + if strings.HasPrefix(part.InlineData.MimeType, "image") { + imgText := "![image](data:" + part.InlineData.MimeType + ";base64," + part.InlineData.Data + ")" + texts = append(texts, imgText) + } else { + // 其他媒体类型,直接显示链接 + texts = append(texts, fmt.Sprintf("[media](data:%s;base64,%s)", part.InlineData.MimeType, part.InlineData.Data)) + } + } else if part.FunctionCall != nil { + choice.FinishReason = constant.FinishReasonToolCalls + if call := getResponseToolCall(&part); call != nil { + toolCalls = append(toolCalls, *call) + } + } else if part.Thought { + choice.Message.ReasoningContent = &part.Text + } else { + if part.ExecutableCode != nil { + texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```") + } else if part.CodeExecutionResult != nil { + texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```") + } else { + // 过滤掉空行 + if part.Text != "\n" { + texts = append(texts, part.Text) + } + } + } + } + if len(toolCalls) > 0 { + choice.Message.SetToolCalls(toolCalls) + isToolCall = true + } + choice.Message.SetStringContent(strings.Join(texts, "\n")) + + } + if candidate.FinishReason != nil { + switch *candidate.FinishReason { + case "STOP": + choice.FinishReason = constant.FinishReasonStop + case "MAX_TOKENS": + choice.FinishReason = constant.FinishReasonLength + case "SAFETY": + // Safety filter triggered + choice.FinishReason = constant.FinishReasonContentFilter + case "RECITATION": + // Recitation (citation) detected + choice.FinishReason = constant.FinishReasonContentFilter + case "BLOCKLIST": + // Blocklist triggered + choice.FinishReason = constant.FinishReasonContentFilter + case "PROHIBITED_CONTENT": + // Prohibited content detected + choice.FinishReason = constant.FinishReasonContentFilter + case "SPII": + // Sensitive personally identifiable information + choice.FinishReason = constant.FinishReasonContentFilter + case "OTHER": + // Other reasons + choice.FinishReason = constant.FinishReasonContentFilter + default: + choice.FinishReason = constant.FinishReasonContentFilter + } + } + if isToolCall { + choice.FinishReason = constant.FinishReasonToolCalls + } + + fullTextResponse.Choices = append(fullTextResponse.Choices, choice) + } + return &fullTextResponse +} + +func streamResponseGeminiChat2OpenAI(geminiResponse *dto.GeminiChatResponse) (*dto.ChatCompletionsStreamResponse, bool) { + choices := make([]dto.ChatCompletionsStreamResponseChoice, 0, len(geminiResponse.Candidates)) + isStop := false + for _, candidate := range geminiResponse.Candidates { + if candidate.FinishReason != nil && *candidate.FinishReason == "STOP" { + isStop = true + candidate.FinishReason = nil + } + choice := dto.ChatCompletionsStreamResponseChoice{ + Index: int(candidate.Index), + Delta: dto.ChatCompletionsStreamResponseChoiceDelta{ + //Role: "assistant", + }, + } + var texts []string + isTools := false + isThought := false + if candidate.FinishReason != nil { + // Map Gemini FinishReason to OpenAI finish_reason + switch *candidate.FinishReason { + case "STOP": + // Normal completion + choice.FinishReason = &constant.FinishReasonStop + case "MAX_TOKENS": + // Reached maximum token limit + choice.FinishReason = &constant.FinishReasonLength + case "SAFETY": + // Safety filter triggered + choice.FinishReason = &constant.FinishReasonContentFilter + case "RECITATION": + // Recitation (citation) detected + choice.FinishReason = &constant.FinishReasonContentFilter + case "BLOCKLIST": + // Blocklist triggered + choice.FinishReason = &constant.FinishReasonContentFilter + case "PROHIBITED_CONTENT": + // Prohibited content detected + choice.FinishReason = &constant.FinishReasonContentFilter + case "SPII": + // Sensitive personally identifiable information + choice.FinishReason = &constant.FinishReasonContentFilter + case "OTHER": + // Other reasons + choice.FinishReason = &constant.FinishReasonContentFilter + default: + // Unknown reason, treat as content filter + choice.FinishReason = &constant.FinishReasonContentFilter + } + } + for _, part := range candidate.Content.Parts { + if part.InlineData != nil { + if strings.HasPrefix(part.InlineData.MimeType, "image") { + imgText := "![image](data:" + part.InlineData.MimeType + ";base64," + part.InlineData.Data + ")" + texts = append(texts, imgText) + } + } else if part.FunctionCall != nil { + isTools = true + if call := getResponseToolCall(&part); call != nil { + call.SetIndex(len(choice.Delta.ToolCalls)) + choice.Delta.ToolCalls = append(choice.Delta.ToolCalls, *call) + } + + } else if part.Thought { + isThought = true + texts = append(texts, part.Text) + } else { + if part.ExecutableCode != nil { + texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```\n") + } else if part.CodeExecutionResult != nil { + texts = append(texts, "```output\n"+part.CodeExecutionResult.Output+"\n```\n") + } else { + if part.Text != "\n" { + texts = append(texts, part.Text) + } + } + } + } + if isThought { + choice.Delta.SetReasoningContent(strings.Join(texts, "\n")) + } else { + choice.Delta.SetContentString(strings.Join(texts, "\n")) + } + if isTools { + choice.FinishReason = &constant.FinishReasonToolCalls + } + choices = append(choices, choice) + } + + var response dto.ChatCompletionsStreamResponse + response.Object = "chat.completion.chunk" + response.Choices = choices + return &response, isStop +} + +func handleStream(c *gin.Context, info *relaycommon.RelayInfo, resp *dto.ChatCompletionsStreamResponse) error { + streamData, err := common.Marshal(resp) + if err != nil { + return fmt.Errorf("failed to marshal stream response: %w", err) + } + err = openai.HandleStreamFormat(c, info, string(streamData), info.ChannelSetting.ForceFormat, info.ChannelSetting.ThinkingToContent) + if err != nil { + return fmt.Errorf("failed to handle stream format: %w", err) + } + return nil +} + +func handleFinalStream(c *gin.Context, info *relaycommon.RelayInfo, resp *dto.ChatCompletionsStreamResponse) error { + streamData, err := common.Marshal(resp) + if err != nil { + return fmt.Errorf("failed to marshal stream response: %w", err) + } + openai.HandleFinalResponse(c, info, string(streamData), resp.Id, resp.Created, resp.Model, resp.GetSystemFingerprint(), resp.Usage, false) + return nil +} + +func geminiStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response, callback func(data string, geminiResponse *dto.GeminiChatResponse) bool) (*dto.Usage, *types.NewAPIError) { + var usage = &dto.Usage{} + var imageCount int + responseText := strings.Builder{} + + helper.StreamScannerHandler(c, resp, info, func(data string, sr *helper.StreamResult) { + var geminiResponse dto.GeminiChatResponse + if err := common.UnmarshalJsonStr(data, &geminiResponse); err != nil { + sr.Stop(fmt.Errorf("unmarshal: %w", err)) + return + } + + if len(geminiResponse.Candidates) == 0 && geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { + common.SetContextKey(c, constant.ContextKeyAdminRejectReason, fmt.Sprintf("gemini_block_reason=%s", *geminiResponse.PromptFeedback.BlockReason)) + } + + // 统计图片数量 + for _, candidate := range geminiResponse.Candidates { + for _, part := range candidate.Content.Parts { + if part.InlineData != nil && part.InlineData.MimeType != "" { + imageCount++ + } + if part.Text != "" { + responseText.WriteString(part.Text) + } + } + } + + // 更新使用量统计 + if geminiResponse.UsageMetadata.TotalTokenCount != 0 { + mappedUsage := buildUsageFromGeminiMetadata(geminiResponse.UsageMetadata, info.GetEstimatePromptTokens()) + *usage = mappedUsage + } + + if !callback(data, &geminiResponse) { + sr.Stop(fmt.Errorf("gemini callback stopped")) + } + }) + + if imageCount != 0 { + if usage.CompletionTokens == 0 { + usage.CompletionTokens = imageCount * 1400 + } + } + + if usage.CompletionTokens <= 0 { + if info.ReceivedResponseCount > 0 { + usage = service.ResponseText2Usage(c, responseText.String(), info.UpstreamModelName, info.GetEstimatePromptTokens()) + } else { + usage = &dto.Usage{} + } + } + + return usage, nil +} + +func GeminiChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*dto.Usage, *types.NewAPIError) { + id := helper.GetResponseID(c) + createAt := common.GetTimestamp() + finishReason := constant.FinishReasonStop + toolCallIndexByChoice := make(map[int]map[string]int) + nextToolCallIndexByChoice := make(map[int]int) + + usage, err := geminiStreamHandler(c, info, resp, func(data string, geminiResponse *dto.GeminiChatResponse) bool { + response, isStop := streamResponseGeminiChat2OpenAI(geminiResponse) + + response.Id = id + response.Created = createAt + response.Model = info.UpstreamModelName + for choiceIdx := range response.Choices { + choiceKey := response.Choices[choiceIdx].Index + for toolIdx := range response.Choices[choiceIdx].Delta.ToolCalls { + tool := &response.Choices[choiceIdx].Delta.ToolCalls[toolIdx] + if tool.ID == "" { + continue + } + m := toolCallIndexByChoice[choiceKey] + if m == nil { + m = make(map[string]int) + toolCallIndexByChoice[choiceKey] = m + } + if idx, ok := m[tool.ID]; ok { + tool.SetIndex(idx) + continue + } + idx := nextToolCallIndexByChoice[choiceKey] + nextToolCallIndexByChoice[choiceKey] = idx + 1 + m[tool.ID] = idx + tool.SetIndex(idx) + } + } + + logger.LogDebug(c, fmt.Sprintf("info.SendResponseCount = %d", info.SendResponseCount)) + if info.SendResponseCount == 0 { + // send first response + emptyResponse := helper.GenerateStartEmptyResponse(id, createAt, info.UpstreamModelName, nil) + if response.IsToolCall() { + if len(emptyResponse.Choices) > 0 && len(response.Choices) > 0 { + toolCalls := response.Choices[0].Delta.ToolCalls + copiedToolCalls := make([]dto.ToolCallResponse, len(toolCalls)) + for idx := range toolCalls { + copiedToolCalls[idx] = toolCalls[idx] + copiedToolCalls[idx].Function.Arguments = "" + } + emptyResponse.Choices[0].Delta.ToolCalls = copiedToolCalls + } + finishReason = constant.FinishReasonToolCalls + err := handleStream(c, info, emptyResponse) + if err != nil { + logger.LogError(c, err.Error()) + } + + response.ClearToolCalls() + if response.IsFinished() { + response.Choices[0].FinishReason = nil + } + } else { + err := handleStream(c, info, emptyResponse) + if err != nil { + logger.LogError(c, err.Error()) + } + } + } + + err := handleStream(c, info, response) + if err != nil { + logger.LogError(c, err.Error()) + } + if isStop { + _ = handleStream(c, info, helper.GenerateStopResponse(id, createAt, info.UpstreamModelName, finishReason)) + } + return true + }) + + if err != nil { + return usage, err + } + + response := helper.GenerateFinalUsageResponse(id, createAt, info.UpstreamModelName, *usage) + handleErr := handleFinalStream(c, info, response) + if handleErr != nil { + common.SysLog("send final response failed: " + handleErr.Error()) + } + return usage, nil +} + +func GeminiChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*dto.Usage, *types.NewAPIError) { + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, types.NewOpenAIError(err, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + service.CloseResponseBodyGracefully(resp) + if common.DebugEnabled { + println(string(responseBody)) + } + var geminiResponse dto.GeminiChatResponse + err = common.Unmarshal(responseBody, &geminiResponse) + if err != nil { + return nil, types.NewOpenAIError(err, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + if len(geminiResponse.Candidates) == 0 { + usage := buildUsageFromGeminiMetadata(geminiResponse.UsageMetadata, info.GetEstimatePromptTokens()) + + var newAPIError *types.NewAPIError + if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { + common.SetContextKey(c, constant.ContextKeyAdminRejectReason, fmt.Sprintf("gemini_block_reason=%s", *geminiResponse.PromptFeedback.BlockReason)) + newAPIError = types.NewOpenAIError( + errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), + types.ErrorCodePromptBlocked, + http.StatusBadRequest, + ) + } else { + common.SetContextKey(c, constant.ContextKeyAdminRejectReason, "gemini_empty_candidates") + newAPIError = types.NewOpenAIError( + errors.New("empty response from Gemini API"), + types.ErrorCodeEmptyResponse, + http.StatusInternalServerError, + ) + } + + service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping")) + + switch info.RelayFormat { + case types.RelayFormatClaude: + c.JSON(newAPIError.StatusCode, gin.H{ + "type": "error", + "error": newAPIError.ToClaudeError(), + }) + default: + c.JSON(newAPIError.StatusCode, gin.H{ + "error": newAPIError.ToOpenAIError(), + }) + } + return &usage, nil + } + fullTextResponse := responseGeminiChat2OpenAI(c, &geminiResponse) + fullTextResponse.Model = info.UpstreamModelName + usage := buildUsageFromGeminiMetadata(geminiResponse.UsageMetadata, info.GetEstimatePromptTokens()) + + fullTextResponse.Usage = usage + + switch info.RelayFormat { + case types.RelayFormatOpenAI: + responseBody, err = common.Marshal(fullTextResponse) + if err != nil { + return nil, types.NewError(err, types.ErrorCodeBadResponseBody) + } + case types.RelayFormatClaude: + claudeResp := service.ResponseOpenAI2Claude(fullTextResponse, info) + claudeRespStr, err := common.Marshal(claudeResp) + if err != nil { + return nil, types.NewError(err, types.ErrorCodeBadResponseBody) + } + responseBody = claudeRespStr + case types.RelayFormatGemini: + break + } + + service.IOCopyBytesGracefully(c, resp, responseBody) + + return &usage, nil +} + +func GeminiEmbeddingHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*dto.Usage, *types.NewAPIError) { + defer service.CloseResponseBodyGracefully(resp) + + responseBody, readErr := io.ReadAll(resp.Body) + if readErr != nil { + return nil, types.NewOpenAIError(readErr, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + + var geminiResponse dto.GeminiBatchEmbeddingResponse + if jsonErr := common.Unmarshal(responseBody, &geminiResponse); jsonErr != nil { + return nil, types.NewOpenAIError(jsonErr, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + + // convert to openai format response + openAIResponse := dto.OpenAIEmbeddingResponse{ + Object: "list", + Data: make([]dto.OpenAIEmbeddingResponseItem, 0, len(geminiResponse.Embeddings)), + Model: info.UpstreamModelName, + } + + for i, embedding := range geminiResponse.Embeddings { + openAIResponse.Data = append(openAIResponse.Data, dto.OpenAIEmbeddingResponseItem{ + Object: "embedding", + Embedding: embedding.Values, + Index: i, + }) + } + + // calculate usage + // https://ai.google.dev/gemini-api/docs/pricing?hl=zh-cn#text-embedding-004 + // Google has not yet clarified how embedding models will be billed + // refer to openai billing method to use input tokens billing + // https://platform.openai.com/docs/guides/embeddings#what-are-embeddings + usage := service.ResponseText2Usage(c, "", info.UpstreamModelName, info.GetEstimatePromptTokens()) + openAIResponse.Usage = *usage + + jsonResponse, jsonErr := common.Marshal(openAIResponse) + if jsonErr != nil { + return nil, types.NewOpenAIError(jsonErr, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + + service.IOCopyBytesGracefully(c, resp, jsonResponse) + return usage, nil +} + +func GeminiImageHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*dto.Usage, *types.NewAPIError) { + responseBody, readErr := io.ReadAll(resp.Body) + if readErr != nil { + return nil, types.NewOpenAIError(readErr, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + _ = resp.Body.Close() + + var geminiResponse dto.GeminiImageResponse + if jsonErr := common.Unmarshal(responseBody, &geminiResponse); jsonErr != nil { + return nil, types.NewOpenAIError(jsonErr, types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + + if len(geminiResponse.Predictions) == 0 { + return nil, types.NewOpenAIError(errors.New("no images generated"), types.ErrorCodeBadResponseBody, http.StatusInternalServerError) + } + + // convert to openai format response + openAIResponse := dto.ImageResponse{ + Created: common.GetTimestamp(), + Data: make([]dto.ImageData, 0, len(geminiResponse.Predictions)), + } + + for _, prediction := range geminiResponse.Predictions { + if prediction.RaiFilteredReason != "" { + continue // skip filtered image + } + openAIResponse.Data = append(openAIResponse.Data, dto.ImageData{ + B64Json: prediction.BytesBase64Encoded, + }) + } + + jsonResponse, jsonErr := json.Marshal(openAIResponse) + if jsonErr != nil { + return nil, types.NewError(jsonErr, types.ErrorCodeBadResponseBody) + } + + c.Writer.Header().Set("Content-Type", "application/json") + c.Writer.WriteHeader(resp.StatusCode) + _, _ = c.Writer.Write(jsonResponse) + + // https://github.com/google-gemini/cookbook/blob/719a27d752aac33f39de18a8d3cb42a70874917e/quickstarts/Counting_Tokens.ipynb + // each image has fixed 258 tokens + const imageTokens = 258 + generatedImages := len(openAIResponse.Data) + + usage := &dto.Usage{ + PromptTokens: imageTokens * generatedImages, // each generated image has fixed 258 tokens + CompletionTokens: 0, // image generation does not calculate completion tokens + TotalTokens: imageTokens * generatedImages, + } + + return usage, nil +} + +type GeminiModelsResponse struct { + Models []dto.GeminiModel `json:"models"` + NextPageToken string `json:"nextPageToken"` +} + +func FetchGeminiModels(baseURL, apiKey, proxyURL string) ([]string, error) { + client, err := service.GetHttpClientWithProxy(proxyURL) + if err != nil { + return nil, fmt.Errorf("创建HTTP客户端失败: %v", err) + } + + allModels := make([]string, 0) + nextPageToken := "" + maxPages := 100 // Safety limit to prevent infinite loops + + for page := 0; page < maxPages; page++ { + url := fmt.Sprintf("%s/v1beta/models", baseURL) + if nextPageToken != "" { + url = fmt.Sprintf("%s?pageToken=%s", url, nextPageToken) + } + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + request, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + cancel() + return nil, fmt.Errorf("创建请求失败: %v", err) + } + + request.Header.Set("x-goog-api-key", apiKey) + + response, err := client.Do(request) + if err != nil { + cancel() + return nil, fmt.Errorf("请求失败: %v", err) + } + + if response.StatusCode != http.StatusOK { + body, _ := io.ReadAll(response.Body) + response.Body.Close() + cancel() + return nil, fmt.Errorf("服务器返回错误 %d: %s", response.StatusCode, string(body)) + } + + body, err := io.ReadAll(response.Body) + response.Body.Close() + cancel() + if err != nil { + return nil, fmt.Errorf("读取响应失败: %v", err) + } + + var modelsResponse GeminiModelsResponse + if err = common.Unmarshal(body, &modelsResponse); err != nil { + return nil, fmt.Errorf("解析响应失败: %v", err) + } + + for _, model := range modelsResponse.Models { + modelNameValue, ok := model.Name.(string) + if !ok { + continue + } + modelName := strings.TrimPrefix(modelNameValue, "models/") + allModels = append(allModels, modelName) + } + + nextPageToken = modelsResponse.NextPageToken + if nextPageToken == "" { + break + } + } + + return allModels, nil +} + +// convertToolChoiceToGeminiConfig converts OpenAI tool_choice to Gemini toolConfig +// OpenAI tool_choice values: +// - "auto": Let the model decide (default) +// - "none": Don't call any tools +// - "required": Must call at least one tool +// - {"type": "function", "function": {"name": "xxx"}}: Call specific function +// +// Gemini functionCallingConfig.mode values: +// - "AUTO": Model decides whether to call functions +// - "NONE": Model won't call functions +// - "ANY": Model must call at least one function +func convertToolChoiceToGeminiConfig(toolChoice any) *dto.ToolConfig { + if toolChoice == nil { + return nil + } + + // Handle string values: "auto", "none", "required" + if toolChoiceStr, ok := toolChoice.(string); ok { + config := &dto.ToolConfig{ + FunctionCallingConfig: &dto.FunctionCallingConfig{}, + } + switch toolChoiceStr { + case "auto": + config.FunctionCallingConfig.Mode = "AUTO" + case "none": + config.FunctionCallingConfig.Mode = "NONE" + case "required": + config.FunctionCallingConfig.Mode = "ANY" + default: + // Unknown string value, default to AUTO + config.FunctionCallingConfig.Mode = "AUTO" + } + return config + } + + // Handle object value: {"type": "function", "function": {"name": "xxx"}} + if toolChoiceMap, ok := toolChoice.(map[string]interface{}); ok { + if toolChoiceMap["type"] == "function" { + config := &dto.ToolConfig{ + FunctionCallingConfig: &dto.FunctionCallingConfig{ + Mode: "ANY", + }, + } + // Extract function name if specified + if function, ok := toolChoiceMap["function"].(map[string]interface{}); ok { + if name, ok := function["name"].(string); ok && name != "" { + config.FunctionCallingConfig.AllowedFunctionNames = []string{name} + } + } + return config + } + // Unsupported map structure (type is not "function"), return nil + return nil + } + + // Unsupported type, return nil + return nil +} diff --git a/_all-in-one-upload/new-api/relay/channel/openai/constant.go b/_all-in-one-upload/new-api/relay/channel/openai/constant.go new file mode 100644 index 0000000..14e3d44 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/openai/constant.go @@ -0,0 +1,76 @@ +package openai + +var ModelList = []string{ + "gpt-3.5-turbo", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-1106", "gpt-3.5-turbo-0125", + "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", + "gpt-3.5-turbo-instruct", "gpt-3.5-turbo-instruct-0914", + "gpt-4", "gpt-4-0613", "gpt-4-1106-preview", "gpt-4-0125-preview", + "gpt-4-32k", "gpt-4-32k-0613", + "gpt-4-turbo-preview", "gpt-4-turbo", "gpt-4-turbo-2024-04-09", + "gpt-4-vision-preview", + "chatgpt-4o-latest", + "gpt-4o", "gpt-4o-2024-05-13", "gpt-4o-2024-08-06", "gpt-4o-2024-11-20", + "gpt-4o-transcribe", "gpt-4o-transcribe-diarize", + "gpt-4o-search-preview", "gpt-4o-search-preview-2025-03-11", + "gpt-4o-mini", "gpt-4o-mini-2024-07-18", + "gpt-4o-mini-transcribe", "gpt-4o-mini-transcribe-2025-03-20", "gpt-4o-mini-transcribe-2025-12-15", + "gpt-4o-mini-tts", "gpt-4o-mini-tts-2025-03-20", "gpt-4o-mini-tts-2025-12-15", + "gpt-4o-mini-search-preview", "gpt-4o-mini-search-preview-2025-03-11", + "gpt-4.5-preview", "gpt-4.5-preview-2025-02-27", + "gpt-4.1", "gpt-4.1-2025-04-14", + "gpt-4.1-mini", "gpt-4.1-mini-2025-04-14", + "gpt-4.1-nano", "gpt-4.1-nano-2025-04-14", + "o1", "o1-2024-12-17", + "o1-preview", "o1-preview-2024-09-12", + "o1-mini", "o1-mini-2024-09-12", + "o1-pro", "o1-pro-2025-03-19", + "o3-mini", "o3-mini-2025-01-31", + "o3-mini-high", "o3-mini-2025-01-31-high", + "o3-mini-low", "o3-mini-2025-01-31-low", + "o3-mini-medium", "o3-mini-2025-01-31-medium", + "o3", "o3-2025-04-16", + "o3-pro", "o3-pro-2025-06-10", + "o3-deep-research", "o3-deep-research-2025-06-26", + "o4-mini", "o4-mini-2025-04-16", + "o4-mini-deep-research", "o4-mini-deep-research-2025-06-26", + "gpt-5", "gpt-5-2025-08-07", "gpt-5-chat-latest", + "gpt-5-mini", "gpt-5-mini-2025-08-07", + "gpt-5-nano", "gpt-5-nano-2025-08-07", + "gpt-5-codex", + "gpt-5-pro", "gpt-5-pro-2025-10-06", + "gpt-5-search-api", "gpt-5-search-api-2025-10-14", + "gpt-5.1", "gpt-5.1-2025-11-13", "gpt-5.1-chat-latest", + "gpt-5.1-codex", "gpt-5.1-codex-mini", "gpt-5.1-codex-max", + "gpt-5.2", "gpt-5.2-2025-12-11", "gpt-5.2-chat-latest", + "gpt-5.2-pro", "gpt-5.2-pro-2025-12-11", + "gpt-5.2-codex", + "gpt-5.3-chat-latest", + "gpt-5.3-codex", + "gpt-5.4", "gpt-5.4-2026-03-05", + "gpt-5.4-pro", "gpt-5.4-pro-2026-03-05", + "gpt-4o-audio-preview", "gpt-4o-audio-preview-2024-10-01", "gpt-4o-audio-preview-2024-12-17", "gpt-4o-audio-preview-2025-06-03", + "gpt-4o-realtime-preview", "gpt-4o-realtime-preview-2024-10-01", "gpt-4o-realtime-preview-2024-12-17", "gpt-4o-realtime-preview-2025-06-03", + "gpt-4o-mini-realtime-preview", "gpt-4o-mini-realtime-preview-2024-12-17", + "gpt-4o-mini-audio-preview", "gpt-4o-mini-audio-preview-2024-12-17", + "gpt-audio", "gpt-audio-2025-08-28", + "gpt-audio-mini", "gpt-audio-mini-2025-10-06", "gpt-audio-mini-2025-12-15", + "gpt-audio-1.5", + "gpt-realtime", "gpt-realtime-2025-08-28", + "gpt-realtime-mini", "gpt-realtime-mini-2025-10-06", "gpt-realtime-mini-2025-12-15", + "gpt-realtime-1.5", + "text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large", + "text-curie-001", "text-babbage-001", "text-ada-001", + "text-moderation-latest", "text-moderation-stable", + "omni-moderation-latest", "omni-moderation-2024-09-26", + "text-davinci-edit-001", + "davinci-002", "babbage-002", + "dall-e-2", "dall-e-3", + "gpt-image-1", "gpt-image-1-mini", "gpt-image-1.5", + "chatgpt-image-latest", + "whisper-1", + "tts-1", "tts-1-1106", "tts-1-hd", "tts-1-hd-1106", + "computer-use-preview", "computer-use-preview-2025-03-11", + "sora-2", "sora-2-pro", +} + +var ChannelName = "openai" diff --git a/_all-in-one-upload/new-api/relay/channel/replicate/dto.go b/_all-in-one-upload/new-api/relay/channel/replicate/dto.go new file mode 100644 index 0000000..2ff06dc --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/replicate/dto.go @@ -0,0 +1,19 @@ +package replicate + +type PredictionResponse struct { + Status string `json:"status"` + Output any `json:"output"` + Error *PredictionError `json:"error"` +} + +type PredictionError struct { + Code string `json:"code"` + Message string `json:"message"` + Detail string `json:"detail"` +} + +type FileUploadResponse struct { + Urls struct { + Get string `json:"get"` + } `json:"urls"` +} diff --git a/_all-in-one-upload/new-api/relay/channel/task/hailuo/adaptor.go b/_all-in-one-upload/new-api/relay/channel/task/hailuo/adaptor.go new file mode 100644 index 0000000..28b3a97 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/task/hailuo/adaptor.go @@ -0,0 +1,302 @@ +package hailuo + +import ( + "bytes" + "fmt" + "io" + "net/http" + "strconv" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/model" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/relay/channel" + taskcommon "github.com/QuantumNous/new-api/relay/channel/task/taskcommon" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/service" +) + +// https://platform.minimaxi.com/docs/api-reference/video-generation-intro +type TaskAdaptor struct { + taskcommon.BaseBilling + ChannelType int + apiKey string + baseURL string +} + +func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { + a.ChannelType = info.ChannelType + a.baseURL = info.ChannelBaseUrl + a.apiKey = info.ApiKey +} + +func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) { + return relaycommon.ValidateBasicTaskRequest(c, info, constant.TaskActionGenerate) +} + +func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) { + return fmt.Sprintf("%s%s", a.baseURL, TextToVideoEndpoint), nil +} + +func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error { + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", "Bearer "+a.apiKey) + return nil +} + +func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) { + v, exists := c.Get("task_request") + if !exists { + return nil, fmt.Errorf("request not found in context") + } + req, ok := v.(relaycommon.TaskSubmitReq) + if !ok { + return nil, fmt.Errorf("invalid request type in context") + } + + body, err := a.convertToRequestPayload(&req, info) + if err != nil { + return nil, errors.Wrap(err, "convert request payload failed") + } + + data, err := common.Marshal(body) + if err != nil { + return nil, err + } + + return bytes.NewReader(data), nil +} + +func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) { + return channel.DoTaskApiRequest(a, c, info, requestBody) +} + +func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) { + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + taskErr = service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) + return + } + _ = resp.Body.Close() + + var hResp VideoResponse + if err := common.Unmarshal(responseBody, &hResp); err != nil { + taskErr = service.TaskErrorWrapper(errors.Wrapf(err, "body: %s", responseBody), "unmarshal_response_body_failed", http.StatusInternalServerError) + return + } + + if hResp.BaseResp.StatusCode != StatusSuccess { + taskErr = service.TaskErrorWrapper( + fmt.Errorf("hailuo api error: %s", hResp.BaseResp.StatusMsg), + strconv.Itoa(hResp.BaseResp.StatusCode), + http.StatusBadRequest, + ) + return + } + + ov := dto.NewOpenAIVideo() + ov.ID = info.PublicTaskID + ov.TaskID = info.PublicTaskID + ov.CreatedAt = time.Now().Unix() + ov.Model = info.OriginModelName + + c.JSON(http.StatusOK, ov) + return hResp.TaskID, responseBody, nil +} + +func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any, proxy string) (*http.Response, error) { + taskID, ok := body["task_id"].(string) + if !ok { + return nil, fmt.Errorf("invalid task_id") + } + + uri := fmt.Sprintf("%s%s?task_id=%s", baseUrl, QueryTaskEndpoint, taskID) + + req, err := http.NewRequest(http.MethodGet, uri, nil) + if err != nil { + return nil, err + } + + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", "Bearer "+key) + + client, err := service.GetHttpClientWithProxy(proxy) + if err != nil { + return nil, fmt.Errorf("new proxy http client failed: %w", err) + } + return client.Do(req) +} + +func (a *TaskAdaptor) GetModelList() []string { + return ModelList +} + +func (a *TaskAdaptor) GetChannelName() string { + return ChannelName +} + +func (a *TaskAdaptor) convertToRequestPayload(req *relaycommon.TaskSubmitReq, info *relaycommon.RelayInfo) (*VideoRequest, error) { + modelConfig := GetModelConfig(info.UpstreamModelName) + duration := DefaultDuration + if req.Duration > 0 { + duration = req.Duration + } + resolution := modelConfig.DefaultResolution + if req.Size != "" { + resolution = a.parseResolutionFromSize(req.Size, modelConfig) + } + + videoRequest := &VideoRequest{ + Model: info.UpstreamModelName, + Prompt: req.Prompt, + Duration: &duration, + Resolution: resolution, + } + if err := req.UnmarshalMetadata(&videoRequest); err != nil { + return nil, errors.Wrap(err, "unmarshal metadata to video request failed") + } + + return videoRequest, nil +} + +func (a *TaskAdaptor) parseResolutionFromSize(size string, modelConfig ModelConfig) string { + switch { + case strings.Contains(size, "1080"): + return Resolution1080P + case strings.Contains(size, "768"): + return Resolution768P + case strings.Contains(size, "720"): + return Resolution720P + case strings.Contains(size, "512"): + return Resolution512P + default: + return modelConfig.DefaultResolution + } +} + +func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) { + resTask := QueryTaskResponse{} + if err := common.Unmarshal(respBody, &resTask); err != nil { + return nil, errors.Wrap(err, "unmarshal task result failed") + } + + taskResult := relaycommon.TaskInfo{} + + if resTask.BaseResp.StatusCode == StatusSuccess { + taskResult.Code = 0 + } else { + taskResult.Code = resTask.BaseResp.StatusCode + taskResult.Reason = resTask.BaseResp.StatusMsg + taskResult.Status = model.TaskStatusFailure + taskResult.Progress = "100%" + } + + switch resTask.Status { + case TaskStatusPreparing, TaskStatusQueueing, TaskStatusProcessing: + taskResult.Status = model.TaskStatusInProgress + taskResult.Progress = "30%" + if resTask.Status == TaskStatusProcessing { + taskResult.Progress = "50%" + } + case TaskStatusSuccess: + taskResult.Status = model.TaskStatusSuccess + taskResult.Progress = "100%" + taskResult.Url = a.buildVideoURL(resTask.TaskID, resTask.FileID) + case TaskStatusFailed: + taskResult.Status = model.TaskStatusFailure + taskResult.Progress = "100%" + if taskResult.Reason == "" { + taskResult.Reason = "task failed" + } + default: + taskResult.Status = model.TaskStatusInProgress + taskResult.Progress = "30%" + } + + return &taskResult, nil +} + +func (a *TaskAdaptor) ConvertToOpenAIVideo(originTask *model.Task) ([]byte, error) { + var hailuoResp QueryTaskResponse + if err := common.Unmarshal(originTask.Data, &hailuoResp); err != nil { + return nil, errors.Wrap(err, "unmarshal hailuo task data failed") + } + + openAIVideo := originTask.ToOpenAIVideo() + if hailuoResp.BaseResp.StatusCode != StatusSuccess { + openAIVideo.Error = &dto.OpenAIVideoError{ + Message: hailuoResp.BaseResp.StatusMsg, + Code: strconv.Itoa(hailuoResp.BaseResp.StatusCode), + } + } + + jsonData, err := common.Marshal(openAIVideo) + if err != nil { + return nil, errors.Wrap(err, "marshal openai video failed") + } + + return jsonData, nil +} + +func (a *TaskAdaptor) buildVideoURL(_, fileID string) string { + if a.apiKey == "" || a.baseURL == "" { + return "" + } + + url := fmt.Sprintf("%s/v1/files/retrieve?file_id=%s", a.baseURL, fileID) + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return "" + } + + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", "Bearer "+a.apiKey) + + resp, err := service.GetHttpClient().Do(req) + if err != nil { + return "" + } + defer resp.Body.Close() + + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return "" + } + + var retrieveResp RetrieveFileResponse + if err := common.Unmarshal(responseBody, &retrieveResp); err != nil { + return "" + } + + if retrieveResp.BaseResp.StatusCode != StatusSuccess { + return "" + } + + return retrieveResp.File.DownloadURL +} + +func contains(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +} + +func containsInt(slice []int, item int) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +} diff --git a/_all-in-one-upload/new-api/relay/channel/task/suno/adaptor.go b/_all-in-one-upload/new-api/relay/channel/task/suno/adaptor.go new file mode 100644 index 0000000..35b5e42 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/task/suno/adaptor.go @@ -0,0 +1,167 @@ +package suno + +import ( + "bytes" + "fmt" + "io" + "net/http" + "strings" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/relay/channel" + taskcommon "github.com/QuantumNous/new-api/relay/channel/task/taskcommon" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/service" + + "github.com/gin-gonic/gin" +) + +type TaskAdaptor struct { + taskcommon.BaseBilling + ChannelType int +} + +// ParseTaskResult is not used for Suno tasks. +// Suno polling uses a dedicated batch-fetch path (service.UpdateSunoTasks) that +// receives dto.TaskResponse[[]dto.SunoDataResponse] from the upstream /fetch API. +// This differs from the per-task polling used by video adaptors. +func (a *TaskAdaptor) ParseTaskResult([]byte) (*relaycommon.TaskInfo, error) { + return nil, fmt.Errorf("suno uses batch polling via UpdateSunoTasks, ParseTaskResult is not applicable") +} + +func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { + a.ChannelType = info.ChannelType +} + +func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) { + action := strings.ToUpper(c.Param("action")) + + var sunoRequest *dto.SunoSubmitReq + err := common.UnmarshalBodyReusable(c, &sunoRequest) + if err != nil { + taskErr = service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest) + return + } + err = actionValidate(c, sunoRequest, action) + if err != nil { + taskErr = service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest) + return + } + + //if sunoRequest.ContinueClipId != "" { + // if sunoRequest.TaskID == "" { + // taskErr = service.TaskErrorWrapperLocal(fmt.Errorf("task id is empty"), "invalid_request", http.StatusBadRequest) + // return + // } + // info.OriginTaskID = sunoRequest.TaskID + //} + + info.Action = action + c.Set("task_request", sunoRequest) + return nil +} + +func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) { + baseURL := info.ChannelBaseUrl + fullRequestURL := fmt.Sprintf("%s%s", baseURL, "/suno/submit/"+info.Action) + return fullRequestURL, nil +} + +func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error { + req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) + req.Header.Set("Accept", c.Request.Header.Get("Accept")) + req.Header.Set("Authorization", "Bearer "+info.ApiKey) + return nil +} + +func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) { + sunoRequest, ok := c.Get("task_request") + if !ok { + return nil, fmt.Errorf("task_request not found in context") + } + data, err := common.Marshal(sunoRequest) + if err != nil { + return nil, err + } + return bytes.NewReader(data), nil +} + +func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) { + return channel.DoTaskApiRequest(a, c, info, requestBody) +} + +func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) { + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + taskErr = service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) + return + } + var sunoResponse dto.TaskResponse[string] + err = common.Unmarshal(responseBody, &sunoResponse) + if err != nil { + taskErr = service.TaskErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError) + return + } + if !sunoResponse.IsSuccess() { + taskErr = service.TaskErrorWrapper(fmt.Errorf("%s", sunoResponse.Message), sunoResponse.Code, http.StatusInternalServerError) + return + } + + // 使用公开 task_xxxx ID 替换上游 ID 返回给客户端 + publicResponse := dto.TaskResponse[string]{ + Code: sunoResponse.Code, + Message: sunoResponse.Message, + Data: info.PublicTaskID, + } + c.JSON(http.StatusOK, publicResponse) + + return sunoResponse.Data, nil, nil +} + +func (a *TaskAdaptor) GetModelList() []string { + return ModelList +} + +func (a *TaskAdaptor) GetChannelName() string { + return ChannelName +} + +func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any, proxy string) (*http.Response, error) { + requestUrl := fmt.Sprintf("%s/suno/fetch", baseUrl) + byteBody, err := common.Marshal(body) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer(byteBody)) + if err != nil { + common.SysLog(fmt.Sprintf("Get Task error: %v", err)) + return nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+key) + client, err := service.GetHttpClientWithProxy(proxy) + if err != nil { + return nil, fmt.Errorf("new proxy http client failed: %w", err) + } + return client.Do(req) +} + +func actionValidate(c *gin.Context, sunoRequest *dto.SunoSubmitReq, action string) (err error) { + switch action { + case constant.SunoActionMusic: + if sunoRequest.Mv == "" { + sunoRequest.Mv = "chirp-v3-0" + } + case constant.SunoActionLyrics: + if sunoRequest.Prompt == "" { + err = fmt.Errorf("prompt_empty") + return + } + default: + err = fmt.Errorf("invalid_action") + } + return +} diff --git a/_all-in-one-upload/new-api/relay/channel/task/vertex/adaptor.go b/_all-in-one-upload/new-api/relay/channel/task/vertex/adaptor.go new file mode 100644 index 0000000..b76364e --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/task/vertex/adaptor.go @@ -0,0 +1,424 @@ +package vertex + +import ( + "bytes" + "fmt" + "io" + "net/http" + "regexp" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/model" + "github.com/gin-gonic/gin" + + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/relay/channel" + geminitask "github.com/QuantumNous/new-api/relay/channel/task/gemini" + taskcommon "github.com/QuantumNous/new-api/relay/channel/task/taskcommon" + vertexcore "github.com/QuantumNous/new-api/relay/channel/vertex" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/service" +) + +// ============================ +// Request / Response structures +// ============================ + +type fetchOperationPayload struct { + OperationName string `json:"operationName"` +} + +type submitResponse struct { + Name string `json:"name"` +} + +type operationVideo struct { + MimeType string `json:"mimeType"` + BytesBase64Encoded string `json:"bytesBase64Encoded"` + Encoding string `json:"encoding"` +} + +type operationResponse struct { + Name string `json:"name"` + Done bool `json:"done"` + Response struct { + Type string `json:"@type"` + RaiMediaFilteredCount int `json:"raiMediaFilteredCount"` + Videos []operationVideo `json:"videos"` + BytesBase64Encoded string `json:"bytesBase64Encoded"` + Encoding string `json:"encoding"` + Video string `json:"video"` + } `json:"response"` + Error struct { + Message string `json:"message"` + } `json:"error"` +} + +// ============================ +// Adaptor implementation +// ============================ + +type TaskAdaptor struct { + taskcommon.BaseBilling + ChannelType int + apiKey string + baseURL string +} + +func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { + a.ChannelType = info.ChannelType + a.baseURL = info.ChannelBaseUrl + a.apiKey = info.ApiKey +} + +// ValidateRequestAndSetAction parses body, validates fields and sets default action. +func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) { + // Use the standard validation method for TaskSubmitReq + return relaycommon.ValidateBasicTaskRequest(c, info, constant.TaskActionTextGenerate) +} + +// BuildRequestURL constructs the upstream URL. +func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) { + adc := &vertexcore.Credentials{} + if err := common.Unmarshal([]byte(a.apiKey), adc); err != nil { + return "", fmt.Errorf("failed to decode credentials: %w", err) + } + modelName := info.UpstreamModelName + if modelName == "" { + modelName = "veo-3.0-generate-001" + } + + region := vertexcore.GetModelRegion(info.ApiVersion, modelName) + if strings.TrimSpace(region) == "" { + region = "global" + } + if region == "global" { + return fmt.Sprintf( + "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:predictLongRunning", + adc.ProjectID, + modelName, + ), nil + } + return fmt.Sprintf( + "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:predictLongRunning", + region, + adc.ProjectID, + region, + modelName, + ), nil +} + +// BuildRequestHeader sets required headers. +func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error { + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + + adc := &vertexcore.Credentials{} + if err := common.Unmarshal([]byte(a.apiKey), adc); err != nil { + return fmt.Errorf("failed to decode credentials: %w", err) + } + + proxy := "" + if info != nil { + proxy = info.ChannelSetting.Proxy + } + token, err := vertexcore.AcquireAccessToken(*adc, proxy) + if err != nil { + return fmt.Errorf("failed to acquire access token: %w", err) + } + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("x-goog-user-project", adc.ProjectID) + return nil +} + +// EstimateBilling returns OtherRatios based on durationSeconds and resolution. +func (a *TaskAdaptor) EstimateBilling(c *gin.Context, info *relaycommon.RelayInfo) map[string]float64 { + v, ok := c.Get("task_request") + if !ok { + return nil + } + req := v.(relaycommon.TaskSubmitReq) + + seconds := geminitask.ResolveVeoDuration(req.Metadata, req.Duration, req.Seconds) + resolution := geminitask.ResolveVeoResolution(req.Metadata, req.Size) + resRatio := geminitask.VeoResolutionRatio(info.UpstreamModelName, resolution) + + return map[string]float64{ + "seconds": float64(seconds), + "resolution": resRatio, + } +} + +// BuildRequestBody converts request into Vertex specific format. +func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) { + v, ok := c.Get("task_request") + if !ok { + return nil, fmt.Errorf("request not found in context") + } + req := v.(relaycommon.TaskSubmitReq) + + instance := geminitask.VeoInstance{Prompt: req.Prompt} + if img := geminitask.ExtractMultipartImage(c, info); img != nil { + instance.Image = img + } else if len(req.Images) > 0 { + if parsed := geminitask.ParseImageInput(req.Images[0]); parsed != nil { + instance.Image = parsed + info.Action = constant.TaskActionGenerate + } + } + + params := &geminitask.VeoParameters{} + if err := taskcommon.UnmarshalMetadata(req.Metadata, params); err != nil { + return nil, fmt.Errorf("unmarshal metadata failed: %w", err) + } + if params.DurationSeconds == 0 && req.Duration > 0 { + params.DurationSeconds = req.Duration + } + if params.Resolution == "" && req.Size != "" { + params.Resolution = geminitask.SizeToVeoResolution(req.Size) + } + if params.AspectRatio == "" && req.Size != "" { + params.AspectRatio = geminitask.SizeToVeoAspectRatio(req.Size) + } + params.Resolution = strings.ToLower(params.Resolution) + params.SampleCount = 1 + + body := geminitask.VeoRequestPayload{ + Instances: []geminitask.VeoInstance{instance}, + Parameters: params, + } + + data, err := common.Marshal(body) + if err != nil { + return nil, err + } + return bytes.NewReader(data), nil +} + +// DoRequest delegates to common helper. +func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) { + return channel.DoTaskApiRequest(a, c, info, requestBody) +} + +// DoResponse handles upstream response, returns taskID etc. +func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) { + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return "", nil, service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) + } + _ = resp.Body.Close() + + var s submitResponse + if err := common.Unmarshal(responseBody, &s); err != nil { + return "", nil, service.TaskErrorWrapper(err, "unmarshal_response_failed", http.StatusInternalServerError) + } + if strings.TrimSpace(s.Name) == "" { + return "", nil, service.TaskErrorWrapper(fmt.Errorf("missing operation name"), "invalid_response", http.StatusInternalServerError) + } + localID := taskcommon.EncodeLocalTaskID(s.Name) + ov := dto.NewOpenAIVideo() + ov.ID = info.PublicTaskID + ov.TaskID = info.PublicTaskID + ov.CreatedAt = time.Now().Unix() + ov.Model = info.OriginModelName + c.JSON(http.StatusOK, ov) + return localID, responseBody, nil +} + +func (a *TaskAdaptor) GetModelList() []string { + return []string{ + "veo-3.0-generate-001", + "veo-3.0-fast-generate-001", + "veo-3.1-generate-preview", + "veo-3.1-fast-generate-preview", + } +} +func (a *TaskAdaptor) GetChannelName() string { return "vertex" } + +// FetchTask fetch task status +func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any, proxy string) (*http.Response, error) { + taskID, ok := body["task_id"].(string) + if !ok { + return nil, fmt.Errorf("invalid task_id") + } + upstreamName, err := taskcommon.DecodeLocalTaskID(taskID) + if err != nil { + return nil, fmt.Errorf("decode task_id failed: %w", err) + } + region := extractRegionFromOperationName(upstreamName) + if region == "" { + region = "us-central1" + } + project := extractProjectFromOperationName(upstreamName) + modelName := extractModelFromOperationName(upstreamName) + if project == "" || modelName == "" { + return nil, fmt.Errorf("cannot extract project/model from operation name") + } + var url string + if region == "global" { + url = fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:fetchPredictOperation", project, modelName) + } else { + url = fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:fetchPredictOperation", region, project, region, modelName) + } + payload := fetchOperationPayload{OperationName: upstreamName} + data, err := common.Marshal(payload) + if err != nil { + return nil, err + } + adc := &vertexcore.Credentials{} + if err := common.Unmarshal([]byte(key), adc); err != nil { + return nil, fmt.Errorf("failed to decode credentials: %w", err) + } + token, err := vertexcore.AcquireAccessToken(*adc, proxy) + if err != nil { + return nil, fmt.Errorf("failed to acquire access token: %w", err) + } + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("x-goog-user-project", adc.ProjectID) + client, err := service.GetHttpClientWithProxy(proxy) + if err != nil { + return nil, fmt.Errorf("new proxy http client failed: %w", err) + } + return client.Do(req) +} + +func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) { + var op operationResponse + if err := common.Unmarshal(respBody, &op); err != nil { + return nil, fmt.Errorf("unmarshal operation response failed: %w", err) + } + ti := &relaycommon.TaskInfo{} + if op.Error.Message != "" { + ti.Status = model.TaskStatusFailure + ti.Reason = op.Error.Message + ti.Progress = "100%" + return ti, nil + } + if !op.Done { + ti.Status = model.TaskStatusInProgress + ti.Progress = "50%" + return ti, nil + } + ti.Status = model.TaskStatusSuccess + ti.Progress = "100%" + if len(op.Response.Videos) > 0 { + v0 := op.Response.Videos[0] + if v0.BytesBase64Encoded != "" { + mime := strings.TrimSpace(v0.MimeType) + if mime == "" { + enc := strings.TrimSpace(v0.Encoding) + if enc == "" { + enc = "mp4" + } + if strings.Contains(enc, "/") { + mime = enc + } else { + mime = "video/" + enc + } + } + ti.Url = "data:" + mime + ";base64," + v0.BytesBase64Encoded + return ti, nil + } + } + if op.Response.BytesBase64Encoded != "" { + enc := strings.TrimSpace(op.Response.Encoding) + if enc == "" { + enc = "mp4" + } + mime := enc + if !strings.Contains(enc, "/") { + mime = "video/" + enc + } + ti.Url = "data:" + mime + ";base64," + op.Response.BytesBase64Encoded + return ti, nil + } + if op.Response.Video != "" { // some variants use `video` as base64 + enc := strings.TrimSpace(op.Response.Encoding) + if enc == "" { + enc = "mp4" + } + mime := enc + if !strings.Contains(enc, "/") { + mime = "video/" + enc + } + ti.Url = "data:" + mime + ";base64," + op.Response.Video + return ti, nil + } + return ti, nil +} + +func (a *TaskAdaptor) ConvertToOpenAIVideo(task *model.Task) ([]byte, error) { + // Use GetUpstreamTaskID() to get the real upstream operation name for model extraction. + // task.TaskID is now a public task_xxxx ID, no longer a base64-encoded upstream name. + upstreamTaskID := task.GetUpstreamTaskID() + upstreamName, err := taskcommon.DecodeLocalTaskID(upstreamTaskID) + if err != nil { + upstreamName = "" + } + modelName := extractModelFromOperationName(upstreamName) + if strings.TrimSpace(modelName) == "" { + modelName = "veo-3.0-generate-001" + } + v := dto.NewOpenAIVideo() + v.ID = task.TaskID + v.Model = modelName + v.Status = task.Status.ToVideoStatus() + v.SetProgressStr(task.Progress) + v.CreatedAt = task.CreatedAt + v.CompletedAt = task.UpdatedAt + if resultURL := task.GetResultURL(); strings.HasPrefix(resultURL, "data:") && len(resultURL) > 0 { + v.SetMetadata("url", resultURL) + } + + return common.Marshal(v) +} + +// ============================ +// helpers +// ============================ + +var regionRe = regexp.MustCompile(`locations/([a-z0-9-]+)/`) + +func extractRegionFromOperationName(name string) string { + m := regionRe.FindStringSubmatch(name) + if len(m) == 2 { + return m[1] + } + return "" +} + +var modelRe = regexp.MustCompile(`models/([^/]+)/operations/`) + +func extractModelFromOperationName(name string) string { + m := modelRe.FindStringSubmatch(name) + if len(m) == 2 { + return m[1] + } + idx := strings.Index(name, "models/") + if idx >= 0 { + s := name[idx+len("models/"):] + if p := strings.Index(s, "/operations/"); p > 0 { + return s[:p] + } + } + return "" +} + +var projectRe = regexp.MustCompile(`projects/([^/]+)/locations/`) + +func extractProjectFromOperationName(name string) string { + m := projectRe.FindStringSubmatch(name) + if len(m) == 2 { + return m[1] + } + return "" +} diff --git a/_all-in-one-upload/new-api/relay/channel/volcengine/constants.go b/_all-in-one-upload/new-api/relay/channel/volcengine/constants.go new file mode 100644 index 0000000..87a12b2 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/volcengine/constants.go @@ -0,0 +1,19 @@ +package volcengine + +var ModelList = []string{ + "Doubao-pro-128k", + "Doubao-pro-32k", + "Doubao-pro-4k", + "Doubao-lite-128k", + "Doubao-lite-32k", + "Doubao-lite-4k", + "Doubao-embedding", + "doubao-seedream-4-0-250828", + "seedream-4-0-250828", + "doubao-seedance-1-0-pro-250528", + "seedance-1-0-pro-250528", + "doubao-seed-1-6-thinking-250715", + "seed-1-6-thinking-250715", +} + +var ChannelName = "volcengine" diff --git a/_all-in-one-upload/new-api/relay/channel/xinference/constant.go b/_all-in-one-upload/new-api/relay/channel/xinference/constant.go new file mode 100644 index 0000000..a119084 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/xinference/constant.go @@ -0,0 +1,8 @@ +package xinference + +var ModelList = []string{ + "bge-reranker-v2-m3", + "jina-reranker-v2", +} + +var ChannelName = "xinference" diff --git a/_all-in-one-upload/new-api/relay/channel/zhipu_4v/relay-zhipu_v4.go b/_all-in-one-upload/new-api/relay/channel/zhipu_4v/relay-zhipu_v4.go new file mode 100644 index 0000000..91ef0c4 --- /dev/null +++ b/_all-in-one-upload/new-api/relay/channel/zhipu_4v/relay-zhipu_v4.go @@ -0,0 +1,60 @@ +package zhipu_4v + +import ( + "strings" + + "github.com/QuantumNous/new-api/dto" +) + +func requestOpenAI2Zhipu(request dto.GeneralOpenAIRequest) *dto.GeneralOpenAIRequest { + messages := make([]dto.Message, 0, len(request.Messages)) + for _, message := range request.Messages { + if !message.IsStringContent() { + mediaMessages := message.ParseContent() + for j, mediaMessage := range mediaMessages { + if mediaMessage.Type == dto.ContentTypeImageURL { + imageUrl := mediaMessage.GetImageMedia() + // check if base64 + if strings.HasPrefix(imageUrl.Url, "data:image/") { + // 去除base64数据的URL前缀(如果有) + if idx := strings.Index(imageUrl.Url, ","); idx != -1 { + imageUrl.Url = imageUrl.Url[idx+1:] + } + } + mediaMessage.ImageUrl = imageUrl + mediaMessages[j] = mediaMessage + } + } + message.SetMediaContent(mediaMessages) + } + messages = append(messages, dto.Message{ + Role: message.Role, + Content: message.Content, + ToolCalls: message.ToolCalls, + ToolCallId: message.ToolCallId, + }) + } + str, ok := request.Stop.(string) + var Stop []string + if ok { + Stop = []string{str} + } else { + Stop, _ = request.Stop.([]string) + } + out := &dto.GeneralOpenAIRequest{ + Model: request.Model, + Stream: request.Stream, + Messages: messages, + Temperature: request.Temperature, + TopP: request.TopP, + Stop: Stop, + Tools: request.Tools, + ToolChoice: request.ToolChoice, + THINKING: request.THINKING, + } + if request.MaxTokens != nil || request.MaxCompletionTokens != nil { + maxTokens := request.GetMaxTokens() + out.MaxTokens = &maxTokens + } + return out +} diff --git a/_all-in-one-upload/new-api/relay/helper/price.go b/_all-in-one-upload/new-api/relay/helper/price.go new file mode 100644 index 0000000..0e68edb --- /dev/null +++ b/_all-in-one-upload/new-api/relay/helper/price.go @@ -0,0 +1,308 @@ +package helper + +import ( + "fmt" + "strings" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/logger" + "github.com/QuantumNous/new-api/model" + "github.com/QuantumNous/new-api/pkg/billingexpr" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/setting/billing_setting" + "github.com/QuantumNous/new-api/setting/operation_setting" + "github.com/QuantumNous/new-api/setting/ratio_setting" + "github.com/QuantumNous/new-api/types" + + "github.com/gin-gonic/gin" +) + +func modelPriceNotConfiguredError(modelName string, userId int) error { + if model.IsAdmin(userId) { + return fmt.Errorf( + "模型 %s 的价格未配置。请前往「系统设置 → 运营设置」开启自用模式,或在「系统设置 → 分组与模型定价设置」中为该模型配置价格;"+ + "Model %s price not configured. Go to System Settings → Operation Settings to enable self-use mode, or configure the model price in System Settings → Group & Model Pricing.", + modelName, modelName, + ) + } + return fmt.Errorf( + "模型 %s 的价格尚未由管理员配置,暂时无法使用,请联系站点管理员开启该模型;"+ + "Model %s has not been priced by the administrator yet. Please contact the site administrator to enable this model.", + modelName, modelName, + ) +} + +// https://docs.claude.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration +const claudeCacheCreation1hMultiplier = 6 / 3.75 + +// HandleGroupRatio checks for "auto_group" in the context and updates the group ratio and relayInfo.UsingGroup if present +func HandleGroupRatio(ctx *gin.Context, relayInfo *relaycommon.RelayInfo) types.GroupRatioInfo { + groupRatioInfo := types.GroupRatioInfo{ + GroupRatio: 1.0, // default ratio + GroupSpecialRatio: -1, + } + + // check auto group + autoGroup, exists := ctx.Get("auto_group") + if exists { + logger.LogDebug(ctx, fmt.Sprintf("final group: %s", autoGroup)) + relayInfo.UsingGroup = autoGroup.(string) + } + + // check user group special ratio + userGroupRatio, ok := ratio_setting.GetGroupGroupRatio(relayInfo.UserGroup, relayInfo.UsingGroup) + if ok { + // user group special ratio + groupRatioInfo.GroupSpecialRatio = userGroupRatio + groupRatioInfo.GroupRatio = userGroupRatio + groupRatioInfo.HasSpecialRatio = true + } else { + // normal group ratio + groupRatioInfo.GroupRatio = ratio_setting.GetGroupRatio(relayInfo.UsingGroup) + } + + return groupRatioInfo +} + +func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens int, meta *types.TokenCountMeta) (types.PriceData, error) { + modelPrice, usePrice := ratio_setting.GetModelPrice(info.OriginModelName, false) + + groupRatioInfo := HandleGroupRatio(c, info) + + // Check if this model uses tiered_expr billing + if billing_setting.GetBillingMode(info.OriginModelName) == billing_setting.BillingModeTieredExpr { + return modelPriceHelperTiered(c, info, promptTokens, meta, groupRatioInfo) + } + + var preConsumedQuota int + var modelRatio float64 + var completionRatio float64 + var cacheRatio float64 + var imageRatio float64 + var cacheCreationRatio float64 + var cacheCreationRatio5m float64 + var cacheCreationRatio1h float64 + var audioRatio float64 + var audioCompletionRatio float64 + var freeModel bool + if !usePrice { + preConsumedTokens := common.Max(promptTokens, common.PreConsumedQuota) + if meta.MaxTokens != 0 { + preConsumedTokens += meta.MaxTokens + } + var success bool + var matchName string + modelRatio, success, matchName = ratio_setting.GetModelRatio(info.OriginModelName) + if !success { + acceptUnsetRatio := false + if info.UserSetting.AcceptUnsetRatioModel { + acceptUnsetRatio = true + } + if !acceptUnsetRatio { + return types.PriceData{}, modelPriceNotConfiguredError(matchName, info.UserId) + } + } + completionRatio = ratio_setting.GetCompletionRatio(info.OriginModelName) + cacheRatio, _ = ratio_setting.GetCacheRatio(info.OriginModelName) + cacheCreationRatio, _ = ratio_setting.GetCreateCacheRatio(info.OriginModelName) + cacheCreationRatio5m = cacheCreationRatio + // 固定1h和5min缓存写入价格的比例 + cacheCreationRatio1h = cacheCreationRatio * claudeCacheCreation1hMultiplier + imageRatio, _ = ratio_setting.GetImageRatio(info.OriginModelName) + audioRatio = ratio_setting.GetAudioRatio(info.OriginModelName) + audioCompletionRatio = ratio_setting.GetAudioCompletionRatio(info.OriginModelName) + ratio := modelRatio * groupRatioInfo.GroupRatio + preConsumedQuota = int(float64(preConsumedTokens) * ratio) + } else { + if meta.ImagePriceRatio != 0 { + modelPrice = modelPrice * meta.ImagePriceRatio + } + preConsumedQuota = int(modelPrice * common.QuotaPerUnit * groupRatioInfo.GroupRatio) + } + + // check if free model pre-consume is disabled + if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume { + // if model price or ratio is 0, do not pre-consume quota + if groupRatioInfo.GroupRatio == 0 { + preConsumedQuota = 0 + freeModel = true + } else if usePrice { + if modelPrice == 0 { + preConsumedQuota = 0 + freeModel = true + } + } else { + if modelRatio == 0 { + preConsumedQuota = 0 + freeModel = true + } + } + } + + priceData := types.PriceData{ + FreeModel: freeModel, + ModelPrice: modelPrice, + ModelRatio: modelRatio, + CompletionRatio: completionRatio, + GroupRatioInfo: groupRatioInfo, + UsePrice: usePrice, + CacheRatio: cacheRatio, + ImageRatio: imageRatio, + AudioRatio: audioRatio, + AudioCompletionRatio: audioCompletionRatio, + CacheCreationRatio: cacheCreationRatio, + CacheCreation5mRatio: cacheCreationRatio5m, + CacheCreation1hRatio: cacheCreationRatio1h, + QuotaToPreConsume: preConsumedQuota, + } + + if common.DebugEnabled { + println(fmt.Sprintf("model_price_helper result: %s", priceData.ToSetting())) + } + info.PriceData = priceData + return priceData, nil +} + +// ModelPriceHelperPerCall 按次/按量计费的 PriceHelper (MJ、Task) +func ModelPriceHelperPerCall(c *gin.Context, info *relaycommon.RelayInfo) (types.PriceData, error) { + groupRatioInfo := HandleGroupRatio(c, info) + + modelPrice, success := ratio_setting.GetModelPrice(info.OriginModelName, true) + usePrice := success + var modelRatio float64 + + if !success { + defaultPrice, ok := ratio_setting.GetDefaultModelPriceMap()[info.OriginModelName] + if ok { + modelPrice = defaultPrice + usePrice = true + } else { + var ratioSuccess bool + var matchName string + modelRatio, ratioSuccess, matchName = ratio_setting.GetModelRatio(info.OriginModelName) + acceptUnsetRatio := false + if info.UserSetting.AcceptUnsetRatioModel { + acceptUnsetRatio = true + } + if !ratioSuccess && !acceptUnsetRatio { + return types.PriceData{}, modelPriceNotConfiguredError(matchName, info.UserId) + } + } + } + + var quota int + freeModel := false + + if usePrice { + quota = int(modelPrice * common.QuotaPerUnit * groupRatioInfo.GroupRatio) + if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume { + if groupRatioInfo.GroupRatio == 0 || modelPrice == 0 { + quota = 0 + freeModel = true + } + } + } else { + // 按量计费:以模型倍率的一半作为预扣额度 + quota = int(modelRatio / 2 * common.QuotaPerUnit * groupRatioInfo.GroupRatio) + modelPrice = -1 + if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume { + if groupRatioInfo.GroupRatio == 0 || modelRatio == 0 { + quota = 0 + freeModel = true + } + } + } + + priceData := types.PriceData{ + FreeModel: freeModel, + ModelPrice: modelPrice, + ModelRatio: modelRatio, + UsePrice: usePrice, + Quota: quota, + GroupRatioInfo: groupRatioInfo, + } + return priceData, nil +} + +func HasModelBillingConfig(modelName string) bool { + if _, ok := ratio_setting.GetModelPrice(modelName, false); ok { + return true + } + if _, ok, _ := ratio_setting.GetModelRatio(modelName); ok { + return true + } + if billing_setting.GetBillingMode(modelName) != billing_setting.BillingModeTieredExpr { + return false + } + expr, ok := billing_setting.GetBillingExpr(modelName) + return ok && strings.TrimSpace(expr) != "" +} + +func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptTokens int, meta *types.TokenCountMeta, groupRatioInfo types.GroupRatioInfo) (types.PriceData, error) { + exprStr, ok := billing_setting.GetBillingExpr(info.OriginModelName) + if !ok { + return types.PriceData{}, fmt.Errorf("model %s is configured as tiered_expr but has no billing expression", info.OriginModelName) + } + + estimatedCompletionTokens := 0 + if meta.MaxTokens != 0 { + estimatedCompletionTokens = meta.MaxTokens + } + + requestInput, err := ResolveIncomingBillingExprRequestInput(c, info) + if err != nil { + return types.PriceData{}, err + } + + rawCost, trace, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{ + P: float64(promptTokens), + C: float64(estimatedCompletionTokens), + Len: float64(promptTokens), + }, requestInput) + if err != nil { + return types.PriceData{}, fmt.Errorf("model %s tiered expr run failed: %w", info.OriginModelName, err) + } + + // Expression coefficients are $/1M tokens prices; convert to quota the same way per-call billing does. + quotaBeforeGroup := rawCost / 1_000_000 * common.QuotaPerUnit + preConsumedQuota := billingexpr.QuotaRound(quotaBeforeGroup * groupRatioInfo.GroupRatio) + + freeModel := false + if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume { + if groupRatioInfo.GroupRatio == 0 { + preConsumedQuota = 0 + freeModel = true + } + } + + exprHash := billingexpr.ExprHashString(exprStr) + snapshot := &billingexpr.BillingSnapshot{ + BillingMode: billing_setting.BillingModeTieredExpr, + ModelName: info.OriginModelName, + ExprString: exprStr, + ExprHash: exprHash, + GroupRatio: groupRatioInfo.GroupRatio, + EstimatedPromptTokens: promptTokens, + EstimatedCompletionTokens: estimatedCompletionTokens, + EstimatedQuotaBeforeGroup: quotaBeforeGroup, + EstimatedQuotaAfterGroup: preConsumedQuota, + EstimatedTier: trace.MatchedTier, + QuotaPerUnit: common.QuotaPerUnit, + ExprVersion: billingexpr.ExprVersion(exprStr), + } + info.TieredBillingSnapshot = snapshot + info.BillingRequestInput = &requestInput + + priceData := types.PriceData{ + FreeModel: freeModel, + GroupRatioInfo: groupRatioInfo, + QuotaToPreConsume: preConsumedQuota, + } + + if common.DebugEnabled { + println(fmt.Sprintf("model_price_helper_tiered result: model=%s preConsume=%d quotaBeforeGroup=%.2f groupRatio=%.2f tier=%s", info.OriginModelName, preConsumedQuota, quotaBeforeGroup, groupRatioInfo.GroupRatio, trace.MatchedTier)) + } + + info.PriceData = priceData + return priceData, nil +} diff --git a/_all-in-one-upload/new-api/service/codex_oauth.go b/_all-in-one-upload/new-api/service/codex_oauth.go new file mode 100644 index 0000000..33ef1d6 --- /dev/null +++ b/_all-in-one-upload/new-api/service/codex_oauth.go @@ -0,0 +1,317 @@ +package service + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" +) + +const ( + codexOAuthClientID = "app_EMoamEEZ73f0CkXaXp7hrann" + codexOAuthAuthorizeURL = "https://auth.openai.com/oauth/authorize" + codexOAuthTokenURL = "https://auth.openai.com/oauth/token" + codexOAuthRedirectURI = "http://localhost:1455/auth/callback" + codexOAuthScope = "openid profile email offline_access" + codexJWTClaimPath = "https://api.openai.com/auth" + defaultHTTPTimeout = 20 * time.Second +) + +type CodexOAuthTokenResult struct { + AccessToken string + RefreshToken string + ExpiresAt time.Time +} + +type CodexOAuthAuthorizationFlow struct { + State string + Verifier string + Challenge string + AuthorizeURL string +} + +func RefreshCodexOAuthToken(ctx context.Context, refreshToken string) (*CodexOAuthTokenResult, error) { + return RefreshCodexOAuthTokenWithProxy(ctx, refreshToken, "") +} + +func RefreshCodexOAuthTokenWithProxy(ctx context.Context, refreshToken string, proxyURL string) (*CodexOAuthTokenResult, error) { + client, err := getCodexOAuthHTTPClient(proxyURL) + if err != nil { + return nil, err + } + return refreshCodexOAuthToken(ctx, client, codexOAuthTokenURL, codexOAuthClientID, refreshToken) +} + +func ExchangeCodexAuthorizationCode(ctx context.Context, code string, verifier string) (*CodexOAuthTokenResult, error) { + return ExchangeCodexAuthorizationCodeWithProxy(ctx, code, verifier, "") +} + +func ExchangeCodexAuthorizationCodeWithProxy(ctx context.Context, code string, verifier string, proxyURL string) (*CodexOAuthTokenResult, error) { + client, err := getCodexOAuthHTTPClient(proxyURL) + if err != nil { + return nil, err + } + return exchangeCodexAuthorizationCode(ctx, client, codexOAuthTokenURL, codexOAuthClientID, code, verifier, codexOAuthRedirectURI) +} + +func CreateCodexOAuthAuthorizationFlow() (*CodexOAuthAuthorizationFlow, error) { + state, err := createStateHex(16) + if err != nil { + return nil, err + } + verifier, challenge, err := generatePKCEPair() + if err != nil { + return nil, err + } + u, err := buildCodexAuthorizeURL(state, challenge) + if err != nil { + return nil, err + } + return &CodexOAuthAuthorizationFlow{ + State: state, + Verifier: verifier, + Challenge: challenge, + AuthorizeURL: u, + }, nil +} + +func refreshCodexOAuthToken( + ctx context.Context, + client *http.Client, + tokenURL string, + clientID string, + refreshToken string, +) (*CodexOAuthTokenResult, error) { + rt := strings.TrimSpace(refreshToken) + if rt == "" { + return nil, errors.New("empty refresh_token") + } + + form := url.Values{} + form.Set("grant_type", "refresh_token") + form.Set("refresh_token", rt) + form.Set("client_id", clientID) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(form.Encode())) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Accept", "application/json") + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var payload struct { + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + ExpiresIn int `json:"expires_in"` + } + + if err := common.DecodeJson(resp.Body, &payload); err != nil { + return nil, err + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, fmt.Errorf("codex oauth refresh failed: status=%d", resp.StatusCode) + } + + if strings.TrimSpace(payload.AccessToken) == "" || strings.TrimSpace(payload.RefreshToken) == "" || payload.ExpiresIn <= 0 { + return nil, errors.New("codex oauth refresh response missing fields") + } + + return &CodexOAuthTokenResult{ + AccessToken: strings.TrimSpace(payload.AccessToken), + RefreshToken: strings.TrimSpace(payload.RefreshToken), + ExpiresAt: time.Now().Add(time.Duration(payload.ExpiresIn) * time.Second), + }, nil +} + +func exchangeCodexAuthorizationCode( + ctx context.Context, + client *http.Client, + tokenURL string, + clientID string, + code string, + verifier string, + redirectURI string, +) (*CodexOAuthTokenResult, error) { + c := strings.TrimSpace(code) + v := strings.TrimSpace(verifier) + if c == "" { + return nil, errors.New("empty authorization code") + } + if v == "" { + return nil, errors.New("empty code_verifier") + } + + form := url.Values{} + form.Set("grant_type", "authorization_code") + form.Set("client_id", clientID) + form.Set("code", c) + form.Set("code_verifier", v) + form.Set("redirect_uri", redirectURI) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(form.Encode())) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("Accept", "application/json") + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var payload struct { + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + ExpiresIn int `json:"expires_in"` + } + if err := common.DecodeJson(resp.Body, &payload); err != nil { + return nil, err + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, fmt.Errorf("codex oauth code exchange failed: status=%d", resp.StatusCode) + } + if strings.TrimSpace(payload.AccessToken) == "" || strings.TrimSpace(payload.RefreshToken) == "" || payload.ExpiresIn <= 0 { + return nil, errors.New("codex oauth token response missing fields") + } + return &CodexOAuthTokenResult{ + AccessToken: strings.TrimSpace(payload.AccessToken), + RefreshToken: strings.TrimSpace(payload.RefreshToken), + ExpiresAt: time.Now().Add(time.Duration(payload.ExpiresIn) * time.Second), + }, nil +} + +func getCodexOAuthHTTPClient(proxyURL string) (*http.Client, error) { + baseClient, err := GetHttpClientWithProxy(strings.TrimSpace(proxyURL)) + if err != nil { + return nil, err + } + if baseClient == nil { + return &http.Client{Timeout: defaultHTTPTimeout}, nil + } + clientCopy := *baseClient + clientCopy.Timeout = defaultHTTPTimeout + return &clientCopy, nil +} + +func buildCodexAuthorizeURL(state string, challenge string) (string, error) { + u, err := url.Parse(codexOAuthAuthorizeURL) + if err != nil { + return "", err + } + q := u.Query() + q.Set("response_type", "code") + q.Set("client_id", codexOAuthClientID) + q.Set("redirect_uri", codexOAuthRedirectURI) + q.Set("scope", codexOAuthScope) + q.Set("code_challenge", challenge) + q.Set("code_challenge_method", "S256") + q.Set("state", state) + q.Set("id_token_add_organizations", "true") + q.Set("codex_cli_simplified_flow", "true") + q.Set("originator", "codex_cli_rs") + u.RawQuery = q.Encode() + return u.String(), nil +} + +func createStateHex(nBytes int) (string, error) { + if nBytes <= 0 { + return "", errors.New("invalid state bytes length") + } + b := make([]byte, nBytes) + if _, err := rand.Read(b); err != nil { + return "", err + } + return fmt.Sprintf("%x", b), nil +} + +func generatePKCEPair() (verifier string, challenge string, err error) { + b := make([]byte, 32) + if _, err := rand.Read(b); err != nil { + return "", "", err + } + verifier = base64.RawURLEncoding.EncodeToString(b) + sum := sha256.Sum256([]byte(verifier)) + challenge = base64.RawURLEncoding.EncodeToString(sum[:]) + return verifier, challenge, nil +} + +func ExtractCodexAccountIDFromJWT(token string) (string, bool) { + claims, ok := decodeJWTClaims(token) + if !ok { + return "", false + } + raw, ok := claims[codexJWTClaimPath] + if !ok { + return "", false + } + obj, ok := raw.(map[string]any) + if !ok { + return "", false + } + v, ok := obj["chatgpt_account_id"] + if !ok { + return "", false + } + s, ok := v.(string) + if !ok { + return "", false + } + s = strings.TrimSpace(s) + if s == "" { + return "", false + } + return s, true +} + +func ExtractEmailFromJWT(token string) (string, bool) { + claims, ok := decodeJWTClaims(token) + if !ok { + return "", false + } + v, ok := claims["email"] + if !ok { + return "", false + } + s, ok := v.(string) + if !ok { + return "", false + } + s = strings.TrimSpace(s) + if s == "" { + return "", false + } + return s, true +} + +func decodeJWTClaims(token string) (map[string]any, bool) { + parts := strings.Split(token, ".") + if len(parts) != 3 { + return nil, false + } + payloadRaw, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil { + return nil, false + } + var claims map[string]any + if err := json.Unmarshal(payloadRaw, &claims); err != nil { + return nil, false + } + return claims, true +} diff --git a/_all-in-one-upload/new-api/service/quota.go b/_all-in-one-upload/new-api/service/quota.go new file mode 100644 index 0000000..398bd1b --- /dev/null +++ b/_all-in-one-upload/new-api/service/quota.go @@ -0,0 +1,544 @@ +package service + +import ( + "errors" + "fmt" + "log" + "math" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/logger" + "github.com/QuantumNous/new-api/model" + "github.com/QuantumNous/new-api/pkg/billingexpr" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/setting/ratio_setting" + "github.com/QuantumNous/new-api/setting/system_setting" + "github.com/QuantumNous/new-api/types" + + "github.com/bytedance/gopkg/util/gopool" + + "github.com/gin-gonic/gin" + "github.com/shopspring/decimal" +) + +type TokenDetails struct { + TextTokens int + AudioTokens int +} + +type QuotaInfo struct { + InputDetails TokenDetails + OutputDetails TokenDetails + ModelName string + UsePrice bool + ModelPrice float64 + ModelRatio float64 + GroupRatio float64 +} + +func hasCustomModelRatio(modelName string, currentRatio float64) bool { + defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName] + if !exists { + return true + } + return currentRatio != defaultRatio +} + +func calculateAudioQuota(info QuotaInfo) int { + if info.UsePrice { + modelPrice := decimal.NewFromFloat(info.ModelPrice) + quotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit) + groupRatio := decimal.NewFromFloat(info.GroupRatio) + + quota := modelPrice.Mul(quotaPerUnit).Mul(groupRatio) + return int(quota.IntPart()) + } + + completionRatio := decimal.NewFromFloat(ratio_setting.GetCompletionRatio(info.ModelName)) + audioRatio := decimal.NewFromFloat(ratio_setting.GetAudioRatio(info.ModelName)) + audioCompletionRatio := decimal.NewFromFloat(ratio_setting.GetAudioCompletionRatio(info.ModelName)) + + groupRatio := decimal.NewFromFloat(info.GroupRatio) + modelRatio := decimal.NewFromFloat(info.ModelRatio) + ratio := groupRatio.Mul(modelRatio) + + inputTextTokens := decimal.NewFromInt(int64(info.InputDetails.TextTokens)) + outputTextTokens := decimal.NewFromInt(int64(info.OutputDetails.TextTokens)) + inputAudioTokens := decimal.NewFromInt(int64(info.InputDetails.AudioTokens)) + outputAudioTokens := decimal.NewFromInt(int64(info.OutputDetails.AudioTokens)) + + quota := decimal.Zero + quota = quota.Add(inputTextTokens) + quota = quota.Add(outputTextTokens.Mul(completionRatio)) + quota = quota.Add(inputAudioTokens.Mul(audioRatio)) + quota = quota.Add(outputAudioTokens.Mul(audioRatio).Mul(audioCompletionRatio)) + + quota = quota.Mul(ratio) + + // If ratio is not zero and quota is less than or equal to zero, set quota to 1 + if !ratio.IsZero() && quota.LessThanOrEqual(decimal.Zero) { + quota = decimal.NewFromInt(1) + } + + return int(quota.Round(0).IntPart()) +} + +func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage *dto.RealtimeUsage) error { + if relayInfo.UsePrice { + return nil + } + userQuota, err := model.GetUserQuota(relayInfo.UserId, false) + if err != nil { + return err + } + + token, err := model.GetTokenByKey(strings.TrimPrefix(relayInfo.TokenKey, "sk-"), false) + if err != nil { + return err + } + + modelName := relayInfo.OriginModelName + textInputTokens := usage.InputTokenDetails.TextTokens + textOutTokens := usage.OutputTokenDetails.TextTokens + audioInputTokens := usage.InputTokenDetails.AudioTokens + audioOutTokens := usage.OutputTokenDetails.AudioTokens + groupRatio := ratio_setting.GetGroupRatio(relayInfo.UsingGroup) + modelRatio, _, _ := ratio_setting.GetModelRatio(modelName) + + autoGroup, exists := common.GetContextKey(ctx, constant.ContextKeyAutoGroup) + if exists { + groupRatio = ratio_setting.GetGroupRatio(autoGroup.(string)) + log.Printf("final group ratio: %f", groupRatio) + relayInfo.UsingGroup = autoGroup.(string) + } + + actualGroupRatio := groupRatio + userGroupRatio, ok := ratio_setting.GetGroupGroupRatio(relayInfo.UserGroup, relayInfo.UsingGroup) + if ok { + actualGroupRatio = userGroupRatio + } + + quotaInfo := QuotaInfo{ + InputDetails: TokenDetails{ + TextTokens: textInputTokens, + AudioTokens: audioInputTokens, + }, + OutputDetails: TokenDetails{ + TextTokens: textOutTokens, + AudioTokens: audioOutTokens, + }, + ModelName: modelName, + UsePrice: relayInfo.UsePrice, + ModelRatio: modelRatio, + GroupRatio: actualGroupRatio, + } + + quota := calculateAudioQuota(quotaInfo) + + if userQuota < quota { + return fmt.Errorf("user quota is not enough, user quota: %s, need quota: %s", logger.FormatQuota(userQuota), logger.FormatQuota(quota)) + } + + if !token.UnlimitedQuota && token.RemainQuota < quota { + return fmt.Errorf("token quota is not enough, token remain quota: %s, need quota: %s", logger.FormatQuota(token.RemainQuota), logger.FormatQuota(quota)) + } + + err = PostConsumeQuota(relayInfo, quota, 0, false) + if err != nil { + return err + } + logger.LogInfo(ctx, "realtime streaming consume quota success, quota: "+fmt.Sprintf("%d", quota)) + return nil +} + +func PostWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, modelName string, + usage *dto.RealtimeUsage, extraContent string) { + + var tieredResult *billingexpr.TieredResult + tieredOk, tieredQuota, tieredRes := TryTieredSettle(relayInfo, billingexpr.TokenParams{ + P: float64(usage.InputTokens), + C: float64(usage.OutputTokens), + Len: float64(usage.InputTokens), + }) + if tieredOk { + tieredResult = tieredRes + } + + useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix() + textInputTokens := usage.InputTokenDetails.TextTokens + textOutTokens := usage.OutputTokenDetails.TextTokens + + audioInputTokens := usage.InputTokenDetails.AudioTokens + audioOutTokens := usage.OutputTokenDetails.AudioTokens + + tokenName := ctx.GetString("token_name") + completionRatio := decimal.NewFromFloat(ratio_setting.GetCompletionRatio(modelName)) + audioRatio := decimal.NewFromFloat(ratio_setting.GetAudioRatio(relayInfo.OriginModelName)) + audioCompletionRatio := decimal.NewFromFloat(ratio_setting.GetAudioCompletionRatio(modelName)) + + modelRatio := relayInfo.PriceData.ModelRatio + groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio + modelPrice := relayInfo.PriceData.ModelPrice + usePrice := relayInfo.PriceData.UsePrice + + quotaInfo := QuotaInfo{ + InputDetails: TokenDetails{ + TextTokens: textInputTokens, + AudioTokens: audioInputTokens, + }, + OutputDetails: TokenDetails{ + TextTokens: textOutTokens, + AudioTokens: audioOutTokens, + }, + ModelName: modelName, + UsePrice: usePrice, + ModelRatio: modelRatio, + GroupRatio: groupRatio, + } + + quota := calculateAudioQuota(quotaInfo) + if tieredOk { + quota = tieredQuota + } + + totalTokens := usage.TotalTokens + var logContent string + if !usePrice { + logContent = fmt.Sprintf("模型倍率 %.2f,补全倍率 %.2f,音频倍率 %.2f,音频补全倍率 %.2f,分组倍率 %.2f", + modelRatio, completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), groupRatio) + } else { + logContent = fmt.Sprintf("模型价格 %.2f,分组倍率 %.2f", modelPrice, groupRatio) + } + + // record all the consume log even if quota is 0 + if totalTokens == 0 { + // in this case, must be some error happened + // we cannot just return, because we may have to return the pre-consumed quota + quota = 0 + logContent += fmt.Sprintf("(可能是上游超时)") + logger.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+ + "tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, modelName, relayInfo.FinalPreConsumedQuota)) + } else { + model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota) + model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota) + } + + if err := SettleBilling(ctx, relayInfo, quota); err != nil { + logger.LogError(ctx, "error settling billing: "+err.Error()) + } + + logModel := modelName + if extraContent != "" { + logContent += ", " + extraContent + } + other := GenerateWssOtherInfo(ctx, relayInfo, usage, modelRatio, groupRatio, + completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), modelPrice, relayInfo.PriceData.GroupRatioInfo.GroupSpecialRatio) + if tieredResult != nil { + InjectTieredBillingInfo(other, relayInfo, tieredResult) + } + model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{ + ChannelId: relayInfo.ChannelId, + PromptTokens: usage.InputTokens, + CompletionTokens: usage.OutputTokens, + ModelName: logModel, + TokenName: tokenName, + Quota: quota, + Content: logContent, + TokenId: relayInfo.TokenId, + UseTimeSeconds: int(useTimeSeconds), + IsStream: relayInfo.IsStream, + Group: relayInfo.UsingGroup, + Other: other, + }) +} + +func CalcOpenRouterCacheCreateTokens(usage dto.Usage, priceData types.PriceData) int { + if priceData.CacheCreationRatio == 1 { + return 0 + } + quotaPrice := priceData.ModelRatio / common.QuotaPerUnit + promptCacheCreatePrice := quotaPrice * priceData.CacheCreationRatio + promptCacheReadPrice := quotaPrice * priceData.CacheRatio + completionPrice := quotaPrice * priceData.CompletionRatio + + cost, _ := usage.Cost.(float64) + totalPromptTokens := float64(usage.PromptTokens) + completionTokens := float64(usage.CompletionTokens) + promptCacheReadTokens := float64(usage.PromptTokensDetails.CachedTokens) + + return int(math.Round((cost - + totalPromptTokens*quotaPrice + + promptCacheReadTokens*(quotaPrice-promptCacheReadPrice) - + completionTokens*completionPrice) / + (promptCacheCreatePrice - quotaPrice))) +} + +func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage *dto.Usage, extraContent string) { + + var tieredUsedVars map[string]bool + if snap := relayInfo.TieredBillingSnapshot; snap != nil { + tieredUsedVars = billingexpr.UsedVars(snap.ExprString) + } + var tieredResult *billingexpr.TieredResult + tieredOk, tieredQuota, tieredRes := TryTieredSettle(relayInfo, BuildTieredTokenParams(usage, false, tieredUsedVars)) + if tieredOk { + tieredResult = tieredRes + } + + useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix() + textInputTokens := usage.PromptTokensDetails.TextTokens + textOutTokens := usage.CompletionTokenDetails.TextTokens + + audioInputTokens := usage.PromptTokensDetails.AudioTokens + audioOutTokens := usage.CompletionTokenDetails.AudioTokens + + tokenName := ctx.GetString("token_name") + completionRatio := decimal.NewFromFloat(ratio_setting.GetCompletionRatio(relayInfo.OriginModelName)) + audioRatio := decimal.NewFromFloat(ratio_setting.GetAudioRatio(relayInfo.OriginModelName)) + audioCompletionRatio := decimal.NewFromFloat(ratio_setting.GetAudioCompletionRatio(relayInfo.OriginModelName)) + + modelRatio := relayInfo.PriceData.ModelRatio + groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio + modelPrice := relayInfo.PriceData.ModelPrice + usePrice := relayInfo.PriceData.UsePrice + + quotaInfo := QuotaInfo{ + InputDetails: TokenDetails{ + TextTokens: textInputTokens, + AudioTokens: audioInputTokens, + }, + OutputDetails: TokenDetails{ + TextTokens: textOutTokens, + AudioTokens: audioOutTokens, + }, + ModelName: relayInfo.OriginModelName, + UsePrice: usePrice, + ModelRatio: modelRatio, + GroupRatio: groupRatio, + } + + quota := calculateAudioQuota(quotaInfo) + if tieredOk { + quota = tieredQuota + } + + totalTokens := usage.TotalTokens + var logContent string + if !usePrice { + logContent = fmt.Sprintf("模型倍率 %.2f,补全倍率 %.2f,音频倍率 %.2f,音频补全倍率 %.2f,分组倍率 %.2f", + modelRatio, completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), groupRatio) + } else { + logContent = fmt.Sprintf("模型价格 %.2f,分组倍率 %.2f", modelPrice, groupRatio) + } + + // record all the consume log even if quota is 0 + if totalTokens == 0 { + // in this case, must be some error happened + // we cannot just return, because we may have to return the pre-consumed quota + quota = 0 + logContent += fmt.Sprintf("(可能是上游超时)") + logger.LogError(ctx, fmt.Sprintf("total tokens is 0, cannot consume quota, userId %d, channelId %d, "+ + "tokenId %d, model %s, pre-consumed quota %d", relayInfo.UserId, relayInfo.ChannelId, relayInfo.TokenId, relayInfo.OriginModelName, relayInfo.FinalPreConsumedQuota)) + } else { + model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, quota) + model.UpdateChannelUsedQuota(relayInfo.ChannelId, quota) + } + + if err := SettleBilling(ctx, relayInfo, quota); err != nil { + logger.LogError(ctx, "error settling billing: "+err.Error()) + } + + logModel := relayInfo.OriginModelName + if extraContent != "" { + logContent += ", " + extraContent + } + other := GenerateAudioOtherInfo(ctx, relayInfo, usage, modelRatio, groupRatio, + completionRatio.InexactFloat64(), audioRatio.InexactFloat64(), audioCompletionRatio.InexactFloat64(), modelPrice, relayInfo.PriceData.GroupRatioInfo.GroupSpecialRatio) + if tieredResult != nil { + InjectTieredBillingInfo(other, relayInfo, tieredResult) + } + model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{ + ChannelId: relayInfo.ChannelId, + PromptTokens: usage.PromptTokens, + CompletionTokens: usage.CompletionTokens, + ModelName: logModel, + TokenName: tokenName, + Quota: quota, + Content: logContent, + TokenId: relayInfo.TokenId, + UseTimeSeconds: int(useTimeSeconds), + IsStream: relayInfo.IsStream, + Group: relayInfo.UsingGroup, + Other: other, + }) +} + +func PreConsumeTokenQuota(relayInfo *relaycommon.RelayInfo, quota int) error { + if quota < 0 { + return errors.New("quota 不能为负数!") + } + if relayInfo.IsPlayground { + return nil + } + //if relayInfo.TokenUnlimited { + // return nil + //} + token, err := model.GetTokenByKey(relayInfo.TokenKey, false) + if err != nil { + return err + } + if !relayInfo.TokenUnlimited && token.RemainQuota < quota { + return fmt.Errorf("token quota is not enough, token remain quota: %s, need quota: %s", logger.FormatQuota(token.RemainQuota), logger.FormatQuota(quota)) + } + err = model.DecreaseTokenQuota(relayInfo.TokenId, relayInfo.TokenKey, quota) + if err != nil { + return err + } + return nil +} + +func PostConsumeQuota(relayInfo *relaycommon.RelayInfo, quota int, preConsumedQuota int, sendEmail bool) (err error) { + + // 1) Consume from wallet quota OR subscription item + if relayInfo != nil && relayInfo.BillingSource == BillingSourceSubscription { + if relayInfo.SubscriptionId == 0 { + return errors.New("subscription id is missing") + } + delta := int64(quota) + if delta != 0 { + if err := model.PostConsumeUserSubscriptionDelta(relayInfo.SubscriptionId, delta); err != nil { + return err + } + relayInfo.SubscriptionPostDelta += delta + } + } else { + // Wallet + if quota > 0 { + err = model.DecreaseUserQuota(relayInfo.UserId, quota, false) + } else { + err = model.IncreaseUserQuota(relayInfo.UserId, -quota, false) + } + if err != nil { + return err + } + } + + if !relayInfo.IsPlayground { + if quota > 0 { + err = model.DecreaseTokenQuota(relayInfo.TokenId, relayInfo.TokenKey, quota) + } else { + err = model.IncreaseTokenQuota(relayInfo.TokenId, relayInfo.TokenKey, -quota) + } + if err != nil { + return err + } + } + + if sendEmail { + if (quota + preConsumedQuota) != 0 { + checkAndSendQuotaNotify(relayInfo, quota, preConsumedQuota) + } + } + + return nil +} + +func checkAndSendQuotaNotify(relayInfo *relaycommon.RelayInfo, quota int, preConsumedQuota int) { + gopool.Go(func() { + userSetting := relayInfo.UserSetting + threshold := common.QuotaRemindThreshold + if userSetting.QuotaWarningThreshold != 0 { + threshold = int(userSetting.QuotaWarningThreshold) + } + + //noMoreQuota := userCache.Quota-(quota+preConsumedQuota) <= 0 + quotaTooLow := false + consumeQuota := quota + preConsumedQuota + if relayInfo.UserQuota-consumeQuota < threshold { + quotaTooLow = true + } + if quotaTooLow { + prompt := "您的额度即将用尽" + topUpLink := fmt.Sprintf("%s/console/topup", system_setting.ServerAddress) + + // 根据通知方式生成不同的内容格式 + var content string + var values []interface{} + + notifyType := userSetting.NotifyType + if notifyType == "" { + notifyType = dto.NotifyTypeEmail + } + + if notifyType == dto.NotifyTypeBark { + // Bark推送使用简短文本,不支持HTML + content = "{{value}},剩余额度:{{value}},请及时充值" + values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota)} + } else if notifyType == dto.NotifyTypeGotify { + content = "{{value}},当前剩余额度为 {{value}},请及时充值。" + values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota)} + } else { + // 默认内容格式,适用于Email和Webhook(支持HTML) + content = "{{value}},当前剩余额度为 {{value}},为了不影响您的使用,请及时充值。
充值链接:
{{value}}" + values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota), topUpLink, topUpLink} + } + + err := NotifyUser(relayInfo.UserId, relayInfo.UserEmail, relayInfo.UserSetting, dto.NewNotify(dto.NotifyTypeQuotaExceed, prompt, content, values)) + if err != nil { + common.SysError(fmt.Sprintf("failed to send quota notify to user %d: %s", relayInfo.UserId, err.Error())) + } + } + }) +} + +func checkAndSendSubscriptionQuotaNotify(relayInfo *relaycommon.RelayInfo) { + gopool.Go(func() { + if relayInfo == nil { + return + } + if relayInfo.SubscriptionId == 0 || relayInfo.SubscriptionAmountTotal <= 0 { + return + } + + userSetting := relayInfo.UserSetting + threshold := common.QuotaRemindThreshold + if userSetting.QuotaWarningThreshold != 0 { + threshold = int(userSetting.QuotaWarningThreshold) + } + + usedAfter := relayInfo.SubscriptionAmountUsedAfterPreConsume + relayInfo.SubscriptionPostDelta + remaining := relayInfo.SubscriptionAmountTotal - usedAfter + if remaining >= int64(threshold) { + return + } + + prompt := "您的订阅额度即将用尽" + topUpLink := fmt.Sprintf("%s/console/topup", system_setting.ServerAddress) + + var content string + var values []interface{} + notifyType := userSetting.NotifyType + if notifyType == "" { + notifyType = dto.NotifyTypeEmail + } + + if notifyType == dto.NotifyTypeBark { + content = "{{value}},剩余额度:{{value}},请及时充值" + values = []interface{}{prompt, logger.FormatQuota(int(remaining))} + } else if notifyType == dto.NotifyTypeGotify { + content = "{{value}},当前剩余额度为 {{value}},请及时充值。" + values = []interface{}{prompt, logger.FormatQuota(int(remaining))} + } else { + content = "{{value}},当前剩余额度为 {{value}},为了不影响您的使用,请及时充值。
充值链接:{{value}}" + values = []interface{}{prompt, logger.FormatQuota(int(remaining)), topUpLink, topUpLink} + } + + if err := NotifyUser(relayInfo.UserId, relayInfo.UserEmail, relayInfo.UserSetting, dto.NewNotify(dto.NotifyTypeQuotaExceed, prompt, content, values)); err != nil { + common.SysError(fmt.Sprintf("failed to send subscription quota notify to user %d: %s", relayInfo.UserId, err.Error())) + } + }) +} diff --git a/_all-in-one-upload/new-api/service/text_quota_test.go b/_all-in-one-upload/new-api/service/text_quota_test.go new file mode 100644 index 0000000..37ce187 --- /dev/null +++ b/_all-in-one-upload/new-api/service/text_quota_test.go @@ -0,0 +1,441 @@ +package service + +import ( + "net/http/httptest" + "testing" + "time" + + "github.com/QuantumNous/new-api/constant" + "github.com/QuantumNous/new-api/dto" + "github.com/QuantumNous/new-api/pkg/billingexpr" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/types" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/require" +) + +func TestCalculateTextQuotaSummaryUnifiedForClaudeSemantic(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + usage := &dto.Usage{ + PromptTokens: 1000, + CompletionTokens: 200, + PromptTokensDetails: dto.InputTokenDetails{ + CachedTokens: 100, + CachedCreationTokens: 50, + }, + ClaudeCacheCreation5mTokens: 10, + ClaudeCacheCreation1hTokens: 20, + } + + priceData := types.PriceData{ + ModelRatio: 1, + CompletionRatio: 2, + CacheRatio: 0.1, + CacheCreationRatio: 1.25, + CacheCreation5mRatio: 1.25, + CacheCreation1hRatio: 2, + GroupRatioInfo: types.GroupRatioInfo{ + GroupRatio: 1, + }, + } + + chatRelayInfo := &relaycommon.RelayInfo{ + RelayFormat: types.RelayFormatOpenAI, + FinalRequestRelayFormat: types.RelayFormatClaude, + OriginModelName: "claude-3-7-sonnet", + PriceData: priceData, + StartTime: time.Now(), + } + messageRelayInfo := &relaycommon.RelayInfo{ + RelayFormat: types.RelayFormatClaude, + FinalRequestRelayFormat: types.RelayFormatClaude, + OriginModelName: "claude-3-7-sonnet", + PriceData: priceData, + StartTime: time.Now(), + } + + chatSummary := calculateTextQuotaSummary(ctx, chatRelayInfo, usage) + messageSummary := calculateTextQuotaSummary(ctx, messageRelayInfo, usage) + + require.Equal(t, messageSummary.Quota, chatSummary.Quota) + require.Equal(t, messageSummary.CacheCreationTokens5m, chatSummary.CacheCreationTokens5m) + require.Equal(t, messageSummary.CacheCreationTokens1h, chatSummary.CacheCreationTokens1h) + require.True(t, chatSummary.IsClaudeUsageSemantic) + require.Equal(t, 1488, chatSummary.Quota) +} + +func TestCalculateTextQuotaSummaryUsesSplitClaudeCacheCreationRatios(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + relayInfo := &relaycommon.RelayInfo{ + RelayFormat: types.RelayFormatOpenAI, + FinalRequestRelayFormat: types.RelayFormatClaude, + OriginModelName: "claude-3-7-sonnet", + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + CacheRatio: 0, + CacheCreationRatio: 1, + CacheCreation5mRatio: 2, + CacheCreation1hRatio: 3, + GroupRatioInfo: types.GroupRatioInfo{ + GroupRatio: 1, + }, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 100, + CompletionTokens: 0, + PromptTokensDetails: dto.InputTokenDetails{ + CachedCreationTokens: 10, + }, + ClaudeCacheCreation5mTokens: 2, + ClaudeCacheCreation1hTokens: 3, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + // 100 + remaining(5)*1 + 2*2 + 3*3 = 118 + require.Equal(t, 118, summary.Quota) +} + +func TestCalculateTextQuotaSummaryUsesAnthropicUsageSemanticFromUpstreamUsage(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + relayInfo := &relaycommon.RelayInfo{ + RelayFormat: types.RelayFormatOpenAI, + OriginModelName: "claude-3-7-sonnet", + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 2, + CacheRatio: 0.1, + CacheCreationRatio: 1.25, + CacheCreation5mRatio: 1.25, + CacheCreation1hRatio: 2, + GroupRatioInfo: types.GroupRatioInfo{ + GroupRatio: 1, + }, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 1000, + CompletionTokens: 200, + UsageSemantic: "anthropic", + PromptTokensDetails: dto.InputTokenDetails{ + CachedTokens: 100, + CachedCreationTokens: 50, + }, + ClaudeCacheCreation5mTokens: 10, + ClaudeCacheCreation1hTokens: 20, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + require.True(t, summary.IsClaudeUsageSemantic) + require.Equal(t, "anthropic", summary.UsageSemantic) + require.Equal(t, 1488, summary.Quota) +} + +func TestCacheWriteTokensTotal(t *testing.T) { + t.Run("split cache creation", func(t *testing.T) { + summary := textQuotaSummary{ + CacheCreationTokens: 50, + CacheCreationTokens5m: 10, + CacheCreationTokens1h: 20, + } + require.Equal(t, 50, cacheWriteTokensTotal(summary)) + }) + + t.Run("legacy cache creation", func(t *testing.T) { + summary := textQuotaSummary{CacheCreationTokens: 50} + require.Equal(t, 50, cacheWriteTokensTotal(summary)) + }) + + t.Run("split cache creation without aggregate remainder", func(t *testing.T) { + summary := textQuotaSummary{ + CacheCreationTokens5m: 10, + CacheCreationTokens1h: 20, + } + require.Equal(t, 30, cacheWriteTokensTotal(summary)) + }) +} + +func TestCalculateTextQuotaSummaryHandlesLegacyClaudeDerivedOpenAIUsage(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + relayInfo := &relaycommon.RelayInfo{ + RelayFormat: types.RelayFormatOpenAI, + OriginModelName: "claude-3-7-sonnet", + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 5, + CacheRatio: 0.1, + CacheCreationRatio: 1.25, + CacheCreation5mRatio: 1.25, + CacheCreation1hRatio: 2, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1}, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 62, + CompletionTokens: 95, + PromptTokensDetails: dto.InputTokenDetails{ + CachedTokens: 3544, + }, + ClaudeCacheCreation5mTokens: 586, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + // 62 + 3544*0.1 + 586*1.25 + 95*5 = 1624.9 => 1624 + require.Equal(t, 1624, summary.Quota) +} + +func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheReadFromPromptBilling(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + relayInfo := &relaycommon.RelayInfo{ + OriginModelName: "openai/gpt-4.1", + ChannelMeta: &relaycommon.ChannelMeta{ + ChannelType: constant.ChannelTypeOpenRouter, + }, + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + CacheRatio: 0.1, + CacheCreationRatio: 1.25, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1}, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 2604, + CompletionTokens: 383, + PromptTokensDetails: dto.InputTokenDetails{ + CachedTokens: 2432, + }, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + // OpenRouter OpenAI-format display keeps prompt_tokens as total input, + // but billing still separates normal input from cache read tokens. + // quota = (2604 - 2432) + 2432*0.1 + 383 = 798.2 => 798 + require.Equal(t, 2604, summary.PromptTokens) + require.Equal(t, 798, summary.Quota) +} + +func TestCalculateTextQuotaSummarySeparatesOpenRouterCacheCreationFromPromptBilling(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + relayInfo := &relaycommon.RelayInfo{ + OriginModelName: "openai/gpt-4.1", + ChannelMeta: &relaycommon.ChannelMeta{ + ChannelType: constant.ChannelTypeOpenRouter, + }, + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + CacheCreationRatio: 1.25, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1}, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 2604, + CompletionTokens: 383, + PromptTokensDetails: dto.InputTokenDetails{ + CachedCreationTokens: 100, + }, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + // prompt_tokens is still logged as total input, but cache creation is billed separately. + // quota = (2604 - 100) + 100*1.25 + 383 = 3012 + require.Equal(t, 2604, summary.PromptTokens) + require.Equal(t, 3012, summary.Quota) +} + +func TestCalculateTextQuotaSummaryKeepsPrePRClaudeOpenRouterBilling(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + + relayInfo := &relaycommon.RelayInfo{ + FinalRequestRelayFormat: types.RelayFormatClaude, + OriginModelName: "anthropic/claude-3.7-sonnet", + ChannelMeta: &relaycommon.ChannelMeta{ + ChannelType: constant.ChannelTypeOpenRouter, + }, + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + CacheRatio: 0.1, + CacheCreationRatio: 1.25, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1}, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 2604, + CompletionTokens: 383, + PromptTokensDetails: dto.InputTokenDetails{ + CachedTokens: 2432, + }, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + // Pre-PR PostClaudeConsumeQuota behavior for OpenRouter: + // prompt = 2604 - 2432 = 172 + // quota = 172 + 2432*0.1 + 383 = 798.2 => 798 + require.True(t, summary.IsClaudeUsageSemantic) + require.Equal(t, 172, summary.PromptTokens) + require.Equal(t, 798, summary.Quota) +} + +func TestComposeTieredTextQuotaKeepsToolCallSurcharges(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + ctx.Set("image_generation_call", true) + ctx.Set("image_generation_call_quality", "low") + ctx.Set("image_generation_call_size", "1024x1024") + + relayInfo := &relaycommon.RelayInfo{ + OriginModelName: "o1", + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1}, + }, + ResponsesUsageInfo: &relaycommon.ResponsesUsageInfo{ + BuiltInTools: map[string]*relaycommon.BuildInToolInfo{ + dto.BuildInToolWebSearchPreview: &relaycommon.BuildInToolInfo{ + CallCount: 1, + }, + dto.BuildInToolFileSearch: &relaycommon.BuildInToolInfo{ + CallCount: 2, + }, + }, + }, + TieredBillingSnapshot: &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + GroupRatio: 1, + EstimatedQuotaBeforeGroup: 1000, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 100, + CompletionTokens: 50, + TotalTokens: 150, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + quota := composeTieredTextQuota(relayInfo, summary, 1000, &billingexpr.TieredResult{ + ActualQuotaBeforeGroup: 1000, + ActualQuotaAfterGroup: 1000, + }) + + require.Equal(t, int64(13000), summary.ToolCallSurchargeQuota.Round(0).IntPart()) + require.Equal(t, 14000, quota) +} + +func TestComposeTieredTextQuotaFallbackKeepsToolCallSurcharges(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + ctx.Set("claude_web_search_requests", 2) + + relayInfo := &relaycommon.RelayInfo{ + OriginModelName: "claude-3-7-sonnet", + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1.25}, + }, + TieredBillingSnapshot: &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + GroupRatio: 1.25, + EstimatedQuotaBeforeGroup: 1000, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 100, + CompletionTokens: 50, + TotalTokens: 150, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + quota := composeTieredTextQuota(relayInfo, summary, 1250, nil) + + require.Equal(t, int64(12500), summary.ToolCallSurchargeQuota.Round(0).IntPart()) + require.Equal(t, 13750, quota) +} + +func TestComposeTieredTextQuotaErrorFallbackUsesPreConsumedQuota(t *testing.T) { + gin.SetMode(gin.TestMode) + w := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(w) + ctx.Set("claude_web_search_requests", 2) + + relayInfo := &relaycommon.RelayInfo{ + OriginModelName: "claude-3-7-sonnet", + PriceData: types.PriceData{ + ModelRatio: 1, + CompletionRatio: 1, + GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1.25}, + }, + TieredBillingSnapshot: &billingexpr.BillingSnapshot{ + BillingMode: "tiered_expr", + GroupRatio: 1.25, + EstimatedQuotaBeforeGroup: 1000, + }, + StartTime: time.Now(), + } + + usage := &dto.Usage{ + PromptTokens: 100, + CompletionTokens: 50, + TotalTokens: 150, + } + + summary := calculateTextQuotaSummary(ctx, relayInfo, usage) + + // tieredResult=nil simulates a settlement error where TryTieredSettle + // falls back to FinalPreConsumedQuota (2000), which differs from + // EstimatedQuotaBeforeGroup * GroupRatio (1250). + preConsumedFallback := 2000 + quota := composeTieredTextQuota(relayInfo, summary, preConsumedFallback, nil) + + require.Equal(t, int64(12500), summary.ToolCallSurchargeQuota.Round(0).IntPart()) + require.Equal(t, 14500, quota) +} diff --git a/_all-in-one-upload/new-api/service/violation_fee.go b/_all-in-one-upload/new-api/service/violation_fee.go new file mode 100644 index 0000000..4550885 --- /dev/null +++ b/_all-in-one-upload/new-api/service/violation_fee.go @@ -0,0 +1,164 @@ +package service + +import ( + "fmt" + "strings" + "time" + + "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/logger" + "github.com/QuantumNous/new-api/model" + relaycommon "github.com/QuantumNous/new-api/relay/common" + "github.com/QuantumNous/new-api/setting/model_setting" + "github.com/QuantumNous/new-api/types" + + "github.com/shopspring/decimal" + + "github.com/gin-gonic/gin" +) + +const ( + ViolationFeeCodePrefix = "violation_fee." + CSAMViolationMarker = "Failed check: SAFETY_CHECK_TYPE" + ContentViolatesUsageMarker = "Content violates usage guidelines" +) + +func IsViolationFeeCode(code types.ErrorCode) bool { + return strings.HasPrefix(string(code), ViolationFeeCodePrefix) +} + +func HasCSAMViolationMarker(err *types.NewAPIError) bool { + if err == nil { + return false + } + if strings.Contains(err.Error(), CSAMViolationMarker) || strings.Contains(err.Error(), ContentViolatesUsageMarker) { + return true + } + msg := err.ToOpenAIError().Message + return strings.Contains(msg, CSAMViolationMarker) || strings.Contains(err.Error(), ContentViolatesUsageMarker) +} + +func WrapAsViolationFeeGrokCSAM(err *types.NewAPIError) *types.NewAPIError { + if err == nil { + return nil + } + oai := err.ToOpenAIError() + oai.Type = string(types.ErrorCodeViolationFeeGrokCSAM) + oai.Code = string(types.ErrorCodeViolationFeeGrokCSAM) + return types.WithOpenAIError(oai, err.StatusCode, types.ErrOptionWithSkipRetry()) +} + +// NormalizeViolationFeeError ensures: +// - if the CSAM marker is present, error.code is set to a stable violation-fee code and skip-retry is enabled. +// - if error.code already has the violation-fee prefix, skip-retry is enabled. +// +// It must be called before retry decision logic. +func NormalizeViolationFeeError(err *types.NewAPIError) *types.NewAPIError { + if err == nil { + return nil + } + + if HasCSAMViolationMarker(err) { + return WrapAsViolationFeeGrokCSAM(err) + } + + if IsViolationFeeCode(err.GetErrorCode()) { + oai := err.ToOpenAIError() + return types.WithOpenAIError(oai, err.StatusCode, types.ErrOptionWithSkipRetry()) + } + + return err +} + +func shouldChargeViolationFee(err *types.NewAPIError) bool { + if err == nil { + return false + } + if err.GetErrorCode() == types.ErrorCodeViolationFeeGrokCSAM { + return true + } + // In case some callers didn't normalize, keep a safety net. + return HasCSAMViolationMarker(err) +} + +func calcViolationFeeQuota(amount, groupRatio float64) int { + if amount <= 0 { + return 0 + } + if groupRatio <= 0 { + return 0 + } + quota := decimal.NewFromFloat(amount). + Mul(decimal.NewFromFloat(common.QuotaPerUnit)). + Mul(decimal.NewFromFloat(groupRatio)). + Round(0). + IntPart() + if quota <= 0 { + return 0 + } + return int(quota) +} + +// ChargeViolationFeeIfNeeded charges an additional fee after the normal flow finishes (including refund). +// It uses Grok fee settings as the fee policy. +func ChargeViolationFeeIfNeeded(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, apiErr *types.NewAPIError) bool { + if ctx == nil || relayInfo == nil || apiErr == nil { + return false + } + //if relayInfo.IsPlayground { + // return false + //} + if !shouldChargeViolationFee(apiErr) { + return false + } + + settings := model_setting.GetGrokSettings() + if settings == nil || !settings.ViolationDeductionEnabled { + return false + } + + groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio + feeQuota := calcViolationFeeQuota(settings.ViolationDeductionAmount, groupRatio) + if feeQuota <= 0 { + return false + } + + if err := PostConsumeQuota(relayInfo, feeQuota, 0, true); err != nil { + logger.LogError(ctx, fmt.Sprintf("failed to charge violation fee: %s", err.Error())) + return false + } + + model.UpdateUserUsedQuotaAndRequestCount(relayInfo.UserId, feeQuota) + model.UpdateChannelUsedQuota(relayInfo.ChannelId, feeQuota) + + useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix() + tokenName := ctx.GetString("token_name") + oai := apiErr.ToOpenAIError() + + other := map[string]any{ + "violation_fee": true, + "violation_fee_code": string(types.ErrorCodeViolationFeeGrokCSAM), + "fee_quota": feeQuota, + "base_amount": settings.ViolationDeductionAmount, + "group_ratio": groupRatio, + "status_code": apiErr.StatusCode, + "upstream_error_type": oai.Type, + "upstream_error_code": fmt.Sprintf("%v", oai.Code), + "violation_fee_marker": CSAMViolationMarker, + } + + model.RecordConsumeLog(ctx, relayInfo.UserId, model.RecordConsumeLogParams{ + ChannelId: relayInfo.ChannelId, + ModelName: relayInfo.OriginModelName, + TokenName: tokenName, + Quota: feeQuota, + Content: "Violation fee charged", + TokenId: relayInfo.TokenId, + UseTimeSeconds: int(useTimeSeconds), + IsStream: relayInfo.IsStream, + Group: relayInfo.UsingGroup, + Other: other, + }) + + return true +} diff --git a/_all-in-one-upload/new-api/setting/model_setting/claude_test.go b/_all-in-one-upload/new-api/setting/model_setting/claude_test.go new file mode 100644 index 0000000..0a806a7 --- /dev/null +++ b/_all-in-one-upload/new-api/setting/model_setting/claude_test.go @@ -0,0 +1,60 @@ +package model_setting + +import ( + "net/http" + "testing" +) + +func TestClaudeSettingsWriteHeadersMergesConfiguredValuesIntoSingleHeader(t *testing.T) { + settings := &ClaudeSettings{ + HeadersSettings: map[string]map[string][]string{ + "claude-3-7-sonnet-20250219-thinking": { + "anthropic-beta": { + "token-efficient-tools-2025-02-19", + }, + }, + }, + } + + headers := http.Header{} + headers.Set("anthropic-beta", "output-128k-2025-02-19") + + settings.WriteHeaders("claude-3-7-sonnet-20250219-thinking", &headers) + + got := headers.Values("anthropic-beta") + if len(got) != 1 { + t.Fatalf("expected a single merged header value, got %v", got) + } + expected := "output-128k-2025-02-19,token-efficient-tools-2025-02-19" + if got[0] != expected { + t.Fatalf("expected merged header %q, got %q", expected, got[0]) + } +} + +func TestClaudeSettingsWriteHeadersDeduplicatesAcrossCommaSeparatedAndRepeatedValues(t *testing.T) { + settings := &ClaudeSettings{ + HeadersSettings: map[string]map[string][]string{ + "claude-3-7-sonnet-20250219-thinking": { + "anthropic-beta": { + "token-efficient-tools-2025-02-19", + "computer-use-2025-01-24", + }, + }, + }, + } + + headers := http.Header{} + headers.Add("anthropic-beta", "output-128k-2025-02-19, token-efficient-tools-2025-02-19") + headers.Add("anthropic-beta", "token-efficient-tools-2025-02-19") + + settings.WriteHeaders("claude-3-7-sonnet-20250219-thinking", &headers) + + got := headers.Values("anthropic-beta") + if len(got) != 1 { + t.Fatalf("expected duplicate values to collapse into one header, got %v", got) + } + expected := "output-128k-2025-02-19,token-efficient-tools-2025-02-19,computer-use-2025-01-24" + if got[0] != expected { + t.Fatalf("expected deduplicated merged header %q, got %q", expected, got[0]) + } +} diff --git a/_all-in-one-upload/new-api/setting/operation_setting/channel_affinity_setting.go b/_all-in-one-upload/new-api/setting/operation_setting/channel_affinity_setting.go new file mode 100644 index 0000000..8cd605c --- /dev/null +++ b/_all-in-one-upload/new-api/setting/operation_setting/channel_affinity_setting.go @@ -0,0 +1,121 @@ +package operation_setting + +import "github.com/QuantumNous/new-api/setting/config" + +type ChannelAffinityKeySource struct { + Type string `json:"type"` // context_int, context_string, gjson + Key string `json:"key,omitempty"` + Path string `json:"path,omitempty"` +} + +type ChannelAffinityRule struct { + Name string `json:"name"` + ModelRegex []string `json:"model_regex"` + PathRegex []string `json:"path_regex"` + UserAgentInclude []string `json:"user_agent_include,omitempty"` + KeySources []ChannelAffinityKeySource `json:"key_sources"` + + ValueRegex string `json:"value_regex"` + TTLSeconds int `json:"ttl_seconds"` + + ParamOverrideTemplate map[string]interface{} `json:"param_override_template,omitempty"` + + SkipRetryOnFailure bool `json:"skip_retry_on_failure"` + + IncludeUsingGroup bool `json:"include_using_group"` + IncludeModelName bool `json:"include_model_name"` + IncludeRuleName bool `json:"include_rule_name"` +} + +type ChannelAffinitySetting struct { + Enabled bool `json:"enabled"` + SwitchOnSuccess bool `json:"switch_on_success"` + MaxEntries int `json:"max_entries"` + DefaultTTLSeconds int `json:"default_ttl_seconds"` + Rules []ChannelAffinityRule `json:"rules"` +} + +var codexCliPassThroughHeaders = []string{ + "Originator", + "Session_id", + "User-Agent", + "X-Codex-Beta-Features", + "X-Codex-Turn-Metadata", +} + +var claudeCliPassThroughHeaders = []string{ + "X-Stainless-Arch", + "X-Stainless-Lang", + "X-Stainless-Os", + "X-Stainless-Package-Version", + "X-Stainless-Retry-Count", + "X-Stainless-Runtime", + "X-Stainless-Runtime-Version", + "X-Stainless-Timeout", + "User-Agent", + "X-App", + "Anthropic-Beta", + "Anthropic-Dangerous-Direct-Browser-Access", + "Anthropic-Version", +} + +func buildPassHeaderTemplate(headers []string) map[string]interface{} { + clonedHeaders := make([]string, 0, len(headers)) + clonedHeaders = append(clonedHeaders, headers...) + return map[string]interface{}{ + "operations": []map[string]interface{}{ + { + "mode": "pass_headers", + "value": clonedHeaders, + "keep_origin": true, + }, + }, + } +} + +var channelAffinitySetting = ChannelAffinitySetting{ + Enabled: true, + SwitchOnSuccess: true, + MaxEntries: 100_000, + DefaultTTLSeconds: 3600, + Rules: []ChannelAffinityRule{ + { + Name: "codex cli trace", + ModelRegex: []string{"^gpt-.*$"}, + PathRegex: []string{"/v1/responses"}, + KeySources: []ChannelAffinityKeySource{ + {Type: "gjson", Path: "prompt_cache_key"}, + }, + ValueRegex: "", + TTLSeconds: 0, + ParamOverrideTemplate: buildPassHeaderTemplate(codexCliPassThroughHeaders), + SkipRetryOnFailure: true, + IncludeUsingGroup: true, + IncludeRuleName: true, + UserAgentInclude: nil, + }, + { + Name: "claude cli trace", + ModelRegex: []string{"^claude-.*$"}, + PathRegex: []string{"/v1/messages"}, + KeySources: []ChannelAffinityKeySource{ + {Type: "gjson", Path: "metadata.user_id"}, + }, + ValueRegex: "", + TTLSeconds: 0, + ParamOverrideTemplate: buildPassHeaderTemplate(claudeCliPassThroughHeaders), + SkipRetryOnFailure: true, + IncludeUsingGroup: true, + IncludeRuleName: true, + UserAgentInclude: nil, + }, + }, +} + +func init() { + config.GlobalConfig.Register("channel_affinity_setting", &channelAffinitySetting) +} + +func GetChannelAffinitySetting() *ChannelAffinitySetting { + return &channelAffinitySetting +} diff --git a/_all-in-one-upload/new-api/setting/payment_stripe.go b/_all-in-one-upload/new-api/setting/payment_stripe.go new file mode 100644 index 0000000..d97120c --- /dev/null +++ b/_all-in-one-upload/new-api/setting/payment_stripe.go @@ -0,0 +1,8 @@ +package setting + +var StripeApiSecret = "" +var StripeWebhookSecret = "" +var StripePriceId = "" +var StripeUnitPrice = 8.0 +var StripeMinTopUp = 1 +var StripePromotionCodesEnabled = false diff --git a/_all-in-one-upload/new-api/types/price_data.go b/_all-in-one-upload/new-api/types/price_data.go new file mode 100644 index 0000000..93bc6ae --- /dev/null +++ b/_all-in-one-upload/new-api/types/price_data.go @@ -0,0 +1,42 @@ +package types + +import "fmt" + +type GroupRatioInfo struct { + GroupRatio float64 + GroupSpecialRatio float64 + HasSpecialRatio bool +} + +type PriceData struct { + FreeModel bool + ModelPrice float64 + ModelRatio float64 + CompletionRatio float64 + CacheRatio float64 + CacheCreationRatio float64 + CacheCreation5mRatio float64 + CacheCreation1hRatio float64 + ImageRatio float64 + AudioRatio float64 + AudioCompletionRatio float64 + OtherRatios map[string]float64 + UsePrice bool + Quota int // 按次计费的最终额度(MJ / Task) + QuotaToPreConsume int // 按量计费的预消耗额度 + GroupRatioInfo GroupRatioInfo +} + +func (p *PriceData) AddOtherRatio(key string, ratio float64) { + if p.OtherRatios == nil { + p.OtherRatios = make(map[string]float64) + } + if ratio <= 0 { + return + } + p.OtherRatios[key] = ratio +} + +func (p *PriceData) ToSetting() string { + return fmt.Sprintf("ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, CacheCreation5mRatio: %f, CacheCreation1hRatio: %f, QuotaToPreConsume: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f", p.ModelPrice, p.ModelRatio, p.CompletionRatio, p.CacheRatio, p.GroupRatioInfo.GroupRatio, p.UsePrice, p.CacheCreationRatio, p.CacheCreation5mRatio, p.CacheCreation1hRatio, p.QuotaToPreConsume, p.ImageRatio, p.AudioRatio, p.AudioCompletionRatio) +} diff --git a/_all-in-one-upload/new-api/web/classic/package.json b/_all-in-one-upload/new-api/web/classic/package.json new file mode 100644 index 0000000..83b5d23 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/package.json @@ -0,0 +1,96 @@ +{ + "name": "react-template", + "version": "0.1.0", + "private": true, + "type": "module", + "dependencies": { + "@douyinfe/semi-icons": "^2.63.1", + "@douyinfe/semi-ui": "^2.69.1", + "@lobehub/icons": "^2.0.0", + "@visactor/react-vchart": "~1.8.8", + "@visactor/vchart": "~1.8.8", + "@visactor/vchart-semi-theme": "~1.8.8", + "axios": "1.15.0", + "clsx": "^2.1.1", + "dayjs": "^1.11.11", + "history": "^5.3.0", + "i18next": "^23.16.8", + "i18next-browser-languagedetector": "^7.2.0", + "katex": "^0.16.22", + "lucide-react": "^0.511.0", + "marked": "^4.1.1", + "mermaid": "^11.6.0", + "qrcode.react": "^4.2.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-dropzone": "^14.2.3", + "react-fireworks": "^1.0.4", + "react-i18next": "^13.0.0", + "react-icons": "^5.5.0", + "react-markdown": "^10.1.0", + "react-router-dom": "^6.3.0", + "react-telegram-login": "^1.1.2", + "react-toastify": "^9.0.8", + "react-turnstile": "^1.0.5", + "rehype-highlight": "^7.0.2", + "rehype-katex": "^7.0.1", + "remark-breaks": "^4.0.0", + "remark-gfm": "^4.0.1", + "remark-math": "^6.0.0", + "sse.js": "^2.6.0", + "unist-util-visit": "^5.0.0", + "use-debounce": "^10.0.4" + }, + "scripts": { + "dev": "vite", + "build": "vite build", + "lint": "prettier . --check", + "lint:fix": "prettier . --write", + "eslint": "bunx eslint \"**/*.{js,jsx}\" --cache", + "eslint:fix": "bunx eslint \"**/*.{js,jsx}\" --fix --cache", + "preview": "vite preview", + "i18n:extract": "bunx i18next-cli extract", + "i18n:status": "bunx i18next-cli status", + "i18n:sync": "bunx i18next-cli sync", + "i18n:lint": "bunx i18next-cli lint" + }, + "eslintConfig": { + "extends": [ + "react-app", + "react-app/jest" + ] + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + }, + "devDependencies": { + "@douyinfe/vite-plugin-semi": "^2.74.0-alpha.6", + "@so1ve/prettier-config": "^3.1.0", + "@vitejs/plugin-react": "^4.2.1", + "autoprefixer": "^10.4.21", + "code-inspector-plugin": "^1.3.3", + "eslint": "8.57.0", + "eslint-plugin-header": "^3.1.1", + "eslint-plugin-react-hooks": "^5.2.0", + "i18next-cli": "^1.10.3", + "postcss": "^8.5.3", + "prettier": "^3.0.0", + "tailwindcss": "^3", + "typescript": "4.4.2", + "vite": "^5.2.0" + }, + "prettier": { + "singleQuote": true, + "jsxSingleQuote": true + }, + "proxy": "http://localhost:3000" +} diff --git a/_all-in-one-upload/new-api/web/classic/src/components/common/logo/OIDCIcon.jsx b/_all-in-one-upload/new-api/web/classic/src/components/common/logo/OIDCIcon.jsx new file mode 100644 index 0000000..28d538e --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/common/logo/OIDCIcon.jsx @@ -0,0 +1,57 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React from 'react'; +import { Icon } from '@douyinfe/semi-ui'; + +const OIDCIcon = (props) => { + function CustomIcon() { + return ( + + + + + ); + } + + return } />; +}; + +export default OIDCIcon; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/common/logo/WeChatIcon.jsx b/_all-in-one-upload/new-api/web/classic/src/components/common/logo/WeChatIcon.jsx new file mode 100644 index 0000000..f9f7057 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/common/logo/WeChatIcon.jsx @@ -0,0 +1,55 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React from 'react'; +import { Icon } from '@douyinfe/semi-ui'; + +const WeChatIcon = () => { + function CustomIcon() { + return ( + + + + + ); + } + + return ( +
+ } /> +
+ ); +}; + +export default WeChatIcon; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/layout/NoticeModal.jsx b/_all-in-one-upload/new-api/web/classic/src/components/layout/NoticeModal.jsx new file mode 100644 index 0000000..c8197a5 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/layout/NoticeModal.jsx @@ -0,0 +1,255 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useEffect, useState, useContext, useMemo } from 'react'; +import { + Button, + Modal, + Empty, + Tabs, + TabPane, + Timeline, +} from '@douyinfe/semi-ui'; +import { useTranslation } from 'react-i18next'; +import { API, showError, getRelativeTime } from '../../helpers'; +import { marked } from 'marked'; +import { + IllustrationNoContent, + IllustrationNoContentDark, +} from '@douyinfe/semi-illustrations'; +import { StatusContext } from '../../context/Status'; +import { Bell, Megaphone } from 'lucide-react'; + +const NoticeModal = ({ + visible, + onClose, + isMobile, + defaultTab = 'inApp', + unreadKeys = [], +}) => { + const { t } = useTranslation(); + const [noticeContent, setNoticeContent] = useState(''); + const [loading, setLoading] = useState(false); + const [activeTab, setActiveTab] = useState(defaultTab); + + const [statusState] = useContext(StatusContext); + + const announcements = statusState?.status?.announcements || []; + + const unreadSet = useMemo(() => new Set(unreadKeys), [unreadKeys]); + + const getKeyForItem = (item) => + `${item?.publishDate || ''}-${(item?.content || '').slice(0, 30)}`; + + const processedAnnouncements = useMemo(() => { + return (announcements || []).slice(0, 20).map((item) => { + const pubDate = item?.publishDate ? new Date(item.publishDate) : null; + const absoluteTime = + pubDate && !isNaN(pubDate.getTime()) + ? `${pubDate.getFullYear()}-${String(pubDate.getMonth() + 1).padStart(2, '0')}-${String(pubDate.getDate()).padStart(2, '0')} ${String(pubDate.getHours()).padStart(2, '0')}:${String(pubDate.getMinutes()).padStart(2, '0')}` + : item?.publishDate || ''; + return { + key: getKeyForItem(item), + type: item.type || 'default', + time: absoluteTime, + content: item.content, + extra: item.extra, + relative: getRelativeTime(item.publishDate), + isUnread: unreadSet.has(getKeyForItem(item)), + }; + }); + }, [announcements, unreadSet]); + + const handleCloseTodayNotice = () => { + const today = new Date().toDateString(); + localStorage.setItem('notice_close_date', today); + onClose(); + }; + + const displayNotice = async () => { + setLoading(true); + try { + const res = await API.get('/api/notice'); + const { success, message, data } = res.data; + if (success) { + if (data !== '') { + const htmlNotice = marked.parse(data); + setNoticeContent(htmlNotice); + } else { + setNoticeContent(''); + } + } else { + showError(message); + } + } catch (error) { + showError(error.message); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + if (visible) { + displayNotice(); + } + }, [visible]); + + useEffect(() => { + if (visible) { + setActiveTab(defaultTab); + } + }, [defaultTab, visible]); + + const renderMarkdownNotice = () => { + if (loading) { + return ( +
+ +
+ ); + } + + if (!noticeContent) { + return ( +
+ + } + darkModeImage={ + + } + description={t('暂无公告')} + /> +
+ ); + } + + return ( +
+ ); + }; + + const renderAnnouncementTimeline = () => { + if (processedAnnouncements.length === 0) { + return ( +
+ + } + darkModeImage={ + + } + description={t('暂无系统公告')} + /> +
+ ); + } + + return ( +
+ + {processedAnnouncements.map((item, idx) => { + const htmlContent = marked.parse(item.content || ''); + const htmlExtra = item.extra ? marked.parse(item.extra) : ''; + return ( + + ) : null + } + className={item.isUnread ? '' : ''} + > +
+
+
+ + ); + })} + +
+ ); + }; + + const renderBody = () => { + if (activeTab === 'inApp') { + return renderMarkdownNotice(); + } + return renderAnnouncementTimeline(); + }; + + return ( + + {t('系统公告')} + + + {t('通知')} + + } + itemKey='inApp' + /> + + {t('系统公告')} + + } + itemKey='system' + /> + +
+ } + visible={visible} + onCancel={onClose} + footer={ +
+ + +
+ } + size={isMobile ? 'full-width' : 'large'} + > + {renderBody()} + + ); +}; + +export default NoticeModal; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/playground/configStorage.js b/_all-in-one-upload/new-api/web/classic/src/components/playground/configStorage.js new file mode 100644 index 0000000..d201ed6 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/playground/configStorage.js @@ -0,0 +1,234 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import { + STORAGE_KEYS, + DEFAULT_CONFIG, +} from '../../constants/playground.constants'; + +const MESSAGES_STORAGE_KEY = 'playground_messages'; + +/** + * 保存配置到 localStorage + * @param {Object} config - 要保存的配置对象 + */ +export const saveConfig = (config) => { + try { + const configToSave = { + ...config, + timestamp: new Date().toISOString(), + }; + localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(configToSave)); + } catch (error) { + console.error('保存配置失败:', error); + } +}; + +/** + * 保存消息到 localStorage + * @param {Array} messages - 要保存的消息数组 + */ +export const saveMessages = (messages) => { + try { + const messagesToSave = { + messages, + timestamp: new Date().toISOString(), + }; + localStorage.setItem(STORAGE_KEYS.MESSAGES, JSON.stringify(messagesToSave)); + } catch (error) { + console.error('保存消息失败:', error); + } +}; + +/** + * 从 localStorage 加载配置 + * @returns {Object} 配置对象,如果不存在则返回默认配置 + */ +export const loadConfig = () => { + try { + const savedConfig = localStorage.getItem(STORAGE_KEYS.CONFIG); + if (savedConfig) { + const parsedConfig = JSON.parse(savedConfig); + const parsedMaxTokens = parseInt(parsedConfig?.inputs?.max_tokens, 10); + + const mergedConfig = { + inputs: { + ...DEFAULT_CONFIG.inputs, + ...parsedConfig.inputs, + max_tokens: Number.isNaN(parsedMaxTokens) + ? parsedConfig?.inputs?.max_tokens + : parsedMaxTokens, + }, + parameterEnabled: { + ...DEFAULT_CONFIG.parameterEnabled, + ...parsedConfig.parameterEnabled, + }, + showDebugPanel: + parsedConfig.showDebugPanel || DEFAULT_CONFIG.showDebugPanel, + customRequestMode: + parsedConfig.customRequestMode || DEFAULT_CONFIG.customRequestMode, + customRequestBody: + parsedConfig.customRequestBody || DEFAULT_CONFIG.customRequestBody, + }; + + return mergedConfig; + } + } catch (error) { + console.error('加载配置失败:', error); + } + + return DEFAULT_CONFIG; +}; + +/** + * 从 localStorage 加载消息 + * @returns {Array} 消息数组,如果不存在则返回 null + */ +export const loadMessages = () => { + try { + const savedMessages = localStorage.getItem(STORAGE_KEYS.MESSAGES); + if (savedMessages) { + const parsedMessages = JSON.parse(savedMessages); + return parsedMessages.messages || null; + } + } catch (error) { + console.error('加载消息失败:', error); + } + + return null; +}; + +/** + * 清除保存的配置 + */ +export const clearConfig = () => { + try { + localStorage.removeItem(STORAGE_KEYS.CONFIG); + localStorage.removeItem(STORAGE_KEYS.MESSAGES); // 同时清除消息 + } catch (error) { + console.error('清除配置失败:', error); + } +}; + +/** + * 清除保存的消息 + */ +export const clearMessages = () => { + try { + localStorage.removeItem(STORAGE_KEYS.MESSAGES); + } catch (error) { + console.error('清除消息失败:', error); + } +}; + +/** + * 检查是否有保存的配置 + * @returns {boolean} 是否存在保存的配置 + */ +export const hasStoredConfig = () => { + try { + return localStorage.getItem(STORAGE_KEYS.CONFIG) !== null; + } catch (error) { + console.error('检查配置失败:', error); + return false; + } +}; + +/** + * 获取配置的最后保存时间 + * @returns {string|null} 最后保存时间的 ISO 字符串 + */ +export const getConfigTimestamp = () => { + try { + const savedConfig = localStorage.getItem(STORAGE_KEYS.CONFIG); + if (savedConfig) { + const parsedConfig = JSON.parse(savedConfig); + return parsedConfig.timestamp || null; + } + } catch (error) { + console.error('获取配置时间戳失败:', error); + } + return null; +}; + +/** + * 导出配置为 JSON 文件(包含消息) + * @param {Object} config - 要导出的配置 + * @param {Array} messages - 要导出的消息 + */ +export const exportConfig = (config, messages = null) => { + try { + const configToExport = { + ...config, + messages: messages || loadMessages(), // 包含消息数据 + exportTime: new Date().toISOString(), + version: '1.0', + }; + + const dataStr = JSON.stringify(configToExport, null, 2); + const dataBlob = new Blob([dataStr], { type: 'application/json' }); + + const link = document.createElement('a'); + link.href = URL.createObjectURL(dataBlob); + link.download = `playground-config-${new Date().toISOString().split('T')[0]}.json`; + link.click(); + + URL.revokeObjectURL(link.href); + } catch (error) { + console.error('导出配置失败:', error); + } +}; + +/** + * 从文件导入配置(包含消息) + * @param {File} file - 包含配置的 JSON 文件 + * @returns {Promise} 导入的配置对象 + */ +export const importConfig = (file) => { + return new Promise((resolve, reject) => { + try { + const reader = new FileReader(); + reader.onload = (e) => { + try { + const importedConfig = JSON.parse(e.target.result); + + if (importedConfig.inputs && importedConfig.parameterEnabled) { + // 如果导入的配置包含消息,也一起导入 + if ( + importedConfig.messages && + Array.isArray(importedConfig.messages) + ) { + saveMessages(importedConfig.messages); + } + + resolve(importedConfig); + } else { + reject(new Error('配置文件格式无效')); + } + } catch (parseError) { + reject(new Error('解析配置文件失败: ' + parseError.message)); + } + }; + reader.onerror = () => reject(new Error('读取文件失败')); + reader.readAsText(file); + } catch (error) { + reject(new Error('导入配置失败: ' + error.message)); + } + }); +}; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/settings/PersonalSetting.jsx b/_all-in-one-upload/new-api/web/classic/src/components/settings/PersonalSetting.jsx new file mode 100644 index 0000000..e735c87 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/settings/PersonalSetting.jsx @@ -0,0 +1,668 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useContext, useEffect, useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { + API, + copy, + showError, + showInfo, + showSuccess, + setStatusData, + prepareCredentialCreationOptions, + buildRegistrationResult, + isPasskeySupported, + setUserData, +} from '../../helpers'; +import { UserContext } from '../../context/User'; +import { Modal } from '@douyinfe/semi-ui'; +import { useTranslation } from 'react-i18next'; + +// 导入子组件 +import UserInfoHeader from './personal/components/UserInfoHeader'; +import AccountManagement from './personal/cards/AccountManagement'; +import NotificationSettings from './personal/cards/NotificationSettings'; +import PreferencesSettings from './personal/cards/PreferencesSettings'; +import CheckinCalendar from './personal/cards/CheckinCalendar'; +import EmailBindModal from './personal/modals/EmailBindModal'; +import WeChatBindModal from './personal/modals/WeChatBindModal'; +import AccountDeleteModal from './personal/modals/AccountDeleteModal'; +import ChangePasswordModal from './personal/modals/ChangePasswordModal'; +import SecureVerificationModal from '../common/modals/SecureVerificationModal'; +import { useSecureVerification } from '../../hooks/common/useSecureVerification'; + +const PersonalSetting = () => { + const [userState, userDispatch] = useContext(UserContext); + let navigate = useNavigate(); + const { t } = useTranslation(); + + const [inputs, setInputs] = useState({ + wechat_verification_code: '', + email_verification_code: '', + email: '', + self_account_deletion_confirmation: '', + original_password: '', + set_new_password: '', + set_new_password_confirmation: '', + }); + const [status, setStatus] = useState({}); + const [showChangePasswordModal, setShowChangePasswordModal] = useState(false); + const [showWeChatBindModal, setShowWeChatBindModal] = useState(false); + const [showEmailBindModal, setShowEmailBindModal] = useState(false); + const [showAccountDeleteModal, setShowAccountDeleteModal] = useState(false); + const [turnstileEnabled, setTurnstileEnabled] = useState(false); + const [turnstileSiteKey, setTurnstileSiteKey] = useState(''); + const [turnstileToken, setTurnstileToken] = useState(''); + const [loading, setLoading] = useState(false); + const [disableButton, setDisableButton] = useState(false); + const [countdown, setCountdown] = useState(30); + const [systemToken, setSystemToken] = useState(''); + const [passkeyStatus, setPasskeyStatus] = useState({ enabled: false }); + const [passkeyRegisterLoading, setPasskeyRegisterLoading] = useState(false); + const [passkeyDeleteLoading, setPasskeyDeleteLoading] = useState(false); + const [passkeySupported, setPasskeySupported] = useState(false); + const [ + passkeyRequiredVerificationMethod, + setPasskeyRequiredVerificationMethod, + ] = useState(null); + const [notificationSettings, setNotificationSettings] = useState({ + warningType: 'email', + warningThreshold: 100000, + webhookUrl: '', + webhookSecret: '', + notificationEmail: '', + barkUrl: '', + gotifyUrl: '', + gotifyToken: '', + gotifyPriority: 5, + upstreamModelUpdateNotifyEnabled: false, + acceptUnsetModelRatioModel: false, + recordIpLog: false, + }); + + const { + isModalVisible: isPasskeyVerificationModalVisible, + verificationMethods: passkeyVerificationMethods, + verificationState: passkeyVerificationState, + startVerification: startPasskeyVerification, + executeVerification: executePasskeyVerification, + cancelVerification: cancelPasskeyVerification, + setVerificationCode: setPasskeyVerificationCode, + switchVerificationMethod: switchPasskeyVerificationMethod, + checkVerificationMethods: checkPasskeyVerificationMethods, + } = useSecureVerification({ + onSuccess: () => { + setPasskeyRequiredVerificationMethod(null); + }, + }); + + const visiblePasskeyVerificationMethods = passkeyRequiredVerificationMethod + ? { + ...passkeyVerificationMethods, + has2FA: + passkeyRequiredVerificationMethod === '2fa' && + passkeyVerificationMethods.has2FA, + hasPasskey: + passkeyRequiredVerificationMethod === 'passkey' && + passkeyVerificationMethods.hasPasskey, + } + : passkeyVerificationMethods; + + useEffect(() => { + let saved = localStorage.getItem('status'); + if (saved) { + const parsed = JSON.parse(saved); + setStatus(parsed); + if (parsed.turnstile_check) { + setTurnstileEnabled(true); + setTurnstileSiteKey(parsed.turnstile_site_key); + } else { + setTurnstileEnabled(false); + setTurnstileSiteKey(''); + } + } + // Always refresh status from server to avoid stale flags (e.g., admin just enabled OAuth) + (async () => { + try { + const res = await API.get('/api/status'); + const { success, data } = res.data; + if (success && data) { + setStatus(data); + setStatusData(data); + if (data.turnstile_check) { + setTurnstileEnabled(true); + setTurnstileSiteKey(data.turnstile_site_key); + } else { + setTurnstileEnabled(false); + setTurnstileSiteKey(''); + } + } + } catch (e) { + // ignore and keep local status + } + })(); + + getUserData(); + + isPasskeySupported() + .then(setPasskeySupported) + .catch(() => setPasskeySupported(false)); + }, []); + + useEffect(() => { + let countdownInterval = null; + if (disableButton && countdown > 0) { + countdownInterval = setInterval(() => { + setCountdown(countdown - 1); + }, 1000); + } else if (countdown === 0) { + setDisableButton(false); + setCountdown(30); + } + return () => clearInterval(countdownInterval); // Clean up on unmount + }, [disableButton, countdown]); + + useEffect(() => { + if (userState?.user?.setting) { + const settings = JSON.parse(userState.user.setting); + setNotificationSettings({ + warningType: settings.notify_type || 'email', + warningThreshold: settings.quota_warning_threshold || 500000, + webhookUrl: settings.webhook_url || '', + webhookSecret: settings.webhook_secret || '', + notificationEmail: settings.notification_email || '', + barkUrl: settings.bark_url || '', + gotifyUrl: settings.gotify_url || '', + gotifyToken: settings.gotify_token || '', + gotifyPriority: + settings.gotify_priority !== undefined ? settings.gotify_priority : 5, + upstreamModelUpdateNotifyEnabled: + settings.upstream_model_update_notify_enabled === true, + acceptUnsetModelRatioModel: + settings.accept_unset_model_ratio_model || false, + recordIpLog: settings.record_ip_log || false, + }); + } + }, [userState?.user?.setting]); + + const handleInputChange = (name, value) => { + setInputs((inputs) => ({ ...inputs, [name]: value })); + }; + + const generateAccessToken = async () => { + const res = await API.get('/api/user/token'); + const { success, message, data } = res.data; + if (success) { + setSystemToken(data); + await copy(data); + showSuccess(t('令牌已重置并已复制到剪贴板')); + } else { + showError(message); + } + }; + + const loadPasskeyStatus = async () => { + try { + const res = await API.get('/api/user/passkey'); + const { success, data, message } = res.data; + if (success) { + setPasskeyStatus({ + enabled: data?.enabled || false, + last_used_at: data?.last_used_at || null, + backup_eligible: data?.backup_eligible || false, + backup_state: data?.backup_state || false, + }); + } else { + showError(message); + } + } catch (error) { + // 忽略错误,保留默认状态 + } + }; + + const startPasskeyManagementVerification = async (apiCall, options = {}) => { + const methods = await checkPasskeyVerificationMethods(); + const requiredMethod = methods.has2FA + ? '2fa' + : methods.hasPasskey + ? 'passkey' + : null; + + if (!requiredMethod) { + showError(t('您需要先启用两步验证或 Passkey 才能执行此操作')); + return; + } + + if (requiredMethod === 'passkey' && !methods.passkeySupported) { + showInfo(t('当前设备不支持 Passkey')); + return; + } + + setPasskeyRequiredVerificationMethod(requiredMethod); + await startPasskeyVerification(apiCall, { + preferredMethod: requiredMethod, + title: t('安全验证'), + ...options, + }); + }; + + const startPasskeyRegistration = async () => { + const methods = await checkPasskeyVerificationMethods(); + if (!methods.has2FA) { + try { + await registerPasskey(); + } catch (error) { + showError(error.message || t('Passkey 注册失败,请重试')); + } + return; + } + + setPasskeyRequiredVerificationMethod('2fa'); + await startPasskeyVerification(registerPasskey, { + preferredMethod: '2fa', + title: t('安全验证'), + }); + }; + + const registerPasskey = async () => { + setPasskeyRegisterLoading(true); + try { + const beginRes = await API.post('/api/user/passkey/register/begin'); + const { success, message, data } = beginRes.data; + if (!success) { + throw new Error(message || t('无法发起 Passkey 注册')); + } + + const publicKey = prepareCredentialCreationOptions( + data?.options || data?.publicKey || data, + ); + const credential = await navigator.credentials.create({ publicKey }); + const payload = buildRegistrationResult(credential); + if (!payload) { + throw new Error(t('Passkey 注册失败,请重试')); + } + + const finishRes = await API.post( + '/api/user/passkey/register/finish', + payload, + ); + if (!finishRes.data.success) { + throw new Error( + finishRes.data.message || t('Passkey 注册失败,请重试'), + ); + } + + showSuccess(t('Passkey 注册成功')); + await loadPasskeyStatus(); + return finishRes.data; + } catch (error) { + if (error?.name === 'AbortError') { + showInfo(t('已取消 Passkey 注册')); + return { cancelled: true }; + } + throw new Error(error?.message || t('Passkey 注册失败,请重试')); + } finally { + setPasskeyRegisterLoading(false); + } + }; + + const handleRegisterPasskey = async () => { + if (!passkeySupported || !window.PublicKeyCredential) { + showInfo(t('当前设备不支持 Passkey')); + return; + } + await startPasskeyRegistration(); + }; + + const removePasskey = async () => { + setPasskeyDeleteLoading(true); + try { + const res = await API.delete('/api/user/passkey'); + const { success, message } = res.data; + if (!success) { + throw new Error(message || t('操作失败,请重试')); + } + + showSuccess(t('Passkey 已解绑')); + await loadPasskeyStatus(); + return res.data; + } catch (error) { + throw new Error(error?.message || t('操作失败,请重试')); + } finally { + setPasskeyDeleteLoading(false); + } + }; + + const handleRemovePasskey = async () => { + await startPasskeyManagementVerification(removePasskey); + }; + + const handlePasskeyVerificationCancel = () => { + setPasskeyRequiredVerificationMethod(null); + cancelPasskeyVerification(); + }; + + const getUserData = async () => { + let res = await API.get(`/api/user/self`); + const { success, message, data } = res.data; + if (success) { + userDispatch({ type: 'login', payload: data }); + setUserData(data); + await loadPasskeyStatus(); + } else { + showError(message); + } + }; + + const handleSystemTokenClick = async (e) => { + e.target.select(); + await copy(e.target.value); + showSuccess(t('系统令牌已复制到剪切板')); + }; + + const deleteAccount = async () => { + if (inputs.self_account_deletion_confirmation !== userState.user.username) { + showError(t('请输入你的账户名以确认删除!')); + return; + } + + const res = await API.delete('/api/user/self'); + const { success, message } = res.data; + + if (success) { + showSuccess(t('账户已删除!')); + await API.get('/api/user/logout'); + userDispatch({ type: 'logout' }); + localStorage.removeItem('user'); + navigate('/login'); + } else { + showError(message); + } + }; + + const bindWeChat = async () => { + if (inputs.wechat_verification_code === '') return; + const res = await API.post('/api/oauth/wechat/bind', { + code: inputs.wechat_verification_code, + }); + const { success, message } = res.data; + if (success) { + showSuccess(t('微信账户绑定成功!')); + setShowWeChatBindModal(false); + } else { + showError(message); + } + }; + + const changePassword = async () => { + // if (inputs.original_password === '') { + // showError(t('请输入原密码!')); + // return; + // } + if (inputs.set_new_password === '') { + showError(t('请输入新密码!')); + return; + } + if (inputs.original_password === inputs.set_new_password) { + showError(t('新密码需要和原密码不一致!')); + return; + } + if (inputs.set_new_password !== inputs.set_new_password_confirmation) { + showError(t('两次输入的密码不一致!')); + return; + } + const res = await API.put(`/api/user/self`, { + original_password: inputs.original_password, + password: inputs.set_new_password, + }); + const { success, message } = res.data; + if (success) { + showSuccess(t('密码修改成功!')); + setShowWeChatBindModal(false); + } else { + showError(message); + } + setShowChangePasswordModal(false); + }; + + const sendVerificationCode = async () => { + if (inputs.email === '') { + showError(t('请输入邮箱!')); + return; + } + setDisableButton(true); + if (turnstileEnabled && turnstileToken === '') { + showInfo(t('请稍后几秒重试,Turnstile 正在检查用户环境!')); + return; + } + setLoading(true); + const res = await API.get( + `/api/verification?email=${inputs.email}&turnstile=${turnstileToken}`, + ); + const { success, message } = res.data; + if (success) { + showSuccess(t('验证码发送成功,请检查邮箱!')); + } else { + showError(message); + } + setLoading(false); + }; + + const bindEmail = async () => { + if (inputs.email_verification_code === '') { + showError(t('请输入邮箱验证码!')); + return; + } + setLoading(true); + const res = await API.post('/api/oauth/email/bind', { + email: inputs.email, + code: inputs.email_verification_code, + }); + const { success, message } = res.data; + if (success) { + showSuccess(t('邮箱账户绑定成功!')); + setShowEmailBindModal(false); + userState.user.email = inputs.email; + } else { + showError(message); + } + setLoading(false); + }; + + const copyText = async (text) => { + if (await copy(text)) { + showSuccess(t('已复制:') + text); + } else { + // setSearchKeyword(text); + Modal.error({ title: t('无法复制到剪贴板,请手动复制'), content: text }); + } + }; + + const handleNotificationSettingChange = (type, value) => { + setNotificationSettings((prev) => ({ + ...prev, + [type]: value.target + ? value.target.value !== undefined + ? value.target.value + : value.target.checked + : value, // handle checkbox properly + })); + }; + + const saveNotificationSettings = async () => { + try { + const res = await API.put('/api/user/setting', { + notify_type: notificationSettings.warningType, + quota_warning_threshold: parseFloat( + notificationSettings.warningThreshold, + ), + webhook_url: notificationSettings.webhookUrl, + webhook_secret: notificationSettings.webhookSecret, + notification_email: notificationSettings.notificationEmail, + bark_url: notificationSettings.barkUrl, + gotify_url: notificationSettings.gotifyUrl, + gotify_token: notificationSettings.gotifyToken, + gotify_priority: (() => { + const parsed = parseInt(notificationSettings.gotifyPriority); + return isNaN(parsed) ? 5 : parsed; + })(), + upstream_model_update_notify_enabled: + notificationSettings.upstreamModelUpdateNotifyEnabled === true, + accept_unset_model_ratio_model: + notificationSettings.acceptUnsetModelRatioModel, + record_ip_log: notificationSettings.recordIpLog, + }); + + if (res.data.success) { + showSuccess(t('设置保存成功')); + await getUserData(); + } else { + showError(res.data.message); + } + } catch (error) { + showError(t('设置保存失败')); + } + }; + + return ( +
+
+
+ {/* 顶部用户信息区域 */} + + + {/* 签到日历 - 仅在启用时显示 */} + {status?.checkin_enabled && ( +
+ +
+ )} + + {/* 账户管理和其他设置 */} +
+ {/* 左侧:账户管理设置 */} +
+ + + {/* 偏好设置(语言等) */} + +
+ + {/* 右侧:其他设置 */} + +
+
+
+ + {/* 模态框组件 */} + + + + + + + + + +
+ ); +}; + +export default PersonalSetting; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/setup/index.jsx b/_all-in-one-upload/new-api/web/classic/src/components/setup/index.jsx new file mode 100644 index 0000000..7ce1247 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/setup/index.jsx @@ -0,0 +1,29 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +// 主要组件导出 +export { default as SetupWizard } from './SetupWizard'; + +export { default as StepNavigation } from './components/StepNavigation'; + +// 步骤组件导出 +export { default as DatabaseStep } from './components/steps/DatabaseStep'; +export { default as AdminStep } from './components/steps/AdminStep'; +export { default as UsageModeStep } from './components/steps/UsageModeStep'; +export { default as CompleteStep } from './components/steps/CompleteStep'; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/table/channels/index.jsx b/_all-in-one-upload/new-api/web/classic/src/components/table/channels/index.jsx new file mode 100644 index 0000000..4a22233 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/table/channels/index.jsx @@ -0,0 +1,116 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React from 'react'; +import { Banner } from '@douyinfe/semi-ui'; +import { IconAlertTriangle } from '@douyinfe/semi-icons'; +import CardPro from '../../common/ui/CardPro'; +import ChannelsTable from './ChannelsTable'; +import ChannelsActions from './ChannelsActions'; +import ChannelsFilters from './ChannelsFilters'; +import ChannelsTabs from './ChannelsTabs'; +import { useChannelsData } from '../../../hooks/channels/useChannelsData'; +import { useIsMobile } from '../../../hooks/common/useIsMobile'; +import BatchTagModal from './modals/BatchTagModal'; +import ModelTestModal from './modals/ModelTestModal'; +import ColumnSelectorModal from './modals/ColumnSelectorModal'; +import EditChannelModal from './modals/EditChannelModal'; +import EditTagModal from './modals/EditTagModal'; +import MultiKeyManageModal from './modals/MultiKeyManageModal'; +import ChannelUpstreamUpdateModal from './modals/ChannelUpstreamUpdateModal'; +import { createCardProPagination } from '../../../helpers/utils'; + +const ChannelsPage = () => { + const channelsData = useChannelsData(); + const isMobile = useIsMobile(); + + return ( + <> + {/* Modals */} + + channelsData.setShowEditTag(false)} + refresh={channelsData.refresh} + /> + + + + channelsData.setShowMultiKeyManageModal(false)} + channel={channelsData.currentMultiKeyChannel} + onRefresh={channelsData.refresh} + /> + + + {/* Main Content */} + {channelsData.globalPassThroughEnabled ? ( + + } + description={channelsData.t( + '已开启全局请求透传:参数覆写、模型重定向、渠道适配等 NewAPI 内置功能将失效,非最佳实践;如因此产生问题,请勿提交 issue 反馈。', + )} + style={{ marginBottom: 12 }} + /> + ) : null} + } + actionsArea={} + searchArea={} + paginationArea={createCardProPagination({ + currentPage: channelsData.activePage, + pageSize: channelsData.pageSize, + total: channelsData.channelCount, + onPageChange: channelsData.handlePageChange, + onPageSizeChange: channelsData.handlePageSizeChange, + isMobile: isMobile, + t: channelsData.t, + })} + t={channelsData.t} + > + + + + ); +}; + +export default ChannelsPage; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/table/channels/modals/EditChannelModal.jsx b/_all-in-one-upload/new-api/web/classic/src/components/table/channels/modals/EditChannelModal.jsx new file mode 100644 index 0000000..fad105b --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/table/channels/modals/EditChannelModal.jsx @@ -0,0 +1,3951 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useEffect, useState, useRef, useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; +import { + API, + showError, + showInfo, + showSuccess, + verifyJSON, +} from '../../../../helpers'; +import { useIsMobile } from '../../../../hooks/common/useIsMobile'; +import { CHANNEL_OPTIONS, MODEL_FETCHABLE_CHANNEL_TYPES } from '../../../../constants'; +import { + SideSheet, + Space, + Spin, + Button, + Typography, + Checkbox, + Banner, + Modal, + ImagePreview, + Card, + Tag, + Avatar, + Form, + Row, + Col, + Highlight, + Input, + Tooltip, + Collapse, + Dropdown, +} from '@douyinfe/semi-ui'; +import { + getChannelModels, + copy, + getChannelIcon, + getModelCategories, + selectFilter, +} from '../../../../helpers'; +import ModelSelectModal from './ModelSelectModal'; +import SingleModelSelectModal from './SingleModelSelectModal'; +import OllamaModelModal from './OllamaModelModal'; +import CodexOAuthModal from './CodexOAuthModal'; +import ParamOverrideEditorModal from './ParamOverrideEditorModal'; +import JSONEditor from '../../../common/ui/JSONEditor'; +import SecureVerificationModal from '../../../common/modals/SecureVerificationModal'; +import StatusCodeRiskGuardModal from './StatusCodeRiskGuardModal'; +import ChannelKeyDisplay from '../../../common/ui/ChannelKeyDisplay'; +import { useSecureVerification } from '../../../../hooks/common/useSecureVerification'; +import { parseChannelConnectionString } from '../../../../helpers/token'; +import { createApiCalls } from '../../../../services/secureVerification'; +import { + collectInvalidStatusCodeEntries, + collectNewDisallowedStatusCodeRedirects, +} from './statusCodeRiskGuard'; +import { + IconSave, + IconClose, + IconServer, + IconSetting, + IconCode, + IconCopy, + IconGlobe, + IconBolt, + IconSearch, + IconChevronDown, +} from '@douyinfe/semi-icons'; + +const { Text, Title } = Typography; + +const MODEL_MAPPING_EXAMPLE = { + 'gpt-3.5-turbo': 'gpt-3.5-turbo-0125', +}; + +const STATUS_CODE_MAPPING_EXAMPLE = { + 400: '500', +}; + +const REGION_EXAMPLE = { + default: 'global', + 'gemini-1.5-pro-002': 'europe-west2', + 'gemini-1.5-flash-002': 'europe-west2', + 'claude-3-5-sonnet-20240620': 'europe-west1', +}; +const UPSTREAM_DETECTED_MODEL_PREVIEW_LIMIT = 8; +const ADVANCED_SETTINGS_EXPANDED_KEY = 'channel-advanced-settings-expanded'; + +const PARAM_OVERRIDE_LEGACY_TEMPLATE = { + temperature: 0, +}; + +const PARAM_OVERRIDE_OPERATIONS_TEMPLATE = { + operations: [ + { + path: 'temperature', + mode: 'set', + value: 0.7, + conditions: [ + { + path: 'model', + mode: 'prefix', + value: 'openai/', + }, + ], + logic: 'AND', + }, + ], +}; + +const DEPRECATED_DOUBAO_CODING_PLAN_BASE_URL = 'doubao-coding-plan'; + +// 支持并且已适配通过接口获取模型列表的渠道类型 +const MODEL_FETCHABLE_TYPES = new Set([ + 1, 4, 14, 34, 17, 26, 27, 24, 47, 25, 20, 23, 31, 40, 42, 48, 43, +]); + +function type2secretPrompt(type) { + // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥') + switch (type) { + case 15: + return '按照如下格式输入:APIKey|SecretKey'; + case 18: + return '按照如下格式输入:APPID|APISecret|APIKey'; + case 22: + return '按照如下格式输入:APIKey-AppId,例如:fastgpt-0sp2gtvfdgyi4k30jwlgwf1i-64f335d84283f05518e9e041'; + case 23: + return '按照如下格式输入:AppId|SecretId|SecretKey'; + case 33: + return '按照如下格式输入:Ak|Sk|Region'; + case 45: + return '请输入渠道对应的鉴权密钥, 豆包语音输入:AppId|AccessToken'; + case 50: + return '按照如下格式输入: AccessKey|SecretKey, 如果上游是New API,则直接输ApiKey'; + case 51: + return '按照如下格式输入: AccessKey|SecretAccessKey'; + case 57: + return '请输入 JSON 格式的 OAuth 凭据(必须包含 access_token 和 account_id)'; + default: + return '请输入渠道对应的鉴权密钥'; + } +} + +const EditChannelModal = (props) => { + const { t } = useTranslation(); + const channelId = props.editingChannel.id; + const isEdit = channelId !== undefined; + const [loading, setLoading] = useState(isEdit); + const isMobile = useIsMobile(); + const handleCancel = () => { + props.handleClose(); + }; + const originInputs = { + name: '', + type: 1, + key: '', + openai_organization: '', + max_input_tokens: 0, + base_url: '', + other: '', + model_mapping: '', + param_override: '', + status_code_mapping: '', + models: [], + auto_ban: 1, + test_model: '', + groups: ['default'], + priority: 0, + weight: 0, + tag: '', + multi_key_mode: 'random', + // 渠道额外设置的默认值 + force_format: false, + thinking_to_content: false, + proxy: '', + pass_through_body_enabled: false, + system_prompt: '', + system_prompt_override: false, + settings: '', + // 仅 Vertex: 密钥格式(存入 settings.vertex_key_type) + vertex_key_type: 'json', + // 仅 AWS: 密钥格式和区域(存入 settings.aws_key_type 和 settings.aws_region) + aws_key_type: 'ak_sk', + // 企业账户设置 + is_enterprise_account: false, + // 字段透传控制默认值 + allow_service_tier: false, + disable_store: false, // false = 允许透传(默认开启) + allow_safety_identifier: false, + allow_include_obfuscation: false, + allow_inference_geo: false, + allow_speed: false, + claude_beta_query: false, + upstream_model_update_check_enabled: false, + upstream_model_update_auto_sync_enabled: false, + upstream_model_update_last_check_time: 0, + upstream_model_update_last_detected_models: [], + upstream_model_update_ignored_models: '', + }; + const [batch, setBatch] = useState(false); + const [multiToSingle, setMultiToSingle] = useState(false); + const [multiKeyMode, setMultiKeyMode] = useState('random'); + const [autoBan, setAutoBan] = useState(true); + const [inputs, setInputs] = useState(originInputs); + const [originModelOptions, setOriginModelOptions] = useState([]); + const [modelOptions, setModelOptions] = useState([]); + const [groupOptions, setGroupOptions] = useState([]); + const [basicModels, setBasicModels] = useState([]); + const [fullModels, setFullModels] = useState([]); + const [modelGroups, setModelGroups] = useState([]); + const [customModel, setCustomModel] = useState(''); + const [modelSearchValue, setModelSearchValue] = useState(''); + const [modalImageUrl, setModalImageUrl] = useState(''); + const [isModalOpenurl, setIsModalOpenurl] = useState(false); + const [modelModalVisible, setModelModalVisible] = useState(false); + const [fetchedModels, setFetchedModels] = useState([]); + const [modelMappingValueModalVisible, setModelMappingValueModalVisible] = + useState(false); + const [modelMappingValueModalModels, setModelMappingValueModalModels] = + useState([]); + const [modelMappingValueKey, setModelMappingValueKey] = useState(''); + const [modelMappingValueSelected, setModelMappingValueSelected] = + useState(''); + const [ollamaModalVisible, setOllamaModalVisible] = useState(false); + const formApiRef = useRef(null); + const [vertexKeys, setVertexKeys] = useState([]); + const [vertexFileList, setVertexFileList] = useState([]); + const vertexErroredNames = useRef(new Set()); // 避免重复报错 + const [isMultiKeyChannel, setIsMultiKeyChannel] = useState(false); + const [channelSearchValue, setChannelSearchValue] = useState(''); + const [useManualInput, setUseManualInput] = useState(false); // 是否使用手动输入模式 + const [keyMode, setKeyMode] = useState('append'); // 密钥模式:replace(覆盖)或 append(追加) + const [isEnterpriseAccount, setIsEnterpriseAccount] = useState(false); // 是否为企业账户 + const [doubaoApiEditUnlocked, setDoubaoApiEditUnlocked] = useState(false); // 豆包渠道自定义 API 地址隐藏入口 + const redirectModelList = useMemo(() => { + const mapping = inputs.model_mapping; + if (typeof mapping !== 'string') return []; + const trimmed = mapping.trim(); + if (!trimmed) return []; + try { + const parsed = JSON.parse(trimmed); + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + return []; + } + const values = Object.values(parsed) + .map((value) => (typeof value === 'string' ? value.trim() : undefined)) + .filter((value) => value); + return Array.from(new Set(values)); + } catch (error) { + return []; + } + }, [inputs.model_mapping]); + const redirectModelKeyList = useMemo(() => { + const mapping = inputs.model_mapping; + if (typeof mapping !== 'string') return []; + const trimmed = mapping.trim(); + if (!trimmed) return []; + try { + const parsed = JSON.parse(trimmed); + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + return []; + } + const keys = Object.keys(parsed) + .map((key) => key.trim()) + .filter((key) => key); + return Array.from(new Set(keys)); + } catch (error) { + return []; + } + }, [inputs.model_mapping]); + const upstreamDetectedModels = useMemo( + () => + Array.from( + new Set( + (inputs.upstream_model_update_last_detected_models || []) + .map((model) => String(model || '').trim()) + .filter(Boolean), + ), + ), + [inputs.upstream_model_update_last_detected_models], + ); + const upstreamDetectedModelsPreview = useMemo( + () => upstreamDetectedModels.slice(0, UPSTREAM_DETECTED_MODEL_PREVIEW_LIMIT), + [upstreamDetectedModels], + ); + const upstreamDetectedModelsOmittedCount = + upstreamDetectedModels.length - upstreamDetectedModelsPreview.length; + const modelSearchMatchedCount = useMemo(() => { + const keyword = modelSearchValue.trim(); + if (!keyword) { + return modelOptions.length; + } + return modelOptions.reduce( + (count, option) => count + (selectFilter(keyword, option) ? 1 : 0), + 0, + ); + }, [modelOptions, modelSearchValue]); + const modelSearchHintText = useMemo(() => { + const keyword = modelSearchValue.trim(); + if (!keyword || modelSearchMatchedCount !== 0) { + return ''; + } + return t('未匹配到模型,按回车键可将「{{name}}」作为自定义模型名添加', { + name: keyword, + }); + }, [modelSearchMatchedCount, modelSearchValue, t]); + const paramOverrideMeta = useMemo(() => { + const raw = + typeof inputs.param_override === 'string' + ? inputs.param_override.trim() + : ''; + if (!raw) { + return { + tagLabel: t('不更改'), + tagColor: 'grey', + preview: t( + '此项可选,用于覆盖请求参数。不支持覆盖 stream 参数', + ), + }; + } + if (!verifyJSON(raw)) { + return { + tagLabel: t('JSON格式错误'), + tagColor: 'red', + preview: raw, + }; + } + try { + const parsed = JSON.parse(raw); + const pretty = JSON.stringify(parsed, null, 2); + if ( + parsed && + typeof parsed === 'object' && + !Array.isArray(parsed) && + Array.isArray(parsed.operations) + ) { + return { + tagLabel: `${t('新格式模板')} (${parsed.operations.length})`, + tagColor: 'cyan', + preview: pretty, + }; + } + if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { + return { + tagLabel: `${t('旧格式模板')} (${Object.keys(parsed).length})`, + tagColor: 'blue', + preview: pretty, + }; + } + return { + tagLabel: t('自定义 JSON'), + tagColor: 'orange', + preview: pretty, + }; + } catch (error) { + return { + tagLabel: t('JSON格式错误'), + tagColor: 'red', + preview: raw, + }; + } + }, [inputs.param_override, t]); + const [isIonetChannel, setIsIonetChannel] = useState(false); + const [ionetMetadata, setIonetMetadata] = useState(null); + const [codexOAuthModalVisible, setCodexOAuthModalVisible] = useState(false); + const [codexCredentialRefreshing, setCodexCredentialRefreshing] = + useState(false); + const [paramOverrideEditorVisible, setParamOverrideEditorVisible] = + useState(false); + + // 密钥显示状态 + const [keyDisplayState, setKeyDisplayState] = useState({ + showModal: false, + keyData: '', + }); + + // 专门的2FA验证状态(用于TwoFactorAuthModal) + const [show2FAVerifyModal, setShow2FAVerifyModal] = useState(false); + const [verifyCode, setVerifyCode] = useState(''); + + useEffect(() => { + if (!isEdit) { + setIsIonetChannel(false); + setIonetMetadata(null); + } + }, [isEdit]); + + const handleOpenIonetDeployment = () => { + if (!ionetMetadata?.deployment_id) { + return; + } + const targetUrl = `/console/deployment?deployment_id=${ionetMetadata.deployment_id}`; + window.open(targetUrl, '_blank', 'noopener'); + }; + const [verifyLoading, setVerifyLoading] = useState(false); + const statusCodeRiskConfirmResolverRef = useRef(null); + const [statusCodeRiskConfirmVisible, setStatusCodeRiskConfirmVisible] = + useState(false); + const [statusCodeRiskDetailItems, setStatusCodeRiskDetailItems] = useState( + [], + ); + + // 剪贴板连接信息自动检测 + const [clipboardConfig, setClipboardConfig] = useState(null); + + // 高级设置折叠状态 + const [advancedSettingsOpen, setAdvancedSettingsOpen] = useState(false); + const toggleAdvancedSettings = (open) => { + setAdvancedSettingsOpen(open); + localStorage.setItem(ADVANCED_SETTINGS_EXPANDED_KEY, String(open)); + }; + const formContainerRef = useRef(null); + const doubaoApiClickCountRef = useRef(0); + const initialBaseUrlRef = useRef(''); + const initialModelsRef = useRef([]); + const initialModelMappingRef = useRef(''); + const initialStatusCodeMappingRef = useRef(''); + const doubaoCodingPlanDeprecationMessage = + 'Doubao Coding Plan 不再允许新增。根据火山方舟文档,Coding 套餐额度仅适用于 AI Coding 产品内调用,不适用于单独 API 调用;在非 AI Coding 产品中使用对应的 Base URL 和 API Key 可能被视为违规,并可能导致订阅停用或账号封禁。'; + const canKeepDeprecatedDoubaoCodingPlan = + initialBaseUrlRef.current === DEPRECATED_DOUBAO_CODING_PLAN_BASE_URL; + const doubaoCodingPlanOptionLabel = ( + + + Doubao Coding Plan + + + ); + + // 2FA状态更新辅助函数 + const updateTwoFAState = (updates) => { + setTwoFAState((prev) => ({ ...prev, ...updates })); + }; + // 使用通用安全验证 Hook + const { + isModalVisible, + verificationMethods, + verificationState, + withVerification, + executeVerification, + cancelVerification, + setVerificationCode, + switchVerificationMethod, + } = useSecureVerification({ + onSuccess: (result) => { + // 验证成功后显示密钥 + console.log('Verification success, result:', result); + if (result && result.success && result.data?.key) { + showSuccess(t('密钥获取成功')); + setKeyDisplayState({ + showModal: true, + keyData: result.data.key, + }); + } else if (result && result.key) { + // 直接返回了 key(没有包装在 data 中) + showSuccess(t('密钥获取成功')); + setKeyDisplayState({ + showModal: true, + keyData: result.key, + }); + } + }, + }); + + // 重置密钥显示状态 + const resetKeyDisplayState = () => { + setKeyDisplayState({ + showModal: false, + keyData: '', + }); + }; + + // 重置2FA验证状态 + const reset2FAVerifyState = () => { + setShow2FAVerifyModal(false); + setVerifyCode(''); + setVerifyLoading(false); + }; + + const handleApiConfigSecretClick = () => { + if (inputs.type !== 45) return; + const next = doubaoApiClickCountRef.current + 1; + doubaoApiClickCountRef.current = next; + if (next >= 10) { + setDoubaoApiEditUnlocked((unlocked) => { + if (!unlocked) { + showInfo(t('已解锁豆包自定义 API 地址编辑')); + } + return true; + }); + } + }; + + // 渠道额外设置状态 + const [channelSettings, setChannelSettings] = useState({ + force_format: false, + thinking_to_content: false, + proxy: '', + pass_through_body_enabled: false, + system_prompt: '', + }); + const showApiConfigCard = true; // 控制是否显示 API 配置卡片 + const getInitValues = () => ({ ...originInputs }); + + // 处理渠道额外设置的更新 + const handleChannelSettingsChange = (key, value) => { + // 更新内部状态 + setChannelSettings((prev) => ({ ...prev, [key]: value })); + + // 同步更新到表单字段 + if (formApiRef.current) { + formApiRef.current.setValue(key, value); + } + + // 同步更新inputs状态 + setInputs((prev) => ({ ...prev, [key]: value })); + + // 生成setting JSON并更新 + const newSettings = { ...channelSettings, [key]: value }; + const settingsJson = JSON.stringify(newSettings); + handleInputChange('setting', settingsJson); + }; + + const handleChannelOtherSettingsChange = (key, value) => { + // 更新内部状态 + setChannelSettings((prev) => ({ ...prev, [key]: value })); + + // 同步更新到表单字段 + if (formApiRef.current) { + formApiRef.current.setValue(key, value); + } + + // 同步更新inputs状态 + setInputs((prev) => ({ ...prev, [key]: value })); + + // 需要更新settings,是一个json,例如{"azure_responses_version": "preview"} + let settings = {}; + if (inputs.settings) { + try { + settings = JSON.parse(inputs.settings); + } catch (error) { + console.error('解析设置失败:', error); + } + } + settings[key] = value; + const settingsJson = JSON.stringify(settings); + handleInputChange('settings', settingsJson); + }; + + const applyClipboardConfig = (config) => { + if (!config) return; + setInputs((prev) => ({ + ...prev, + key: config.key, + base_url: config.url, + })); + if (formApiRef.current) { + formApiRef.current.setValue('key', config.key); + formApiRef.current.setValue('base_url', config.url); + } + setClipboardConfig(null); + showSuccess(t('连接信息已填入')); + }; + + const pasteFromClipboard = async () => { + if (!navigator?.clipboard?.readText) { + showError(t('无法读取剪贴板')); + return; + } + try { + const text = await navigator.clipboard.readText(); + const parsed = parseChannelConnectionString(text); + if (parsed) { + applyClipboardConfig(parsed); + } else { + showInfo(t('剪贴板中未检测到连接信息')); + } + } catch { + showError(t('无法读取剪贴板')); + } + }; + + const isIonetLocked = isIonetChannel && isEdit; + + const handleInputChange = (name, value) => { + if ( + isIonetChannel && + isEdit && + ['type', 'key', 'base_url'].includes(name) + ) { + return; + } + if (formApiRef.current) { + formApiRef.current.setValue(name, value); + } + if (name === 'models' && Array.isArray(value)) { + value = Array.from(new Set(value.map((m) => (m || '').trim()))); + } + + if (name === 'base_url' && value.endsWith('/v1')) { + Modal.confirm({ + title: '警告', + content: + '不需要在末尾加/v1,New API会自动处理,添加后可能导致请求失败,是否继续?', + onOk: () => { + setInputs((inputs) => ({ ...inputs, [name]: value })); + }, + }); + return; + } + setInputs((inputs) => ({ ...inputs, [name]: value })); + if (name === 'type') { + let localModels = []; + switch (value) { + case 2: + localModels = [ + 'mj_imagine', + 'mj_variation', + 'mj_reroll', + 'mj_blend', + 'mj_upscale', + 'mj_describe', + 'mj_uploads', + ]; + break; + case 5: + localModels = [ + 'swap_face', + 'mj_imagine', + 'mj_video', + 'mj_edits', + 'mj_variation', + 'mj_reroll', + 'mj_blend', + 'mj_upscale', + 'mj_describe', + 'mj_zoom', + 'mj_shorten', + 'mj_modal', + 'mj_inpaint', + 'mj_custom_zoom', + 'mj_high_variation', + 'mj_low_variation', + 'mj_pan', + 'mj_uploads', + ]; + break; + case 36: + localModels = ['suno_music', 'suno_lyrics']; + break; + case 45: + localModels = getChannelModels(value); + setInputs((prevInputs) => ({ + ...prevInputs, + base_url: 'https://ark.cn-beijing.volces.com', + })); + break; + default: + localModels = getChannelModels(value); + break; + } + if (inputs.models.length === 0) { + setInputs((inputs) => ({ ...inputs, models: localModels })); + } + setBasicModels(localModels); + + // 重置手动输入模式状态 + setUseManualInput(false); + + if (value === 57) { + setBatch(false); + setMultiToSingle(false); + setMultiKeyMode('random'); + setVertexKeys([]); + setVertexFileList([]); + if (formApiRef.current) { + formApiRef.current.setValue('vertex_files', []); + } + setInputs((prev) => ({ ...prev, vertex_files: [] })); + } + } + //setAutoBan + }; + + const formatJsonField = (fieldName) => { + const rawValue = (inputs?.[fieldName] ?? '').trim(); + if (!rawValue) return; + + try { + const parsed = JSON.parse(rawValue); + handleInputChange(fieldName, JSON.stringify(parsed, null, 2)); + } catch (error) { + showError(`${t('JSON格式错误')}: ${error.message}`); + } + }; + + const formatUnixTime = (timestamp) => { + const value = Number(timestamp || 0); + if (!value) { + return t('暂无'); + } + return new Date(value * 1000).toLocaleString(); + }; + + const copyParamOverrideJson = async () => { + const raw = + typeof inputs.param_override === 'string' + ? inputs.param_override.trim() + : ''; + if (!raw) { + showInfo(t('暂无可复制 JSON')); + return; + } + + let content = raw; + if (verifyJSON(raw)) { + try { + content = JSON.stringify(JSON.parse(raw), null, 2); + } catch (error) { + content = raw; + } + } + + const ok = await copy(content); + if (ok) { + showSuccess(t('参数覆盖 JSON 已复制')); + } else { + showError(t('复制失败')); + } + }; + + const parseParamOverrideInput = () => { + const raw = + typeof inputs.param_override === 'string' + ? inputs.param_override.trim() + : ''; + if (!raw) return null; + if (!verifyJSON(raw)) { + throw new Error(t('当前参数覆盖不是合法的 JSON')); + } + return JSON.parse(raw); + }; + + const applyParamOverrideTemplate = ( + templateType = 'operations', + applyMode = 'fill', + ) => { + try { + const parsedCurrent = parseParamOverrideInput(); + if (templateType === 'legacy') { + if (applyMode === 'fill') { + handleInputChange( + 'param_override', + JSON.stringify(PARAM_OVERRIDE_LEGACY_TEMPLATE, null, 2), + ); + return; + } + const currentLegacy = + parsedCurrent && + typeof parsedCurrent === 'object' && + !Array.isArray(parsedCurrent) && + !Array.isArray(parsedCurrent.operations) + ? parsedCurrent + : {}; + const merged = { + ...PARAM_OVERRIDE_LEGACY_TEMPLATE, + ...currentLegacy, + }; + handleInputChange('param_override', JSON.stringify(merged, null, 2)); + return; + } + + if (applyMode === 'fill') { + handleInputChange( + 'param_override', + JSON.stringify(PARAM_OVERRIDE_OPERATIONS_TEMPLATE, null, 2), + ); + return; + } + const currentOperations = + parsedCurrent && + typeof parsedCurrent === 'object' && + !Array.isArray(parsedCurrent) && + Array.isArray(parsedCurrent.operations) + ? parsedCurrent.operations + : []; + const merged = { + operations: [ + ...currentOperations, + ...PARAM_OVERRIDE_OPERATIONS_TEMPLATE.operations, + ], + }; + handleInputChange('param_override', JSON.stringify(merged, null, 2)); + } catch (error) { + showError(error.message || t('模板应用失败')); + } + }; + + const clearParamOverride = () => { + handleInputChange('param_override', ''); + }; + + const loadChannel = async () => { + setLoading(true); + let res = await API.get(`/api/channel/${channelId}`); + if (res === undefined) { + return; + } + const { success, message, data } = res.data; + if (success) { + if (data.models === '') { + data.models = []; + } else { + data.models = data.models.split(','); + } + if (data.group === '') { + data.groups = []; + } else { + data.groups = data.group.split(','); + } + if (data.model_mapping !== '') { + data.model_mapping = JSON.stringify( + JSON.parse(data.model_mapping), + null, + 2, + ); + } + const chInfo = data.channel_info || {}; + const isMulti = chInfo.is_multi_key === true; + setIsMultiKeyChannel(isMulti); + if (isMulti) { + setBatch(true); + setMultiToSingle(true); + const modeVal = chInfo.multi_key_mode || 'random'; + setMultiKeyMode(modeVal); + data.multi_key_mode = modeVal; + } else { + setBatch(false); + setMultiToSingle(false); + } + // 解析渠道额外设置并合并到data中 + if (data.setting) { + try { + const parsedSettings = JSON.parse(data.setting); + data.force_format = parsedSettings.force_format || false; + data.thinking_to_content = + parsedSettings.thinking_to_content || false; + data.proxy = parsedSettings.proxy || ''; + data.pass_through_body_enabled = + parsedSettings.pass_through_body_enabled || false; + data.system_prompt = parsedSettings.system_prompt || ''; + data.system_prompt_override = + parsedSettings.system_prompt_override || false; + } catch (error) { + console.error('解析渠道设置失败:', error); + data.force_format = false; + data.thinking_to_content = false; + data.proxy = ''; + data.pass_through_body_enabled = false; + data.system_prompt = ''; + data.system_prompt_override = false; + } + } else { + data.force_format = false; + data.thinking_to_content = false; + data.proxy = ''; + data.pass_through_body_enabled = false; + data.system_prompt = ''; + data.system_prompt_override = false; + } + + if (data.settings) { + try { + const parsedSettings = JSON.parse(data.settings); + data.azure_responses_version = + parsedSettings.azure_responses_version || ''; + // 读取 Vertex 密钥格式 + data.vertex_key_type = parsedSettings.vertex_key_type || 'json'; + // 读取 AWS 密钥格式和区域 + data.aws_key_type = parsedSettings.aws_key_type || 'ak_sk'; + // 读取企业账户设置 + data.is_enterprise_account = + parsedSettings.openrouter_enterprise === true; + // 读取字段透传控制设置 + data.allow_service_tier = parsedSettings.allow_service_tier || false; + data.disable_store = parsedSettings.disable_store || false; + data.allow_safety_identifier = + parsedSettings.allow_safety_identifier || false; + data.allow_include_obfuscation = + parsedSettings.allow_include_obfuscation || false; + data.allow_inference_geo = + parsedSettings.allow_inference_geo || false; + data.allow_speed = parsedSettings.allow_speed || false; + data.claude_beta_query = parsedSettings.claude_beta_query || false; + data.upstream_model_update_check_enabled = + parsedSettings.upstream_model_update_check_enabled === true; + data.upstream_model_update_auto_sync_enabled = + parsedSettings.upstream_model_update_auto_sync_enabled === true; + data.upstream_model_update_last_check_time = + Number(parsedSettings.upstream_model_update_last_check_time) || 0; + data.upstream_model_update_last_detected_models = Array.isArray( + parsedSettings.upstream_model_update_last_detected_models, + ) + ? parsedSettings.upstream_model_update_last_detected_models + : []; + data.upstream_model_update_ignored_models = Array.isArray( + parsedSettings.upstream_model_update_ignored_models, + ) + ? parsedSettings.upstream_model_update_ignored_models.join(',') + : ''; + } catch (error) { + console.error('解析其他设置失败:', error); + data.azure_responses_version = ''; + data.region = ''; + data.vertex_key_type = 'json'; + data.aws_key_type = 'ak_sk'; + data.is_enterprise_account = false; + data.allow_service_tier = false; + data.disable_store = false; + data.allow_safety_identifier = false; + data.allow_include_obfuscation = false; + data.allow_inference_geo = false; + data.allow_speed = false; + data.claude_beta_query = false; + data.upstream_model_update_check_enabled = false; + data.upstream_model_update_auto_sync_enabled = false; + data.upstream_model_update_last_check_time = 0; + data.upstream_model_update_last_detected_models = []; + data.upstream_model_update_ignored_models = ''; + } + } else { + // 兼容历史数据:老渠道没有 settings 时,默认按 json 展示 + data.vertex_key_type = 'json'; + data.aws_key_type = 'ak_sk'; + data.is_enterprise_account = false; + data.allow_service_tier = false; + data.disable_store = false; + data.allow_safety_identifier = false; + data.allow_include_obfuscation = false; + data.allow_inference_geo = false; + data.allow_speed = false; + data.claude_beta_query = false; + data.upstream_model_update_check_enabled = false; + data.upstream_model_update_auto_sync_enabled = false; + data.upstream_model_update_last_check_time = 0; + data.upstream_model_update_last_detected_models = []; + data.upstream_model_update_ignored_models = ''; + } + + if ( + data.type === 45 && + (!data.base_url || + (typeof data.base_url === 'string' && data.base_url.trim() === '')) + ) { + data.base_url = 'https://ark.cn-beijing.volces.com'; + } + + initialBaseUrlRef.current = data.base_url || ''; + setInputs(data); + if (formApiRef.current) { + formApiRef.current.setValues(data); + } + if (data.auto_ban === 0) { + setAutoBan(false); + } else { + setAutoBan(true); + } + // 同步企业账户状态 + setIsEnterpriseAccount(data.is_enterprise_account || false); + setBasicModels(getChannelModels(data.type)); + // 同步更新channelSettings状态显示 + setChannelSettings({ + force_format: data.force_format, + thinking_to_content: data.thinking_to_content, + proxy: data.proxy, + pass_through_body_enabled: data.pass_through_body_enabled, + system_prompt: data.system_prompt, + system_prompt_override: data.system_prompt_override || false, + }); + initialModelsRef.current = (data.models || []) + .map((model) => (model || '').trim()) + .filter(Boolean); + initialModelMappingRef.current = data.model_mapping || ''; + initialStatusCodeMappingRef.current = data.status_code_mapping || ''; + + let parsedIonet = null; + if (data.other_info) { + try { + const maybeMeta = JSON.parse(data.other_info); + if ( + maybeMeta && + typeof maybeMeta === 'object' && + maybeMeta.source === 'ionet' + ) { + parsedIonet = maybeMeta; + } + } catch (error) { + // ignore parse error + } + } + const managedByIonet = !!parsedIonet; + setIsIonetChannel(managedByIonet); + setIonetMetadata(parsedIonet); + + // Smart expand: auto-open advanced settings if any advanced field has a value + const hasAdvancedValues = + (data.model_mapping && data.model_mapping.trim()) || + (data.param_override && data.param_override.trim()) || + (data.status_code_mapping && data.status_code_mapping.trim()) || + (data.header_override && data.header_override.trim()) || + (data.tag && data.tag.trim()) || + (data.remark && data.remark.trim()) || + (data.priority && data.priority !== 0) || + (data.weight && data.weight !== 0) || + (data.proxy && data.proxy.trim()) || + (data.system_prompt && data.system_prompt.trim()) || + data.thinking_to_content || + data.pass_through_body_enabled || + data.force_format || + data.claude_beta_query || + data.system_prompt_override; + if (hasAdvancedValues) { + setAdvancedSettingsOpen(true); + } + } else { + showError(message); + } + setLoading(false); + }; + + const fetchUpstreamModelList = async (name, options = {}) => { + const silent = !!options.silent; + // if (inputs['type'] !== 1) { + // showError(t('仅支持 OpenAI 接口格式')); + // return; + // } + setLoading(true); + const models = []; + let err = false; + + if (isEdit) { + // 如果是编辑模式,使用已有的 channelId 获取模型列表 + const res = await API.get('/api/channel/fetch_models/' + channelId, { + skipErrorHandler: true, + }); + if (res && res.data && res.data.success) { + models.push(...res.data.data); + } else { + err = true; + } + } else { + // 如果是新建模式,通过后端代理获取模型列表 + if (!inputs?.['key']) { + showError(t('请填写密钥')); + err = true; + } else { + try { + const res = await API.post( + '/api/channel/fetch_models', + { + base_url: inputs['base_url'], + type: inputs['type'], + key: inputs['key'], + }, + { skipErrorHandler: true }, + ); + + if (res && res.data && res.data.success) { + models.push(...res.data.data); + } else { + err = true; + } + } catch (error) { + console.error('Error fetching models:', error); + err = true; + } + } + } + + if (!err) { + const uniqueModels = Array.from(new Set(models)); + setFetchedModels(uniqueModels); + if (!silent) { + setModelModalVisible(true); + } + setLoading(false); + return uniqueModels; + } else { + showError(t('获取模型列表失败')); + } + setLoading(false); + return null; + }; + + const openModelMappingValueModal = async ({ pairKey, value }) => { + const mappingKey = String(pairKey ?? '').trim(); + if (!mappingKey) return; + + if (!MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type)) { + return; + } + + let modelsToUse = fetchedModels; + if (!Array.isArray(modelsToUse) || modelsToUse.length === 0) { + const fetched = await fetchUpstreamModelList('models', { silent: true }); + if (Array.isArray(fetched)) { + modelsToUse = fetched; + } + } + + if (!Array.isArray(modelsToUse) || modelsToUse.length === 0) { + showInfo(t('暂无模型')); + return; + } + + const normalizedModelsToUse = Array.from( + new Set( + modelsToUse.map((model) => String(model ?? '').trim()).filter(Boolean), + ), + ); + const currentValue = String(value ?? '').trim(); + + setModelMappingValueModalModels(normalizedModelsToUse); + setModelMappingValueKey(mappingKey); + setModelMappingValueSelected( + normalizedModelsToUse.includes(currentValue) ? currentValue : '', + ); + setModelMappingValueModalVisible(true); + }; + + const fetchModels = async () => { + try { + let res = await API.get(`/api/channel/models`); + const localModelOptions = res.data.data.map((model) => { + const id = (model.id || '').trim(); + return { + key: id, + label: id, + value: id, + }; + }); + setOriginModelOptions(localModelOptions); + setFullModels(res.data.data.map((model) => model.id)); + setBasicModels( + res.data.data + .filter((model) => { + return model.id.startsWith('gpt-') || model.id.startsWith('text-'); + }) + .map((model) => model.id), + ); + } catch (error) { + showError(error.message); + } + }; + + const fetchGroups = async () => { + try { + let res = await API.get(`/api/group/`); + if (res === undefined) { + return; + } + setGroupOptions( + res.data.data.map((group) => ({ + label: group, + value: group, + })), + ); + } catch (error) { + showError(error.message); + } + }; + + const fetchModelGroups = async () => { + try { + const res = await API.get('/api/prefill_group?type=model'); + if (res?.data?.success) { + setModelGroups(res.data.data || []); + } + } catch (error) { + // ignore + } + }; + + // 查看渠道密钥(透明验证) + const handleShow2FAModal = async () => { + try { + // 使用 withVerification 包装,会自动处理需要验证的情况 + const result = await withVerification( + createApiCalls.viewChannelKey(channelId), + { + title: t('查看渠道密钥'), + description: t('为了保护账户安全,请验证您的身份。'), + preferredMethod: 'passkey', // 优先使用 Passkey + }, + ); + + // 如果直接返回了结果(已验证),显示密钥 + if (result && result.success && result.data?.key) { + showSuccess(t('密钥获取成功')); + setKeyDisplayState({ + showModal: true, + keyData: result.data.key, + }); + } + } catch (error) { + console.error('Failed to view channel key:', error); + showError(error.message || t('获取密钥失败')); + } + }; + + const handleCodexOAuthGenerated = (key) => { + handleInputChange('key', key); + formatJsonField('key'); + }; + + const handleRefreshCodexCredential = async () => { + if (!isEdit) return; + + setCodexCredentialRefreshing(true); + try { + const res = await API.post( + `/api/channel/${channelId}/codex/refresh`, + {}, + { skipErrorHandler: true }, + ); + if (!res?.data?.success) { + throw new Error(res?.data?.message || 'Failed to refresh credential'); + } + showSuccess(t('凭证已刷新')); + } catch (error) { + showError(error.message || t('刷新失败')); + } finally { + setCodexCredentialRefreshing(false); + } + }; + + useEffect(() => { + if (inputs.type !== 45) { + doubaoApiClickCountRef.current = 0; + setDoubaoApiEditUnlocked(false); + } + }, [inputs.type]); + + useEffect(() => { + const modelMap = new Map(); + + originModelOptions.forEach((option) => { + const v = (option.value || '').trim(); + if (!modelMap.has(v)) { + modelMap.set(v, option); + } + }); + + inputs.models.forEach((model) => { + const v = (model || '').trim(); + if (!modelMap.has(v)) { + modelMap.set(v, { + key: v, + label: v, + value: v, + }); + } + }); + + const categories = getModelCategories(t); + const optionsWithIcon = Array.from(modelMap.values()).map((opt) => { + const modelName = opt.value; + let icon = null; + for (const [key, category] of Object.entries(categories)) { + if (key !== 'all' && category.filter({ model_name: modelName })) { + icon = category.icon; + break; + } + } + return { + ...opt, + label: ( + + {icon} + {modelName} + + ), + }; + }); + + setModelOptions(optionsWithIcon); + }, [originModelOptions, inputs.models, t]); + + useEffect(() => { + fetchModels().then(); + fetchGroups().then(); + if (!isEdit) { + initialBaseUrlRef.current = ''; + setInputs(originInputs); + if (formApiRef.current) { + formApiRef.current.setValues(originInputs); + } + let localModels = getChannelModels(inputs.type); + setBasicModels(localModels); + setInputs((inputs) => ({ ...inputs, models: localModels })); + } + }, [props.editingChannel.id]); + + useEffect(() => { + if (formApiRef.current) { + formApiRef.current.setValues(inputs); + } + }, [inputs]); + + useEffect(() => { + setModelSearchValue(''); + if (props.visible) { + if (isEdit) { + loadChannel(); + } else { + formApiRef.current?.setValues(getInitValues()); + try { + navigator?.clipboard?.readText()?.then((text) => { + const parsed = parseChannelConnectionString(text); + if (parsed) { + setClipboardConfig(parsed); + } + }).catch(() => {}); + } catch {} + } + fetchModelGroups(); + // 重置手动输入模式状态 + setUseManualInput(false); + // 编辑模式下恢复用户偏好,创建模式一律折叠 + setAdvancedSettingsOpen( + isEdit && localStorage.getItem(ADVANCED_SETTINGS_EXPANDED_KEY) === 'true' + ); + } else { + // 统一的模态框关闭重置逻辑 + resetModalState(); + } + }, [props.visible, channelId]); + + useEffect(() => { + if (!isEdit) { + initialModelsRef.current = []; + initialModelMappingRef.current = ''; + initialStatusCodeMappingRef.current = ''; + } + }, [isEdit, props.visible]); + + useEffect(() => { + return () => { + if (statusCodeRiskConfirmResolverRef.current) { + statusCodeRiskConfirmResolverRef.current(false); + statusCodeRiskConfirmResolverRef.current = null; + } + }; + }, []); + + // 统一的模态框重置函数 + const resetModalState = () => { + resolveStatusCodeRiskConfirm(false); + formApiRef.current?.reset(); + // 重置渠道设置状态 + setChannelSettings({ + force_format: false, + thinking_to_content: false, + proxy: '', + pass_through_body_enabled: false, + system_prompt: '', + system_prompt_override: false, + }); + // 重置密钥模式状态 + setKeyMode('append'); + // 重置企业账户状态 + setIsEnterpriseAccount(false); + // 重置豆包隐藏入口状态 + setDoubaoApiEditUnlocked(false); + doubaoApiClickCountRef.current = 0; + setModelSearchValue(''); + // 重置高级设置折叠状态 + setAdvancedSettingsOpen(false); + // 清空表单中的key_mode字段 + if (formApiRef.current) { + formApiRef.current.setValue('key_mode', undefined); + } + // 重置本地输入,避免下次打开残留上一次的 JSON 字段值 + setInputs(getInitValues()); + // 重置密钥显示状态 + resetKeyDisplayState(); + // 重置剪贴板检测状态 + setClipboardConfig(null); + }; + + const handleVertexUploadChange = ({ fileList }) => { + vertexErroredNames.current.clear(); + (async () => { + let validFiles = []; + let keys = []; + const errorNames = []; + for (const item of fileList) { + const fileObj = item.fileInstance; + if (!fileObj) continue; + try { + const txt = await fileObj.text(); + keys.push(JSON.parse(txt)); + validFiles.push(item); + } catch (err) { + if (!vertexErroredNames.current.has(item.name)) { + errorNames.push(item.name); + vertexErroredNames.current.add(item.name); + } + } + } + + // 非批量模式下只保留一个文件(最新选择的),避免重复叠加 + if (!batch && validFiles.length > 1) { + validFiles = [validFiles[validFiles.length - 1]]; + keys = [keys[keys.length - 1]]; + } + + setVertexKeys(keys); + setVertexFileList(validFiles); + if (formApiRef.current) { + formApiRef.current.setValue('vertex_files', validFiles); + } + setInputs((prev) => ({ ...prev, vertex_files: validFiles })); + + if (errorNames.length > 0) { + showError( + t('以下文件解析失败,已忽略:{{list}}', { + list: errorNames.join(', '), + }), + ); + } + })(); + }; + + const confirmMissingModelMappings = (missingModels) => + new Promise((resolve) => { + const modal = Modal.confirm({ + title: t('模型未加入列表,可能无法调用'), + content: ( +
+
+ {t( + '模型重定向里的下列模型尚未添加到“模型”列表,调用时会因为缺少可用模型而失败:', + )} +
+
+ {missingModels.join(', ')} +
+
+ {t( + '你可以在“自定义模型名称”处手动添加它们,然后点击填入后再提交,或者直接使用下方操作自动处理。', + )} +
+
+ ), + centered: true, + footer: ( + + + + + + ), + }); + }); + + const resolveStatusCodeRiskConfirm = (confirmed) => { + setStatusCodeRiskConfirmVisible(false); + setStatusCodeRiskDetailItems([]); + if (statusCodeRiskConfirmResolverRef.current) { + statusCodeRiskConfirmResolverRef.current(confirmed); + statusCodeRiskConfirmResolverRef.current = null; + } + }; + + const confirmStatusCodeRisk = (detailItems) => + new Promise((resolve) => { + statusCodeRiskConfirmResolverRef.current = resolve; + setStatusCodeRiskDetailItems(detailItems); + setStatusCodeRiskConfirmVisible(true); + }); + + const hasModelConfigChanged = (normalizedModels, modelMappingStr) => { + if (!isEdit) return true; + const initialModels = initialModelsRef.current; + if (normalizedModels.length !== initialModels.length) { + return true; + } + for (let i = 0; i < normalizedModels.length; i++) { + if (normalizedModels[i] !== initialModels[i]) { + return true; + } + } + const normalizedMapping = (modelMappingStr || '').trim(); + const initialMapping = (initialModelMappingRef.current || '').trim(); + return normalizedMapping !== initialMapping; + }; + + const submit = async () => { + const formValues = formApiRef.current ? formApiRef.current.getValues() : {}; + let localInputs = { ...formValues }; + localInputs.param_override = inputs.param_override; + + if (localInputs.type === 57) { + if (batch) { + showInfo(t('Codex 渠道不支持批量创建')); + return; + } + + const rawKey = (localInputs.key || '').trim(); + if (!isEdit && rawKey === '') { + showInfo(t('请输入密钥!')); + return; + } + + if (rawKey !== '') { + if (!verifyJSON(rawKey)) { + showInfo(t('密钥必须是合法的 JSON 格式!')); + return; + } + try { + const parsed = JSON.parse(rawKey); + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + showInfo(t('密钥必须是 JSON 对象')); + return; + } + const accessToken = String(parsed.access_token || '').trim(); + const accountId = String(parsed.account_id || '').trim(); + if (!accessToken) { + showInfo(t('密钥 JSON 必须包含 access_token')); + return; + } + if (!accountId) { + showInfo(t('密钥 JSON 必须包含 account_id')); + return; + } + localInputs.key = JSON.stringify(parsed); + } catch (error) { + showInfo(t('密钥必须是合法的 JSON 格式!')); + return; + } + } + } + + if (localInputs.type === 41) { + const keyType = localInputs.vertex_key_type || 'json'; + if (keyType === 'api_key') { + // 直接作为普通字符串密钥处理 + if (!isEdit && (!localInputs.key || localInputs.key.trim() === '')) { + showInfo(t('请输入密钥!')); + return; + } + } else { + // JSON 服务账号密钥 + if (useManualInput) { + if (localInputs.key && localInputs.key.trim() !== '') { + try { + const parsedKey = JSON.parse(localInputs.key); + localInputs.key = JSON.stringify(parsedKey); + } catch (err) { + showError(t('密钥格式无效,请输入有效的 JSON 格式密钥')); + return; + } + } else if (!isEdit) { + showInfo(t('请输入密钥!')); + return; + } + } else { + // 文件上传模式 + let keys = vertexKeys; + if (keys.length === 0 && vertexFileList.length > 0) { + try { + const parsed = await Promise.all( + vertexFileList.map(async (item) => { + const fileObj = item.fileInstance; + if (!fileObj) return null; + const txt = await fileObj.text(); + return JSON.parse(txt); + }), + ); + keys = parsed.filter(Boolean); + } catch (err) { + showError(t('解析密钥文件失败: {{msg}}', { msg: err.message })); + return; + } + } + if (keys.length === 0) { + if (!isEdit) { + showInfo(t('请上传密钥文件!')); + return; + } else { + delete localInputs.key; + } + } else { + localInputs.key = batch + ? JSON.stringify(keys) + : JSON.stringify(keys[0]); + } + } + } + } + + // 如果是编辑模式且 key 为空字符串,避免提交空值覆盖旧密钥 + if (isEdit && (!localInputs.key || localInputs.key.trim() === '')) { + delete localInputs.key; + } + delete localInputs.vertex_files; + + if (!isEdit && (!localInputs.name || !localInputs.key)) { + showInfo(t('请填写渠道名称和渠道密钥!')); + return; + } + if (!Array.isArray(localInputs.models) || localInputs.models.length === 0) { + showInfo(t('请至少选择一个模型!')); + return; + } + if ( + localInputs.type === 45 && + (!localInputs.base_url || localInputs.base_url.trim() === '') + ) { + showInfo(t('请输入API地址!')); + return; + } + const hasModelMapping = + typeof localInputs.model_mapping === 'string' && + localInputs.model_mapping.trim() !== ''; + let parsedModelMapping = null; + if (hasModelMapping) { + if (!verifyJSON(localInputs.model_mapping)) { + showInfo(t('模型映射必须是合法的 JSON 格式!')); + return; + } + try { + parsedModelMapping = JSON.parse(localInputs.model_mapping); + } catch (error) { + showInfo(t('模型映射必须是合法的 JSON 格式!')); + return; + } + } + + const normalizedModels = (localInputs.models || []) + .map((model) => (model || '').trim()) + .filter(Boolean); + localInputs.models = normalizedModels; + + if ( + parsedModelMapping && + typeof parsedModelMapping === 'object' && + !Array.isArray(parsedModelMapping) + ) { + const modelSet = new Set(normalizedModels); + const missingModels = Object.keys(parsedModelMapping) + .map((key) => (key || '').trim()) + .filter((key) => key && !modelSet.has(key)); + const shouldPromptMissing = + missingModels.length > 0 && + hasModelConfigChanged(normalizedModels, localInputs.model_mapping); + if (shouldPromptMissing) { + const confirmAction = await confirmMissingModelMappings(missingModels); + if (confirmAction === 'cancel') { + return; + } + if (confirmAction === 'add') { + const updatedModels = Array.from( + new Set([...normalizedModels, ...missingModels]), + ); + localInputs.models = updatedModels; + handleInputChange('models', updatedModels); + } + } + } + + const invalidStatusCodeEntries = collectInvalidStatusCodeEntries( + localInputs.status_code_mapping, + ); + if (invalidStatusCodeEntries.length > 0) { + showError( + `${t('状态码复写包含无效的状态码')}: ${invalidStatusCodeEntries.join(', ')}`, + ); + return; + } + + const riskyStatusCodeRedirects = collectNewDisallowedStatusCodeRedirects( + initialStatusCodeMappingRef.current, + localInputs.status_code_mapping, + ); + if (riskyStatusCodeRedirects.length > 0) { + const confirmed = await confirmStatusCodeRisk(riskyStatusCodeRedirects); + if (!confirmed) { + return; + } + } + + if (localInputs.base_url && localInputs.base_url.endsWith('/')) { + localInputs.base_url = localInputs.base_url.slice( + 0, + localInputs.base_url.length - 1, + ); + } + if (localInputs.type === 18 && localInputs.other === '') { + localInputs.other = 'v2.1'; + } + + // 生成渠道额外设置JSON + const channelExtraSettings = { + force_format: localInputs.force_format || false, + thinking_to_content: localInputs.thinking_to_content || false, + proxy: localInputs.proxy || '', + pass_through_body_enabled: localInputs.pass_through_body_enabled || false, + system_prompt: localInputs.system_prompt || '', + system_prompt_override: localInputs.system_prompt_override || false, + }; + localInputs.setting = JSON.stringify(channelExtraSettings); + + // 处理 settings 字段(包括企业账户设置和字段透传控制) + let settings = {}; + if (localInputs.settings) { + try { + settings = JSON.parse(localInputs.settings); + } catch (error) { + console.error('解析settings失败:', error); + } + } + + // type === 20: 设置企业账户标识,无论是true还是false都要传到后端 + if (localInputs.type === 20) { + settings.openrouter_enterprise = + localInputs.is_enterprise_account === true; + } + + // type === 33 (AWS): 保存 aws_key_type 到 settings + if (localInputs.type === 33) { + settings.aws_key_type = localInputs.aws_key_type || 'ak_sk'; + } + + // type === 41 (Vertex): 始终保存 vertex_key_type 到 settings,避免编辑时被重置 + if (localInputs.type === 41) { + settings.vertex_key_type = localInputs.vertex_key_type || 'json'; + } else if ('vertex_key_type' in settings) { + delete settings.vertex_key_type; + } + + // type === 1 (OpenAI) 或 type === 14 (Claude): 设置字段透传控制(显式保存布尔值) + if (localInputs.type === 1 || localInputs.type === 14) { + settings.allow_service_tier = localInputs.allow_service_tier === true; + // 仅 OpenAI 渠道需要 store / safety_identifier / include_obfuscation + if (localInputs.type === 1) { + settings.disable_store = localInputs.disable_store === true; + settings.allow_safety_identifier = + localInputs.allow_safety_identifier === true; + settings.allow_include_obfuscation = + localInputs.allow_include_obfuscation === true; + } + if (localInputs.type === 14) { + settings.allow_inference_geo = localInputs.allow_inference_geo === true; + settings.allow_speed = localInputs.allow_speed === true; + settings.claude_beta_query = localInputs.claude_beta_query === true; + } + } + + settings.upstream_model_update_check_enabled = + localInputs.upstream_model_update_check_enabled === true; + settings.upstream_model_update_auto_sync_enabled = + settings.upstream_model_update_check_enabled && + localInputs.upstream_model_update_auto_sync_enabled === true; + settings.upstream_model_update_ignored_models = Array.from( + new Set( + String(localInputs.upstream_model_update_ignored_models || '') + .split(',') + .map((model) => model.trim()) + .filter(Boolean), + ), + ); + if ( + !Array.isArray(settings.upstream_model_update_last_detected_models) || + !settings.upstream_model_update_check_enabled + ) { + settings.upstream_model_update_last_detected_models = []; + } + if (typeof settings.upstream_model_update_last_check_time !== 'number') { + settings.upstream_model_update_last_check_time = 0; + } + + localInputs.settings = JSON.stringify(settings); + + // 清理不需要发送到后端的字段 + delete localInputs.force_format; + delete localInputs.thinking_to_content; + delete localInputs.proxy; + delete localInputs.pass_through_body_enabled; + delete localInputs.system_prompt; + delete localInputs.system_prompt_override; + delete localInputs.is_enterprise_account; + // 顶层的 vertex_key_type 不应发送给后端 + delete localInputs.vertex_key_type; + // 顶层的 aws_key_type 不应发送给后端 + delete localInputs.aws_key_type; + // 清理字段透传控制的临时字段 + delete localInputs.allow_service_tier; + delete localInputs.disable_store; + delete localInputs.allow_safety_identifier; + delete localInputs.allow_include_obfuscation; + delete localInputs.allow_inference_geo; + delete localInputs.allow_speed; + delete localInputs.claude_beta_query; + delete localInputs.upstream_model_update_check_enabled; + delete localInputs.upstream_model_update_auto_sync_enabled; + delete localInputs.upstream_model_update_last_check_time; + delete localInputs.upstream_model_update_last_detected_models; + delete localInputs.upstream_model_update_ignored_models; + + let res; + localInputs.auto_ban = localInputs.auto_ban ? 1 : 0; + localInputs.models = localInputs.models.join(','); + localInputs.group = (localInputs.groups || []).join(','); + + let mode = 'single'; + if (batch) { + mode = multiToSingle ? 'multi_to_single' : 'batch'; + } + + if (isEdit) { + res = await API.put(`/api/channel/`, { + ...localInputs, + id: parseInt(channelId), + key_mode: isMultiKeyChannel ? keyMode : undefined, // 只在多key模式下传递 + }); + } else { + res = await API.post(`/api/channel/`, { + mode: mode, + multi_key_mode: mode === 'multi_to_single' ? multiKeyMode : undefined, + channel: localInputs, + }); + } + const { success, message } = res.data; + if (success) { + if (isEdit) { + showSuccess(t('渠道更新成功!')); + } else { + showSuccess(t('渠道创建成功!')); + setInputs(originInputs); + } + props.refresh(); + props.handleClose(); + } else { + showError(message); + } + }; + + // 密钥去重函数 + const deduplicateKeys = () => { + const currentKey = formApiRef.current?.getValue('key') || inputs.key || ''; + + if (!currentKey.trim()) { + showInfo(t('请先输入密钥')); + return; + } + + // 按行分割密钥 + const keyLines = currentKey.split('\n'); + const beforeCount = keyLines.length; + + // 使用哈希表去重,保持原有顺序 + const keySet = new Set(); + const deduplicatedKeys = []; + + keyLines.forEach((line) => { + const trimmedLine = line.trim(); + if (trimmedLine && !keySet.has(trimmedLine)) { + keySet.add(trimmedLine); + deduplicatedKeys.push(trimmedLine); + } + }); + + const afterCount = deduplicatedKeys.length; + const deduplicatedKeyText = deduplicatedKeys.join('\n'); + + // 更新表单和状态 + if (formApiRef.current) { + formApiRef.current.setValue('key', deduplicatedKeyText); + } + handleInputChange('key', deduplicatedKeyText); + + // 显示去重结果 + const message = t( + '去重完成:去重前 {{before}} 个密钥,去重后 {{after}} 个密钥', + { + before: beforeCount, + after: afterCount, + }, + ); + + if (beforeCount === afterCount) { + showInfo(t('未发现重复密钥')); + } else { + showSuccess(message); + } + }; + + const addCustomModels = () => { + if (customModel.trim() === '') return; + const modelArray = customModel.split(',').map((model) => model.trim()); + + let localModels = [...inputs.models]; + let localModelOptions = [...modelOptions]; + const addedModels = []; + + modelArray.forEach((model) => { + if (model && !localModels.includes(model)) { + localModels.push(model); + localModelOptions.push({ + key: model, + label: model, + value: model, + }); + addedModels.push(model); + } + }); + + setModelOptions(localModelOptions); + setCustomModel(''); + handleInputChange('models', localModels); + + if (addedModels.length > 0) { + showSuccess( + t('已新增 {{count}} 个模型:{{list}}', { + count: addedModels.length, + list: addedModels.join(', '), + }), + ); + } else { + showInfo(t('未发现新增模型')); + } + }; + + const batchAllowed = (!isEdit || isMultiKeyChannel) && inputs.type !== 57; + const batchExtra = batchAllowed ? ( + + {!isEdit && ( + { + const checked = e.target.checked; + + if (!checked && vertexFileList.length > 1) { + Modal.confirm({ + title: t('切换为单密钥模式'), + content: t( + '将仅保留第一个密钥文件,其余文件将被移除,是否继续?', + ), + onOk: () => { + const firstFile = vertexFileList[0]; + const firstKey = vertexKeys[0] ? [vertexKeys[0]] : []; + + setVertexFileList([firstFile]); + setVertexKeys(firstKey); + + formApiRef.current?.setValue('vertex_files', [firstFile]); + setInputs((prev) => ({ ...prev, vertex_files: [firstFile] })); + + setBatch(false); + setMultiToSingle(false); + setMultiKeyMode('random'); + }, + onCancel: () => { + setBatch(true); + }, + centered: true, + }); + return; + } + + setBatch(checked); + if (!checked) { + setMultiToSingle(false); + setMultiKeyMode('random'); + } else { + // 批量模式下禁用手动输入,并清空手动输入的内容 + setUseManualInput(false); + if (inputs.type === 41) { + // 清空手动输入的密钥内容 + if (formApiRef.current) { + formApiRef.current.setValue('key', ''); + } + handleInputChange('key', ''); + } + } + }} + > + {t('批量创建')} + + )} + {batch && ( + <> + { + setMultiToSingle((prev) => { + const nextValue = !prev; + setInputs((prevInputs) => { + const newInputs = { ...prevInputs }; + if (nextValue) { + newInputs.multi_key_mode = multiKeyMode; + } else { + delete newInputs.multi_key_mode; + } + return newInputs; + }); + return nextValue; + }); + }} + > + {t('密钥聚合模式')} + + + {inputs.type !== 41 && ( + + )} + + )} + + ) : null; + + const channelOptionList = useMemo( + () => + CHANNEL_OPTIONS.map((opt) => ({ + ...opt, + // 保持 label 为纯文本以支持搜索 + label: opt.label, + })), + [], + ); + + const renderChannelOption = (renderProps) => { + const { + disabled, + selected, + label, + value, + focused, + className, + style, + onMouseEnter, + onClick, + ...rest + } = renderProps; + + const searchWords = channelSearchValue ? [channelSearchValue] : []; + + // 构建样式类名 + const optionClassName = [ + 'flex items-center gap-3 px-3 py-2 transition-all duration-200 rounded-lg mx-2 my-1', + focused && 'bg-blue-50 shadow-sm', + selected && + 'bg-blue-100 text-blue-700 shadow-lg ring-2 ring-blue-200 ring-opacity-50', + disabled && 'opacity-50 cursor-not-allowed', + !disabled && 'hover:bg-gray-50 hover:shadow-md cursor-pointer', + className, + ] + .filter(Boolean) + .join(' '); + + return ( +
!disabled && onClick()} + onMouseEnter={(e) => onMouseEnter()} + > +
+
+ {getChannelIcon(value)} +
+
+ +
+ {selected && ( +
+ + + +
+ )} +
+
+ ); + }; + + return ( + <> + + + + {isEdit ? t('编辑') : t('新建')} + + + {isEdit ? t('更新渠道信息') : t('创建新的渠道')} + + + {!isEdit && ( + + )} + + } + bodyStyle={{ padding: '0' }} + visible={props.visible} + width={isMobile ? '100%' : 600} + footer={ +
+ + +
+ } + closeIcon={null} + onCancel={() => handleCancel()} + > +
(formApiRef.current = api)} + onSubmit={submit} + > + {() => { + const advancedSettingsContent = ( +
+ {/* Upstream Model Management Section */} + {MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type) && ( +
+ + {t('上游模型管理')} + + + + handleChannelOtherSettingsChange( + 'upstream_model_update_check_enabled', + value, + ) + } + extraText={t( + '开启后由后端定时任务检测该渠道上游模型变化', + )} + /> + + handleChannelOtherSettingsChange('upstream_model_update_auto_sync_enabled', value) + } + extraText={t('开启后检测到新增模型会自动加入当前渠道模型列表')} + /> + + handleInputChange( + 'upstream_model_update_ignored_models', + value, + ) + } + showClear + /> +
+ {t('上次检测时间')}:  + {formatUnixTime( + inputs.upstream_model_update_last_check_time, + )} +
+
+ {t('上次检测到可加入模型')}:  + {upstreamDetectedModels.length === 0 ? ( + t('暂无') + ) : ( + <> + + {upstreamDetectedModels.join(', ')} +
+ } + > + + {upstreamDetectedModelsPreview.join(', ')} + + + + {upstreamDetectedModelsOmittedCount > 0 + ? t('(共 {{total}} 个,省略 {{omit}} 个)', { + total: upstreamDetectedModels.length, + omit: upstreamDetectedModelsOmittedCount, + }) + : t('(共 {{total}} 个)', { + total: upstreamDetectedModels.length, + })} + + + )} +
+
+ )} + + {/* Request Config Section */} +
+ + {t('请求配置')} + + +
+
+ {t('参数覆盖')} + + + applyParamOverrideTemplate('operations', 'fill') }, + { node: 'item', name: t('填充旧模板'), onClick: () => applyParamOverrideTemplate('legacy', 'fill') }, + { node: 'item', name: t('清空'), onClick: clearParamOverride }, + ]} + > + + + +
+ + {t('此项可选,用于覆盖请求参数。不支持覆盖 stream 参数')} + +
+
+ + {paramOverrideMeta.tagLabel} + + +
+
+                        {paramOverrideMeta.preview}
+                      
+
+
+ + + handleInputChange('header_override', value) + } + extraText={ +
+
+ + handleInputChange( + 'header_override', + JSON.stringify({ '*': true, 're:^X-Trace-.*$': true, 'X-Foo': '{client_header:X-Foo}', Authorization: 'Bearer {api_key}', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0' }, null, 2), + ) + } + > + {t('填入模板')} + + + handleInputChange('header_override', JSON.stringify({ '*': true }, null, 2)) + } + > + {t('填入透传模版')} + + formatJsonField('header_override')} + > + {t('格式化')} + +
+
+ + {t('支持变量:')} + +
+
+ {t('渠道密钥')}: {'{api_key}'} +
+
+
+
+ } + showClear + /> + + handleInputChange('status_code_mapping', value) + } + template={STATUS_CODE_MAPPING_EXAMPLE} + templateLabel={t('填入模板')} + editorType='keyValue' + formApi={formApiRef.current} + extraText={t('键为原状态码,值为要复写的状态码,仅影响本地判断')} + /> +
+ + {/* Channel Behavior Section */} +
+ + {t('渠道行为')} + + + handleInputChange('tag', value)} + /> + handleInputChange('remark', value)} + /> + + + + handleInputChange('priority', value)} + style={{ width: '100%' }} + /> + + + handleInputChange('weight', value)} + style={{ width: '100%' }} + /> + + + + {inputs.type === 1 && ( + <> +
+ {t('字段透传控制')} +
+ handleChannelOtherSettingsChange('allow_service_tier', value)} extraText={t('service_tier 字段用于指定服务层级,允许透传可能导致实际计费高于预期。默认关闭以避免额外费用')} /> + handleChannelOtherSettingsChange('disable_store', value)} extraText={t('store 字段用于授权 OpenAI 存储请求数据以评估和优化产品。默认关闭,开启后可能导致 Codex 无法正常使用')} /> + handleChannelOtherSettingsChange('allow_safety_identifier', value)} extraText={t('safety_identifier 字段用于帮助 OpenAI 识别可能违反使用政策的应用程序用户。默认关闭以保护用户隐私')} /> + handleChannelOtherSettingsChange('allow_include_obfuscation', value)} extraText={t('include_obfuscation 用于控制 Responses 流混淆字段。默认关闭以避免客户端关闭该安全保护')} /> + + )} + + {inputs.type === 14 && ( + <> +
+ {t('字段透传控制')} +
+ handleChannelOtherSettingsChange('allow_service_tier', value)} extraText={t('service_tier 字段用于指定服务层级,允许透传可能导致实际计费高于预期。默认关闭以避免额外费用')} /> + handleChannelOtherSettingsChange('allow_inference_geo', value)} extraText={t('inference_geo 字段用于控制 Claude 数据驻留推理区域。默认关闭以避免未经授权透传地域信息')} /> + handleChannelOtherSettingsChange('allow_speed', value)} extraText={t('speed 字段用于控制 Claude 推理速度模式。默认关闭以避免意外切换到 fast 模式')} /> + + )} +
+ + {/* Extra Settings Section */} +
+ + {t('额外设置')} + + + {inputs.type === 14 && ( + handleChannelOtherSettingsChange('claude_beta_query', value)} extraText={t('开启后,该渠道请求 Claude 时将强制追加 ?beta=true(无需客户端手动传参)')} /> + )} + + {inputs.type === 1 && ( + handleChannelSettingsChange('force_format', value)} extraText={t('强制将响应格式化为 OpenAI 标准格式(只适用于OpenAI渠道类型)')} /> + )} + + handleChannelSettingsChange('thinking_to_content', value)} extraText={t('将 reasoning_content 转换为 标签拼接到内容中')} /> + handleChannelSettingsChange('pass_through_body_enabled', value)} extraText={t('启用请求体透传功能')} /> + + handleChannelSettingsChange('proxy', value)} showClear extraText={t('用于配置网络代理,支持 socks5 协议')} /> + + handleChannelSettingsChange('system_prompt', value)} autosize showClear extraText={t('用户优先:如果用户在请求中指定了系统提示词,将优先使用用户的设置')} /> + handleChannelSettingsChange('system_prompt_override', value)} extraText={t('如果用户请求中包含系统提示词,则使用此设置拼接到用户的系统提示词前面')} /> +
+ + ); + + return ( + <> + +
+ {!isEdit && clipboardConfig && ( + + {t('检测到剪贴板中的连接信息')} +
+ + +
+
+ } + /> + )} + {/* Core Configuration Card - Always Visible */} + + {/* Header */} +
+ + + +
+ + {t('核心配置')} + +
+ {t('创建渠道所需的基本信息')} +
+
+
+ + {isIonetChannel && ( + + + {ionetMetadata?.deployment_id && ( + + )} + + + )} + + setChannelSearchValue(value)} + renderOptionItem={renderChannelOption} + onChange={(value) => handleInputChange('type', value)} + disabled={isIonetLocked} + /> + + {inputs.type === 57 && ( + + )} + + {inputs.type === 20 && ( + { + setIsEnterpriseAccount(value); + handleInputChange('is_enterprise_account', value); + }} + extraText={t( + '企业账户为特殊返回格式,需要特殊处理,如果非企业账户,请勿勾选', + )} + initValue={inputs.is_enterprise_account} + /> + )} + + handleInputChange('name', value)} + autoComplete='new-password' + /> + + {inputs.type === 33 && ( + <> + { + handleChannelOtherSettingsChange( + 'aws_key_type', + value, + ); + }} + extraText={t( + 'AK/SK 模式:使用 AccessKey 和 SecretAccessKey;API Key 模式:使用 API Key', + )} + /> + + )} + + {inputs.type === 41 && ( + { + // 更新设置中的 vertex_key_type + handleChannelOtherSettingsChange( + 'vertex_key_type', + value, + ); + // 切换为 api_key 时,关闭批量与手动/文件切换,并清理已选文件 + if (value === 'api_key') { + setBatch(false); + setUseManualInput(false); + setVertexKeys([]); + setVertexFileList([]); + if (formApiRef.current) { + formApiRef.current.setValue('vertex_files', []); + } + } + }} + extraText={ + inputs.vertex_key_type === 'api_key' + ? t('API Key 模式下不支持批量创建') + : t('JSON 模式支持手动输入或上传服务账号 JSON') + } + /> + )} + {batch ? ( + inputs.type === 41 && + (inputs.vertex_key_type || 'json') === 'json' ? ( + } + dragMainText={t('点击上传文件或拖拽文件到这里')} + dragSubText={t('仅支持 JSON 文件,支持多文件')} + style={{ marginTop: 10 }} + uploadTrigger='custom' + beforeUpload={() => false} + onChange={handleVertexUploadChange} + fileList={vertexFileList} + rules={ + isEdit + ? [] + : [ + { + required: true, + message: t('请上传密钥文件'), + }, + ] + } + extraText={batchExtra} + /> + ) : ( + handleInputChange('key', value)} + disabled={isIonetLocked} + extraText={ +
+ {isEdit && + isMultiKeyChannel && + keyMode === 'append' && ( + + {t( + '追加模式:新密钥将添加到现有密钥列表的末尾', + )} + + )} + {isEdit && ( + + )} + {batchExtra} +
+ } + showClear + /> + ) + ) : ( + <> + {inputs.type === 57 ? ( + <> + + handleInputChange('key', value) + } + disabled={isIonetLocked} + extraText={ +
+ + {t( + '仅支持 JSON 对象,必须包含 access_token 与 account_id', + )} + + + + + {isEdit && ( + + )} + + {isEdit && ( + + )} + {batchExtra} + +
+ } + autosize + showClear + /> + + setCodexOAuthModalVisible(false)} + onSuccess={handleCodexOAuthGenerated} + /> + + ) : inputs.type === 41 && + (inputs.vertex_key_type || 'json') === 'json' ? ( + <> + {!batch && ( +
+ + {t('密钥输入方式')} + + + + + +
+ )} + + {batch && ( + + )} + + {useManualInput && !batch ? ( + + handleInputChange('key', value) + } + extraText={ +
+ + {t('请输入完整的 JSON 格式密钥内容')} + + {isEdit && + isMultiKeyChannel && + keyMode === 'append' && ( + + {t( + '追加模式:新密钥将添加到现有密钥列表的末尾', + )} + + )} + {isEdit && ( + + )} + {batchExtra} +
+ } + autosize + showClear + /> + ) : ( + } + dragMainText={t('点击上传文件或拖拽文件到这里')} + dragSubText={t('仅支持 JSON 文件')} + style={{ marginTop: 10 }} + uploadTrigger='custom' + beforeUpload={() => false} + onChange={handleVertexUploadChange} + fileList={vertexFileList} + rules={ + isEdit + ? [] + : [ + { + required: true, + message: t('请上传密钥文件'), + }, + ] + } + extraText={batchExtra} + /> + )} + + ) : ( + + handleInputChange('key', value) + } + extraText={ +
+ {isEdit && + isMultiKeyChannel && + keyMode === 'append' && ( + + {t( + '追加模式:新密钥将添加到现有密钥列表的末尾', + )} + + )} + {isEdit && ( + + )} + {batchExtra} +
+ } + showClear + /> + )} + + )} + + {isEdit && isMultiKeyChannel && ( + setKeyMode(value)} + extraText={ + + {keyMode === 'replace' + ? t('覆盖模式:将完全替换现有的所有密钥') + : t('追加模式:将新密钥添加到现有密钥列表末尾')} + + } + /> + )} + {batch && multiToSingle && ( + <> + { + setMultiKeyMode(value); + handleInputChange('multi_key_mode', value); + }} + /> + {inputs.multi_key_mode === 'polling' && ( + + )} + + )} + + {inputs.type === 18 && ( + handleInputChange('other', value)} + showClear + /> + )} + + {inputs.type === 41 && ( + handleInputChange('other', value)} + rules={[ + { required: true, message: t('请填写部署地区') }, + ]} + template={REGION_EXAMPLE} + templateLabel={t('填入模板')} + editorType='region' + formApi={formApiRef.current} + extraText={t('设置默认地区和特定模型的专用地区')} + /> + )} + + {inputs.type === 21 && ( + handleInputChange('other', value)} + showClear + /> + )} + + {inputs.type === 39 && ( + handleInputChange('other', value)} + showClear + /> + )} + + {inputs.type === 49 && ( + handleInputChange('other', value)} + showClear + /> + )} + + {inputs.type === 1 && ( + + handleInputChange('openai_organization', value) + } + /> + )} + + {/* API Configuration Section */} + {showApiConfigCard && ( +
+ + {inputs.type === 40 && ( + + {t('邀请链接')}: + + window.open( + 'https://cloud.siliconflow.cn/i/hij0YNTZ', + ) + } + > + https://cloud.siliconflow.cn/i/hij0YNTZ + +
+ } + className='!rounded-lg' + /> + )} + + {inputs.type === 3 && ( + <> + +
+ + handleInputChange('base_url', value) + } + showClear + disabled={isIonetLocked} + /> +
+
+ + handleInputChange('other', value) + } + showClear + /> +
+
+ + handleChannelOtherSettingsChange( + 'azure_responses_version', + value, + ) + } + showClear + /> +
+ + )} + + {inputs.type === 8 && ( + <> + +
+ + handleInputChange('base_url', value) + } + showClear + disabled={isIonetLocked} + /> +
+ + )} + + {inputs.type === 37 && ( + + )} + + {inputs.type !== 3 && + inputs.type !== 8 && + inputs.type !== 22 && + inputs.type !== 36 && + (inputs.type !== 45 || doubaoApiEditUnlocked) && ( +
+ + handleInputChange('base_url', value) + } + showClear + disabled={isIonetLocked} + extraText={t( + '对于官方渠道,new-api已经内置地址,除非是第三方代理站点或者Azure的特殊接入地址,否则不需要填写', + )} + /> +
+ )} + + {inputs.type === 22 && ( +
+ + handleInputChange('base_url', value) + } + showClear + disabled={isIonetLocked} + /> +
+ )} + + {inputs.type === 36 && ( +
+ + handleInputChange('base_url', value) + } + showClear + disabled={isIonetLocked} + /> +
+ )} + + {inputs.type === 45 && !doubaoApiEditUnlocked && ( +
+ + handleInputChange('base_url', value) + } + optionList={[ + { + value: 'https://ark.cn-beijing.volces.com', + label: 'https://ark.cn-beijing.volces.com', + }, + { + value: + 'https://ark.ap-southeast.bytepluses.com', + label: + 'https://ark.ap-southeast.bytepluses.com', + }, + { + value: DEPRECATED_DOUBAO_CODING_PLAN_BASE_URL, + label: doubaoCodingPlanOptionLabel, + disabled: !canKeepDeprecatedDoubaoCodingPlan, + }, + ]} + defaultValue='https://ark.cn-beijing.volces.com' + disabled={isIonetLocked} + /> +
+ )} + + )} + + {/* Model Selection - Part of Core Config */} + setModelSearchValue(value)} + innerBottomSlot={ + modelSearchHintText ? ( + + {modelSearchHintText} + + ) : null + } + style={{ width: '100%' }} + onChange={(value) => handleInputChange('models', value)} + renderSelectedItem={(optionNode) => { + const modelName = String(optionNode?.value ?? ''); + return { + isRenderInTag: true, + content: ( + { + e.stopPropagation(); + const ok = await copy(modelName); + if (ok) { + showSuccess( + t('已复制:{{name}}', { name: modelName }), + ); + } else { + showError(t('复制失败')); + } + }} + > + {optionNode.label || modelName} + + ), + }; + }} + extraText={ + + + {MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type) && ( + + )} + handleInputChange('models', fullModels) }, + ...(inputs.type === 4 && isEdit ? [{ node: 'item', name: t('Ollama 模型管理'), onClick: () => setOllamaModalVisible(true) }] : []), + { node: 'divider' }, + { node: 'item', name: t('复制所有模型'), onClick: () => { + if (inputs.models.length === 0) { showInfo(t('没有模型可以复制')); return; } + try { copy(inputs.models.join(',')); showSuccess(t('模型列表已复制到剪贴板')); } catch (error) { showError(t('复制失败')); } + }}, + { node: 'item', name: t('清除所有模型'), type: 'danger', onClick: () => handleInputChange('models', []) }, + ...((modelGroups && modelGroups.length > 0) ? [ + { node: 'divider' }, + ...modelGroups.map((group) => ({ + node: 'item', + name: group.name, + onClick: () => { + let items = []; + try { + if (Array.isArray(group.items)) { items = group.items; } + else if (typeof group.items === 'string') { + const parsed = JSON.parse(group.items || '[]'); + if (Array.isArray(parsed)) items = parsed; + } + } catch {} + const current = formApiRef.current?.getValue('models') || inputs.models || []; + const merged = Array.from(new Set([...current, ...items].map((m) => (m || '').trim()).filter(Boolean))); + handleInputChange('models', merged); + }, + })), + ] : []), + ]} + > + + + + } + /> + + {/* Custom Model Name - Core Config */} + setCustomModel(value.trim())} + value={customModel} + suffix={ + + } + /> + + {/* Groups - Core Config */} + handleInputChange('groups', value)} + /> + + {/* Model Mapping - Core Config */} + + handleInputChange('model_mapping', value) + } + template={MODEL_MAPPING_EXAMPLE} + templateLabel={t('填入模板')} + editorType='keyValue' + formApi={formApiRef.current} + renderStringValueSuffix={({ pairKey, value }) => { + if (!MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type)) { + return null; + } + const disabled = !String(pairKey ?? '').trim(); + return ( + + + } + width={700} + style={{ maxWidth: '90vw' }} + > + + + + setParamOverrideEditorVisible(false)} + onSave={(nextValue) => { + handleInputChange('param_override', nextValue); + setParamOverrideEditorVisible(false); + }} + /> + + { + handleInputChange('models', selectedModels); + showSuccess(t('模型列表已更新')); + setModelModalVisible(false); + }} + onCancel={() => setModelModalVisible(false)} + /> + + { + const modelName = String(selectedModel ?? '').trim(); + if (!modelName) { + showError(t('请先选择模型!')); + return; + } + + const mappingKey = String(modelMappingValueKey ?? '').trim(); + if (!mappingKey) { + setModelMappingValueModalVisible(false); + return; + } + + let parsed = {}; + const currentMapping = inputs.model_mapping; + if (typeof currentMapping === 'string' && currentMapping.trim()) { + try { + parsed = JSON.parse(currentMapping); + } catch (error) { + parsed = {}; + } + } else if ( + currentMapping && + typeof currentMapping === 'object' && + !Array.isArray(currentMapping) + ) { + parsed = currentMapping; + } + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + parsed = {}; + } + + parsed[mappingKey] = modelName; + const nextMapping = JSON.stringify(parsed, null, 2); + handleInputChange('model_mapping', nextMapping); + if (formApiRef.current) { + formApiRef.current.setValue('model_mapping', nextMapping); + } + setModelMappingValueModalVisible(false); + }} + onCancel={() => setModelMappingValueModalVisible(false)} + /> + + setOllamaModalVisible(false)} + channelId={channelId} + channelInfo={inputs} + onModelsUpdate={(options = {}) => { + // 当模型更新后,重新获取模型列表以更新表单 + fetchUpstreamModelList('models', { silent: !!options.silent }); + }} + onApplyModels={({ mode, modelIds } = {}) => { + if (!Array.isArray(modelIds) || modelIds.length === 0) { + return; + } + const existingModels = Array.isArray(inputs.models) + ? inputs.models.map(String) + : []; + const incoming = modelIds.map(String); + const nextModels = Array.from( + new Set([...existingModels, ...incoming]), + ); + + handleInputChange('models', nextModels); + if (formApiRef.current) { + formApiRef.current.setValue('models', nextModels); + } + showSuccess(t('模型列表已追加更新')); + }} + /> + + ); +}; + +export default EditChannelModal; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/table/model-deployments/DeploymentsTable.jsx b/_all-in-one-upload/new-api/web/classic/src/components/table/model-deployments/DeploymentsTable.jsx new file mode 100644 index 0000000..7a7eb98 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/table/model-deployments/DeploymentsTable.jsx @@ -0,0 +1,247 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useMemo, useState } from 'react'; +import { Empty } from '@douyinfe/semi-ui'; +import CardTable from '../../common/ui/CardTable'; +import { + IllustrationNoResult, + IllustrationNoResultDark, +} from '@douyinfe/semi-illustrations'; +import { getDeploymentsColumns } from './DeploymentsColumnDefs'; + +// Import all the new modals +import ViewLogsModal from './modals/ViewLogsModal'; +import ExtendDurationModal from './modals/ExtendDurationModal'; +import ViewDetailsModal from './modals/ViewDetailsModal'; +import UpdateConfigModal from './modals/UpdateConfigModal'; +import ConfirmationDialog from './modals/ConfirmationDialog'; + +const DeploymentsTable = (deploymentsData) => { + const { + deployments, + loading, + searching, + activePage, + pageSize, + deploymentCount, + compactMode, + visibleColumns, + rowSelection, + batchOperationsEnabled = true, + handlePageChange, + handlePageSizeChange, + handleRow, + t, + COLUMN_KEYS, + // Column functions and data + startDeployment, + restartDeployment, + deleteDeployment, + syncDeploymentToChannel, + setEditingDeployment, + setShowEdit, + refresh, + } = deploymentsData; + + // Modal states + const [selectedDeployment, setSelectedDeployment] = useState(null); + const [showLogsModal, setShowLogsModal] = useState(false); + const [showExtendModal, setShowExtendModal] = useState(false); + const [showDetailsModal, setShowDetailsModal] = useState(false); + const [showConfigModal, setShowConfigModal] = useState(false); + const [showConfirmDialog, setShowConfirmDialog] = useState(false); + const [confirmOperation, setConfirmOperation] = useState('delete'); + + // Enhanced modal handlers + const handleViewLogs = (deployment) => { + setSelectedDeployment(deployment); + setShowLogsModal(true); + }; + + const handleExtendDuration = (deployment) => { + setSelectedDeployment(deployment); + setShowExtendModal(true); + }; + + const handleViewDetails = (deployment) => { + setSelectedDeployment(deployment); + setShowDetailsModal(true); + }; + + const handleUpdateConfig = (deployment, operation = 'update') => { + setSelectedDeployment(deployment); + if (operation === 'delete' || operation === 'destroy') { + setConfirmOperation(operation); + setShowConfirmDialog(true); + } else { + setShowConfigModal(true); + } + }; + + const handleConfirmAction = () => { + if ( + selectedDeployment && + (confirmOperation === 'delete' || confirmOperation === 'destroy') + ) { + deleteDeployment(selectedDeployment.id); + } + setShowConfirmDialog(false); + setSelectedDeployment(null); + }; + + const handleModalSuccess = (updatedDeployment) => { + // Refresh the deployments list + refresh?.(); + }; + + // Get all columns + const allColumns = useMemo(() => { + return getDeploymentsColumns({ + t, + COLUMN_KEYS, + startDeployment, + restartDeployment, + deleteDeployment, + setEditingDeployment, + setShowEdit, + refresh, + activePage, + deployments, + // Enhanced handlers + onViewLogs: handleViewLogs, + onExtendDuration: handleExtendDuration, + onViewDetails: handleViewDetails, + onUpdateConfig: handleUpdateConfig, + onSyncToChannel: syncDeploymentToChannel, + }); + }, [ + t, + COLUMN_KEYS, + startDeployment, + restartDeployment, + deleteDeployment, + syncDeploymentToChannel, + setEditingDeployment, + setShowEdit, + refresh, + activePage, + deployments, + ]); + + // Filter columns based on visibility settings + const getVisibleColumns = () => { + return allColumns.filter((column) => visibleColumns[column.key]); + }; + + const visibleColumnsList = useMemo(() => { + return getVisibleColumns(); + }, [visibleColumns, allColumns]); + + const tableColumns = useMemo(() => { + if (compactMode) { + // In compact mode, remove fixed columns and adjust widths + return visibleColumnsList.map(({ fixed, width, ...rest }) => ({ + ...rest, + width: width ? Math.max(width * 0.8, 80) : undefined, // Reduce width by 20% but keep minimum + })); + } + return visibleColumnsList; + }, [compactMode, visibleColumnsList]); + + return ( + <> + } + darkModeImage={ + + } + description={t('搜索无结果')} + style={{ padding: 30 }} + /> + } + className='rounded-xl overflow-hidden' + size='middle' + loading={loading || searching} + /> + + {/* Enhanced Modals */} + setShowLogsModal(false)} + deployment={selectedDeployment} + t={t} + /> + + setShowExtendModal(false)} + deployment={selectedDeployment} + onSuccess={handleModalSuccess} + t={t} + /> + + setShowDetailsModal(false)} + deployment={selectedDeployment} + t={t} + /> + + setShowConfigModal(false)} + deployment={selectedDeployment} + onSuccess={handleModalSuccess} + t={t} + /> + + setShowConfirmDialog(false)} + onConfirm={handleConfirmAction} + title={t('确认操作')} + type='danger' + deployment={selectedDeployment} + operation={confirmOperation} + t={t} + /> + + ); +}; + +export default DeploymentsTable; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/table/model-pricing/filter/PricingTags.jsx b/_all-in-one-upload/new-api/web/classic/src/components/table/model-pricing/filter/PricingTags.jsx new file mode 100644 index 0000000..4dee913 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/table/model-pricing/filter/PricingTags.jsx @@ -0,0 +1,109 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React from 'react'; +import SelectableButtonGroup from '../../../common/ui/SelectableButtonGroup'; + +/** + * 模型标签筛选组件 + * @param {string|'all'} filterTag 当前选中的标签 + * @param {Function} setFilterTag setter + * @param {Array} models 当前过滤后模型列表(用于计数) + * @param {Array} allModels 所有模型列表(用于获取所有标签) + * @param {boolean} loading 是否加载中 + * @param {Function} t i18n + */ +const PricingTags = ({ + filterTag, + setFilterTag, + models = [], + allModels = [], + loading = false, + t, +}) => { + // 提取系统所有标签 + const getAllTags = React.useMemo(() => { + const tagSet = new Set(); + + (allModels.length > 0 ? allModels : models).forEach((model) => { + if (model.tags) { + model.tags + .split(/[,;|]+/) // 逗号、分号或竖线(保留空格,允许多词标签如 "open weights") + .map((tag) => tag.trim()) + .filter(Boolean) + .forEach((tag) => tagSet.add(tag.toLowerCase())); + } + }); + + return Array.from(tagSet).sort((a, b) => a.localeCompare(b)); + }, [allModels, models]); + + // 计算标签对应的模型数量 + const getTagCount = React.useCallback( + (tag) => { + if (tag === 'all') return models.length; + + const tagLower = tag.toLowerCase(); + return models.filter((model) => { + if (!model.tags) return false; + return model.tags + .toLowerCase() + .split(/[,;|]+/) + .map((tg) => tg.trim()) + .includes(tagLower); + }).length; + }, + [models], + ); + + const items = React.useMemo(() => { + const result = [ + { + value: 'all', + label: t('全部标签'), + tagCount: getTagCount('all'), + }, + ]; + + getAllTags.forEach((tag) => { + const count = getTagCount(tag); + result.push({ + value: tag, + label: tag, + tagCount: count, + }); + }); + + return result; + }, [getAllTags, getTagCount, t, models.length]); + + return ( + + ); +}; + +export default PricingTags; diff --git a/_all-in-one-upload/new-api/web/classic/src/components/table/model-pricing/modal/ModelDetailSideSheet.jsx b/_all-in-one-upload/new-api/web/classic/src/components/table/model-pricing/modal/ModelDetailSideSheet.jsx new file mode 100644 index 0000000..f187339 --- /dev/null +++ b/_all-in-one-upload/new-api/web/classic/src/components/table/model-pricing/modal/ModelDetailSideSheet.jsx @@ -0,0 +1,132 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React from 'react'; +import { SideSheet, Typography, Button, Divider } from '@douyinfe/semi-ui'; +import { IconClose } from '@douyinfe/semi-icons'; + +import { useIsMobile } from '../../../../hooks/common/useIsMobile'; +import ModelHeader from './components/ModelHeader'; +import ModelBasicInfo from './components/ModelBasicInfo'; +import ModelEndpoints from './components/ModelEndpoints'; +import ModelPricingTable from './components/ModelPricingTable'; +import DynamicPricingBreakdown from './components/DynamicPricingBreakdown'; + +const { Text } = Typography; + +const ModelDetailSideSheet = ({ + visible, + onClose, + modelData, + groupRatio, + currency, + siteDisplayType, + tokenUnit, + displayPrice, + showRatio, + usableGroup, + vendorsMap, + endpointMap, + autoGroups, + t, +}) => { + const isMobile = useIsMobile(); + + return ( + + } + bodyStyle={{ + padding: '0', + display: 'flex', + flexDirection: 'column', + borderBottom: '1px solid var(--semi-color-border)', + }} + visible={visible} + width={isMobile ? '100%' : 600} + closeIcon={ + + ) : ( + + )} + + + + + + + + ); + }, + }, + ]; + }, [t, planTitleMap]); + + return ( + + + {t('管理')} + + + {t('用户订阅管理')} + + + {user?.username || '-'} (ID: {user?.id || '-'}) + + + } + > +
+ {/* 顶部操作栏:新增订阅 */} +
+
+ + ) +} + +function InputGroupTextarea({ + className, + ...props +}: React.ComponentProps<'textarea'>) { + return ( +