跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

notify.go

78 行 · 1.9 KB历史下载
1package limaembed
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8 "net"
9 "os"
10 "sync"
11
12 "github.com/lima-vm/lima/v2/pkg/hostagent/events"
13
14 "github.com/sylvinus/agent-vm/internal/desktop"
15)
16
17// eventWatch passes the hostagent's events (one JSON object a line) to w,
18// and tells the user, out of any terminal, of the ports forwarded from the
19// guest: a guest listening on a port gets it on the host's loopback, and a
20// surprise one should not go unseen.
21type eventWatch struct {
22 w io.Writer
23 vm string
24 notify func(title, msg string)
25
26 mu sync.Mutex
27 partial []byte
28}
29
30func newEventWatch(w io.Writer, vm string) *eventWatch {
31 n := desktop.Notify
32 if os.Getenv("AGENT_VM_NOTIFY") == "0" {
33 n = func(string, string) {}
34 }
35 return &eventWatch{w: w, vm: vm, notify: n}
36}
37
38func (e *eventWatch) Write(p []byte) (int, error) {
39 e.mu.Lock()
40 e.partial = append(e.partial, p...)
41 for {
42 i := bytes.IndexByte(e.partial, '\n')
43 if i < 0 {
44 break
45 }
46 e.event(e.partial[:i])
47 e.partial = e.partial[i+1:]
48 }
49 e.mu.Unlock()
50 return e.w.Write(p)
51}
52
53// Preserve file flushing through the notification wrapper. Windows file
54// watchers need it to observe newly appended hostagent status events.
55func (e *eventWatch) Sync() error {
56 if s, ok := e.w.(syncer); ok {
57 return s.Sync()
58 }
59 return nil
60}
61
62func (e *eventWatch) event(line []byte) {
63 var ev events.Event
64 if json.Unmarshal(line, &ev) != nil || ev.Status.PortForward == nil {
65 return
66 }
67 pf := ev.Status.PortForward
68 // Ours (the forwarder builds it), but an address all the same.
69 if _, _, err := net.SplitHostPort(pf.HostAddr); err != nil {
70 return
71 }
72 switch pf.Type {
73 case events.PortForwardEventForwarding:
74 e.notify("agent-vm: "+e.vm, fmt.Sprintf("The VM now serves %s on this machine.", pf.HostAddr))
75 case events.PortForwardEventFailed:
76 e.notify("agent-vm: "+e.vm, fmt.Sprintf("Not forwarded: %s is in use on this machine.", pf.HostAddr))
77 }
78}