answerAsk now clears the pending question only after api.chatAnswer accepts it, and resolves whether it did. On failure the new askError ref carries the message, shown on the card as a ReadError whose Retry resends the last answer; ChatAskCard keeps the typed reply until acceptance and routes every choice button through the same path, and the questionnaire submits through the card so its answers are retryable too. askError resets on turn start, on a new ask, and on conversation switch.
91 lines
2.9 KiB
Vue
91 lines
2.9 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 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. The card owns answerAsk so
|
|
* a failed submit is recorded for the card's retry.
|
|
*/
|
|
const props = defineProps<{ ask: ChatAsk; send: (answer: string) => Promise<boolean> }>();
|
|
|
|
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 props.send(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>
|