refactor: use ContextCollector for hook injection and remove unused background tools

Split changes:
- Replace injectHookMessage with ContextCollector.register() pattern for improved hook content injection
- Remove unused background task tools infrastructure (createBackgroundOutput, createBackgroundCancel)

🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2026-01-06 17:02:16 +09:00
parent 5331a9f8c1
commit c074da0b93
3 changed files with 34 additions and 35 deletions

View File

@@ -27,14 +27,18 @@ import { cacheToolInput, getToolInput } from "./tool-input-cache"
import { recordToolUse, recordToolResult, getTranscriptPath, recordUserMessage } from "./transcript"
import type { PluginConfig } from "./types"
import { log, isHookDisabled } from "../../shared"
import { injectHookMessage } from "../../features/hook-message-injector"
import { detectKeywordsWithType, removeCodeBlocks } from "../keyword-detector"
import type { ContextCollector } from "../../features/context-injector"
const sessionFirstMessageProcessed = new Set<string>()
const sessionErrorState = new Map<string, { hasError: boolean; errorMessage?: string }>()
const sessionInterruptState = new Map<string, { interrupted: boolean }>()
export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig = {}) {
export function createClaudeCodeHooksHook(
ctx: PluginInput,
config: PluginConfig = {},
contextCollector?: ContextCollector
) {
return {
"experimental.session.compacting": async (
input: { sessionID: string },
@@ -164,24 +168,26 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
output.parts[idx].text = `${hookContent}\n\n${output.parts[idx].text ?? ""}`
log("UserPromptSubmit hooks prepended to first message parts directly", { sessionID: input.sessionID })
}
} else {
const message = output.message as {
agent?: string
model?: { modelID?: string; providerID?: string }
path?: { cwd?: string; root?: string }
tools?: Record<string, boolean>
}
const success = injectHookMessage(input.sessionID, hookContent, {
agent: message.agent,
model: message.model,
path: message.path ?? { cwd: ctx.directory, root: "/" },
tools: message.tools,
} else if (contextCollector) {
contextCollector.register(input.sessionID, {
id: "hook-context",
source: "custom",
content: hookContent,
priority: "high",
})
log(success ? "Hook message injected via file system" : "File injection failed", {
log("Hook content registered for synthetic message injection", {
sessionID: input.sessionID,
contentLength: hookContent.length,
})
} else {
const idx = output.parts.findIndex((p) => p.type === "text" && p.text)
if (idx >= 0) {
output.parts[idx].text = `${hookContent}\n\n${output.parts[idx].text ?? ""}`
log("Hook content prepended to message (fallback)", {
sessionID: input.sessionID,
})
}
}
}
}

View File

@@ -126,10 +126,14 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
? createEmptyTaskResponseDetectorHook(ctx)
: null;
const thinkMode = isHookEnabled("think-mode") ? createThinkModeHook() : null;
const claudeCodeHooks = createClaudeCodeHooksHook(ctx, {
disabledHooks: (pluginConfig.claude_code?.hooks ?? true) ? undefined : true,
keywordDetectorDisabled: !isHookEnabled("keyword-detector"),
});
const claudeCodeHooks = createClaudeCodeHooksHook(
ctx,
{
disabledHooks: (pluginConfig.claude_code?.hooks ?? true) ? undefined : true,
keywordDetectorDisabled: !isHookEnabled("keyword-detector"),
},
contextCollector
);
const anthropicContextWindowLimitRecovery = isHookEnabled(
"anthropic-context-window-limit-recovery"
)
@@ -224,7 +228,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
const backgroundNotificationHook = isHookEnabled("background-notification")
? createBackgroundNotificationHook(backgroundManager)
: null;
const backgroundTools = createBackgroundTools(backgroundManager, ctx.client);
const backgroundTools = createBackgroundTools();
const callOmoAgent = createCallOmoAgent(ctx, backgroundManager);
const lookAt = createLookAt(ctx);

View File

@@ -35,25 +35,14 @@ export { createSkillTool } from "./skill"
export { getTmuxPath } from "./interactive-bash/utils"
export { createSkillMcpTool } from "./skill-mcp"
import {
createBackgroundOutput,
createBackgroundCancel,
} from "./background-task"
import type { PluginInput, ToolDefinition } from "@opencode-ai/plugin"
import type { BackgroundManager } from "../features/background-agent"
type OpencodeClient = PluginInput["client"]
import type { ToolDefinition } from "@opencode-ai/plugin"
export { createCallOmoAgent } from "./call-omo-agent"
export { createLookAt } from "./look-at"
export { createSisyphusTask, type SisyphusTaskToolOptions, DEFAULT_CATEGORIES, CATEGORY_PROMPT_APPENDS } from "./sisyphus-task"
export function createBackgroundTools(manager: BackgroundManager, client: OpencodeClient): Record<string, ToolDefinition> {
return {
background_output: createBackgroundOutput(manager, client),
background_cancel: createBackgroundCancel(manager, client),
}
export function createBackgroundTools(): Record<string, ToolDefinition> {
return {}
}
export const builtinTools: Record<string, ToolDefinition> = {