Feature: return web-console remarks from the Go MCP host tools
list_hosts and get_host_detail now include the remark an admin set in the web console, and the name/query filters match it like the Win32 server (name: hostname + remark; query: hostname + remark + IP). The text-only host table gains a remark column, and the MCP instructions stop claiming the Go server keeps no remarks. The remark table is injected through a small Note() interface so the mcp package still does not import web. An optional Notes() snapshot capability is borrowed once per call, so a listing neither takes a read lock per device nor mixes labels across a concurrent save. The design docs are updated to match the new fields and table column. Verified: go build ./..., go vet, gofmt and the full go test ./... pass; new tests cover remark projection, both filters and the snapshot path. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
95d2bbc1b6
commit
5f764b2425
8 files changed
+136
-28
No files matched your search
@@ -142,6 +142,7 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
ctF := strings.ToLower(args.ClientType)
|
||||
|
||||
matched := make([]hub.DeviceInfo, 0, len(pool))
|
||||
remarkOf := s.remarkLookup()
|
||||
for _, d := range pool {
|
||||
group := d.Group
|
||||
if group == "" {
|
||||
@@ -155,12 +156,20 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
if d.Online {
|
||||
clientType = s.clientType(d.ID)
|
||||
}
|
||||
// query matches computer name, remark and IP. The two servers keep no
|
||||
// remark column, so name+IP is the full set here.
|
||||
if q != "" && !strings.Contains(strings.ToLower(d.Name), q) && !strings.Contains(strings.ToLower(ip), q) {
|
||||
remark := ""
|
||||
if remarkOf != nil {
|
||||
remark = remarkOf(d.ID)
|
||||
}
|
||||
// query matches computer name, remark and IP; name matches computer
|
||||
// name and remark, like the Win32 server. The remark is the web
|
||||
// console's label (empty unless an admin set it).
|
||||
if q != "" && !strings.Contains(strings.ToLower(d.Name), q) &&
|
||||
!strings.Contains(strings.ToLower(ip), q) &&
|
||||
!strings.Contains(strings.ToLower(remark), q) {
|
||||
continue
|
||||
}
|
||||
if nameF != "" && !strings.Contains(strings.ToLower(d.Name), nameF) {
|
||||
if nameF != "" && !strings.Contains(strings.ToLower(d.Name), nameF) &&
|
||||
!strings.Contains(strings.ToLower(remark), nameF) {
|
||||
continue
|
||||
}
|
||||
if ipF != "" && !strings.Contains(strings.ToLower(ip), ipF) {
|
||||
@@ -185,7 +194,7 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
end := min(begin+limit, total)
|
||||
hosts := make([]map[string]any, 0, end-begin)
|
||||
for _, d := range matched[begin:end] {
|
||||
hosts = append(hosts, s.hostJSON(d))
|
||||
hosts = append(hosts, s.hostJSON(d, remarkOf))
|
||||
}
|
||||
|
||||
structured := map[string]any{
|
||||
@@ -205,9 +214,11 @@ func (s *Server) callListHosts(_ context.Context, raw json.RawMessage) (any, *rp
|
||||
}
|
||||
|
||||
// hostJSON projects a hub device onto the list_hosts/get_host_detail schema.
|
||||
// Fields the Go server does not know (authStatus, tls, remark, clientType of
|
||||
// an offline entry) are omitted rather than fabricated.
|
||||
func (s *Server) hostJSON(d hub.DeviceInfo) map[string]any {
|
||||
// Fields the Go server does not know (authStatus, tls, clientType of an
|
||||
// offline entry) are omitted rather than fabricated. remark is included when
|
||||
// the web console has one for the device; remarkOf is the per-call snapshot
|
||||
// (nil when no remark table is configured).
|
||||
func (s *Server) hostJSON(d hub.DeviceInfo, remarkOf func(string) string) map[string]any {
|
||||
group := d.Group
|
||||
if group == "" {
|
||||
group = "default"
|
||||
@@ -218,6 +229,11 @@ func (s *Server) hostJSON(d hub.DeviceInfo) map[string]any {
|
||||
"group": group,
|
||||
"online": d.Online,
|
||||
}
|
||||
if remarkOf != nil {
|
||||
if remark := remarkOf(d.ID); remark != "" {
|
||||
h["remark"] = remark
|
||||
}
|
||||
}
|
||||
if d.Version != "" {
|
||||
h["version"] = d.Version
|
||||
}
|
||||
@@ -300,7 +316,7 @@ func (s *Server) callGetHostDetail(_ context.Context, raw json.RawMessage) (any,
|
||||
return nil, errf(CodeInvalidParams, "Missing required parameter: id or ids")
|
||||
}
|
||||
online, offline := s.hostMaps()
|
||||
host, rerr := s.lookupHost(args.ID, online, offline)
|
||||
host, rerr := s.lookupHost(args.ID, online, offline, s.remarkLookup())
|
||||
if rerr != nil {
|
||||
return nil, rerr
|
||||
}
|
||||
@@ -313,10 +329,11 @@ func (s *Server) hostDetailBatch(ids []string) (any, *rpcError) {
|
||||
"Too many ids: %d (max %d per batch)", len(ids), batchMaxIDs)
|
||||
}
|
||||
online, offline := s.hostMaps()
|
||||
remarkOf := s.remarkLookup() // one snapshot for the whole batch
|
||||
results := make([]map[string]any, 0, len(ids))
|
||||
succeeded, failed := 0, 0
|
||||
for _, id := range ids {
|
||||
host, rerr := s.lookupHost(id, online, offline)
|
||||
host, rerr := s.lookupHost(id, online, offline, remarkOf)
|
||||
if rerr != nil {
|
||||
results = append(results, map[string]any{
|
||||
"id": id,
|
||||
@@ -375,17 +392,17 @@ func (s *Server) hostMaps() (online, offline map[string]hub.DeviceInfo) {
|
||||
// lookupHost resolves one host: online first, then the last-known history
|
||||
// record. It mirrors the Win32 server, which answers offline hosts with their
|
||||
// final snapshot instead of an error.
|
||||
func (s *Server) lookupHost(id string, online, offline map[string]hub.DeviceInfo) (map[string]any, *rpcError) {
|
||||
func (s *Server) lookupHost(id string, online, offline map[string]hub.DeviceInfo, remarkOf func(string) string) (map[string]any, *rpcError) {
|
||||
if !isDecimalID(id) {
|
||||
return nil, errf(CodeInvalidParams, "Invalid id: expected a decimal host id string")
|
||||
}
|
||||
if d, ok := online[id]; ok {
|
||||
h := s.hostJSON(d)
|
||||
h := s.hostJSON(d, remarkOf)
|
||||
h["capabilities"] = s.capabilities(d)
|
||||
return h, nil
|
||||
}
|
||||
if d, ok := offline[id]; ok {
|
||||
return s.hostJSON(d), nil
|
||||
return s.hostJSON(d, remarkOf), nil
|
||||
}
|
||||
return nil, errf(CodeOffline, "Host not found or offline: %s", id)
|
||||
}
|
||||
@@ -449,13 +466,13 @@ func appendHostRows(text string, hosts []map[string]any) string {
|
||||
for _, h := range hosts {
|
||||
online, _ := h["online"].(bool)
|
||||
rows = append(rows, []string{
|
||||
tableText(h["id"]), tableText(h["name"]), tableText(h["ip"]),
|
||||
tableText(h["group"]), tableText(h["os"]), strconv.FormatBool(online),
|
||||
tableText(h["clientType"]), tableText(h["last_seen"]),
|
||||
tableText(h["id"]), tableText(h["name"]), tableText(h["remark"]),
|
||||
tableText(h["ip"]), tableText(h["group"]), tableText(h["os"]),
|
||||
strconv.FormatBool(online), tableText(h["clientType"]), tableText(h["last_seen"]),
|
||||
})
|
||||
}
|
||||
return appendRows(text,
|
||||
[]string{"id", "name", "ip", "group", "os", "online", "clientType", "last_seen"},
|
||||
[]string{"id", "name", "remark", "ip", "group", "os", "online", "clientType", "last_seen"},
|
||||
rows)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user