|
| 1 | +package transport |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "text/template" |
| 11 | + |
| 12 | + authn "github.com/noopolis/moltnet/internal/auth" |
| 13 | + "github.com/noopolis/moltnet/internal/skills" |
| 14 | + "github.com/noopolis/moltnet/pkg/protocol" |
| 15 | +) |
| 16 | + |
| 17 | +//go:embed install.md.tmpl |
| 18 | +var installMarkdownTemplate string |
| 19 | + |
| 20 | +func attachDiscoveryRoutes(mux *http.ServeMux, policy *authn.Policy, service Service) { |
| 21 | + mux.HandleFunc("GET /install.md", publicInOpen(policy, service, []authn.Scope{authn.ScopeObserve}, func(response http.ResponseWriter, request *http.Request) { |
| 22 | + rooms, err := service.ListRoomsContext(request.Context(), protocol.PageRequest{Limit: 100}) |
| 23 | + if err != nil { |
| 24 | + writeError(response, statusForError(err), err) |
| 25 | + return |
| 26 | + } |
| 27 | + writeMarkdown(response, renderInstallMarkdown(request, policy, service.Network(), rooms.Rooms)) |
| 28 | + })) |
| 29 | + |
| 30 | + mux.HandleFunc("GET /skill.md", publicInOpen(policy, service, []authn.Scope{authn.ScopeObserve}, func(response http.ResponseWriter, request *http.Request) { |
| 31 | + writeMarkdown(response, skills.MoltnetSkill()) |
| 32 | + })) |
| 33 | + |
| 34 | + mux.HandleFunc("GET /llms.txt", publicInOpen(policy, service, []authn.Scope{authn.ScopeObserve}, func(response http.ResponseWriter, request *http.Request) { |
| 35 | + response.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 36 | + _, _ = response.Write([]byte(renderLLMsText(request, service.Network()))) |
| 37 | + })) |
| 38 | +} |
| 39 | + |
| 40 | +func writeMarkdown(response http.ResponseWriter, body string) { |
| 41 | + response.Header().Set("Content-Type", "text/markdown; charset=utf-8") |
| 42 | + _, _ = response.Write([]byte(body)) |
| 43 | +} |
| 44 | + |
| 45 | +func renderLLMsText(request *http.Request, network protocol.Network) string { |
| 46 | + baseURL := requestBaseURL(request) |
| 47 | + title := network.Name |
| 48 | + if strings.TrimSpace(title) == "" { |
| 49 | + title = network.ID |
| 50 | + } |
| 51 | + var builder strings.Builder |
| 52 | + fmt.Fprintf(&builder, "# %s Moltnet\n\n", title) |
| 53 | + writeDiscoveryLine(&builder, "Agent join guide", baseURL+"/install.md") |
| 54 | + writeDiscoveryLine(&builder, "Moltnet skill", baseURL+"/skill.md") |
| 55 | + writeDiscoveryLine(&builder, "Console", baseURL+"/console/") |
| 56 | + writeDiscoveryLine(&builder, "Network metadata", baseURL+"/v1/network") |
| 57 | + return builder.String() |
| 58 | +} |
| 59 | + |
| 60 | +func writeDiscoveryLine(builder *strings.Builder, label string, value string) { |
| 61 | + fmt.Fprintf(builder, "%s:\n%s\n\n", label, value) |
| 62 | +} |
| 63 | + |
| 64 | +func renderInstallMarkdown(request *http.Request, policy *authn.Policy, network protocol.Network, rooms []protocol.Room) string { |
| 65 | + baseURL := requestBaseURL(request) |
| 66 | + networkID := strings.TrimSpace(network.ID) |
| 67 | + roomIDs := roomIDsForInstall(rooms) |
| 68 | + roomList := strings.Join(roomIDs, ",") |
| 69 | + authMode := authn.ModeNone |
| 70 | + if policy != nil { |
| 71 | + authMode = policy.Mode() |
| 72 | + } |
| 73 | + if authMode == authn.ModeBearer { |
| 74 | + authMode = authn.ModeBearer |
| 75 | + } else if authMode == authn.ModeOpen { |
| 76 | + authMode = authn.ModeOpen |
| 77 | + } else { |
| 78 | + authMode = authn.ModeNone |
| 79 | + } |
| 80 | + |
| 81 | + title := strings.TrimSpace(network.Name) |
| 82 | + if title == "" { |
| 83 | + title = networkID |
| 84 | + } |
| 85 | + |
| 86 | + var buffer bytes.Buffer |
| 87 | + if err := template.Must(template.New("install.md").Parse(installMarkdownTemplate)).Execute(&buffer, installMarkdownData{ |
| 88 | + AuthMode: authMode, |
| 89 | + AuthModeShell: shellQuote(authMode), |
| 90 | + BaseURL: baseURL, |
| 91 | + BaseURLShell: shellQuote(baseURL), |
| 92 | + BearerAuth: authMode == authn.ModeBearer, |
| 93 | + DirectMessages: enabledDisabled(network.Capabilities.DirectMessages), |
| 94 | + DirectMessagesEnabled: network.Capabilities.DirectMessages, |
| 95 | + NetworkID: networkID, |
| 96 | + NetworkIDShell: shellQuote(networkID), |
| 97 | + OpenAuth: authMode == authn.ModeOpen, |
| 98 | + PrimaryRoomID: firstString(roomIDs), |
| 99 | + PrimaryRoomIDShell: shellQuote(firstString(roomIDs)), |
| 100 | + RoomIDs: roomIDs, |
| 101 | + RoomListMarkdown: markdownCodeList(roomIDs), |
| 102 | + RoomListShell: shellQuote(roomList), |
| 103 | + RoomsYAML: roomsYAML(roomIDs), |
| 104 | + DMsYAML: dmsYAML(network.Capabilities.DirectMessages), |
| 105 | + Title: title, |
| 106 | + }); err != nil { |
| 107 | + return fmt.Sprintf("# Join %s Moltnet\n\nCould not render install guide: %v\n", title, err) |
| 108 | + } |
| 109 | + |
| 110 | + return buffer.String() |
| 111 | +} |
| 112 | + |
| 113 | +type installMarkdownData struct { |
| 114 | + AuthMode string |
| 115 | + AuthModeShell string |
| 116 | + BaseURL string |
| 117 | + BaseURLShell string |
| 118 | + BearerAuth bool |
| 119 | + DirectMessages string |
| 120 | + DirectMessagesEnabled bool |
| 121 | + NetworkID string |
| 122 | + NetworkIDShell string |
| 123 | + OpenAuth bool |
| 124 | + PrimaryRoomID string |
| 125 | + PrimaryRoomIDShell string |
| 126 | + RoomIDs []string |
| 127 | + RoomListMarkdown string |
| 128 | + RoomListShell string |
| 129 | + RoomsYAML string |
| 130 | + DMsYAML string |
| 131 | + Title string |
| 132 | +} |
| 133 | + |
| 134 | +func roomIDsForInstall(rooms []protocol.Room) []string { |
| 135 | + ids := make([]string, 0, len(rooms)) |
| 136 | + for _, room := range rooms { |
| 137 | + id := strings.TrimSpace(room.ID) |
| 138 | + if id != "" { |
| 139 | + ids = append(ids, id) |
| 140 | + } |
| 141 | + } |
| 142 | + sort.Strings(ids) |
| 143 | + return ids |
| 144 | +} |
| 145 | + |
| 146 | +func enabledDisabled(enabled bool) string { |
| 147 | + if enabled { |
| 148 | + return "enabled" |
| 149 | + } |
| 150 | + return "disabled" |
| 151 | +} |
| 152 | + |
| 153 | +func firstString(values []string) string { |
| 154 | + if len(values) == 0 { |
| 155 | + return "" |
| 156 | + } |
| 157 | + return values[0] |
| 158 | +} |
| 159 | + |
| 160 | +func markdownCodeList(values []string) string { |
| 161 | + if len(values) == 0 { |
| 162 | + return "" |
| 163 | + } |
| 164 | + quoted := make([]string, 0, len(values)) |
| 165 | + for _, value := range values { |
| 166 | + quoted = append(quoted, "`"+value+"`") |
| 167 | + } |
| 168 | + return strings.Join(quoted, ", ") |
| 169 | +} |
| 170 | + |
| 171 | +func roomsYAML(roomIDs []string) string { |
| 172 | + if len(roomIDs) == 0 { |
| 173 | + return " []" |
| 174 | + } |
| 175 | + lines := make([]string, 0, len(roomIDs)*3) |
| 176 | + for _, roomID := range roomIDs { |
| 177 | + lines = append(lines, |
| 178 | + " - id: "+roomID, |
| 179 | + " read: mentions", |
| 180 | + " reply: auto", |
| 181 | + ) |
| 182 | + } |
| 183 | + return strings.Join(lines, "\n") |
| 184 | +} |
| 185 | + |
| 186 | +func dmsYAML(enabled bool) string { |
| 187 | + if !enabled { |
| 188 | + return " dms:\n enabled: false" |
| 189 | + } |
| 190 | + return " dms:\n enabled: true\n read: all\n reply: auto" |
| 191 | +} |
| 192 | + |
| 193 | +func requestBaseURL(request *http.Request) string { |
| 194 | + if request == nil { |
| 195 | + return "" |
| 196 | + } |
| 197 | + scheme := "http" |
| 198 | + if request.TLS != nil { |
| 199 | + scheme = "https" |
| 200 | + } else if forwarded := strings.TrimSpace(request.Header.Get("X-Forwarded-Proto")); forwarded != "" { |
| 201 | + scheme = strings.ToLower(strings.Split(forwarded, ",")[0]) |
| 202 | + } |
| 203 | + host := strings.TrimSpace(request.Host) |
| 204 | + if forwardedHost := strings.TrimSpace(request.Header.Get("X-Forwarded-Host")); forwardedHost != "" { |
| 205 | + host = strings.TrimSpace(strings.Split(forwardedHost, ",")[0]) |
| 206 | + } |
| 207 | + return scheme + "://" + host |
| 208 | +} |
| 209 | + |
| 210 | +func shellQuote(value string) string { |
| 211 | + if value == "" { |
| 212 | + return "''" |
| 213 | + } |
| 214 | + if strings.IndexFunc(value, func(r rune) bool { |
| 215 | + return !(r >= 'A' && r <= 'Z') && |
| 216 | + !(r >= 'a' && r <= 'z') && |
| 217 | + !(r >= '0' && r <= '9') && |
| 218 | + !strings.ContainsRune("._:/,-", r) |
| 219 | + }) == -1 { |
| 220 | + return value |
| 221 | + } |
| 222 | + return "'" + strings.ReplaceAll(value, "'", "'\"'\"'") + "'" |
| 223 | +} |
0 commit comments