Files
oh-my-opencode/src/plugin/hooks/create-tool-guard-hooks.ts
T
YeonGyu-Kim 51dde4d43f feat(hashline): port hashline edit tool from oh-my-pi
This PR ports the hashline edit tool from oh-my-pi to oh-my-opencode as an experimental feature.

## Features
- New experimental.hashline_edit config flag
- hashline_edit tool with 4 operations: set_line, replace_lines, insert_after, replace
- Hash-based line anchors for safe concurrent editing
- Edit tool disabler for non-OpenAI providers
- Read output enhancer with LINE:HASH prefixes
- Provider state tracking module

## Technical Details
- xxHash32-based 2-char hex hashes
- Bottom-up edit application to prevent index shifting
- OpenAI provider exemption (uses native apply_patch)
- 90 tests covering all operations and edge cases
- All files under 200 LOC limit

## Files Added/Modified
- src/tools/hashline-edit/ (7 files, ~400 LOC)
- src/hooks/hashline-edit-disabler/ (4 files, ~200 LOC)
- src/hooks/hashline-read-enhancer/ (3 files, ~400 LOC)
- src/features/hashline-provider-state.ts (13 LOC)
- src/config/schema/experimental.ts (hashline_edit flag)
- src/config/schema/hooks.ts (2 new hook names)
- src/plugin/tool-registry.ts (conditional registration)
- src/plugin/chat-params.ts (provider state tracking)
- src/tools/index.ts (export)
- src/hooks/index.ts (exports)
2026-02-17 00:03:10 +09:00

113 lines
4.5 KiB
TypeScript

import type { HookName, OhMyOpenCodeConfig } from "../../config"
import type { PluginContext } from "../types"
import {
createCommentCheckerHooks,
createToolOutputTruncatorHook,
createDirectoryAgentsInjectorHook,
createDirectoryReadmeInjectorHook,
createEmptyTaskResponseDetectorHook,
createRulesInjectorHook,
createTasksTodowriteDisablerHook,
createWriteExistingFileGuardHook,
createHashlineEditDisablerHook,
createHashlineReadEnhancerHook,
} from "../../hooks"
import {
getOpenCodeVersion,
isOpenCodeVersionAtLeast,
log,
OPENCODE_NATIVE_AGENTS_INJECTION_VERSION,
} from "../../shared"
import { safeCreateHook } from "../../shared/safe-create-hook"
export type ToolGuardHooks = {
commentChecker: ReturnType<typeof createCommentCheckerHooks> | null
toolOutputTruncator: ReturnType<typeof createToolOutputTruncatorHook> | null
directoryAgentsInjector: ReturnType<typeof createDirectoryAgentsInjectorHook> | null
directoryReadmeInjector: ReturnType<typeof createDirectoryReadmeInjectorHook> | null
emptyTaskResponseDetector: ReturnType<typeof createEmptyTaskResponseDetectorHook> | null
rulesInjector: ReturnType<typeof createRulesInjectorHook> | null
tasksTodowriteDisabler: ReturnType<typeof createTasksTodowriteDisablerHook> | null
writeExistingFileGuard: ReturnType<typeof createWriteExistingFileGuardHook> | null
hashlineEditDisabler: ReturnType<typeof createHashlineEditDisablerHook> | null
hashlineReadEnhancer: ReturnType<typeof createHashlineReadEnhancerHook> | null
}
export function createToolGuardHooks(args: {
ctx: PluginContext
pluginConfig: OhMyOpenCodeConfig
isHookEnabled: (hookName: HookName) => boolean
safeHookEnabled: boolean
}): ToolGuardHooks {
const { ctx, pluginConfig, isHookEnabled, safeHookEnabled } = args
const safeHook = <T>(hookName: HookName, factory: () => T): T | null =>
safeCreateHook(hookName, factory, { enabled: safeHookEnabled })
const commentChecker = isHookEnabled("comment-checker")
? safeHook("comment-checker", () => createCommentCheckerHooks(pluginConfig.comment_checker))
: null
const toolOutputTruncator = isHookEnabled("tool-output-truncator")
? safeHook("tool-output-truncator", () =>
createToolOutputTruncatorHook(ctx, { experimental: pluginConfig.experimental }))
: null
let directoryAgentsInjector: ReturnType<typeof createDirectoryAgentsInjectorHook> | null = null
if (isHookEnabled("directory-agents-injector")) {
const currentVersion = getOpenCodeVersion()
const hasNativeSupport =
currentVersion !== null && isOpenCodeVersionAtLeast(OPENCODE_NATIVE_AGENTS_INJECTION_VERSION)
if (hasNativeSupport) {
log("directory-agents-injector auto-disabled due to native OpenCode support", {
currentVersion,
nativeVersion: OPENCODE_NATIVE_AGENTS_INJECTION_VERSION,
})
} else {
directoryAgentsInjector = safeHook("directory-agents-injector", () => createDirectoryAgentsInjectorHook(ctx))
}
}
const directoryReadmeInjector = isHookEnabled("directory-readme-injector")
? safeHook("directory-readme-injector", () => createDirectoryReadmeInjectorHook(ctx))
: null
const emptyTaskResponseDetector = isHookEnabled("empty-task-response-detector")
? safeHook("empty-task-response-detector", () => createEmptyTaskResponseDetectorHook(ctx))
: null
const rulesInjector = isHookEnabled("rules-injector")
? safeHook("rules-injector", () => createRulesInjectorHook(ctx))
: null
const tasksTodowriteDisabler = isHookEnabled("tasks-todowrite-disabler")
? safeHook("tasks-todowrite-disabler", () =>
createTasksTodowriteDisablerHook({ experimental: pluginConfig.experimental }))
: null
const writeExistingFileGuard = isHookEnabled("write-existing-file-guard")
? safeHook("write-existing-file-guard", () => createWriteExistingFileGuardHook(ctx))
: null
const hashlineEditDisabler = isHookEnabled("hashline-edit-disabler")
? safeHook("hashline-edit-disabler", () => createHashlineEditDisablerHook(pluginConfig))
: null
const hashlineReadEnhancer = isHookEnabled("hashline-read-enhancer")
? safeHook("hashline-read-enhancer", () => createHashlineReadEnhancerHook(ctx, { hashline_edit: { enabled: pluginConfig.experimental?.hashline_edit ?? false } }))
: null
return {
commentChecker,
toolOutputTruncator,
directoryAgentsInjector,
directoryReadmeInjector,
emptyTaskResponseDetector,
rulesInjector,
tasksTodowriteDisabler,
writeExistingFileGuard,
hashlineEditDisabler,
hashlineReadEnhancer,
}
}