( $: Shell, verb: HookVerb, payload: HookPayload, directory: string, )
| 58 | * @returns A `HookResult` describing the outcome (never throws) |
| 59 | */ |
| 60 | export async function invokeHook( |
| 61 | $: Shell, |
| 62 | verb: HookVerb, |
| 63 | payload: HookPayload, |
| 64 | directory: string, |
| 65 | ): Promise<HookResult> { |
| 66 | const start = performance.now(); |
| 67 | const json = JSON.stringify(payload); |
| 68 | |
| 69 | try { |
| 70 | const result = |
| 71 | await $`echo ${json} | ${ATOMIC_CMD} agent hooks opencode ${verb}` |
| 72 | .cwd(directory) |
| 73 | .quiet() |
| 74 | .nothrow(); |
| 75 | |
| 76 | const duration = Math.round(performance.now() - start); |
| 77 | const ok = result.exitCode === 0; |
| 78 | const stderr = result.stderr.toString().trim(); |
| 79 | const stdout = result.stdout.toString().trim(); |
| 80 | |
| 81 | if (!ok) { |
| 82 | return { |
| 83 | ok: false, |
| 84 | verb, |
| 85 | exitCode: result.exitCode, |
| 86 | error: stderr || `exit code ${result.exitCode}`, |
| 87 | duration, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | return { ok: true, verb, exitCode: 0, duration, stderr, stdout }; |
| 92 | } catch (err) { |
| 93 | const duration = Math.round(performance.now() - start); |
| 94 | const message = err instanceof Error ? err.message : String(err); |
| 95 | |
| 96 | return { |
| 97 | ok: false, |
| 98 | verb, |
| 99 | error: message, |
| 100 | duration, |
| 101 | }; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // ============================================================================= |
| 106 | // Availability Check |
no outgoing calls
no test coverage detected