feat: add inbox
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
type Config struct {
|
||||
Events Events `toml:"events"`
|
||||
Notify Notify `toml:"notify"`
|
||||
Inbox Inbox `toml:"inbox"`
|
||||
}
|
||||
|
||||
type Events struct {
|
||||
@@ -28,6 +29,15 @@ type Notify struct {
|
||||
BodyTool string `toml:"body_tool"`
|
||||
}
|
||||
|
||||
type Inbox struct {
|
||||
Enabled bool `toml:"enabled"`
|
||||
Socket string `toml:"socket"`
|
||||
RemoteSocket string `toml:"remote_socket"`
|
||||
Addr string `toml:"addr"`
|
||||
FallbackLocal bool `toml:"fallback_local"`
|
||||
TimeoutMS int `toml:"timeout_ms"`
|
||||
}
|
||||
|
||||
func Default() Config {
|
||||
return Config{
|
||||
Events: Events{Stop: true, Response: true, Idle: false, Tool: false},
|
||||
@@ -38,9 +48,32 @@ func Default() Config {
|
||||
BodyIdle: "空闲 60s+,等待输入",
|
||||
BodyTool: "工具执行完成",
|
||||
},
|
||||
Inbox: Inbox{
|
||||
Enabled: true,
|
||||
Socket: DefaultInboxSocket(),
|
||||
RemoteSocket: DefaultInboxRemoteSocket(),
|
||||
Addr: "127.0.0.1:17777",
|
||||
FallbackLocal: true,
|
||||
TimeoutMS: 500,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultInboxRemoteSocket() string {
|
||||
user := os.Getenv("USER")
|
||||
if user == "" {
|
||||
user = "user"
|
||||
}
|
||||
return filepath.Join("/tmp", "agent-notify-"+user+".sock")
|
||||
}
|
||||
|
||||
func DefaultInboxSocket() string {
|
||||
if runtimeDir := os.Getenv("XDG_RUNTIME_DIR"); runtimeDir != "" {
|
||||
return filepath.Join(runtimeDir, "agent-notify.sock")
|
||||
}
|
||||
return filepath.Join(os.Getenv("HOME"), ".local", "state", "agent-notify", "agent-notify.sock")
|
||||
}
|
||||
|
||||
func DefaultPath() string {
|
||||
return filepath.Join(os.Getenv("HOME"), ".config", "agent-notify", "config.toml")
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
t.Setenv("USER", "example")
|
||||
cfg := Default()
|
||||
if !cfg.Events.Stop {
|
||||
t.Fatal("expected stop=true by default")
|
||||
@@ -17,6 +18,24 @@ func TestDefaultConfig(t *testing.T) {
|
||||
if cfg.Notify.Protocol != "osc777" {
|
||||
t.Fatalf("expected osc777, got %q", cfg.Notify.Protocol)
|
||||
}
|
||||
if !cfg.Inbox.Enabled {
|
||||
t.Fatal("expected inbox enabled by default")
|
||||
}
|
||||
if cfg.Inbox.Addr != "127.0.0.1:17777" {
|
||||
t.Fatalf("expected default inbox addr, got %q", cfg.Inbox.Addr)
|
||||
}
|
||||
if cfg.Inbox.Socket == "" {
|
||||
t.Fatal("expected default inbox socket")
|
||||
}
|
||||
if cfg.Inbox.RemoteSocket != "/tmp/agent-notify-example.sock" {
|
||||
t.Fatalf("expected default remote socket, got %q", cfg.Inbox.RemoteSocket)
|
||||
}
|
||||
if !cfg.Inbox.FallbackLocal {
|
||||
t.Fatal("expected local fallback enabled by default")
|
||||
}
|
||||
if cfg.Inbox.TimeoutMS != 500 {
|
||||
t.Fatalf("expected 500ms timeout, got %d", cfg.Inbox.TimeoutMS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromFile(t *testing.T) {
|
||||
@@ -29,6 +48,14 @@ tool = true
|
||||
|
||||
[notify]
|
||||
body_stop = "custom stop"
|
||||
|
||||
[inbox]
|
||||
enabled = false
|
||||
socket = "/tmp/custom-agent-notify.sock"
|
||||
remote_socket = "/tmp/custom-remote-agent-notify.sock"
|
||||
addr = "127.0.0.1:18888"
|
||||
fallback_local = false
|
||||
timeout_ms = 250
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -46,6 +73,35 @@ body_stop = "custom stop"
|
||||
if cfg.Notify.BodyStop != "custom stop" {
|
||||
t.Fatalf("got %q", cfg.Notify.BodyStop)
|
||||
}
|
||||
if cfg.Inbox.Enabled {
|
||||
t.Fatal("expected inbox disabled")
|
||||
}
|
||||
if cfg.Inbox.Socket != "/tmp/custom-agent-notify.sock" {
|
||||
t.Fatalf("got inbox socket %q", cfg.Inbox.Socket)
|
||||
}
|
||||
if cfg.Inbox.RemoteSocket != "/tmp/custom-remote-agent-notify.sock" {
|
||||
t.Fatalf("got inbox remote socket %q", cfg.Inbox.RemoteSocket)
|
||||
}
|
||||
if cfg.Inbox.Addr != "127.0.0.1:18888" {
|
||||
t.Fatalf("got inbox addr %q", cfg.Inbox.Addr)
|
||||
}
|
||||
if cfg.Inbox.FallbackLocal {
|
||||
t.Fatal("expected fallback_local=false")
|
||||
}
|
||||
if cfg.Inbox.TimeoutMS != 250 {
|
||||
t.Fatalf("got timeout %d", cfg.Inbox.TimeoutMS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultInboxSocketPrefersXDGRuntimeDir(t *testing.T) {
|
||||
t.Setenv("XDG_RUNTIME_DIR", "/run/user/1234")
|
||||
t.Setenv("HOME", "/home/example")
|
||||
|
||||
got := DefaultInboxSocket()
|
||||
want := "/run/user/1234/agent-notify.sock"
|
||||
if got != want {
|
||||
t.Fatalf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventEnabled(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user