okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(cli): remove unused state fields5924d14

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