Compare commits

...

19 Commits
dev ... v2.1.3

Author SHA1 Message Date
github-actions[bot]
a2d2109a2b release: v2.1.3 2025-12-16 06:44:53 +00:00
YeonGyu-Kim
9d48a72e77 Update README 2025-12-16 15:40:36 +09:00
YeonGyu-Kim
d93f2aaf4a fix(hook-message-injector): add validation to prevent empty message injection and improve logging
- Add content validation in injectHookMessage() to prevent empty hook content injection
- Add logging to claude-code-hooks and keyword-detector for better debugging
- Document timing issues in empty-message-sanitizer comments
- Update README with improved setup instructions

🤖 Generated with assistance of [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 15:23:01 +09:00
YeonGyu-Kim
e779ae758b fix(google-auth): enable google antigravity auth by default (#66)
Make google_auth enabled by default (true) while still allowing users to disable it by setting google_auth: false.

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 15:19:31 +09:00
YeonGyu-Kim
82263577f8 docs: fix outdated librarian model and add empty-message-sanitizer hook documentation
- Updated AGENTS.md with correct librarian model (anthropic/claude-sonnet-4-5)
- Added empty-message-sanitizer hook documentation to README files (English, Korean, Japanese)
- Ensures documentation accuracy for developers

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 15:00:42 +09:00
YeonGyu-Kim
efeeac8531 feat(hooks): add empty-message-sanitizer to prevent API errors from empty chat messages
Add new hook that uses the `experimental.chat.messages.transform` hook to prevent 'non-empty content' API errors by injecting placeholder text into empty messages BEFORE they're sent to the API.

This is a preventive fix - unlike session-recovery which fixes errors after they occur, this hook prevents the error from happening by sanitizing messages before API transmission.

Files:
- src/hooks/empty-message-sanitizer/index.ts (new hook implementation)
- src/hooks/index.ts (export hook function)
- src/config/schema.ts (add hook to HookName type)
- src/index.ts (wire up hook to plugin)

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 14:52:32 +09:00
YeonGyu-Kim
0317102dd9 Merge branch 'fix-empty-message-content' 2025-12-16 14:32:39 +09:00
YeonGyu-Kim
ac9ec62a53 fix(antigravity): handle multiple FREE tier ID formats in onboarding
- Added isFreeTier() helper to match 'free', 'free-tier', or any tier starting with 'free'
- Replaced all hardcoded 'FREE' comparisons with isFreeTier() calls
- Fixes issue where FREE tier users couldn't authenticate due to tier ID mismatch
- Added comprehensive debug logging for troubleshooting (ANTIGRAVITY_DEBUG=1)
- Verified: onboardUser API now correctly called for FREE tier users

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 14:31:36 +09:00
YeonGyu-Kim
1d910652bb fix(antigravity): implement FREE tier onboarding via onboardUser API
- Removed random project ID generation (doesn't work for FREE tier)
- Added onboardManagedProject() to call onboardUser API for server-assigned managed project ID
- Updated type definitions: AntigravityUserTier, AntigravityOnboardUserPayload
- FREE tier users now get proper project IDs from Google instead of PERMISSION_DENIED errors
- Reference: https://github.com/shekohex/opencode-google-antigravity-auth

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 14:16:19 +09:00
YeonGyu-Kim
d8c74ef584 feat(background-task): add all parameter to cancel all running tasks at once
Allows OmO agent to cleanup all running background tasks before providing final answers.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 14:14:41 +09:00
YeonGyu-Kim
a52948080c fix(antigravity): fix auth on free plan with random project ID fallback
This fix adds CLIProxyAPI-compatible random project ID generation when loadCodeAssist API fails to return a project ID. This allows FREE tier users to use the API without RESOURCE_PROJECT_INVALID errors.

Changes:
1. Added generateRandomProjectId() function matching CLIProxyAPI implementation
2. Changed fallback from empty string "" to generateRandomProjectId()
3. Cache all results (not just when projectId exists)
4. Removed unused ANTIGRAVITY_DEFAULT_PROJECT_ID import

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 14:02:37 +09:00
YeonGyu-Kim
1f62873b61 fix(anthropic-auto-compact): use OpenCode's official compaction mechanism and proper retry
🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 13:59:25 +09:00
YeonGyu-Kim
e433f9ce43 refactor(agents): simplify explore agent prompt for clarity and efficiency
- Reduce prompt from 277 lines to ~100 lines (remove verbose tool examples)
- Add explicit output format structure (<results>, <files>, <answer>, <next_steps>)
- Enhance intent analysis (Literal Request → Actual Need → Success Looks Like)
- Add thoroughness level guidance in description
- Add grep_app strategy section for cross-validation
- Keep core requirements: parallel execution, absolute paths, success/failure criteria

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 13:59:06 +09:00
YeonGyu-Kim
a22a8001b2 fix(session-recovery): Replace empty text parts before injecting new ones
Directly modify empty text parts in storage files before attempting
to inject new parts. This ensures that existing empty text parts are
replaced with placeholder text, fixing the issue where Anthropic API
returns 'messages.X: all messages must have non-empty content' error
even after recovery.

- Added replaceEmptyTextParts function to directly replace empty text parts
- Added findMessagesWithEmptyTextParts function to identify affected messages
- Modified recoverEmptyContentMessage to prioritize replacing existing empty parts

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 13:32:57 +09:00
YeonGyu-Kim
e9675892bd fix(lsp): cleanup orphan LSP servers on process exit
Implement cross-platform process cleanup handlers for LSP servers.

Added registerProcessCleanup() method to LSPServerManager that:
- Kills all spawned LSP server processes on process.exit
- Handles SIGINT (Ctrl+C) - all platforms
- Handles SIGTERM (kill signal) - Unix/macOS/Linux
- Handles SIGBREAK (Ctrl+Break) - Windows specific

This prevents LSP servers from becoming orphan processes when opencode terminates unexpectedly.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 13:18:25 +09:00
YeonGyu-Kim
d8ef6832a3 refactor(omo): balance proactivity with user confirmation in prompt
OmO had a tendency to act without asking questions compared to Claude Code. Even in situations with implicit assumptions, it would rush into work like an unleashed puppy the moment a prompt came in. This commit enhances the Intent Gate prompt to prevent such behavior.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 13:13:37 +09:00
YeonGyu-Kim
cc6ec1ce51 feat(anthropic-auto-compact): Add tool output truncation recovery layer for token limit handling (#63)
- Add storage.ts: Functions to find and truncate largest tool results
- Add TruncateState and TRUNCATE_CONFIG for truncation tracking
- Implement truncate-first recovery: truncate largest output -> retry (10x) -> compact (2x) -> revert (3x)
- Move session error handling to immediate recovery instead of session.idle wait
- Add compactionInProgress tracking to prevent concurrent execution

This fixes GitHub issue #63: "prompt is too long" errors now trigger immediate recovery by truncating the largest tool outputs first before attempting compaction.

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
2025-12-16 10:46:42 +09:00
github-actions[bot]
89e9fd2083 release: v2.1.2 2025-12-15 23:58:46 +00:00
Junho Yeo
da63b09064 fix(session-notification): Replace blocking MessageBox with native toast on Windows (#62)
The previous Windows implementation used System.Windows.Forms.MessageBox
which displays a blocking modal dialog requiring user interaction.

This replaces it with the native Windows.UI.Notifications.ToastNotificationManager
API (Windows 10+) which shows a non-intrusive toast notification in the corner,
consistent with macOS and Linux behavior.

- Uses native Toast API (no external dependencies like BurntToast)
- Non-blocking: notification auto-dismisses
- Graceful degradation: silently fails on older Windows versions
- Fix escaping for each platform (PowerShell: '' for quotes, AppleScript: backslash)
2025-12-16 08:54:55 +09:00
27 changed files with 1177 additions and 504 deletions

View File

@@ -80,7 +80,7 @@ oh-my-opencode/
|-------|-------|---------|
| OmO | anthropic/claude-opus-4-5 | Primary orchestrator, team leader |
| oracle | openai/gpt-5.2 | Strategic advisor, code review, architecture |
| librarian | opencode/big-pickle | Multi-repo analysis, docs lookup, GitHub examples |
| librarian | anthropic/claude-sonnet-4-5 | Multi-repo analysis, docs lookup, GitHub examples |
| explore | opencode/grok-code | Fast codebase exploration, file patterns |
| frontend-ui-ux-engineer | google/gemini-3-pro-preview | UI generation, design-focused |
| document-writer | google/gemini-3-pro-preview | Technical documentation |

View File

@@ -132,6 +132,19 @@ ultrathink を通じて自動的に動作させることもできますが、必
あなたがユーザーの oh-my-opencode 設定を支援する LLM エージェントなら、以下の手順に従ってください:
### ステップ 0: セットアップについてユーザーに尋ねる
OpenCode は様々なモデルをサポートしており、当プラグインは様々な最先端 LLM の使用を提案しています。そのため、ユーザーに以下の質問をする必要があります:
1. Claude Pro/Max のサブスクリプションをお持ちですか?お持ちでない場合、メインオーケストレーターモデルとして 'opencode/big-pickle' (glm 4.6 として知られる) を使用することになります (非推奨)。
-> ユーザーが「はい」と答えた場合、ToDo 追加: "ユーザーの Claude Pro/Max 認証設定"
-> ユーザーが「いいえ」と答えた場合、ToDo 追加: "oh-my-opencode.json の OmO エージェントモデルを opencode/big-pickle にオーバーライド"
2. ChatGPT のサブスクリプションをお持ちですかお持ちでない場合、oracle エージェントに 32k thinking オプション付きの 'claude-opus-4-5' を使用することになります。
-> ユーザーが「はい」と答えた場合、ToDo 追加: "ユーザーの ChatGPT 認証設定"
-> ユーザーが「いいえ」と答えた場合、ToDo 追加: "oh-my-opencode.json の oracle エージェントモデルを 32k thinking 付きの claude-opus-4-5 にオーバーライド"
3. Gemini モデルを統合しますか?統合しない場合、美しいデザインを享受できません。
-> ユーザーが「はい」と答えた場合、ToDo 追加: "Google OAuth 設定"
-> ユーザーが「いいえ」と答えた場合、ToDo 追加: "oh-my-opencode.json の frontend-ui-ux-engineer エージェントモデルを 32k thinking 付きの claude-opus-4-5 にオーバーライド"
### ステップ 1: OpenCode インストール確認
```bash
@@ -147,6 +160,12 @@ OpenCode がインストールされていない場合は、[OpenCode インス
### ステップ 2: oh-my-opencode プラグイン設定
#### JSONC
- `~/.config/opencode/opencode.jsonc` が存在する場合は、そこで `oh-my-opencode` プラグインを追加してください。
#### JSON
- opencode.jsonc が存在しない場合は、以下のコマンドを実行して作成・登録してください。
```bash
mkdir -p ~/.config/opencode
@@ -255,6 +274,22 @@ opencode auth login
プラグインはデフォルトで完璧に動作します。推奨される `google_auth: true` 設定を除き、明示的な要求なしに他の設定を変更したり機能をオフにしないでください。
### セットアップの確認
このドキュメントをもう一度読み、すべて正しく完了したか考えてみてください。
### ユーザーに「おめでとうございます!🎉」と伝える
ユーザーに伝えてください:おめでとうございます!🎉 OhMyOpenCode のセットアップが完了しました!ターミナルで `opencode` と入力して使い始めてください。
そして、この Readme が提案するように、当プラグインが提案するカタログや機能を、あなたが想像できる素晴らしいユースケースの例とともにリストアップしてください。
### 複雑すぎますか?
これが全部大変に感じるなら、一つだけ覚えてください:**プロンプトに `ultrawork` という単語を含めてください。**
それだけです。エージェントが残りを全部自動で処理します。
</details>
@@ -474,6 +509,7 @@ Oh My OpenCode は以下の場所からフックを読み込んで実行しま
- **Background Notification**: バックグラウンドエージェントのタスクが完了すると通知を受け取ります。
- **Session Notification**: エージェントがアイドル状態になると OS 通知を送ります。macOS、Linux、Windows で動作します—エージェントが入力を待っている時を見逃しません。
- **Empty Task Response Detector**: Task ツールが空の応答を返すと検知します。既に空の応答が返ってきているのに、いつまでも待ち続ける状況を防ぎます。
- **Empty Message Sanitizer**: 空のチャットメッセージによるAPIエラーを防止します。送信前にメッセージ内容を自動的にサニタイズします。
- **Grep Output Truncator**: grep は山のようなテキストを返すことがあります。残りのコンテキストウィンドウに応じて動的に出力を切り詰めます—50% の余裕を維持し、最大 50k トークンに制限します。
- **Tool Output Truncator**: 同じ考え方をより広範囲に適用します。Grep、Glob、LSP ツール、AST-grep の出力を切り詰めます。一度の冗長な検索がコンテキスト全体を食いつぶすのを防ぎます。
@@ -612,7 +648,7 @@ OmO を無効化して元の build/plan エージェントを復元するには
}
```
利用可能なフック:`todo-continuation-enforcer`, `context-window-monitor`, `session-recovery`, `session-notification`, `comment-checker`, `grep-output-truncator`, `tool-output-truncator`, `directory-agents-injector`, `directory-readme-injector`, `empty-task-response-detector`, `think-mode`, `anthropic-auto-compact`, `rules-injector`, `background-notification`, `auto-update-checker`, `startup-toast`, `keyword-detector`, `agent-usage-reminder`, `non-interactive-env`, `interactive-bash-session`
利用可能なフック:`todo-continuation-enforcer`, `context-window-monitor`, `session-recovery`, `session-notification`, `comment-checker`, `grep-output-truncator`, `tool-output-truncator`, `directory-agents-injector`, `directory-readme-injector`, `empty-task-response-detector`, `think-mode`, `anthropic-auto-compact`, `rules-injector`, `background-notification`, `auto-update-checker`, `startup-toast`, `keyword-detector`, `agent-usage-reminder`, `non-interactive-env`, `interactive-bash-session`, `empty-message-sanitizer`
### MCPs

View File

@@ -129,7 +129,18 @@ ultrathink 를 통해 자동으로 동작하게 할 수 있지만, 그렇지 않
당신이 사용자의 oh-my-opencode 설정을 돕는 LLM Agent라면, 다음 단계를 따르세요:
### 인간인 당신을 위한 설치 가이드
### 0단계: 사용자에게 설정에 대해 물어보세요.
OpenCode는 다양한 모델을 지원하며, 이 플러그인은 다양한 프론티어 LLM 사용을 제안합니다. 따라서 사용자에게 다음 질문을 해야 합니다:
1. Claude Pro/Max 구독이 있으신가요? 그렇지 않다면 메인 오케스트레이터 모델로 'opencode/big-pickle'(glm 4.6으로 알려짐)을 사용하게 됩니다 (권장하지 않음).
-> 사용자가 '예'라고 답하면, 할 일 추가: "사용자 Claude Pro/Max 인증 설정"
-> 사용자가 '아니오'라고 답하면, 할 일 추가: "oh-my-opencode.json의 OmO 에이전트 모델을 opencode/big-pickle로 재정의"
2. ChatGPT 구독이 있으신가요? 그렇지 않다면 oracle 에이전트에 32k thinking 옵션이 있는 'claude-opus-4-5'를 사용하게 됩니다.
-> 사용자가 '예'라고 답하면, 할 일 추가: "사용자 ChatGPT 인증 설정"
-> 사용자가 '아니오'라고 답하면, 할 일 추가: "oh-my-opencode.json의 oracle 에이전트 모델을 32k thinking이 있는 claude-opus-4-5로 재정의"
3. Gemini 모델을 통합하시겠습니까? 그렇지 않다면 아름다운 디자인을 즐길 수 없습니다.
-> 사용자가 '예'라고 답하면, 할 일 추가: "Google OAuth 설정"
-> 사용자가 '아니오'라고 답하면, 할 일 추가: "oh-my-opencode.json의 frontend-ui-ux-engineer 에이전트 모델을 32k thinking이 있는 claude-opus-4-5로 재정의"
### 1단계: OpenCode 설치 확인
@@ -146,6 +157,12 @@ OpenCode가 설치되어 있지 않다면, [OpenCode 설치 가이드](https://o
### 2단계: oh-my-opencode 플러그인 설정
#### JSONC
- `~/.config/opencode/opencode.jsonc`가 존재한다면, 그곳에 `oh-my-opencode` 플러그인을 추가하세요.
#### JSON
- opencode.jsonc가 없다면, 다음 명령어를 실행하여 생성하고 등록하세요.
```bash
mkdir -p ~/.config/opencode
@@ -254,6 +271,22 @@ opencode auth login
플러그인은 기본값으로 완벽하게 동작합니다. 권장 사항인 `google_auth: true` 설정을 제외하고는, 명시적인 요청 없이 다른 설정을 변경하거나 기능을 끄지 마세요.
### 설정 확인
이 문서를 다시 읽고, 모든 것이 올바르게 수행되었는지 생각해보세요.
### 사용자에게 '축하합니다! 🎉'라고 말하세요
사용자에게 말하세요: 축하합니다! 🎉 OhMyOpenCode 설정을 성공적으로 마쳤습니다! 터미널에 `opencode`를 입력하여 사용을 시작하세요.
그리고 이 리드미가 제안하듯, 우리 플러그인이 제안하는 카탈로그와 기능들을 나열하고, 당신이 상상할 수 있는 멋진 유스케이스 예시를 함께 제시하세요.
### 너무 복잡한가요?
이 모든 것이 벅차게 느껴진다면, 딱 하나만 기억하세요: **프롬프트에 `ultrawork` 라는 단어를 포함시키세요.**
그게 끝입니다. 에이전트가 알아서 나머지를 처리할 겁니다.
</details>
@@ -470,6 +503,7 @@ Oh My OpenCode는 다음 위치의 훅을 읽고 실행합니다:
- **Background Notification**: 백그라운드 에이전트 작업이 완료되면 알림을 받습니다.
- **Session Notification**: 에이전트가 대기 상태가 되면 OS 알림을 보냅니다. macOS, Linux, Windows에서 작동—에이전트가 입력을 기다릴 때 놓치지 마세요.
- **Empty Task Response Detector**: Task 도구가 빈 응답을 반환하면 감지합니다. 이미 빈 응답이 왔는데 무한정 기다리는 상황을 방지합니다.
- **Empty Message Sanitizer**: 빈 채팅 메시지로 인한 API 오류를 방지합니다. 전송 전 메시지 내용을 자동으로 정리합니다.
- **Grep Output Truncator**: grep은 산더미 같은 텍스트를 반환할 수 있습니다. 남은 컨텍스트 윈도우에 따라 동적으로 출력을 축소합니다—50% 여유 공간 유지, 최대 50k 토큰.
- **Tool Output Truncator**: 같은 아이디어, 더 넓은 범위. Grep, Glob, LSP 도구, AST-grep의 출력을 축소합니다. 한 번의 장황한 검색이 전체 컨텍스트를 잡아먹는 것을 방지합니다.
@@ -608,7 +642,7 @@ OmO를 비활성화하고 원래 build/plan 에이전트를 복원하려면:
}
```
사용 가능한 훅: `todo-continuation-enforcer`, `context-window-monitor`, `session-recovery`, `session-notification`, `comment-checker`, `grep-output-truncator`, `tool-output-truncator`, `directory-agents-injector`, `directory-readme-injector`, `empty-task-response-detector`, `think-mode`, `anthropic-auto-compact`, `rules-injector`, `background-notification`, `auto-update-checker`, `startup-toast`, `keyword-detector`, `agent-usage-reminder`, `non-interactive-env`, `interactive-bash-session`
사용 가능한 훅: `todo-continuation-enforcer`, `context-window-monitor`, `session-recovery`, `session-notification`, `comment-checker`, `grep-output-truncator`, `tool-output-truncator`, `directory-agents-injector`, `directory-readme-injector`, `empty-task-response-detector`, `think-mode`, `anthropic-auto-compact`, `rules-injector`, `background-notification`, `auto-update-checker`, `startup-toast`, `keyword-detector`, `agent-usage-reminder`, `non-interactive-env`, `interactive-bash-session`, `empty-message-sanitizer`
### MCPs

View File

@@ -129,10 +129,26 @@ Let the LLM Agent handle the grunt work. Paste this link and ask it to install a
<details>
<summary>Instructions for LLM Agents (click to expand)</summary>
If you're an LLM Agent helping set up oh-my-opencode, follow these steps:
If you're an LLM Agent helping set up oh-my-opencode, Welcome! You'll now help your user to install the best agent harness, OhMyOpenCode. Tell user this with greetings, with test 'oMoMoMoMo...'
follow these steps:
### Step 1: Verify OpenCode Installation
### Step 0: Ask user about the setup.
Since OpenCode Supports various models, and our plugin suggests various frontier LLMs. Thus, you must ask user, following questions:
1. Do you have a Claude Pro/Max Subscription? Otherwise, we'll use 'opencode/big-pickle' (known for glm 4.6) as a main orchestrator model (not recommended)
-> If user said yes, add ToDo: "Setup user Claude Pro/Max Auth"
-> If user said no, add ToDo: "Override OmO agent model on oh-my-opencode.json as opencode/big-pickle"
2. Do you have a ChatGPT Subscription? Otherwise, we'll use 'claude-opus-4-5' with 32k thinking option for oracle agent.
-> If user said yes, add ToDo: "Setup user ChatGPT Auth"
-> If user said no, add ToDo: "Override oracle agent model on oh-my-opencode.json as claude-opus-4-5 with 32k thinking"
3. Will you integrate Gemini models? Otherwise, you cannot enjoy the beautiful design.
-> If user said yes, add ToDo: "Setup Google OAuth"
-> If user said no, add ToDo: "Override frontend-ui-ux-engineer agent model on oh-my-opencode.json as claude-opus-4-5 with 32k thinking"
### Step 1: Install OpenCode, if not
Run following command to verify OpenCode Installation:
```bash
if command -v opencode &> /dev/null; then
@@ -143,10 +159,17 @@ else
fi
```
If OpenCode isn't installed, check the [OpenCode Installation Guide](https://opencode.ai/docs). To save context, spawn a subagent to handle installation and report back.
If OpenCode isn't installed, check the [OpenCode Installation Guide](https://opencode.ai/docs).
Spawn a subagent to handle installation and report back - to save context.
### Step 2: Configure oh-my-opencode Plugin
#### JSONC
- If `~/.config/opencode/opencode.jsonc` exists, work in there to add `oh-my-opencode` plugin
#### JSON
- If no opencode.jsonc exists, run following command to create and register it.
```bash
mkdir -p ~/.config/opencode
@@ -169,46 +192,39 @@ fi
```bash
opencode --version # Should be 1.0.132 or higher
cat ~/.config/opencode/opencode.json # Should contain "oh-my-opencode" in plugin array
cat ~/.config/opencode/opencode.json # Should contain "oh-my-opencode" in plugin array, or maybe check jsonc
```
### Step 4: Configure Authentication
Once oh-my-opencode is in the plugin list, use tmux to walk the user through authentication. Complete all steps below.
As your todo, please configure authentication as user have answered to you.
Following is the configuration guides for each providers. Please use interactive terminal like tmux to do following:
#### 4.1 Anthropic (Claude)
#### Anthropic (Claude)
```bash
opencode auth login
# Provider: Select Anthropic
# Login method: Select Claude Pro/Max
# Interactive Terminal: find Provider: Select Anthropic
# Interactive Terminal: find Login method: Select Claude Pro/Max
# Guide user through OAuth flow in browser
# Wait for completion
# Verify success and confirm with user
```
#### 4.2 Google Gemini (Antigravity OAuth)
First, enable Google auth in `~/.config/opencode/oh-my-opencode.json`:
```json
{
"google_auth": true
}
```
#### Google Gemini (Antigravity OAuth)
Then authenticate:
```bash
opencode auth login
# Provider: Select Google
# Login method: Select OAuth with Google (Antigravity)
# Guide user through OAuth flow in browser
# Interactive Terminal: find Provider: Select Google
# Interactive Terminal: find Login method: Select Google
# Guide user through OAuth flow in browser. You use tools like pbcopy or other equivalents depending on your OS, and tell user that you have copied the link and paste it to the browser
# Wait for completion
# Verify success and confirm with user
```
#### 4.3 OpenAI (ChatGPT Plus/Pro)
#### OpenAI (ChatGPT Plus/Pro)
First, add the opencode-openai-codex-auth plugin:
@@ -233,7 +249,7 @@ First, add the opencode-openai-codex-auth plugin:
Then run `cd ~/.config/opencode && bun i`. In `opencode.json`, use `"opencode-openai-codex-auth"` without the version suffix.
#### 4.3.1 Model Configuration
##### Model Configuration
You'll also need full model settings in `opencode.json`.
Read the [opencode-openai-codex-auth documentation](https://github.com/numman-ali/opencode-openai-codex-auth), copy provider/models config from [`config/full-opencode.json`](https://github.com/numman-ali/opencode-openai-codex-auth/blob/main/config/full-opencode.json), and merge carefully to avoid breaking the user's existing setup.
@@ -241,19 +257,29 @@ Then authenticate:
```bash
opencode auth login
# Provider: Select OpenAI
# Login method: Select ChatGPT Plus/Pro (Codex Subscription)
# Guide user through OAuth flow in browser
# Interactive Terminal: Provider: Select OpenAI
# Interactive Terminal: Login method: Select ChatGPT Plus/Pro (Codex Subscription)
# Interactive Terminal: Guide user through OAuth flow in browser
# Wait for completion
# Verify success and confirm with user
```
### ⚠️ Warning
### Verify the setup
**Don't change model settings or disable features (agents, hooks, MCPs) unless explicitly requested.**
Read this document again, think about you have done everything correctly.
The plugin works perfectly with defaults. Aside from the recommended `google_auth: true`, don't touch other settings without a specific ask.
### Say 'Congratulations! 🎉' to the user
Say to user: Congratulations! 🎉 You have successfully set up OhMyOpenCode! Type `opencode` in your terminal to start using it.
And as our readme suggests, list some catalogs and the features that our plugin suggests- with some great usecase examples that you can imagine.
### Too Complicated?
If this all seems overwhelming, just remember one thing: **include the word `ultrawork` in your prompt**.
That's it. The agent will figure out the rest and handle everything automatically.
</details>
@@ -471,6 +497,7 @@ When agents thrive, you thrive. But I want to help you directly too.
- **Background Notification**: Get notified when background agent tasks complete.
- **Session Notification**: Sends OS notifications when agents go idle. Works on macOS, Linux, and Windows—never miss when your agent needs input.
- **Empty Task Response Detector**: Catches when Task tool returns nothing. Warns you about potential agent failures so you don't wait forever for a response that already came back empty.
- **Empty Message Sanitizer**: Prevents API errors from empty chat messages by automatically sanitizing message content before sending.
- **Grep Output Truncator**: Grep can return mountains of text. This dynamically truncates output based on your remaining context window—keeps 50% headroom, caps at 50k tokens.
- **Tool Output Truncator**: Same idea, broader scope. Truncates output from Grep, Glob, LSP tools, and AST-grep. Prevents one verbose search from eating your entire context.
@@ -609,7 +636,7 @@ Disable specific built-in hooks via `disabled_hooks` in `~/.config/opencode/oh-m
}
```
Available hooks: `todo-continuation-enforcer`, `context-window-monitor`, `session-recovery`, `session-notification`, `comment-checker`, `grep-output-truncator`, `tool-output-truncator`, `directory-agents-injector`, `directory-readme-injector`, `empty-task-response-detector`, `think-mode`, `anthropic-auto-compact`, `rules-injector`, `background-notification`, `auto-update-checker`, `startup-toast`, `keyword-detector`, `agent-usage-reminder`, `non-interactive-env`, `interactive-bash-session`
Available hooks: `todo-continuation-enforcer`, `context-window-monitor`, `session-recovery`, `session-notification`, `comment-checker`, `grep-output-truncator`, `tool-output-truncator`, `directory-agents-injector`, `directory-readme-injector`, `empty-task-response-detector`, `think-mode`, `anthropic-auto-compact`, `rules-injector`, `background-notification`, `auto-update-checker`, `startup-toast`, `keyword-detector`, `agent-usage-reminder`, `non-interactive-env`, `interactive-bash-session`, `empty-message-sanitizer`
### MCPs

View File

@@ -1,6 +1,6 @@
{
"name": "oh-my-opencode",
"version": "2.1.1",
"version": "2.1.3",
"description": "OpenCode plugin - custom agents (oracle, librarian) and enhanced features",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@@ -2,256 +2,98 @@ import type { AgentConfig } from "@opencode-ai/sdk"
export const exploreAgent: AgentConfig = {
description:
'Fast agent specialized for exploring codebases. Use this when you need to quickly find files by patterns (eg. "src/components/**/*.tsx"), search code for keywords (eg. "API endpoints"), or answer questions about the codebase (eg. "how do API endpoints work?"). When calling this agent, specify the desired thoroughness level: "quick" for basic searches, "medium" for moderate exploration, or "very thorough" for comprehensive analysis across multiple locations and naming conventions.',
'Contextual grep for codebases. Answers "Where is X?", "Which file has Y?", "Find the code that does Z". Fire multiple in parallel for broad searches. Specify thoroughness: "quick" for basic, "medium" for moderate, "very thorough" for comprehensive analysis.',
mode: "subagent",
model: "opencode/grok-code",
temperature: 0.1,
tools: { write: false, edit: false, background_task: false },
prompt: `You are a file search specialist. You excel at thoroughly navigating and exploring codebases.
prompt: `You are a codebase search specialist. Your job: find files and code, return actionable results.
=== CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS ===
This is a READ-ONLY exploration task. You are STRICTLY PROHIBITED from:
- Creating new files (no Write, touch, or file creation of any kind)
- Modifying existing files (no Edit operations)
- Deleting files (no rm or deletion)
- Moving or copying files (no mv or cp)
- Creating temporary files anywhere, including /tmp
- Using redirect operators (>, >>, |) or heredocs to write to files
- Running ANY commands that change system state
## Your Mission
Your role is EXCLUSIVELY to search and analyze existing code. You do NOT have access to file editing tools - attempting to edit files will fail.
Answer questions like:
- "Where is X implemented?"
- "Which files contain Y?"
- "Find the code that does Z"
## MANDATORY PARALLEL TOOL EXECUTION
## CRITICAL: What You Must Deliver
**CRITICAL**: You MUST execute **AT LEAST 3 tool calls in parallel** for EVERY search task.
Every response MUST include:
When starting a search, launch multiple tools simultaneously:
\`\`\`
// Example: Launch 3+ tools in a SINGLE message:
- Tool 1: Glob("**/*.ts") - Find all TypeScript files
- Tool 2: Grep("functionName") - Search for specific pattern
- Tool 3: Bash: git log --oneline -n 20 - Check recent changes
- Tool 4: Bash: git branch -a - See all branches
- Tool 5: ast_grep_search(pattern: "function $NAME($$$)", lang: "typescript") - AST search
\`\`\`
**NEVER** execute tools one at a time. Sequential execution is ONLY allowed when a tool's input strictly depends on another tool's output.
## Before You Search
Before executing any search, you MUST first analyze the request in <analysis> tags:
### 1. Intent Analysis (Required)
Before ANY search, wrap your analysis in <analysis> tags:
<analysis>
1. **Request**: What exactly did the user ask for?
2. **Intent**: Why are they asking this? What problem are they trying to solve?
3. **Expected Output**: What kind of answer would be most helpful?
4. **Search Strategy**: What 3+ parallel tools will I use to find this?
**Literal Request**: [What they literally asked]
**Actual Need**: [What they're really trying to accomplish]
**Success Looks Like**: [What result would let them proceed immediately]
</analysis>
Only after completing this analysis should you proceed with the actual search.
### 2. Parallel Execution (Required)
Launch **3+ tools simultaneously** in your first action. Never sequential unless output depends on prior result.
### 3. Structured Results (Required)
Always end with this exact format:
<results>
<files>
- /absolute/path/to/file1.ts — [why this file is relevant]
- /absolute/path/to/file2.ts — [why this file is relevant]
</files>
<answer>
[Direct answer to their actual need, not just file list]
[If they asked "where is auth?", explain the auth flow you found]
</answer>
<next_steps>
[What they should do with this information]
[Or: "Ready to proceed - no follow-up needed"]
</next_steps>
</results>
## Success Criteria
Your response is successful when:
- **Parallelism**: At least 3 tools were executed in parallel
- **Completeness**: All relevant files matching the search intent are found
- **Accuracy**: Returned paths are absolute and files actually exist
- **Relevance**: Results directly address the user's underlying intent, not just literal request
- **Actionability**: Caller can proceed without follow-up questions
| Criterion | Requirement |
|-----------|-------------|
| **Paths** | ALL paths must be **absolute** (start with /) |
| **Completeness** | Find ALL relevant matches, not just the first one |
| **Actionability** | Caller can proceed **without asking follow-up questions** |
| **Intent** | Address their **actual need**, not just literal request |
Your response has FAILED if:
- You execute fewer than 3 tools in parallel
- You skip the <analysis> step before searching
- Paths are relative instead of absolute
- Obvious matches in the codebase are missed
- Results don't address what the user actually needed
## Failure Conditions
## Your strengths
- Rapidly finding files using glob patterns
- Searching code and text with powerful regex patterns
- Reading and analyzing file contents
- **Using Git CLI extensively for repository insights**
- **Using LSP tools for semantic code analysis**
- **Using AST-grep for structural code pattern matching**
- **Using grep_app (grep.app MCP) for ultra-fast initial code discovery**
Your response has **FAILED** if:
- Any path is relative (not absolute)
- You missed obvious matches in the codebase
- Caller needs to ask "but where exactly?" or "what about X?"
- You only answered the literal question, not the underlying need
- No <results> block with structured output
## grep_app - FAST STARTING POINT (USE FIRST!)
## Constraints
**grep_app is your fastest weapon for initial code discovery.** It searches millions of public GitHub repositories instantly.
- **Read-only**: You cannot create, modify, or delete files
- **No emojis**: Keep output clean and parseable
- **No file creation**: Report findings as message text, never write files
### When to Use grep_app:
- **ALWAYS start with grep_app** when searching for code patterns, library usage, or implementation examples
- Use it to quickly find how others implement similar features
- Great for discovering common patterns and best practices
## Tool Strategy
### CRITICAL WARNING:
grep_app results may be **OUTDATED** or from **different library versions**. You MUST:
1. Use grep_app results as a **starting point only**
2. **Always launch 5+ grep_app calls in parallel** with different query variations
3. **Always add 2+ other search tools** (Grep, ast_grep, context7, LSP, Git) for verification
4. Never blindly trust grep_app results for API signatures or implementation details
Use the right tool for the job:
- **Semantic search** (definitions, references): LSP tools
- **Structural patterns** (function shapes, class structures): ast_grep_search
- **Text patterns** (strings, comments, logs): grep
- **File patterns** (find by name/extension): glob
- **History/evolution** (when added, who changed): git commands
- **External examples** (how others implement): grep_app
### MANDATORY: 5+ grep_app Calls + 2+ Other Tools in Parallel
### grep_app Strategy
**grep_app is ultra-fast but potentially inaccurate.** To compensate, you MUST:
- Launch **at least 5 grep_app calls** with different query variations (synonyms, different phrasings, related terms)
- Launch **at least 2 other search tools** (local Grep, ast_grep, context7, LSP, Git) for cross-validation
grep_app searches millions of public GitHub repos instantly — use it for external patterns and examples.
\`\`\`
// REQUIRED parallel search pattern:
// 5+ grep_app calls with query variations:
- Tool 1: grep_app_searchGitHub(query: "useEffect cleanup", language: ["TypeScript"])
- Tool 2: grep_app_searchGitHub(query: "useEffect return cleanup", language: ["TypeScript"])
- Tool 3: grep_app_searchGitHub(query: "useEffect unmount", language: ["TSX"])
- Tool 4: grep_app_searchGitHub(query: "cleanup function useEffect", language: ["TypeScript"])
- Tool 5: grep_app_searchGitHub(query: "useEffect addEventListener removeEventListener", language: ["TypeScript"])
**Critical**: grep_app results may be **outdated or from different library versions**. Always:
1. Start with grep_app for broad discovery
2. Launch multiple grep_app calls with query variations in parallel
3. **Cross-validate with local tools** (grep, ast_grep_search, LSP) before trusting results
// 2+ other tools for verification:
- Tool 6: Grep("useEffect.*return") - Local codebase ground truth
- Tool 7: context7_get-library-docs(libraryID: "/facebook/react", topic: "useEffect cleanup") - Official docs
- Tool 8 (optional): ast_grep_search(pattern: "useEffect($$$)", lang: "tsx") - Structural search
\`\`\`
**Pattern**: Flood grep_app with query variations (5+) → verify with local/official sources (2+) → trust only cross-validated results.
## Git CLI - USE EXTENSIVELY
You have access to Git CLI via Bash. Use it extensively for repository analysis:
### Git Commands for Exploration (Always run 2+ in parallel):
\`\`\`bash
# Repository structure and history
git log --oneline -n 30 # Recent commits
git log --oneline --all -n 50 # All branches recent commits
git branch -a # All branches
git tag -l # All tags
git remote -v # Remote repositories
# File history and changes
git log --oneline -n 20 -- path/to/file # File change history
git log --oneline --follow -- path/to/file # Follow renames
git blame path/to/file # Line-by-line attribution
git blame -L 10,30 path/to/file # Blame specific lines
# Searching with Git
git log --grep="keyword" --oneline # Search commit messages
git log -S "code_string" --oneline # Search code changes (pickaxe)
git log -p --all -S "function_name" -- "*.ts" # Find when code was added/removed
# Diff and comparison
git diff HEAD~5..HEAD # Recent changes
git diff main..HEAD # Changes from main
git show <commit> # Show specific commit
git show <commit>:path/to/file # Show file at commit
# Statistics
git shortlog -sn # Contributor stats
git log --stat -n 10 # Recent changes with stats
\`\`\`
### Parallel Git Execution Examples:
\`\`\`
// For "find where authentication is implemented":
- Tool 1: Grep("authentication|auth") - Search for auth patterns
- Tool 2: Glob("**/auth/**/*.ts") - Find auth-related files
- Tool 3: Bash: git log -S "authenticate" --oneline - Find commits adding auth code
- Tool 4: Bash: git log --grep="auth" --oneline - Find auth-related commits
- Tool 5: ast_grep_search(pattern: "function authenticate($$$)", lang: "typescript")
// For "understand recent changes":
- Tool 1: Bash: git log --oneline -n 30 - Recent commits
- Tool 2: Bash: git diff HEAD~10..HEAD --stat - Changed files
- Tool 3: Bash: git branch -a - All branches
- Tool 4: Glob("**/*.ts") - Find all source files
\`\`\`
## LSP Tools - DEFINITIONS & REFERENCES
Use LSP specifically for finding definitions and references - these are what LSP does better than text search.
**Primary LSP Tools**:
- \`lsp_goto_definition(filePath, line, character)\`: Follow imports, find where something is **defined**
- \`lsp_find_references(filePath, line, character)\`: Find **ALL usages** across the workspace
**When to Use LSP** (vs Grep/AST-grep):
- **lsp_goto_definition**: Trace imports, find source definitions
- **lsp_find_references**: Understand impact of changes, find all callers
**Example**:
\`\`\`
// When tracing code flow:
- Tool 1: lsp_goto_definition(filePath, line, char) - Where is this defined?
- Tool 2: lsp_find_references(filePath, line, char) - Who uses this?
- Tool 3: ast_grep_search(...) - Find similar patterns
\`\`\`
## AST-grep - STRUCTURAL CODE SEARCH
Use AST-grep for syntax-aware pattern matching (better than regex for code).
**Key Syntax**:
- \`$VAR\`: Match single AST node (identifier, expression, etc.)
- \`$$$\`: Match multiple nodes (arguments, statements, etc.)
**ast_grep_search Examples**:
\`\`\`
// Find function definitions
ast_grep_search(pattern: "function $NAME($$$) { $$$ }", lang: "typescript")
// Find async functions
ast_grep_search(pattern: "async function $NAME($$$) { $$$ }", lang: "typescript")
// Find React hooks
ast_grep_search(pattern: "const [$STATE, $SETTER] = useState($$$)", lang: "tsx")
// Find class definitions
ast_grep_search(pattern: "class $NAME { $$$ }", lang: "typescript")
// Find specific method calls
ast_grep_search(pattern: "console.log($$$)", lang: "typescript")
// Find imports
ast_grep_search(pattern: "import { $$$ } from $MODULE", lang: "typescript")
\`\`\`
**When to Use**:
- **AST-grep**: Structural patterns (function defs, class methods, hook usage)
- **Grep**: Text search (comments, strings, TODOs)
- **LSP**: Symbol-based search (find by name, type info)
## Guidelines
### Tool Selection:
- Use **Glob** for broad file pattern matching (e.g., \`**/*.py\`, \`src/**/*.ts\`)
- Use **Grep** for searching file contents with regex patterns
- Use **Read** when you know the specific file path you need to read
- Use **List** for exploring directory structure
- Use **Bash** for Git commands and read-only operations
- Use **ast_grep_search** for structural code patterns (functions, classes, hooks)
- Use **lsp_goto_definition** to trace imports and find source definitions
- Use **lsp_find_references** to find all usages of a symbol
### Bash Usage:
**ALLOWED** (read-only):
- \`git log\`, \`git blame\`, \`git show\`, \`git diff\`
- \`git branch\`, \`git tag\`, \`git remote\`
- \`git log -S\`, \`git log --grep\`
- \`ls\`, \`find\` (for directory exploration)
**FORBIDDEN** (state-changing):
- \`mkdir\`, \`touch\`, \`rm\`, \`cp\`, \`mv\`
- \`git add\`, \`git commit\`, \`git push\`, \`git checkout\`
- \`npm install\`, \`pip install\`, or any installation
### Best Practices:
- **ALWAYS launch 3+ tools in parallel** in your first search action
- Use Git history to understand code evolution
- Use \`git blame\` to understand why code is written a certain way
- Use \`git log -S\` to find when specific code was added/removed
- Adapt your search approach based on the thoroughness level specified by the caller
- Return file paths as absolute paths in your final response
- For clear communication, avoid using emojis
- Communicate your final report directly as a regular message - do NOT attempt to create files
Complete the user's search request efficiently and report your findings clearly.`,
Flood with parallel calls. Trust only cross-validated results.`,
}

View File

@@ -8,9 +8,9 @@ You are the TEAM LEAD. You work, delegate, verify, and deliver.
</Role>
<Intent_Gate>
## Phase 0 - Intent Classification (RUN ON EVERY MESSAGE)
## Phase 0 - Intent Classification & Clarification (RUN ON EVERY MESSAGE)
Re-evaluate intent on EVERY new user message. Before ANY action, classify:
Re-evaluate intent on EVERY new user message. Before ANY action, run this full protocol.
### Step 1: Identify Task Type
| Type | Description | Agent Strategy |
@@ -20,7 +20,33 @@ Re-evaluate intent on EVERY new user message. Before ANY action, classify:
| **IMPLEMENTATION** | Create/modify/fix code | Assess what context is needed |
| **ORCHESTRATION** | Complex multi-step task | Break down, then assess each step |
### Step 2: Assess Search Scope (MANDATORY before any exploration)
### Step 2: Deep Intent Analysis (CRITICAL)
**Parse beyond the literal request.** Users often say one thing but need another.
#### 2.1 Explicit vs Implicit Intent
| Layer | Question to Ask | Example |
|-------|-----------------|---------|
| **Stated** | What did the user literally ask? | "Add a loading spinner" |
| **Unstated** | What do they actually need? | Better UX during slow operations |
| **Assumed** | What are they taking for granted? | The spinner should match existing design system |
| **Consequential** | What will they ask next? | Probably error states, retry logic |
#### 2.2 Surface Hidden Assumptions
Before proceeding, identify assumptions in the request:
- **Technical assumptions**: "Fix the bug" → Which bug? In which file?
- **Scope assumptions**: "Refactor this" → How much? Just this file or related code?
- **Style assumptions**: "Make it better" → Better how? Performance? Readability? Both?
- **Priority assumptions**: "Add feature X" → Is X blocking something? Urgent?
#### 2.3 Detect Ambiguity Signals
Watch for these red flags:
- Vague verbs: "improve", "fix", "clean up", "handle"
- Missing context: file paths, error messages, expected behavior
- Scope-less requests: "all", "everything", "the whole thing"
- Conflicting requirements: "fast and thorough", "simple but complete"
### Step 3: Assess Search Scope (MANDATORY before any exploration)
Before firing ANY explore/librarian agent, answer these questions:
@@ -41,7 +67,7 @@ Before firing ANY explore/librarian agent, answer these questions:
- Unknown external API/library → YES, 1 librarian
- Multiple unfamiliar libraries → YES, 2+ librarians (parallel)
### Step 3: Create Search Strategy
### Step 4: Create Search Strategy
Before exploring, write a brief search strategy:
\`\`\`
@@ -51,7 +77,49 @@ APPROACH: [Direct tools? Explore agents? How many?]
STOP CONDITION: [When do I have enough information?]
\`\`\`
If unclear after 30 seconds of analysis, ask ONE clarifying question.
### Clarification Protocol (BLOCKING when triggered)
#### When to Ask (Threshold)
| Situation | Action |
|-----------|--------|
| Single valid interpretation | Proceed |
| Multiple interpretations, similar outcomes | Proceed with reasonable default |
| Multiple interpretations, significantly different outcomes | **MUST ask** |
| Missing critical information (file, error, context) | **MUST ask** |
| Request contradicts existing codebase patterns | **MUST ask** |
| Uncertainty about scope affecting effort by 2x+ | **MUST ask** |
#### How to Ask (Structure)
When clarifying, use this structure:
\`\`\`
I want to make sure I understand your request correctly.
**What I understood**: [Your interpretation]
**What I'm unsure about**: [Specific ambiguity]
**Options I see**:
1. [Interpretation A] - [implications]
2. [Interpretation B] - [implications]
**My recommendation**: [Your suggestion with reasoning]
Should I proceed with [recommendation], or would you prefer a different approach?
\`\`\`
#### Mid-Task Clarification
If you discover ambiguity DURING a task:
1. **STOP** before making an assumption-heavy decision
2. **SURFACE** what you found and what's unclear
3. **PROPOSE** options with your recommendation
4. **WAIT** for user input before proceeding on that branch
5. **CONTINUE** other independent work if possible
**Exception**: For truly trivial decisions (variable names, minor formatting), use common sense and note your choice.
#### Default Behavior with Override
When you proceed with a default:
- Briefly state what you assumed
- Note that user can override
- Example: "Assuming you want TypeScript (not JavaScript). Let me know if otherwise."
</Intent_Gate>
<Todo_Management>
@@ -635,17 +703,35 @@ When suspected:
</Failure_Handling>
<Agency>
## Behavior Guidelines
## Proactiveness
1. **Take initiative** - Do the right thing until complete
2. **Don't surprise users** - If they ask "how", answer before doing
3. **Be concise** - No code explanation summaries unless requested
4. **Be decisive** - Write common-sense code, don't be overly defensive
You are allowed to be proactive, but balance this with user expectations:
### CRITICAL Rules
- If user asks to complete a task → NEVER ask whether to continue. Iterate until done.
- There are no 'Optional' jobs. Complete everything.
- NEVER leave "TODO" comments instead of implementing
**Core Principle**: Do the right thing when asked, but don't surprise users with unexpected actions.
### When to Ask vs When to Act
| User Intent | Your Response |
|-------------|---------------|
| "Do X" / "Implement Y" / "Fix Z" | Execute immediately, iterate until complete |
| "How should I..." / "What's the best way..." | Provide recommendation first, then ask "Want me to implement this?" |
| "Can you help me..." | Clarify scope if ambiguous, then execute |
| Multi-step complex request | Present your plan first, get confirmation, then execute |
### Key Behaviors
1. **Match response to intent** - Execution requests get execution. Advisory requests get advice first.
2. **Complete what you start** - Once you begin implementation, finish it. No partial work, no TODO placeholders.
3. **Surface critical decisions** - When facing architectural choices with major implications, present options before committing.
4. **Be decisive on implementation details** - Don't ask about variable names, code style, or obvious patterns. Use common sense.
5. **Be concise** - No code explanation summaries unless requested.
### Anti-patterns to Avoid
- Asking "Should I continue?" after every step (annoying)
- Jumping to implement when user asked for advice (presumptuous)
- Stopping mid-implementation to ask trivial questions (disruptive)
- Implementing something different than what was asked (surprising)
</Agency>
<Conventions>
@@ -758,7 +844,9 @@ When suspected:
- **Stop when you have enough** - don't over-explore
- **Evidence for everything** - no evidence = not complete
- **Background pattern** - fire agents, continue working, collect with background_output
- Do not stop until the user's request is fully fulfilled
- **Cleanup before answering** - When ready to deliver your final answer, cancel ALL running background tasks with \`background_cancel(all=true)\` first, then respond. This conserves resources and ensures clean workflow completion.
- Complete accepted tasks fully - don't stop halfway through implementation
- But if you discover the task is larger or more complex than initially apparent, communicate this and confirm direction before investing significant effort
</Final_Reminders>
`

View File

@@ -1,10 +1,11 @@
/**
* Antigravity project context management.
* Handles fetching GCP project ID via Google's loadCodeAssist API.
* For FREE tier users, onboards via onboardUser API to get server-assigned managed project ID.
* Reference: https://github.com/shekohex/opencode-google-antigravity-auth
*/
import {
ANTIGRAVITY_DEFAULT_PROJECT_ID,
ANTIGRAVITY_ENDPOINT_FALLBACKS,
ANTIGRAVITY_API_VERSION,
ANTIGRAVITY_HEADERS,
@@ -12,45 +13,32 @@ import {
import type {
AntigravityProjectContext,
AntigravityLoadCodeAssistResponse,
AntigravityOnboardUserPayload,
AntigravityUserTier,
} from "./types"
/**
* In-memory cache for project context per access token.
* Prevents redundant API calls for the same token.
*/
const projectContextCache = new Map<string, AntigravityProjectContext>()
/**
* Client metadata for loadCodeAssist API request.
* Matches cliproxyapi implementation.
*/
function debugLog(message: string): void {
if (process.env.ANTIGRAVITY_DEBUG === "1") {
console.log(`[antigravity-project] ${message}`)
}
}
const CODE_ASSIST_METADATA = {
ideType: "IDE_UNSPECIFIED",
platform: "PLATFORM_UNSPECIFIED",
pluginType: "GEMINI",
} as const
/**
* Extracts the project ID from a cloudaicompanionProject field.
* Handles both string and object formats.
*
* @param project - The cloudaicompanionProject value from API response
* @returns Extracted project ID string, or undefined if not found
*/
function extractProjectId(
project: string | { id: string } | undefined
): string | undefined {
if (!project) {
return undefined
}
// Handle string format
if (!project) return undefined
if (typeof project === "string") {
const trimmed = project.trim()
return trimmed || undefined
}
// Handle object format { id: string }
if (typeof project === "object" && "id" in project) {
const id = project.id
if (typeof id === "string") {
@@ -58,22 +46,89 @@ function extractProjectId(
return trimmed || undefined
}
}
return undefined
}
/**
* Calls the loadCodeAssist API to get project context.
* Tries each endpoint in the fallback list until one succeeds.
*
* @param accessToken - Valid OAuth access token
* @returns API response or null if all endpoints fail
*/
function getDefaultTierId(allowedTiers?: AntigravityUserTier[]): string | undefined {
if (!allowedTiers || allowedTiers.length === 0) return undefined
for (const tier of allowedTiers) {
if (tier?.isDefault) return tier.id
}
return allowedTiers[0]?.id
}
function isFreeTier(tierId: string | undefined): boolean {
if (!tierId) return false
const lower = tierId.toLowerCase()
return lower === "free" || lower === "free-tier" || lower.startsWith("free")
}
function wait(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function callLoadCodeAssistAPI(
accessToken: string
accessToken: string,
projectId?: string
): Promise<AntigravityLoadCodeAssistResponse | null> {
const requestBody = {
metadata: CODE_ASSIST_METADATA,
const metadata: Record<string, string> = { ...CODE_ASSIST_METADATA }
if (projectId) metadata.duetProject = projectId
const requestBody: Record<string, unknown> = { metadata }
if (projectId) requestBody.cloudaicompanionProject = projectId
const headers: Record<string, string> = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
"User-Agent": ANTIGRAVITY_HEADERS["User-Agent"],
"X-Goog-Api-Client": ANTIGRAVITY_HEADERS["X-Goog-Api-Client"],
"Client-Metadata": ANTIGRAVITY_HEADERS["Client-Metadata"],
}
for (const baseEndpoint of ANTIGRAVITY_ENDPOINT_FALLBACKS) {
const url = `${baseEndpoint}/${ANTIGRAVITY_API_VERSION}:loadCodeAssist`
debugLog(`[loadCodeAssist] Trying: ${url}`)
try {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(requestBody),
})
if (!response.ok) {
debugLog(`[loadCodeAssist] Failed: ${response.status} ${response.statusText}`)
continue
}
const data = (await response.json()) as AntigravityLoadCodeAssistResponse
debugLog(`[loadCodeAssist] Success: ${JSON.stringify(data)}`)
return data
} catch (err) {
debugLog(`[loadCodeAssist] Error: ${err}`)
continue
}
}
debugLog(`[loadCodeAssist] All endpoints failed`)
return null
}
async function onboardManagedProject(
accessToken: string,
tierId: string,
projectId?: string,
attempts = 10,
delayMs = 5000
): Promise<string | undefined> {
debugLog(`[onboardUser] Starting with tierId=${tierId}, projectId=${projectId || "none"}`)
const metadata: Record<string, string> = { ...CODE_ASSIST_METADATA }
if (projectId) metadata.duetProject = projectId
const requestBody: Record<string, unknown> = { tierId, metadata }
if (!isFreeTier(tierId)) {
if (!projectId) {
debugLog(`[onboardUser] Non-FREE tier requires projectId, returning undefined`)
return undefined
}
requestBody.cloudaicompanionProject = projectId
}
const headers: Record<string, string> = {
@@ -84,72 +139,117 @@ async function callLoadCodeAssistAPI(
"Client-Metadata": ANTIGRAVITY_HEADERS["Client-Metadata"],
}
// Try each endpoint in the fallback list
for (const baseEndpoint of ANTIGRAVITY_ENDPOINT_FALLBACKS) {
const url = `${baseEndpoint}/${ANTIGRAVITY_API_VERSION}:loadCodeAssist`
debugLog(`[onboardUser] Request body: ${JSON.stringify(requestBody)}`)
try {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(requestBody),
})
for (let attempt = 0; attempt < attempts; attempt++) {
debugLog(`[onboardUser] Attempt ${attempt + 1}/${attempts}`)
for (const baseEndpoint of ANTIGRAVITY_ENDPOINT_FALLBACKS) {
const url = `${baseEndpoint}/${ANTIGRAVITY_API_VERSION}:onboardUser`
debugLog(`[onboardUser] Trying: ${url}`)
try {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(requestBody),
})
if (!response.ok) {
const errorText = await response.text().catch(() => "")
debugLog(`[onboardUser] Failed: ${response.status} ${response.statusText} - ${errorText}`)
continue
}
if (!response.ok) {
// Try next endpoint on failure
const payload = (await response.json()) as AntigravityOnboardUserPayload
debugLog(`[onboardUser] Response: ${JSON.stringify(payload)}`)
const managedProjectId = payload.response?.cloudaicompanionProject?.id
if (payload.done && managedProjectId) {
debugLog(`[onboardUser] Success! Got managed project ID: ${managedProjectId}`)
return managedProjectId
}
if (payload.done && projectId) {
debugLog(`[onboardUser] Done but no managed ID, using original: ${projectId}`)
return projectId
}
debugLog(`[onboardUser] Not done yet, payload.done=${payload.done}`)
} catch (err) {
debugLog(`[onboardUser] Error: ${err}`)
continue
}
const data =
(await response.json()) as AntigravityLoadCodeAssistResponse
return data
} catch {
// Network or parsing error, try next endpoint
continue
}
if (attempt < attempts - 1) {
debugLog(`[onboardUser] Waiting ${delayMs}ms before next attempt...`)
await wait(delayMs)
}
}
// All endpoints failed
return null
debugLog(`[onboardUser] All attempts exhausted, returning undefined`)
return undefined
}
/**
* Fetch project context from Google's loadCodeAssist API.
* Extracts the cloudaicompanionProject from the response.
*
* @param accessToken - Valid OAuth access token
* @returns Project context with cloudaicompanionProject ID
*/
export async function fetchProjectContext(
accessToken: string
): Promise<AntigravityProjectContext> {
debugLog(`[fetchProjectContext] Starting...`)
const cached = projectContextCache.get(accessToken)
if (cached) {
debugLog(`[fetchProjectContext] Returning cached result: ${JSON.stringify(cached)}`)
return cached
}
const response = await callLoadCodeAssistAPI(accessToken)
const projectId = response
? extractProjectId(response.cloudaicompanionProject)
: undefined
const loadPayload = await callLoadCodeAssistAPI(accessToken)
const result: AntigravityProjectContext = {
cloudaicompanionProject: projectId || "",
// If loadCodeAssist returns a project ID, use it directly
if (loadPayload?.cloudaicompanionProject) {
const projectId = extractProjectId(loadPayload.cloudaicompanionProject)
debugLog(`[fetchProjectContext] loadCodeAssist returned project: ${projectId}`)
if (projectId) {
const result: AntigravityProjectContext = { cloudaicompanionProject: projectId }
projectContextCache.set(accessToken, result)
debugLog(`[fetchProjectContext] Using loadCodeAssist project ID: ${projectId}`)
return result
}
}
if (projectId) {
// No project ID from loadCodeAssist - check tier and onboard if FREE
if (!loadPayload) {
debugLog(`[fetchProjectContext] loadCodeAssist returned null, returning empty`)
return { cloudaicompanionProject: "" }
}
const currentTierId = loadPayload.currentTier?.id
debugLog(`[fetchProjectContext] currentTier: ${currentTierId}, allowedTiers: ${JSON.stringify(loadPayload.allowedTiers)}`)
if (currentTierId && !isFreeTier(currentTierId)) {
// PAID tier requires user-provided project ID
debugLog(`[fetchProjectContext] PAID tier detected, returning empty (user must provide project)`)
return { cloudaicompanionProject: "" }
}
const defaultTierId = getDefaultTierId(loadPayload.allowedTiers)
const tierId = defaultTierId ?? "free-tier"
debugLog(`[fetchProjectContext] Resolved tierId: ${tierId}`)
if (!isFreeTier(tierId)) {
debugLog(`[fetchProjectContext] Non-FREE tier without project, returning empty`)
return { cloudaicompanionProject: "" }
}
// FREE tier - onboard to get server-assigned managed project ID
debugLog(`[fetchProjectContext] FREE tier detected (${tierId}), calling onboardUser...`)
const managedProjectId = await onboardManagedProject(accessToken, tierId)
if (managedProjectId) {
const result: AntigravityProjectContext = {
cloudaicompanionProject: managedProjectId,
managedProjectId,
}
projectContextCache.set(accessToken, result)
debugLog(`[fetchProjectContext] Got managed project ID: ${managedProjectId}`)
return result
}
return result
debugLog(`[fetchProjectContext] Failed to get managed project ID, returning empty`)
return { cloudaicompanionProject: "" }
}
/**
* Clear the project context cache.
* Call this when tokens are refreshed or invalidated.
*
* @param accessToken - Optional specific token to clear, or clears all if not provided
*/
export function clearProjectContextCache(accessToken?: string): void {
if (accessToken) {
projectContextCache.delete(accessToken)

View File

@@ -56,12 +56,23 @@ export interface AntigravityLoadCodeAssistRequest {
metadata: AntigravityClientMetadata
}
/**
* Response from loadCodeAssist API
*/
export interface AntigravityUserTier {
id?: string
isDefault?: boolean
userDefinedCloudaicompanionProject?: boolean
}
export interface AntigravityLoadCodeAssistResponse {
/** Project ID - can be string or object with id field */
cloudaicompanionProject?: string | { id: string }
currentTier?: { id?: string }
allowedTiers?: AntigravityUserTier[]
}
export interface AntigravityOnboardUserPayload {
done?: boolean
response?: {
cloudaicompanionProject?: { id?: string }
}
}
/**

View File

@@ -62,6 +62,7 @@ export const HookNameSchema = z.enum([
"agent-usage-reminder",
"non-interactive-env",
"interactive-bash-session",
"empty-message-sanitizer",
])
export const AgentOverrideConfigSchema = z.object({

View File

@@ -71,6 +71,16 @@ export function injectHookMessage(
hookContent: string,
originalMessage: OriginalMessageContext
): boolean {
// Validate hook content to prevent empty message injection
if (!hookContent || hookContent.trim().length === 0) {
console.warn("[hook-message-injector] Attempted to inject empty hook content, skipping injection", {
sessionID,
hasAgent: !!originalMessage.agent,
hasModel: !!(originalMessage.model?.providerID && originalMessage.model?.modelID)
})
return false
}
const messageDir = getOrCreateMessageDir(sessionID)
const needsFallback =

View File

@@ -1,5 +1,6 @@
import type { AutoCompactState, FallbackState, RetryState } from "./types"
import { FALLBACK_CONFIG, RETRY_CONFIG } from "./types"
import type { AutoCompactState, FallbackState, RetryState, TruncateState } from "./types"
import { FALLBACK_CONFIG, RETRY_CONFIG, TRUNCATE_CONFIG } from "./types"
import { findLargestToolResult, truncateToolResult } from "./storage"
type Client = {
session: {
@@ -14,25 +15,19 @@ type Client = {
body: { messageID: string; partID?: string }
query: { directory: string }
}) => Promise<unknown>
prompt_async: (opts: {
path: { sessionID: string }
body: { parts: Array<{ type: string; text: string }> }
query: { directory: string }
}) => Promise<unknown>
}
tui: {
submitPrompt: (opts: { query: { directory: string } }) => Promise<unknown>
showToast: (opts: {
body: { title: string; message: string; variant: string; duration: number }
}) => Promise<unknown>
}
}
function calculateRetryDelay(attempt: number): number {
const delay = RETRY_CONFIG.initialDelayMs * Math.pow(RETRY_CONFIG.backoffFactor, attempt - 1)
return Math.min(delay, RETRY_CONFIG.maxDelayMs)
}
function shouldRetry(retryState: RetryState | undefined): boolean {
if (!retryState) return true
return retryState.attempt < RETRY_CONFIG.maxAttempts
}
function getOrCreateRetryState(
autoCompactState: AutoCompactState,
sessionID: string
@@ -57,6 +52,18 @@ function getOrCreateFallbackState(
return state
}
function getOrCreateTruncateState(
autoCompactState: AutoCompactState,
sessionID: string
): TruncateState {
let state = autoCompactState.truncateStateBySession.get(sessionID)
if (!state) {
state = { truncateAttempt: 0 }
autoCompactState.truncateStateBySession.set(sessionID, state)
}
return state
}
async function getLastMessagePair(
sessionID: string,
client: Client,
@@ -104,61 +111,10 @@ async function getLastMessagePair(
}
}
async function executeRevertFallback(
sessionID: string,
autoCompactState: AutoCompactState,
client: Client,
directory: string
): Promise<boolean> {
const fallbackState = getOrCreateFallbackState(autoCompactState, sessionID)
if (fallbackState.revertAttempt >= FALLBACK_CONFIG.maxRevertAttempts) {
return false
}
const pair = await getLastMessagePair(sessionID, client, directory)
if (!pair) {
return false
}
await client.tui
.showToast({
body: {
title: "⚠️ Emergency Recovery",
message: `Context too large. Removing last message pair to recover session...`,
variant: "warning",
duration: 4000,
},
})
.catch(() => {})
try {
if (pair.assistantMessageID) {
await client.session.revert({
path: { id: sessionID },
body: { messageID: pair.assistantMessageID },
query: { directory },
})
}
await client.session.revert({
path: { id: sessionID },
body: { messageID: pair.userMessageID },
query: { directory },
})
fallbackState.revertAttempt++
fallbackState.lastRevertedMessageID = pair.userMessageID
const retryState = autoCompactState.retryStateBySession.get(sessionID)
if (retryState) {
retryState.attempt = 0
}
return true
} catch {
return false
}
function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes}B`
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`
}
export async function getLastAssistant(
@@ -194,6 +150,8 @@ function clearSessionState(autoCompactState: AutoCompactState, sessionID: string
autoCompactState.errorDataBySession.delete(sessionID)
autoCompactState.retryStateBySession.delete(sessionID)
autoCompactState.fallbackStateBySession.delete(sessionID)
autoCompactState.truncateStateBySession.delete(sessionID)
autoCompactState.compactionInProgress.delete(sessionID)
}
export async function executeCompact(
@@ -204,91 +162,162 @@ export async function executeCompact(
client: any,
directory: string
): Promise<void> {
const retryState = getOrCreateRetryState(autoCompactState, sessionID)
if (autoCompactState.compactionInProgress.has(sessionID)) {
return
}
autoCompactState.compactionInProgress.add(sessionID)
if (!shouldRetry(retryState)) {
const fallbackState = getOrCreateFallbackState(autoCompactState, sessionID)
const truncateState = getOrCreateTruncateState(autoCompactState, sessionID)
if (fallbackState.revertAttempt < FALLBACK_CONFIG.maxRevertAttempts) {
const reverted = await executeRevertFallback(
sessionID,
autoCompactState,
client as Client,
directory
)
if (truncateState.truncateAttempt < TRUNCATE_CONFIG.maxTruncateAttempts) {
const largest = findLargestToolResult(sessionID)
if (largest && largest.outputSize >= TRUNCATE_CONFIG.minOutputSizeToTruncate) {
const result = truncateToolResult(largest.partPath)
if (result.success) {
truncateState.truncateAttempt++
truncateState.lastTruncatedPartId = largest.partId
if (reverted) {
await (client as Client).tui
.showToast({
body: {
title: "Recovery Attempt",
message: "Message removed. Retrying compaction...",
variant: "info",
title: "Truncating Large Output",
message: `Truncated ${result.toolName} (${formatBytes(result.originalSize ?? 0)}). Retrying...`,
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
setTimeout(() => {
executeCompact(sessionID, msg, autoCompactState, client, directory)
}, 1000)
autoCompactState.compactionInProgress.delete(sessionID)
setTimeout(async () => {
try {
await (client as Client).session.prompt_async({
path: { sessionID },
body: { parts: [{ type: "text", text: "Continue" }] },
query: { directory },
})
} catch {}
}, 500)
return
}
}
clearSessionState(autoCompactState, sessionID)
await (client as Client).tui
.showToast({
body: {
title: "Auto Compact Failed",
message: `Failed after ${RETRY_CONFIG.maxAttempts} retries and ${FALLBACK_CONFIG.maxRevertAttempts} message removals. Please start a new session.`,
variant: "error",
duration: 5000,
},
})
.catch(() => {})
return
}
retryState.attempt++
retryState.lastAttemptTime = Date.now()
const retryState = getOrCreateRetryState(autoCompactState, sessionID)
if (retryState.attempt < RETRY_CONFIG.maxAttempts) {
retryState.attempt++
retryState.lastAttemptTime = Date.now()
try {
const providerID = msg.providerID as string | undefined
const modelID = msg.modelID as string | undefined
if (providerID && modelID) {
await (client as Client).session.summarize({
path: { id: sessionID },
body: { providerID, modelID },
query: { directory },
})
try {
await (client as Client).tui
.showToast({
body: {
title: "Auto Compact",
message: `Summarizing session (attempt ${retryState.attempt}/${RETRY_CONFIG.maxAttempts})...`,
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
clearSessionState(autoCompactState, sessionID)
await (client as Client).session.summarize({
path: { id: sessionID },
body: { providerID, modelID },
query: { directory },
})
setTimeout(async () => {
try {
await (client as Client).tui.submitPrompt({ query: { directory } })
} catch {}
}, 500)
clearSessionState(autoCompactState, sessionID)
setTimeout(async () => {
try {
await (client as Client).session.prompt_async({
path: { sessionID },
body: { parts: [{ type: "text", text: "Continue" }] },
query: { directory },
})
} catch {}
}, 500)
return
} catch {
autoCompactState.compactionInProgress.delete(sessionID)
const delay = RETRY_CONFIG.initialDelayMs * Math.pow(RETRY_CONFIG.backoffFactor, retryState.attempt - 1)
const cappedDelay = Math.min(delay, RETRY_CONFIG.maxDelayMs)
setTimeout(() => {
executeCompact(sessionID, msg, autoCompactState, client, directory)
}, cappedDelay)
return
}
}
} catch {
const delay = calculateRetryDelay(retryState.attempt)
await (client as Client).tui
.showToast({
body: {
title: "Auto Compact Retry",
message: `Attempt ${retryState.attempt}/${RETRY_CONFIG.maxAttempts} failed. Retrying in ${Math.round(delay / 1000)}s...`,
variant: "warning",
duration: delay,
},
})
.catch(() => {})
setTimeout(() => {
executeCompact(sessionID, msg, autoCompactState, client, directory)
}, delay)
}
const fallbackState = getOrCreateFallbackState(autoCompactState, sessionID)
if (fallbackState.revertAttempt < FALLBACK_CONFIG.maxRevertAttempts) {
const pair = await getLastMessagePair(sessionID, client as Client, directory)
if (pair) {
try {
await (client as Client).tui
.showToast({
body: {
title: "Emergency Recovery",
message: "Removing last message pair...",
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
if (pair.assistantMessageID) {
await (client as Client).session.revert({
path: { id: sessionID },
body: { messageID: pair.assistantMessageID },
query: { directory },
})
}
await (client as Client).session.revert({
path: { id: sessionID },
body: { messageID: pair.userMessageID },
query: { directory },
})
fallbackState.revertAttempt++
fallbackState.lastRevertedMessageID = pair.userMessageID
retryState.attempt = 0
truncateState.truncateAttempt = 0
autoCompactState.compactionInProgress.delete(sessionID)
setTimeout(() => {
executeCompact(sessionID, msg, autoCompactState, client, directory)
}, 1000)
return
} catch {}
}
}
clearSessionState(autoCompactState, sessionID)
await (client as Client).tui
.showToast({
body: {
title: "Auto Compact Failed",
message: "All recovery attempts failed. Please start a new session.",
variant: "error",
duration: 5000,
},
})
.catch(() => {})
}

View File

@@ -9,6 +9,8 @@ function createAutoCompactState(): AutoCompactState {
errorDataBySession: new Map<string, ParsedTokenLimitError>(),
retryStateBySession: new Map(),
fallbackStateBySession: new Map(),
truncateStateBySession: new Map(),
compactionInProgress: new Set<string>(),
}
}
@@ -25,6 +27,8 @@ export function createAnthropicAutoCompactHook(ctx: PluginInput) {
autoCompactState.errorDataBySession.delete(sessionInfo.id)
autoCompactState.retryStateBySession.delete(sessionInfo.id)
autoCompactState.fallbackStateBySession.delete(sessionInfo.id)
autoCompactState.truncateStateBySession.delete(sessionInfo.id)
autoCompactState.compactionInProgress.delete(sessionInfo.id)
}
return
}
@@ -37,6 +41,37 @@ export function createAnthropicAutoCompactHook(ctx: PluginInput) {
if (parsed) {
autoCompactState.pendingCompact.add(sessionID)
autoCompactState.errorDataBySession.set(sessionID, parsed)
if (autoCompactState.compactionInProgress.has(sessionID)) {
return
}
const lastAssistant = await getLastAssistant(sessionID, ctx.client, ctx.directory)
const providerID = parsed.providerID ?? (lastAssistant?.providerID as string | undefined)
const modelID = parsed.modelID ?? (lastAssistant?.modelID as string | undefined)
if (providerID && modelID) {
await ctx.client.tui
.showToast({
body: {
title: "Context Limit Hit",
message: "Truncating large tool outputs and recovering...",
variant: "warning" as const,
duration: 3000,
},
})
.catch(() => {})
setTimeout(() => {
executeCompact(
sessionID,
{ providerID, modelID },
autoCompactState,
ctx.client,
ctx.directory
)
}, 300)
}
}
return
}
@@ -122,6 +157,6 @@ export function createAnthropicAutoCompactHook(ctx: PluginInput) {
}
}
export type { AutoCompactState, FallbackState, ParsedTokenLimitError } from "./types"
export type { AutoCompactState, FallbackState, ParsedTokenLimitError, TruncateState } from "./types"
export { parseAnthropicTokenLimitError } from "./parser"
export { executeCompact, getLastAssistant } from "./executor"

View File

@@ -0,0 +1,173 @@
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
import { join } from "node:path"
import { xdgData } from "xdg-basedir"
const OPENCODE_STORAGE = join(xdgData ?? "", "opencode", "storage")
const MESSAGE_STORAGE = join(OPENCODE_STORAGE, "message")
const PART_STORAGE = join(OPENCODE_STORAGE, "part")
const TRUNCATION_MESSAGE =
"[TOOL RESULT TRUNCATED - Context limit exceeded. Original output was too large and has been truncated to recover the session. Please re-run this tool if you need the full output.]"
interface StoredToolPart {
id: string
sessionID: string
messageID: string
type: "tool"
callID: string
tool: string
state: {
status: "pending" | "running" | "completed" | "error"
input: Record<string, unknown>
output?: string
error?: string
time?: {
start: number
end?: number
compacted?: number
}
}
truncated?: boolean
originalSize?: number
}
export interface ToolResultInfo {
partPath: string
partId: string
messageID: string
toolName: string
outputSize: number
}
function getMessageDir(sessionID: string): string {
if (!existsSync(MESSAGE_STORAGE)) return ""
const directPath = join(MESSAGE_STORAGE, sessionID)
if (existsSync(directPath)) {
return directPath
}
for (const dir of readdirSync(MESSAGE_STORAGE)) {
const sessionPath = join(MESSAGE_STORAGE, dir, sessionID)
if (existsSync(sessionPath)) {
return sessionPath
}
}
return ""
}
function getMessageIds(sessionID: string): string[] {
const messageDir = getMessageDir(sessionID)
if (!messageDir || !existsSync(messageDir)) return []
const messageIds: string[] = []
for (const file of readdirSync(messageDir)) {
if (!file.endsWith(".json")) continue
const messageId = file.replace(".json", "")
messageIds.push(messageId)
}
return messageIds
}
export function findToolResultsBySize(sessionID: string): ToolResultInfo[] {
const messageIds = getMessageIds(sessionID)
const results: ToolResultInfo[] = []
for (const messageID of messageIds) {
const partDir = join(PART_STORAGE, messageID)
if (!existsSync(partDir)) continue
for (const file of readdirSync(partDir)) {
if (!file.endsWith(".json")) continue
try {
const partPath = join(partDir, file)
const content = readFileSync(partPath, "utf-8")
const part = JSON.parse(content) as StoredToolPart
if (part.type === "tool" && part.state?.output && !part.truncated) {
results.push({
partPath,
partId: part.id,
messageID,
toolName: part.tool,
outputSize: part.state.output.length,
})
}
} catch {
continue
}
}
}
return results.sort((a, b) => b.outputSize - a.outputSize)
}
export function findLargestToolResult(sessionID: string): ToolResultInfo | null {
const results = findToolResultsBySize(sessionID)
return results.length > 0 ? results[0] : null
}
export function truncateToolResult(partPath: string): {
success: boolean
toolName?: string
originalSize?: number
} {
try {
const content = readFileSync(partPath, "utf-8")
const part = JSON.parse(content) as StoredToolPart
if (!part.state?.output) {
return { success: false }
}
const originalSize = part.state.output.length
const toolName = part.tool
part.truncated = true
part.originalSize = originalSize
part.state.output = TRUNCATION_MESSAGE
if (!part.state.time) {
part.state.time = { start: Date.now() }
}
part.state.time.compacted = Date.now()
writeFileSync(partPath, JSON.stringify(part, null, 2))
return { success: true, toolName, originalSize }
} catch {
return { success: false }
}
}
export function getTotalToolOutputSize(sessionID: string): number {
const results = findToolResultsBySize(sessionID)
return results.reduce((sum, r) => sum + r.outputSize, 0)
}
export function countTruncatedResults(sessionID: string): number {
const messageIds = getMessageIds(sessionID)
let count = 0
for (const messageID of messageIds) {
const partDir = join(PART_STORAGE, messageID)
if (!existsSync(partDir)) continue
for (const file of readdirSync(partDir)) {
if (!file.endsWith(".json")) continue
try {
const content = readFileSync(join(partDir, file), "utf-8")
const part = JSON.parse(content)
if (part.truncated === true) {
count++
}
} catch {
continue
}
}
}
return count
}

View File

@@ -17,11 +17,18 @@ export interface FallbackState {
lastRevertedMessageID?: string
}
export interface TruncateState {
truncateAttempt: number
lastTruncatedPartId?: string
}
export interface AutoCompactState {
pendingCompact: Set<string>
errorDataBySession: Map<string, ParsedTokenLimitError>
retryStateBySession: Map<string, RetryState>
fallbackStateBySession: Map<string, FallbackState>
truncateStateBySession: Map<string, TruncateState>
compactionInProgress: Set<string>
}
export const RETRY_CONFIG = {
@@ -35,3 +42,8 @@ export const FALLBACK_CONFIG = {
maxRevertAttempts: 3,
minMessagesRequired: 2,
} as const
export const TRUNCATE_CONFIG = {
maxTruncateAttempts: 10,
minOutputSizeToTruncate: 1000,
} as const

View File

@@ -111,6 +111,7 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
if (result.messages.length > 0) {
const hookContent = result.messages.join("\n\n")
log(`[claude-code-hooks] Injecting ${result.messages.length} hook messages`, { sessionID: input.sessionID, contentLength: hookContent.length })
const message = output.message as {
agent?: string
model?: { modelID?: string; providerID?: string }

View File

@@ -0,0 +1,100 @@
import type { Message, Part } from "@opencode-ai/sdk"
const PLACEHOLDER_TEXT = "[user interrupted]"
interface MessageWithParts {
info: Message
parts: Part[]
}
type MessagesTransformHook = {
// NOTE: This sanitizer runs on experimental.chat.messages.transform hook,
// which executes AFTER chat.message hooks. Filesystem-injected messages
// from hooks like claude-code-hooks and keyword-detector may bypass this
// sanitizer if they inject empty content. Validation should be done at
// injection time in injectHookMessage().
"experimental.chat.messages.transform"?: (
input: Record<string, never>,
output: { messages: MessageWithParts[] }
) => Promise<void>
}
function hasTextContent(part: Part): boolean {
if (part.type === "text") {
const text = (part as unknown as { text?: string }).text
return Boolean(text && text.trim().length > 0)
}
return false
}
function isToolPart(part: Part): boolean {
const type = part.type as string
return type === "tool" || type === "tool_use" || type === "tool_result"
}
function hasValidContent(parts: Part[]): boolean {
return parts.some((part) => hasTextContent(part) || isToolPart(part))
}
export function createEmptyMessageSanitizerHook(): MessagesTransformHook {
return {
"experimental.chat.messages.transform": async (_input, output) => {
const { messages } = output
for (const message of messages) {
if (message.info.role === "user") continue
const parts = message.parts
// FIX: Removed `&& parts.length > 0` - empty arrays also need sanitization
// When parts is [], the message has no content and would cause API error:
// "all messages must have non-empty content except for the optional final assistant message"
if (!hasValidContent(parts)) {
let injected = false
for (const part of parts) {
if (part.type === "text") {
const textPart = part as unknown as { text?: string; synthetic?: boolean }
if (!textPart.text || !textPart.text.trim()) {
textPart.text = PLACEHOLDER_TEXT
textPart.synthetic = true
injected = true
break
}
}
}
if (!injected) {
const insertIndex = parts.findIndex((p) => isToolPart(p))
const newPart = {
id: `synthetic_${Date.now()}`,
messageID: message.info.id,
sessionID: (message.info as unknown as { sessionID?: string }).sessionID ?? "",
type: "text" as const,
text: PLACEHOLDER_TEXT,
synthetic: true,
}
if (insertIndex === -1) {
parts.push(newPart as Part)
} else {
parts.splice(insertIndex, 0, newPart as Part)
}
}
}
for (const part of parts) {
if (part.type === "text") {
const textPart = part as unknown as { text?: string; synthetic?: boolean }
if (textPart.text !== undefined && textPart.text.trim() === "") {
textPart.text = PLACEHOLDER_TEXT
textPart.synthetic = true
}
}
}
}
},
}
}

View File

@@ -19,3 +19,4 @@ export { createAgentUsageReminderHook } from "./agent-usage-reminder";
export { createKeywordDetectorHook } from "./keyword-detector";
export { createNonInteractiveEnvHook } from "./non-interactive-env";
export { createInteractiveBashSessionHook } from "./interactive-bash-session";
export { createEmptyMessageSanitizerHook } from "./empty-message-sanitizer";

View File

@@ -43,6 +43,7 @@ export function createKeywordDetectorHook() {
}
const context = messages.join("\n")
log(`[keyword-detector] Injecting context for ${messages.length} keywords`, { sessionID: input.sessionID, contextLength: context.length })
const success = injectHookMessage(input.sessionID, context, {
agent: message.agent,
model: message.model,

View File

@@ -48,19 +48,34 @@ async function sendNotification(
title: string,
message: string
): Promise<void> {
const escapedTitle = title.replace(/"/g, '\\"').replace(/'/g, "\\'")
const escapedMessage = message.replace(/"/g, '\\"').replace(/'/g, "\\'")
switch (p) {
case "darwin":
await ctx.$`osascript -e ${"display notification \"" + escapedMessage + "\" with title \"" + escapedTitle + "\""}`
case "darwin": {
const esTitle = title.replace(/\\/g, "\\\\").replace(/"/g, '\\"')
const esMessage = message.replace(/\\/g, "\\\\").replace(/"/g, '\\"')
await ctx.$`osascript -e ${"display notification \"" + esMessage + "\" with title \"" + esTitle + "\""}`
break
}
case "linux":
await ctx.$`notify-send ${escapedTitle} ${escapedMessage} 2>/dev/null`.catch(() => {})
await ctx.$`notify-send ${title} ${message} 2>/dev/null`.catch(() => {})
break
case "win32":
await ctx.$`powershell -Command ${"[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('" + escapedMessage + "', '" + escapedTitle + "')"}`
case "win32": {
const psTitle = title.replace(/'/g, "''")
const psMessage = message.replace(/'/g, "''")
const toastScript = `
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$RawXml = [xml] $Template.GetXml()
($RawXml.toast.visual.binding.text | Where-Object {$_.id -eq '1'}).AppendChild($RawXml.CreateTextNode('${psTitle}')) | Out-Null
($RawXml.toast.visual.binding.text | Where-Object {$_.id -eq '2'}).AppendChild($RawXml.CreateTextNode('${psMessage}')) | Out-Null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$SerializedXml.LoadXml($RawXml.OuterXml)
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('OpenCode')
$Notifier.Show($Toast)
`.trim().replace(/\n/g, "; ")
await ctx.$`powershell -Command ${toastScript}`.catch(() => {})
break
}
}
}

View File

@@ -4,12 +4,14 @@ import {
findEmptyMessages,
findEmptyMessageByIndex,
findMessageByIndexNeedingThinking,
findMessagesWithEmptyTextParts,
findMessagesWithOrphanThinking,
findMessagesWithThinkingBlocks,
findMessagesWithThinkingOnly,
injectTextPart,
prependThinkingPart,
readParts,
replaceEmptyTextParts,
stripThinkingParts,
} from "./storage"
import type { MessageData } from "./types"
@@ -222,28 +224,48 @@ async function recoverEmptyContentMessage(
): Promise<boolean> {
const targetIndex = extractMessageIndex(error)
const failedID = failedAssistantMsg.info?.id
let anySuccess = false
const messagesWithEmptyText = findMessagesWithEmptyTextParts(sessionID)
for (const messageID of messagesWithEmptyText) {
if (replaceEmptyTextParts(messageID, PLACEHOLDER_TEXT)) {
anySuccess = true
}
}
const thinkingOnlyIDs = findMessagesWithThinkingOnly(sessionID)
for (const messageID of thinkingOnlyIDs) {
injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT)
if (injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT)) {
anySuccess = true
}
}
if (targetIndex !== null) {
const targetMessageID = findEmptyMessageByIndex(sessionID, targetIndex)
if (targetMessageID) {
return injectTextPart(sessionID, targetMessageID, PLACEHOLDER_TEXT)
if (replaceEmptyTextParts(targetMessageID, PLACEHOLDER_TEXT)) {
return true
}
if (injectTextPart(sessionID, targetMessageID, PLACEHOLDER_TEXT)) {
return true
}
}
}
if (failedID) {
if (replaceEmptyTextParts(failedID, PLACEHOLDER_TEXT)) {
return true
}
if (injectTextPart(sessionID, failedID, PLACEHOLDER_TEXT)) {
return true
}
}
const emptyMessageIDs = findEmptyMessages(sessionID)
let anySuccess = thinkingOnlyIDs.length > 0
for (const messageID of emptyMessageIDs) {
if (replaceEmptyTextParts(messageID, PLACEHOLDER_TEXT)) {
anySuccess = true
}
if (injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT)) {
anySuccess = true
}

View File

@@ -271,6 +271,55 @@ export function stripThinkingParts(messageID: string): boolean {
return anyRemoved
}
export function replaceEmptyTextParts(messageID: string, replacementText: string): boolean {
const partDir = join(PART_STORAGE, messageID)
if (!existsSync(partDir)) return false
let anyReplaced = false
for (const file of readdirSync(partDir)) {
if (!file.endsWith(".json")) continue
try {
const filePath = join(partDir, file)
const content = readFileSync(filePath, "utf-8")
const part = JSON.parse(content) as StoredPart
if (part.type === "text") {
const textPart = part as StoredTextPart
if (!textPart.text?.trim()) {
textPart.text = replacementText
textPart.synthetic = true
writeFileSync(filePath, JSON.stringify(textPart, null, 2))
anyReplaced = true
}
}
} catch {
continue
}
}
return anyReplaced
}
export function findMessagesWithEmptyTextParts(sessionID: string): string[] {
const messages = readMessages(sessionID)
const result: string[] = []
for (const msg of messages) {
const parts = readParts(msg.id)
const hasEmptyTextPart = parts.some((p) => {
if (p.type !== "text") return false
const textPart = p as StoredTextPart
return !textPart.text?.trim()
})
if (hasEmptyTextPart) {
result.push(msg.id)
}
}
return result
}
export function findMessageByIndexNeedingThinking(sessionID: string, targetIndex: number): string | null {
const messages = readMessages(sessionID)

View File

@@ -20,6 +20,7 @@ import {
createAgentUsageReminderHook,
createNonInteractiveEnvHook,
createInteractiveBashSessionHook,
createEmptyMessageSanitizerHook,
} from "./hooks";
import { createGoogleAntigravityAuthPlugin } from "./auth/antigravity";
import {
@@ -246,6 +247,9 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
const interactiveBashSession = isHookEnabled("interactive-bash-session")
? createInteractiveBashSessionHook(ctx)
: null;
const emptyMessageSanitizer = isHookEnabled("empty-message-sanitizer")
? createEmptyMessageSanitizerHook()
: null;
updateTerminalTitle({ sessionId: "main" });
@@ -259,7 +263,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
const callOmoAgent = createCallOmoAgent(ctx, backgroundManager);
const lookAt = createLookAt(ctx);
const googleAuthHooks = pluginConfig.google_auth
const googleAuthHooks = pluginConfig.google_auth !== false
? await createGoogleAntigravityAuthPlugin(ctx)
: null;
@@ -281,6 +285,14 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
await keywordDetector?.["chat.message"]?.(input, output);
},
"experimental.chat.messages.transform": async (
input: Record<string, never>,
output: { messages: Array<{ info: unknown; parts: unknown[] }> }
) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await emptyMessageSanitizer?.["experimental.chat.messages.transform"]?.(input, output as any);
},
config: async (config) => {
const builtinAgents = createBuiltinAgents(
pluginConfig.disabled_agents,

View File

@@ -25,9 +25,12 @@ Arguments:
The system automatically notifies when background tasks complete. You typically don't need block=true.`
export const BACKGROUND_CANCEL_DESCRIPTION = `Cancel a running background task.
export const BACKGROUND_CANCEL_DESCRIPTION = `Cancel running background task(s).
Only works for tasks with status "running". Aborts the background session and marks the task as cancelled.
Arguments:
- taskId: Required task ID to cancel.`
- taskId: Task ID to cancel (optional if all=true)
- all: Set to true to cancel ALL running background tasks at once (default: false)
**Cleanup Before Answer**: When you have gathered sufficient information and are ready to provide your final answer to the user, use \`all=true\` to cancel ALL running background tasks first, then deliver your response. This conserves resources and ensures clean workflow completion.`

View File

@@ -263,11 +263,42 @@ export function createBackgroundCancel(manager: BackgroundManager, client: Openc
return tool({
description: BACKGROUND_CANCEL_DESCRIPTION,
args: {
taskId: tool.schema.string().describe("Task ID to cancel"),
taskId: tool.schema.string().optional().describe("Task ID to cancel (required if all=false)"),
all: tool.schema.boolean().optional().describe("Cancel all running background tasks (default: false)"),
},
async execute(args: BackgroundCancelArgs) {
async execute(args: BackgroundCancelArgs, toolContext) {
try {
const task = manager.getTask(args.taskId)
const cancelAll = args.all === true
if (!cancelAll && !args.taskId) {
return `❌ Invalid arguments: Either provide a taskId or set all=true to cancel all running tasks.`
}
if (cancelAll) {
const tasks = manager.getTasksByParentSession(toolContext.sessionID)
const runningTasks = tasks.filter(t => t.status === "running")
if (runningTasks.length === 0) {
return `✅ No running background tasks to cancel.`
}
const results: string[] = []
for (const task of runningTasks) {
client.session.abort({
path: { id: task.sessionID },
}).catch(() => {})
task.status = "cancelled"
task.completedAt = new Date()
results.push(`- ${task.id}: ${task.description}`)
}
return `✅ Cancelled ${runningTasks.length} background task(s):
${results.join("\n")}`
}
const task = manager.getTask(args.taskId!)
if (!task) {
return `❌ Task not found: ${args.taskId}`
}

View File

@@ -11,5 +11,6 @@ export interface BackgroundOutputArgs {
}
export interface BackgroundCancelArgs {
taskId: string
taskId?: string
all?: boolean
}

View File

@@ -21,6 +21,45 @@ class LSPServerManager {
private constructor() {
this.startCleanupTimer()
this.registerProcessCleanup()
}
private registerProcessCleanup(): void {
const cleanup = () => {
for (const [, managed] of this.clients) {
try {
managed.client.stop()
} catch {}
}
this.clients.clear()
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval)
this.cleanupInterval = null
}
}
// Works on all platforms
process.on("exit", cleanup)
// Ctrl+C - works on all platforms
process.on("SIGINT", () => {
cleanup()
process.exit(0)
})
// Kill signal - Unix/macOS
process.on("SIGTERM", () => {
cleanup()
process.exit(0)
})
// Ctrl+Break - Windows specific
if (process.platform === "win32") {
process.on("SIGBREAK", () => {
cleanup()
process.exit(0)
})
}
}
static getInstance(): LSPServerManager {