Files
breakpilot-compliance/ai-compliance-sdk/internal/api/handlers/iace_handler_norms.go
T
Benjamin Admin 97a52533a8
Build + Deploy / build-admin-compliance (push) Successful in 2m29s
Build + Deploy / build-backend-compliance (push) Successful in 3m23s
Build + Deploy / build-ai-sdk (push) Failing after 47s
Build + Deploy / build-developer-portal (push) Successful in 1m19s
Build + Deploy / build-tts (push) Failing after 1m29s
Build + Deploy / build-document-crawler (push) Successful in 43s
Build + Deploy / build-dsms-gateway (push) Successful in 25s
Build + Deploy / build-dsms-node (push) Successful in 11s
CI / branch-name (push) Has been skipped
Build + Deploy / trigger-orca (push) Has been skipped
CI / guardrail-integrity (push) Has been skipped
CI / loc-budget (push) Failing after 18s
CI / secret-scan (push) Has been skipped
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / nodejs-build (push) Successful in 3m17s
CI / dep-audit (push) Has been skipped
CI / sbom-scan (push) Has been skipped
CI / test-go (push) Failing after 48s
CI / test-python-backend (push) Successful in 42s
CI / test-python-document-crawler (push) Successful in 31s
CI / test-python-dsms-gateway (push) Successful in 26s
CI / validate-canonical-controls (push) Successful in 18s
Merge remote gitea/main — resolve conflicts keeping local (origin) state
Local origin is 20+ commits ahead of remote gitea. All conflicts
resolved by keeping HEAD (our version) which includes the full
56→138 check expansion and doc_checks package split.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-07 12:40:23 +02:00

201 lines
5.3 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"strings"
"github.com/breakpilot/ai-compliance-sdk/internal/iace"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// ============================================================================
// Norms Library & Norm Suggestions
// ============================================================================
// ListNormsLibrary handles GET /norms-library
// Returns the full norms library, optionally filtered by ?type=A|B1|B2|C
// and ?hazard_category=xxx.
func (h *IACEHandler) ListNormsLibrary(c *gin.Context) {
normType := c.Query("type")
hazardCat := c.Query("hazard_category")
allNorms := iace.GetNormsLibrary()
allNorms = append(allNorms, iace.GetExtendedB2Norms()...)
allNorms = append(allNorms, iace.GetCNormsLibrary()...)
allNorms = append(allNorms, iace.GetExtendedCNormsLibrary()...)
allNorms = append(allNorms, iace.GetWoodMetalCNorms()...)
allNorms = append(allNorms, iace.GetFoodPkgCNorms()...)
allNorms = append(allNorms, iace.GetLiftMiscCNorms()...)
allNorms = append(allNorms, iace.GetMachiningCNorms()...)
allNorms = append(allNorms, iace.GetConveyorAutoCNorms()...)
allNorms = append(allNorms, iace.GetProcessCNorms()...)
allNorms = append(allNorms, iace.GetConstructionCNorms()...)
var filtered []iace.NormReference
for _, norm := range allNorms {
if normType != "" && norm.NormType != normType {
continue
}
if hazardCat != "" && !containsString(norm.HazardCats, hazardCat) {
continue
}
filtered = append(filtered, norm)
}
if filtered == nil {
filtered = []iace.NormReference{}
}
c.JSON(http.StatusOK, gin.H{
"norms": filtered,
"total": len(filtered),
})
}
// SuggestProjectNorms handles GET /projects/:id/suggested-norms
// Returns norm suggestions based on the project's machine type, identified
// hazards, and component tags.
func (h *IACEHandler) SuggestProjectNorms(c *gin.Context) {
projectID, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid project ID"})
return
}
// Fetch project to get machine type
project, err := h.store.GetProject(c.Request.Context(), projectID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if project == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "project not found"})
return
}
// Collect unique hazard categories from project hazards
hazardCategories := collectHazardCategories(h, c, projectID)
// Collect tags from component metadata
tags := collectComponentTags(h, c, projectID)
result := iace.SuggestNorms(project.MachineType, hazardCategories, tags)
c.JSON(http.StatusOK, gin.H{
"suggestions": result,
"machine_type": project.MachineType,
"hazard_categories": hazardCategories,
"tags": tags,
})
}
// collectHazardCategories extracts unique hazard categories from a project's hazards.
func collectHazardCategories(h *IACEHandler, c *gin.Context, projectID uuid.UUID) []string {
hazards, err := h.store.ListHazards(c.Request.Context(), projectID)
if err != nil {
return []string{}
}
seen := make(map[string]bool)
var categories []string
for _, hz := range hazards {
if hz.Category != "" && !seen[hz.Category] {
seen[hz.Category] = true
categories = append(categories, hz.Category)
}
}
return categories
}
// collectComponentTags extracts tags from the metadata JSON of project components.
// Components store tags in their metadata field as {"tags": ["tag1", "tag2"]}.
// Additionally, component types are mapped to tags via the component library.
func collectComponentTags(h *IACEHandler, c *gin.Context, projectID uuid.UUID) []string {
components, err := h.store.ListComponents(c.Request.Context(), projectID)
if err != nil {
return []string{}
}
seen := make(map[string]bool)
var tags []string
for _, comp := range components {
// Extract tags from metadata JSON
extracted := extractTagsFromMetadata(comp.Metadata)
for _, t := range extracted {
if !seen[t] {
seen[t] = true
tags = append(tags, t)
}
}
// Extract tags from description field ("Tags: x, y, z" pattern)
descTags := parseDescriptionTags(comp.Description)
for _, t := range descTags {
if !seen[t] {
seen[t] = true
tags = append(tags, t)
}
}
}
return tags
}
// extractTagsFromMetadata parses the component metadata JSON for a "tags" array.
func extractTagsFromMetadata(metadata json.RawMessage) []string {
if len(metadata) == 0 {
return nil
}
var m map[string]interface{}
if err := json.Unmarshal(metadata, &m); err != nil {
return nil
}
tagsRaw, ok := m["tags"]
if !ok {
return nil
}
arr, ok := tagsRaw.([]interface{})
if !ok {
return nil
}
var tags []string
for _, v := range arr {
if s, ok := v.(string); ok && s != "" {
tags = append(tags, s)
}
}
return tags
}
// parseDescriptionTags looks for a "Tags: x, y, z" pattern in the description.
func parseDescriptionTags(description string) []string {
idx := strings.Index(strings.ToLower(description), "tags:")
if idx < 0 {
return nil
}
// Take everything after "Tags:"
rest := strings.TrimSpace(description[idx+5:])
if rest == "" {
return nil
}
// Split by comma and trim each tag
parts := strings.Split(rest, ",")
var tags []string
for _, p := range parts {
t := strings.TrimSpace(p)
if t != "" {
tags = append(tags, t)
}
}
return tags
}