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.
90 lines
2.8 KiB
Vue
90 lines
2.8 KiB
Vue
<script setup lang="ts">
|
|
import { computed } from "vue";
|
|
|
|
import {
|
|
Questionnaire,
|
|
QuestionnaireActions,
|
|
QuestionnaireChoice,
|
|
QuestionnaireChoices,
|
|
QuestionnaireDescription,
|
|
QuestionnaireError,
|
|
QuestionnaireInput,
|
|
QuestionnaireItem,
|
|
QuestionnaireNext,
|
|
QuestionnairePrevious,
|
|
QuestionnaireProgress,
|
|
QuestionnaireSkip,
|
|
QuestionnaireSubmit,
|
|
QuestionnaireTitle,
|
|
} from "@/components/ui/questionnaire";
|
|
import { answerAsk, type ChatAsk } from "./state";
|
|
|
|
/**
|
|
* Several questions from one turn, answered in a single reply. The answers go
|
|
* back as a JSON object of question name to the list of values given, the shape
|
|
* the server checks against the questions it asked.
|
|
*/
|
|
const props = defineProps<{ ask: ChatAsk }>();
|
|
|
|
const questions = computed(() => props.ask.questions ?? []);
|
|
const items = computed(() =>
|
|
questions.value.map((q) => ({
|
|
name: q.name,
|
|
required: q.required,
|
|
choices: (q.choices ?? []).map((c) => ({ value: c.value })),
|
|
})),
|
|
);
|
|
const last = computed(() => questions.value.length <= 1);
|
|
|
|
function submit(event: Event): void {
|
|
event.preventDefault();
|
|
const form = new FormData(event.target as HTMLFormElement);
|
|
const answers: Record<string, string[]> = {};
|
|
for (const q of questions.value) {
|
|
const values = form.getAll(q.name).map(String).filter((v) => v.trim());
|
|
if (values.length) answers[q.name] = values;
|
|
}
|
|
void answerAsk(JSON.stringify(answers));
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Questionnaire
|
|
class="mt-2 min-w-72"
|
|
:default-item="questions[0]?.name"
|
|
:items="items"
|
|
shortcuts="letters"
|
|
@submit="submit"
|
|
>
|
|
<QuestionnaireProgress v-if="!last" />
|
|
<QuestionnaireItem
|
|
v-for="q in questions"
|
|
:key="q.name"
|
|
:name="q.name"
|
|
:required="q.required"
|
|
:multiple="q.multiple"
|
|
>
|
|
<QuestionnaireTitle>{{ q.title }}</QuestionnaireTitle>
|
|
<QuestionnaireDescription v-if="q.description">{{ q.description }}</QuestionnaireDescription>
|
|
<QuestionnaireChoices>
|
|
<QuestionnaireChoice v-for="c in q.choices ?? []" :key="c.value" :value="c.value">
|
|
<span class="font-medium">{{ c.label || c.value }}</span>
|
|
<span v-if="c.description" class="text-muted-foreground">{{ c.description }}</span>
|
|
</QuestionnaireChoice>
|
|
<QuestionnaireInput
|
|
v-if="q.allow_other || !q.choices?.length"
|
|
:aria-label="q.title"
|
|
:placeholder="q.choices?.length ? 'Something else…' : 'Your answer'"
|
|
/>
|
|
</QuestionnaireChoices>
|
|
<QuestionnaireError />
|
|
</QuestionnaireItem>
|
|
<QuestionnaireActions>
|
|
<QuestionnairePrevious v-if="!last" />
|
|
<QuestionnaireSkip />
|
|
<QuestionnaireNext v-if="!last">Next</QuestionnaireNext>
|
|
<QuestionnaireSubmit>Send</QuestionnaireSubmit>
|
|
</QuestionnaireActions>
|
|
</Questionnaire>
|
|
</template>
|