package webui import ( "context" "crypto/rand" "encoding/hex" "encoding/json" "errors" "net/http" "strings" "sync" "github.com/samcharles93/archie-core/internal/domain/messaging" ) // chatAsk is a question a turn is blocked on, rendered as a card in the chat. type chatAsk struct { ID string `json:"id"` Kind string `json:"kind"` // approval, clarify, picker or questionnaire Prompt string `json:"prompt"` Detail string `json:"detail,omitempty"` Choices []chatAskChoice `json:"choices,omitempty"` // Questions are those of a questionnaire. Questions []chatAskQuestion `json:"questions,omitempty"` } type chatAskQuestion struct { Name string `json:"name"` Title string `json:"title"` Description string `json:"description,omitempty"` Multiple bool `json:"multiple,omitempty"` Required bool `json:"required,omitempty"` AllowOther bool `json:"allow_other,omitempty"` Choices []chatAskQuestionOpt `json:"choices,omitempty"` } type chatAskQuestionOpt struct { Value string `json:"value"` Label string `json:"label"` Description string `json:"description,omitempty"` } type chatAskChoice struct { ID string `json:"id"` Label string `json:"label"` } // chatAsks holds the asks waiting for the browser's answer, keyed by id. type chatAsks struct { mu sync.Mutex waiting map[string]chan string owners map[string]string } func (a *chatAsks) open(owner string) (string, chan string) { var b [12]byte _, _ = rand.Read(b[:]) id := hex.EncodeToString(b[:]) ch := make(chan string, 1) a.mu.Lock() if a.waiting == nil { a.waiting = make(map[string]chan string) a.owners = make(map[string]string) } a.waiting[id] = ch a.owners[id] = owner a.mu.Unlock() return id, ch } func (a *chatAsks) close(id string) { a.mu.Lock() delete(a.waiting, id) delete(a.owners, id) a.mu.Unlock() } func (a *chatAsks) answer(id, answer, owner string) bool { a.mu.Lock() ch, ok := a.waiting[id] ok = ok && a.owners[id] == owner a.mu.Unlock() if ok { select { case ch <- answer: default: } } return ok } // dashboardAsker answers a turn's approvals and questions through cards in // the chat stream, so the dashboard carries everything Telegram does. type dashboardAsker struct { asks *chatAsks write func(chatStreamEvent) } func (d dashboardAsker) ask(ctx context.Context, q chatAsk) (string, error) { id, ch := d.asks.open(chatUser(ctx)) defer d.asks.close(id) q.ID = id d.write(chatStreamEvent{Type: "ask", Ask: &q}) select { case answer := <-ch: return answer, nil case <-ctx.Done(): return "", ctx.Err() } } func (d dashboardAsker) RequestApproval(ctx context.Context, action, description string) (messaging.ApprovalDecision, error) { answer, err := d.ask(ctx, chatAsk{Kind: "approval", Prompt: action, Detail: description}) switch { case err != nil: return messaging.ApprovalDenied, err case answer == "approve": return messaging.ApprovalApproved, nil case answer == "always": return messaging.ApprovalPermanentlyApproved, nil } return messaging.ApprovalDenied, nil } func (d dashboardAsker) RequestClarification(ctx context.Context, req messaging.ClarifyRequest) (string, error) { answer, err := d.ask(ctx, chatAsk{Kind: "clarify", Prompt: req.Question, Choices: askChoices(req.Suggestions)}) if err != nil { return "", err } if answer = strings.TrimSpace(answer); answer == "" { return "", messaging.ErrEmptyReply } return answer, nil } func (d dashboardAsker) RequestChoice(ctx context.Context, req messaging.PickerRequest) (messaging.InteractiveChoice, error) { if len(req.Options) == 0 { return messaging.InteractiveChoice{}, messaging.ErrNoPickerOptions } answer, err := d.ask(ctx, chatAsk{Kind: "picker", Prompt: req.Prompt, Choices: askChoices(req.Options)}) if err != nil { return messaging.InteractiveChoice{}, err } for _, option := range req.Options { if option.ID == answer { return option, nil } } return messaging.InteractiveChoice{}, errors.New("picker answer matches no option") } func (d dashboardAsker) RequestAnswers(ctx context.Context, req messaging.QuestionnaireRequest) (messaging.QuestionnaireAnswers, error) { questions := make([]chatAskQuestion, 0, len(req.Questions)) for _, q := range req.Questions { view := chatAskQuestion{Name: q.Name, Title: q.Title, Description: q.Description, Multiple: q.Multiple, Required: q.Required, AllowOther: q.AllowOther} for _, c := range q.Choices { view.Choices = append(view.Choices, chatAskQuestionOpt{Value: c.Value, Label: c.Label, Description: c.Description}) } questions = append(questions, view) } answer, err := d.ask(ctx, chatAsk{Kind: "questionnaire", Questions: questions}) if err != nil { return nil, err } var raw map[string][]string if err := json.Unmarshal([]byte(answer), &raw); err != nil { return nil, errors.New("questionnaire answer is not a JSON object of lists") } return messaging.CheckAnswers(req, raw) } func askChoices(choices []messaging.InteractiveChoice) []chatAskChoice { out := make([]chatAskChoice, 0, len(choices)) for _, c := range choices { out = append(out, chatAskChoice{ID: c.ID, Label: c.Label}) } return out } // withDashboardAsks puts the dashboard's askers on a turn's context. func withDashboardAsks(ctx context.Context, asker dashboardAsker) context.Context { ctx = messaging.WithApprovalRequester(ctx, asker) return messaging.WithInteractive(ctx, messaging.Interactive{Clarifier: asker, Picker: asker, Questioner: asker}) } type chatAnswerRequest struct { ID string `json:"id"` Answer string `json:"answer"` } // handleChatAnswer delivers the operator's answer to the ask a turn waits on. func (s *Server) handleChatAnswer(w http.ResponseWriter, r *http.Request) { var req chatAnswerRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.ID == "" { http.Error(w, "id is required", http.StatusBadRequest) return } if !s.chatAsks.answer(req.ID, req.Answer, chatUser(r.Context())) { http.Error(w, "that question is no longer waiting", http.StatusNotFound) return } writeJSON(w, map[string]bool{"answered": true}) }