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,24 +139,33 @@ 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 })
|
||||||
const message = output.message as {
|
|
||||||
agent?: string
|
if (isFirstMessage) {
|
||||||
model?: { modelID?: string; providerID?: string }
|
const idx = output.parts.findIndex((p) => p.type === "text" && p.text)
|
||||||
path?: { cwd?: string; root?: string }
|
if (idx >= 0) {
|
||||||
tools?: Record<string, boolean>
|
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,
|
||||||
|
})
|
||||||
|
|
||||||
|
log(success ? "Hook message injected via file system" : "File injection failed", {
|
||||||
|
sessionID: input.sessionID,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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", {
|
|
||||||
sessionID: input.sessionID,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user