-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappstate.go
More file actions
115 lines (98 loc) · 2.35 KB
/
Copy pathappstate.go
File metadata and controls
115 lines (98 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"log/slog"
"os"
"sync"
"github.com/gen2brain/beeep"
"github.com/wailsapp/wails/v3/pkg/application"
)
type AppState struct {
mu sync.Mutex
wailsApp *application.App
window *application.WebviewWindow
systray *application.SystemTray
logger *slog.Logger
iconPath string
sessionChan chan Session
answerChan chan SessionAnswer
done chan struct{}
timeoutChan chan struct{}
pending bool
}
func NewAppState(logger *slog.Logger) *AppState {
return &AppState{
logger: logger,
sessionChan: make(chan Session, 1),
answerChan: make(chan SessionAnswer),
done: make(chan struct{}),
timeoutChan: make(chan struct{}, 1),
}
}
func (s *AppState) SetPending(v bool) {
s.mu.Lock()
s.pending = v
s.mu.Unlock()
}
func (s *AppState) IsPending() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.pending
}
func (s *AppState) SubmitAnswers(answer SessionAnswer) {
s.SetPending(false)
s.systray.SetIcon(trayIdle)
s.answerChan <- answer
}
func (s *AppState) ServiceStartup(ctx context.Context, opts application.ServiceOptions) error {
s.iconPath = writeTempIcon(s.logger)
go runMCPServer(s, s.logger)
go s.dispatchSessions()
go s.watchTimeouts()
go s.watchDone()
s.logger.Info("service started")
return nil
}
func (s *AppState) ServiceShutdown() error {
if s.iconPath != "" {
os.Remove(s.iconPath)
}
s.logger.Info("service shutting down")
return nil
}
func writeTempIcon(logger *slog.Logger) string {
f, err := os.CreateTemp("", "askd-notify-*.png")
if err != nil {
logger.Warn("failed to create temp icon for notifications", "error", err)
return ""
}
if _, err := f.Write(trayPending); err != nil {
f.Close()
os.Remove(f.Name())
logger.Warn("failed to write temp icon", "error", err)
return ""
}
f.Close()
return f.Name()
}
func (s *AppState) dispatchSessions() {
for session := range s.sessionChan {
s.SetPending(true)
s.systray.SetIcon(trayPending)
if err := beeep.Notify("askd", "New question from agent", s.iconPath); err != nil {
s.logger.Warn("beeep notify failed", "error", err)
}
s.window.EmitEvent("new-session", session)
}
}
func (s *AppState) watchTimeouts() {
for range s.timeoutChan {
s.SetPending(false)
s.systray.SetIcon(trayIdle)
s.window.EmitEvent("session-timeout")
}
}
func (s *AppState) watchDone() {
<-s.done
s.wailsApp.Quit()
}