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
@@ -5,6 +5,7 @@ import { ref } from "vue";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Item, ItemActions, ItemContent, ItemDescription, ItemMedia, ItemTitle } from "@/components/ui/item";
|
||||
import ChatQuestionnaire from "./ChatQuestionnaire.vue";
|
||||
import { answerAsk, type ChatAsk } from "./state";
|
||||
|
||||
defineProps<{ ask: ChatAsk }>();
|
||||
@@ -19,7 +20,9 @@ function sendReply() {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Item v-if="ask.kind === 'approval'" variant="outline" size="sm" class="mt-2" role="group" :aria-label="ask.prompt">
|
||||
<ChatQuestionnaire v-if="ask.kind === 'questionnaire'" :ask="ask" />
|
||||
|
||||
<Item v-else-if="ask.kind === 'approval'" variant="outline" size="sm" class="mt-2" role="group" :aria-label="ask.prompt">
|
||||
<ItemMedia variant="icon"><ShieldQuestion /></ItemMedia>
|
||||
<ItemContent>
|
||||
<ItemTitle>Allow {{ ask.prompt }}?</ItemTitle>
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
<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>
|
||||
+13
-1
@@ -118,10 +118,22 @@ export interface StreamingTurn {
|
||||
/** An approval, clarify or picker question from a running turn. */
|
||||
export interface ChatAsk {
|
||||
id: string;
|
||||
kind: "approval" | "clarify" | "picker";
|
||||
kind: "approval" | "clarify" | "picker" | "questionnaire";
|
||||
prompt: string;
|
||||
detail?: string;
|
||||
choices?: { id: string; label: string }[];
|
||||
questions?: ChatQuestion[];
|
||||
}
|
||||
|
||||
/** One question of a questionnaire ask. */
|
||||
export interface ChatQuestion {
|
||||
name: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
multiple?: boolean;
|
||||
required?: boolean;
|
||||
allow_other?: boolean;
|
||||
choices?: { value: string; label: string; description?: string }[];
|
||||
}
|
||||
|
||||
/** Everything the selectors on the panel read, from one sessions read. */
|
||||
|
||||
Reference in new issue
Block a user