|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + builder "github.com/harness/harness-mcp/pkg/harness/event" |
| 8 | +) |
| 9 | + |
| 10 | +// EventType defines the type of SCS event |
| 11 | +type EventType string |
| 12 | + |
| 13 | +// SCS event types |
| 14 | +const ( |
| 15 | + GenericTableEvent EventType = "table" |
| 16 | + OPAEvent EventType = "opa" |
| 17 | + PromptEvent EventType = "prompt" |
| 18 | +) |
| 19 | + |
| 20 | +var Reg = builder.Registry{ |
| 21 | + string(GenericTableEvent): GenericTableBuilder{}, |
| 22 | + string(OPAEvent): OPABuilder{}, |
| 23 | + string(PromptEvent): PromptBuilder{}, |
| 24 | +} |
| 25 | + |
| 26 | +type GenericTableBuilder struct{} |
| 27 | + |
| 28 | +func (GenericTableBuilder) Build(raw json.RawMessage, tool string, args ...any) string { |
| 29 | + // unmarshal only to discover the shape |
| 30 | + var rows []map[string]interface{} |
| 31 | + _ = json.Unmarshal(raw, &rows) // ignore errors; empty rows is OK |
| 32 | + |
| 33 | + // determine allowed columns from args[0] |
| 34 | + var allowedCols []string |
| 35 | + if len(args) > 0 { |
| 36 | + if ac, ok := args[0].([]string); ok { |
| 37 | + allowedCols = ac |
| 38 | + } |
| 39 | + } |
| 40 | + allowed := map[string]bool{} |
| 41 | + for _, col := range allowedCols { |
| 42 | + allowed[col] = true |
| 43 | + } |
| 44 | + |
| 45 | + // build column descriptors and filter rows in the order of allowedCols |
| 46 | + var cols []map[string]string |
| 47 | + var filteredRows []map[string]interface{} |
| 48 | + if len(allowedCols) > 0 { |
| 49 | + // Only include columns in allowedCols, in the order given |
| 50 | + for _, col := range allowedCols { |
| 51 | + cols = append(cols, map[string]string{ |
| 52 | + "key": col, |
| 53 | + "label": col, |
| 54 | + }) |
| 55 | + } |
| 56 | + for _, row := range rows { |
| 57 | + filtered := map[string]interface{}{} |
| 58 | + for _, col := range allowedCols { |
| 59 | + if v, ok := row[col]; ok { |
| 60 | + filtered[col] = v |
| 61 | + } |
| 62 | + } |
| 63 | + filteredRows = append(filteredRows, filtered) |
| 64 | + } |
| 65 | + } else { |
| 66 | + // Fallback: include all columns in original order |
| 67 | + if len(rows) > 0 { |
| 68 | + for k := range rows[0] { |
| 69 | + cols = append(cols, map[string]string{ |
| 70 | + "key": k, |
| 71 | + "label": k, |
| 72 | + }) |
| 73 | + } |
| 74 | + } |
| 75 | + for _, row := range rows { |
| 76 | + filtered := map[string]interface{}{} |
| 77 | + for k, v := range row { |
| 78 | + filtered[k] = v |
| 79 | + } |
| 80 | + filteredRows = append(filteredRows, filtered) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Create base response with consistent structure |
| 85 | + env := builder.CreateBaseResponse(string(GenericTableEvent), tool) |
| 86 | + // Add table-specific data |
| 87 | + env["table"] = map[string]interface{}{ |
| 88 | + "columns": cols, |
| 89 | + "rows": filteredRows, |
| 90 | + } |
| 91 | + // Format the response using the common formatter |
| 92 | + return builder.FormatEventResponse(string(GenericTableEvent), env) |
| 93 | +} |
| 94 | + |
| 95 | +type OPABuilder struct{} |
| 96 | + |
| 97 | +type PromptBuilder struct{} |
| 98 | + |
| 99 | +func (PromptBuilder) Build(raw json.RawMessage, tool string, args ...any) string { |
| 100 | + eventType := string(PromptEvent) |
| 101 | + // Parse the raw JSON into a slice of prompts |
| 102 | + var prompts []string |
| 103 | + if err := json.Unmarshal(raw, &prompts); err != nil { |
| 104 | + // Return a safe error response instead of raw data |
| 105 | + return `{"error": "Failed to parse prompt data", "type": "prompt"}` |
| 106 | + } |
| 107 | + |
| 108 | + // Create base response with consistent structure |
| 109 | + env := builder.CreateBaseResponse(eventType, tool) |
| 110 | + env["prompts"] = prompts |
| 111 | + |
| 112 | + // Format the response using the common formatter |
| 113 | + return builder.FormatEventResponse(eventType, env) |
| 114 | +} |
| 115 | + |
| 116 | +func (OPABuilder) Build(raw json.RawMessage, tool string, args ...any) string { |
| 117 | + // Use the event type constant for OPA policies |
| 118 | + eventType := string(OPAEvent) |
| 119 | + |
| 120 | + // Parse the raw JSON into a map |
| 121 | + var data map[string]interface{} |
| 122 | + if err := json.Unmarshal(raw, &data); err != nil { |
| 123 | + // Return a safe error response |
| 124 | + return fmt.Sprintf(`{"type": "%s", "error": "Failed to parse policy data"}`, eventType) |
| 125 | + } |
| 126 | + |
| 127 | + // Create base response with consistent structure |
| 128 | + env := builder.CreateBaseResponse(eventType, tool) |
| 129 | + // Add OPA-specific data with the new format |
| 130 | + env["policy"] = map[string]interface{}{ |
| 131 | + "name": "deny-list", |
| 132 | + "content": data["policy"], |
| 133 | + } |
| 134 | + env["metadata"] = map[string]interface{}{ |
| 135 | + "denied_licenses": data["denied_licenses"], |
| 136 | + } |
| 137 | + |
| 138 | + // Format the response using the common formatter |
| 139 | + return builder.FormatEventResponse(eventType, env) |
| 140 | +} |
0 commit comments