okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
5924d14
main
1package daemon 2 3import ( 4"encoding/json" 5"strings" 6 7"albedo/cli/internal/daemon/protocol" 8) 9 10type CommandArgument struct { 11Name ,Description ,Type string 12Choices []string 13Required bool 14field protocol.FormField 15} 16type SessionCommand struct { 17Page * bool 18Name ,Description ,Method ,ID ,Delivery ,CommandID string 19Arguments []CommandArgument 20UserTurn bool 21Operation protocol.ActionOperation 22} 23 24func decodeSessionCommand (raw json.RawMessage ) (SessionCommand ,error ) { 25var discriminator struct { 26Delivery string `json:"delivery"` 27 } 28if err := decodeRequired (raw ,& discriminator ,"delivery" );err != nil { 29return SessionCommand {},err 30 } 31var command protocol.HTTPCommand 32var commandID string 33if discriminator .Delivery == "input" { 34var input protocol.InputCommand 35if err := decodeRequired (raw ,& input );err != nil { 36return SessionCommand {},err 37 } 38command = protocol.HTTPCommand {ID :input .ID ,SlashName :input .SlashName ,Description :input .Description ,Arguments :input .Arguments ,CallerPermissions :input .CallerPermissions ,Delivery :input .Delivery } 39commandID = input .CommandID 40 }else if err := decodeRequired (raw ,& command );err != nil { 41return SessionCommand {},err 42 } 43return sessionCommandValue (command ,commandID ) 44} 45 46func sessionCommandValue (command protocol.HTTPCommand ,commandID string ) (SessionCommand ,error ) { 47if command .ID == "" || ! strings .HasPrefix (command .SlashName ,"/" )|| command .Arguments == nil { 48return SessionCommand {},fieldError ("command" ) 49 } 50result := SessionCommand {ID :command .ID ,Name :command .SlashName ,Description :command .Description ,Delivery :command .Delivery ,Operation :command .Operation } 51if command .Delivery == "input" { 52if commandID == "" { 53return SessionCommand {},fieldError ("command ID" ) 54 } 55result .CommandID = commandID 56result .Method = "PUT" 57result .UserTurn = true 58 }else if command .Delivery != "read" && command .Delivery != "mutation" { 59return SessionCommand {},fieldError ("command delivery" ) 60 } 61if ! result .UserTurn { 62result .Method = command .Operation .Method 63 } 64page := command .Delivery == "read" && len (command .Arguments )== 0 && (strings .HasPrefix (command .Operation .PathTemplate ,"/extensions/" )|| command .SlashName == "/ttl" || command .SlashName == "/requests" ) 65result .Page = & page 66for _ ,field := range command .Arguments { 67argument := CommandArgument {Name :field .Name ,Description :field .Description ,Required :field .Required ,Type :field .Type ,field :field } 68for _ ,choice := range field .Choices { 69argument .Choices = append (argument .Choices ,choice .Label ) 70 } 71result .Arguments = append (result .Arguments ,argument ) 72 } 73return result ,nil 74}