Compare commits

...

10 Commits

Author SHA1 Message Date
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
github-actions[bot]
9bed2dd79a release: v0.1.16 2025-12-05 06:55:27 +00:00
YeonGyu-Kim
4c1d534093 docs: simplify agent configuration, add OpenCode doc links 2025-12-05 15:52:04 +09:00
13 changed files with 613 additions and 301 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,112 +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 설정
각 에이전트의 모델, 프롬프트, 권한 등을 세밀하게 조정할 수 있어.
**설정 옵션:**
| 옵션 | 설명 |
|------|------|
| `model` | 사용할 모델 ID (예: `anthropic/claude-sonnet-4`) |
| `temperature` | 창의성 조절 (0.0 ~ 2.0) |
| `top_p` | 단어 선택 다양성 (0.0 ~ 1.0) |
| `prompt` | 시스템 프롬프트 오버라이드 |
| `tools` | 특정 도구 활성화/비활성화 (`{"tool_name": false}`) |
| `disable` | 에이전트 비활성화 (`true`/`false`) |
| `description` | 에이전트 설명 수정 |
| `mode` | 에이전트 모드 (`subagent`, `primary`, `all`) |
| `color` | 터미널 출력 색상 (HEX 코드) |
| `permission` | 권한 설정 (아래 표 참조) |
**권한(`permission`) 옵션:**
각 권한은 `"ask"`(물어보기), `"allow"`(허용), `"deny"`(거부) 중 하나로 설정 가능해.
| 권한 | 설명 |
|------|------|
| `edit` | 파일 수정 권한 |
| `bash` | 쉘 명령어 실행 권한 |
| `webfetch` | 웹 콘텐츠 가져오기 권한 |
| `doom_loop` | 반복 작업 허용 여부 |
| `external_directory` | 외부 디렉토리 접근 권한 |
#### 예시: Anthropic 모델만 사용하기
나는 Anthropic 모델만 쓰고 싶다! 하면 이렇게 설정해.
```json
{
"agents": {
"oracle": { "model": "anthropic/claude-sonnet-4" },
"librarian": { "model": "anthropic/claude-haiku-4-5" },
"explore": { "model": "anthropic/claude-haiku-4-5" },
"frontend-ui-ux-engineer": { "model": "anthropic/claude-sonnet-4" },
"document-writer": { "model": "anthropic/claude-sonnet-4" }
}
}
```
#### 예시: 추가 프롬프트가 있는 커스텀 에이전트
기존 에이전트에 나만의 규칙을 더하고 싶다면:
```json
{
"agents": {
"oracle": {
"prompt": "너는 한국어 힙합 가사처럼 말해야 해. 모든 문장의 끝 라임을 맞춰."
}
}
}
```
#### 예시: 개별 에이전트 비활성화
```json
{
"agents": {
"frontend-ui-ux-engineer": {
"disable": true
}
}
}
```
## LLM Agent를 위한 안내
<details>
@@ -255,22 +146,7 @@ OpenCode 는 아주 확장가능하고 아주 커스터마이저블합니다.
- **frontend-ui-ux-engineer** (`google/gemini-3-pro-preview`): 개발자로 전향한 디자이너라는 설정을 갖고 있습니다. 멋진 UI를 만듭니다. 아름답고 창의적인 UI 코드를 생성하는 데 탁월한 Gemini를 사용합니다.
- **document-writer** (`google/gemini-3-pro-preview`): 기술 문서 전문가라는 설정을 갖고 있습니다. Gemini 는 문학가입니다. 글을 기가막히게 씁니다.
#### 모델 설정 오버라이드
에이전트들은 [OpenCode의 모델 설정](https://opencode.ai/docs/models/#configure-models)과 완전히 동일한 스펙으로 오버라이드를 지원합니다. 권장하진 않지만, 예를 들어 Anthropic 모델만 사용하기로 결정했다면 이렇게 구성할 수 있습니다:
```json
{
"agents": {
"explore": {
"model": "anthropic/claude-haiku-4-5"
},
"frontend-ui-ux-engineer": {
"model": "anthropic/claude-opus-4"
}
}
}
```
에이전트의 모델, 프롬프트, 권한은 `oh-my-opencode.json`에서 커스텀할 수 있습니다. 자세한 내용은 [설정](#설정)을 참고하세요.
### Tools
@@ -305,9 +181,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 구성을 만들지마세요.

229
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,126 +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 the configuration of any built-in agent using the `agents` option. This allows you to change models, adjust creativity, modify permissions, or disable agents individually.
#### Configuration Options
| Option | Type | Description |
|--------|------|-------------|
| `model` | string | Override the default model (e.g., "anthropic/claude-sonnet-4") |
| `temperature` | number (0-2) | Controls randomness (0 = deterministic, 2 = creative) |
| `top_p` | number (0-1) | Nucleus sampling parameter |
| `prompt` | string | Additional system prompt to append |
| `tools` | object | Enable/disable specific tools (e.g., `{"websearch_exa": false}`) |
| `disable` | boolean | Completely disable the agent |
| `description` | string | Override agent description |
| `mode` | "subagent" | "primary" | "all" | When agent is available |
| `color` | string | Hex color code for terminal output (e.g., "#FF0000") |
| `permission` | object | Permission settings for sensitive operations |
#### Permission Options
| Option | Values | Description |
|--------|--------|-------------|
| `edit` | "ask" | "allow" | "deny" | File modification permissions |
| `bash` | "ask" | "allow" | "deny" | object | Shell command execution permissions |
| `webfetch` | "ask" | "allow" | "deny" | Web access permissions |
| `doom_loop` | "ask" | "allow" | "deny" | Infinite loop prevention |
| `external_directory` | "ask" | "allow" | "deny" | Access outside project root |
#### Examples
**Using Only Anthropic Models**
This configuration forces all agents to use Anthropic models, suitable for users with only Anthropic API access.
```json
{
"agents": {
"oracle": {
"model": "anthropic/claude-sonnet-4"
},
"librarian": {
"model": "anthropic/claude-haiku-4-5"
},
"explore": {
"model": "anthropic/claude-haiku-4-5"
},
"frontend-ui-ux-engineer": {
"model": "anthropic/claude-sonnet-4"
},
"document-writer": {
"model": "anthropic/claude-sonnet-4"
}
}
}
```
**Custom Agent with Additional Prompt**
Inject custom instructions into an agent's system prompt.
```json
{
"agents": {
"frontend-ui-ux-engineer": {
"prompt": "ALWAYS use Tailwind CSS. NEVER use inline styles. Prefer dark mode defaults.",
"temperature": 0.8
}
}
}
```
**Disable Agents Individually**
You can also disable agents using the `disable` property within the agent config.
```json
{
"agents": {
"explore": {
"disable": true
}
}
}
```
## Why OpenCode & Why Oh My OpenCode
OpenCode is limitlessly extensible and customizable. Zero screen flicker.
@@ -265,22 +142,7 @@ 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.
#### Model Configuration Override
Agents follow the exact same model configuration spec as [OpenCode's model configuration](https://opencode.ai/docs/models/#configure-models). While not generally recommended, if you decide to use only Anthropic models, you could configure like this:
```json
{
"agents": {
"explore": {
"model": "anthropic/claude-haiku-4-5"
},
"frontend-ui-ux-engineer": {
"model": "anthropic/claude-opus-4"
}
}
}
```
Agent models, prompts, and permissions can be customized via `oh-my-opencode.json`. See [Configuration](#configuration) for details.
### Tools
@@ -317,10 +179,97 @@ Agents follow the exact same model configuration spec as [OpenCode's model confi
- **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

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

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

@@ -70,7 +70,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"
}

View File

@@ -136,7 +136,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) {

View File

@@ -1,5 +1,7 @@
import { spawn } from "bun"
import { SG_CLI_PATH } from "./constants"
import { existsSync } from "fs"
import { getSgCliPath, setSgCliPath, findSgCliPathSync } from "./constants"
import { ensureAstGrepBinary } from "./downloader"
import type { CliMatch, CliLanguage } from "./types"
export interface RunOptions {
@@ -12,6 +14,65 @@ export interface RunOptions {
updateAll?: boolean
}
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(() => {})
}
}
interface SpawnResult {
stdout: string
stderr: string
exitCode: number
}
async function spawnSg(cliPath: string, args: string[]): Promise<SpawnResult> {
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
return { stdout, stderr, exitCode }
}
export async function runSg(options: RunOptions): Promise<CliMatch[]> {
const args = ["run", "-p", options.pattern, "--lang", options.lang, "--json=compact"]
@@ -35,14 +96,45 @@ 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], {
stdout: "pipe",
stderr: "pipe",
})
let cliPath = getSgCliPath()
const stdout = await new Response(proc.stdout).text()
const stderr = await new Response(proc.stderr).text()
const exitCode = await proc.exited
if (!existsSync(cliPath) && cliPath !== "sg") {
const downloadedPath = await getAstGrepPath()
if (downloadedPath) {
cliPath = downloadedPath
}
}
let result: SpawnResult
try {
result = await spawnSg(cliPath, args)
} catch (e) {
const error = e as NodeJS.ErrnoException
if (
error.code === "ENOENT" ||
error.message?.includes("ENOENT") ||
error.message?.includes("not found")
) {
const downloadedPath = await ensureAstGrepBinary()
if (downloadedPath) {
resolvedCliPath = downloadedPath
setSgCliPath(downloadedPath)
result = await spawnSg(downloadedPath, args)
} else {
throw new 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`
)
}
} else {
throw new Error(`Failed to spawn ast-grep: ${error.message}`)
}
}
const { stdout, stderr, exitCode } = result
if (exitCode !== 0 && stdout.trim() === "") {
if (stderr.includes("No files found")) {
@@ -64,3 +156,13 @@ export async function runSg(options: RunOptions): Promise<CliMatch[]> {
return []
}
}
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,6 +1,7 @@
import { createRequire } from "module"
import { dirname, join } from "path"
import { existsSync } from "fs"
import { getCachedBinaryPath } from "./downloader"
type Platform = "darwin" | "linux" | "win32" | "unsupported"
@@ -21,30 +22,30 @@ 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"
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)) {
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)) {
return binaryPath
@@ -54,12 +55,44 @@ 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)) {
return path
}
}
}
const cachedPath = getCachedBinaryPath()
if (cachedPath) {
return cachedPath
}
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 = [
@@ -121,3 +154,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) {