MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / runHook

Function runHook

packages/agent-core/src/session/hooks/runner.ts:42–112  ·  view source on GitHub ↗
(
  command: string,
  input: Record<string, unknown>,
  options: RunHookOptions,
)

Source from the content-addressed store, hash-verified

40});
41
42export async function runHook(
43 command: string,
44 input: Record<string, unknown>,
45 options: RunHookOptions,
46): Promise<HookResult> {
47 let child: ChildProcessWithoutNullStreams;
48 try {
49 child = spawn(command, {
50 shell: true,
51 cwd: options.cwd,
52 stdio: 'pipe',
53 detached: process.platform !== 'win32',
54 env: options.env ? { ...process.env, ...options.env } : undefined,
55 });
56 } catch (error) {
57 return allowResult({ stderr: errorMessage(error) });
58 }
59
60 return new Promise<HookResult>((resolve) => {
61 let stdout = '';
62 let stderr = '';
63 let settled = false;
64 const timeoutMs = timeoutSeconds(options.timeout) * 1000;
65
66 const cleanup = () => {
67 clearTimeout(timeout);
68 options.signal?.removeEventListener('abort', onAbort);
69 };
70
71 const settle = (result: HookResult): void => {
72 if (settled) return;
73 settled = true;
74 cleanup();
75 resolve(result);
76 };
77
78 const timeout = setTimeout(() => {
79 killProcess(child);
80 settle(allowResult({ stdout, stderr, timedOut: true }));
81 }, timeoutMs);
82
83 const onAbort = (): void => {
84 killProcess(child);
85 settle(allowResult({ stdout, stderr }));
86 };
87
88 options.signal?.addEventListener('abort', onAbort, { once: true });
89 if (options.signal?.aborted === true) {
90 onAbort();
91 return;
92 }
93
94 child.stdout.setEncoding('utf8');
95 child.stderr.setEncoding('utf8');
96 child.stdout.on('data', (chunk: string) => {
97 stdout += chunk;
98 });
99 child.stderr.on('data', (chunk: string) => {

Callers 2

triggerInnerMethod · 0.90
runner.test.tsFile · 0.85

Calls 10

allowResultFunction · 0.85
timeoutSecondsFunction · 0.85
killProcessFunction · 0.85
resultFromExitCodeFunction · 0.85
errorMessageFunction · 0.70
settleFunction · 0.70
onAbortFunction · 0.70
onMethod · 0.65
spawnFunction · 0.50
endMethod · 0.45

Tested by

no test coverage detected