feat: implement agent-notify CLI with hooks and tmux passthrough
Add OSC 777 notification sender, Cursor/Claude hook adapters, config/install commands, and README for Ghostty + tmux setup. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Events Events `toml:"events"`
|
||||
Notify Notify `toml:"notify"`
|
||||
}
|
||||
|
||||
type Events struct {
|
||||
Stop bool `toml:"stop"`
|
||||
Idle bool `toml:"idle"`
|
||||
Tool bool `toml:"tool"`
|
||||
}
|
||||
|
||||
type Notify struct {
|
||||
Protocol string `toml:"protocol"`
|
||||
TitleTemplate string `toml:"title_template"`
|
||||
BodyStop string `toml:"body_stop"`
|
||||
BodyIdle string `toml:"body_idle"`
|
||||
BodyTool string `toml:"body_tool"`
|
||||
}
|
||||
|
||||
func Default() Config {
|
||||
return Config{
|
||||
Events: Events{Stop: true, Idle: false, Tool: false},
|
||||
Notify: Notify{
|
||||
Protocol: "osc777",
|
||||
TitleTemplate: "{agent} — {context}",
|
||||
BodyStop: "等待输入",
|
||||
BodyIdle: "空闲 60s+,等待输入",
|
||||
BodyTool: "工具执行完成",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultPath() string {
|
||||
return filepath.Join(os.Getenv("HOME"), ".config", "agent-notify", "config.toml")
|
||||
}
|
||||
|
||||
func Load(path string) (Config, error) {
|
||||
cfg := Default()
|
||||
if path == "" {
|
||||
return cfg, nil
|
||||
}
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return cfg, nil
|
||||
}
|
||||
if _, err := toml.DecodeFile(path, &cfg); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func LoadDefault() (Config, error) {
|
||||
return Load(DefaultPath())
|
||||
}
|
||||
|
||||
func (c Config) EventEnabled(event string) bool {
|
||||
switch strings.ToLower(event) {
|
||||
case "stop":
|
||||
return c.Events.Stop
|
||||
case "idle":
|
||||
return c.Events.Idle
|
||||
case "tool":
|
||||
return c.Events.Tool
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) BodyForEvent(event string) string {
|
||||
switch strings.ToLower(event) {
|
||||
case "idle":
|
||||
return c.Notify.BodyIdle
|
||||
case "tool":
|
||||
return c.Notify.BodyTool
|
||||
default:
|
||||
return c.Notify.BodyStop
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) WriteDefault(path string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
return toml.NewEncoder(f).Encode(Default())
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
cfg := Default()
|
||||
if !cfg.Events.Stop {
|
||||
t.Fatal("expected stop=true by default")
|
||||
}
|
||||
if cfg.Events.Idle {
|
||||
t.Fatal("expected idle=false by default")
|
||||
}
|
||||
if cfg.Notify.Protocol != "osc777" {
|
||||
t.Fatalf("expected osc777, got %q", cfg.Notify.Protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
content := `
|
||||
[events]
|
||||
stop = false
|
||||
tool = true
|
||||
|
||||
[notify]
|
||||
body_stop = "custom stop"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Events.Stop {
|
||||
t.Fatal("expected stop=false")
|
||||
}
|
||||
if !cfg.Events.Tool {
|
||||
t.Fatal("expected tool=true")
|
||||
}
|
||||
if cfg.Notify.BodyStop != "custom stop" {
|
||||
t.Fatalf("got %q", cfg.Notify.BodyStop)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventEnabled(t *testing.T) {
|
||||
cfg := Default()
|
||||
if !cfg.EventEnabled("stop") {
|
||||
t.Fatal("expected stop enabled by default")
|
||||
}
|
||||
if !cfg.EventEnabled("STOP") {
|
||||
t.Fatal("expected case-insensitive match")
|
||||
}
|
||||
cfg.Events.Stop = false
|
||||
if cfg.EventEnabled("stop") {
|
||||
t.Fatal("expected stop disabled")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user