feat(chat): ask several questions in one interaction with ask_questions
A turn can pose a questionnaire (single and multiple choice, free text, skippable) and read the answers back, rendered in the chat with the shadcn-vue questionnaire. The gateway contract gains a questionnaire ask and a can_question capability; answers travel as a JSON object keyed by question name and are checked against the questions that were asked.
This commit is contained in:
1 parent
31ac8f1338
commit
5c47ebd05b
30 files changed
+3211
-392
No files matched your search
@@ -16,10 +16,28 @@ import (
|
||||
// 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 or picker
|
||||
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 {
|
||||
@@ -131,6 +149,26 @@ func (d dashboardAsker) RequestChoice(ctx context.Context, req messaging.PickerR
|
||||
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 {
|
||||
@@ -142,7 +180,7 @@ func askChoices(choices []messaging.InteractiveChoice) []chatAskChoice {
|
||||
// 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})
|
||||
return messaging.WithInteractive(ctx, messaging.Interactive{Clarifier: asker, Picker: asker, Questioner: asker})
|
||||
}
|
||||
|
||||
type chatAnswerRequest struct {
|
||||
|
||||
Reference in new issue
Block a user