Compare commits

..

29 Commits

Author SHA1 Message Date
github-actions[bot]
64db980803 release: v0.1.26 2025-12-08 00:41:34 +00:00
YeonGyu-Kim
b86346a79d fix(lsp): add Push model support and fix JSON LSP diagnostics
- Add diagnosticsStore to capture Push model notifications
- Handle textDocument/publishDiagnostics notifications in processBuffer
- Fix workspace/configuration response for JSON LSP validation
- Add missing language mappings (json, html, css, sh, fish, md, tf)
- diagnostics() now tries Pull first, falls back to Push store
2025-12-08 09:38:00 +09:00
YeonGyu-Kim
4debb57402 feat(hooks): add pulse-monitor for token stall detection and auto-recovery
- Detect token stalls via message.part.updated heartbeat monitoring
- Support thinking/reasoning block detection with extended timeout
- Auto-recover: abort + 'continue' prompt on 5min stall
- Pause monitoring during tool execution
2025-12-06 11:17:55 +09:00
YeonGyu-Kim
a763db61cf improve(hooks): refine todo-continuation-enforcer message tone and status format 2025-12-06 10:54:36 +09:00
YeonGyu-Kim
341e5a959d feat(hooks): add grep-output-truncator for context-aware output limiting 2025-12-06 10:54:22 +09:00
YeonGyu-Kim
bac304c035 docs: add explicit agent invocation examples to README 2025-12-05 23:48:11 +09:00
YeonGyu-Kim
1aaeefac0e docs: enhance LSP tools description in README 2025-12-05 23:27:07 +09:00
github-actions[bot]
dda7b4f56d release: v0.1.25 2025-12-05 14:25:22 +00:00
YeonGyu-Kim
a287e59262 feat(session-recovery): add filesystem-based empty content recovery
- Replace API-based recovery with direct JSON file editing for empty content messages
- Add cross-platform storage path support via xdg-basedir (Linux/macOS/Windows)
- Inject '(interrupted)' text part to fix messages with only thinking/meta blocks
- Update README docs with detailed session recovery scenarios
2025-12-05 23:24:20 +09:00
github-actions[bot]
80fe3ae612 release: v0.1.24 2025-12-05 13:53:30 +00:00
YeonGyu-Kim
b045f6918e feat(lsp): add result limits to prevent token overflow
- Add DEFAULT_MAX_REFERENCES, DEFAULT_MAX_SYMBOLS, DEFAULT_MAX_DIAGNOSTICS (200 each)
- Apply limits to lsp_find_references, lsp_document_symbols, lsp_workspace_symbols, lsp_diagnostics
- Show truncation warning when results exceed limits
2025-12-05 22:52:33 +09:00
YeonGyu-Kim
725ec9b91d feat(ast-grep): add safety limits to prevent token overflow
- Add timeout (5min), output limit (1MB), match limit (500)
- Add SgResult type with truncation info
- Update formatSearchResult/formatReplaceResult for truncation display
- cli.ts: timeout + output truncation + graceful JSON recovery
2025-12-05 22:52:33 +09:00
github-actions[bot]
1f717a76be release: v0.1.23 2025-12-05 13:19:23 +00:00
YeonGyu-Kim
3bcb869a5d fix(ast-grep): add isValidBinary check to all path resolutions
- Check file size >10KB to filter out placeholder files
- Check cached binary first
- Then npm package paths with validation
- Homebrew paths as last resort
- Fixes SIGTRAP/ENOEXEC from invalid binaries
2025-12-05 22:18:17 +09:00
github-actions[bot]
54e13e4330 release: v0.1.22 2025-12-05 13:13:29 +00:00
YeonGyu-Kim
1780e2971d refactor(ast-grep): simplify binary resolution, rely on auto-download
- Remove hardcoded homebrew paths
- Remove npm package path resolution (prone to placeholder issues)
- Only check cached binary (~/.cache/oh-my-opencode/bin/sg)
- If not found, cli.ts will auto-download from GitHub releases

The download logic in cli.ts handles all cases properly.
2025-12-05 22:12:12 +09:00
github-actions[bot]
ded97701b8 release: v0.1.21 2025-12-05 13:04:11 +00:00
YeonGyu-Kim
316cdc1a62 fix(ast-grep): validate binary before using, prioritize homebrew path
- Add isValidBinary() check: file must be >10KB (placeholder files are ~100 bytes)
- Check homebrew paths first on macOS (most reliable)
- Check cached binary second
- npm package paths last (prone to placeholder issues)

Fixes ENOEXEC error when @ast-grep/cli has placeholder instead of real binary
2025-12-05 22:03:05 +09:00
YeonGyu-Kim
f19cd8fc71 improve(ast-grep): better Python pattern hints
- Show exact pattern without colon when pattern ends with ':'
- More actionable hint message
2025-12-05 21:57:58 +09:00
github-actions[bot]
181194ae3c release: v0.1.19 2025-12-05 12:00:31 +00:00
YeonGyu-Kim
b8f5599e61 feat(ast-grep): add helpful hints for incomplete Python patterns
- Show hints when Python class/function patterns return empty results
- Detect patterns ending with ':' that need body (class :, def ():)
- Removed validation that could cause false positives
- Hints only appear on empty results, not on successful matches
2025-12-05 20:59:05 +09:00
github-actions[bot]
ea2b09ebb9 release: v0.1.18 2025-12-05 11:07:21 +00:00
YeonGyu-Kim
143dd8aaa9 fix(session-recovery): improve error detection and add continue prompt
- Enhance error type detection for thinking block order issues
- Add continue prompt after successful session recovery
- Improve error message matching logic
2025-12-05 20:01:47 +09:00
YeonGyu-Kim
36169c83fb feat(ast-grep): add CLI path resolution and auto-download functionality
- Add automatic CLI binary path detection and resolution
- Implement lazy binary download with caching
- Add environment check utilities for CLI and NAPI availability
- Improve error handling and fallback mechanisms
- Export new utilities from index.ts
2025-12-05 20:01:35 +09:00
github-actions[bot]
bf9f033635 release: v0.1.17 2025-12-05 07:26:35 +00:00
YeonGyu-Kim
de2f9dfc86 chore: move schema to assets/, add dist/ to gitignore 2025-12-05 16:24:54 +09:00
YeonGyu-Kim
5f5218ccae docs: fix Agents config section - remove incorrect OpenCode link, add correct options 2025-12-05 16:17:00 +09:00
YeonGyu-Kim
41d3817910 docs: reorganize Configuration section to bottom, add LSP config docs 2025-12-05 16:13:25 +09:00
YeonGyu-Kim
a119429ebe docs: remove MCP config from Agent section (already in top-level Config) 2025-12-05 16:01:19 +09:00
25 changed files with 1354 additions and 306 deletions

3
.gitignore vendored
View File

@@ -2,8 +2,7 @@
node_modules/
# Build output
dist/*
!dist/oh-my-opencode.schema.json
dist/
# IDE
.idea/

View File

@@ -5,10 +5,6 @@
- [Oh My OpenCode](#oh-my-opencode)
- [세 줄 요약](#세-줄-요약)
- [설치](#설치)
- [설정](#설정)
- [특정 MCP 비활성화](#특정-mcp-비활성화)
- [특정 Agent 비활성화](#특정-agent-비활성화)
- [Agent 설정](#agent-설정)
- [LLM Agent를 위한 안내](#llm-agent를-위한-안내)
- [Why OpenCode & Why Oh My OpenCode](#why-opencode--why-oh-my-opencode)
- [기능](#기능)
@@ -20,6 +16,7 @@
- [Safe Grep](#safe-grep)
- [내장 MCPs](#내장-mcps)
- [기타 편의 기능](#기타-편의-기능)
- [설정](#설정)
- [작성자의 노트](#작성자의-노트)
- [주의](#주의)
@@ -61,45 +58,6 @@ OpenCode 가 낭만이 사라진것같은 오늘날의 시대에, 당신에게
}
```
## 설정
`oh-my-opencode.json` (또는 `.oh-my-opencode.json`) 파일을 프로젝트 루트에 생성해서 Oh My OpenCode를 입맛대로 설정할 수 있어.
```json
{
"$schema": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/dist/oh-my-opencode.schema.json"
}
```
설정 파일은 스키마를 통해 자동완성을 지원합니다. 자세한 내용은 각 기능 섹션에서 설명합니다.
### 특정 MCP 비활성화
특정 MCP가 거슬린다면 끌 수 있어.
```json
{
"disabled_mcps": ["websearch_exa"]
}
```
### 특정 Agent 비활성화
특정 에이전트가 마음에 안 들거나, 토큰을 아끼고 싶다면 비활성화해.
비활성화 가능한 목록: `oracle`, `librarian`, `explore`, `frontend-ui-ux-engineer`, `document-writer`
```json
{
"disabled_agents": ["frontend-ui-ux-engineer"]
}
```
### Agent 설정
`agents` 옵션으로 에이전트의 모델, 프롬프트, 권한 등을 세밀하게 조정할 수 있습니다. 스키마를 통해 자동완성을 지원합니다.
전체 설정 옵션과 예시는 [OpenCode Agents 문서](https://opencode.ai/docs/agents)를 참고하세요.
## LLM Agent를 위한 안내
<details>
@@ -178,6 +136,11 @@ OpenCode 는 아주 확장가능하고 아주 커스터마이저블합니다.
- **Context Window Monitor**: [컨텍스트 윈도우 불안 관리](https://agentic-patterns.com/patterns/context-window-anxiety-management/) 패턴을 구현합니다.
- 사용량이 70%를 넘으면 에이전트에게 아직 토큰이 충분하다고 상기시켜, 급하게 불완전한 작업을 하는 것을 완화합니다.
- **Session Notification**: 에이전트가 작업을 마치면 OS 네이티브 알림을 보냅니다 (macOS, Linux, Windows).
- **Session Recovery**: API 에러로부터 자동으로 복구하여 세션 안정성을 보장합니다. 네 가지 시나리오를 처리합니다:
- **Tool Result Missing**: `tool_use` 블록이 있지만 `tool_result`가 없을 때 (ESC 인터럽트) → "cancelled" tool result 주입
- **Thinking Block Order**: thinking 블록이 첫 번째여야 하는데 아닐 때 → 빈 thinking 블록 추가
- **Thinking Disabled Violation**: thinking 이 비활성화인데 thinking 블록이 있을 때 → thinking 블록 제거
- **Empty Content Message**: 메시지가 thinking/meta 블록만 있고 실제 내용이 없을 때 → 파일시스템을 통해 "(interrupted)" 텍스트 주입
- **Comment Checker**: 코드 수정 후 불필요한 주석을 감지하여 보고합니다. BDD 패턴, 지시어, 독스트링 등 유효한 주석은 똑똑하게 제외하고, AI가 남긴 흔적을 제거하여 코드를 깨끗하게 유지합니다.
### Agents
@@ -188,29 +151,22 @@ OpenCode 는 아주 확장가능하고 아주 커스터마이저블합니다.
- **frontend-ui-ux-engineer** (`google/gemini-3-pro-preview`): 개발자로 전향한 디자이너라는 설정을 갖고 있습니다. 멋진 UI를 만듭니다. 아름답고 창의적인 UI 코드를 생성하는 데 탁월한 Gemini를 사용합니다.
- **document-writer** (`google/gemini-3-pro-preview`): 기술 문서 전문가라는 설정을 갖고 있습니다. Gemini 는 문학가입니다. 글을 기가막히게 씁니다.
#### 설정
각 에이전트는 메인 에이전트가 알아서 호출하지만, 명시적으로 요청할 수도 있습니다:
에이전트들은 OpenCode와 동일한 설정 스펙을 따릅니다:
- **모델 변경**: `agents.{name}.model`로 에이전트 모델 오버라이드. [OpenCode Models](https://opencode.ai/docs/models/#configure-models) 참고.
- **MCP 비활성화**: `disabled_mcps`로 내장 MCP 끄기. [OpenCode MCP Servers](https://opencode.ai/docs/mcp-servers) 참고.
- **에이전트 비활성화**: `disabled_agents` 또는 `agents.{name}.disable` 사용. [OpenCode Agents](https://opencode.ai/docs/agents) 참고.
권장하진 않지만(이 플러그인은 멀티 모델 오케스트레이션용), Anthropic만 사용하는 경우 예시:
```json
{
"agents": {
"explore": { "model": "anthropic/claude-haiku-4-5" },
"frontend-ui-ux-engineer": { "model": "anthropic/claude-opus-4" }
}
}
```
@oracle 한테 이 부분 설계 고민하고서 아키텍쳐 제안을 부탁해줘
@librarian 한테 이 부분 어떻게 구현돼있길래 자꾸 안에서 동작이 바뀌는지 알려달라고 해줘
@explore 한테 이 기능 정책 알려달라고 해줘
```
에이전트의 모델, 프롬프트, 권한은 `oh-my-opencode.json`에서 커스텀할 수 있습니다. 자세한 내용은 [설정](#설정)을 참고하세요.
### Tools
#### 내장 LSP Tools
당신이 에디터에서 사용하는 그 기능을 다른 에이전트들은 사용하지 못합니다. Oh My OpenCode 는 당신만의 그 도구를 LLM Agent 에게 쥐어줍니다. 리팩토링하고, 탐색하고, 분석하는 모든 작업을 OpenCode 의 설정값을 그대로 사용하여 지원합니다.
[OpenCode 는 LSP 를 제공하지만](https://opencode.ai/docs/lsp/), 오로지 분석용으로만 제공합니다. 탐색과 리팩토링을 위한 도구는 OpenCode 와 동일한 스펙과 설정으로 Oh My OpenCode 가 제공합니다.
- **lsp_hover**: 위치의 타입 정보, 문서, 시그니처 가져오기
@@ -240,9 +196,96 @@ OpenCode 는 아주 확장가능하고 아주 커스터마이저블합니다.
- **websearch_exa**: Exa AI 웹 검색. 실시간 웹 검색과 콘텐츠 스크래핑을 수행합니다. 관련 웹사이트에서 LLM에 최적화된 컨텍스트를 반환합니다.
- **context7**: 라이브러리 문서 조회. 정확한 코딩을 위해 최신 라이브러리 문서를 가져옵니다.
필요 없다면 `oh-my-opencode.json`에서 비활성화할 수 있습니다:
```json
{
"disabled_mcps": ["websearch_exa"]
}
```
### 기타 편의 기능
- **Terminal Title**: 세션 상태에 따라 터미널 타이틀을 자동 업데이트합니다 (유휴 ○, 처리중 ◐, 도구 ⚡, 에러 ✖). tmux를 지원합니다.
## 설정
설정 파일 위치 (우선순위 순):
1. `.opencode/oh-my-opencode.json` (프로젝트)
2. `~/.config/opencode/oh-my-opencode.json` (사용자)
Schema 자동 완성이 지원됩니다:
```json
{
"$schema": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json"
}
```
### Agents
내장 에이전트 설정을 오버라이드할 수 있습니다:
```json
{
"agents": {
"explore": {
"model": "anthropic/claude-haiku-4-5",
"temperature": 0.5
},
"frontend-ui-ux-engineer": {
"disable": true
}
}
}
```
각 에이전트에서 지원하는 옵션: `model`, `temperature`, `top_p`, `prompt`, `tools`, `disable`, `description`, `mode`, `color`, `permission`.
또는 `disabled_agents`로 비활성화할 수 있습니다:
```json
{
"disabled_agents": ["oracle", "frontend-ui-ux-engineer"]
}
```
사용 가능한 에이전트: `oracle`, `librarian`, `explore`, `frontend-ui-ux-engineer`, `document-writer`
### MCPs
내장된 MCP를 비활성화합니다:
```json
{
"disabled_mcps": ["context7", "websearch_exa"]
}
```
더 자세한 내용은 [OpenCode MCP Servers](https://opencode.ai/docs/mcp-servers)를 참조하세요.
### LSP
Oh My OpenCode의 LSP 도구는 오직 **리팩토링(이름 변경, 코드 액션)만을 위한 것**입니다. 분석용 LSP는 OpenCode 자체에서 처리합니다.
`lsp` 옵션을 통해 LSP 서버를 설정합니다:
```json
{
"lsp": {
"typescript-language-server": {
"command": ["typescript-language-server", "--stdio"],
"extensions": [".ts", ".tsx"],
"priority": 10
},
"pylsp": {
"disabled": true
}
}
}
```
각 서버는 다음을 지원합니다: `command`, `extensions`, `priority`, `env`, `initialization`, `disabled`.
## 작성자의 노트
Oh My OpenCode 를 설치하세요. 복잡하게 OpenCode 구성을 만들지마세요.

163
README.md
View File

@@ -6,10 +6,6 @@ English | [한국어](README.ko.md)
- [TL;DR](#tldr)
- [Installation](#installation)
- [For LLM Agents](#for-llm-agents)
- [Configuration](#configuration)
- [Disable specific MCPs](#disable-specific-mcps)
- [Disable specific Agents](#disable-specific-agents)
- [Agent Configuration](#agent-configuration)
- [Why OpenCode & Why Oh My OpenCode](#why-opencode--why-oh-my-opencode)
- [Features](#features)
- [Hooks](#hooks)
@@ -20,6 +16,7 @@ English | [한국어](README.ko.md)
- [Safe Grep](#safe-grep)
- [Built-in MCPs](#built-in-mcps)
- [Other Features](#other-features)
- [Configuration](#configuration)
- [Author's Note](#authors-note)
- [Warnings](#warnings)
@@ -110,46 +107,6 @@ cat ~/.config/opencode/opencode.json # Should contain "oh-my-opencode" in plugi
</details>
## Configuration
You can configure Oh My OpenCode by creating a `oh-my-opencode.json` (or `.oh-my-opencode.json`) file in your project root.
Configuration supports autocomplete via schema. Details are covered in each feature section below.
```json
{
"$schema": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/dist/oh-my-opencode.schema.json"
}
```
### Disable specific MCPs
If you want to disable specific built-in MCPs, you can use the `disabled_mcps` option.
```json
{
"disabled_mcps": ["context7", "websearch_exa"]
}
```
### Disable specific Agents
If you want to disable specific built-in agents, you can use the `disabled_agents` option.
```json
{
"disabled_agents": ["explore", "frontend-ui-ux-engineer"]
}
```
Available agents: `oracle`, `librarian`, `explore`, `frontend-ui-ux-engineer`, `document-writer`
### Agent Configuration
You can override any built-in agent's model, prompt, permissions, and more using the `agents` option. Configuration uses autocomplete via schema.
For full configuration options and examples, see the [OpenCode Agents documentation](https://opencode.ai/docs/agents).
## Why OpenCode & Why Oh My OpenCode
OpenCode is limitlessly extensible and customizable. Zero screen flicker.
@@ -175,7 +132,11 @@ I believe in the right tool for the job. For your wallet's sake, use CLIProxyAPI
- **Todo Continuation Enforcer**: Forces the agent to complete all tasks before exiting. Eliminates the common LLM issue of "giving up halfway".
- **Context Window Monitor**: Implements [Context Window Anxiety Management](https://agentic-patterns.com/patterns/context-window-anxiety-management/). When context usage exceeds 70%, it reminds the agent that resources are sufficient, preventing rushed or low-quality output.
- **Session Notification**: Sends a native OS notification when the job is done (macOS, Linux, Windows).
- **Session Recovery**: Automatically recovers from API errors by injecting missing tool results and correcting thinking block violations, ensuring session stability.
- **Session Recovery**: Automatically recovers from API errors, ensuring session stability. Handles four scenarios:
- **Tool Result Missing**: When `tool_use` block exists without `tool_result` (ESC interrupt) → injects "cancelled" tool results
- **Thinking Block Order**: When thinking block must be first but isn't → prepends empty thinking block
- **Thinking Disabled Violation**: When thinking blocks exist but thinking is disabled → strips thinking blocks
- **Empty Content Message**: When message has only thinking/meta blocks without actual content → injects "(interrupted)" text via filesystem
- **Comment Checker**: Detects and reports unnecessary comments after code modifications. Smartly ignores valid patterns (BDD, directives, docstrings, shebangs) to keep the codebase clean from AI-generated artifacts.
### Agents
@@ -185,29 +146,22 @@ I believe in the right tool for the job. For your wallet's sake, use CLIProxyAPI
- **frontend-ui-ux-engineer** (`google/gemini-3-pro-preview`): A designer turned developer. Creates stunning UIs. Uses Gemini because its creativity and UI code generation are superior.
- **document-writer** (`google/gemini-3-pro-preview`): A technical writing expert. Gemini is a wordsmith; it writes prose that flows naturally.
#### Configuration
Each agent is automatically invoked by the main agent, but you can also explicitly request them:
Agents follow the same configuration spec as OpenCode:
- **Change models**: Override any agent's model via `agents.{name}.model`. See [OpenCode Models](https://opencode.ai/docs/models/#configure-models).
- **Disable MCPs**: Use `disabled_mcps` to turn off built-in MCPs. See [OpenCode MCP Servers](https://opencode.ai/docs/mcp-servers).
- **Disable agents**: Use `disabled_agents` or `agents.{name}.disable`. See [OpenCode Agents](https://opencode.ai/docs/agents).
While not generally recommended (this plugin is designed for multi-model orchestration), here's an example for Anthropic-only users:
```json
{
"agents": {
"explore": { "model": "anthropic/claude-haiku-4-5" },
"frontend-ui-ux-engineer": { "model": "anthropic/claude-opus-4" }
}
}
```
@oracle Please think through the design of this part and suggest an architecture.
@librarian Tell me how this is implemented — why does the behavior keep changing internally?
@explore Tell me about the policy for this feature.
```
Agent models, prompts, and permissions can be customized via `oh-my-opencode.json`. See [Configuration](#configuration) for details.
### Tools
#### Built-in LSP Tools
The features you use in your editor—other agents cannot access them. Oh My OpenCode hands those very tools to your LLM Agent. Refactoring, navigation, and analysis are all supported using the same OpenCode configuration.
[OpenCode provides LSP](https://opencode.ai/docs/lsp/), but only for analysis. Oh My OpenCode equips you with navigation and refactoring tools matching the same specification.
- **lsp_hover**: Get type info, docs, signatures at position
@@ -239,10 +193,97 @@ While not generally recommended (this plugin is designed for multi-model orchest
- **websearch_exa**: Exa AI web search. Performs real-time web searches and can scrape content from specific URLs. Returns LLM-optimized context from relevant websites.
- **context7**: Library documentation lookup. Fetches up-to-date documentation for any library to assist with accurate coding.
Don't need these? Disable them via `oh-my-opencode.json`:
```json
{
"disabled_mcps": ["websearch_exa"]
}
```
### Other Features
- **Terminal Title**: Auto-updates terminal title with session status (idle ○, processing ◐, tool ⚡, error ✖). Supports tmux.
## Configuration
Configuration file locations (in priority order):
1. `.opencode/oh-my-opencode.json` (project)
2. `~/.config/opencode/oh-my-opencode.json` (user)
Schema autocomplete is supported:
```json
{
"$schema": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json"
}
```
### Agents
Override built-in agent settings:
```json
{
"agents": {
"explore": {
"model": "anthropic/claude-haiku-4-5",
"temperature": 0.5
},
"frontend-ui-ux-engineer": {
"disable": true
}
}
}
```
Each agent supports: `model`, `temperature`, `top_p`, `prompt`, `tools`, `disable`, `description`, `mode`, `color`, `permission`.
Or disable agents via `disabled_agents`:
```json
{
"disabled_agents": ["oracle", "frontend-ui-ux-engineer"]
}
```
Available agents: `oracle`, `librarian`, `explore`, `frontend-ui-ux-engineer`, `document-writer`
### MCPs
Disable built-in MCPs:
```json
{
"disabled_mcps": ["context7", "websearch_exa"]
}
```
See [OpenCode MCP Servers](https://opencode.ai/docs/mcp-servers) for more.
### LSP
Oh My OpenCode's LSP tools are for **refactoring only** (rename, code actions). Analysis LSP is handled by OpenCode itself.
Configure LSP servers via `lsp` option:
```json
{
"lsp": {
"typescript-language-server": {
"command": ["typescript-language-server", "--stdio"],
"extensions": [".ts", ".tsx"],
"priority": 10
},
"pylsp": {
"disabled": true
}
}
}
```
Each server supports: `command`, `extensions`, `priority`, `env`, `initialization`, `disabled`.
## Author's Note
Install Oh My OpenCode. Do not waste time configuring OpenCode from scratch.

View File

@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/dist/oh-my-opencode.schema.json",
"$id": "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json",
"title": "Oh My OpenCode Configuration",
"description": "Configuration schema for oh-my-opencode plugin",
"type": "object",

View File

@@ -9,6 +9,7 @@
"@ast-grep/napi": "^0.40.0",
"@code-yeongyu/comment-checker": "^0.4.1",
"@opencode-ai/plugin": "^1.0.7",
"xdg-basedir": "^5.1.0",
"zod": "^4.1.8",
},
"devDependencies": {
@@ -102,6 +103,8 @@
"undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
"xdg-basedir": ["xdg-basedir@5.1.0", "", {}, "sha512-GCPAHLvrIH13+c0SuacwvRYj2SxJXQ4kaVTT5xgL3kPrz56XxkF21IGhjSE1+W0aw7gpBWRGXLCPnPby6lSpmQ=="],
"zod": ["zod@4.1.8", "", {}, "sha512-5R1P+WwQqmmMIEACyzSvo4JXHY5WiAFHRMg+zBZKgKS+Q1viRa0C1hmUKtHltoIFKtIdki3pRxkmpP74jnNYHQ=="],
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "oh-my-opencode",
"version": "0.1.16",
"version": "0.1.26",
"description": "OpenCode plugin - custom agents (oracle, librarian) and enhanced features",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -46,6 +46,7 @@
"@ast-grep/napi": "^0.40.0",
"@code-yeongyu/comment-checker": "^0.4.1",
"@opencode-ai/plugin": "^1.0.7",
"xdg-basedir": "^5.1.0",
"zod": "^4.1.8"
},
"devDependencies": {

View File

@@ -2,7 +2,7 @@
import * as z from "zod"
import { OhMyOpenCodeConfigSchema } from "../src/config/schema"
const SCHEMA_OUTPUT_PATH = "dist/oh-my-opencode.schema.json"
const SCHEMA_OUTPUT_PATH = "assets/oh-my-opencode.schema.json"
async function main() {
console.log("Generating JSON Schema...")
@@ -14,7 +14,7 @@ async function main() {
const finalSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/dist/oh-my-opencode.schema.json",
$id: "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json",
title: "Oh My OpenCode Configuration",
description: "Configuration schema for oh-my-opencode plugin",
...jsonSchema,

View File

@@ -0,0 +1,131 @@
import type { PluginInput } from "@opencode-ai/plugin"
const ANTHROPIC_ACTUAL_LIMIT = 200_000
const CHARS_PER_TOKEN_ESTIMATE = 4
const TARGET_MAX_TOKENS = 50_000
interface AssistantMessageInfo {
role: "assistant"
tokens: {
input: number
output: number
reasoning: number
cache: { read: number; write: number }
}
}
interface MessageWrapper {
info: { role: string } & Partial<AssistantMessageInfo>
}
function estimateTokens(text: string): number {
return Math.ceil(text.length / CHARS_PER_TOKEN_ESTIMATE)
}
function truncateToTokenLimit(output: string, maxTokens: number): { result: string; truncated: boolean } {
const currentTokens = estimateTokens(output)
if (currentTokens <= maxTokens) {
return { result: output, truncated: false }
}
const lines = output.split("\n")
if (lines.length <= 3) {
const maxChars = maxTokens * CHARS_PER_TOKEN_ESTIMATE
return {
result: output.slice(0, maxChars) + "\n\n[Output truncated due to context window limit]",
truncated: true,
}
}
const headerLines = lines.slice(0, 3)
const contentLines = lines.slice(3)
const headerText = headerLines.join("\n")
const headerTokens = estimateTokens(headerText)
const availableTokens = maxTokens - headerTokens - 50
if (availableTokens <= 0) {
return {
result: headerText + "\n\n[Content truncated due to context window limit]",
truncated: true,
}
}
let resultLines: string[] = []
let currentTokenCount = 0
for (const line of contentLines) {
const lineTokens = estimateTokens(line + "\n")
if (currentTokenCount + lineTokens > availableTokens) {
break
}
resultLines.push(line)
currentTokenCount += lineTokens
}
const truncatedContent = [...headerLines, ...resultLines].join("\n")
const removedCount = contentLines.length - resultLines.length
return {
result: truncatedContent + `\n\n[${removedCount} more lines truncated due to context window limit]`,
truncated: true,
}
}
export function createGrepOutputTruncatorHook(ctx: PluginInput) {
const GREP_TOOLS = ["safe_grep", "Grep"]
const toolExecuteAfter = async (
input: { tool: string; sessionID: string; callID: string },
output: { title: string; output: string; metadata: unknown }
) => {
if (!GREP_TOOLS.includes(input.tool)) return
const { sessionID } = input
try {
const response = await ctx.client.session.messages({
path: { id: sessionID },
})
const messages = (response.data ?? response) as MessageWrapper[]
const assistantMessages = messages
.filter((m) => m.info.role === "assistant")
.map((m) => m.info as AssistantMessageInfo)
if (assistantMessages.length === 0) return
const totalInputTokens = assistantMessages.reduce((sum, m) => {
const inputTokens = m.tokens?.input ?? 0
const cacheReadTokens = m.tokens?.cache?.read ?? 0
return sum + inputTokens + cacheReadTokens
}, 0)
const remainingTokens = ANTHROPIC_ACTUAL_LIMIT - totalInputTokens
const maxOutputTokens = Math.min(
remainingTokens * 0.5,
TARGET_MAX_TOKENS
)
if (maxOutputTokens <= 0) {
output.output = "[Output suppressed - context window exhausted]"
return
}
const { result, truncated } = truncateToTokenLimit(output.output, maxOutputTokens)
if (truncated) {
output.output = result
}
} catch {
// Graceful degradation
}
}
return {
"tool.execute.after": toolExecuteAfter,
}
}

View File

@@ -3,3 +3,5 @@ export { createContextWindowMonitorHook } from "./context-window-monitor"
export { createSessionNotification } from "./session-notification"
export { createSessionRecoveryHook } from "./session-recovery"
export { createCommentCheckerHooks } from "./comment-checker"
export { createGrepOutputTruncatorHook } from "./grep-output-truncator"
export { createPulseMonitorHook } from "./pulse-monitor"

142
src/hooks/pulse-monitor.ts Normal file
View File

@@ -0,0 +1,142 @@
import type { PluginInput } from "@opencode-ai/plugin"
export function createPulseMonitorHook(ctx: PluginInput) {
const STANDARD_TIMEOUT = 5 * 60 * 1000 // 5 minutes
const THINKING_TIMEOUT = 5 * 60 * 1000 // 5 minutes
const CHECK_INTERVAL = 5 * 1000 // 5 seconds
let lastHeartbeat = Date.now()
let isMonitoring = false
let currentSessionID: string | null = null
let monitorTimer: ReturnType<typeof setInterval> | null = null
let isThinking = false
const startMonitoring = (sessionID: string) => {
if (currentSessionID !== sessionID) {
currentSessionID = sessionID
// Reset thinking state when switching sessions or starting new
isThinking = false
}
lastHeartbeat = Date.now()
if (!isMonitoring) {
isMonitoring = true
if (monitorTimer) clearInterval(monitorTimer)
monitorTimer = setInterval(async () => {
if (!isMonitoring || !currentSessionID) return
const timeSinceLastHeartbeat = Date.now() - lastHeartbeat
const currentTimeout = isThinking ? THINKING_TIMEOUT : STANDARD_TIMEOUT
if (timeSinceLastHeartbeat > currentTimeout) {
await recoverStalledSession(currentSessionID, timeSinceLastHeartbeat, isThinking)
}
}, CHECK_INTERVAL)
}
}
const stopMonitoring = () => {
isMonitoring = false
if (monitorTimer) {
clearInterval(monitorTimer)
monitorTimer = null
}
}
const updateHeartbeat = (isThinkingUpdate?: boolean) => {
if (isMonitoring) {
lastHeartbeat = Date.now()
if (isThinkingUpdate !== undefined) {
isThinking = isThinkingUpdate
}
}
}
const recoverStalledSession = async (sessionID: string, stalledDuration: number, wasThinking: boolean) => {
stopMonitoring()
try {
const durationSec = Math.round(stalledDuration/1000)
const typeStr = wasThinking ? "Thinking" : "Standard"
// 1. Notify User
await ctx.client.tui.showToast({
body: {
title: "Pulse Monitor: Cardiac Arrest",
message: `Session stalled (${typeStr}) for ${durationSec}s. Defibrillating...`,
variant: "error",
duration: 5000
}
}).catch(() => {})
// 2. Abort current generation (Defibrillation shock)
await ctx.client.session.abort({ path: { id: sessionID } }).catch(() => {})
// 3. Wait a bit for state to settle
await new Promise(resolve => setTimeout(resolve, 1500))
// 4. Prompt "continue" to kickstart (CPR)
await ctx.client.session.prompt({
path: { id: sessionID },
body: { parts: [{ type: "text", text: "The connection was unstable and stalled. Please continue from where you left off." }] },
query: { directory: ctx.directory }
})
// Resume monitoring
startMonitoring(sessionID)
} catch (err) {
console.error("[PulseMonitor] Recovery failed:", err)
// If recovery fails, we stop monitoring to avoid loops
stopMonitoring()
}
}
return {
event: async (input: { event: any }) => {
const { event } = input
const props = event.properties as Record<string, any> | undefined
// Monitor both session updates and part updates to capture token flow
if (event.type === "session.updated" || event.type === "message.part.updated") {
// Try to get sessionID from various common locations
const sessionID = props?.info?.id || props?.sessionID
if (sessionID) {
if (!isMonitoring) startMonitoring(sessionID)
// Check for thinking indicators in the payload
let thinkingUpdate: boolean | undefined = undefined
if (event.type === "message.part.updated") {
const part = props?.part
if (part) {
const THINKING_TYPES = ["thinking", "redacted_thinking", "reasoning"]
if (THINKING_TYPES.includes(part.type)) {
thinkingUpdate = true
} else if (part.type === "text" || part.type === "tool_use") {
thinkingUpdate = false
}
}
}
updateHeartbeat(thinkingUpdate)
}
} else if (event.type === "session.idle" || event.type === "session.error" || event.type === "session.stopped") {
stopMonitoring()
}
},
"tool.execute.before": async () => {
// Pause monitoring while tool runs locally (tools can take time)
stopMonitoring()
},
"tool.execute.after": async (input: { sessionID: string }) => {
// Resume monitoring after tool finishes
if (input.sessionID) {
startMonitoring(input.sessionID)
}
}
}
}

View File

@@ -12,14 +12,21 @@
* - Recovery: strip thinking/redacted_thinking blocks
*
* 4. Empty content message (non-empty content required)
* - Recovery: delete the empty message via revert
* - Recovery: inject text part directly via filesystem
*/
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
import { join } from "node:path"
import { xdgData } from "xdg-basedir"
import type { PluginInput } from "@opencode-ai/plugin"
import type { createOpencodeClient } from "@opencode-ai/sdk"
type Client = ReturnType<typeof createOpencodeClient>
const OPENCODE_STORAGE = join(xdgData ?? "", "opencode", "storage")
const MESSAGE_STORAGE = join(OPENCODE_STORAGE, "message")
const PART_STORAGE = join(OPENCODE_STORAGE, "part")
type RecoveryErrorType = "tool_result_missing" | "thinking_block_order" | "thinking_disabled_violation" | "empty_content_message" | null
interface MessageInfo {
@@ -70,7 +77,10 @@ function detectErrorType(error: unknown): RecoveryErrorType {
return "tool_result_missing"
}
if (message.includes("thinking") && message.includes("first block")) {
if (
message.includes("thinking") &&
(message.includes("first block") || message.includes("must start with") || message.includes("preceeding"))
) {
return "thinking_block_order"
}
@@ -212,6 +222,140 @@ async function recoverThinkingDisabledViolation(
}
const THINKING_TYPES = new Set(["thinking", "redacted_thinking", "reasoning"])
const META_TYPES = new Set(["step-start", "step-finish"])
interface StoredMessageMeta {
id: string
sessionID: string
role: string
parentID?: string
}
interface StoredPart {
id: string
sessionID: string
messageID: string
type: string
text?: string
}
function generatePartId(): string {
const timestamp = Date.now().toString(16)
const random = Math.random().toString(36).substring(2, 10)
return `prt_${timestamp}${random}`
}
function getMessageDir(sessionID: string): string {
const projectHash = readdirSync(MESSAGE_STORAGE).find((dir) => {
const sessionDir = join(MESSAGE_STORAGE, dir)
try {
return readdirSync(sessionDir).some((f) => f.includes(sessionID.replace("ses_", "")))
} catch {
return false
}
})
if (projectHash) {
return join(MESSAGE_STORAGE, projectHash, sessionID)
}
for (const dir of readdirSync(MESSAGE_STORAGE)) {
const sessionPath = join(MESSAGE_STORAGE, dir, sessionID)
if (existsSync(sessionPath)) {
return sessionPath
}
}
return ""
}
function readMessagesFromStorage(sessionID: string): StoredMessageMeta[] {
const messageDir = getMessageDir(sessionID)
if (!messageDir || !existsSync(messageDir)) return []
const messages: StoredMessageMeta[] = []
for (const file of readdirSync(messageDir)) {
if (!file.endsWith(".json")) continue
try {
const content = readFileSync(join(messageDir, file), "utf-8")
messages.push(JSON.parse(content))
} catch {
continue
}
}
return messages.sort((a, b) => a.id.localeCompare(b.id))
}
function readPartsFromStorage(messageID: string): StoredPart[] {
const partDir = join(PART_STORAGE, messageID)
if (!existsSync(partDir)) return []
const parts: StoredPart[] = []
for (const file of readdirSync(partDir)) {
if (!file.endsWith(".json")) continue
try {
const content = readFileSync(join(partDir, file), "utf-8")
parts.push(JSON.parse(content))
} catch {
continue
}
}
return parts
}
function injectTextPartToStorage(sessionID: string, messageID: string, text: string): boolean {
const partDir = join(PART_STORAGE, messageID)
if (!existsSync(partDir)) {
mkdirSync(partDir, { recursive: true })
}
const partId = generatePartId()
const part: StoredPart = {
id: partId,
sessionID,
messageID,
type: "text",
text,
}
try {
writeFileSync(join(partDir, `${partId}.json`), JSON.stringify(part, null, 2))
return true
} catch {
return false
}
}
function findEmptyContentMessageFromStorage(sessionID: string): string | null {
const messages = readMessagesFromStorage(sessionID)
for (let i = 0; i < messages.length; i++) {
const msg = messages[i]
if (msg.role !== "assistant") continue
const isLastMessage = i === messages.length - 1
if (isLastMessage) continue
const parts = readPartsFromStorage(msg.id)
const hasContent = parts.some((p) => {
if (THINKING_TYPES.has(p.type)) return false
if (META_TYPES.has(p.type)) return false
if (p.type === "text" && p.text?.trim()) return true
if (p.type === "tool_use") return true
if (p.type === "tool_result") return true
return false
})
if (!hasContent && parts.length > 0) {
return msg.id
}
}
return null
}
function hasNonEmptyOutput(msg: MessageData): boolean {
const parts = msg.parts
@@ -243,65 +387,15 @@ function findEmptyContentMessage(msgs: MessageData[]): MessageData | null {
}
async function recoverEmptyContentMessage(
client: Client,
_client: Client,
sessionID: string,
failedAssistantMsg: MessageData,
directory: string
_directory: string
): Promise<boolean> {
try {
const messagesResp = await client.session.messages({
path: { id: sessionID },
query: { directory },
})
const msgs = (messagesResp as { data?: MessageData[] }).data
const emptyMessageID = findEmptyContentMessageFromStorage(sessionID) || failedAssistantMsg.info?.id
if (!emptyMessageID) return false
if (!msgs || msgs.length === 0) return false
const emptyMsg = findEmptyContentMessage(msgs) || failedAssistantMsg
const messageID = emptyMsg.info?.id
if (!messageID) return false
const existingParts = emptyMsg.parts || []
const hasOnlyThinkingOrMeta = existingParts.length > 0 && existingParts.every(
(p) => THINKING_TYPES.has(p.type) || p.type === "step-start" || p.type === "step-finish"
)
if (hasOnlyThinkingOrMeta) {
const strippedParts: MessagePart[] = [{ type: "text", text: "(interrupted)" }]
try {
// @ts-expect-error - Experimental API
await client.message?.update?.({
path: { id: messageID },
body: { parts: strippedParts },
})
return true
} catch {
// message.update not available
}
try {
// @ts-expect-error - Experimental API
await client.session.patch?.({
path: { id: sessionID },
body: { messageID, parts: strippedParts },
})
return true
} catch {
// session.patch not available
}
}
const revertTargetID = emptyMsg.info?.parentID || messageID
await client.session.revert({
path: { id: sessionID },
body: { messageID: revertTargetID },
query: { directory },
})
return true
} catch {
return false
}
return injectTextPartToStorage(sessionID, emptyMessageID, "(interrupted)")
}
async function fallbackRevertStrategy(

View File

@@ -7,18 +7,13 @@ interface Todo {
id: string
}
const CONTINUATION_PROMPT = `[SYSTEM REMINDER - TODO ENFORCEMENT]
const CONTINUATION_PROMPT = `[SYSTEM REMINDER - TODO CONTINUATION]
Your todo list is NOT complete. There are still incomplete tasks remaining.
Incomplete tasks remain in your todo list. Continue working on the next pending task.
CRITICAL INSTRUCTION:
- You MUST NOT stop working until ALL todos are marked as completed
- Continue working on the next pending task immediately
- Work honestly and diligently to finish every task
- Do NOT ask for permission to continue - just proceed with the work
- Mark each task as completed as soon as you finish it
Resume your work NOW.`
- Proceed without asking for permission
- Mark each task complete when finished
- Do not stop until all tasks are done`
function detectInterrupt(error: unknown): boolean {
if (!error) return false
@@ -113,7 +108,7 @@ export function createTodoContinuationEnforcer(ctx: PluginInput) {
parts: [
{
type: "text",
text: `${CONTINUATION_PROMPT}\n\n[Status: ${incomplete.length}/${todos.length} tasks remaining]`,
text: `${CONTINUATION_PROMPT}\n\n[Status: ${todos.length - incomplete.length}/${todos.length} completed, ${incomplete.length} remaining]`,
},
],
},

View File

@@ -1,6 +1,6 @@
import type { Plugin } from "@opencode-ai/plugin"
import { createBuiltinAgents } from "./agents"
import { createTodoContinuationEnforcer, createContextWindowMonitorHook, createSessionRecoveryHook, createCommentCheckerHooks } from "./hooks"
import { createTodoContinuationEnforcer, createContextWindowMonitorHook, createSessionRecoveryHook, createCommentCheckerHooks, createGrepOutputTruncatorHook, createPulseMonitorHook } from "./hooks"
import { updateTerminalTitle } from "./features/terminal"
import { builtinTools } from "./tools"
import { createBuiltinMcps } from "./mcp"
@@ -43,7 +43,9 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
const todoContinuationEnforcer = createTodoContinuationEnforcer(ctx)
const contextWindowMonitor = createContextWindowMonitorHook(ctx)
const sessionRecovery = createSessionRecoveryHook(ctx)
const pulseMonitor = createPulseMonitorHook(ctx)
const commentChecker = createCommentCheckerHooks()
const grepOutputTruncator = createGrepOutputTruncatorHook(ctx)
updateTerminalTitle({ sessionId: "main" })
@@ -79,6 +81,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
event: async (input) => {
await todoContinuationEnforcer(input)
await contextWindowMonitor.event(input)
await pulseMonitor.event(input)
const { event } = input
const props = event.properties as Record<string, unknown> | undefined
@@ -136,7 +139,15 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
sessionID,
error,
}
await sessionRecovery.handleSessionRecovery(messageInfo)
const recovered = await sessionRecovery.handleSessionRecovery(messageInfo)
if (recovered && sessionID && sessionID === mainSessionID) {
await ctx.client.session.prompt({
path: { id: sessionID },
body: { parts: [{ type: "text", text: "continue" }] },
query: { directory: ctx.directory },
}).catch(() => {})
}
}
if (sessionID && sessionID === mainSessionID) {
@@ -163,6 +174,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
},
"tool.execute.before": async (input, output) => {
await pulseMonitor["tool.execute.before"]()
await commentChecker["tool.execute.before"](input, output)
if (input.sessionID === mainSessionID) {
@@ -177,6 +189,8 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
},
"tool.execute.after": async (input, output) => {
await pulseMonitor["tool.execute.after"](input)
await grepOutputTruncator["tool.execute.after"](input, output)
await contextWindowMonitor["tool.execute.after"](input, output)
await commentChecker["tool.execute.after"](input, output)

View File

@@ -1,6 +1,15 @@
import { spawn } from "bun"
import { SG_CLI_PATH } from "./constants"
import type { CliMatch, CliLanguage } from "./types"
import { existsSync } from "fs"
import {
getSgCliPath,
setSgCliPath,
findSgCliPathSync,
DEFAULT_TIMEOUT_MS,
DEFAULT_MAX_OUTPUT_BYTES,
DEFAULT_MAX_MATCHES,
} from "./constants"
import { ensureAstGrepBinary } from "./downloader"
import type { CliMatch, CliLanguage, SgResult } from "./types"
export interface RunOptions {
pattern: string
@@ -12,7 +21,47 @@ export interface RunOptions {
updateAll?: boolean
}
export async function runSg(options: RunOptions): Promise<CliMatch[]> {
let resolvedCliPath: string | null = null
let initPromise: Promise<string | null> | null = null
export async function getAstGrepPath(): Promise<string | null> {
if (resolvedCliPath !== null && existsSync(resolvedCliPath)) {
return resolvedCliPath
}
if (initPromise) {
return initPromise
}
initPromise = (async () => {
const syncPath = findSgCliPathSync()
if (syncPath && existsSync(syncPath)) {
resolvedCliPath = syncPath
setSgCliPath(syncPath)
return syncPath
}
const downloadedPath = await ensureAstGrepBinary()
if (downloadedPath) {
resolvedCliPath = downloadedPath
setSgCliPath(downloadedPath)
return downloadedPath
}
return null
})()
return initPromise
}
export function startBackgroundInit(): void {
if (!initPromise) {
initPromise = getAstGrepPath()
initPromise.catch(() => {})
}
}
export async function runSg(options: RunOptions): Promise<SgResult> {
const args = ["run", "-p", options.pattern, "--lang", options.lang, "--json=compact"]
if (options.rewrite) {
@@ -35,32 +84,147 @@ export async function runSg(options: RunOptions): Promise<CliMatch[]> {
const paths = options.paths && options.paths.length > 0 ? options.paths : ["."]
args.push(...paths)
const proc = spawn([SG_CLI_PATH, ...args], {
let cliPath = getSgCliPath()
if (!existsSync(cliPath) && cliPath !== "sg") {
const downloadedPath = await getAstGrepPath()
if (downloadedPath) {
cliPath = downloadedPath
}
}
const timeout = DEFAULT_TIMEOUT_MS
const proc = spawn([cliPath, ...args], {
stdout: "pipe",
stderr: "pipe",
})
const stdout = await new Response(proc.stdout).text()
const stderr = await new Response(proc.stderr).text()
const exitCode = await proc.exited
const timeoutPromise = new Promise<never>((_, reject) => {
const id = setTimeout(() => {
proc.kill()
reject(new Error(`Search timeout after ${timeout}ms`))
}, timeout)
proc.exited.then(() => clearTimeout(id))
})
let stdout: string
let stderr: string
let exitCode: number
try {
stdout = await Promise.race([new Response(proc.stdout).text(), timeoutPromise])
stderr = await new Response(proc.stderr).text()
exitCode = await proc.exited
} catch (e) {
const error = e as Error
if (error.message?.includes("timeout")) {
return {
matches: [],
totalMatches: 0,
truncated: true,
truncatedReason: "timeout",
error: error.message,
}
}
const nodeError = e as NodeJS.ErrnoException
if (
nodeError.code === "ENOENT" ||
nodeError.message?.includes("ENOENT") ||
nodeError.message?.includes("not found")
) {
const downloadedPath = await ensureAstGrepBinary()
if (downloadedPath) {
resolvedCliPath = downloadedPath
setSgCliPath(downloadedPath)
return runSg(options)
} else {
return {
matches: [],
totalMatches: 0,
truncated: false,
error:
`ast-grep CLI binary not found.\n\n` +
`Auto-download failed. Manual install options:\n` +
` bun add -D @ast-grep/cli\n` +
` cargo install ast-grep --locked\n` +
` brew install ast-grep`,
}
}
}
return {
matches: [],
totalMatches: 0,
truncated: false,
error: `Failed to spawn ast-grep: ${error.message}`,
}
}
if (exitCode !== 0 && stdout.trim() === "") {
if (stderr.includes("No files found")) {
return []
return { matches: [], totalMatches: 0, truncated: false }
}
if (stderr.trim()) {
throw new Error(stderr.trim())
return { matches: [], totalMatches: 0, truncated: false, error: stderr.trim() }
}
return []
return { matches: [], totalMatches: 0, truncated: false }
}
if (!stdout.trim()) {
return []
return { matches: [], totalMatches: 0, truncated: false }
}
const outputTruncated = stdout.length >= DEFAULT_MAX_OUTPUT_BYTES
const outputToProcess = outputTruncated ? stdout.substring(0, DEFAULT_MAX_OUTPUT_BYTES) : stdout
let matches: CliMatch[] = []
try {
return JSON.parse(stdout) as CliMatch[]
matches = JSON.parse(outputToProcess) as CliMatch[]
} catch {
return []
if (outputTruncated) {
try {
const lastValidIndex = outputToProcess.lastIndexOf("}")
if (lastValidIndex > 0) {
const bracketIndex = outputToProcess.lastIndexOf("},", lastValidIndex)
if (bracketIndex > 0) {
const truncatedJson = outputToProcess.substring(0, bracketIndex + 1) + "]"
matches = JSON.parse(truncatedJson) as CliMatch[]
}
}
} catch {
return {
matches: [],
totalMatches: 0,
truncated: true,
truncatedReason: "max_output_bytes",
error: "Output too large and could not be parsed",
}
}
} else {
return { matches: [], totalMatches: 0, truncated: false }
}
}
const totalMatches = matches.length
const matchesTruncated = totalMatches > DEFAULT_MAX_MATCHES
const finalMatches = matchesTruncated ? matches.slice(0, DEFAULT_MAX_MATCHES) : matches
return {
matches: finalMatches,
totalMatches,
truncated: outputTruncated || matchesTruncated,
truncatedReason: outputTruncated ? "max_output_bytes" : matchesTruncated ? "max_matches" : undefined,
}
}
export function isCliAvailable(): boolean {
const path = findSgCliPathSync()
return path !== null && existsSync(path)
}
export async function ensureCliAvailable(): Promise<boolean> {
const path = await getAstGrepPath()
return path !== null && existsSync(path)
}

View File

@@ -1,9 +1,18 @@
import { createRequire } from "module"
import { dirname, join } from "path"
import { existsSync } from "fs"
import { existsSync, statSync } from "fs"
import { getCachedBinaryPath } from "./downloader"
type Platform = "darwin" | "linux" | "win32" | "unsupported"
function isValidBinary(filePath: string): boolean {
try {
return statSync(filePath).size > 10000
} catch {
return false
}
}
function getPlatformPackageName(): string | null {
const platform = process.platform as Platform
const arch = process.arch
@@ -21,32 +30,37 @@ function getPlatformPackageName(): string | null {
return platformMap[`${platform}-${arch}`] ?? null
}
function findSgCliPath(): string {
// 1. Try to find from @ast-grep/cli package (installed via npm)
export function findSgCliPathSync(): string | null {
const binaryName = process.platform === "win32" ? "sg.exe" : "sg"
const cachedPath = getCachedBinaryPath()
if (cachedPath && isValidBinary(cachedPath)) {
return cachedPath
}
try {
const require = createRequire(import.meta.url)
const cliPkgPath = require.resolve("@ast-grep/cli/package.json")
const cliDir = dirname(cliPkgPath)
const sgPath = join(cliDir, process.platform === "win32" ? "sg.exe" : "sg")
const sgPath = join(cliDir, binaryName)
if (existsSync(sgPath)) {
if (existsSync(sgPath) && isValidBinary(sgPath)) {
return sgPath
}
} catch {
// @ast-grep/cli not installed, try platform-specific package
// @ast-grep/cli not installed
}
// 2. Try platform-specific package directly
const platformPkg = getPlatformPackageName()
if (platformPkg) {
try {
const require = createRequire(import.meta.url)
const pkgPath = require.resolve(`${platformPkg}/package.json`)
const pkgDir = dirname(pkgPath)
const binaryName = process.platform === "win32" ? "ast-grep.exe" : "ast-grep"
const binaryPath = join(pkgDir, binaryName)
const astGrepName = process.platform === "win32" ? "ast-grep.exe" : "ast-grep"
const binaryPath = join(pkgDir, astGrepName)
if (existsSync(binaryPath)) {
if (existsSync(binaryPath) && isValidBinary(binaryPath)) {
return binaryPath
}
} catch {
@@ -54,12 +68,39 @@ function findSgCliPath(): string {
}
}
// 3. Fallback to system PATH
if (process.platform === "darwin") {
const homebrewPaths = ["/opt/homebrew/bin/sg", "/usr/local/bin/sg"]
for (const path of homebrewPaths) {
if (existsSync(path) && isValidBinary(path)) {
return path
}
}
}
return null
}
let resolvedCliPath: string | null = null
export function getSgCliPath(): string {
if (resolvedCliPath !== null) {
return resolvedCliPath
}
const syncPath = findSgCliPathSync()
if (syncPath) {
resolvedCliPath = syncPath
return syncPath
}
return "sg"
}
// ast-grep CLI path (auto-detected from node_modules or system PATH)
export const SG_CLI_PATH = findSgCliPath()
export function setSgCliPath(path: string): void {
resolvedCliPath = path
}
export const SG_CLI_PATH = getSgCliPath()
// CLI supported languages (25 total)
export const CLI_LANGUAGES = [
@@ -94,6 +135,10 @@ export const CLI_LANGUAGES = [
export const NAPI_LANGUAGES = ["html", "javascript", "tsx", "css", "typescript"] as const
// Language to file extensions mapping
export const DEFAULT_TIMEOUT_MS = 300_000
export const DEFAULT_MAX_OUTPUT_BYTES = 1 * 1024 * 1024
export const DEFAULT_MAX_MATCHES = 500
export const LANG_EXTENSIONS: Record<string, string[]> = {
bash: [".bash", ".sh", ".zsh", ".bats"],
c: [".c", ".h"],
@@ -121,3 +166,99 @@ export const LANG_EXTENSIONS: Record<string, string[]> = {
tsx: [".tsx"],
yaml: [".yml", ".yaml"],
}
export interface EnvironmentCheckResult {
cli: {
available: boolean
path: string
error?: string
}
napi: {
available: boolean
error?: string
}
}
/**
* Check if ast-grep CLI and NAPI are available.
* Call this at startup to provide early feedback about missing dependencies.
*/
export function checkEnvironment(): EnvironmentCheckResult {
const result: EnvironmentCheckResult = {
cli: {
available: false,
path: SG_CLI_PATH,
},
napi: {
available: false,
},
}
// Check CLI availability
if (existsSync(SG_CLI_PATH)) {
result.cli.available = true
} else if (SG_CLI_PATH === "sg") {
// Fallback path - try which/where to find in PATH
try {
const { spawnSync } = require("child_process")
const whichResult = spawnSync(process.platform === "win32" ? "where" : "which", ["sg"], {
encoding: "utf-8",
timeout: 5000,
})
result.cli.available = whichResult.status === 0 && !!whichResult.stdout?.trim()
if (!result.cli.available) {
result.cli.error = "sg binary not found in PATH"
}
} catch {
result.cli.error = "Failed to check sg availability"
}
} else {
result.cli.error = `Binary not found: ${SG_CLI_PATH}`
}
// Check NAPI availability
try {
require("@ast-grep/napi")
result.napi.available = true
} catch (e) {
result.napi.available = false
result.napi.error = `@ast-grep/napi not installed: ${e instanceof Error ? e.message : String(e)}`
}
return result
}
/**
* Format environment check result as user-friendly message.
*/
export function formatEnvironmentCheck(result: EnvironmentCheckResult): string {
const lines: string[] = ["ast-grep Environment Status:", ""]
// CLI status
if (result.cli.available) {
lines.push(`✓ CLI: Available (${result.cli.path})`)
} else {
lines.push(`✗ CLI: Not available`)
if (result.cli.error) {
lines.push(` Error: ${result.cli.error}`)
}
lines.push(` Install: bun add -D @ast-grep/cli`)
}
// NAPI status
if (result.napi.available) {
lines.push(`✓ NAPI: Available`)
} else {
lines.push(`✗ NAPI: Not available`)
if (result.napi.error) {
lines.push(` Error: ${result.napi.error}`)
}
lines.push(` Install: bun add -D @ast-grep/napi`)
}
lines.push("")
lines.push(`CLI supports ${CLI_LANGUAGES.length} languages`)
lines.push(`NAPI supports ${NAPI_LANGUAGES.length} languages: ${NAPI_LANGUAGES.join(", ")}`)
return lines.join("\n")
}

View File

@@ -0,0 +1,151 @@
import { spawn } from "bun"
import { existsSync, mkdirSync, chmodSync, unlinkSync } from "fs"
import { join } from "path"
import { homedir } from "os"
import { createRequire } from "module"
const REPO = "ast-grep/ast-grep"
// IMPORTANT: Update this when bumping @ast-grep/cli in package.json
// This is only used as fallback when @ast-grep/cli package.json cannot be read
const DEFAULT_VERSION = "0.40.0"
function getAstGrepVersion(): string {
try {
const require = createRequire(import.meta.url)
const pkg = require("@ast-grep/cli/package.json")
return pkg.version
} catch {
return DEFAULT_VERSION
}
}
interface PlatformInfo {
arch: string
os: string
}
const PLATFORM_MAP: Record<string, PlatformInfo> = {
"darwin-arm64": { arch: "aarch64", os: "apple-darwin" },
"darwin-x64": { arch: "x86_64", os: "apple-darwin" },
"linux-arm64": { arch: "aarch64", os: "unknown-linux-gnu" },
"linux-x64": { arch: "x86_64", os: "unknown-linux-gnu" },
"win32-x64": { arch: "x86_64", os: "pc-windows-msvc" },
"win32-arm64": { arch: "aarch64", os: "pc-windows-msvc" },
"win32-ia32": { arch: "i686", os: "pc-windows-msvc" },
}
export function getCacheDir(): string {
if (process.platform === "win32") {
const localAppData = process.env.LOCALAPPDATA || process.env.APPDATA
const base = localAppData || join(homedir(), "AppData", "Local")
return join(base, "oh-my-opencode", "bin")
}
const xdgCache = process.env.XDG_CACHE_HOME
const base = xdgCache || join(homedir(), ".cache")
return join(base, "oh-my-opencode", "bin")
}
export function getBinaryName(): string {
return process.platform === "win32" ? "sg.exe" : "sg"
}
export function getCachedBinaryPath(): string | null {
const binaryPath = join(getCacheDir(), getBinaryName())
return existsSync(binaryPath) ? binaryPath : null
}
async function extractZip(archivePath: string, destDir: string): Promise<void> {
const proc =
process.platform === "win32"
? spawn(
[
"powershell",
"-command",
`Expand-Archive -Path '${archivePath}' -DestinationPath '${destDir}' -Force`,
],
{ stdout: "pipe", stderr: "pipe" }
)
: spawn(["unzip", "-o", archivePath, "-d", destDir], { stdout: "pipe", stderr: "pipe" })
const exitCode = await proc.exited
if (exitCode !== 0) {
const stderr = await new Response(proc.stderr).text()
const toolHint =
process.platform === "win32"
? "Ensure PowerShell is available on your system."
: "Please install 'unzip' (e.g., apt install unzip, brew install unzip)."
throw new Error(`zip extraction failed (exit ${exitCode}): ${stderr}\n\n${toolHint}`)
}
}
export async function downloadAstGrep(version: string = DEFAULT_VERSION): Promise<string | null> {
const platformKey = `${process.platform}-${process.arch}`
const platformInfo = PLATFORM_MAP[platformKey]
if (!platformInfo) {
console.error(`[oh-my-opencode] Unsupported platform for ast-grep: ${platformKey}`)
return null
}
const cacheDir = getCacheDir()
const binaryName = getBinaryName()
const binaryPath = join(cacheDir, binaryName)
if (existsSync(binaryPath)) {
return binaryPath
}
const { arch, os } = platformInfo
const assetName = `app-${arch}-${os}.zip`
const downloadUrl = `https://github.com/${REPO}/releases/download/${version}/${assetName}`
console.log(`[oh-my-opencode] Downloading ast-grep binary...`)
try {
if (!existsSync(cacheDir)) {
mkdirSync(cacheDir, { recursive: true })
}
const response = await fetch(downloadUrl, { redirect: "follow" })
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
}
const archivePath = join(cacheDir, assetName)
const arrayBuffer = await response.arrayBuffer()
await Bun.write(archivePath, arrayBuffer)
await extractZip(archivePath, cacheDir)
if (existsSync(archivePath)) {
unlinkSync(archivePath)
}
if (process.platform !== "win32" && existsSync(binaryPath)) {
chmodSync(binaryPath, 0o755)
}
console.log(`[oh-my-opencode] ast-grep binary ready.`)
return binaryPath
} catch (err) {
console.error(
`[oh-my-opencode] Failed to download ast-grep: ${err instanceof Error ? err.message : err}`
)
return null
}
}
export async function ensureAstGrepBinary(): Promise<string | null> {
const cachedPath = getCachedBinaryPath()
if (cachedPath) {
return cachedPath
}
const version = getAstGrepVersion()
return downloadAstGrep(version)
}

View File

@@ -1,14 +1,12 @@
import {
ast_grep_search,
ast_grep_replace,
} from "./tools"
import { ast_grep_search, ast_grep_replace } from "./tools"
export const builtinTools = {
ast_grep_search,
ast_grep_replace,
}
export {
ast_grep_search,
ast_grep_replace,
}
export { ast_grep_search, ast_grep_replace }
export { ensureAstGrepBinary, getCachedBinaryPath, getCacheDir } from "./downloader"
export { getAstGrepPath, isCliAvailable, ensureCliAvailable, startBackgroundInit } from "./cli"
export { checkEnvironment, formatEnvironmentCheck } from "./constants"
export type { EnvironmentCheckResult } from "./constants"

View File

@@ -1,4 +1,5 @@
import { parse, Lang } from "@ast-grep/napi"
import { NAPI_LANGUAGES } from "./constants"
import type { NapiLanguage, AnalyzeResult, MetaVariable, Range } from "./types"
const LANG_MAP: Record<NapiLanguage, Lang> = {
@@ -10,7 +11,16 @@ const LANG_MAP: Record<NapiLanguage, Lang> = {
}
export function parseCode(code: string, lang: NapiLanguage) {
return parse(LANG_MAP[lang], code)
const parseLang = LANG_MAP[lang]
if (!parseLang) {
const supportedLangs = NAPI_LANGUAGES.join(", ")
throw new Error(
`Unsupported language for NAPI: "${lang}"\n` +
`Supported languages: ${supportedLangs}\n\n` +
`Use ast_grep_search for other languages (25 supported via CLI).`
)
}
return parse(parseLang, code)
}
export function findPattern(root: ReturnType<typeof parseCode>, pattern: string) {

View File

@@ -10,44 +10,27 @@ function showOutputToUser(context: unknown, output: string): void {
ctx.metadata?.({ metadata: { output } })
}
/**
* JS/TS languages that require complete function declaration patterns
*/
const JS_TS_LANGUAGES = ["javascript", "typescript", "tsx"] as const
/**
* Validates AST pattern for common incomplete patterns that will fail silently.
* Only validates JS/TS languages where function declarations require body.
*
* @throws Error with helpful message if pattern is incomplete
*/
function validatePatternForCli(pattern: string, lang: CliLanguage): void {
if (!JS_TS_LANGUAGES.includes(lang as (typeof JS_TS_LANGUAGES)[number])) {
return
}
function getEmptyResultHint(pattern: string, lang: CliLanguage): string | null {
const src = pattern.trim()
// Detect incomplete function declarations:
// - "function $NAME" (no params/body)
// - "export function $NAME" (no params/body)
// - "export async function $NAME" (no params/body)
// - "export default function $NAME" (no params/body)
// Pattern: ends with $METAVAR (uppercase, underscore, digits) without ( or {
const incompleteFunctionDecl =
/^(export\s+)?(default\s+)?(async\s+)?function\s+\$[A-Z_][A-Z0-9_]*\s*$/i.test(src)
if (incompleteFunctionDecl) {
throw new Error(
`Incomplete AST pattern for ${lang}: "${pattern}"\n\n` +
`ast-grep requires complete AST nodes. Function declarations must include parameters and body.\n\n` +
`Examples of correct patterns:\n` +
` - "export async function $NAME($$$) { $$$ }" (matches export async functions)\n` +
` - "function $NAME($$$) { $$$ }" (matches all function declarations)\n` +
` - "async function $NAME($$$) { $$$ }" (matches async functions)\n\n` +
`Your pattern "${pattern}" is missing the parameter list and body.`
)
if (lang === "python") {
if (src.startsWith("class ") && src.endsWith(":")) {
const withoutColon = src.slice(0, -1)
return `💡 Hint: Remove trailing colon. Try: "${withoutColon}"`
}
if ((src.startsWith("def ") || src.startsWith("async def ")) && src.endsWith(":")) {
const withoutColon = src.slice(0, -1)
return `💡 Hint: Remove trailing colon. Try: "${withoutColon}"`
}
}
if (["javascript", "typescript", "tsx"].includes(lang)) {
if (/^(export\s+)?(async\s+)?function\s+\$[A-Z_]+\s*$/i.test(src)) {
return `💡 Hint: Function patterns need params and body. Try "function $NAME($$$) { $$$ }"`
}
}
return null
}
export const ast_grep_search = tool({
@@ -66,16 +49,23 @@ export const ast_grep_search = tool({
},
execute: async (args, context) => {
try {
validatePatternForCli(args.pattern, args.lang as CliLanguage)
const matches = await runSg({
const result = await runSg({
pattern: args.pattern,
lang: args.lang as CliLanguage,
paths: args.paths,
globs: args.globs,
context: args.context,
})
const output = formatSearchResult(matches)
let output = formatSearchResult(result)
if (result.matches.length === 0 && !result.error) {
const hint = getEmptyResultHint(args.pattern, args.lang as CliLanguage)
if (hint) {
output += `\n\n${hint}`
}
}
showOutputToUser(context, output)
return output
} catch (e) {
@@ -101,7 +91,7 @@ export const ast_grep_replace = tool({
},
execute: async (args, context) => {
try {
const matches = await runSg({
const result = await runSg({
pattern: args.pattern,
rewrite: args.rewrite,
lang: args.lang as CliLanguage,
@@ -109,7 +99,7 @@ export const ast_grep_replace = tool({
globs: args.globs,
updateAll: args.dryRun === false,
})
const output = formatReplaceResult(matches, args.dryRun !== false)
const output = formatReplaceResult(result, args.dryRun !== false)
showOutputToUser(context, output)
return output
} catch (e) {

View File

@@ -51,3 +51,11 @@ export interface TransformResult {
transformed: string
editCount: number
}
export interface SgResult {
matches: CliMatch[]
totalMatches: number
truncated: boolean
truncatedReason?: "max_matches" | "max_output_bytes" | "timeout"
error?: string
}

View File

@@ -1,13 +1,28 @@
import type { CliMatch, AnalyzeResult } from "./types"
import type { CliMatch, AnalyzeResult, SgResult } from "./types"
export function formatSearchResult(matches: CliMatch[]): string {
if (matches.length === 0) {
export function formatSearchResult(result: SgResult): string {
if (result.error) {
return `Error: ${result.error}`
}
if (result.matches.length === 0) {
return "No matches found"
}
const lines: string[] = [`Found ${matches.length} match(es):\n`]
const lines: string[] = []
for (const match of matches) {
if (result.truncated) {
const reason = result.truncatedReason === "max_matches"
? `showing first ${result.matches.length} of ${result.totalMatches}`
: result.truncatedReason === "max_output_bytes"
? "output exceeded 1MB limit"
: "search timed out"
lines.push(`⚠️ Results truncated (${reason})\n`)
}
lines.push(`Found ${result.matches.length} match(es)${result.truncated ? ` (truncated from ${result.totalMatches})` : ""}:\n`)
for (const match of result.matches) {
const loc = `${match.file}:${match.range.start.line + 1}:${match.range.start.column + 1}`
lines.push(`${loc}`)
lines.push(` ${match.lines.trim()}`)
@@ -17,15 +32,30 @@ export function formatSearchResult(matches: CliMatch[]): string {
return lines.join("\n")
}
export function formatReplaceResult(matches: CliMatch[], isDryRun: boolean): string {
if (matches.length === 0) {
export function formatReplaceResult(result: SgResult, isDryRun: boolean): string {
if (result.error) {
return `Error: ${result.error}`
}
if (result.matches.length === 0) {
return "No matches found to replace"
}
const prefix = isDryRun ? "[DRY RUN] " : ""
const lines: string[] = [`${prefix}${matches.length} replacement(s):\n`]
const lines: string[] = []
for (const match of matches) {
if (result.truncated) {
const reason = result.truncatedReason === "max_matches"
? `showing first ${result.matches.length} of ${result.totalMatches}`
: result.truncatedReason === "max_output_bytes"
? "output exceeded 1MB limit"
: "search timed out"
lines.push(`⚠️ Results truncated (${reason})\n`)
}
lines.push(`${prefix}${result.matches.length} replacement(s):\n`)
for (const match of result.matches) {
const loc = `${match.file}:${match.range.start.line + 1}:${match.range.start.column + 1}`
lines.push(`${loc}`)
lines.push(` ${match.text}`)

View File

@@ -3,6 +3,7 @@ import { readFileSync } from "fs"
import { extname, resolve } from "path"
import type { ResolvedServer } from "./config"
import { getLanguageId } from "./config"
import type { Diagnostic } from "./types"
interface ManagedClient {
client: LSPClient
@@ -155,6 +156,7 @@ export class LSPClient {
private openedFiles = new Set<string>()
private stderrBuffer: string[] = []
private processExited = false
private diagnosticsStore = new Map<string, Diagnostic[]>()
constructor(
private root: string,
@@ -290,7 +292,11 @@ export class LSPClient {
try {
const msg = JSON.parse(content)
if ("id" in msg && "method" in msg) {
if ("method" in msg && !("id" in msg)) {
if (msg.method === "textDocument/publishDiagnostics" && msg.params?.uri) {
this.diagnosticsStore.set(msg.params.uri, msg.params.diagnostics ?? [])
}
} else if ("id" in msg && "method" in msg) {
this.handleServerRequest(msg.id, msg.method, msg.params)
} else if ("id" in msg && this.pending.has(msg.id)) {
const handler = this.pending.get(msg.id)!
@@ -347,9 +353,14 @@ export class LSPClient {
this.proc.stdin.write(`Content-Length: ${Buffer.byteLength(msg)}\r\n\r\n${msg}`)
}
private handleServerRequest(id: number | string, method: string, _params?: unknown): void {
private handleServerRequest(id: number | string, method: string, params?: unknown): void {
if (method === "workspace/configuration") {
this.respond(id, [{}])
const items = (params as { items?: Array<{ section?: string }> })?.items ?? []
const result = items.map((item) => {
if (item.section === "json") return { validate: { enable: true } }
return {}
})
this.respond(id, result)
} else if (method === "client/registerCapability") {
this.respond(id, null)
} else if (method === "window/workDoneProgress/create") {
@@ -412,7 +423,9 @@ export class LSPClient {
...this.server.initialization,
})
this.notify("initialized")
this.notify("workspace/didChangeConfiguration", { settings: {} })
this.notify("workspace/didChangeConfiguration", {
settings: { json: { validate: { enable: true } } },
})
await new Promise((r) => setTimeout(r, 300))
}
@@ -477,13 +490,23 @@ export class LSPClient {
return this.send("workspace/symbol", { query })
}
async diagnostics(filePath: string): Promise<unknown> {
async diagnostics(filePath: string): Promise<{ items: Diagnostic[] }> {
const absPath = resolve(filePath)
const uri = `file://${absPath}`
await this.openFile(absPath)
await new Promise((r) => setTimeout(r, 500))
return this.send("textDocument/diagnostic", {
textDocument: { uri: `file://${absPath}` },
})
try {
const result = await this.send("textDocument/diagnostic", {
textDocument: { uri },
})
if (result && typeof result === "object" && "items" in result) {
return result as { items: Diagnostic[] }
}
} catch {
}
return { items: this.diagnosticsStore.get(uri) ?? [] }
}
async prepareRename(filePath: string, line: number, character: number): Promise<unknown> {
@@ -545,5 +568,6 @@ export class LSPClient {
this.proc?.kill()
this.proc = null
this.processExited = true
this.diagnosticsStore.clear()
}
}

View File

@@ -36,6 +36,10 @@ export const SEVERITY_MAP: Record<number, string> = {
4: "hint",
}
export const DEFAULT_MAX_REFERENCES = 200
export const DEFAULT_MAX_SYMBOLS = 200
export const DEFAULT_MAX_DIAGNOSTICS = 200
export const BUILTIN_SERVERS: Record<string, Omit<LSPServerConfig, "id">> = {
typescript: {
command: ["typescript-language-server", "--stdio"],
@@ -171,4 +175,18 @@ export const EXT_TO_LANG: Record<string, string> = {
".astro": "astro",
".yaml": "yaml",
".yml": "yaml",
".json": "json",
".jsonc": "jsonc",
".html": "html",
".htm": "html",
".css": "css",
".scss": "scss",
".less": "less",
".sh": "shellscript",
".bash": "shellscript",
".zsh": "shellscript",
".fish": "fish",
".md": "markdown",
".tf": "terraform",
".tfvars": "terraform",
}

View File

@@ -1,5 +1,10 @@
import { tool } from "@opencode-ai/plugin/tool"
import { getAllServers } from "./config"
import {
DEFAULT_MAX_REFERENCES,
DEFAULT_MAX_SYMBOLS,
DEFAULT_MAX_DIAGNOSTICS,
} from "./constants"
import {
withLspClient,
formatHoverResult,
@@ -112,7 +117,14 @@ export const lsp_find_references = tool({
return output
}
const output = result.map(formatLocation).join("\n")
const total = result.length
const truncated = total > DEFAULT_MAX_REFERENCES
const limited = truncated ? result.slice(0, DEFAULT_MAX_REFERENCES) : result
const lines = limited.map(formatLocation)
if (truncated) {
lines.unshift(`Found ${total} references (showing first ${DEFAULT_MAX_REFERENCES}):`)
}
const output = lines.join("\n")
return output
} catch (e) {
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
@@ -138,13 +150,21 @@ export const lsp_document_symbols = tool({
return output
}
let output: string
if ("range" in result[0]) {
output = (result as DocumentSymbol[]).map((s) => formatDocumentSymbol(s)).join("\n")
} else {
output = (result as SymbolInfo[]).map(formatSymbolInfo).join("\n")
const total = result.length
const truncated = total > DEFAULT_MAX_SYMBOLS
const limited = truncated ? result.slice(0, DEFAULT_MAX_SYMBOLS) : result
const lines: string[] = []
if (truncated) {
lines.push(`Found ${total} symbols (showing first ${DEFAULT_MAX_SYMBOLS}):`)
}
return output
if ("range" in limited[0]) {
lines.push(...(limited as DocumentSymbol[]).map((s) => formatDocumentSymbol(s)))
} else {
lines.push(...(limited as SymbolInfo[]).map(formatSymbolInfo))
}
return lines.join("\n")
} catch (e) {
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
return output
@@ -171,8 +191,15 @@ export const lsp_workspace_symbols = tool({
return output
}
const limited = args.limit ? result.slice(0, args.limit) : result
const output = limited.map(formatSymbolInfo).join("\n")
const total = result.length
const limit = Math.min(args.limit ?? DEFAULT_MAX_SYMBOLS, DEFAULT_MAX_SYMBOLS)
const truncated = total > limit
const limited = result.slice(0, limit)
const lines = limited.map(formatSymbolInfo)
if (truncated) {
lines.unshift(`Found ${total} symbols (showing first ${limit}):`)
}
const output = lines.join("\n")
return output
} catch (e) {
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
@@ -213,7 +240,14 @@ export const lsp_diagnostics = tool({
return output
}
const output = diagnostics.map(formatDiagnostic).join("\n")
const total = diagnostics.length
const truncated = total > DEFAULT_MAX_DIAGNOSTICS
const limited = truncated ? diagnostics.slice(0, DEFAULT_MAX_DIAGNOSTICS) : diagnostics
const lines = limited.map(formatDiagnostic)
if (truncated) {
lines.unshift(`Found ${total} diagnostics (showing first ${DEFAULT_MAX_DIAGNOSTICS}):`)
}
const output = lines.join("\n")
return output
} catch (e) {
const output = `Error: ${e instanceof Error ? e.message : String(e)}`

View File

@@ -12,6 +12,7 @@ import type {
Diagnostic,
PrepareRenameResult,
PrepareRenameDefaultBehavior,
Range,
WorkspaceEdit,
TextEdit,
CodeAction,
@@ -165,21 +166,35 @@ export function filterDiagnosticsBySeverity(
}
export function formatPrepareRenameResult(
result: PrepareRenameResult | PrepareRenameDefaultBehavior | null
result: PrepareRenameResult | PrepareRenameDefaultBehavior | Range | null
): string {
if (!result) return "Cannot rename at this position"
// Case 1: { defaultBehavior: boolean }
if ("defaultBehavior" in result) {
return result.defaultBehavior ? "Rename supported (using default behavior)" : "Cannot rename at this position"
}
const startLine = result.range.start.line + 1
const startChar = result.range.start.character
const endLine = result.range.end.line + 1
const endChar = result.range.end.character
const placeholder = result.placeholder ? ` (current: "${result.placeholder}")` : ""
// Case 2: { range: Range, placeholder?: string }
if ("range" in result && result.range) {
const startLine = result.range.start.line + 1
const startChar = result.range.start.character
const endLine = result.range.end.line + 1
const endChar = result.range.end.character
const placeholder = result.placeholder ? ` (current: "${result.placeholder}")` : ""
return `Rename available at ${startLine}:${startChar}-${endLine}:${endChar}${placeholder}`
}
return `Rename available at ${startLine}:${startChar}-${endLine}:${endChar}${placeholder}`
// Case 3: Range directly (has start/end but no range property)
if ("start" in result && "end" in result) {
const startLine = result.start.line + 1
const startChar = result.start.character
const endLine = result.end.line + 1
const endChar = result.end.character
return `Rename available at ${startLine}:${startChar}-${endLine}:${endChar}`
}
return "Cannot rename at this position"
}
export function formatTextEdit(edit: TextEdit): string {