package review import ( "embed" "encoding/json" "fmt" "os" "path/filepath" "strings" ) //go:embed personas/*.json var embeddedPersonas embed.FS // Persona defines a specialized review role with focused expertise. type Persona struct { Name string `json:"name"` DisplayName string `json:"display_name"` ModelPref string `json:"model_preference,omitempty"` Identity string `json:"identity"` Focus []string `json:"focus"` Ignore []string `json:"ignore"` Severity Severity `json:"severity"` OutputFormat string `json:"output_format,omitempty"` } // Severity defines what constitutes each severity level for this persona. // These are prompt guidance for the LLM, not output format changes. type Severity struct { Major string `json:"major"` Minor string `json:"minor"` Nit string `json:"nit"` } // LoadPersona loads a persona from a file path. func LoadPersona(path string) (*Persona, error) { data, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("read persona file %s: %w", path, err) } return parsePersona(data, path) } // LoadBuiltinPersona loads a built-in persona by name. // Returns an error if the persona doesn't exist. func LoadBuiltinPersona(name string) (*Persona, error) { filename := name + ".json" data, err := embeddedPersonas.ReadFile(filepath.Join("personas", filename)) if err != nil { available := ListBuiltinPersonas() return nil, fmt.Errorf("unknown built-in persona %q (available: %s)", name, strings.Join(available, ", ")) } return parsePersona(data, "builtin:"+name) } // ListBuiltinPersonas returns the names of all built-in personas. func ListBuiltinPersonas() []string { entries, err := embeddedPersonas.ReadDir("personas") if err != nil { return nil } var names []string for _, e := range entries { if e.IsDir() { continue } name := e.Name() if strings.HasSuffix(name, ".json") { names = append(names, strings.TrimSuffix(name, ".json")) } } return names } func parsePersona(data []byte, source string) (*Persona, error) { var p Persona if err := json.Unmarshal(data, &p); err != nil { return nil, fmt.Errorf("parse persona %s: %w", source, err) } if err := validatePersona(&p, source); err != nil { return nil, err } return &p, nil } func validatePersona(p *Persona, source string) error { if p.Name == "" { return fmt.Errorf("persona %s: name is required", source) } if p.Identity == "" { return fmt.Errorf("persona %s: identity is required", source) } // DisplayName defaults to Name if not set if p.DisplayName == "" { p.DisplayName = p.Name } return nil }