The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
package hooks
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/hermessession"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/hookcmd"
)
type HermesOptions struct {
Level Level
LogPath string
UserMode bool
}
type HermesStatus struct {
Installed bool `json:"installed"`
BinaryPath string `json:"binary_path,omitempty"`
ConfigPath string `json:"config_path,omitempty"`
Message string `json:"message,omitempty"`
}
type hermesConfig struct {
values map[string]interface{}
hooks map[string][]hermesHookRef
}
type hermesHookRef struct {
Matcher string `yaml:"matcher,omitempty"`
Command string `yaml:"command"`
Timeout int `yaml:"timeout,omitempty"`
}
var hermesRuntime = hookRuntime{
displayName: "Hermes Agent",
configPath: hermesConfigPath,
install: installHermesConfig,
uninstall: removeHermesEndpointHooks,
isInstalled: isHermesInstalledAt,
}
func InstallHermes(opts HermesOptions) (HermesStatus, error) {
status, err := installRuntimeHooks(hermesRuntime, RuntimeOptions(opts))
if err != nil {
return HermesStatus{}, err
}
return hermesStatusFromRuntime(status), nil
}
func UninstallHermes(opts HermesOptions) (HermesStatus, error) {
status, err := uninstallRuntimeHooks(hermesRuntime, RuntimeOptions(opts))
if err != nil {
return HermesStatus{}, err
}
return hermesStatusFromRuntime(status), nil
}
func HermesHookStatus(opts HermesOptions) HermesStatus {
return hermesStatusFromRuntime(runtimeHookStatus(hermesRuntime, RuntimeOptions(opts)))
}
func IsHermesInstalled(opts HermesOptions) bool {
return isRuntimeInstalled(hermesRuntime, RuntimeOptions(opts))
}
func hermesStatusFromRuntime(status runtimeStatus) HermesStatus {
return HermesStatus{
Installed: status.Installed,
BinaryPath: status.BinaryPath,
ConfigPath: status.ConfigPath,
Message: status.Message,
}
}
func installHermesConfig(path, binaryPath, logPath, configPath string) error {
config, err := readHermesConfig(path)
if err != nil {
return err
}
prefix := endpointCommandPrefix("hermes", binaryPath, logPath, configPath)
endpointHooks := map[string]hermesHookRef{
"on_session_start": {Command: prefix + " session-start"},
"pre_llm_call": {Command: prefix + " prompt-submit", Timeout: 30},
"pre_tool_call": {Matcher: ".*", Command: prefix + " pre-tool", Timeout: 10},
"post_tool_call": {Matcher: ".*", Command: prefix + " post-tool", Timeout: 10},
"pre_approval_request": {Command: prefix + " permission-request", Timeout: 10},
"post_approval_response": {Command: prefix + " permission-request", Timeout: 10},
"subagent_stop": {Command: prefix + " subagent-stop", Timeout: 10},
"on_session_end": {Command: prefix + " session-end"},
"on_session_finalize": {Command: prefix + " session-end"},
}
for eventName, ref := range endpointHooks {
config.hooks[eventName] = mergeHermesEndpointHook(config.hooks[eventName], ref)
}
return writeHermesConfig(path, config)
}
// Hermes used to need its own builder, and now it does not.
//
// It prefixed the command with `env`, which is what an inline `VAR=value cmd` prefix requires when the
// command is executed without a shell to interpret it. Passing the settings as flags removes the need
// for a prefix at all, so there is nothing left for `env` to set and the special case collapses into
// the shared builder.
//
// One pre-existing question this does not answer: if Hermes really does invoke without a shell, the
// quoting around each path is literal there rather than syntax. That was equally true of the old form,
// and Hermes is not a runtime this repository can exercise, so it stays as it was rather than being
// changed on a guess.
func readHermesConfig(path string) (hermesConfig, error) {
config := hermesConfig{
values: map[string]interface{}{},
hooks: map[string][]hermesHookRef{},
}
data, err := os.ReadFile(path)
if err == nil {
if len(data) > 0 {
if err := yaml.Unmarshal(data, &config.values); err != nil {
return hermesConfig{}, err
}
}
} else if !os.IsNotExist(err) {
return hermesConfig{}, err
}
if config.values == nil {
config.values = map[string]interface{}{}
}
if rawHooks, ok := config.values["hooks"]; ok && rawHooks != nil {
data, err := yaml.Marshal(rawHooks)
if err != nil {
return hermesConfig{}, err
}
if err := yaml.Unmarshal(data, &config.hooks); err != nil {
return hermesConfig{}, err
}
}
if config.hooks == nil {
config.hooks = map[string][]hermesHookRef{}
}
return config, nil
}
func writeHermesConfig(path string, config hermesConfig) error {
out := make(map[string]interface{}, len(config.values)+1)
for key, value := range config.values {
if key != "hooks" {
out[key] = value
}
}
if len(config.hooks) > 0 {
out["hooks"] = config.hooks
}
data, err := yaml.Marshal(out)
if err != nil {
return err
}
return os.WriteFile(path, data, 0600)
}
func mergeHermesEndpointHook(existing []hermesHookRef, ref hermesHookRef) []hermesHookRef {
out := make([]hermesHookRef, 0, len(existing)+1)
for _, item := range existing {
if !hookcmd.IsEndpointHook(item.Command, "hermes") {
out = append(out, item)
}
}
return append(out, ref)
}
func removeHermesEndpointHooks(path string) (bool, error) {
config, err := readHermesConfig(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
changed := false
for eventName, refs := range config.hooks {
filtered := refs[:0]
for _, ref := range refs {
if hookcmd.IsEndpointHook(ref.Command, "hermes") {
changed = true
continue
}
filtered = append(filtered, ref)
}
if len(filtered) == 0 {
delete(config.hooks, eventName)
} else {
config.hooks[eventName] = filtered
}
}
if !changed {
return false, nil
}
if len(config.hooks) == 0 {
delete(config.values, "hooks")
}
if len(config.values) == 0 && len(config.hooks) == 0 {
return true, os.Remove(path)
}
return true, writeHermesConfig(path, config)
}
func isHermesInstalledAt(path string) bool {
config, err := readHermesConfig(path)
if err != nil {
return false
}
for _, refs := range config.hooks {
for _, ref := range refs {
if hookcmd.IsEndpointHook(ref.Command, "hermes") {
return true
}
}
}
return false
}
// hermesConfigPath is the config.yaml a plain `hermes` reads: HERMES_HOME or the native root, then
// the active profile, as hermessession.HomeDir resolves it.
func hermesConfigPath(level Level) (string, error) {
switch level {
case "", LevelUser:
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
dir, err := hermessession.HomeDir(home)
if err != nil {
return "", err
}
return filepath.Join(dir, "config.yaml"), nil
case LevelProject:
return "", fmt.Errorf("Hermes Agent endpoint hooks support user-level config only")
default:
return "", fmt.Errorf("unknown hook level %q", level)
}
}