This commit is contained in:
Michael Yang 2024-04-26 15:13:27 -07:00
parent 8907bf51d2
commit 4d08363580

View file

@ -53,22 +53,26 @@ func Parse(r io.Reader) (cmds []Command, err error) {
return nil, err return nil, err
} }
// process the state transition, some transitions need to be intercepted and redirected
if next != curr { if next != curr {
switch curr { switch curr {
case stateName, stateParameter: case stateName, stateParameter:
// next state sometimes depends on the current buffer value
switch s := strings.ToLower(b.String()); s { switch s := strings.ToLower(b.String()); s {
case "from": case "from":
cmd.Name = "model" cmd.Name = "model"
case "parameter": case "parameter":
// transition to stateParameter which sets command name
next = stateParameter next = stateParameter
case "message": case "message":
// transition to stateMessage which validates the message role
next = stateMessage next = stateMessage
fallthrough fallthrough
default: default:
cmd.Name = s cmd.Name = s
} }
case stateMessage: case stateMessage:
if !isValidRole(b.String()) { if !isValidMessageRole(b.String()) {
return nil, errInvalidRole return nil, errInvalidRole
} }
@ -234,6 +238,6 @@ func isNewline(r rune) bool {
return r == '\r' || r == '\n' return r == '\r' || r == '\n'
} }
func isValidRole(role string) bool { func isValidMessageRole(role string) bool {
return role == "system" || role == "user" || role == "assistant" return role == "system" || role == "user" || role == "assistant"
} }