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:
parent
5331a9f8c1
commit
c074da0b93
@ -27,14 +27,18 @@ import { cacheToolInput, getToolInput } from "./tool-input-cache"
|
|||||||
import { recordToolUse, recordToolResult, getTranscriptPath, recordUserMessage } from "./transcript"
|
import { recordToolUse, recordToolResult, getTranscriptPath, recordUserMessage } from "./transcript"
|
||||||
import type { PluginConfig } from "./types"
|
import type { PluginConfig } from "./types"
|
||||||
import { log, isHookDisabled } from "../../shared"
|
import { log, isHookDisabled } from "../../shared"
|
||||||
import { injectHookMessage } from "../../features/hook-message-injector"
|
|
||||||
import { detectKeywordsWithType, removeCodeBlocks } from "../keyword-detector"
|
import { detectKeywordsWithType, removeCodeBlocks } from "../keyword-detector"
|
||||||
|
import type { ContextCollector } from "../../features/context-injector"
|
||||||
|
|
||||||
const sessionFirstMessageProcessed = new Set<string>()
|
const sessionFirstMessageProcessed = new Set<string>()
|
||||||
const sessionErrorState = new Map<string, { hasError: boolean; errorMessage?: string }>()
|
const sessionErrorState = new Map<string, { hasError: boolean; errorMessage?: string }>()
|
||||||
const sessionInterruptState = new Map<string, { interrupted: boolean }>()
|
const sessionInterruptState = new Map<string, { interrupted: boolean }>()
|
||||||
|
|
||||||
export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig = {}) {
|
export function createClaudeCodeHooksHook(
|
||||||
|
ctx: PluginInput,
|
||||||
|
config: PluginConfig = {},
|
||||||
|
contextCollector?: ContextCollector
|
||||||
|
) {
|
||||||
return {
|
return {
|
||||||
"experimental.session.compacting": async (
|
"experimental.session.compacting": async (
|
||||||
input: { sessionID: string },
|
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 ?? ""}`
|
output.parts[idx].text = `${hookContent}\n\n${output.parts[idx].text ?? ""}`
|
||||||
log("UserPromptSubmit hooks prepended to first message parts directly", { sessionID: input.sessionID })
|
log("UserPromptSubmit hooks prepended to first message parts directly", { sessionID: input.sessionID })
|
||||||
}
|
}
|
||||||
} else {
|
} else if (contextCollector) {
|
||||||
const message = output.message as {
|
contextCollector.register(input.sessionID, {
|
||||||
agent?: string
|
id: "hook-context",
|
||||||
model?: { modelID?: string; providerID?: string }
|
source: "custom",
|
||||||
path?: { cwd?: string; root?: string }
|
content: hookContent,
|
||||||
tools?: Record<string, boolean>
|
priority: "high",
|
||||||
}
|
|
||||||
|
|
||||||
const success = injectHookMessage(input.sessionID, hookContent, {
|
|
||||||
agent: message.agent,
|
|
||||||
model: message.model,
|
|
||||||
path: message.path ?? { cwd: ctx.directory, root: "/" },
|
|
||||||
tools: message.tools,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
log(success ? "Hook message injected via file system" : "File injection failed", {
|
log("Hook content registered for synthetic message injection", {
|
||||||
sessionID: input.sessionID,
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/index.ts
14
src/index.ts
@ -126,10 +126,14 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
|
|||||||
? createEmptyTaskResponseDetectorHook(ctx)
|
? createEmptyTaskResponseDetectorHook(ctx)
|
||||||
: null;
|
: null;
|
||||||
const thinkMode = isHookEnabled("think-mode") ? createThinkModeHook() : null;
|
const thinkMode = isHookEnabled("think-mode") ? createThinkModeHook() : null;
|
||||||
const claudeCodeHooks = createClaudeCodeHooksHook(ctx, {
|
const claudeCodeHooks = createClaudeCodeHooksHook(
|
||||||
disabledHooks: (pluginConfig.claude_code?.hooks ?? true) ? undefined : true,
|
ctx,
|
||||||
keywordDetectorDisabled: !isHookEnabled("keyword-detector"),
|
{
|
||||||
});
|
disabledHooks: (pluginConfig.claude_code?.hooks ?? true) ? undefined : true,
|
||||||
|
keywordDetectorDisabled: !isHookEnabled("keyword-detector"),
|
||||||
|
},
|
||||||
|
contextCollector
|
||||||
|
);
|
||||||
const anthropicContextWindowLimitRecovery = isHookEnabled(
|
const anthropicContextWindowLimitRecovery = isHookEnabled(
|
||||||
"anthropic-context-window-limit-recovery"
|
"anthropic-context-window-limit-recovery"
|
||||||
)
|
)
|
||||||
@ -224,7 +228,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
|
|||||||
const backgroundNotificationHook = isHookEnabled("background-notification")
|
const backgroundNotificationHook = isHookEnabled("background-notification")
|
||||||
? createBackgroundNotificationHook(backgroundManager)
|
? createBackgroundNotificationHook(backgroundManager)
|
||||||
: null;
|
: null;
|
||||||
const backgroundTools = createBackgroundTools(backgroundManager, ctx.client);
|
const backgroundTools = createBackgroundTools();
|
||||||
|
|
||||||
const callOmoAgent = createCallOmoAgent(ctx, backgroundManager);
|
const callOmoAgent = createCallOmoAgent(ctx, backgroundManager);
|
||||||
const lookAt = createLookAt(ctx);
|
const lookAt = createLookAt(ctx);
|
||||||
|
|||||||
@ -35,25 +35,14 @@ export { createSkillTool } from "./skill"
|
|||||||
export { getTmuxPath } from "./interactive-bash/utils"
|
export { getTmuxPath } from "./interactive-bash/utils"
|
||||||
export { createSkillMcpTool } from "./skill-mcp"
|
export { createSkillMcpTool } from "./skill-mcp"
|
||||||
|
|
||||||
import {
|
import type { ToolDefinition } from "@opencode-ai/plugin"
|
||||||
createBackgroundOutput,
|
|
||||||
createBackgroundCancel,
|
|
||||||
} from "./background-task"
|
|
||||||
|
|
||||||
import type { PluginInput, ToolDefinition } from "@opencode-ai/plugin"
|
|
||||||
import type { BackgroundManager } from "../features/background-agent"
|
|
||||||
|
|
||||||
type OpencodeClient = PluginInput["client"]
|
|
||||||
|
|
||||||
export { createCallOmoAgent } from "./call-omo-agent"
|
export { createCallOmoAgent } from "./call-omo-agent"
|
||||||
export { createLookAt } from "./look-at"
|
export { createLookAt } from "./look-at"
|
||||||
export { createSisyphusTask, type SisyphusTaskToolOptions, DEFAULT_CATEGORIES, CATEGORY_PROMPT_APPENDS } from "./sisyphus-task"
|
export { createSisyphusTask, type SisyphusTaskToolOptions, DEFAULT_CATEGORIES, CATEGORY_PROMPT_APPENDS } from "./sisyphus-task"
|
||||||
|
|
||||||
export function createBackgroundTools(manager: BackgroundManager, client: OpencodeClient): Record<string, ToolDefinition> {
|
export function createBackgroundTools(): Record<string, ToolDefinition> {
|
||||||
return {
|
return {}
|
||||||
background_output: createBackgroundOutput(manager, client),
|
|
||||||
background_cancel: createBackgroundCancel(manager, client),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const builtinTools: Record<string, ToolDefinition> = {
|
export const builtinTools: Record<string, ToolDefinition> = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user