133 lines
3.9 KiB
TypeScript
133 lines
3.9 KiB
TypeScript
import type {
|
|
UserPromptSubmitInput,
|
|
PostToolUseOutput,
|
|
ClaudeHooksConfig,
|
|
} from "./types"
|
|
import { findMatchingHooks, log } from "../../shared"
|
|
import { isRealUserTextPart } from "../../shared/internal-initiator-marker"
|
|
import { dispatchHook, getHookIdentifier } from "./dispatch-hook"
|
|
import { isHookCommandDisabled, type PluginExtendedConfig } from "./config-loader"
|
|
import { normalizeHookText } from "./hook-text"
|
|
|
|
const USER_PROMPT_SUBMIT_TAG_OPEN = "<user-prompt-submit-hook>"
|
|
const USER_PROMPT_SUBMIT_TAG_CLOSE = "</user-prompt-submit-hook>"
|
|
|
|
export interface MessagePart {
|
|
type: "text" | "tool_use" | "tool_result"
|
|
text?: string
|
|
[key: string]: unknown
|
|
}
|
|
|
|
export interface UserPromptSubmitContext {
|
|
sessionId: string
|
|
parentSessionId?: string
|
|
prompt: string
|
|
parts: MessagePart[]
|
|
cwd: string
|
|
permissionMode?: "default" | "acceptEdits" | "bypassPermissions"
|
|
}
|
|
|
|
export interface UserPromptSubmitResult {
|
|
block: boolean
|
|
reason?: string
|
|
modifiedParts: MessagePart[]
|
|
messages: string[]
|
|
}
|
|
|
|
export async function executeUserPromptSubmitHooks(
|
|
ctx: UserPromptSubmitContext,
|
|
config: ClaudeHooksConfig | null,
|
|
extendedConfig?: PluginExtendedConfig | null
|
|
): Promise<UserPromptSubmitResult> {
|
|
const modifiedParts = ctx.parts
|
|
const messages: string[] = []
|
|
|
|
if (ctx.parentSessionId) {
|
|
return { block: false, modifiedParts, messages }
|
|
}
|
|
|
|
const realUserTextParts = ctx.parts.filter(isRealUserTextPart)
|
|
if (realUserTextParts.length === 0) {
|
|
return { block: false, modifiedParts, messages }
|
|
}
|
|
|
|
// Check if hook tags are in the current user input only (not in injected context)
|
|
// by checking only the text parts that were provided in this message
|
|
const userInputText = realUserTextParts
|
|
.map((p) => p.text ?? "")
|
|
.join("\n")
|
|
|
|
if (
|
|
userInputText.includes(USER_PROMPT_SUBMIT_TAG_OPEN) &&
|
|
userInputText.includes(USER_PROMPT_SUBMIT_TAG_CLOSE)
|
|
) {
|
|
return { block: false, modifiedParts, messages }
|
|
}
|
|
|
|
if (!config) {
|
|
return { block: false, modifiedParts, messages }
|
|
}
|
|
|
|
const matchers = findMatchingHooks(config, "UserPromptSubmit")
|
|
if (matchers.length === 0) {
|
|
return { block: false, modifiedParts, messages }
|
|
}
|
|
|
|
const stdinData: UserPromptSubmitInput = {
|
|
session_id: ctx.sessionId,
|
|
cwd: ctx.cwd,
|
|
permission_mode: ctx.permissionMode ?? "bypassPermissions",
|
|
hook_event_name: "UserPromptSubmit",
|
|
prompt: ctx.prompt,
|
|
session: { id: ctx.sessionId },
|
|
hook_source: "opencode-plugin",
|
|
}
|
|
|
|
for (const matcher of matchers) {
|
|
if (!matcher.hooks || matcher.hooks.length === 0) continue
|
|
for (const hook of matcher.hooks) {
|
|
if (hook.type !== "command" && hook.type !== "http") continue
|
|
|
|
const hookName = getHookIdentifier(hook)
|
|
if (isHookCommandDisabled("UserPromptSubmit", hookName, extendedConfig ?? null)) {
|
|
log("UserPromptSubmit hook command skipped (disabled by config)", { command: hookName })
|
|
continue
|
|
}
|
|
|
|
const result = await dispatchHook(hook, JSON.stringify(stdinData), ctx.cwd)
|
|
|
|
if (result.stdout) {
|
|
const output = normalizeHookText(result.stdout)
|
|
if (output === undefined) {
|
|
continue
|
|
}
|
|
if (output.startsWith(USER_PROMPT_SUBMIT_TAG_OPEN)) {
|
|
messages.push(output)
|
|
} else {
|
|
messages.push(`${USER_PROMPT_SUBMIT_TAG_OPEN}\n${output}\n${USER_PROMPT_SUBMIT_TAG_CLOSE}`)
|
|
}
|
|
}
|
|
|
|
if (result.exitCode !== 0) {
|
|
try {
|
|
const output = JSON.parse(result.stdout || "{}") as PostToolUseOutput
|
|
if (output.decision === "block") {
|
|
return {
|
|
block: true,
|
|
reason: normalizeHookText(output.reason) ?? normalizeHookText(result.stderr),
|
|
modifiedParts,
|
|
messages,
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (!(error instanceof SyntaxError)) {
|
|
throw error
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return { block: false, modifiedParts, messages }
|
|
}
|