fix(claude-code-hooks): handle UserPromptSubmit on first message properly
- Allow UserPromptSubmit hooks to run on first message (used for title generation) - For first message: prepend hook content directly to message parts - For subsequent messages: use file system injection as before - Preserves hook injection integrity while enabling title generation hooks 🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -112,11 +112,6 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
|
|||||||
const isFirstMessage = !sessionFirstMessageProcessed.has(input.sessionID)
|
const isFirstMessage = !sessionFirstMessageProcessed.has(input.sessionID)
|
||||||
sessionFirstMessageProcessed.add(input.sessionID)
|
sessionFirstMessageProcessed.add(input.sessionID)
|
||||||
|
|
||||||
if (isFirstMessage) {
|
|
||||||
log("Skipping UserPromptSubmit hooks on first message for title generation", { sessionID: input.sessionID })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isHookDisabled(config, "UserPromptSubmit")) {
|
if (!isHookDisabled(config, "UserPromptSubmit")) {
|
||||||
const userPromptCtx: UserPromptSubmitContext = {
|
const userPromptCtx: UserPromptSubmitContext = {
|
||||||
sessionId: input.sessionID,
|
sessionId: input.sessionID,
|
||||||
@@ -144,7 +139,15 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
|
|||||||
|
|
||||||
if (result.messages.length > 0) {
|
if (result.messages.length > 0) {
|
||||||
const hookContent = result.messages.join("\n\n")
|
const hookContent = result.messages.join("\n\n")
|
||||||
log(`[claude-code-hooks] Injecting ${result.messages.length} hook messages`, { sessionID: input.sessionID, contentLength: hookContent.length })
|
log(`[claude-code-hooks] Injecting ${result.messages.length} hook messages`, { sessionID: input.sessionID, contentLength: hookContent.length, isFirstMessage })
|
||||||
|
|
||||||
|
if (isFirstMessage) {
|
||||||
|
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("UserPromptSubmit hooks prepended to first message parts directly", { sessionID: input.sessionID })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
const message = output.message as {
|
const message = output.message as {
|
||||||
agent?: string
|
agent?: string
|
||||||
model?: { modelID?: string; providerID?: string }
|
model?: { modelID?: string; providerID?: string }
|
||||||
@@ -164,6 +167,7 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
"tool.execute.before": async (
|
"tool.execute.before": async (
|
||||||
|
|||||||
Reference in New Issue
Block a user