feat: fix Cursor hook delivery and improve notify workflow
Route hook notifications through /dev/tty and client_tty so OSC reaches Ghostty when Cursor captures stdout. Add afterAgentResponse hook, hook logging, debounce, split test modes, and use directory name for titles. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+45
-4
@@ -1,24 +1,39 @@
|
||||
package hook
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/longbin/agent-notify/internal/config"
|
||||
"github.com/longbin/agent-notify/internal/context"
|
||||
"github.com/longbin/agent-notify/internal/logx"
|
||||
"github.com/longbin/agent-notify/internal/notify"
|
||||
"github.com/longbin/agent-notify/internal/tmux"
|
||||
)
|
||||
|
||||
type cursorPayload struct {
|
||||
WorkspaceRoots []string `json:"workspace_roots"`
|
||||
HookEventName string `json:"hook_event_name"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func RunCursor(r io.Reader, cfg config.Config, event string, _ io.Writer) error {
|
||||
var payload cursorPayload
|
||||
fromHook := !isInteractiveStdin(r)
|
||||
decodeJSON(r, &payload)
|
||||
|
||||
hookName := payload.HookEventName
|
||||
if hookName == "" {
|
||||
hookName = event
|
||||
}
|
||||
clientTTY, _ := tmux.ClientTTY()
|
||||
logx.Append("hook cursor event=%s hook_event_name=%s status=%s enabled=%v from_hook=%v stdout_tty=%v client_tty=%q",
|
||||
event, hookName, payload.Status, cfg.EventEnabled(event), fromHook, stdoutIsTerminal(), clientTTY)
|
||||
|
||||
if !cfg.EventEnabled(event) {
|
||||
logx.Append("hook cursor event=%s skipped (disabled in config)", event)
|
||||
return nil
|
||||
}
|
||||
var payload cursorPayload
|
||||
_ = json.NewDecoder(r).Decode(&payload)
|
||||
|
||||
meta := context.MetaFromEnv("Cursor", event)
|
||||
if len(payload.WorkspaceRoots) > 0 {
|
||||
@@ -26,5 +41,31 @@ func RunCursor(r io.Reader, cfg config.Config, event string, _ io.Writer) error
|
||||
}
|
||||
title := context.Render(cfg.Notify.TitleTemplate, meta)
|
||||
body := cfg.BodyForEvent(event)
|
||||
return notify.SendAuto(cfg.Notify.Protocol, title, body)
|
||||
|
||||
if shouldDebounce(title) {
|
||||
logx.Append("hook cursor event=%s skipped (debounced duplicate)", event)
|
||||
return nil
|
||||
}
|
||||
|
||||
var result notify.SendResult
|
||||
var err error
|
||||
if fromHook {
|
||||
result, err = notify.SendForHookWithResult(cfg.Notify.Protocol, title, body)
|
||||
} else {
|
||||
result, err = notify.SendAutoWithResult(cfg.Notify.Protocol, title, body)
|
||||
}
|
||||
if err != nil {
|
||||
logx.Append("hook cursor event=%s send FAILED: %v", event, err)
|
||||
return err
|
||||
}
|
||||
logx.Append("hook cursor event=%s send OK via %s title=%q", event, result.Method, title)
|
||||
return nil
|
||||
}
|
||||
|
||||
func stdoutIsTerminal() bool {
|
||||
fi, err := os.Stdout.Stat()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return fi.Mode()&os.ModeCharDevice != 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user