|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import type { PartListUnion } from '@google/genai'; |
| 8 | +import type { MessageBus } from '../confirmation-bus/message-bus.js'; |
| 9 | +import { |
| 10 | + MessageBusType, |
| 11 | + type HookExecutionRequest, |
| 12 | + type HookExecutionResponse, |
| 13 | +} from '../confirmation-bus/types.js'; |
| 14 | +import { createHookOutput, type DefaultHookOutput } from '../hooks/types.js'; |
| 15 | +import { partToString } from '../utils/partUtils.js'; |
| 16 | +import { debugLogger } from '../utils/debugLogger.js'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Fires the BeforeAgent hook and returns the hook output. |
| 20 | + * This should be called before processing a user prompt. |
| 21 | + * |
| 22 | + * The caller can use the returned DefaultHookOutput methods: |
| 23 | + * - isBlockingDecision() / shouldStopExecution() to check if blocked |
| 24 | + * - getEffectiveReason() to get the blocking reason |
| 25 | + * - getAdditionalContext() to get additional context to add |
| 26 | + * |
| 27 | + * @param messageBus The message bus to use for hook communication |
| 28 | + * @param request The user's request (prompt) |
| 29 | + * @returns The hook output, or undefined if no hook was executed or on error |
| 30 | + */ |
| 31 | +export async function fireBeforeAgentHook( |
| 32 | + messageBus: MessageBus, |
| 33 | + request: PartListUnion, |
| 34 | +): Promise<DefaultHookOutput | undefined> { |
| 35 | + try { |
| 36 | + const promptText = partToString(request); |
| 37 | + |
| 38 | + const response = await messageBus.request< |
| 39 | + HookExecutionRequest, |
| 40 | + HookExecutionResponse |
| 41 | + >( |
| 42 | + { |
| 43 | + type: MessageBusType.HOOK_EXECUTION_REQUEST, |
| 44 | + eventName: 'BeforeAgent', |
| 45 | + input: { |
| 46 | + prompt: promptText, |
| 47 | + }, |
| 48 | + }, |
| 49 | + MessageBusType.HOOK_EXECUTION_RESPONSE, |
| 50 | + ); |
| 51 | + |
| 52 | + return response.output |
| 53 | + ? createHookOutput('BeforeAgent', response.output) |
| 54 | + : undefined; |
| 55 | + } catch (error) { |
| 56 | + debugLogger.warn(`BeforeAgent hook failed: ${error}`); |
| 57 | + return undefined; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Fires the AfterAgent hook and returns the hook output. |
| 63 | + * This should be called after the agent has generated a response. |
| 64 | + * |
| 65 | + * The caller can use the returned DefaultHookOutput methods: |
| 66 | + * - isBlockingDecision() / shouldStopExecution() to check if continuation is requested |
| 67 | + * - getEffectiveReason() to get the continuation reason |
| 68 | + * |
| 69 | + * @param messageBus The message bus to use for hook communication |
| 70 | + * @param request The original user's request (prompt) |
| 71 | + * @param responseText The agent's response text |
| 72 | + * @returns The hook output, or undefined if no hook was executed or on error |
| 73 | + */ |
| 74 | +export async function fireAfterAgentHook( |
| 75 | + messageBus: MessageBus, |
| 76 | + request: PartListUnion, |
| 77 | + responseText: string, |
| 78 | +): Promise<DefaultHookOutput | undefined> { |
| 79 | + try { |
| 80 | + const promptText = partToString(request); |
| 81 | + |
| 82 | + const response = await messageBus.request< |
| 83 | + HookExecutionRequest, |
| 84 | + HookExecutionResponse |
| 85 | + >( |
| 86 | + { |
| 87 | + type: MessageBusType.HOOK_EXECUTION_REQUEST, |
| 88 | + eventName: 'AfterAgent', |
| 89 | + input: { |
| 90 | + prompt: promptText, |
| 91 | + prompt_response: responseText, |
| 92 | + stop_hook_active: false, |
| 93 | + }, |
| 94 | + }, |
| 95 | + MessageBusType.HOOK_EXECUTION_RESPONSE, |
| 96 | + ); |
| 97 | + |
| 98 | + return response.output |
| 99 | + ? createHookOutput('AfterAgent', response.output) |
| 100 | + : undefined; |
| 101 | + } catch (error) { |
| 102 | + debugLogger.warn(`AfterAgent hook failed: ${error}`); |
| 103 | + return undefined; |
| 104 | + } |
| 105 | +} |
0 commit comments