|
1 | 1 | package modules
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "fmt" |
4 | 6 | "log/slog"
|
5 | 7 | "strings"
|
6 | 8 |
|
@@ -101,57 +103,94 @@ func (r *ModuleRegistry) RegisterPrompts(mcpServer *server.MCPServer) error {
|
101 | 103 | }
|
102 | 104 |
|
103 | 105 | func registerPrompts(moduleID string, cfg *config.Config, mcpServer *server.MCPServer) error {
|
104 |
| - // Get module-specific prompts |
105 |
| - modulePrompts, err := prompts.GetModulePrompts(prompts.PromptFiles, strings.ToLower(moduleID), cfg.Internal) |
106 |
| - if err != nil { |
107 |
| - return err |
108 |
| - } |
109 |
| - |
110 |
| - if len(modulePrompts) == 0 { |
111 |
| - // No prompts for this module |
112 |
| - return nil |
113 |
| - } |
114 |
| - |
115 |
| - // Combine all prompt contents into a single prompt |
116 |
| - var combinedContent strings.Builder |
117 |
| - |
118 |
| - // Use the first prompt's metadata for description and result description if available |
119 |
| - description := "" |
120 |
| - if len(modulePrompts) > 0 && modulePrompts[0].Metadata.Description != "" { |
121 |
| - description = modulePrompts[0].Metadata.Description |
122 |
| - } |
123 |
| - |
124 |
| - resultDescription := "" |
125 |
| - if len(modulePrompts) > 0 && modulePrompts[0].Metadata.ResultDescription != "" { |
126 |
| - resultDescription = modulePrompts[0].Metadata.ResultDescription |
127 |
| - } |
128 |
| - |
129 |
| - // Combine all prompt contents |
130 |
| - for i, promptFile := range modulePrompts { |
131 |
| - if promptFile.Content != "" { |
132 |
| - if i > 0 { |
133 |
| - combinedContent.WriteString("\n\n") |
134 |
| - } |
135 |
| - combinedContent.WriteString(promptFile.Content) |
136 |
| - } |
137 |
| - } |
138 |
| - |
139 |
| - // Create a single MCP prompt with the module ID as the name |
140 |
| - mcpPrompt := p.NewPrompt(). |
141 |
| - SetName(moduleID). // Use moduleID as the prompt name |
142 |
| - SetDescription(description). |
143 |
| - SetResultDescription(resultDescription). |
144 |
| - SetText(combinedContent.String()). |
145 |
| - Build() |
146 |
| - |
147 |
| - // Create a Prompts collection with the single prompt |
148 |
| - mcpPrompts := p.Prompts{} |
149 |
| - mcpPrompts.Append(mcpPrompt) |
150 |
| - |
151 |
| - slog.Info("Registering prompt for module", "module", moduleID, "contentLength", len(combinedContent.String())) |
152 |
| - |
153 |
| - // Register the prompt with the MCP server |
154 |
| - p.AddPrompts(mcpPrompts, mcpServer) |
155 |
| - |
156 |
| - return nil |
| 106 | + // Create a map to store prompts by mode |
| 107 | + modulePromptsByMode := map[string][]prompts.PromptFile{ |
| 108 | + string(p.Standard): {}, |
| 109 | + string(p.Architect): {}, |
| 110 | + } |
| 111 | + |
| 112 | + // Get module-specific prompts for standard mode |
| 113 | + standardPrompts, err := prompts.GetModulePrompts(prompts.PromptFiles, strings.ToLower(moduleID), cfg.Internal, string(p.Standard)) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("failed to get standard prompts for module %s: %v", moduleID, err) |
| 116 | + } |
| 117 | + modulePromptsByMode[string(p.Standard)] = standardPrompts |
| 118 | + |
| 119 | + // Get module-specific prompts for architect mode (only for internal) |
| 120 | + if cfg.Internal { |
| 121 | + architectPrompts, err := prompts.GetModulePrompts(prompts.PromptFiles, strings.ToLower(moduleID), cfg.Internal, string(p.Architect)) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("failed to get architect prompts for module %s: %v", moduleID, err) |
| 124 | + } |
| 125 | + modulePromptsByMode[string(p.Architect)] = architectPrompts |
| 126 | + } |
| 127 | + |
| 128 | + // Check if we have any prompts for this module |
| 129 | + totalPrompts := len(modulePromptsByMode[string(p.Standard)]) + len(modulePromptsByMode[string(p.Architect)]) |
| 130 | + if totalPrompts == 0 { |
| 131 | + // No prompts for this module |
| 132 | + return nil |
| 133 | + } |
| 134 | + |
| 135 | + // Create a map to store combined content for each mode |
| 136 | + modeContents := make(map[string]string) |
| 137 | + |
| 138 | + // Get description and result description from the first available prompt |
| 139 | + description := "" |
| 140 | + resultDescription := "" |
| 141 | + |
| 142 | + // Process each mode separately to build the content map |
| 143 | + for mode, modePrompts := range modulePromptsByMode { |
| 144 | + if len(modePrompts) == 0 { |
| 145 | + continue // Skip empty modes |
| 146 | + } |
| 147 | + |
| 148 | + // Use the first prompt's metadata for description and result description if not already set |
| 149 | + if description == "" && modePrompts[0].Metadata.Description != "" { |
| 150 | + description = modePrompts[0].Metadata.Description |
| 151 | + } |
| 152 | + |
| 153 | + if resultDescription == "" && modePrompts[0].Metadata.ResultDescription != "" { |
| 154 | + resultDescription = modePrompts[0].Metadata.ResultDescription |
| 155 | + } |
| 156 | + |
| 157 | + // Combine all prompt contents for this mode |
| 158 | + var combinedContent strings.Builder |
| 159 | + for i, promptFile := range modePrompts { |
| 160 | + if promptFile.Content != "" { |
| 161 | + if i > 0 { |
| 162 | + combinedContent.WriteString("\n\n") |
| 163 | + } |
| 164 | + combinedContent.WriteString(promptFile.Content) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Store the combined content for this mode |
| 169 | + modeContents[mode] = combinedContent.String() |
| 170 | + } |
| 171 | + |
| 172 | + // Convert the mode contents map to JSON |
| 173 | + contentJSON, err := json.Marshal(modeContents) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("failed to marshal mode contents: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + // Create a single MCP prompt with the module ID as the name |
| 179 | + mcpPrompt := p.NewPrompt(). |
| 180 | + SetName(strings.ToUpper(moduleID)). // Use moduleID as the prompt name |
| 181 | + SetDescription(description). |
| 182 | + SetResultDescription(resultDescription). |
| 183 | + SetText(string(contentJSON)). // Store the JSON map as the prompt text |
| 184 | + Build() |
| 185 | + |
| 186 | + // Create a Prompts collection with the single prompt |
| 187 | + mcpPrompts := p.Prompts{} |
| 188 | + mcpPrompts.Append(mcpPrompt) |
| 189 | + |
| 190 | + slog.Info("Registering prompt for", "module", moduleID, "contentLength", len(contentJSON)) |
| 191 | + |
| 192 | + // Register the prompt with the MCP server |
| 193 | + p.AddPrompts(mcpPrompts, mcpServer) |
| 194 | + |
| 195 | + return nil |
157 | 196 | }
|
0 commit comments