Compare commits
3 Commits
fix/docs-o
...
fix/ripgre
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
594c97d36b | ||
|
|
74008a8ceb | ||
|
|
62300791b2 |
@@ -7,9 +7,11 @@ import {
|
|||||||
DEFAULT_MAX_DEPTH,
|
DEFAULT_MAX_DEPTH,
|
||||||
DEFAULT_MAX_OUTPUT_BYTES,
|
DEFAULT_MAX_OUTPUT_BYTES,
|
||||||
RG_FILES_FLAGS,
|
RG_FILES_FLAGS,
|
||||||
|
DEFAULT_RG_THREADS,
|
||||||
} from "./constants"
|
} from "./constants"
|
||||||
import type { GlobOptions, GlobResult, FileMatch } from "./types"
|
import type { GlobOptions, GlobResult, FileMatch } from "./types"
|
||||||
import { stat } from "node:fs/promises"
|
import { stat } from "node:fs/promises"
|
||||||
|
import { rgSemaphore } from "../shared/semaphore"
|
||||||
|
|
||||||
export interface ResolvedCli {
|
export interface ResolvedCli {
|
||||||
path: string
|
path: string
|
||||||
@@ -19,6 +21,7 @@ export interface ResolvedCli {
|
|||||||
function buildRgArgs(options: GlobOptions): string[] {
|
function buildRgArgs(options: GlobOptions): string[] {
|
||||||
const args: string[] = [
|
const args: string[] = [
|
||||||
...RG_FILES_FLAGS,
|
...RG_FILES_FLAGS,
|
||||||
|
`--threads=${Math.min(options.threads ?? DEFAULT_RG_THREADS, DEFAULT_RG_THREADS)}`,
|
||||||
`--max-depth=${Math.min(options.maxDepth ?? DEFAULT_MAX_DEPTH, DEFAULT_MAX_DEPTH)}`,
|
`--max-depth=${Math.min(options.maxDepth ?? DEFAULT_MAX_DEPTH, DEFAULT_MAX_DEPTH)}`,
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -91,6 +94,18 @@ export { buildRgArgs, buildFindArgs, buildPowerShellCommand }
|
|||||||
export async function runRgFiles(
|
export async function runRgFiles(
|
||||||
options: GlobOptions,
|
options: GlobOptions,
|
||||||
resolvedCli?: ResolvedCli
|
resolvedCli?: ResolvedCli
|
||||||
|
): Promise<GlobResult> {
|
||||||
|
await rgSemaphore.acquire()
|
||||||
|
try {
|
||||||
|
return await runRgFilesInternal(options, resolvedCli)
|
||||||
|
} finally {
|
||||||
|
rgSemaphore.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runRgFilesInternal(
|
||||||
|
options: GlobOptions,
|
||||||
|
resolvedCli?: ResolvedCli
|
||||||
): Promise<GlobResult> {
|
): Promise<GlobResult> {
|
||||||
const cli = resolvedCli ?? resolveGrepCli()
|
const cli = resolvedCli ?? resolveGrepCli()
|
||||||
const timeout = Math.min(options.timeout ?? DEFAULT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS)
|
const timeout = Math.min(options.timeout ?? DEFAULT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export { resolveGrepCli, resolveGrepCliWithAutoInstall, type GrepBackend } from "../grep/constants"
|
export { resolveGrepCli, resolveGrepCliWithAutoInstall, type GrepBackend, DEFAULT_RG_THREADS } from "../grep/constants"
|
||||||
|
|
||||||
export const DEFAULT_TIMEOUT_MS = 60_000
|
export const DEFAULT_TIMEOUT_MS = 60_000
|
||||||
export const DEFAULT_LIMIT = 100
|
export const DEFAULT_LIMIT = 100
|
||||||
|
|||||||
@@ -19,4 +19,5 @@ export interface GlobOptions {
|
|||||||
maxDepth?: number
|
maxDepth?: number
|
||||||
timeout?: number
|
timeout?: number
|
||||||
limit?: number
|
limit?: number
|
||||||
|
threads?: number // limit rg thread count
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,17 @@ import {
|
|||||||
DEFAULT_MAX_COLUMNS,
|
DEFAULT_MAX_COLUMNS,
|
||||||
DEFAULT_TIMEOUT_MS,
|
DEFAULT_TIMEOUT_MS,
|
||||||
DEFAULT_MAX_OUTPUT_BYTES,
|
DEFAULT_MAX_OUTPUT_BYTES,
|
||||||
|
DEFAULT_RG_THREADS,
|
||||||
RG_SAFETY_FLAGS,
|
RG_SAFETY_FLAGS,
|
||||||
GREP_SAFETY_FLAGS,
|
GREP_SAFETY_FLAGS,
|
||||||
} from "./constants"
|
} from "./constants"
|
||||||
import type { GrepOptions, GrepMatch, GrepResult, CountResult } from "./types"
|
import type { GrepOptions, GrepMatch, GrepResult, CountResult } from "./types"
|
||||||
|
import { rgSemaphore } from "../shared/semaphore"
|
||||||
|
|
||||||
function buildRgArgs(options: GrepOptions): string[] {
|
function buildRgArgs(options: GrepOptions): string[] {
|
||||||
const args: string[] = [
|
const args: string[] = [
|
||||||
...RG_SAFETY_FLAGS,
|
...RG_SAFETY_FLAGS,
|
||||||
|
`--threads=${Math.min(options.threads ?? DEFAULT_RG_THREADS, DEFAULT_RG_THREADS)}`,
|
||||||
`--max-depth=${Math.min(options.maxDepth ?? DEFAULT_MAX_DEPTH, DEFAULT_MAX_DEPTH)}`,
|
`--max-depth=${Math.min(options.maxDepth ?? DEFAULT_MAX_DEPTH, DEFAULT_MAX_DEPTH)}`,
|
||||||
`--max-filesize=${options.maxFilesize ?? DEFAULT_MAX_FILESIZE}`,
|
`--max-filesize=${options.maxFilesize ?? DEFAULT_MAX_FILESIZE}`,
|
||||||
`--max-count=${Math.min(options.maxCount ?? DEFAULT_MAX_COUNT, DEFAULT_MAX_COUNT)}`,
|
`--max-count=${Math.min(options.maxCount ?? DEFAULT_MAX_COUNT, DEFAULT_MAX_COUNT)}`,
|
||||||
@@ -51,6 +54,12 @@ function buildRgArgs(options: GrepOptions): string[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.outputMode === "files_with_matches") {
|
||||||
|
args.push("--files-with-matches")
|
||||||
|
} else if (options.outputMode === "count") {
|
||||||
|
args.push("--count")
|
||||||
|
}
|
||||||
|
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +95,7 @@ function buildArgs(options: GrepOptions, backend: GrepBackend): string[] {
|
|||||||
return backend === "rg" ? buildRgArgs(options) : buildGrepArgs(options)
|
return backend === "rg" ? buildRgArgs(options) : buildGrepArgs(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseOutput(output: string): GrepMatch[] {
|
function parseOutput(output: string, filesOnly = false): GrepMatch[] {
|
||||||
if (!output.trim()) return []
|
if (!output.trim()) return []
|
||||||
|
|
||||||
const matches: GrepMatch[] = []
|
const matches: GrepMatch[] = []
|
||||||
@@ -95,6 +104,16 @@ function parseOutput(output: string): GrepMatch[] {
|
|||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!line.trim()) continue
|
if (!line.trim()) continue
|
||||||
|
|
||||||
|
if (filesOnly) {
|
||||||
|
// --files-with-matches outputs only file paths, one per line
|
||||||
|
matches.push({
|
||||||
|
file: line.trim(),
|
||||||
|
line: 0,
|
||||||
|
text: "",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
const match = line.match(/^(.+?):(\d+):(.*)$/)
|
const match = line.match(/^(.+?):(\d+):(.*)$/)
|
||||||
if (match) {
|
if (match) {
|
||||||
matches.push({
|
matches.push({
|
||||||
@@ -130,6 +149,15 @@ function parseCountOutput(output: string): CountResult[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function runRg(options: GrepOptions): Promise<GrepResult> {
|
export async function runRg(options: GrepOptions): Promise<GrepResult> {
|
||||||
|
await rgSemaphore.acquire()
|
||||||
|
try {
|
||||||
|
return await runRgInternal(options)
|
||||||
|
} finally {
|
||||||
|
rgSemaphore.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runRgInternal(options: GrepOptions): Promise<GrepResult> {
|
||||||
const cli = resolveGrepCli()
|
const cli = resolveGrepCli()
|
||||||
const args = buildArgs(options, cli.backend)
|
const args = buildArgs(options, cli.backend)
|
||||||
const timeout = Math.min(options.timeout ?? DEFAULT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS)
|
const timeout = Math.min(options.timeout ?? DEFAULT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS)
|
||||||
@@ -173,14 +201,17 @@ export async function runRg(options: GrepOptions): Promise<GrepResult> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const matches = parseOutput(outputToProcess)
|
const matches = parseOutput(outputToProcess, options.outputMode === "files_with_matches")
|
||||||
const filesSearched = new Set(matches.map((m) => m.file)).size
|
const limited = options.headLimit && options.headLimit > 0
|
||||||
|
? matches.slice(0, options.headLimit)
|
||||||
|
: matches
|
||||||
|
const filesSearched = new Set(limited.map((m) => m.file)).size
|
||||||
|
|
||||||
return {
|
return {
|
||||||
matches,
|
matches: limited,
|
||||||
totalMatches: matches.length,
|
totalMatches: limited.length,
|
||||||
filesSearched,
|
filesSearched,
|
||||||
truncated,
|
truncated: truncated || (options.headLimit ? matches.length > options.headLimit : false),
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return {
|
return {
|
||||||
@@ -194,6 +225,15 @@ export async function runRg(options: GrepOptions): Promise<GrepResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function runRgCount(options: Omit<GrepOptions, "context">): Promise<CountResult[]> {
|
export async function runRgCount(options: Omit<GrepOptions, "context">): Promise<CountResult[]> {
|
||||||
|
await rgSemaphore.acquire()
|
||||||
|
try {
|
||||||
|
return await runRgCountInternal(options)
|
||||||
|
} finally {
|
||||||
|
rgSemaphore.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runRgCountInternal(options: Omit<GrepOptions, "context">): Promise<CountResult[]> {
|
||||||
const cli = resolveGrepCli()
|
const cli = resolveGrepCli()
|
||||||
const args = buildArgs({ ...options, context: 0 }, cli.backend)
|
const args = buildArgs({ ...options, context: 0 }, cli.backend)
|
||||||
|
|
||||||
|
|||||||
@@ -113,8 +113,9 @@ export const DEFAULT_MAX_FILESIZE = "10M"
|
|||||||
export const DEFAULT_MAX_COUNT = 500
|
export const DEFAULT_MAX_COUNT = 500
|
||||||
export const DEFAULT_MAX_COLUMNS = 1000
|
export const DEFAULT_MAX_COLUMNS = 1000
|
||||||
export const DEFAULT_CONTEXT = 2
|
export const DEFAULT_CONTEXT = 2
|
||||||
export const DEFAULT_TIMEOUT_MS = 300_000
|
export const DEFAULT_TIMEOUT_MS = 60_000
|
||||||
export const DEFAULT_MAX_OUTPUT_BYTES = 10 * 1024 * 1024
|
export const DEFAULT_MAX_OUTPUT_BYTES = 256 * 1024
|
||||||
|
export const DEFAULT_RG_THREADS = 4
|
||||||
|
|
||||||
export const RG_SAFETY_FLAGS = [
|
export const RG_SAFETY_FLAGS = [
|
||||||
"--no-follow",
|
"--no-follow",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { existsSync, readdirSync } from "node:fs"
|
import { existsSync, readdirSync } from "node:fs"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
import { extractZip as extractZipBase } from "../../shared"
|
import { extractZip as extractZipBase } from "../../shared/zip-extractor"
|
||||||
import {
|
import {
|
||||||
cleanupArchive,
|
cleanupArchive,
|
||||||
downloadArchive,
|
downloadArchive,
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool"
|
import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool"
|
||||||
import { runRg } from "./cli"
|
import { runRg, runRgCount } from "./cli"
|
||||||
import { formatGrepResult } from "./result-formatter"
|
import { formatGrepResult, formatCountResult } from "./result-formatter"
|
||||||
|
|
||||||
export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition> {
|
export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition> {
|
||||||
const grep: ToolDefinition = tool({
|
const grep: ToolDefinition = tool({
|
||||||
description:
|
description:
|
||||||
"Fast content search tool with safety limits (60s timeout, 10MB output). " +
|
"Fast content search tool with safety limits (60s timeout, 256KB output). " +
|
||||||
"Searches file contents using regular expressions. " +
|
"Searches file contents using regular expressions. " +
|
||||||
"Supports full regex syntax (eg. \"log.*Error\", \"function\\s+\\w+\", etc.). " +
|
"Supports full regex syntax (eg. \"log.*Error\", \"function\\s+\\w+\", etc.). " +
|
||||||
"Filter files by pattern with the include parameter (eg. \"*.js\", \"*.{ts,tsx}\"). " +
|
"Filter files by pattern with the include parameter (eg. \"*.js\", \"*.{ts,tsx}\"). " +
|
||||||
"Returns file paths with matches sorted by modification time.",
|
"Output modes: \"content\" shows matching lines, \"files_with_matches\" shows only file paths (default), \"count\" shows match counts per file.",
|
||||||
args: {
|
args: {
|
||||||
pattern: tool.schema.string().describe("The regex pattern to search for in file contents"),
|
pattern: tool.schema.string().describe("The regex pattern to search for in file contents"),
|
||||||
include: tool.schema
|
include: tool.schema
|
||||||
@@ -21,18 +21,42 @@ export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition
|
|||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
.describe("The directory to search in. Defaults to the current working directory."),
|
.describe("The directory to search in. Defaults to the current working directory."),
|
||||||
|
output_mode: tool.schema
|
||||||
|
.enum(["content", "files_with_matches", "count"])
|
||||||
|
.optional()
|
||||||
|
.describe(
|
||||||
|
"Output mode: \"content\" shows matching lines, \"files_with_matches\" shows only file paths (default), \"count\" shows match counts per file."
|
||||||
|
),
|
||||||
|
head_limit: tool.schema
|
||||||
|
.number()
|
||||||
|
.optional()
|
||||||
|
.describe("Limit output to first N entries. 0 or omitted means no limit."),
|
||||||
},
|
},
|
||||||
execute: async (args) => {
|
execute: async (args) => {
|
||||||
try {
|
try {
|
||||||
const globs = args.include ? [args.include] : undefined
|
const globs = args.include ? [args.include] : undefined
|
||||||
const searchPath = args.path ?? ctx.directory
|
const searchPath = args.path ?? ctx.directory
|
||||||
const paths = [searchPath]
|
const paths = [searchPath]
|
||||||
|
const outputMode = args.output_mode ?? "files_with_matches"
|
||||||
|
const headLimit = args.head_limit ?? 0
|
||||||
|
|
||||||
|
if (outputMode === "count") {
|
||||||
|
const results = await runRgCount({
|
||||||
|
pattern: args.pattern,
|
||||||
|
paths,
|
||||||
|
globs,
|
||||||
|
})
|
||||||
|
const limited = headLimit > 0 ? results.slice(0, headLimit) : results
|
||||||
|
return formatCountResult(limited)
|
||||||
|
}
|
||||||
|
|
||||||
const result = await runRg({
|
const result = await runRg({
|
||||||
pattern: args.pattern,
|
pattern: args.pattern,
|
||||||
paths,
|
paths,
|
||||||
globs,
|
globs,
|
||||||
context: 0,
|
context: 0,
|
||||||
|
outputMode,
|
||||||
|
headLimit,
|
||||||
})
|
})
|
||||||
|
|
||||||
return formatGrepResult(result)
|
return formatGrepResult(result)
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ export interface GrepOptions {
|
|||||||
noIgnore?: boolean
|
noIgnore?: boolean
|
||||||
fileType?: string[]
|
fileType?: string[]
|
||||||
timeout?: number
|
timeout?: number
|
||||||
|
threads?: number
|
||||||
|
outputMode?: "content" | "files_with_matches" | "count"
|
||||||
|
headLimit?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CountResult {
|
export interface CountResult {
|
||||||
|
|||||||
32
src/tools/shared/semaphore.ts
Normal file
32
src/tools/shared/semaphore.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Simple counting semaphore to limit concurrent process execution.
|
||||||
|
* Used to prevent multiple ripgrep processes from saturating CPU.
|
||||||
|
*/
|
||||||
|
export class Semaphore {
|
||||||
|
private queue: (() => void)[] = []
|
||||||
|
private running = 0
|
||||||
|
|
||||||
|
constructor(private readonly max: number) {}
|
||||||
|
|
||||||
|
async acquire(): Promise<void> {
|
||||||
|
if (this.running < this.max) {
|
||||||
|
this.running++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
this.queue.push(() => {
|
||||||
|
this.running++
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
release(): void {
|
||||||
|
this.running--
|
||||||
|
const next = this.queue.shift()
|
||||||
|
if (next) next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Global semaphore limiting concurrent ripgrep processes to 2 */
|
||||||
|
export const rgSemaphore = new Semaphore(2)
|
||||||
Reference in New Issue
Block a user