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

127 lines
3.8 KiB
TypeScript

'use client';
import type { ReactNode } from 'react';
import {
ChatStarters,
ChatThread,
Composer,
type ChatMessageData,
type FooterInfo,
} from '@bitbaum/chatkit/react';
import { VOICE } from '@/components/forms/TextBox';
/** A source an answer was drawn from, shown under that answer. */
export interface ChatSourceItem {
label: string;
detail?: string;
}
/** A chat turn as botsmann keeps it: chatkit's message plus the sources it cites. */
export type EmbeddedChatMessage = ChatMessageData & { sources?: readonly ChatSourceItem[] };
interface EmbeddedChatProps {
messages: readonly EmbeddedChatMessage[];
onSend: (text: string) => void;
sending: boolean;
placeholder: string;
onRetry?: () => void;
onStop?: () => void;
/** Shown above the composer while the person has not asked anything yet. */
starters?: readonly string[];
/** Shown in place of the thread while there are no messages. */
empty?: ReactNode;
/** Why sending is not possible right now (e.g. no document uploaded yet). */
sendBlockedReason?: string | null;
/** A disclaimer or note above the composer. */
note?: ReactNode;
/** Height and frame of the panel; the thread scrolls inside it. */
className?: string;
renderFooter?: (message: EmbeddedChatMessage, info: FooterInfo) => ReactNode;
}
const Sources = ({ sources }: { sources: readonly ChatSourceItem[] }) => (
<div className="mt-2 text-xs text-gray-600">
<p className="font-medium text-gray-500 mb-1">Sources:</p>
<ul className="space-y-0.5">
{sources.map((source, i) => (
<li key={i}>
<span className="font-medium">{source.label}</span>
{source.detail && <span className="text-gray-400"> — {source.detail}</span>}
</li>
))}
</ul>
</div>
);
/**
* Botsmann's in-page chat panel: chatkit's thread and composer (microphone,
* copy, retry, markdown, suggested replies) inside a bounded card. Each surface
* keeps its own prompt, route and persistence and passes the turns in.
*/
export const EmbeddedChat = ({
messages,
onSend,
sending,
placeholder,
onRetry,
onStop,
starters,
empty,
sendBlockedReason,
note,
className = 'h-[500px]',
renderFooter,
}: EmbeddedChatProps) => {
const asked = messages.some((m) => m.role === 'user');
// Retry belongs to a failed turn, not to a welcome message or a good answer.
const lastFailed = messages.at(-1)?.failed === true;
const footer = (m: ChatMessageData, info: FooterInfo) => {
const message = m as EmbeddedChatMessage;
const own = renderFooter?.(message, info);
const sources =
message.role === 'assistant' && message.sources?.some((s) => s.label)
? message.sources.filter((s) => s.label)
: null;
if (!own && !sources) return null;
return (
<>
{sources && <Sources sources={sources} />}
{own}
</>
);
};
return (
<div className={`flex flex-col min-h-0 ${className}`}>
<div className="flex-1 min-h-0 overflow-y-auto px-3 py-4">
<ChatThread
messages={messages as ChatMessageData[]}
loading={sending}
onStop={onStop}
onRetry={lastFailed ? onRetry : undefined}
onReply={onSend}
renderFooter={footer}
empty={empty}
/>
</div>
<div className="border-t border-gray-100 p-3 space-y-2">
{!asked && starters && starters.length > 0 && !sendBlockedReason && (
<ChatStarters starters={starters.slice(0, 4)} onPick={onSend} title="Try asking:" />
)}
{note}
<Composer
onSend={(text) => {
onSend(text);
}}
placeholder={placeholder}
sending={sending}
onStop={onStop}
sendBlockedReason={sendBlockedReason ?? null}
density="compact"
voice={VOICE}
/>
</div>
</div>
);
};