- Fix #1991 crash: optional chaining for task-history sessionID access - Fix #1992 think-mode: add antigravity entries to HIGH_VARIANT_MAP - Fix #1949 Copilot premium misattribution: use createInternalAgentTextPart - Fix #1982 load_skills: pass directory to discoverSkills for project-level skills - Fix command priority: sort scopePriority before .find(), project-first return - Fix Google provider transform: apply in userFallbackModels path - Fix ralph-loop TUI: optional chaining for event handler - Fix runtime-fallback: unify dual fallback engines, remove HTTP 400 from retry, fix pendingFallbackModel stuck state, add priority gate to skip model-fallback when runtime-fallback is active - Fix Prometheus task system: exempt from todowrite/todoread deny - Fix background_output: default full_session to true - Remove orphan hooks: hashline-edit-diff-enhancer (redundant with hashline_edit built-in diff), task-reminder (dead code) - Remove orphan config entries: 3 stale hook names from Zod schema - Fix disabled_hooks schema: accept arbitrary strings for forward compatibility - Register json-error-recovery hook in tool-guard pipeline - Add disabled_hooks gating for question-label-truncator, task-resume-info, claude-code-hooks - Update test expectations to match new behavior
121 lines
4.7 KiB
TypeScript
121 lines
4.7 KiB
TypeScript
import type { HookName, OhMyOpenCodeConfig } from "../../config"
|
|
import type { ModelCacheState } from "../../plugin-state"
|
|
import type { PluginContext } from "../types"
|
|
|
|
import {
|
|
createCommentCheckerHooks,
|
|
createToolOutputTruncatorHook,
|
|
createDirectoryAgentsInjectorHook,
|
|
createDirectoryReadmeInjectorHook,
|
|
createEmptyTaskResponseDetectorHook,
|
|
createRulesInjectorHook,
|
|
createTasksTodowriteDisablerHook,
|
|
createWriteExistingFileGuardHook,
|
|
createHashlineReadEnhancerHook,
|
|
createJsonErrorRecoveryHook,
|
|
} 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
|
|
hashlineReadEnhancer: ReturnType<typeof createHashlineReadEnhancerHook> | null
|
|
jsonErrorRecovery: ReturnType<typeof createJsonErrorRecoveryHook> | null
|
|
}
|
|
|
|
export function createToolGuardHooks(args: {
|
|
ctx: PluginContext
|
|
pluginConfig: OhMyOpenCodeConfig
|
|
modelCacheState: ModelCacheState
|
|
isHookEnabled: (hookName: HookName) => boolean
|
|
safeHookEnabled: boolean
|
|
}): ToolGuardHooks {
|
|
const { ctx, pluginConfig, modelCacheState, 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, {
|
|
modelCacheState,
|
|
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, modelCacheState))
|
|
}
|
|
}
|
|
|
|
const directoryReadmeInjector = isHookEnabled("directory-readme-injector")
|
|
? safeHook("directory-readme-injector", () =>
|
|
createDirectoryReadmeInjectorHook(ctx, modelCacheState))
|
|
: 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, modelCacheState))
|
|
: 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 hashlineReadEnhancer = isHookEnabled("hashline-read-enhancer")
|
|
? safeHook("hashline-read-enhancer", () => createHashlineReadEnhancerHook(ctx, { hashline_edit: { enabled: pluginConfig.hashline_edit ?? true } }))
|
|
: null
|
|
|
|
const jsonErrorRecovery = isHookEnabled("json-error-recovery")
|
|
? safeHook("json-error-recovery", () => createJsonErrorRecoveryHook(ctx))
|
|
: null
|
|
|
|
return {
|
|
commentChecker,
|
|
toolOutputTruncator,
|
|
directoryAgentsInjector,
|
|
directoryReadmeInjector,
|
|
emptyTaskResponseDetector,
|
|
rulesInjector,
|
|
tasksTodowriteDisabler,
|
|
writeExistingFileGuard,
|
|
hashlineReadEnhancer,
|
|
jsonErrorRecovery,
|
|
}
|
|
}
|