π OpenHands: AI-Driven Development
fix(hooks): show prompt and agent hook text in the Available Hooks dialog (#18107)
fengyue-xve91fcf4372d7b
diff --git a/__tests__/api/agent-server-adapter.test.ts b/__tests__/api/agent-server-adapter.test.tsindex bfc8b1cba9..bd21ade4f3 100644--- a/__tests__/api/agent-server-adapter.test.ts+++ b/__tests__/api/agent-server-adapter.test.ts@@ -15,6 +15,7 @@ import {getDefaultConversationTitle,parseRuntimeServicesInfo,toAppConversation,+ toHooksResponse,type DirectConversationInfo,} from "#/api/agent-server-adapter";import SettingsService from "#/api/settings-service/settings-service.api";@@ -2216,3 +2217,119 @@ describe("buildStartConversationRequestWithEncryptedSettings", () => {).not.toContain("ROUTE_AT_CONVERSATION_START");});});++describe("toHooksResponse", () => {+ // Prompt and agent hooks reach Canvas over the wire with `prompt`/`system_prompt`,+ // which the typed client's `HookDefinition` does not declare yet.+ const wireHook = (hook: Record<string, unknown>) =>+ hook as unknown as HookConfig["stop"][number]["hooks"][number];++ it("carries a prompt hook's prompt through", () => {+ const response = toHooksResponse(+ makeHookConfig({+ stop: [+ {+ matcher: "*",+ hooks: [+ wireHook({+ type: "prompt",+ command: "",+ prompt: "QA_F07 prompt hook",+ system_prompt: null,+ timeout: 60,+ async: false,+ }),+ ],+ },+ ],+ }),+ );++ expect(response.hooks).toEqual([+ {+ event_type: "stop",+ matchers: [+ {+ matcher: "*",+ hooks: [+ {+ type: "prompt",+ command: "",+ prompt: "QA_F07 prompt hook",+ timeout: 60,+ async: false,+ },+ ],+ },+ ],+ },+ ]);+ });++ it("carries an agent hook's system_prompt through", () => {+ const response = toHooksResponse(+ makeHookConfig({+ session_start: [+ {+ matcher: "*",+ hooks: [+ wireHook({+ type: "agent",+ command: "",+ prompt: null,+ system_prompt: "QA_F07 agent hook",+ timeout: 60,+ async: false,+ }),+ ],+ },+ ],+ }),+ );++ expect(response.hooks).toEqual([+ {+ event_type: "session_start",+ matchers: [+ {+ matcher: "*",+ hooks: [+ {+ type: "agent",+ command: "",+ system_prompt: "QA_F07 agent hook",+ timeout: 60,+ async: false,+ },+ ],+ },+ ],+ },+ ]);+ });++ it("keeps command hooks unchanged", () => {+ const response = toHooksResponse(+ makeHookConfig({+ pre_tool_use: [+ {+ matcher: "terminal",+ hooks: [{ type: HookType.COMMAND, command: "true", timeout: 10 }],+ },+ ],+ }),+ );++ expect(response.hooks).toEqual([+ {+ event_type: "pre_tool_use",+ matchers: [+ {+ matcher: "terminal",+ hooks: [{ type: HookType.COMMAND, command: "true", timeout: 10 }],+ },+ ],+ },+ ]);+ });+});diff --git a/__tests__/components/features/conversation-panel/hooks-modal.test.tsx b/__tests__/components/features/conversation-panel/hooks-modal.test.tsxindex 8624b8ef7a..5d6d849892 100644--- a/__tests__/components/features/conversation-panel/hooks-modal.test.tsx+++ b/__tests__/components/features/conversation-panel/hooks-modal.test.tsx@@ -190,6 +190,113 @@ describe("HookEventItem", () => {expect(screen.getByText("Async")).toBeInTheDocument();});+ it("should render a prompt hook's prompt instead of an empty box", () => {+ const promptHookEvent: HookEvent = {+ event_type: "stop",+ matchers: [+ {+ matcher: "*",+ hooks: [+ { type: "prompt", command: "", prompt: "QA_F07 prompt hook" },+ ],+ },+ ],+ };++ const { container } = render(+ <HookEventItem+ {...defaultProps}+ hookEvent={promptHookEvent}+ isExpanded={true}+ />,+ );++ const preBlocks = container.querySelectorAll("pre");+ expect(preBlocks).toHaveLength(1);+ expect(preBlocks[0]).toHaveTextContent("QA_F07 prompt hook");+ expect(screen.getByText("Type: prompt")).toBeInTheDocument();+ });++ it("should render an agent hook's system_prompt instead of an empty box", () => {+ const agentHookEvent: HookEvent = {+ event_type: "session_start",+ matchers: [+ {+ matcher: "*",+ hooks: [+ { type: "agent", command: "", system_prompt: "QA_F07 agent hook" },+ ],+ },+ ],+ };++ const { container } = render(+ <HookEventItem+ {...defaultProps}+ hookEvent={agentHookEvent}+ isExpanded={true}+ />,+ );++ const preBlocks = container.querySelectorAll("pre");+ expect(preBlocks).toHaveLength(1);+ expect(preBlocks[0]).toHaveTextContent("QA_F07 agent hook");+ expect(screen.getByText("Type: agent")).toBeInTheDocument();+ });++ it("should not render an empty box when a hook omits its text", () => {+ const textlessHookEvent: HookEvent = {+ event_type: "session_start",+ matchers: [+ {+ matcher: "*",+ hooks: [{ type: "agent", command: "" }],+ },+ ],+ };++ const { container } = render(+ <HookEventItem+ {...defaultProps}+ hookEvent={textlessHookEvent}+ isExpanded={true}+ />,+ );++ expect(container.querySelectorAll("pre")).toHaveLength(0);+ expect(screen.getByText("Type: agent")).toBeInTheDocument();+ });++ it("should show each hook's own text in order for a matcher mixing hook types", () => {+ const mixedHookEvent: HookEvent = {+ event_type: "stop",+ matchers: [+ {+ matcher: "*",+ hooks: [+ { type: "command", command: "echo mixed", timeout: 10 },+ { type: "prompt", command: "", prompt: "mixed prompt hook" },+ ],+ },+ ],+ };++ const { container } = render(+ <HookEventItem+ {...defaultProps}+ hookEvent={mixedHookEvent}+ isExpanded={true}+ />,+ );++ const preBlocks = container.querySelectorAll("pre");+ expect(preBlocks).toHaveLength(2);+ expect(preBlocks[0]).toHaveTextContent("echo mixed");+ expect(preBlocks[1]).toHaveTextContent("mixed prompt hook");+ expect(screen.getByText("Type: command")).toBeInTheDocument();+ expect(screen.getByText("Type: prompt")).toBeInTheDocument();+ });+it("should render different event types with correct i18n labels", () => {const eventTypes = [{ type: "pre_tool_use", label: "Pre Tool Use" },diff --git a/src/api/agent-server-adapter.ts b/src/api/agent-server-adapter.tsindex d393fb5da2..2b7fc51951 100644--- a/src/api/agent-server-adapter.ts+++ b/src/api/agent-server-adapter.ts@@ -1792,12 +1792,22 @@ export function toHooksResponse(matchers: hookConfig[eventType].map(({ matcher, hooks }) => ({// The SDK's own defaults for fields a hooks.json may omit.matcher: matcher ?? "*",- hooks: hooks.map((hook) => ({- type: hook.type ?? HookType.COMMAND,- command: hook.command,- timeout: hook.timeout,- async: hook.async,- })),+ hooks: hooks.map((hook) => {+ // `prompt` and `system_prompt` reach Canvas over the wire but lag in+ // the client's HookDefinition; the server sends them as null when absent.+ const wireHook = hook as typeof hook & {+ prompt?: string | null;+ system_prompt?: string | null;+ };+ return {+ type: hook.type ?? HookType.COMMAND,+ command: hook.command,+ prompt: wireHook.prompt ?? undefined,+ system_prompt: wireHook.system_prompt ?? undefined,+ timeout: hook.timeout,+ async: hook.async,+ };+ }),})),})),};diff --git a/src/api/conversation-service/agent-server-conversation-service.types.ts b/src/api/conversation-service/agent-server-conversation-service.types.tsindex 317a632c84..2d7d7e97e0 100644--- a/src/api/conversation-service/agent-server-conversation-service.types.ts+++ b/src/api/conversation-service/agent-server-conversation-service.types.ts@@ -250,8 +250,10 @@ export interface GetSkillsResponse {}export interface HookDefinition {- type: string; // 'command' or 'prompt'- command: string;+ type: string; // 'command', 'prompt' or 'agent'+ command: string; // Empty for prompt and agent hooks+ prompt?: string; // Text of a prompt hook; the server sends null when absent+ system_prompt?: string; // Text of an agent hook; the server sends null when absenttimeout?: number; // Omitted when a server leaves the SDK default implicitasync?: boolean;}diff --git a/src/components/features/conversation-panel/hook-matcher-content.tsx b/src/components/features/conversation-panel/hook-matcher-content.tsxindex 6192b10058..6913af89a4 100644--- a/src/components/features/conversation-panel/hook-matcher-content.tsx+++ b/src/components/features/conversation-panel/hook-matcher-content.tsx@@ -27,37 +27,44 @@ export function HookMatcherContent({ matcher }: HookMatcherContentProps) {<Typography.Text className="text-sm font-semibold text-text-tertiary mb-2">{t(I18nKey.HOOKS_MODAL$COMMANDS)}</Typography.Text>- {(matcher.hooks ?? []).map((hook, index) => (- <div key={`${hook.command}-${index}`} className="mt-2">- <Pre- size="small"- font="mono"- lineHeight="relaxed"- padding="medium"- borderRadius="medium"- maxHeight="small"- overflow="auto"- className="border border-border bg-base text-text-tertiary"- >- {hook.command}- </Pre>- <div className="mt-2 flex flex-wrap gap-2 text-xs text-muted">- <span className={HOOK_PILL_CLASS}>- {t(I18nKey.HOOKS_MODAL$TYPE, { type: hook.type })}- </span>- {hook.timeout !== undefined ? (- <span className={HOOK_PILL_CLASS}>- {t(I18nKey.HOOKS_MODAL$TIMEOUT, { timeout: hook.timeout })}- </span>+ {(matcher.hooks ?? []).map((hook, index) => {+ // Prompt and agent hooks carry their text in `prompt`/`system_prompt`+ // with an empty `command`; only command hooks are guaranteed a command.+ const text = hook.command || hook.prompt || hook.system_prompt;+ return (+ <div key={`${text}-${index}`} className="mt-2">+ {text ? (+ <Pre+ size="small"+ font="mono"+ lineHeight="relaxed"+ padding="medium"+ borderRadius="medium"+ maxHeight="small"+ overflow="auto"+ className="border border-border bg-base text-text-tertiary"+ >+ {text}+ </Pre>) : null}- {hook.async ? (+ <div className="mt-2 flex flex-wrap gap-2 text-xs text-muted"><span className={HOOK_PILL_CLASS}>- {t(I18nKey.HOOKS_MODAL$ASYNC)}+ {t(I18nKey.HOOKS_MODAL$TYPE, { type: hook.type })}</span>- ) : null}+ {hook.timeout !== undefined ? (+ <span className={HOOK_PILL_CLASS}>+ {t(I18nKey.HOOKS_MODAL$TIMEOUT, { timeout: hook.timeout })}+ </span>+ ) : null}+ {hook.async ? (+ <span className={HOOK_PILL_CLASS}>+ {t(I18nKey.HOOKS_MODAL$ASYNC)}+ </span>+ ) : null}+ </div></div>- </div>- ))}+ );+ })}</div></div>);