278 lines
9.0 KiB
TypeScript
278 lines
9.0 KiB
TypeScript
import { homedir } from 'os'
|
|
import { join } from 'path'
|
|
import type { SFTPWrapper } from 'ssh2'
|
|
import type { AgentHookInstallState, AgentHookInstallStatus } from '../../shared/agent-hook-types'
|
|
import {
|
|
createManagedCommandMatcher,
|
|
buildWindowsAgentHookPostCommand,
|
|
getSharedManagedScriptPath,
|
|
readHooksJson,
|
|
removeManagedCommands,
|
|
wrapPosixHookCommand,
|
|
writeHooksJson,
|
|
writeManagedScript,
|
|
type HookDefinition
|
|
} from '../agent-hooks/installer-utils'
|
|
import {
|
|
readHooksJsonRemote,
|
|
writeHooksJsonRemote,
|
|
writeManagedScriptRemote
|
|
} from '../agent-hooks/installer-utils-remote'
|
|
|
|
const GROK_EVENTS = [
|
|
{ eventName: 'SessionStart', definition: { hooks: [{ type: 'command', command: '' }] } },
|
|
{ eventName: 'UserPromptSubmit', definition: { hooks: [{ type: 'command', command: '' }] } },
|
|
{ eventName: 'Stop', definition: { hooks: [{ type: 'command', command: '' }] } },
|
|
{ eventName: 'SessionEnd', definition: { hooks: [{ type: 'command', command: '' }] } },
|
|
{
|
|
eventName: 'PreToolUse',
|
|
definition: { matcher: '*', hooks: [{ type: 'command', command: '' }] }
|
|
},
|
|
{
|
|
eventName: 'PostToolUse',
|
|
definition: { matcher: '*', hooks: [{ type: 'command', command: '' }] }
|
|
},
|
|
{
|
|
eventName: 'PostToolUseFailure',
|
|
definition: { matcher: '*', hooks: [{ type: 'command', command: '' }] }
|
|
},
|
|
{ eventName: 'Notification', definition: { hooks: [{ type: 'command', command: '' }] } }
|
|
] as const
|
|
|
|
function getConfigPath(): string {
|
|
// Why: Grok loads trusted global hook files from ~/.grok/hooks/*.json. Keep
|
|
// Orca's managed entries in a dedicated file so user-authored hook files stay
|
|
// untouched and project-level trust is not required for status reporting.
|
|
return join(homedir(), '.grok', 'hooks', 'orca-status.json')
|
|
}
|
|
|
|
function getManagedScriptFileName(): string {
|
|
return process.platform === 'win32' ? 'grok-hook.cmd' : 'grok-hook.sh'
|
|
}
|
|
|
|
function getManagedScriptPath(): string {
|
|
return getSharedManagedScriptPath(getManagedScriptFileName())
|
|
}
|
|
|
|
function getManagedCommand(scriptPath: string): string {
|
|
return process.platform === 'win32' ? scriptPath : wrapPosixHookCommand(scriptPath)
|
|
}
|
|
|
|
function getManagedScript(target: 'local' | 'posix' = 'local'): string {
|
|
if (target === 'local' && process.platform === 'win32') {
|
|
return [
|
|
'@echo off',
|
|
'setlocal',
|
|
'if defined ORCA_AGENT_HOOK_ENDPOINT if exist "%ORCA_AGENT_HOOK_ENDPOINT%" call "%ORCA_AGENT_HOOK_ENDPOINT%" 2>nul',
|
|
'if "%ORCA_AGENT_HOOK_PORT%"=="" exit /b 0',
|
|
'if "%ORCA_AGENT_HOOK_TOKEN%"=="" exit /b 0',
|
|
'if "%ORCA_PANE_KEY%"=="" exit /b 0',
|
|
buildWindowsAgentHookPostCommand('grok'),
|
|
'exit /b 0',
|
|
''
|
|
].join('\r\n')
|
|
}
|
|
|
|
return [
|
|
'#!/bin/sh',
|
|
'if [ -n "$ORCA_AGENT_HOOK_ENDPOINT" ] && [ -r "$ORCA_AGENT_HOOK_ENDPOINT" ]; then',
|
|
' . "$ORCA_AGENT_HOOK_ENDPOINT" 2>/dev/null || :',
|
|
'fi',
|
|
'if [ -z "$ORCA_AGENT_HOOK_PORT" ] || [ -z "$ORCA_AGENT_HOOK_TOKEN" ] || [ -z "$ORCA_PANE_KEY" ]; then',
|
|
' exit 0',
|
|
'fi',
|
|
'payload=$(cat)',
|
|
'if [ -z "$payload" ]; then',
|
|
' exit 0',
|
|
'fi',
|
|
'curl -sS -X POST "http://127.0.0.1:${ORCA_AGENT_HOOK_PORT}/hook/grok" \\',
|
|
' -H "Content-Type: application/x-www-form-urlencoded" \\',
|
|
' -H "X-Orca-Agent-Hook-Token: ${ORCA_AGENT_HOOK_TOKEN}" \\',
|
|
' --data-urlencode "paneKey=${ORCA_PANE_KEY}" \\',
|
|
' --data-urlencode "tabId=${ORCA_TAB_ID}" \\',
|
|
' --data-urlencode "worktreeId=${ORCA_WORKTREE_ID}" \\',
|
|
' --data-urlencode "env=${ORCA_AGENT_HOOK_ENV}" \\',
|
|
' --data-urlencode "version=${ORCA_AGENT_HOOK_VERSION}" \\',
|
|
' --data-urlencode "payload=${payload}" >/dev/null 2>&1 || true',
|
|
'exit 0',
|
|
''
|
|
].join('\n')
|
|
}
|
|
|
|
function buildInstalledConfig(
|
|
config: NonNullable<ReturnType<typeof readHooksJson>>,
|
|
command: string,
|
|
scriptFileName: string
|
|
): void {
|
|
const nextHooks = { ...config.hooks }
|
|
const isManagedCommand = createManagedCommandMatcher(scriptFileName)
|
|
const managedEvents = new Set<string>(GROK_EVENTS.map((event) => event.eventName))
|
|
|
|
// Why: Orca owns only grok-hook.* entries. Sweep stale managed commands out
|
|
// of retired events while preserving any user-authored hooks in this file.
|
|
for (const [eventName, definitions] of Object.entries(nextHooks)) {
|
|
if (managedEvents.has(eventName) || !Array.isArray(definitions)) {
|
|
continue
|
|
}
|
|
const cleaned = removeManagedCommands(definitions, isManagedCommand)
|
|
if (cleaned.length === 0) {
|
|
delete nextHooks[eventName]
|
|
} else {
|
|
nextHooks[eventName] = cleaned
|
|
}
|
|
}
|
|
|
|
for (const event of GROK_EVENTS) {
|
|
const current = Array.isArray(nextHooks[event.eventName]) ? nextHooks[event.eventName] : []
|
|
const cleaned = removeManagedCommands(current, isManagedCommand)
|
|
const definition: HookDefinition = {
|
|
...event.definition,
|
|
hooks: [{ type: 'command', command }]
|
|
}
|
|
nextHooks[event.eventName] = [...cleaned, definition]
|
|
}
|
|
|
|
config.hooks = nextHooks
|
|
}
|
|
|
|
export class GrokHookService {
|
|
getStatus(): AgentHookInstallStatus {
|
|
const configPath = getConfigPath()
|
|
const scriptPath = getManagedScriptPath()
|
|
const config = readHooksJson(configPath)
|
|
if (!config) {
|
|
return {
|
|
agent: 'grok',
|
|
state: 'error',
|
|
configPath,
|
|
managedHooksPresent: false,
|
|
detail: 'Could not parse Grok hook config'
|
|
}
|
|
}
|
|
|
|
const command = getManagedCommand(scriptPath)
|
|
const missing: string[] = []
|
|
let presentCount = 0
|
|
for (const event of GROK_EVENTS) {
|
|
const definitions = Array.isArray(config.hooks?.[event.eventName])
|
|
? config.hooks![event.eventName]!
|
|
: []
|
|
const hasCommand = definitions.some((definition) =>
|
|
(definition.hooks ?? []).some((hook) => hook.command === command)
|
|
)
|
|
if (hasCommand) {
|
|
presentCount += 1
|
|
} else {
|
|
missing.push(event.eventName)
|
|
}
|
|
}
|
|
|
|
const managedHooksPresent = presentCount > 0
|
|
let state: AgentHookInstallState
|
|
let detail: string | null
|
|
if (missing.length === 0) {
|
|
state = 'installed'
|
|
detail = null
|
|
} else if (presentCount === 0) {
|
|
state = 'not_installed'
|
|
detail = null
|
|
} else {
|
|
state = 'partial'
|
|
detail = `Managed hook missing for events: ${missing.join(', ')}`
|
|
}
|
|
return { agent: 'grok', state, configPath, managedHooksPresent, detail }
|
|
}
|
|
|
|
install(): AgentHookInstallStatus {
|
|
const configPath = getConfigPath()
|
|
const scriptPath = getManagedScriptPath()
|
|
const config = readHooksJson(configPath)
|
|
if (!config) {
|
|
return {
|
|
agent: 'grok',
|
|
state: 'error',
|
|
configPath,
|
|
managedHooksPresent: false,
|
|
detail: 'Could not parse Grok hook config'
|
|
}
|
|
}
|
|
|
|
buildInstalledConfig(config, getManagedCommand(scriptPath), getManagedScriptFileName())
|
|
writeManagedScript(scriptPath, getManagedScript())
|
|
writeHooksJson(configPath, config)
|
|
return this.getStatus()
|
|
}
|
|
|
|
async installRemote(sftp: SFTPWrapper, remoteHome: string): Promise<AgentHookInstallStatus> {
|
|
const home = remoteHome.replace(/\/$/, '')
|
|
const remoteConfigPath = `${home}/.grok/hooks/orca-status.json`
|
|
const remoteScriptPath = `${home}/.orca/agent-hooks/grok-hook.sh`
|
|
try {
|
|
const config = await readHooksJsonRemote(sftp, remoteConfigPath)
|
|
if (!config) {
|
|
return {
|
|
agent: 'grok',
|
|
state: 'error',
|
|
configPath: remoteConfigPath,
|
|
managedHooksPresent: false,
|
|
detail: 'Could not parse remote Grok hook config'
|
|
}
|
|
}
|
|
|
|
buildInstalledConfig(config, wrapPosixHookCommand(remoteScriptPath), 'grok-hook.sh')
|
|
await writeManagedScriptRemote(sftp, remoteScriptPath, getManagedScript('posix'))
|
|
await writeHooksJsonRemote(sftp, remoteConfigPath, config)
|
|
|
|
return {
|
|
agent: 'grok',
|
|
state: 'installed',
|
|
configPath: remoteConfigPath,
|
|
managedHooksPresent: true,
|
|
detail: null
|
|
}
|
|
} catch (err) {
|
|
return {
|
|
agent: 'grok',
|
|
state: 'error',
|
|
configPath: remoteConfigPath,
|
|
managedHooksPresent: false,
|
|
detail: err instanceof Error ? err.message : String(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
remove(): AgentHookInstallStatus {
|
|
const configPath = getConfigPath()
|
|
const config = readHooksJson(configPath)
|
|
if (!config) {
|
|
return {
|
|
agent: 'grok',
|
|
state: 'error',
|
|
configPath,
|
|
managedHooksPresent: false,
|
|
detail: 'Could not parse Grok hook config'
|
|
}
|
|
}
|
|
|
|
const nextHooks = { ...config.hooks }
|
|
const isManagedCommand = createManagedCommandMatcher(getManagedScriptFileName())
|
|
for (const [eventName, definitions] of Object.entries(nextHooks)) {
|
|
if (!Array.isArray(definitions)) {
|
|
continue
|
|
}
|
|
const cleaned = removeManagedCommands(definitions, isManagedCommand)
|
|
if (cleaned.length === 0) {
|
|
delete nextHooks[eventName]
|
|
} else {
|
|
nextHooks[eventName] = cleaned
|
|
}
|
|
}
|
|
|
|
config.hooks = nextHooks
|
|
writeHooksJson(configPath, config)
|
|
return this.getStatus()
|
|
}
|
|
}
|
|
|
|
export const grokHookService = new GrokHookService()
|