Files
CatoandClaude Opus 5.5 9dde8fbee9 feat(forms): prose fields on chatkit DictationField, with a working microphone (#330)
* wip: prose fields on chatkit DictationField (awaits chatkit 0.45)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuMRAXYWNkfowAXVfi

* feat(forms): prose fields on chatkit DictationField, with a working microphone

- Bump @bitbaum/chatkit to ^0.45.0.
- One app wrapper, components/forms/TextBox, around DictationField. Used by
  the bot builder (name, description, knowledge topic and content, system
  prompt), the custom-bot edit page (name, description, system prompt,
  greeting), the contact message, the demo intake questions, quick-create
  (name, relationship, memories) and the consultation form's
  react-hook-form message field ({...register('message')}).
- app/api/transcribe: dictation server leg on ai-kit's speechChain and
  transcribe(), rate limited, recordings and transcripts neither stored nor
  logged. Every field and every in-page chat points voice at it, so the
  microphone works where the browser has no recogniser of its own.
- Permissions-Policy allowed no microphone at all (microphone=()), which
  blocked every mic on the site; it now allows the site itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuMRAXYWNkfowAXVfi

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-19 14:23:42 +01:00

221 lines
7.3 KiB
TypeScript

'use client';
import { useState, type FC, type FormEvent } from 'react';
import type { IntakeQuestion, IntakeResponses } from '@/lib/demo/types';
import { BRAND_BUTTON, BRAND_FOCUS_RING, BRAND_CHECKBOX } from '@/lib/config/colors';
import { TextBox } from '@/components/forms/TextBox';
interface DemoIntakeProps {
questions: IntakeQuestion[];
responses: IntakeResponses;
onUpdate: (questionId: string, value: string | string[]) => void;
onStart: () => void;
icon: string;
botName: string;
}
export const DemoIntake: FC<DemoIntakeProps> = ({
questions,
responses,
onUpdate,
onStart,
icon,
botName,
}) => {
const [showAdvanced, setShowAdvanced] = useState(false);
const buttonClass = BRAND_BUTTON;
const focusClass = BRAND_FOCUS_RING;
const checkboxClass = BRAND_CHECKBOX;
const essentialQuestions = questions.filter((q) => q.phase === 'essential');
const advancedQuestions = questions.filter((q) => q.phase === 'advanced');
// Check if all required questions are answered
const requiredQuestions = questions.filter((q) => q.required);
const allRequiredAnswered = requiredQuestions.every((q) => {
const value = responses[q.id];
if (Array.isArray(value)) return value.length > 0;
return value && value.trim() !== '';
});
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
if (allRequiredAnswered) {
onStart();
}
};
const renderQuestion = (question: IntakeQuestion) => {
const value = responses[question.id] || '';
switch (question.type) {
case 'text':
return (
<TextBox
multiline={false}
id={question.id}
value={typeof value === 'string' ? value : ''}
onValueChange={(next) => onUpdate(question.id, next)}
placeholder={question.placeholder}
required={question.required}
/>
);
case 'textarea':
return (
<TextBox
id={question.id}
value={typeof value === 'string' ? value : ''}
onValueChange={(next) => onUpdate(question.id, next)}
placeholder={question.placeholder}
rows={3}
required={question.required}
/>
);
case 'select':
return (
<select
id={question.id}
value={typeof value === 'string' ? value : ''}
onChange={(e) => onUpdate(question.id, e.target.value)}
className={`w-full rounded-lg border border-gray-300 px-4 py-2.5 text-gray-900 ${focusClass} focus:outline-none focus:ring-1 bg-white`}
>
<option value="">Select an option...</option>
{question.options?.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
);
case 'multiselect':
return (
<div className="space-y-2">
{question.options?.map((option) => {
const selectedValues = Array.isArray(value) ? value : [];
const isChecked = selectedValues.includes(option);
return (
<label key={option} className="flex items-center gap-3 cursor-pointer">
<input
type="checkbox"
checked={isChecked}
onChange={(e) => {
if (e.target.checked) {
onUpdate(question.id, [...selectedValues, option]);
} else {
onUpdate(
question.id,
selectedValues.filter((v) => v !== option),
);
}
}}
className={`h-4 w-4 rounded border-gray-300 ${checkboxClass}`}
/>
<span className="text-gray-700">{option}</span>
</label>
);
})}
</div>
);
default:
return null;
}
};
return (
<form onSubmit={handleSubmit} className="mx-auto max-w-2xl">
{/* Header */}
<div className="mb-8 text-center">
<div className="mb-4 text-5xl">{icon}</div>
<h3 className="text-xl font-semibold text-gray-900 mb-2">
Let&apos;s get started with {botName}
</h3>
<p className="text-gray-600">
Answer a few questions so I can better understand your needs
</p>
</div>
{/* Essential Questions */}
<div className="space-y-6">
{essentialQuestions.map((question) => (
<div key={question.id}>
<label htmlFor={question.id} className="block text-sm font-medium text-gray-700 mb-2">
{question.question}
{question.required && <span className="text-red-500 ml-1">*</span>}
</label>
{renderQuestion(question)}
</div>
))}
</div>
{/* Advanced Questions Toggle */}
{advancedQuestions.length > 0 && (
<div className="mt-8">
<button
type="button"
onClick={() => setShowAdvanced(!showAdvanced)}
className="flex items-center gap-2 text-sm text-gray-600 hover:text-gray-900 transition-colors"
>
<svg
className={`h-4 w-4 transition-transform ${showAdvanced ? 'rotate-180' : ''}`}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 9l-7 7-7-7"
/>
</svg>
{showAdvanced ? 'Hide' : 'Show'} additional options ({advancedQuestions.length})
</button>
{showAdvanced && (
<div className="mt-6 space-y-6 border-t border-gray-200 pt-6">
{advancedQuestions.map((question) => (
<div key={question.id}>
<label
htmlFor={question.id}
className="block text-sm font-medium text-gray-700 mb-2"
>
{question.question}
{question.required && <span className="text-red-500 ml-1">*</span>}
</label>
{renderQuestion(question)}
</div>
))}
</div>
)}
</div>
)}
{/* Submit Button */}
<div className="mt-8 space-y-3">
<button
type="submit"
disabled={!allRequiredAnswered}
className={`w-full rounded-lg px-6 py-3 text-white font-medium transition-colors ${buttonClass} disabled:opacity-50 disabled:cursor-not-allowed focus:outline-none focus:ring-2 focus:ring-offset-2`}
>
Start Conversation
</button>
<button
type="button"
onClick={onStart}
className="w-full rounded-lg px-6 py-2.5 text-gray-600 font-medium transition-colors hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-300 text-sm"
>
Skip and chat directly →
</button>
<p className="text-center text-xs text-gray-500">
You can upload documents and continue the conversation in the next step
</p>
</div>
</form>
);
};