MCPcopy Index your code
hub / github.com/github/copilot-sdk / _handleHooksInvoke

Method _handleHooksInvoke

nodejs/src/session.ts:1210–1251  ·  view source on GitHub ↗

* Handles a hooks invocation from the Copilot CLI. * * @param hookType - The type of hook being invoked * @param input - The input data for the hook * @returns A promise that resolves with the hook output, or undefined * @internal This method is for internal use by the SDK.

(hookType: string, input: unknown)

Source from the content-addressed store, hash-verified

1208 * @internal This method is for internal use by the SDK.
1209 */
1210 async _handleHooksInvoke(hookType: string, input: unknown): Promise<unknown> {
1211 if (!this.hooks) {
1212 return undefined;
1213 }
1214
1215 // All hook inputs share BaseHookInput, which exposes `timestamp` as a Date.
1216 // The wire format sends it as Unix epoch ms (number), so we deserialize
1217 // here, at the one place that knows the input is a hook payload. Bad data
1218 // is left alone — the user-facing handler types still cast unknown to the
1219 // specific HookInput, so a runtime type mismatch surfaces as a normal
1220 // TypeError in user code rather than being silently masked.
1221 const normalized = deserializeHookInput(input);
1222
1223 type GenericHandler = (
1224 input: unknown,
1225 invocation: { sessionId: string }
1226 ) => Promise<unknown> | unknown;
1227
1228 const handlerMap: Record<string, GenericHandler | undefined> = {
1229 preToolUse: this.hooks.onPreToolUse as GenericHandler | undefined,
1230 preMcpToolCall: this.hooks.onPreMcpToolCall as GenericHandler | undefined,
1231 postToolUse: this.hooks.onPostToolUse as GenericHandler | undefined,
1232 postToolUseFailure: this.hooks.onPostToolUseFailure as GenericHandler | undefined,
1233 userPromptSubmitted: this.hooks.onUserPromptSubmitted as GenericHandler | undefined,
1234 sessionStart: this.hooks.onSessionStart as GenericHandler | undefined,
1235 sessionEnd: this.hooks.onSessionEnd as GenericHandler | undefined,
1236 errorOccurred: this.hooks.onErrorOccurred as GenericHandler | undefined,
1237 };
1238
1239 const handler = handlerMap[hookType];
1240 if (!handler) {
1241 return undefined;
1242 }
1243
1244 try {
1245 const result = await handler(normalized, { sessionId: this.sessionId });
1246 return result;
1247 } catch (_error) {
1248 // Hook failed, return undefined
1249 return undefined;
1250 }
1251 }
1252
1253 /**
1254 * Retrieves all events and messages from this session's history.

Callers 2

client.test.tsFile · 0.80
handleHooksInvokeMethod · 0.80

Calls 2

deserializeHookInputFunction · 0.85
handlerFunction · 0.85

Tested by

no test coverage detected