* feat(hooks): Initial implementation of hooks UI using background terminal UI * Hooks UI improvements * Hook discovery improvements * Separate hooks UI into separate files * Fix failing tests * Add missing docs * Hooks hardening and handling edge cases * Improvements to hooks error messaging * Improve TaskCancel hook UI * Simplify and improve console.log() output from hooks * Changes from usability feedback * test: verify linting fixes * fix: resolve cancel functionality in PostToolUse hook using flag pattern * fix: add cancel button support for TaskResume and UserPromptSubmit hooks * Getting things working again * Fixing cancel behaviors * Trying another fix * Demoed * Add examples to .clinerules/hooks/ directory * Refactored away loadTaskStateWithoutWorkflow * Fix unsafe return in finally block Refactored PostToolUse hook execution to avoid using return statement in finally block, which the linter flags as unsafe. Instead of early return, wrapped the hook logic in a conditional check for abort status. This resolves the biome lint error: lint/correctness/noUnsafeFinally - Unsafe usage of 'return' in finally block * Remove unnecessary files from branch * remove cancelHookExecution() in favor of cancelTask() * Remove unnecessary polling pattern for hook cancellation * Fixing cancel behavior * Fixing cancel behavior * Fix resume * Sqaush commits to improve merge tool behavior Plan out the work needed to resolve race conditions Fix race conditions (1) Fix race conditions (2) Fix race conditions (3) Fix race conditions (4) Fix race conditions (5) Fix unit tests Updated implementation plan doc Code quality improvement Temporary logging to troubleshoot race conditions in hooks UI blocks Fix streaming output for hooks Remove in-progress planning file Revert breakage Remove unneeded arg output in PreToolUse hook UI Fix race condition with PreToolUse and PostToolUse hooks Fix PreToolUse attempt_completion use case Fix ChatTextArea prompt input Fixed part of the rersume bug Clean-up before code review Change cancel button to abort button Fixed TaskCancel behavior Fixing resume behavior (partially fixed) Make notch arrow visible in hooks block up expand/contract toggle Prevent cancel/abort from clearing unsent user message Change 'Cancelled' to 'Aborted' in hook UI block Use color styles consistent with background terminal Fix npm compile issue Remove unneeded hot-cold tracking from hooks Remove unused constant as per PR feedback Minor changes Skip combineHookSequences() if hooks feature setting not enabled Improve mutex pattern for general use and fix additional race condition Refactor to reduce duplication (keep it DRY) Refactor to reduce duplication (keep it DRY) part 2 Fix tests Skip some tests on Windows (not yet supported) Hooks unsupported on Windows Fix broken unit tests * Fix broken unit tests * Bee and Eve fixing 'API Request...' test in e2e tests * Add comment as per ellipsis-dev PR feedback * Changes as per PR feedback from Evan * Remove if that always resolves to true * Don't use vscode API; use cross-compatible solutions instead * Remove mention of obsolete clineAsk handler
300 lines
8.8 KiB
TypeScript
300 lines
8.8 KiB
TypeScript
import { ClineMessage } from "@shared/ExtensionMessage"
|
|
import { EmptyRequest } from "@shared/proto/cline/common"
|
|
import { memo, useMemo, useState } from "react"
|
|
import { TaskServiceClient } from "@/services/grpc-client"
|
|
import { CHAT_ROW_EXPANDED_BG_COLOR } from "../common/CodeBlock"
|
|
import { HOOK_OUTPUT_STRING } from "./constants"
|
|
|
|
const normalColor = "var(--vscode-foreground)"
|
|
const errorColor = "var(--vscode-errorForeground)"
|
|
const successColor = "var(--vscode-charts-green)"
|
|
const completedColor = "var(--vscode-descriptionForeground)"
|
|
|
|
/**
|
|
* Determines if a hook message should be expanded by default.
|
|
*
|
|
* Expansion logic:
|
|
* - Historical messages (>5 seconds old): Always collapsed for better UX
|
|
* - Fresh failed/cancelled hooks: Expanded to show error details
|
|
* - Fresh successful hooks: Collapsed to minimize clutter
|
|
* - Running hooks: Not applicable (handled separately)
|
|
*
|
|
* @param message The message containing timestamp information
|
|
* @param metadata The hook metadata containing status
|
|
* @returns true if the hook output should be expanded by default
|
|
*/
|
|
function shouldExpandHookByDefault(message: ClineMessage, metadata: HookMetadata): boolean {
|
|
// Always collapse historical messages (>5 seconds old) for better UX
|
|
const isHistorical = message.ts && Date.now() - message.ts > 5000
|
|
if (isHistorical) {
|
|
return false
|
|
}
|
|
|
|
// Expand fresh failed/cancelled hooks to show error details
|
|
return metadata.status === "failed" || metadata.status === "cancelled"
|
|
}
|
|
|
|
interface HookMessageProps {
|
|
message: ClineMessage
|
|
// CommandOutput component - we'll import and use it here
|
|
CommandOutput: React.ComponentType<{
|
|
output: string
|
|
isOutputFullyExpanded: boolean
|
|
onToggle: () => void
|
|
isContainerExpanded: boolean
|
|
}>
|
|
}
|
|
|
|
interface HookMetadata {
|
|
hookName: string
|
|
toolName?: string
|
|
status: string
|
|
exitCode?: number
|
|
hasJsonResponse?: boolean
|
|
pendingToolInfo?: {
|
|
tool: string
|
|
path?: string
|
|
command?: string
|
|
content?: string
|
|
diff?: string
|
|
regex?: string
|
|
url?: string
|
|
mcpTool?: string
|
|
mcpServer?: string
|
|
resourceUri?: string
|
|
}
|
|
error?: {
|
|
type: "timeout" | "validation" | "execution" | "cancellation"
|
|
message: string
|
|
details?: string
|
|
scriptPath?: string
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Displays a hook execution message with status, pending tool info, and output.
|
|
*
|
|
* Smart expansion defaults:
|
|
* - Failed hooks: Expanded by default (show error details)
|
|
* - Aborted hooks: Expanded by default (show what happened)
|
|
* - Successful hooks: Collapsed by default (minimize clutter)
|
|
* - Running hooks: Always shows pending tool info
|
|
*/
|
|
const HookMessage = memo(({ message, CommandOutput }: HookMessageProps) => {
|
|
// Log when component mounts/updates
|
|
console.log(
|
|
`[HOOK-UI RENDER] HookMessage rendering: ${JSON.stringify({ hookName: message.text?.substring(0, 100), ts: message.ts })}`,
|
|
)
|
|
|
|
// Parse hook metadata and output
|
|
const { metadata, output } = useMemo(() => {
|
|
const splitMessage = (text: string) => {
|
|
const outputIndex = text.indexOf(HOOK_OUTPUT_STRING)
|
|
if (outputIndex === -1) {
|
|
return { metadata: text, output: "" }
|
|
}
|
|
return {
|
|
metadata: text.slice(0, outputIndex).trim(),
|
|
output: text
|
|
.slice(outputIndex + HOOK_OUTPUT_STRING.length)
|
|
.trim()
|
|
.split("")
|
|
.map((char) => {
|
|
switch (char) {
|
|
case "\t":
|
|
return "→ "
|
|
case "\b":
|
|
return "⌫"
|
|
case "\f":
|
|
return "⏏"
|
|
case "\v":
|
|
return "⇳"
|
|
default:
|
|
return char
|
|
}
|
|
})
|
|
.join(""),
|
|
}
|
|
}
|
|
|
|
const { metadata: metadataStr, output } = splitMessage(message.text || "")
|
|
|
|
let hookMetadata: HookMetadata
|
|
try {
|
|
hookMetadata = JSON.parse(metadataStr)
|
|
} catch {
|
|
hookMetadata = { hookName: "Unknown", status: "unknown" }
|
|
}
|
|
|
|
return { metadata: hookMetadata, output }
|
|
}, [message.text])
|
|
|
|
// Determine initial expansion state using pure function
|
|
const [isHookOutputExpanded, setIsHookOutputExpanded] = useState(() => shouldExpandHookByDefault(message, metadata))
|
|
|
|
const isRunning = metadata.status === "running"
|
|
const isCompleted = metadata.status === "completed"
|
|
const isFailed = metadata.status === "failed"
|
|
const isCancelled = metadata.status === "cancelled"
|
|
|
|
const headerStyle: React.CSSProperties = {
|
|
display: "flex",
|
|
alignItems: "center",
|
|
gap: "10px",
|
|
marginBottom: "12px",
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div style={headerStyle}>
|
|
<span
|
|
className="codicon codicon-symbol-event"
|
|
style={{
|
|
color: normalColor,
|
|
marginBottom: "-1.5px",
|
|
}}></span>
|
|
<span style={{ color: normalColor, fontWeight: "bold" }}>Hook:</span>
|
|
<span style={{ color: normalColor }}>{metadata.hookName}</span>
|
|
{metadata.toolName && (
|
|
<span style={{ color: "var(--vscode-descriptionForeground)", fontSize: "0.9em" }}>({metadata.toolName})</span>
|
|
)}
|
|
</div>
|
|
<div
|
|
style={{
|
|
borderRadius: 6,
|
|
border: "1px solid var(--vscode-editorGroup-border)",
|
|
overflow: "visible",
|
|
backgroundColor: CHAT_ROW_EXPANDED_BG_COLOR,
|
|
transition: "all 0.3s ease-in-out",
|
|
}}>
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
padding: "8px 10px",
|
|
backgroundColor: CHAT_ROW_EXPANDED_BG_COLOR,
|
|
borderBottom:
|
|
metadata.pendingToolInfo || output.length > 0 ? "1px solid var(--vscode-editorGroup-border)" : "none",
|
|
borderTopLeftRadius: "6px",
|
|
borderTopRightRadius: "6px",
|
|
}}>
|
|
<div
|
|
style={{
|
|
display: "flex",
|
|
alignItems: "center",
|
|
gap: "8px",
|
|
flex: 1,
|
|
minWidth: 0,
|
|
}}>
|
|
<div
|
|
style={{
|
|
width: "8px",
|
|
height: "8px",
|
|
borderRadius: "50%",
|
|
backgroundColor: isRunning ? successColor : isFailed || isCancelled ? errorColor : completedColor,
|
|
animation: isRunning ? "pulse 2s ease-in-out infinite" : "none",
|
|
flexShrink: 0,
|
|
}}
|
|
/>
|
|
<span
|
|
style={{
|
|
color: isRunning ? successColor : isFailed || isCancelled ? errorColor : completedColor,
|
|
fontWeight: 500,
|
|
fontSize: "13px",
|
|
flexShrink: 0,
|
|
}}>
|
|
{isRunning
|
|
? "Running"
|
|
: isFailed
|
|
? "Failed"
|
|
: isCancelled
|
|
? "Aborted"
|
|
: isCompleted
|
|
? "Completed"
|
|
: "Unknown"}
|
|
</span>
|
|
{metadata.exitCode !== undefined && metadata.exitCode !== 0 && (
|
|
<span
|
|
style={{
|
|
color: "var(--vscode-descriptionForeground)",
|
|
fontSize: "12px",
|
|
}}>
|
|
(exit: {metadata.exitCode})
|
|
</span>
|
|
)}
|
|
</div>
|
|
{isRunning && metadata.hookName !== "TaskCancel" && (
|
|
<button
|
|
onClick={(e) => {
|
|
e.stopPropagation()
|
|
console.log(
|
|
`[HOOK-UI CANCEL] Hook cancel button clicked for ${metadata.hookName}${metadata.toolName ? ` (${metadata.toolName})` : ""}`,
|
|
)
|
|
// Cancel the task - cancelling a hook always cancels the entire task
|
|
TaskServiceClient.cancelTask(EmptyRequest.create({})).catch((err) =>
|
|
console.error("Failed to cancel task:", err),
|
|
)
|
|
}}
|
|
onMouseEnter={(e) => {
|
|
e.currentTarget.style.background = "var(--vscode-button-secondaryHoverBackground)"
|
|
}}
|
|
onMouseLeave={(e) => {
|
|
e.currentTarget.style.background = "var(--vscode-button-secondaryBackground)"
|
|
}}
|
|
style={{
|
|
background: "var(--vscode-button-secondaryBackground)",
|
|
color: "var(--vscode-button-secondaryForeground)",
|
|
border: "none",
|
|
borderRadius: "2px",
|
|
padding: "4px 10px",
|
|
fontSize: "12px",
|
|
cursor: "pointer",
|
|
fontFamily: "inherit",
|
|
}}>
|
|
Abort
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{/* Show concise error message for specific error types */}
|
|
{isFailed && metadata.error && metadata.error.type === "timeout" && (
|
|
<div
|
|
style={{
|
|
padding: "12px",
|
|
borderBottom: output.length > 0 ? "1px solid var(--vscode-editorGroup-border)" : "none",
|
|
fontSize: "13px",
|
|
color: "var(--vscode-descriptionForeground)",
|
|
}}>
|
|
Took longer than 30 seconds. Check for infinite loops or add timeouts to network requests.
|
|
</div>
|
|
)}
|
|
|
|
{isFailed && metadata.error && metadata.error.type === "validation" && (
|
|
<div
|
|
style={{
|
|
padding: "12px",
|
|
borderBottom: output.length > 0 ? "1px solid var(--vscode-editorGroup-border)" : "none",
|
|
fontSize: "13px",
|
|
color: "var(--vscode-descriptionForeground)",
|
|
}}>
|
|
Hook returned invalid JSON. See error details below for more information.
|
|
</div>
|
|
)}
|
|
|
|
{/* Show hook output if present */}
|
|
{output.length > 0 && (
|
|
<CommandOutput
|
|
isContainerExpanded={true}
|
|
isOutputFullyExpanded={isHookOutputExpanded}
|
|
onToggle={() => setIsHookOutputExpanded(!isHookOutputExpanded)}
|
|
output={output}
|
|
/>
|
|
)}
|
|
</div>
|
|
</>
|
|
)
|
|
})
|
|
|
|
export default HookMessage
|