Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

13.6 KB
Raw Blame
package hooks

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"

	"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/hookcmd"
)

type DevinOptions struct {
	Level    Level
	LogPath  string
	UserMode bool
}

type DevinDesktopOptions struct {
	Level    Level
	LogPath  string
	UserMode bool
}

type DevinStatus struct {
	Installed  bool   `json:"installed"`
	BinaryPath string `json:"binary_path,omitempty"`
	ConfigPath string `json:"config_path,omitempty"`
	Message    string `json:"message,omitempty"`
}

type devinHookGroup struct {
	Matcher string         `json:"matcher,omitempty"`
	Hooks   []devinHookRef `json:"hooks"`
}

type devinHookRef struct {
	Type    string `json:"type"`
	Command string `json:"command"`
	Timeout int    `json:"timeout,omitempty"`
}

type windsurfHookRef struct {
	Command    string `json:"command"`
	ShowOutput *bool  `json:"show_output,omitempty"`
}

type windsurfHooksFile struct {
	values map[string]json.RawMessage
	hooks  map[string][]windsurfHookRef
}

type devinConfig struct {
	values     map[string]json.RawMessage
	hooks      map[string][]devinHookGroup
	standalone bool
}

var devinRuntime = hookRuntime{
	displayName: "Devin CLI",
	configPath:  devinConfigPath,
	install:     installDevinCLIHooks,
	uninstall:   removeDevinCLIEndpointHooks,
	isInstalled: isDevinCLIInstalledAt,
}

var devinDesktopRuntime = hookRuntime{
	displayName: "Devin Desktop via Cascade/Windsurf",
	configPath:  devinDesktopConfigPath,
	install:     installDevinDesktopHooks,
	uninstall:   removeDevinDesktopEndpointHooks,
	isInstalled: isDevinDesktopInstalledAt,
}

func InstallDevin(opts DevinOptions) (DevinStatus, error) {
	status, err := installRuntimeHooks(devinRuntime, RuntimeOptions(opts))
	if err != nil {
		return DevinStatus{}, err
	}
	return devinStatusFromRuntime(status), nil
}

func UninstallDevin(opts DevinOptions) (DevinStatus, error) {
	status, err := uninstallRuntimeHooks(devinRuntime, RuntimeOptions(opts))
	if err != nil {
		return DevinStatus{}, err
	}
	return devinStatusFromRuntime(status), nil
}

func DevinHookStatus(opts DevinOptions) DevinStatus {
	return devinStatusFromRuntime(runtimeHookStatus(devinRuntime, RuntimeOptions(opts)))
}

func IsDevinInstalled(opts DevinOptions) bool {
	return isRuntimeInstalled(devinRuntime, RuntimeOptions(opts))
}

func InstallDevinDesktop(opts DevinDesktopOptions) (DevinStatus, error) {
	status, err := installRuntimeHooks(devinDesktopRuntime, RuntimeOptions(opts))
	if err != nil {
		return DevinStatus{}, err
	}
	return devinStatusFromRuntime(status), nil
}

func UninstallDevinDesktop(opts DevinDesktopOptions) (DevinStatus, error) {
	status, err := uninstallRuntimeHooks(devinDesktopRuntime, RuntimeOptions(opts))
	if err != nil {
		return DevinStatus{}, err
	}
	return devinStatusFromRuntime(status), nil
}

func DevinDesktopHookStatus(opts DevinDesktopOptions) DevinStatus {
	return devinStatusFromRuntime(runtimeHookStatus(devinDesktopRuntime, RuntimeOptions(opts)))
}

func devinStatusFromRuntime(status runtimeStatus) DevinStatus {
	return DevinStatus{
		Installed:  status.Installed,
		BinaryPath: status.BinaryPath,
		ConfigPath: status.ConfigPath,
		Message:    status.Message,
	}
}

func installDevinCLIHooks(path, binaryPath, logPath, configPath string) error {
	return installDevinHooksForPlatform(path, binaryPath, logPath, configPath, "devin-cli", []string{"devin", "devin-cli"})
}

func installDevinDesktopHooks(path, binaryPath, logPath, configPath string) error {
	config, err := readWindsurfHooks(path)
	if err != nil {
		return err
	}
	prefix := endpointCommandPrefix("devin-desktop", binaryPath, logPath, configPath)
	hideOutput := false
	endpointHooks := map[string]windsurfHookRef{
		"pre_user_prompt":   {Command: prefix + " prompt-submit", ShowOutput: &hideOutput},
		"post_write_code":   {Command: prefix + " post-tool", ShowOutput: &hideOutput},
		"post_run_command":  {Command: prefix + " post-tool", ShowOutput: &hideOutput},
		"post_mcp_tool_use": {Command: prefix + " post-tool", ShowOutput: &hideOutput},
		"post_read_code":    {Command: prefix + " post-tool", ShowOutput: &hideOutput},
		// The only Cascade hook that carries the agent's reply.
		"post_cascade_response": {Command: prefix + " cascade-response", ShowOutput: &hideOutput},
	}
	for eventName, hook := range endpointHooks {
		config.hooks[eventName] = mergeWindsurfEndpointHook(config.hooks[eventName], hook, "devin-desktop")
	}
	data, err := config.marshal()
	if err != nil {
		return err
	}
	return os.WriteFile(path, data, 0600)
}

func installDevinHooks(path, binaryPath, logPath, configPath string) error {
	return installDevinHooksForPlatform(path, binaryPath, logPath, configPath, "devin", []string{"devin"})
}

func installDevinHooksForPlatform(path, binaryPath, logPath, configPath, platform string, replacePlatforms []string) error {
	config, err := readDevinConfig(path)
	if err != nil {
		return err
	}
	prefix := endpointCommandPrefix(platform, binaryPath, logPath, configPath)
	endpointHooks := map[string]devinHookGroup{
		"SessionStart":      {Hooks: []devinHookRef{{Type: "command", Command: prefix + " session-start"}}},
		"UserPromptSubmit":  {Hooks: []devinHookRef{{Type: "command", Command: prefix + " prompt-submit", Timeout: 30}}},
		"PreToolUse":        {Matcher: "", Hooks: []devinHookRef{{Type: "command", Command: prefix + " pre-tool"}}},
		"PermissionRequest": {Matcher: "", Hooks: []devinHookRef{{Type: "command", Command: prefix + " permission-request"}}},
		"PostToolUse":       {Matcher: "", Hooks: []devinHookRef{{Type: "command", Command: prefix + " post-tool"}}},
		"Stop":              {Hooks: []devinHookRef{{Type: "command", Command: prefix + " stop", Timeout: 45}}},
		"SessionEnd":        {Hooks: []devinHookRef{{Type: "command", Command: prefix + " session-end"}}},
	}
	for eventName, group := range endpointHooks {
		config.hooks[eventName] = mergeDevinEndpointHook(config.hooks[eventName], group, replacePlatforms...)
	}
	data, err := config.marshal()
	if err != nil {
		return err
	}
	return os.WriteFile(path, data, 0600)
}

func readDevinConfig(path string) (devinConfig, error) {
	config := devinConfig{
		values:     map[string]json.RawMessage{},
		hooks:      map[string][]devinHookGroup{},
		standalone: filepath.Base(path) == "hooks.v1.json",
	}
	data, err := os.ReadFile(path)
	if err == nil {
		if config.standalone {
			if err := json.Unmarshal(data, &config.hooks); err != nil {
				return devinConfig{}, err
			}
		} else {
			if err := json.Unmarshal(data, &config.values); err != nil {
				return devinConfig{}, err
			}
			if rawHooks, ok := config.values["hooks"]; ok {
				if err := json.Unmarshal(rawHooks, &config.hooks); err != nil {
					return devinConfig{}, err
				}
			}
		}
	} else if !os.IsNotExist(err) {
		return devinConfig{}, err
	}
	if config.hooks == nil {
		config.hooks = map[string][]devinHookGroup{}
	}
	return config, nil
}

func readWindsurfHooks(path string) (windsurfHooksFile, error) {
	config := windsurfHooksFile{
		values: map[string]json.RawMessage{},
		hooks:  map[string][]windsurfHookRef{},
	}
	data, err := os.ReadFile(path)
	if err == nil {
		if err := json.Unmarshal(data, &config.values); err != nil {
			return windsurfHooksFile{}, err
		}
		if rawHooks, ok := config.values["hooks"]; ok {
			if err := json.Unmarshal(rawHooks, &config.hooks); err != nil {
				return windsurfHooksFile{}, err
			}
		}
	} else if !os.IsNotExist(err) {
		return windsurfHooksFile{}, err
	}
	if config.hooks == nil {
		config.hooks = map[string][]windsurfHookRef{}
	}
	return config, nil
}

func (config windsurfHooksFile) marshal() ([]byte, error) {
	out := make(map[string]json.RawMessage, len(config.values)+1)
	for key, value := range config.values {
		if key != "hooks" {
			out[key] = value
		}
	}
	if len(config.hooks) > 0 {
		data, err := json.Marshal(config.hooks)
		if err != nil {
			return nil, err
		}
		out["hooks"] = data
	}
	return json.MarshalIndent(out, "", "  ")
}

func (config devinConfig) marshal() ([]byte, error) {
	if config.standalone {
		if len(config.hooks) == 0 {
			return []byte("{}"), nil
		}
		return json.MarshalIndent(config.hooks, "", "  ")
	}
	out := make(map[string]json.RawMessage, len(config.values)+1)
	for key, value := range config.values {
		if key != "hooks" {
			out[key] = value
		}
	}
	if len(config.hooks) > 0 {
		data, err := json.Marshal(config.hooks)
		if err != nil {
			return nil, err
		}
		out["hooks"] = data
	}
	return json.MarshalIndent(out, "", "  ")
}

func mergeDevinEndpointHook(existing []devinHookGroup, group devinHookGroup, platforms ...string) []devinHookGroup {
	out := make([]devinHookGroup, 0, len(existing)+1)
	for _, item := range existing {
		filtered, changed := filterDevinEndpointHooks(item, platforms...)
		if !changed || len(filtered.Hooks) > 0 {
			out = append(out, item)
			if changed {
				out[len(out)-1] = filtered
			}
		}
	}
	return append(out, group)
}

func mergeWindsurfEndpointHook(existing []windsurfHookRef, hook windsurfHookRef, platform string) []windsurfHookRef {
	out := make([]windsurfHookRef, 0, len(existing)+1)
	for _, item := range existing {
		if isWindsurfEndpointHookCommand(item.Command, platform) {
			continue
		}
		out = append(out, item)
	}
	return append(out, hook)
}

func removeDevinEndpointHooks(path string) (bool, error) {
	return removeDevinEndpointHooksForPlatforms(path, "devin")
}

func removeDevinCLIEndpointHooks(path string) (bool, error) {
	return removeDevinEndpointHooksForPlatforms(path, "devin", "devin-cli")
}

func removeDevinDesktopEndpointHooks(path string) (bool, error) {
	config, err := readWindsurfHooks(path)
	if err != nil {
		if os.IsNotExist(err) {
			return false, nil
		}
		return false, err
	}
	changed := false
	for eventName, hooks := range config.hooks {
		filtered := hooks[:0]
		for _, hook := range hooks {
			if isWindsurfEndpointHookCommand(hook.Command, "devin-desktop") {
				changed = true
				continue
			}
			filtered = append(filtered, hook)
		}
		if len(filtered) == 0 {
			delete(config.hooks, eventName)
		} else {
			config.hooks[eventName] = filtered
		}
	}
	if !changed {
		return false, nil
	}
	out, err := config.marshal()
	if err != nil {
		return false, err
	}
	return true, os.WriteFile(path, out, 0600)
}

func removeDevinEndpointHooksForPlatforms(path string, platforms ...string) (bool, error) {
	config, err := readDevinConfig(path)
	if err != nil {
		if os.IsNotExist(err) {
			return false, nil
		}
		return false, err
	}
	changed := false
	for eventName, groups := range config.hooks {
		filtered := groups[:0]
		for _, group := range groups {
			withoutEndpointHooks, groupChanged := filterDevinEndpointHooks(group, platforms...)
			if groupChanged {
				changed = true
			}
			if len(withoutEndpointHooks.Hooks) == 0 {
				continue
			}
			filtered = append(filtered, withoutEndpointHooks)
		}
		if len(filtered) == 0 {
			delete(config.hooks, eventName)
		} else {
			config.hooks[eventName] = filtered
		}
	}
	if !changed {
		return false, nil
	}
	if config.standalone && len(config.hooks) == 0 {
		return true, os.Remove(path)
	}
	out, err := config.marshal()
	if err != nil {
		return false, err
	}
	return true, os.WriteFile(path, out, 0600)
}

func isDevinEndpointHookGroup(group devinHookGroup, platforms ...string) bool {
	for _, hook := range group.Hooks {
		if isDevinEndpointHookCommand(hook.Command, platforms...) {
			return true
		}
	}
	return false
}

func filterDevinEndpointHooks(group devinHookGroup, platforms ...string) (devinHookGroup, bool) {
	filtered := group
	filtered.Hooks = group.Hooks[:0]
	changed := false
	for _, hook := range group.Hooks {
		if isDevinEndpointHookCommand(hook.Command, platforms...) {
			changed = true
			continue
		}
		filtered.Hooks = append(filtered.Hooks, hook)
	}
	return filtered, changed
}

func isDevinInstalledAt(path string) bool {
	return isDevinInstalledAtPlatforms(path, "devin")
}

func isDevinCLIInstalledAt(path string) bool {
	return isDevinInstalledAtPlatforms(path, "devin", "devin-cli")
}

func isDevinDesktopInstalledAt(path string) bool {
	config, err := readWindsurfHooks(path)
	if err != nil {
		return false
	}
	for _, hooks := range config.hooks {
		for _, hook := range hooks {
			if isWindsurfEndpointHookCommand(hook.Command, "devin-desktop") {
				return true
			}
		}
	}
	return false
}

func isDevinInstalledAtPlatforms(path string, platforms ...string) bool {
	config, err := readDevinConfig(path)
	if err != nil {
		return false
	}
	for _, groups := range config.hooks {
		for _, group := range groups {
			if isDevinEndpointHookGroup(group, platforms...) {
				return true
			}
		}
	}
	return false
}

func isDevinEndpointHookCommand(command string, platforms ...string) bool {
	if len(platforms) == 0 {
		platforms = []string{"devin"}
	}
	for _, platform := range platforms {
		if hookcmd.IsEndpointHook(command, platform) {
			return true
		}
	}
	return false
}

func isWindsurfEndpointHookCommand(command, platform string) bool {
	return hookcmd.IsEndpointHook(command, platform)
}

func devinConfigPath(level Level) (string, error) {
	switch level {
	case LevelProject:
		cwd, err := os.Getwd()
		if err != nil {
			return "", err
		}
		return filepath.Join(cwd, ".devin", "hooks.v1.json"), nil
	case "", LevelUser:
		home, err := os.UserHomeDir()
		if err != nil {
			return "", err
		}
		return filepath.Join(home, ".config", "devin", "config.json"), nil
	default:
		return "", fmt.Errorf("unknown hook level %q", level)
	}
}

func devinDesktopConfigPath(level Level) (string, error) {
	switch level {
	case LevelProject:
		cwd, err := os.Getwd()
		if err != nil {
			return "", err
		}
		return filepath.Join(cwd, ".windsurf", "hooks.json"), nil
	case "", LevelUser:
		home, err := os.UserHomeDir()
		if err != nil {
			return "", err
		}
		return filepath.Join(home, ".codeium", "windsurf", "hooks.json"), nil
	default:
		return "", fmt.Errorf("unknown hook level %q", level)
	}
}