Armored the decision parsing.

This commit is contained in:
Steve White 2025-01-25 20:51:37 -06:00
parent 80d60acf64
commit 36fdd5f00e
1 changed files with 19 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import (
"net/http"
"strings"
"time"
"unicode"
)
// Paper represents a single academic paper
@ -185,13 +186,24 @@ Abstract: %s`, criteria, paper.Title, paper.Abstract)
return nil, fmt.Errorf("invalid response format")
}
decisionParts := bytes.SplitN(lines[0], []byte(":"), 2)
if len(decisionParts) != 2 {
return nil, fmt.Errorf("invalid decision format, expected 'DECISION: [VALUE]'")
}
decision := strings.ToUpper(string(bytes.TrimSpace(decisionParts[1])))
if decision != "ACCEPT" && decision != "REJECT" {
return nil, fmt.Errorf("invalid decision value: %s", decision)
// Clean and normalize decision
rawDecision := string(bytes.TrimSpace(lines[0]))
// Remove any non-alphabetic characters from start/end
cleanDecision := strings.TrimFunc(rawDecision, func(r rune) bool {
return !unicode.IsLetter(r)
})
// Normalize case and check for valid decision
upperDecision := strings.ToUpper(cleanDecision)
var decision string
switch {
case strings.HasPrefix(upperDecision, "ACCEPT"):
decision = "ACCEPT"
case strings.HasPrefix(upperDecision, "REJECT"):
decision = "REJECT"
default:
return nil, fmt.Errorf("invalid decision value: %q (cleaned: %q)", rawDecision, cleanDecision)
}
explanation := string(bytes.TrimSpace(bytes.Join(lines[1:], []byte("\n"))))