@@ -203,6 +203,10 @@ func (s *ClassificationAPIServer) setupRoutes() *http.ServeMux {
203203 mux .HandleFunc ("GET /config/classification" , s .handleGetConfig )
204204 mux .HandleFunc ("PUT /config/classification" , s .handleUpdateConfig )
205205
206+ // System prompt configuration endpoints
207+ mux .HandleFunc ("GET /config/system-prompts" , s .handleGetSystemPrompts )
208+ mux .HandleFunc ("PUT /config/system-prompts" , s .handleUpdateSystemPrompts )
209+
206210 return mux
207211}
208212
@@ -705,3 +709,151 @@ func (s *ClassificationAPIServer) calculateUnifiedStatistics(unifiedResults *ser
705709 LowConfidenceCount : lowConfidenceCount ,
706710 }
707711}
712+
713+ // SystemPromptInfo represents system prompt information for a category
714+ type SystemPromptInfo struct {
715+ Category string `json:"category"`
716+ Prompt string `json:"prompt"`
717+ Enabled bool `json:"enabled"`
718+ Mode string `json:"mode"` // "replace" or "insert"
719+ }
720+
721+ // SystemPromptsResponse represents the response for GET /config/system-prompts
722+ type SystemPromptsResponse struct {
723+ SystemPrompts []SystemPromptInfo `json:"system_prompts"`
724+ }
725+
726+ // SystemPromptUpdateRequest represents a request to update system prompt settings
727+ type SystemPromptUpdateRequest struct {
728+ Category string `json:"category,omitempty"` // If empty, applies to all categories
729+ Enabled * bool `json:"enabled,omitempty"` // true to enable, false to disable
730+ Mode string `json:"mode,omitempty"` // "replace" or "insert"
731+ }
732+
733+ // handleGetSystemPrompts handles GET /config/system-prompts
734+ func (s * ClassificationAPIServer ) handleGetSystemPrompts (w http.ResponseWriter , r * http.Request ) {
735+ cfg := s .classificationSvc .GetConfig ()
736+ if cfg == nil {
737+ http .Error (w , "Configuration not available" , http .StatusInternalServerError )
738+ return
739+ }
740+
741+ var systemPrompts []SystemPromptInfo
742+ for _ , category := range cfg .Categories {
743+ systemPrompts = append (systemPrompts , SystemPromptInfo {
744+ Category : category .Name ,
745+ Prompt : category .SystemPrompt ,
746+ Enabled : category .IsSystemPromptEnabled (),
747+ Mode : category .GetSystemPromptMode (),
748+ })
749+ }
750+
751+ response := SystemPromptsResponse {
752+ SystemPrompts : systemPrompts ,
753+ }
754+
755+ w .Header ().Set ("Content-Type" , "application/json" )
756+ if err := json .NewEncoder (w ).Encode (response ); err != nil {
757+ http .Error (w , "Failed to encode response" , http .StatusInternalServerError )
758+ return
759+ }
760+ }
761+
762+ // handleUpdateSystemPrompts handles PUT /config/system-prompts
763+ func (s * ClassificationAPIServer ) handleUpdateSystemPrompts (w http.ResponseWriter , r * http.Request ) {
764+ var req SystemPromptUpdateRequest
765+ if err := s .parseJSONRequest (r , & req ); err != nil {
766+ http .Error (w , err .Error (), http .StatusBadRequest )
767+ return
768+ }
769+
770+ if req .Enabled == nil && req .Mode == "" {
771+ http .Error (w , "either enabled or mode field is required" , http .StatusBadRequest )
772+ return
773+ }
774+
775+ // Validate mode if provided
776+ if req .Mode != "" && req .Mode != "replace" && req .Mode != "insert" {
777+ http .Error (w , "mode must be either 'replace' or 'insert'" , http .StatusBadRequest )
778+ return
779+ }
780+
781+ cfg := s .classificationSvc .GetConfig ()
782+ if cfg == nil {
783+ http .Error (w , "Configuration not available" , http .StatusInternalServerError )
784+ return
785+ }
786+
787+ // Create a copy of the config to modify
788+ newCfg := * cfg
789+ newCategories := make ([]config.Category , len (cfg .Categories ))
790+ copy (newCategories , cfg .Categories )
791+ newCfg .Categories = newCategories
792+
793+ updated := false
794+ if req .Category == "" {
795+ // Update all categories
796+ for i := range newCfg .Categories {
797+ if newCfg .Categories [i ].SystemPrompt != "" {
798+ if req .Enabled != nil {
799+ newCfg .Categories [i ].SystemPromptEnabled = req .Enabled
800+ }
801+ if req .Mode != "" {
802+ newCfg .Categories [i ].SystemPromptMode = req .Mode
803+ }
804+ updated = true
805+ }
806+ }
807+ } else {
808+ // Update specific category
809+ for i := range newCfg .Categories {
810+ if newCfg .Categories [i ].Name == req .Category {
811+ if newCfg .Categories [i ].SystemPrompt == "" {
812+ http .Error (w , fmt .Sprintf ("Category '%s' has no system prompt configured" , req .Category ), http .StatusBadRequest )
813+ return
814+ }
815+ if req .Enabled != nil {
816+ newCfg .Categories [i ].SystemPromptEnabled = req .Enabled
817+ }
818+ if req .Mode != "" {
819+ newCfg .Categories [i ].SystemPromptMode = req .Mode
820+ }
821+ updated = true
822+ break
823+ }
824+ }
825+ if ! updated {
826+ http .Error (w , fmt .Sprintf ("Category '%s' not found" , req .Category ), http .StatusNotFound )
827+ return
828+ }
829+ }
830+
831+ if ! updated {
832+ http .Error (w , "No categories with system prompts found to update" , http .StatusBadRequest )
833+ return
834+ }
835+
836+ // Update the configuration
837+ s .classificationSvc .UpdateConfig (& newCfg )
838+
839+ // Return the updated system prompts
840+ var systemPrompts []SystemPromptInfo
841+ for _ , category := range newCfg .Categories {
842+ systemPrompts = append (systemPrompts , SystemPromptInfo {
843+ Category : category .Name ,
844+ Prompt : category .SystemPrompt ,
845+ Enabled : category .IsSystemPromptEnabled (),
846+ Mode : category .GetSystemPromptMode (),
847+ })
848+ }
849+
850+ response := SystemPromptsResponse {
851+ SystemPrompts : systemPrompts ,
852+ }
853+
854+ w .Header ().Set ("Content-Type" , "application/json" )
855+ if err := json .NewEncoder (w ).Encode (response ); err != nil {
856+ http .Error (w , "Failed to encode response" , http .StatusInternalServerError )
857+ return
858+ }
859+ }
0 commit comments